| 1 |
// Copyright (C) 2019 Yasuhiro Matsumoto <[email protected]>. |
| 2 |
// |
| 3 |
// Use of this source code is governed by an MIT-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
//go:build sqlite_vtable || vtable |
| 7 |
|
| 8 |
package sqlite3 |
| 9 |
|
| 10 |
/* |
| 11 |
#cgo CFLAGS: -std=gnu99 |
| 12 |
#cgo CFLAGS: -DSQLITE_ENABLE_RTREE |
| 13 |
#cgo CFLAGS: -DSQLITE_THREADSAFE |
| 14 |
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3 |
| 15 |
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS |
| 16 |
#cgo CFLAGS: -DSQLITE_ENABLE_FTS4_UNICODE61 |
| 17 |
#cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15 |
| 18 |
#cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA=1 |
| 19 |
#cgo CFLAGS: -Wno-deprecated-declarations |
| 20 |
|
| 21 |
#ifndef USE_LIBSQLITE3 |
| 22 |
#include "sqlite3-binding.h" |
| 23 |
#else |
| 24 |
#include <sqlite3.h> |
| 25 |
#endif |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdint.h> |
| 28 |
#include <memory.h> |
| 29 |
|
| 30 |
static inline char *_sqlite3_mprintf(char *zFormat, char *arg) { |
| 31 |
return sqlite3_mprintf(zFormat, arg); |
| 32 |
} |
| 33 |
|
| 34 |
typedef struct goVTab goVTab; |
| 35 |
|
| 36 |
struct goVTab { |
| 37 |
sqlite3_vtab base; |
| 38 |
void *vTab; |
| 39 |
}; |
| 40 |
|
| 41 |
uintptr_t goMInit(void *db, void *pAux, int argc, char **argv, char **pzErr, int isCreate); |
| 42 |
|
| 43 |
static int cXInit(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr, int isCreate) { |
| 44 |
void *vTab = (void *)goMInit(db, pAux, argc, (char**)argv, pzErr, isCreate); |
| 45 |
if (!vTab || *pzErr) { |
| 46 |
return SQLITE_ERROR; |
| 47 |
} |
| 48 |
goVTab *pvTab = (goVTab *)sqlite3_malloc(sizeof(goVTab)); |
| 49 |
if (!pvTab) { |
| 50 |
*pzErr = sqlite3_mprintf("%s", "Out of memory"); |
| 51 |
return SQLITE_NOMEM; |
| 52 |
} |
| 53 |
memset(pvTab, 0, sizeof(goVTab)); |
| 54 |
pvTab->vTab = vTab; |
| 55 |
|
| 56 |
*ppVTab = (sqlite3_vtab *)pvTab; |
| 57 |
*pzErr = 0; |
| 58 |
return SQLITE_OK; |
| 59 |
} |
| 60 |
|
| 61 |
static inline int cXCreate(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) { |
| 62 |
return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 1); |
| 63 |
} |
| 64 |
static inline int cXConnect(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) { |
| 65 |
return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 0); |
| 66 |
} |
| 67 |
|
| 68 |
char* goVBestIndex(void *pVTab, void *icp); |
| 69 |
|
| 70 |
static inline int cXBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *info) { |
| 71 |
char *pzErr = goVBestIndex(((goVTab*)pVTab)->vTab, info); |
| 72 |
if (pzErr) { |
| 73 |
if (pVTab->zErrMsg) |
| 74 |
sqlite3_free(pVTab->zErrMsg); |
| 75 |
pVTab->zErrMsg = pzErr; |
| 76 |
return SQLITE_ERROR; |
| 77 |
} |
| 78 |
return SQLITE_OK; |
| 79 |
} |
| 80 |
|
| 81 |
char* goVRelease(void *pVTab, int isDestroy); |
| 82 |
|
| 83 |
static int cXRelease(sqlite3_vtab *pVTab, int isDestroy) { |
| 84 |
char *pzErr = goVRelease(((goVTab*)pVTab)->vTab, isDestroy); |
| 85 |
if (pzErr) { |
| 86 |
if (pVTab->zErrMsg) |
| 87 |
sqlite3_free(pVTab->zErrMsg); |
| 88 |
pVTab->zErrMsg = pzErr; |
| 89 |
return SQLITE_ERROR; |
| 90 |
} |
| 91 |
if (pVTab->zErrMsg) |
| 92 |
sqlite3_free(pVTab->zErrMsg); |
| 93 |
sqlite3_free(pVTab); |
| 94 |
return SQLITE_OK; |
| 95 |
} |
| 96 |
|
| 97 |
static inline int cXDisconnect(sqlite3_vtab *pVTab) { |
| 98 |
return cXRelease(pVTab, 0); |
| 99 |
} |
| 100 |
static inline int cXDestroy(sqlite3_vtab *pVTab) { |
| 101 |
return cXRelease(pVTab, 1); |
| 102 |
} |
| 103 |
|
| 104 |
typedef struct goVTabCursor goVTabCursor; |
| 105 |
|
| 106 |
struct goVTabCursor { |
| 107 |
sqlite3_vtab_cursor base; |
| 108 |
void *vTabCursor; |
| 109 |
}; |
| 110 |
|
| 111 |
uintptr_t goVOpen(void *pVTab, char **pzErr); |
| 112 |
|
| 113 |
static int cXOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor) { |
| 114 |
void *vTabCursor = (void *)goVOpen(((goVTab*)pVTab)->vTab, &(pVTab->zErrMsg)); |
| 115 |
if (!vTabCursor) { |
| 116 |
return SQLITE_ERROR; |
| 117 |
} |
| 118 |
goVTabCursor *pCursor = (goVTabCursor *)sqlite3_malloc(sizeof(goVTabCursor)); |
| 119 |
if (!pCursor) { |
| 120 |
return SQLITE_NOMEM; |
| 121 |
} |
| 122 |
memset(pCursor, 0, sizeof(goVTabCursor)); |
| 123 |
pCursor->vTabCursor = vTabCursor; |
| 124 |
*ppCursor = (sqlite3_vtab_cursor *)pCursor; |
| 125 |
return SQLITE_OK; |
| 126 |
} |
| 127 |
|
| 128 |
static int setErrMsg(sqlite3_vtab_cursor *pCursor, char *pzErr) { |
| 129 |
if (pCursor->pVtab->zErrMsg) |
| 130 |
sqlite3_free(pCursor->pVtab->zErrMsg); |
| 131 |
pCursor->pVtab->zErrMsg = pzErr; |
| 132 |
return SQLITE_ERROR; |
| 133 |
} |
| 134 |
|
| 135 |
char* goVClose(void *pCursor); |
| 136 |
|
| 137 |
static int cXClose(sqlite3_vtab_cursor *pCursor) { |
| 138 |
char *pzErr = goVClose(((goVTabCursor*)pCursor)->vTabCursor); |
| 139 |
if (pzErr) { |
| 140 |
return setErrMsg(pCursor, pzErr); |
| 141 |
} |
| 142 |
sqlite3_free(pCursor); |
| 143 |
return SQLITE_OK; |
| 144 |
} |
| 145 |
|
| 146 |
char* goVFilter(void *pCursor, int idxNum, char* idxName, int argc, sqlite3_value **argv); |
| 147 |
|
| 148 |
static int cXFilter(sqlite3_vtab_cursor *pCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) { |
| 149 |
char *pzErr = goVFilter(((goVTabCursor*)pCursor)->vTabCursor, idxNum, (char*)idxStr, argc, argv); |
| 150 |
if (pzErr) { |
| 151 |
return setErrMsg(pCursor, pzErr); |
| 152 |
} |
| 153 |
return SQLITE_OK; |
| 154 |
} |
| 155 |
|
| 156 |
char* goVNext(void *pCursor); |
| 157 |
|
| 158 |
static int cXNext(sqlite3_vtab_cursor *pCursor) { |
| 159 |
char *pzErr = goVNext(((goVTabCursor*)pCursor)->vTabCursor); |
| 160 |
if (pzErr) { |
| 161 |
return setErrMsg(pCursor, pzErr); |
| 162 |
} |
| 163 |
return SQLITE_OK; |
| 164 |
} |
| 165 |
|
| 166 |
int goVEof(void *pCursor); |
| 167 |
|
| 168 |
static inline int cXEof(sqlite3_vtab_cursor *pCursor) { |
| 169 |
return goVEof(((goVTabCursor*)pCursor)->vTabCursor); |
| 170 |
} |
| 171 |
|
| 172 |
char* goVColumn(void *pCursor, void *cp, int col); |
| 173 |
|
| 174 |
static int cXColumn(sqlite3_vtab_cursor *pCursor, sqlite3_context *ctx, int i) { |
| 175 |
char *pzErr = goVColumn(((goVTabCursor*)pCursor)->vTabCursor, ctx, i); |
| 176 |
if (pzErr) { |
| 177 |
return setErrMsg(pCursor, pzErr); |
| 178 |
} |
| 179 |
return SQLITE_OK; |
| 180 |
} |
| 181 |
|
| 182 |
char* goVRowid(void *pCursor, sqlite3_int64 *pRowid); |
| 183 |
|
| 184 |
static int cXRowid(sqlite3_vtab_cursor *pCursor, sqlite3_int64 *pRowid) { |
| 185 |
char *pzErr = goVRowid(((goVTabCursor*)pCursor)->vTabCursor, pRowid); |
| 186 |
if (pzErr) { |
| 187 |
return setErrMsg(pCursor, pzErr); |
| 188 |
} |
| 189 |
return SQLITE_OK; |
| 190 |
} |
| 191 |
|
| 192 |
char* goVUpdate(void *pVTab, int argc, sqlite3_value **argv, sqlite3_int64 *pRowid); |
| 193 |
|
| 194 |
static int cXUpdate(sqlite3_vtab *pVTab, int argc, sqlite3_value **argv, sqlite3_int64 *pRowid) { |
| 195 |
char *pzErr = goVUpdate(((goVTab*)pVTab)->vTab, argc, argv, pRowid); |
| 196 |
if (pzErr) { |
| 197 |
if (pVTab->zErrMsg) |
| 198 |
sqlite3_free(pVTab->zErrMsg); |
| 199 |
pVTab->zErrMsg = pzErr; |
| 200 |
return SQLITE_ERROR; |
| 201 |
} |
| 202 |
return SQLITE_OK; |
| 203 |
} |
| 204 |
|
| 205 |
static sqlite3_module goModule = { |
| 206 |
0, // iVersion |
| 207 |
cXCreate, // xCreate - create a table |
| 208 |
cXConnect, // xConnect - connect to an existing table |
| 209 |
cXBestIndex, // xBestIndex - Determine search strategy |
| 210 |
cXDisconnect, // xDisconnect - Disconnect from a table |
| 211 |
cXDestroy, // xDestroy - Drop a table |
| 212 |
cXOpen, // xOpen - open a cursor |
| 213 |
cXClose, // xClose - close a cursor |
| 214 |
cXFilter, // xFilter - configure scan constraints |
| 215 |
cXNext, // xNext - advance a cursor |
| 216 |
cXEof, // xEof |
| 217 |
cXColumn, // xColumn - read data |
| 218 |
cXRowid, // xRowid - read data |
| 219 |
cXUpdate, // xUpdate - write data |
| 220 |
// Not implemented |
| 221 |
0, // xBegin - begin transaction |
| 222 |
0, // xSync - sync transaction |
| 223 |
0, // xCommit - commit transaction |
| 224 |
0, // xRollback - rollback transaction |
| 225 |
0, // xFindFunction - function overloading |
| 226 |
0, // xRename - rename the table |
| 227 |
0, // xSavepoint |
| 228 |
0, // xRelease |
| 229 |
0 // xRollbackTo |
| 230 |
}; |
| 231 |
|
| 232 |
// See https://sqlite.org/vtab.html#eponymous_only_virtual_tables |
| 233 |
static sqlite3_module goModuleEponymousOnly = { |
| 234 |
0, // iVersion |
| 235 |
0, // xCreate - create a table, which here is null |
| 236 |
cXConnect, // xConnect - connect to an existing table |
| 237 |
cXBestIndex, // xBestIndex - Determine search strategy |
| 238 |
cXDisconnect, // xDisconnect - Disconnect from a table |
| 239 |
cXDestroy, // xDestroy - Drop a table |
| 240 |
cXOpen, // xOpen - open a cursor |
| 241 |
cXClose, // xClose - close a cursor |
| 242 |
cXFilter, // xFilter - configure scan constraints |
| 243 |
cXNext, // xNext - advance a cursor |
| 244 |
cXEof, // xEof |
| 245 |
cXColumn, // xColumn - read data |
| 246 |
cXRowid, // xRowid - read data |
| 247 |
cXUpdate, // xUpdate - write data |
| 248 |
// Not implemented |
| 249 |
0, // xBegin - begin transaction |
| 250 |
0, // xSync - sync transaction |
| 251 |
0, // xCommit - commit transaction |
| 252 |
0, // xRollback - rollback transaction |
| 253 |
0, // xFindFunction - function overloading |
| 254 |
0, // xRename - rename the table |
| 255 |
0, // xSavepoint |
| 256 |
0, // xRelease |
| 257 |
0 // xRollbackTo |
| 258 |
}; |
| 259 |
|
| 260 |
void goMDestroy(void*); |
| 261 |
|
| 262 |
static int _sqlite3_create_module(sqlite3 *db, const char *zName, uintptr_t pClientData) { |
| 263 |
return sqlite3_create_module_v2(db, zName, &goModule, (void*) pClientData, goMDestroy); |
| 264 |
} |
| 265 |
|
| 266 |
static int _sqlite3_create_module_eponymous_only(sqlite3 *db, const char *zName, uintptr_t pClientData) { |
| 267 |
return sqlite3_create_module_v2(db, zName, &goModuleEponymousOnly, (void*) pClientData, goMDestroy); |
| 268 |
} |
| 269 |
*/ |
| 270 |
import "C" |
| 271 |
|
| 272 |
import ( |
| 273 |
"fmt" |
| 274 |
"math" |
| 275 |
"unsafe" |
| 276 |
) |
| 277 |
|
| 278 |
type sqliteModule struct { |
| 279 |
c *SQLiteConn |
| 280 |
name string |
| 281 |
module Module |
| 282 |
} |
| 283 |
|
| 284 |
type sqliteVTab struct { |
| 285 |
module *sqliteModule |
| 286 |
vTab VTab |
| 287 |
} |
| 288 |
|
| 289 |
type sqliteVTabCursor struct { |
| 290 |
vTab *sqliteVTab |
| 291 |
vTabCursor VTabCursor |
| 292 |
} |
| 293 |
|
| 294 |
// Op is type of operations. |
| 295 |
type Op uint8 |
| 296 |
|
| 297 |
// Op mean identity of operations. |
| 298 |
const ( |
| 299 |
OpEQ Op = 2 |
| 300 |
OpGT = 4 |
| 301 |
OpLE = 8 |
| 302 |
OpLT = 16 |
| 303 |
OpGE = 32 |
| 304 |
OpMATCH = 64 |
| 305 |
OpLIKE = 65 /* 3.10.0 and later only */ |
| 306 |
OpGLOB = 66 /* 3.10.0 and later only */ |
| 307 |
OpREGEXP = 67 /* 3.10.0 and later only */ |
| 308 |
OpNE = 68 /* 3.21.0 and later only */ |
| 309 |
OpISNOT = 69 /* 3.21.0 and later */ |
| 310 |
OpISNOTNULL = 70 /* 3.21.0 and later */ |
| 311 |
OpISNULL = 71 /* 3.21.0 and later */ |
| 312 |
OpIS = 72 /* 3.21.0 and later */ |
| 313 |
OpLIMIT = 73 /* 3.38.0 and later */ |
| 314 |
OpOFFSET = 74 /* 3.38.0 and later */ |
| 315 |
OpFUNCTION = 150 /* 3.25.0 and later */ |
| 316 |
OpScanUnique = 1 /* Scan visits at most 1 row */ |
| 317 |
) |
| 318 |
|
| 319 |
// InfoConstraint give information of constraint. |
| 320 |
type InfoConstraint struct { |
| 321 |
Column int |
| 322 |
Op Op |
| 323 |
Usable bool |
| 324 |
} |
| 325 |
|
| 326 |
// InfoOrderBy give information of order-by. |
| 327 |
type InfoOrderBy struct { |
| 328 |
Column int |
| 329 |
Desc bool |
| 330 |
} |
| 331 |
|
| 332 |
func constraints(info *C.sqlite3_index_info) []InfoConstraint { |
| 333 |
slice := unsafe.Slice(info.aConstraint, int(info.nConstraint)) |
| 334 |
|
| 335 |
cst := make([]InfoConstraint, 0, len(slice)) |
| 336 |
for _, c := range slice { |
| 337 |
var usable bool |
| 338 |
if c.usable > 0 { |
| 339 |
usable = true |
| 340 |
} |
| 341 |
cst = append(cst, InfoConstraint{ |
| 342 |
Column: int(c.iColumn), |
| 343 |
Op: Op(c.op), |
| 344 |
Usable: usable, |
| 345 |
}) |
| 346 |
} |
| 347 |
return cst |
| 348 |
} |
| 349 |
|
| 350 |
func orderBys(info *C.sqlite3_index_info) []InfoOrderBy { |
| 351 |
slice := unsafe.Slice(info.aOrderBy, int(info.nOrderBy)) |
| 352 |
|
| 353 |
ob := make([]InfoOrderBy, 0, len(slice)) |
| 354 |
for _, c := range slice { |
| 355 |
var desc bool |
| 356 |
if c.desc > 0 { |
| 357 |
desc = true |
| 358 |
} |
| 359 |
ob = append(ob, InfoOrderBy{ |
| 360 |
Column: int(c.iColumn), |
| 361 |
Desc: desc, |
| 362 |
}) |
| 363 |
} |
| 364 |
return ob |
| 365 |
} |
| 366 |
|
| 367 |
// IndexResult is a Go struct representation of what eventually ends up in the |
| 368 |
// output fields for `sqlite3_index_info` |
| 369 |
// See: https://www.sqlite.org/c3ref/index_info.html |
| 370 |
type IndexResult struct { |
| 371 |
Used []bool // aConstraintUsage |
| 372 |
IdxNum int |
| 373 |
IdxStr string |
| 374 |
AlreadyOrdered bool // orderByConsumed |
| 375 |
EstimatedCost float64 |
| 376 |
EstimatedRows float64 |
| 377 |
} |
| 378 |
|
| 379 |
// mPrintf is a utility wrapper around sqlite3_mprintf |
| 380 |
func mPrintf(format, arg string) *C.char { |
| 381 |
cf := C.CString(format) |
| 382 |
defer C.free(unsafe.Pointer(cf)) |
| 383 |
ca := C.CString(arg) |
| 384 |
defer C.free(unsafe.Pointer(ca)) |
| 385 |
return C._sqlite3_mprintf(cf, ca) |
| 386 |
} |
| 387 |
|
| 388 |
//export goMInit |
| 389 |
func goMInit(db, pClientData unsafe.Pointer, argc C.int, argv **C.char, pzErr **C.char, isCreate C.int) C.uintptr_t { |
| 390 |
m := lookupHandle(pClientData).(*sqliteModule) |
| 391 |
if m.c.db != (*C.sqlite3)(db) { |
| 392 |
*pzErr = mPrintf("%s", "Inconsistent db handles") |
| 393 |
return 0 |
| 394 |
} |
| 395 |
args := make([]string, argc) |
| 396 |
for i, s := range unsafe.Slice(argv, int(argc)) { |
| 397 |
args[i] = C.GoString(s) |
| 398 |
} |
| 399 |
var vTab VTab |
| 400 |
var err error |
| 401 |
if isCreate == 1 { |
| 402 |
vTab, err = m.module.Create(m.c, args) |
| 403 |
} else { |
| 404 |
vTab, err = m.module.Connect(m.c, args) |
| 405 |
} |
| 406 |
|
| 407 |
if err != nil { |
| 408 |
*pzErr = mPrintf("%s", err.Error()) |
| 409 |
return 0 |
| 410 |
} |
| 411 |
vt := sqliteVTab{m, vTab} |
| 412 |
*pzErr = nil |
| 413 |
return C.uintptr_t(uintptr(newHandle(m.c, &vt))) |
| 414 |
} |
| 415 |
|
| 416 |
//export goVRelease |
| 417 |
func goVRelease(pVTab unsafe.Pointer, isDestroy C.int) *C.char { |
| 418 |
vt := lookupHandle(pVTab).(*sqliteVTab) |
| 419 |
var err error |
| 420 |
if isDestroy == 1 { |
| 421 |
err = vt.vTab.Destroy() |
| 422 |
} else { |
| 423 |
err = vt.vTab.Disconnect() |
| 424 |
} |
| 425 |
if err != nil && isDestroy == 1 { |
| 426 |
// SQLite retains p->pVtab when xDestroy fails and later calls |
| 427 |
// xDisconnect on the same object; keep the handle registered so |
| 428 |
// that call can resolve it (a missing handle panics above). |
| 429 |
return mPrintf("%s", err.Error()) |
| 430 |
} |
| 431 |
// On xDisconnect (success or error) SQLite discards the vtab object, |
| 432 |
// and on successful xDestroy it clears p->pVtab; release the handle |
| 433 |
// in both cases. |
| 434 |
deleteHandle(pVTab) |
| 435 |
if err != nil { |
| 436 |
return mPrintf("%s", err.Error()) |
| 437 |
} |
| 438 |
return nil |
| 439 |
} |
| 440 |
|
| 441 |
//export goVOpen |
| 442 |
func goVOpen(pVTab unsafe.Pointer, pzErr **C.char) C.uintptr_t { |
| 443 |
vt := lookupHandle(pVTab).(*sqliteVTab) |
| 444 |
vTabCursor, err := vt.vTab.Open() |
| 445 |
if err != nil { |
| 446 |
*pzErr = mPrintf("%s", err.Error()) |
| 447 |
return 0 |
| 448 |
} |
| 449 |
vtc := sqliteVTabCursor{vt, vTabCursor} |
| 450 |
*pzErr = nil |
| 451 |
return C.uintptr_t(uintptr(newHandle(vt.module.c, &vtc))) |
| 452 |
} |
| 453 |
|
| 454 |
//export goVBestIndex |
| 455 |
func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char { |
| 456 |
vt := lookupHandle(pVTab).(*sqliteVTab) |
| 457 |
info := (*C.sqlite3_index_info)(icp) |
| 458 |
csts := constraints(info) |
| 459 |
res, err := vt.vTab.BestIndex(csts, orderBys(info)) |
| 460 |
if err != nil { |
| 461 |
return mPrintf("%s", err.Error()) |
| 462 |
} |
| 463 |
if res == nil { |
| 464 |
return mPrintf("%s", "BestIndex returned a nil IndexResult") |
| 465 |
} |
| 466 |
if len(res.Used) != len(csts) { |
| 467 |
return mPrintf("Result.Used != expected value", "") |
| 468 |
} |
| 469 |
|
| 470 |
// Get a pointer to constraint_usage struct so we can update in place. |
| 471 |
|
| 472 |
slice := unsafe.Slice(info.aConstraintUsage, int(info.nConstraint)) |
| 473 |
index := 1 |
| 474 |
for i := range slice { |
| 475 |
// SQLite returns "xBestIndex malfunction" when an argvIndex is |
| 476 |
// assigned to a constraint it marked as not usable, so ignore |
| 477 |
// Used for those; they may become usable on a later xBestIndex |
| 478 |
// invocation for a different plan. |
| 479 |
if res.Used[i] && csts[i].Usable { |
| 480 |
slice[i].argvIndex = C.int(index) |
| 481 |
slice[i].omit = C.uchar(1) |
| 482 |
index++ |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
info.idxNum = C.int(res.IdxNum) |
| 487 |
info.idxStr = (*C.char)(C.sqlite3_malloc(C.int(len(res.IdxStr) + 1))) |
| 488 |
if info.idxStr == nil { |
| 489 |
// C.malloc and C.CString ordinarily do this for you. See https://golang.org/cmd/cgo/ |
| 490 |
panic("out of memory") |
| 491 |
} |
| 492 |
info.needToFreeIdxStr = C.int(1) |
| 493 |
|
| 494 |
idxStr := unsafe.Slice((*byte)(unsafe.Pointer(info.idxStr)), len(res.IdxStr)+1) |
| 495 |
copy(idxStr, res.IdxStr) |
| 496 |
idxStr[len(idxStr)-1] = 0 // null-terminated string |
| 497 |
|
| 498 |
if res.AlreadyOrdered { |
| 499 |
info.orderByConsumed = C.int(1) |
| 500 |
} |
| 501 |
// SQLite pre-initializes estimatedCost and estimatedRows with sensible |
| 502 |
// defaults; overwriting them with the Go zero value would make every |
| 503 |
// candidate plan look free and break query planning, so only pass |
| 504 |
// values the implementation actually set. |
| 505 |
if res.EstimatedCost > 0 { |
| 506 |
info.estimatedCost = C.double(res.EstimatedCost) |
| 507 |
} |
| 508 |
if res.EstimatedRows > 0 { |
| 509 |
var rows int64 |
| 510 |
if res.EstimatedRows >= float64(math.MaxInt64) { |
| 511 |
rows = math.MaxInt64 |
| 512 |
} else if rows = int64(res.EstimatedRows); rows < 1 { |
| 513 |
// A positive fractional estimate must not truncate to 0. |
| 514 |
rows = 1 |
| 515 |
} |
| 516 |
info.estimatedRows = C.sqlite3_int64(rows) |
| 517 |
} |
| 518 |
|
| 519 |
return nil |
| 520 |
} |
| 521 |
|
| 522 |
//export goVClose |
| 523 |
func goVClose(pCursor unsafe.Pointer) *C.char { |
| 524 |
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) |
| 525 |
err := vtc.vTabCursor.Close() |
| 526 |
// The cursor is gone as far as SQLite is concerned regardless of the |
| 527 |
// callback result, so release the handle either way. |
| 528 |
deleteHandle(pCursor) |
| 529 |
if err != nil { |
| 530 |
return mPrintf("%s", err.Error()) |
| 531 |
} |
| 532 |
return nil |
| 533 |
} |
| 534 |
|
| 535 |
//export goMDestroy |
| 536 |
func goMDestroy(pClientData unsafe.Pointer) { |
| 537 |
m := lookupHandle(pClientData).(*sqliteModule) |
| 538 |
m.module.DestroyModule() |
| 539 |
deleteHandle(pClientData) |
| 540 |
} |
| 541 |
|
| 542 |
//export goVFilter |
| 543 |
func goVFilter(pCursor unsafe.Pointer, idxNum C.int, idxName *C.char, argc C.int, argv **C.sqlite3_value) *C.char { |
| 544 |
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) |
| 545 |
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc] |
| 546 |
vals := make([]any, 0, argc) |
| 547 |
for _, v := range args { |
| 548 |
conv, err := callbackArgGeneric(v) |
| 549 |
if err != nil { |
| 550 |
return mPrintf("%s", err.Error()) |
| 551 |
} |
| 552 |
|
| 553 |
// work around for SQLITE_NULL |
| 554 |
x := conv.Interface() |
| 555 |
if z, ok := x.([]byte); ok && z == nil { |
| 556 |
x = nil |
| 557 |
} |
| 558 |
|
| 559 |
vals = append(vals, x) |
| 560 |
} |
| 561 |
err := vtc.vTabCursor.Filter(int(idxNum), C.GoString(idxName), vals) |
| 562 |
if err != nil { |
| 563 |
return mPrintf("%s", err.Error()) |
| 564 |
} |
| 565 |
return nil |
| 566 |
} |
| 567 |
|
| 568 |
//export goVNext |
| 569 |
func goVNext(pCursor unsafe.Pointer) *C.char { |
| 570 |
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) |
| 571 |
err := vtc.vTabCursor.Next() |
| 572 |
if err != nil { |
| 573 |
return mPrintf("%s", err.Error()) |
| 574 |
} |
| 575 |
return nil |
| 576 |
} |
| 577 |
|
| 578 |
//export goVEof |
| 579 |
func goVEof(pCursor unsafe.Pointer) C.int { |
| 580 |
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) |
| 581 |
err := vtc.vTabCursor.EOF() |
| 582 |
if err { |
| 583 |
return 1 |
| 584 |
} |
| 585 |
return 0 |
| 586 |
} |
| 587 |
|
| 588 |
//export goVColumn |
| 589 |
func goVColumn(pCursor, cp unsafe.Pointer, col C.int) *C.char { |
| 590 |
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) |
| 591 |
c := (*SQLiteContext)(cp) |
| 592 |
err := vtc.vTabCursor.Column(c, int(col)) |
| 593 |
if err != nil { |
| 594 |
return mPrintf("%s", err.Error()) |
| 595 |
} |
| 596 |
return nil |
| 597 |
} |
| 598 |
|
| 599 |
//export goVRowid |
| 600 |
func goVRowid(pCursor unsafe.Pointer, pRowid *C.sqlite3_int64) *C.char { |
| 601 |
vtc := lookupHandle(pCursor).(*sqliteVTabCursor) |
| 602 |
rowid, err := vtc.vTabCursor.Rowid() |
| 603 |
if err != nil { |
| 604 |
return mPrintf("%s", err.Error()) |
| 605 |
} |
| 606 |
*pRowid = C.sqlite3_int64(rowid) |
| 607 |
return nil |
| 608 |
} |
| 609 |
|
| 610 |
//export goVUpdate |
| 611 |
func goVUpdate(pVTab unsafe.Pointer, argc C.int, argv **C.sqlite3_value, pRowid *C.sqlite3_int64) *C.char { |
| 612 |
vt := lookupHandle(pVTab).(*sqliteVTab) |
| 613 |
|
| 614 |
var tname string |
| 615 |
if n, ok := vt.vTab.(interface { |
| 616 |
TableName() string |
| 617 |
}); ok { |
| 618 |
tname = n.TableName() + " " |
| 619 |
} |
| 620 |
|
| 621 |
err := fmt.Errorf("virtual %s table %sis read-only", vt.module.name, tname) |
| 622 |
if v, ok := vt.vTab.(VTabUpdater); ok { |
| 623 |
// convert argv |
| 624 |
args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc] |
| 625 |
vals := make([]any, 0, argc) |
| 626 |
for _, v := range args { |
| 627 |
conv, err := callbackArgGeneric(v) |
| 628 |
if err != nil { |
| 629 |
return mPrintf("%s", err.Error()) |
| 630 |
} |
| 631 |
|
| 632 |
// work around for SQLITE_NULL |
| 633 |
x := conv.Interface() |
| 634 |
if z, ok := x.([]byte); ok && z == nil { |
| 635 |
x = nil |
| 636 |
} |
| 637 |
|
| 638 |
vals = append(vals, x) |
| 639 |
} |
| 640 |
|
| 641 |
switch { |
| 642 |
case argc == 1: |
| 643 |
err = v.Delete(vals[0]) |
| 644 |
|
| 645 |
case argc > 1 && vals[0] == nil: |
| 646 |
var id int64 |
| 647 |
id, err = v.Insert(vals[1], vals[2:]) |
| 648 |
if err == nil { |
| 649 |
*pRowid = C.sqlite3_int64(id) |
| 650 |
} |
| 651 |
|
| 652 |
case argc > 1: |
| 653 |
// Per the xUpdate contract argv[0] identifies the row being |
| 654 |
// updated while argv[1] is its new rowid. VTabUpdater has no |
| 655 |
// way to convey a rowid change, so reject it instead of |
| 656 |
// silently updating values under the old rowid. |
| 657 |
if vals[0] != vals[1] { |
| 658 |
err = fmt.Errorf("virtual %s table %sdoes not support changing the rowid", vt.module.name, tname) |
| 659 |
} else { |
| 660 |
err = v.Update(vals[0], vals[2:]) |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
if err != nil { |
| 666 |
return mPrintf("%s", err.Error()) |
| 667 |
} |
| 668 |
|
| 669 |
return nil |
| 670 |
} |
| 671 |
|
| 672 |
// Module is a "virtual table module", it defines the implementation of a |
| 673 |
// virtual tables. See: http://sqlite.org/c3ref/module.html |
| 674 |
type Module interface { |
| 675 |
// http://sqlite.org/vtab.html#xcreate |
| 676 |
Create(c *SQLiteConn, args []string) (VTab, error) |
| 677 |
// http://sqlite.org/vtab.html#xconnect |
| 678 |
Connect(c *SQLiteConn, args []string) (VTab, error) |
| 679 |
// http://sqlite.org/c3ref/create_module.html |
| 680 |
DestroyModule() |
| 681 |
} |
| 682 |
|
| 683 |
// EponymousOnlyModule is a "virtual table module" (as above), but |
| 684 |
// for defining "eponymous only" virtual tables See: https://sqlite.org/vtab.html#eponymous_only_virtual_tables |
| 685 |
type EponymousOnlyModule interface { |
| 686 |
Module |
| 687 |
EponymousOnlyModule() |
| 688 |
} |
| 689 |
|
| 690 |
// VTab describes a particular instance of the virtual table. |
| 691 |
// See: http://sqlite.org/c3ref/vtab.html |
| 692 |
type VTab interface { |
| 693 |
// http://sqlite.org/vtab.html#xbestindex |
| 694 |
BestIndex([]InfoConstraint, []InfoOrderBy) (*IndexResult, error) |
| 695 |
// http://sqlite.org/vtab.html#xdisconnect |
| 696 |
Disconnect() error |
| 697 |
// http://sqlite.org/vtab.html#sqlite3_module.xDestroy |
| 698 |
Destroy() error |
| 699 |
// http://sqlite.org/vtab.html#xopen |
| 700 |
Open() (VTabCursor, error) |
| 701 |
} |
| 702 |
|
| 703 |
// VTabUpdater is a type that allows a VTab to be inserted, updated, or |
| 704 |
// deleted. |
| 705 |
// See: https://sqlite.org/vtab.html#xupdate |
| 706 |
type VTabUpdater interface { |
| 707 |
Delete(any) error |
| 708 |
Insert(any, []any) (int64, error) |
| 709 |
Update(any, []any) error |
| 710 |
} |
| 711 |
|
| 712 |
// VTabCursor describes cursors that point into the virtual table and are used |
| 713 |
// to loop through the virtual table. See: http://sqlite.org/c3ref/vtab_cursor.html |
| 714 |
type VTabCursor interface { |
| 715 |
// http://sqlite.org/vtab.html#xclose |
| 716 |
Close() error |
| 717 |
// http://sqlite.org/vtab.html#xfilter |
| 718 |
Filter(idxNum int, idxStr string, vals []any) error |
| 719 |
// http://sqlite.org/vtab.html#xnext |
| 720 |
Next() error |
| 721 |
// http://sqlite.org/vtab.html#xeof |
| 722 |
EOF() bool |
| 723 |
// http://sqlite.org/vtab.html#xcolumn |
| 724 |
Column(c *SQLiteContext, col int) error |
| 725 |
// http://sqlite.org/vtab.html#xrowid |
| 726 |
Rowid() (int64, error) |
| 727 |
} |
| 728 |
|
| 729 |
// DeclareVTab declares the Schema of a virtual table. |
| 730 |
// See: http://sqlite.org/c3ref/declare_vtab.html |
| 731 |
func (c *SQLiteConn) DeclareVTab(sql string) error { |
| 732 |
zSQL := C.CString(sql) |
| 733 |
defer C.free(unsafe.Pointer(zSQL)) |
| 734 |
rv := C.sqlite3_declare_vtab(c.db, zSQL) |
| 735 |
if rv != C.SQLITE_OK { |
| 736 |
return c.lastError() |
| 737 |
} |
| 738 |
return nil |
| 739 |
} |
| 740 |
|
| 741 |
// CreateModule registers a virtual table implementation. |
| 742 |
// See: http://sqlite.org/c3ref/create_module.html |
| 743 |
func (c *SQLiteConn) CreateModule(moduleName string, module Module) error { |
| 744 |
mname := C.CString(moduleName) |
| 745 |
defer C.free(unsafe.Pointer(mname)) |
| 746 |
udm := sqliteModule{c, moduleName, module} |
| 747 |
switch module.(type) { |
| 748 |
case EponymousOnlyModule: |
| 749 |
rv := C._sqlite3_create_module_eponymous_only(c.db, mname, C.uintptr_t(uintptr(newHandle(c, &udm)))) |
| 750 |
if rv != C.SQLITE_OK { |
| 751 |
return c.lastError() |
| 752 |
} |
| 753 |
return nil |
| 754 |
case Module: |
| 755 |
rv := C._sqlite3_create_module(c.db, mname, C.uintptr_t(uintptr(newHandle(c, &udm)))) |
| 756 |
if rv != C.SQLITE_OK { |
| 757 |
return c.lastError() |
| 758 |
} |
| 759 |
return nil |
| 760 |
} |
| 761 |
return fmt.Errorf("sqlite3: CreateModule requires a non-nil module") |
| 762 |
} |