@@ -20,11 +20,198 @@ package sqlite3
20
20
#endif
21
21
#include <stdlib.h>
22
22
#include <stdint.h>
23
+ #include <memory.h>
23
24
24
- int goSqlite3CreateModule(sqlite3 *db, const char *zName, uintptr_t pClientData);
25
+ static inline char *_sqlite3_mprintf(char *zFormat, char *arg) {
26
+ return sqlite3_mprintf(zFormat, arg);
27
+ }
28
+
29
+ typedef struct goVTab goVTab;
30
+
31
+ struct goVTab {
32
+ sqlite3_vtab base;
33
+ void *vTab;
34
+ };
35
+
36
+ uintptr_t goMInit(void *db, void *pAux, int argc, char **argv, char **pzErr, int isCreate);
37
+
38
+ static int cXInit(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr, int isCreate) {
39
+ void *vTab = (void *)goMInit(db, pAux, argc, (char**)argv, pzErr, isCreate);
40
+ if (!vTab || *pzErr) {
41
+ return SQLITE_ERROR;
42
+ }
43
+ goVTab *pvTab = (goVTab *)sqlite3_malloc(sizeof(goVTab));
44
+ if (!pvTab) {
45
+ *pzErr = sqlite3_mprintf("%s", "Out of memory");
46
+ return SQLITE_NOMEM;
47
+ }
48
+ memset(pvTab, 0, sizeof(goVTab));
49
+ pvTab->vTab = vTab;
50
+
51
+ *ppVTab = (sqlite3_vtab *)pvTab;
52
+ *pzErr = 0;
53
+ return SQLITE_OK;
54
+ }
55
+
56
+ static inline int cXCreate(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) {
57
+ return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 1);
58
+ }
59
+ static inline int cXConnect(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) {
60
+ return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 0);
61
+ }
62
+
63
+ char* goVBestIndex(void *pVTab, void *icp);
64
+
65
+ static inline int cXBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *info) {
66
+ char *pzErr = goVBestIndex(((goVTab*)pVTab)->vTab, info);
67
+ if (pzErr) {
68
+ if (pVTab->zErrMsg)
69
+ sqlite3_free(pVTab->zErrMsg);
70
+ pVTab->zErrMsg = pzErr;
71
+ return SQLITE_ERROR;
72
+ }
73
+ return SQLITE_OK;
74
+ }
75
+
76
+ char* goVRelease(void *pVTab, int isDestroy);
77
+
78
+ static int cXRelease(sqlite3_vtab *pVTab, int isDestroy) {
79
+ char *pzErr = goVRelease(((goVTab*)pVTab)->vTab, isDestroy);
80
+ if (pzErr) {
81
+ if (pVTab->zErrMsg)
82
+ sqlite3_free(pVTab->zErrMsg);
83
+ pVTab->zErrMsg = pzErr;
84
+ return SQLITE_ERROR;
85
+ }
86
+ if (pVTab->zErrMsg)
87
+ sqlite3_free(pVTab->zErrMsg);
88
+ sqlite3_free(pVTab);
89
+ return SQLITE_OK;
90
+ }
91
+
92
+ static inline int cXDisconnect(sqlite3_vtab *pVTab) {
93
+ return cXRelease(pVTab, 0);
94
+ }
95
+ static inline int cXDestroy(sqlite3_vtab *pVTab) {
96
+ return cXRelease(pVTab, 1);
97
+ }
98
+
99
+ typedef struct goVTabCursor goVTabCursor;
100
+
101
+ struct goVTabCursor {
102
+ sqlite3_vtab_cursor base;
103
+ void *vTabCursor;
104
+ };
105
+
106
+ uintptr_t goVOpen(void *pVTab, char **pzErr);
107
+
108
+ static int cXOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor) {
109
+ void *vTabCursor = (void *)goVOpen(((goVTab*)pVTab)->vTab, &(pVTab->zErrMsg));
110
+ goVTabCursor *pCursor = (goVTabCursor *)sqlite3_malloc(sizeof(goVTabCursor));
111
+ if (!pCursor) {
112
+ return SQLITE_NOMEM;
113
+ }
114
+ memset(pCursor, 0, sizeof(goVTabCursor));
115
+ pCursor->vTabCursor = vTabCursor;
116
+ *ppCursor = (sqlite3_vtab_cursor *)pCursor;
117
+ return SQLITE_OK;
118
+ }
119
+
120
+ static int setErrMsg(sqlite3_vtab_cursor *pCursor, char *pzErr) {
121
+ if (pCursor->pVtab->zErrMsg)
122
+ sqlite3_free(pCursor->pVtab->zErrMsg);
123
+ pCursor->pVtab->zErrMsg = pzErr;
124
+ return SQLITE_ERROR;
125
+ }
126
+
127
+ char* goVClose(void *pCursor);
128
+
129
+ static int cXClose(sqlite3_vtab_cursor *pCursor) {
130
+ char *pzErr = goVClose(((goVTabCursor*)pCursor)->vTabCursor);
131
+ if (pzErr) {
132
+ return setErrMsg(pCursor, pzErr);
133
+ }
134
+ sqlite3_free(pCursor);
135
+ return SQLITE_OK;
136
+ }
137
+
138
+ char* goVFilter(void *pCursor, int idxNum, char* idxName, int argc, sqlite3_value **argv);
139
+
140
+ static int cXFilter(sqlite3_vtab_cursor *pCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) {
141
+ char *pzErr = goVFilter(((goVTabCursor*)pCursor)->vTabCursor, idxNum, (char*)idxStr, argc, argv);
142
+ if (pzErr) {
143
+ return setErrMsg(pCursor, pzErr);
144
+ }
145
+ return SQLITE_OK;
146
+ }
147
+
148
+ char* goVNext(void *pCursor);
149
+
150
+ static int cXNext(sqlite3_vtab_cursor *pCursor) {
151
+ char *pzErr = goVNext(((goVTabCursor*)pCursor)->vTabCursor);
152
+ if (pzErr) {
153
+ return setErrMsg(pCursor, pzErr);
154
+ }
155
+ return SQLITE_OK;
156
+ }
157
+
158
+ int goVEof(void *pCursor);
159
+
160
+ static inline int cXEof(sqlite3_vtab_cursor *pCursor) {
161
+ return goVEof(((goVTabCursor*)pCursor)->vTabCursor);
162
+ }
163
+
164
+ char* goVColumn(void *pCursor, void *cp, int col);
165
+
166
+ static int cXColumn(sqlite3_vtab_cursor *pCursor, sqlite3_context *ctx, int i) {
167
+ char *pzErr = goVColumn(((goVTabCursor*)pCursor)->vTabCursor, ctx, i);
168
+ if (pzErr) {
169
+ return setErrMsg(pCursor, pzErr);
170
+ }
171
+ return SQLITE_OK;
172
+ }
173
+
174
+ char* goVRowid(void *pCursor, sqlite3_int64 *pRowid);
175
+
176
+ static int cXRowid(sqlite3_vtab_cursor *pCursor, sqlite3_int64 *pRowid) {
177
+ char *pzErr = goVRowid(((goVTabCursor*)pCursor)->vTabCursor, pRowid);
178
+ if (pzErr) {
179
+ return setErrMsg(pCursor, pzErr);
180
+ }
181
+ return SQLITE_OK;
182
+ }
25
183
26
- static inline char *my_mprintf(char *zFormat, char *arg) {
27
- return sqlite3_mprintf(zFormat, arg);
184
+ static sqlite3_module goModule = {
185
+ 0, // iVersion
186
+ cXCreate, // xCreate - create a table
187
+ cXConnect, // xConnect - connect to an existing table
188
+ cXBestIndex, // xBestIndex - Determine search strategy
189
+ cXDisconnect, // xDisconnect - Disconnect from a table
190
+ cXDestroy, // xDestroy - Drop a table
191
+ cXOpen, // xOpen - open a cursor
192
+ cXClose, // xClose - close a cursor
193
+ cXFilter, // xFilter - configure scan constraints
194
+ cXNext, // xNext - advance a cursor
195
+ cXEof, // xEof
196
+ cXColumn, // xColumn - read data
197
+ cXRowid, // xRowid - read data
198
+ // Not implemented
199
+ 0, // xUpdate - write data
200
+ 0, // xBegin - begin transaction
201
+ 0, // xSync - sync transaction
202
+ 0, // xCommit - commit transaction
203
+ 0, // xRollback - rollback transaction
204
+ 0, // xFindFunction - function overloading
205
+ 0, // xRename - rename the table
206
+ 0, // xSavepoint
207
+ 0, // xRelease
208
+ 0 // xRollbackTo
209
+ };
210
+
211
+ void goMDestroy(void*);
212
+
213
+ static int _sqlite3_create_module(sqlite3 *db, const char *zName, uintptr_t pClientData) {
214
+ return sqlite3_create_module_v2(db, zName, &goModule, (void*) pClientData, goMDestroy);
28
215
}
29
216
*/
30
217
import "C"
@@ -136,19 +323,19 @@ func mPrintf(format, arg string) *C.char {
136
323
defer C .free (unsafe .Pointer (cf ))
137
324
ca := C .CString (arg )
138
325
defer C .free (unsafe .Pointer (ca ))
139
- return C .my_mprintf (cf , ca )
326
+ return C ._sqlite3_mprintf (cf , ca )
140
327
}
141
328
142
329
//export goMInit
143
- func goMInit (db , pClientData unsafe.Pointer , argc int , argv * * C.char , pzErr * * C.char , isCreate int ) C.uintptr_t {
330
+ func goMInit (db , pClientData unsafe.Pointer , argc C. int , argv * * C.char , pzErr * * C.char , isCreate C. int ) C.uintptr_t {
144
331
m := lookupHandle (uintptr (pClientData )).(* sqliteModule )
145
332
if m .c .db != (* C .sqlite3 )(db ) {
146
333
* pzErr = mPrintf ("%s" , "Inconsistent db handles" )
147
334
return 0
148
335
}
149
336
args := make ([]string , argc )
150
337
var A []* C.char
151
- slice := reflect.SliceHeader {Data : uintptr (unsafe .Pointer (argv )), Len : argc , Cap : argc }
338
+ slice := reflect.SliceHeader {Data : uintptr (unsafe .Pointer (argv )), Len : int ( argc ) , Cap : int ( argc ) }
152
339
a := reflect .NewAt (reflect .TypeOf (A ), unsafe .Pointer (& slice )).Elem ().Interface ()
153
340
for i , s := range a .([]* C.char ) {
154
341
args [i ] = C .GoString (s )
@@ -171,7 +358,7 @@ func goMInit(db, pClientData unsafe.Pointer, argc int, argv **C.char, pzErr **C.
171
358
}
172
359
173
360
//export goVRelease
174
- func goVRelease (pVTab unsafe.Pointer , isDestroy int ) * C.char {
361
+ func goVRelease (pVTab unsafe.Pointer , isDestroy C. int ) * C.char {
175
362
vt := lookupHandle (uintptr (pVTab )).(* sqliteVTab )
176
363
var err error
177
364
if isDestroy == 1 {
@@ -254,7 +441,7 @@ func goMDestroy(pClientData unsafe.Pointer) {
254
441
}
255
442
256
443
//export goVFilter
257
- func goVFilter (pCursor unsafe.Pointer , idxNum int , idxName * C.char , argc int , argv * * C.sqlite3_value ) * C.char {
444
+ func goVFilter (pCursor unsafe.Pointer , idxNum C. int , idxName * C.char , argc C. int , argv * * C.sqlite3_value ) * C.char {
258
445
vtc := lookupHandle (uintptr (pCursor )).(* sqliteVTabCursor )
259
446
args := (* [(math .MaxInt32 - 1 ) / unsafe .Sizeof ((* C .sqlite3_value )(nil ))]* C.sqlite3_value )(unsafe .Pointer (argv ))[:argc :argc ]
260
447
vals := make ([]interface {}, 0 , argc )
@@ -265,7 +452,7 @@ func goVFilter(pCursor unsafe.Pointer, idxNum int, idxName *C.char, argc int, ar
265
452
}
266
453
vals = append (vals , conv .Interface ())
267
454
}
268
- err := vtc .vTabCursor .Filter (idxNum , C .GoString (idxName ), vals )
455
+ err := vtc .vTabCursor .Filter (int ( idxNum ) , C .GoString (idxName ), vals )
269
456
if err != nil {
270
457
return mPrintf ("%s" , err .Error ())
271
458
}
@@ -293,10 +480,10 @@ func goVEof(pCursor unsafe.Pointer) C.int {
293
480
}
294
481
295
482
//export goVColumn
296
- func goVColumn (pCursor , cp unsafe.Pointer , col int ) * C.char {
483
+ func goVColumn (pCursor , cp unsafe.Pointer , col C. int ) * C.char {
297
484
vtc := lookupHandle (uintptr (pCursor )).(* sqliteVTabCursor )
298
485
c := (* SQLiteContext )(cp )
299
- err := vtc .vTabCursor .Column (c , col )
486
+ err := vtc .vTabCursor .Column (c , int ( col ) )
300
487
if err != nil {
301
488
return mPrintf ("%s" , err .Error ())
302
489
}
@@ -373,7 +560,7 @@ func (c *SQLiteConn) CreateModule(moduleName string, module Module) error {
373
560
mname := C .CString (moduleName )
374
561
defer C .free (unsafe .Pointer (mname ))
375
562
udm := sqliteModule {c , moduleName , module }
376
- rv := C .goSqlite3CreateModule (c .db , mname , C .uintptr_t (newHandle (c , & udm )))
563
+ rv := C ._sqlite3_create_module (c .db , mname , C .uintptr_t (newHandle (c , & udm )))
377
564
if rv != C .SQLITE_OK {
378
565
return c .lastError ()
379
566
}
0 commit comments