本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

651 lines
17KB

  1. // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. // +build sqlite_vtable vtable
  6. package sqlite3
  7. /*
  8. #cgo CFLAGS: -std=gnu99
  9. #cgo CFLAGS: -DSQLITE_ENABLE_RTREE
  10. #cgo CFLAGS: -DSQLITE_THREADSAFE
  11. #cgo CFLAGS: -DSQLITE_ENABLE_FTS3
  12. #cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS
  13. #cgo CFLAGS: -DSQLITE_ENABLE_FTS4_UNICODE61
  14. #cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15
  15. #cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA=1
  16. #cgo CFLAGS: -Wno-deprecated-declarations
  17. #ifndef USE_LIBSQLITE3
  18. #include <sqlite3-binding.h>
  19. #else
  20. #include <sqlite3.h>
  21. #endif
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <memory.h>
  25. static inline char *_sqlite3_mprintf(char *zFormat, char *arg) {
  26. return sqlite3_mprintf(zFormat, arg);
  27. }
  28. typedef struct goVTab goVTab;
  29. struct goVTab {
  30. sqlite3_vtab base;
  31. void *vTab;
  32. };
  33. uintptr_t goMInit(void *db, void *pAux, int argc, char **argv, char **pzErr, int isCreate);
  34. static int cXInit(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr, int isCreate) {
  35. void *vTab = (void *)goMInit(db, pAux, argc, (char**)argv, pzErr, isCreate);
  36. if (!vTab || *pzErr) {
  37. return SQLITE_ERROR;
  38. }
  39. goVTab *pvTab = (goVTab *)sqlite3_malloc(sizeof(goVTab));
  40. if (!pvTab) {
  41. *pzErr = sqlite3_mprintf("%s", "Out of memory");
  42. return SQLITE_NOMEM;
  43. }
  44. memset(pvTab, 0, sizeof(goVTab));
  45. pvTab->vTab = vTab;
  46. *ppVTab = (sqlite3_vtab *)pvTab;
  47. *pzErr = 0;
  48. return SQLITE_OK;
  49. }
  50. static inline int cXCreate(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) {
  51. return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 1);
  52. }
  53. static inline int cXConnect(sqlite3 *db, void *pAux, int argc, const char *const*argv, sqlite3_vtab **ppVTab, char **pzErr) {
  54. return cXInit(db, pAux, argc, argv, ppVTab, pzErr, 0);
  55. }
  56. char* goVBestIndex(void *pVTab, void *icp);
  57. static inline int cXBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *info) {
  58. char *pzErr = goVBestIndex(((goVTab*)pVTab)->vTab, info);
  59. if (pzErr) {
  60. if (pVTab->zErrMsg)
  61. sqlite3_free(pVTab->zErrMsg);
  62. pVTab->zErrMsg = pzErr;
  63. return SQLITE_ERROR;
  64. }
  65. return SQLITE_OK;
  66. }
  67. char* goVRelease(void *pVTab, int isDestroy);
  68. static int cXRelease(sqlite3_vtab *pVTab, int isDestroy) {
  69. char *pzErr = goVRelease(((goVTab*)pVTab)->vTab, isDestroy);
  70. if (pzErr) {
  71. if (pVTab->zErrMsg)
  72. sqlite3_free(pVTab->zErrMsg);
  73. pVTab->zErrMsg = pzErr;
  74. return SQLITE_ERROR;
  75. }
  76. if (pVTab->zErrMsg)
  77. sqlite3_free(pVTab->zErrMsg);
  78. sqlite3_free(pVTab);
  79. return SQLITE_OK;
  80. }
  81. static inline int cXDisconnect(sqlite3_vtab *pVTab) {
  82. return cXRelease(pVTab, 0);
  83. }
  84. static inline int cXDestroy(sqlite3_vtab *pVTab) {
  85. return cXRelease(pVTab, 1);
  86. }
  87. typedef struct goVTabCursor goVTabCursor;
  88. struct goVTabCursor {
  89. sqlite3_vtab_cursor base;
  90. void *vTabCursor;
  91. };
  92. uintptr_t goVOpen(void *pVTab, char **pzErr);
  93. static int cXOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor) {
  94. void *vTabCursor = (void *)goVOpen(((goVTab*)pVTab)->vTab, &(pVTab->zErrMsg));
  95. goVTabCursor *pCursor = (goVTabCursor *)sqlite3_malloc(sizeof(goVTabCursor));
  96. if (!pCursor) {
  97. return SQLITE_NOMEM;
  98. }
  99. memset(pCursor, 0, sizeof(goVTabCursor));
  100. pCursor->vTabCursor = vTabCursor;
  101. *ppCursor = (sqlite3_vtab_cursor *)pCursor;
  102. return SQLITE_OK;
  103. }
  104. static int setErrMsg(sqlite3_vtab_cursor *pCursor, char *pzErr) {
  105. if (pCursor->pVtab->zErrMsg)
  106. sqlite3_free(pCursor->pVtab->zErrMsg);
  107. pCursor->pVtab->zErrMsg = pzErr;
  108. return SQLITE_ERROR;
  109. }
  110. char* goVClose(void *pCursor);
  111. static int cXClose(sqlite3_vtab_cursor *pCursor) {
  112. char *pzErr = goVClose(((goVTabCursor*)pCursor)->vTabCursor);
  113. if (pzErr) {
  114. return setErrMsg(pCursor, pzErr);
  115. }
  116. sqlite3_free(pCursor);
  117. return SQLITE_OK;
  118. }
  119. char* goVFilter(void *pCursor, int idxNum, char* idxName, int argc, sqlite3_value **argv);
  120. static int cXFilter(sqlite3_vtab_cursor *pCursor, int idxNum, const char *idxStr, int argc, sqlite3_value **argv) {
  121. char *pzErr = goVFilter(((goVTabCursor*)pCursor)->vTabCursor, idxNum, (char*)idxStr, argc, argv);
  122. if (pzErr) {
  123. return setErrMsg(pCursor, pzErr);
  124. }
  125. return SQLITE_OK;
  126. }
  127. char* goVNext(void *pCursor);
  128. static int cXNext(sqlite3_vtab_cursor *pCursor) {
  129. char *pzErr = goVNext(((goVTabCursor*)pCursor)->vTabCursor);
  130. if (pzErr) {
  131. return setErrMsg(pCursor, pzErr);
  132. }
  133. return SQLITE_OK;
  134. }
  135. int goVEof(void *pCursor);
  136. static inline int cXEof(sqlite3_vtab_cursor *pCursor) {
  137. return goVEof(((goVTabCursor*)pCursor)->vTabCursor);
  138. }
  139. char* goVColumn(void *pCursor, void *cp, int col);
  140. static int cXColumn(sqlite3_vtab_cursor *pCursor, sqlite3_context *ctx, int i) {
  141. char *pzErr = goVColumn(((goVTabCursor*)pCursor)->vTabCursor, ctx, i);
  142. if (pzErr) {
  143. return setErrMsg(pCursor, pzErr);
  144. }
  145. return SQLITE_OK;
  146. }
  147. char* goVRowid(void *pCursor, sqlite3_int64 *pRowid);
  148. static int cXRowid(sqlite3_vtab_cursor *pCursor, sqlite3_int64 *pRowid) {
  149. char *pzErr = goVRowid(((goVTabCursor*)pCursor)->vTabCursor, pRowid);
  150. if (pzErr) {
  151. return setErrMsg(pCursor, pzErr);
  152. }
  153. return SQLITE_OK;
  154. }
  155. char* goVUpdate(void *pVTab, int argc, sqlite3_value **argv, sqlite3_int64 *pRowid);
  156. static int cXUpdate(sqlite3_vtab *pVTab, int argc, sqlite3_value **argv, sqlite3_int64 *pRowid) {
  157. char *pzErr = goVUpdate(((goVTab*)pVTab)->vTab, argc, argv, pRowid);
  158. if (pzErr) {
  159. if (pVTab->zErrMsg)
  160. sqlite3_free(pVTab->zErrMsg);
  161. pVTab->zErrMsg = pzErr;
  162. return SQLITE_ERROR;
  163. }
  164. return SQLITE_OK;
  165. }
  166. static sqlite3_module goModule = {
  167. 0, // iVersion
  168. cXCreate, // xCreate - create a table
  169. cXConnect, // xConnect - connect to an existing table
  170. cXBestIndex, // xBestIndex - Determine search strategy
  171. cXDisconnect, // xDisconnect - Disconnect from a table
  172. cXDestroy, // xDestroy - Drop a table
  173. cXOpen, // xOpen - open a cursor
  174. cXClose, // xClose - close a cursor
  175. cXFilter, // xFilter - configure scan constraints
  176. cXNext, // xNext - advance a cursor
  177. cXEof, // xEof
  178. cXColumn, // xColumn - read data
  179. cXRowid, // xRowid - read data
  180. cXUpdate, // xUpdate - write data
  181. // Not implemented
  182. 0, // xBegin - begin transaction
  183. 0, // xSync - sync transaction
  184. 0, // xCommit - commit transaction
  185. 0, // xRollback - rollback transaction
  186. 0, // xFindFunction - function overloading
  187. 0, // xRename - rename the table
  188. 0, // xSavepoint
  189. 0, // xRelease
  190. 0 // xRollbackTo
  191. };
  192. void goMDestroy(void*);
  193. static int _sqlite3_create_module(sqlite3 *db, const char *zName, uintptr_t pClientData) {
  194. return sqlite3_create_module_v2(db, zName, &goModule, (void*) pClientData, goMDestroy);
  195. }
  196. */
  197. import "C"
  198. import (
  199. "fmt"
  200. "math"
  201. "reflect"
  202. "unsafe"
  203. )
  204. type sqliteModule struct {
  205. c *SQLiteConn
  206. name string
  207. module Module
  208. }
  209. type sqliteVTab struct {
  210. module *sqliteModule
  211. vTab VTab
  212. }
  213. type sqliteVTabCursor struct {
  214. vTab *sqliteVTab
  215. vTabCursor VTabCursor
  216. }
  217. // Op is type of operations.
  218. type Op uint8
  219. // Op mean identity of operations.
  220. const (
  221. OpEQ Op = 2
  222. OpGT = 4
  223. OpLE = 8
  224. OpLT = 16
  225. OpGE = 32
  226. OpMATCH = 64
  227. OpLIKE = 65 /* 3.10.0 and later only */
  228. OpGLOB = 66 /* 3.10.0 and later only */
  229. OpREGEXP = 67 /* 3.10.0 and later only */
  230. OpScanUnique = 1 /* Scan visits at most 1 row */
  231. )
  232. // InfoConstraint give information of constraint.
  233. type InfoConstraint struct {
  234. Column int
  235. Op Op
  236. Usable bool
  237. }
  238. // InfoOrderBy give information of order-by.
  239. type InfoOrderBy struct {
  240. Column int
  241. Desc bool
  242. }
  243. func constraints(info *C.sqlite3_index_info) []InfoConstraint {
  244. l := info.nConstraint
  245. slice := (*[1 << 30]C.struct_sqlite3_index_constraint)(unsafe.Pointer(info.aConstraint))[:l:l]
  246. cst := make([]InfoConstraint, 0, l)
  247. for _, c := range slice {
  248. var usable bool
  249. if c.usable > 0 {
  250. usable = true
  251. }
  252. cst = append(cst, InfoConstraint{
  253. Column: int(c.iColumn),
  254. Op: Op(c.op),
  255. Usable: usable,
  256. })
  257. }
  258. return cst
  259. }
  260. func orderBys(info *C.sqlite3_index_info) []InfoOrderBy {
  261. l := info.nOrderBy
  262. slice := (*[1 << 30]C.struct_sqlite3_index_orderby)(unsafe.Pointer(info.aOrderBy))[:l:l]
  263. ob := make([]InfoOrderBy, 0, l)
  264. for _, c := range slice {
  265. var desc bool
  266. if c.desc > 0 {
  267. desc = true
  268. }
  269. ob = append(ob, InfoOrderBy{
  270. Column: int(c.iColumn),
  271. Desc: desc,
  272. })
  273. }
  274. return ob
  275. }
  276. // IndexResult is a Go struct representation of what eventually ends up in the
  277. // output fields for `sqlite3_index_info`
  278. // See: https://www.sqlite.org/c3ref/index_info.html
  279. type IndexResult struct {
  280. Used []bool // aConstraintUsage
  281. IdxNum int
  282. IdxStr string
  283. AlreadyOrdered bool // orderByConsumed
  284. EstimatedCost float64
  285. EstimatedRows float64
  286. }
  287. // mPrintf is a utility wrapper around sqlite3_mprintf
  288. func mPrintf(format, arg string) *C.char {
  289. cf := C.CString(format)
  290. defer C.free(unsafe.Pointer(cf))
  291. ca := C.CString(arg)
  292. defer C.free(unsafe.Pointer(ca))
  293. return C._sqlite3_mprintf(cf, ca)
  294. }
  295. //export goMInit
  296. func goMInit(db, pClientData unsafe.Pointer, argc C.int, argv **C.char, pzErr **C.char, isCreate C.int) C.uintptr_t {
  297. m := lookupHandle(uintptr(pClientData)).(*sqliteModule)
  298. if m.c.db != (*C.sqlite3)(db) {
  299. *pzErr = mPrintf("%s", "Inconsistent db handles")
  300. return 0
  301. }
  302. args := make([]string, argc)
  303. var A []*C.char
  304. slice := reflect.SliceHeader{Data: uintptr(unsafe.Pointer(argv)), Len: int(argc), Cap: int(argc)}
  305. a := reflect.NewAt(reflect.TypeOf(A), unsafe.Pointer(&slice)).Elem().Interface()
  306. for i, s := range a.([]*C.char) {
  307. args[i] = C.GoString(s)
  308. }
  309. var vTab VTab
  310. var err error
  311. if isCreate == 1 {
  312. vTab, err = m.module.Create(m.c, args)
  313. } else {
  314. vTab, err = m.module.Connect(m.c, args)
  315. }
  316. if err != nil {
  317. *pzErr = mPrintf("%s", err.Error())
  318. return 0
  319. }
  320. vt := sqliteVTab{m, vTab}
  321. *pzErr = nil
  322. return C.uintptr_t(newHandle(m.c, &vt))
  323. }
  324. //export goVRelease
  325. func goVRelease(pVTab unsafe.Pointer, isDestroy C.int) *C.char {
  326. vt := lookupHandle(uintptr(pVTab)).(*sqliteVTab)
  327. var err error
  328. if isDestroy == 1 {
  329. err = vt.vTab.Destroy()
  330. } else {
  331. err = vt.vTab.Disconnect()
  332. }
  333. if err != nil {
  334. return mPrintf("%s", err.Error())
  335. }
  336. return nil
  337. }
  338. //export goVOpen
  339. func goVOpen(pVTab unsafe.Pointer, pzErr **C.char) C.uintptr_t {
  340. vt := lookupHandle(uintptr(pVTab)).(*sqliteVTab)
  341. vTabCursor, err := vt.vTab.Open()
  342. if err != nil {
  343. *pzErr = mPrintf("%s", err.Error())
  344. return 0
  345. }
  346. vtc := sqliteVTabCursor{vt, vTabCursor}
  347. *pzErr = nil
  348. return C.uintptr_t(newHandle(vt.module.c, &vtc))
  349. }
  350. //export goVBestIndex
  351. func goVBestIndex(pVTab unsafe.Pointer, icp unsafe.Pointer) *C.char {
  352. vt := lookupHandle(uintptr(pVTab)).(*sqliteVTab)
  353. info := (*C.sqlite3_index_info)(icp)
  354. csts := constraints(info)
  355. res, err := vt.vTab.BestIndex(csts, orderBys(info))
  356. if err != nil {
  357. return mPrintf("%s", err.Error())
  358. }
  359. if len(res.Used) != len(csts) {
  360. return mPrintf("Result.Used != expected value", "")
  361. }
  362. // Get a pointer to constraint_usage struct so we can update in place.
  363. l := info.nConstraint
  364. s := (*[1 << 30]C.struct_sqlite3_index_constraint_usage)(unsafe.Pointer(info.aConstraintUsage))[:l:l]
  365. index := 1
  366. for i := C.int(0); i < info.nConstraint; i++ {
  367. if res.Used[i] {
  368. s[i].argvIndex = C.int(index)
  369. s[i].omit = C.uchar(1)
  370. index++
  371. }
  372. }
  373. info.idxNum = C.int(res.IdxNum)
  374. idxStr := C.CString(res.IdxStr)
  375. defer C.free(unsafe.Pointer(idxStr))
  376. info.idxStr = idxStr
  377. info.needToFreeIdxStr = C.int(0)
  378. if res.AlreadyOrdered {
  379. info.orderByConsumed = C.int(1)
  380. }
  381. info.estimatedCost = C.double(res.EstimatedCost)
  382. info.estimatedRows = C.sqlite3_int64(res.EstimatedRows)
  383. return nil
  384. }
  385. //export goVClose
  386. func goVClose(pCursor unsafe.Pointer) *C.char {
  387. vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
  388. err := vtc.vTabCursor.Close()
  389. if err != nil {
  390. return mPrintf("%s", err.Error())
  391. }
  392. return nil
  393. }
  394. //export goMDestroy
  395. func goMDestroy(pClientData unsafe.Pointer) {
  396. m := lookupHandle(uintptr(pClientData)).(*sqliteModule)
  397. m.module.DestroyModule()
  398. }
  399. //export goVFilter
  400. func goVFilter(pCursor unsafe.Pointer, idxNum C.int, idxName *C.char, argc C.int, argv **C.sqlite3_value) *C.char {
  401. vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
  402. args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc]
  403. vals := make([]interface{}, 0, argc)
  404. for _, v := range args {
  405. conv, err := callbackArgGeneric(v)
  406. if err != nil {
  407. return mPrintf("%s", err.Error())
  408. }
  409. vals = append(vals, conv.Interface())
  410. }
  411. err := vtc.vTabCursor.Filter(int(idxNum), C.GoString(idxName), vals)
  412. if err != nil {
  413. return mPrintf("%s", err.Error())
  414. }
  415. return nil
  416. }
  417. //export goVNext
  418. func goVNext(pCursor unsafe.Pointer) *C.char {
  419. vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
  420. err := vtc.vTabCursor.Next()
  421. if err != nil {
  422. return mPrintf("%s", err.Error())
  423. }
  424. return nil
  425. }
  426. //export goVEof
  427. func goVEof(pCursor unsafe.Pointer) C.int {
  428. vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
  429. err := vtc.vTabCursor.EOF()
  430. if err {
  431. return 1
  432. }
  433. return 0
  434. }
  435. //export goVColumn
  436. func goVColumn(pCursor, cp unsafe.Pointer, col C.int) *C.char {
  437. vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
  438. c := (*SQLiteContext)(cp)
  439. err := vtc.vTabCursor.Column(c, int(col))
  440. if err != nil {
  441. return mPrintf("%s", err.Error())
  442. }
  443. return nil
  444. }
  445. //export goVRowid
  446. func goVRowid(pCursor unsafe.Pointer, pRowid *C.sqlite3_int64) *C.char {
  447. vtc := lookupHandle(uintptr(pCursor)).(*sqliteVTabCursor)
  448. rowid, err := vtc.vTabCursor.Rowid()
  449. if err != nil {
  450. return mPrintf("%s", err.Error())
  451. }
  452. *pRowid = C.sqlite3_int64(rowid)
  453. return nil
  454. }
  455. //export goVUpdate
  456. func goVUpdate(pVTab unsafe.Pointer, argc C.int, argv **C.sqlite3_value, pRowid *C.sqlite3_int64) *C.char {
  457. vt := lookupHandle(uintptr(pVTab)).(*sqliteVTab)
  458. var tname string
  459. if n, ok := vt.vTab.(interface {
  460. TableName() string
  461. }); ok {
  462. tname = n.TableName() + " "
  463. }
  464. err := fmt.Errorf("virtual %s table %sis read-only", vt.module.name, tname)
  465. if v, ok := vt.vTab.(VTabUpdater); ok {
  466. // convert argv
  467. args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc]
  468. vals := make([]interface{}, 0, argc)
  469. for _, v := range args {
  470. conv, err := callbackArgGeneric(v)
  471. if err != nil {
  472. return mPrintf("%s", err.Error())
  473. }
  474. // work around for SQLITE_NULL
  475. x := conv.Interface()
  476. if z, ok := x.([]byte); ok && z == nil {
  477. x = nil
  478. }
  479. vals = append(vals, x)
  480. }
  481. switch {
  482. case argc == 1:
  483. err = v.Delete(vals[0])
  484. case argc > 1 && vals[0] == nil:
  485. var id int64
  486. id, err = v.Insert(vals[1], vals[2:])
  487. if err == nil {
  488. *pRowid = C.sqlite3_int64(id)
  489. }
  490. case argc > 1:
  491. err = v.Update(vals[1], vals[2:])
  492. }
  493. }
  494. if err != nil {
  495. return mPrintf("%s", err.Error())
  496. }
  497. return nil
  498. }
  499. // Module is a "virtual table module", it defines the implementation of a
  500. // virtual tables. See: http://sqlite.org/c3ref/module.html
  501. type Module interface {
  502. // http://sqlite.org/vtab.html#xcreate
  503. Create(c *SQLiteConn, args []string) (VTab, error)
  504. // http://sqlite.org/vtab.html#xconnect
  505. Connect(c *SQLiteConn, args []string) (VTab, error)
  506. // http://sqlite.org/c3ref/create_module.html
  507. DestroyModule()
  508. }
  509. // VTab describes a particular instance of the virtual table.
  510. // See: http://sqlite.org/c3ref/vtab.html
  511. type VTab interface {
  512. // http://sqlite.org/vtab.html#xbestindex
  513. BestIndex([]InfoConstraint, []InfoOrderBy) (*IndexResult, error)
  514. // http://sqlite.org/vtab.html#xdisconnect
  515. Disconnect() error
  516. // http://sqlite.org/vtab.html#sqlite3_module.xDestroy
  517. Destroy() error
  518. // http://sqlite.org/vtab.html#xopen
  519. Open() (VTabCursor, error)
  520. }
  521. // VTabUpdater is a type that allows a VTab to be inserted, updated, or
  522. // deleted.
  523. // See: https://sqlite.org/vtab.html#xupdate
  524. type VTabUpdater interface {
  525. Delete(interface{}) error
  526. Insert(interface{}, []interface{}) (int64, error)
  527. Update(interface{}, []interface{}) error
  528. }
  529. // VTabCursor describes cursors that point into the virtual table and are used
  530. // to loop through the virtual table. See: http://sqlite.org/c3ref/vtab_cursor.html
  531. type VTabCursor interface {
  532. // http://sqlite.org/vtab.html#xclose
  533. Close() error
  534. // http://sqlite.org/vtab.html#xfilter
  535. Filter(idxNum int, idxStr string, vals []interface{}) error
  536. // http://sqlite.org/vtab.html#xnext
  537. Next() error
  538. // http://sqlite.org/vtab.html#xeof
  539. EOF() bool
  540. // http://sqlite.org/vtab.html#xcolumn
  541. Column(c *SQLiteContext, col int) error
  542. // http://sqlite.org/vtab.html#xrowid
  543. Rowid() (int64, error)
  544. }
  545. // DeclareVTab declares the Schema of a virtual table.
  546. // See: http://sqlite.org/c3ref/declare_vtab.html
  547. func (c *SQLiteConn) DeclareVTab(sql string) error {
  548. zSQL := C.CString(sql)
  549. defer C.free(unsafe.Pointer(zSQL))
  550. rv := C.sqlite3_declare_vtab(c.db, zSQL)
  551. if rv != C.SQLITE_OK {
  552. return c.lastError()
  553. }
  554. return nil
  555. }
  556. // CreateModule registers a virtual table implementation.
  557. // See: http://sqlite.org/c3ref/create_module.html
  558. func (c *SQLiteConn) CreateModule(moduleName string, module Module) error {
  559. mname := C.CString(moduleName)
  560. defer C.free(unsafe.Pointer(mname))
  561. udm := sqliteModule{c, moduleName, module}
  562. rv := C._sqlite3_create_module(c.db, mname, C.uintptr_t(newHandle(c, &udm)))
  563. if rv != C.SQLITE_OK {
  564. return c.lastError()
  565. }
  566. return nil
  567. }
上海开阖软件有限公司 沪ICP备12045867号-1