]>
Commit | Line | Data |
---|---|---|
1 | #include <stdarg.h> | |
2 | #include <stdio.h> | |
3 | ||
4 | #define DBAPI2_ADAPTER | |
5 | #include "../dbapi/dbapi.h" | |
6 | ||
7 | #define DBAPI2_RESULT_HANDLE DBResult | |
8 | ||
9 | #include "../dbapi2/dbapi2.h" | |
10 | #include "../lib/stringbuf.h" | |
11 | #include "../lib/version.h" | |
12 | ||
13 | #include <string.h> | |
14 | ||
15 | static DBAPI2_HANDLE *dbapi2_adapter_new(const DBAPIConn *); | |
16 | static void dbapi2_adapter_close(DBAPIConn *); | |
17 | ||
18 | static void dbapi2_adapter_query(const DBAPIConn *, DBAPIQueryCallback, DBAPIUserData, const char *); | |
19 | static void dbapi2_adapter_createtable(const DBAPIConn *, DBAPIQueryCallback, DBAPIUserData, const char *); | |
20 | static void dbapi2_adapter_loadtable(const DBAPIConn *, DBAPIQueryCallback, DBAPIQueryCallback, DBAPIQueryCallback, DBAPIUserData data, const char *); | |
21 | ||
22 | static void dbapi2_adapter_escapestring(const DBAPIConn *, char *, const char *, size_t); | |
23 | static int dbapi2_adapter_quotestring(const DBAPIConn *, char *, size_t, const char *, size_t); | |
24 | ||
25 | static char *dbapi2_adapter_tablename(const DBAPIConn *, const char *); | |
26 | ||
27 | static void dbapi2_adapter_call(const DBAPIConn *, DBAPIQueryCallback, DBAPIUserData, const char *, const char *); | |
28 | ||
29 | static DBAPIProvider adapterprovider = { | |
30 | .new = dbapi2_adapter_new, | |
31 | .close = dbapi2_adapter_close, | |
32 | ||
33 | .query = dbapi2_adapter_query, | |
34 | .createtable = dbapi2_adapter_createtable, | |
35 | .loadtable = dbapi2_adapter_loadtable, | |
36 | ||
37 | .escapestring = dbapi2_adapter_escapestring, | |
38 | .quotestring = dbapi2_adapter_quotestring, | |
39 | ||
40 | .tablename = dbapi2_adapter_tablename, | |
41 | ||
42 | .call = dbapi2_adapter_call, | |
43 | }; | |
44 | ||
45 | struct DBAPI2AdapterQueryCallback { | |
46 | const DBAPIConn *db; | |
47 | DBAPIUserData data; | |
48 | DBAPIQueryCallback callback; | |
49 | }; | |
50 | ||
51 | struct DBAPI2AdapterLoadTableCallback { | |
52 | const DBAPIConn *db; | |
53 | DBAPIUserData data; | |
54 | DBAPIQueryCallback init, callback, fini; | |
55 | }; | |
56 | ||
57 | static int adapterhandle; | |
58 | ||
59 | static void registeradapterprovider(void) { | |
60 | adapterhandle = registerdbprovider(DBAPI2_ADAPTER_NAME, &adapterprovider); | |
61 | } | |
62 | ||
63 | static void deregisteradapterprovider(void) { | |
64 | deregisterdbprovider(adapterhandle); | |
65 | } | |
66 | ||
67 | static DBAPI2_HANDLE *dbapi2_adapter_new(const DBAPIConn *db) { | |
68 | long id = dbgetid(); | |
69 | ||
70 | dbattach(((DBAPIConn *)db)->name); | |
71 | ||
72 | return (void *)id; | |
73 | } | |
74 | ||
75 | static void dbapi2_adapter_close(DBAPIConn *db) { | |
76 | dbfreeid((int)(long)db->handle); | |
77 | dbdetach(db->name); | |
78 | } | |
79 | ||
80 | static char *dbapi2_adapter_result_get(const DBAPIResult *r, unsigned int column) { | |
81 | return dbgetvalue(r->handle, column); | |
82 | } | |
83 | ||
84 | static int dbapi2_adapter_result_next(const DBAPIResult *r) { | |
85 | return dbfetchrow(r->handle); | |
86 | } | |
87 | ||
88 | static void dbapi2_adapter_result_clear(const DBAPIResult *r) { | |
89 | if(!r) | |
90 | return; | |
91 | ||
92 | dbclear(r->handle); | |
93 | } | |
94 | ||
95 | static DBAPIResult *wrapresult(DBAPIResult *r, DBConn *c) { | |
96 | if(!c) | |
97 | return NULL; | |
98 | ||
99 | if(!r) { | |
100 | r = calloc(1, sizeof(DBAPIResult)); | |
101 | } else { | |
102 | memset(r, 0, sizeof(DBAPIResult)); | |
103 | } | |
104 | ||
105 | r->clear = dbapi2_adapter_result_clear; | |
106 | r->handle = dbgetresult(c); | |
107 | ||
108 | r->affected = dbnumaffected(c, r->handle); | |
109 | ||
110 | if(!dbquerysuccessful(r->handle)) | |
111 | return r; | |
112 | ||
113 | r->success = 1; | |
114 | r->fields = dbnumfields(r->handle); | |
115 | ||
116 | r->get = dbapi2_adapter_result_get; | |
117 | r->next = dbapi2_adapter_result_next; | |
118 | ||
119 | return r; | |
120 | } | |
121 | ||
122 | static void dbapi2_adapter_querywrapper(DBConn *c, void *data) { | |
123 | struct DBAPI2AdapterQueryCallback *a = data; | |
124 | DBAPIResult r; | |
125 | ||
126 | a->callback(wrapresult(&r, c), a->data); | |
127 | ||
128 | free(a); | |
129 | } | |
130 | ||
131 | static void sqquery(const DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, int flags, const char *query) { | |
132 | struct DBAPI2AdapterQueryCallback *a; | |
133 | ||
134 | if(cb) { | |
135 | a = malloc(sizeof(struct DBAPI2AdapterQueryCallback)); | |
136 | ||
137 | a->db = db; | |
138 | a->data = data; | |
139 | a->callback = cb; | |
140 | } else { | |
141 | a = NULL; | |
142 | } | |
143 | ||
144 | dbasyncqueryf((int)(long)db->handle, cb?dbapi2_adapter_querywrapper:NULL, a, flags, "%s", query); | |
145 | } | |
146 | ||
147 | static void dbapi2_adapter_query(const DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, const char *query) { | |
148 | sqquery(db, cb, data, 0, query); | |
149 | } | |
150 | ||
151 | static void dbapi2_adapter_createtable(const DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, const char *query) { | |
152 | sqquery(db, cb, data, DB_CREATE, query); | |
153 | } | |
154 | ||
155 | static void dbapi2_adapter_loadtablewrapper_init(DBConn *c, void *data) { | |
156 | struct DBAPI2AdapterLoadTableCallback *a = data; | |
157 | ||
158 | a->init(NULL, a->data); | |
159 | } | |
160 | ||
161 | static void dbapi2_adapter_loadtablewrapper_data(DBConn *c, void *data) { | |
162 | struct DBAPI2AdapterLoadTableCallback *a = data; | |
163 | DBAPIResult r; | |
164 | ||
165 | a->callback(wrapresult(&r, c), a->data); | |
166 | } | |
167 | ||
168 | static void dbapi2_adapter_loadtablewrapper_fini(DBConn *c, void *data) { | |
169 | struct DBAPI2AdapterLoadTableCallback *a = data; | |
170 | ||
171 | if(a->fini) | |
172 | a->fini(NULL, a->data); | |
173 | ||
174 | free(a); | |
175 | } | |
176 | ||
177 | static void dbapi2_adapter_loadtable(const DBAPIConn *db, DBAPIQueryCallback init, DBAPIQueryCallback cb, DBAPIQueryCallback final, DBAPIUserData data, const char *table) { | |
178 | struct DBAPI2AdapterLoadTableCallback *a = malloc(sizeof(struct DBAPI2AdapterLoadTableCallback)); | |
179 | ||
180 | a->db = db; | |
181 | a->data = data; | |
182 | ||
183 | a->init = init; | |
184 | a->callback = cb; | |
185 | a->fini = final; | |
186 | ||
187 | dbloadtable_tag((char *)table, init?dbapi2_adapter_loadtablewrapper_init:NULL, cb?dbapi2_adapter_loadtablewrapper_data:NULL, dbapi2_adapter_loadtablewrapper_fini, a); | |
188 | } | |
189 | ||
190 | static void dbapi2_adapter_escapestring(const DBAPIConn *db, char *buf, const char *data, size_t len) { | |
191 | dbescapestring(buf, (char *)data, len); | |
192 | } | |
193 | ||
194 | #ifndef DBAPI2_CUSTOM_QUOTESTRING | |
195 | static int dbapi2_adapter_quotestring(const DBAPIConn *db, char *buf, size_t buflen, const char *data, size_t len) { | |
196 | StringBuf b; | |
197 | char xbuf[len * 2 + 5]; | |
198 | size_t esclen; | |
199 | ||
200 | sbinit(&b, buf, buflen); | |
201 | esclen = dbescapestring(xbuf, (char *)data, len); | |
202 | ||
203 | sbaddchar(&b, '\''); | |
204 | sbaddstrlen(&b, xbuf, esclen); | |
205 | sbaddchar(&b, '\''); | |
206 | ||
207 | if(!sbterminate(&b)) | |
208 | return 0; | |
209 | ||
210 | return 1; | |
211 | } | |
212 | #endif | |
213 | ||
214 | #ifndef DBAPI2_CUSTOM_TABLENAME | |
215 | static char *dbapi2_adapter_tablename(const DBAPIConn *db, const char *tablename) { | |
216 | static char buf[1024]; | |
217 | ||
218 | snprintf(buf, sizeof(buf), "%s.%s", db->name, tablename); | |
219 | ||
220 | return buf; | |
221 | } | |
222 | #endif | |
223 | ||
224 | static void dbapi2_adapter_call(const DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, const char *function, const char *query) { | |
225 | struct DBAPI2AdapterQueryCallback *a; | |
226 | char buf[512]; | |
227 | ||
228 | if(cb) { | |
229 | a = malloc(sizeof(struct DBAPI2AdapterQueryCallback)); | |
230 | ||
231 | a->db = db; | |
232 | a->data = data; | |
233 | a->callback = cb; | |
234 | } else { | |
235 | a = NULL; | |
236 | } | |
237 | ||
238 | snprintf(buf, sizeof(buf), "%s", db->tablename(db, function)); | |
239 | dbcall((int)(long)db->handle, cb?dbapi2_adapter_querywrapper:NULL, a, buf, query); | |
240 | } | |
241 |