forked from nevali/libsql
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpg-engine.c
More file actions
135 lines (120 loc) · 2.52 KB
/
Copy pathpg-engine.c
File metadata and controls
135 lines (120 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright 2015 BBC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_postgres.h"
static void engine_alloc(void);
static pthread_once_t engine_control = PTHREAD_ONCE_INIT;
static SQL_ENGINE *engine;
static SQL_ENGINE_API pg_engine_api = {
sql_engine_def_queryinterface_,
sql_engine_def_addref_,
sql_engine_def_release_,
sql_engine_pg_create_
};
static SQL_API pg_api = {
sql_def_queryinterface_,
sql_def_addref_,
sql_pg_free_,
sql_def_lock_,
sql_def_unlock_,
sql_def_trylock_,
sql_pg_escape_,
sql_pg_sqlstate_,
sql_pg_error_,
sql_pg_connect_,
sql_pg_execute_,
sql_pg_statement_,
sql_pg_begin_,
sql_pg_commit_,
sql_pg_rollback_,
sql_pg_deadlocked_,
sql_pg_schema_get_version_,
sql_pg_schema_set_version_,
sql_pg_schema_create_table_,
sql_pg_set_querylog_,
sql_pg_set_errorlog_,
sql_pg_set_noticelog_,
sql_pg_lang_,
sql_pg_variant_,
sql_pg_set_userdata_,
sql_pg_userdata_
};
SQL_ENGINE *
sql_postgres_engine(void)
{
/* Create the engine instance as a singleton */
pthread_once(&engine_control, engine_alloc);
return engine;
}
static void
engine_alloc(void)
{
engine = (SQL_ENGINE *) calloc(1, sizeof(SQL_ENGINE));
if(!engine)
{
return;
}
engine->api = &pg_engine_api;
engine->refcount = 1;
}
SQL *
sql_engine_pg_create_(SQL_ENGINE *me)
{
SQL *inst;
(void) me;
inst = (SQL *) calloc(1, sizeof(SQL));
if(!inst)
{
return NULL;
}
inst->api = &pg_api;
inst->refcount = 1;
strcpy(inst->sqlstate, "00000");
strcpy(inst->error, "No error");
pthread_mutex_init(&(inst->lock), NULL);
return inst;
}
unsigned long
sql_pg_free_(SQL *me)
{
me->refcount--;
if(me->refcount)
{
return me->refcount;
}
pthread_mutex_destroy(&(me->lock));
if(me->pg)
{
PQfinish(me->pg);
}
free(me->qbuf);
free(me);
return 0;
}
int
sql_pg_set_userdata_(SQL *restrict me, void *restrict userdata)
{
me->userdata = userdata;
return 0;
}
void *
sql_pg_userdata_(SQL *me)
{
return me->userdata;
}