4 #include "../dbapi/dbapi.h"
6 #define DBAPI2_RESULT_HANDLE DBResult
8 #include "../dbapi2/dbapi2.h"
10 #include "../lib/stringbuf.h"
12 static DBAPI2_HANDLE *dbapi2_adapter_new(DBAPIConn *);
13 static void dbapi2_adapter_close(DBAPIConn *);
15 static void dbapi2_adapter_query(DBAPIConn *, DBAPIQueryCallback, DBAPIUserData, const char *, va_list);
16 static void dbapi2_adapter_createtable(DBAPIConn *, DBAPIQueryCallback, DBAPIUserData, const char *, ...);
17 static void dbapi2_adapter_loadtable(DBAPIConn *, DBAPIQueryCallback, DBAPIQueryCallback, DBAPIQueryCallback, DBAPIUserData data, const char *);
19 static void dbapi2_adapter_escapestring(DBAPIConn *, char *, const char *, size_t);
20 static int dbapi2_adapter_quotestring(DBAPIConn *, char *, size_t, const char *, size_t);
22 static DBAPIProvider adapterprovider = {
23 .new = dbapi2_adapter_new,
24 .close = dbapi2_adapter_close,
26 .query = dbapi2_adapter_query,
27 .createtable = dbapi2_adapter_createtable,
28 .loadtable = dbapi2_adapter_loadtable,
30 .escapestring = dbapi2_adapter_escapestring,
31 .quotestring = dbapi2_adapter_quotestring
34 struct DBAPI2AdapterQueryCallback {
37 DBAPIQueryCallback callback;
40 struct DBAPI2AdapterLoadTableCallback {
43 DBAPIQueryCallback init, callback, fini;
46 static int adapterhandle;
48 static void registeradapterprovider(void) {
49 adapterhandle = registerdbprovider(DBAPI2_ADAPTER_NAME, &adapterprovider);
52 static void deregisteradapterprovider(void) {
53 deregisterdbprovider(adapterhandle);
56 static DBAPI2_HANDLE *dbapi2_adapter_new(DBAPIConn *db) {
64 static void dbapi2_adapter_close(DBAPIConn *db) {
65 dbfreeid((int)(long)db->handle);
69 static char *dbapi2_adapter_result_get(DBAPIResult *r, int column) {
70 return dbgetvalue(r->handle, column);
73 static int dbapi2_adapter_result_next(DBAPIResult *r) {
74 return dbfetchrow(r->handle);
77 static void dbapi2_adapter_result_clear(DBAPIResult *r) {
84 static DBAPIResult *wrapresult(DBAPIResult *r, DBConn *c) {
89 r = calloc(1, sizeof(DBAPIResult));
91 r->clear = dbapi2_adapter_result_clear;
92 r->handle = dbgetresult(c);
94 if(!dbquerysuccessful(r->handle))
98 r->fields = dbnumfields(r->handle);
100 r->get = dbapi2_adapter_result_get;
101 r->next = dbapi2_adapter_result_next;
106 static void dbapi2_adapter_querywrapper(DBConn *c, void *data) {
107 struct DBAPI2AdapterQueryCallback *a = data;
108 DBAPIResult r_, *r = wrapresult(&r_, c);
110 a->callback(r, a->data);
115 static void sqquery(DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, const char *format, va_list ap, int flags) {
116 struct DBAPI2AdapterQueryCallback *a;
119 a = malloc(sizeof(struct DBAPI2AdapterQueryCallback));
128 dbasyncqueryfv((int)(long)db->handle, cb?dbapi2_adapter_querywrapper:NULL, a, flags, (char *)format, ap);
131 static void dbapi2_adapter_query(DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, const char *format, va_list ap) {
132 sqquery(db, cb, data, format, ap, 0);
135 static void dbapi2_adapter_createtable(DBAPIConn *db, DBAPIQueryCallback cb, DBAPIUserData data, const char *format, ...) {
138 va_start(ap, format);
139 sqquery(db, cb, data, format, ap, DB_CREATE);
143 static void dbapi2_adapter_loadtablewrapper_init(DBConn *c, void *data) {
144 struct DBAPI2AdapterLoadTableCallback *a = data;
147 a->init(NULL, a->data);
150 static void dbapi2_adapter_loadtablewrapper_data(DBConn *c, void *data) {
151 struct DBAPI2AdapterLoadTableCallback *a = data;
153 a->callback(NULL, a->data);
156 static void dbapi2_adapter_loadtablewrapper_fini(DBConn *c, void *data) {
157 struct DBAPI2AdapterLoadTableCallback *a = data;
160 a->fini(NULL, a->data);
165 static void dbapi2_adapter_loadtable(DBAPIConn *db, DBAPIQueryCallback init, DBAPIQueryCallback cb, DBAPIQueryCallback final, DBAPIUserData data, const char *table) {
166 struct DBAPI2AdapterLoadTableCallback *a = malloc(sizeof(struct DBAPI2AdapterLoadTableCallback));
175 dbloadtable_tag((char *)table, init?dbapi2_adapter_loadtablewrapper_init:NULL, cb?dbapi2_adapter_loadtablewrapper_data:NULL, dbapi2_adapter_loadtablewrapper_fini, a);
178 static void dbapi2_adapter_escapestring(DBAPIConn *db, char *buf, const char *data, size_t len) {
179 dbescapestring(buf, (char *)data, len);
182 #ifndef DBAPI2_CUSTOM_QUOTESTRING
183 static int dbapi2_adapter_quotestring(DBAPIConn *db, char *buf, size_t buflen, const char *data, size_t len) {
185 sbinit(&b, buf, buflen);
186 char xbuf[len * 2 + 5];
189 esclen = dbescapestring(xbuf, (char *)data, len);
192 sbaddstrlen(&b, xbuf, esclen);