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.

sqlite3.go 60KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112
  1. // Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. // Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
  3. //
  4. // Use of this source code is governed by an MIT-style
  5. // license that can be found in the LICENSE file.
  6. // +build cgo
  7. package sqlite3
  8. /*
  9. #cgo CFLAGS: -std=gnu99
  10. #cgo CFLAGS: -DSQLITE_ENABLE_RTREE
  11. #cgo CFLAGS: -DSQLITE_THREADSAFE=1
  12. #cgo CFLAGS: -DHAVE_USLEEP=1
  13. #cgo CFLAGS: -DSQLITE_ENABLE_FTS3
  14. #cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS
  15. #cgo CFLAGS: -DSQLITE_ENABLE_FTS4_UNICODE61
  16. #cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15
  17. #cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED
  18. #cgo CFLAGS: -DSQLITE_DISABLE_INTRINSIC
  19. #cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
  20. #cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT
  21. #cgo CFLAGS: -Wno-deprecated-declarations
  22. #cgo linux,!android CFLAGS: -DHAVE_PREAD64=1 -DHAVE_PWRITE64=1
  23. #ifndef USE_LIBSQLITE3
  24. #include <sqlite3-binding.h>
  25. #else
  26. #include <sqlite3.h>
  27. #endif
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifdef __CYGWIN__
  31. # include <errno.h>
  32. #endif
  33. #ifndef SQLITE_OPEN_READWRITE
  34. # define SQLITE_OPEN_READWRITE 0
  35. #endif
  36. #ifndef SQLITE_OPEN_FULLMUTEX
  37. # define SQLITE_OPEN_FULLMUTEX 0
  38. #endif
  39. #ifndef SQLITE_DETERMINISTIC
  40. # define SQLITE_DETERMINISTIC 0
  41. #endif
  42. static int
  43. _sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
  44. #ifdef SQLITE_OPEN_URI
  45. return sqlite3_open_v2(filename, ppDb, flags | SQLITE_OPEN_URI, zVfs);
  46. #else
  47. return sqlite3_open_v2(filename, ppDb, flags, zVfs);
  48. #endif
  49. }
  50. static int
  51. _sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
  52. return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
  53. }
  54. static int
  55. _sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
  56. return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
  57. }
  58. #include <stdio.h>
  59. #include <stdint.h>
  60. static int
  61. _sqlite3_exec(sqlite3* db, const char* pcmd, long long* rowid, long long* changes)
  62. {
  63. int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
  64. *rowid = (long long) sqlite3_last_insert_rowid(db);
  65. *changes = (long long) sqlite3_changes(db);
  66. return rv;
  67. }
  68. #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
  69. extern int _sqlite3_step_blocking(sqlite3_stmt *stmt);
  70. extern int _sqlite3_step_row_blocking(sqlite3_stmt* stmt, long long* rowid, long long* changes);
  71. extern int _sqlite3_prepare_v2_blocking(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail);
  72. static int
  73. _sqlite3_step_internal(sqlite3_stmt *stmt)
  74. {
  75. return _sqlite3_step_blocking(stmt);
  76. }
  77. static int
  78. _sqlite3_step_row_internal(sqlite3_stmt* stmt, long long* rowid, long long* changes)
  79. {
  80. return _sqlite3_step_row_blocking(stmt, rowid, changes);
  81. }
  82. static int
  83. _sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail)
  84. {
  85. return _sqlite3_prepare_v2_blocking(db, zSql, nBytes, ppStmt, pzTail);
  86. }
  87. #else
  88. static int
  89. _sqlite3_step_internal(sqlite3_stmt *stmt)
  90. {
  91. return sqlite3_step(stmt);
  92. }
  93. static int
  94. _sqlite3_step_row_internal(sqlite3_stmt* stmt, long long* rowid, long long* changes)
  95. {
  96. int rv = sqlite3_step(stmt);
  97. sqlite3* db = sqlite3_db_handle(stmt);
  98. *rowid = (long long) sqlite3_last_insert_rowid(db);
  99. *changes = (long long) sqlite3_changes(db);
  100. return rv;
  101. }
  102. static int
  103. _sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail)
  104. {
  105. return sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail);
  106. }
  107. #endif
  108. void _sqlite3_result_text(sqlite3_context* ctx, const char* s) {
  109. sqlite3_result_text(ctx, s, -1, &free);
  110. }
  111. void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l) {
  112. sqlite3_result_blob(ctx, b, l, SQLITE_TRANSIENT);
  113. }
  114. int _sqlite3_create_function(
  115. sqlite3 *db,
  116. const char *zFunctionName,
  117. int nArg,
  118. int eTextRep,
  119. uintptr_t pApp,
  120. void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  121. void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  122. void (*xFinal)(sqlite3_context*)
  123. ) {
  124. return sqlite3_create_function(db, zFunctionName, nArg, eTextRep, (void*) pApp, xFunc, xStep, xFinal);
  125. }
  126. void callbackTrampoline(sqlite3_context*, int, sqlite3_value**);
  127. void stepTrampoline(sqlite3_context*, int, sqlite3_value**);
  128. void doneTrampoline(sqlite3_context*);
  129. int compareTrampoline(void*, int, char*, int, char*);
  130. int commitHookTrampoline(void*);
  131. void rollbackHookTrampoline(void*);
  132. void updateHookTrampoline(void*, int, char*, char*, sqlite3_int64);
  133. int authorizerTrampoline(void*, int, char*, char*, char*, char*);
  134. #ifdef SQLITE_LIMIT_WORKER_THREADS
  135. # define _SQLITE_HAS_LIMIT
  136. # define SQLITE_LIMIT_LENGTH 0
  137. # define SQLITE_LIMIT_SQL_LENGTH 1
  138. # define SQLITE_LIMIT_COLUMN 2
  139. # define SQLITE_LIMIT_EXPR_DEPTH 3
  140. # define SQLITE_LIMIT_COMPOUND_SELECT 4
  141. # define SQLITE_LIMIT_VDBE_OP 5
  142. # define SQLITE_LIMIT_FUNCTION_ARG 6
  143. # define SQLITE_LIMIT_ATTACHED 7
  144. # define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
  145. # define SQLITE_LIMIT_VARIABLE_NUMBER 9
  146. # define SQLITE_LIMIT_TRIGGER_DEPTH 10
  147. # define SQLITE_LIMIT_WORKER_THREADS 11
  148. # else
  149. # define SQLITE_LIMIT_WORKER_THREADS 11
  150. #endif
  151. static int _sqlite3_limit(sqlite3* db, int limitId, int newLimit) {
  152. #ifndef _SQLITE_HAS_LIMIT
  153. return -1;
  154. #else
  155. return sqlite3_limit(db, limitId, newLimit);
  156. #endif
  157. }
  158. #if SQLITE_VERSION_NUMBER < 3012000
  159. static int sqlite3_system_errno(sqlite3 *db) {
  160. return 0;
  161. }
  162. #endif
  163. */
  164. import "C"
  165. import (
  166. "context"
  167. "database/sql"
  168. "database/sql/driver"
  169. "errors"
  170. "fmt"
  171. "io"
  172. "net/url"
  173. "reflect"
  174. "runtime"
  175. "strconv"
  176. "strings"
  177. "sync"
  178. "syscall"
  179. "time"
  180. "unsafe"
  181. )
  182. // SQLiteTimestampFormats is timestamp formats understood by both this module
  183. // and SQLite. The first format in the slice will be used when saving time
  184. // values into the database. When parsing a string from a timestamp or datetime
  185. // column, the formats are tried in order.
  186. var SQLiteTimestampFormats = []string{
  187. // By default, store timestamps with whatever timezone they come with.
  188. // When parsed, they will be returned with the same timezone.
  189. "2006-01-02 15:04:05.999999999-07:00",
  190. "2006-01-02T15:04:05.999999999-07:00",
  191. "2006-01-02 15:04:05.999999999",
  192. "2006-01-02T15:04:05.999999999",
  193. "2006-01-02 15:04:05",
  194. "2006-01-02T15:04:05",
  195. "2006-01-02 15:04",
  196. "2006-01-02T15:04",
  197. "2006-01-02",
  198. }
  199. const (
  200. columnDate string = "date"
  201. columnDatetime string = "datetime"
  202. columnTimestamp string = "timestamp"
  203. )
  204. func init() {
  205. sql.Register("sqlite3", &SQLiteDriver{})
  206. }
  207. // Version returns SQLite library version information.
  208. func Version() (libVersion string, libVersionNumber int, sourceID string) {
  209. libVersion = C.GoString(C.sqlite3_libversion())
  210. libVersionNumber = int(C.sqlite3_libversion_number())
  211. sourceID = C.GoString(C.sqlite3_sourceid())
  212. return libVersion, libVersionNumber, sourceID
  213. }
  214. const (
  215. // used by authorizer and pre_update_hook
  216. SQLITE_DELETE = C.SQLITE_DELETE
  217. SQLITE_INSERT = C.SQLITE_INSERT
  218. SQLITE_UPDATE = C.SQLITE_UPDATE
  219. // used by authorzier - as return value
  220. SQLITE_OK = C.SQLITE_OK
  221. SQLITE_IGNORE = C.SQLITE_IGNORE
  222. SQLITE_DENY = C.SQLITE_DENY
  223. // different actions query tries to do - passed as argument to authorizer
  224. SQLITE_CREATE_INDEX = C.SQLITE_CREATE_INDEX
  225. SQLITE_CREATE_TABLE = C.SQLITE_CREATE_TABLE
  226. SQLITE_CREATE_TEMP_INDEX = C.SQLITE_CREATE_TEMP_INDEX
  227. SQLITE_CREATE_TEMP_TABLE = C.SQLITE_CREATE_TEMP_TABLE
  228. SQLITE_CREATE_TEMP_TRIGGER = C.SQLITE_CREATE_TEMP_TRIGGER
  229. SQLITE_CREATE_TEMP_VIEW = C.SQLITE_CREATE_TEMP_VIEW
  230. SQLITE_CREATE_TRIGGER = C.SQLITE_CREATE_TRIGGER
  231. SQLITE_CREATE_VIEW = C.SQLITE_CREATE_VIEW
  232. SQLITE_CREATE_VTABLE = C.SQLITE_CREATE_VTABLE
  233. SQLITE_DROP_INDEX = C.SQLITE_DROP_INDEX
  234. SQLITE_DROP_TABLE = C.SQLITE_DROP_TABLE
  235. SQLITE_DROP_TEMP_INDEX = C.SQLITE_DROP_TEMP_INDEX
  236. SQLITE_DROP_TEMP_TABLE = C.SQLITE_DROP_TEMP_TABLE
  237. SQLITE_DROP_TEMP_TRIGGER = C.SQLITE_DROP_TEMP_TRIGGER
  238. SQLITE_DROP_TEMP_VIEW = C.SQLITE_DROP_TEMP_VIEW
  239. SQLITE_DROP_TRIGGER = C.SQLITE_DROP_TRIGGER
  240. SQLITE_DROP_VIEW = C.SQLITE_DROP_VIEW
  241. SQLITE_DROP_VTABLE = C.SQLITE_DROP_VTABLE
  242. SQLITE_PRAGMA = C.SQLITE_PRAGMA
  243. SQLITE_READ = C.SQLITE_READ
  244. SQLITE_SELECT = C.SQLITE_SELECT
  245. SQLITE_TRANSACTION = C.SQLITE_TRANSACTION
  246. SQLITE_ATTACH = C.SQLITE_ATTACH
  247. SQLITE_DETACH = C.SQLITE_DETACH
  248. SQLITE_ALTER_TABLE = C.SQLITE_ALTER_TABLE
  249. SQLITE_REINDEX = C.SQLITE_REINDEX
  250. SQLITE_ANALYZE = C.SQLITE_ANALYZE
  251. SQLITE_FUNCTION = C.SQLITE_FUNCTION
  252. SQLITE_SAVEPOINT = C.SQLITE_SAVEPOINT
  253. SQLITE_COPY = C.SQLITE_COPY
  254. /*SQLITE_RECURSIVE = C.SQLITE_RECURSIVE*/
  255. )
  256. // SQLiteDriver implements driver.Driver.
  257. type SQLiteDriver struct {
  258. Extensions []string
  259. ConnectHook func(*SQLiteConn) error
  260. }
  261. // SQLiteConn implements driver.Conn.
  262. type SQLiteConn struct {
  263. mu sync.Mutex
  264. db *C.sqlite3
  265. loc *time.Location
  266. txlock string
  267. funcs []*functionInfo
  268. aggregators []*aggInfo
  269. }
  270. // SQLiteTx implements driver.Tx.
  271. type SQLiteTx struct {
  272. c *SQLiteConn
  273. }
  274. // SQLiteStmt implements driver.Stmt.
  275. type SQLiteStmt struct {
  276. mu sync.Mutex
  277. c *SQLiteConn
  278. s *C.sqlite3_stmt
  279. t string
  280. closed bool
  281. cls bool
  282. }
  283. // SQLiteResult implements sql.Result.
  284. type SQLiteResult struct {
  285. id int64
  286. changes int64
  287. }
  288. // SQLiteRows implements driver.Rows.
  289. type SQLiteRows struct {
  290. s *SQLiteStmt
  291. nc int
  292. cols []string
  293. decltype []string
  294. cls bool
  295. closed bool
  296. ctx context.Context // no better alternative to pass context into Next() method
  297. }
  298. type functionInfo struct {
  299. f reflect.Value
  300. argConverters []callbackArgConverter
  301. variadicConverter callbackArgConverter
  302. retConverter callbackRetConverter
  303. }
  304. func (fi *functionInfo) Call(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
  305. args, err := callbackConvertArgs(argv, fi.argConverters, fi.variadicConverter)
  306. if err != nil {
  307. callbackError(ctx, err)
  308. return
  309. }
  310. ret := fi.f.Call(args)
  311. if len(ret) == 2 && ret[1].Interface() != nil {
  312. callbackError(ctx, ret[1].Interface().(error))
  313. return
  314. }
  315. err = fi.retConverter(ctx, ret[0])
  316. if err != nil {
  317. callbackError(ctx, err)
  318. return
  319. }
  320. }
  321. type aggInfo struct {
  322. constructor reflect.Value
  323. // Active aggregator objects for aggregations in flight. The
  324. // aggregators are indexed by a counter stored in the aggregation
  325. // user data space provided by sqlite.
  326. active map[int64]reflect.Value
  327. next int64
  328. stepArgConverters []callbackArgConverter
  329. stepVariadicConverter callbackArgConverter
  330. doneRetConverter callbackRetConverter
  331. }
  332. func (ai *aggInfo) agg(ctx *C.sqlite3_context) (int64, reflect.Value, error) {
  333. aggIdx := (*int64)(C.sqlite3_aggregate_context(ctx, C.int(8)))
  334. if *aggIdx == 0 {
  335. *aggIdx = ai.next
  336. ret := ai.constructor.Call(nil)
  337. if len(ret) == 2 && ret[1].Interface() != nil {
  338. return 0, reflect.Value{}, ret[1].Interface().(error)
  339. }
  340. if ret[0].IsNil() {
  341. return 0, reflect.Value{}, errors.New("aggregator constructor returned nil state")
  342. }
  343. ai.next++
  344. ai.active[*aggIdx] = ret[0]
  345. }
  346. return *aggIdx, ai.active[*aggIdx], nil
  347. }
  348. func (ai *aggInfo) Step(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
  349. _, agg, err := ai.agg(ctx)
  350. if err != nil {
  351. callbackError(ctx, err)
  352. return
  353. }
  354. args, err := callbackConvertArgs(argv, ai.stepArgConverters, ai.stepVariadicConverter)
  355. if err != nil {
  356. callbackError(ctx, err)
  357. return
  358. }
  359. ret := agg.MethodByName("Step").Call(args)
  360. if len(ret) == 1 && ret[0].Interface() != nil {
  361. callbackError(ctx, ret[0].Interface().(error))
  362. return
  363. }
  364. }
  365. func (ai *aggInfo) Done(ctx *C.sqlite3_context) {
  366. idx, agg, err := ai.agg(ctx)
  367. if err != nil {
  368. callbackError(ctx, err)
  369. return
  370. }
  371. defer func() { delete(ai.active, idx) }()
  372. ret := agg.MethodByName("Done").Call(nil)
  373. if len(ret) == 2 && ret[1].Interface() != nil {
  374. callbackError(ctx, ret[1].Interface().(error))
  375. return
  376. }
  377. err = ai.doneRetConverter(ctx, ret[0])
  378. if err != nil {
  379. callbackError(ctx, err)
  380. return
  381. }
  382. }
  383. // Commit transaction.
  384. func (tx *SQLiteTx) Commit() error {
  385. _, err := tx.c.exec(context.Background(), "COMMIT", nil)
  386. if err != nil && err.(Error).Code == C.SQLITE_BUSY {
  387. // sqlite3 will leave the transaction open in this scenario.
  388. // However, database/sql considers the transaction complete once we
  389. // return from Commit() - we must clean up to honour its semantics.
  390. tx.c.exec(context.Background(), "ROLLBACK", nil)
  391. }
  392. return err
  393. }
  394. // Rollback transaction.
  395. func (tx *SQLiteTx) Rollback() error {
  396. _, err := tx.c.exec(context.Background(), "ROLLBACK", nil)
  397. return err
  398. }
  399. // RegisterCollation makes a Go function available as a collation.
  400. //
  401. // cmp receives two UTF-8 strings, a and b. The result should be 0 if
  402. // a==b, -1 if a < b, and +1 if a > b.
  403. //
  404. // cmp must always return the same result given the same
  405. // inputs. Additionally, it must have the following properties for all
  406. // strings A, B and C: if A==B then B==A; if A==B and B==C then A==C;
  407. // if A<B then B>A; if A<B and B<C then A<C.
  408. //
  409. // If cmp does not obey these constraints, sqlite3's behavior is
  410. // undefined when the collation is used.
  411. func (c *SQLiteConn) RegisterCollation(name string, cmp func(string, string) int) error {
  412. handle := newHandle(c, cmp)
  413. cname := C.CString(name)
  414. defer C.free(unsafe.Pointer(cname))
  415. rv := C.sqlite3_create_collation(c.db, cname, C.SQLITE_UTF8, unsafe.Pointer(handle), (*[0]byte)(unsafe.Pointer(C.compareTrampoline)))
  416. if rv != C.SQLITE_OK {
  417. return c.lastError()
  418. }
  419. return nil
  420. }
  421. // RegisterCommitHook sets the commit hook for a connection.
  422. //
  423. // If the callback returns non-zero the transaction will become a rollback.
  424. //
  425. // If there is an existing commit hook for this connection, it will be
  426. // removed. If callback is nil the existing hook (if any) will be removed
  427. // without creating a new one.
  428. func (c *SQLiteConn) RegisterCommitHook(callback func() int) {
  429. if callback == nil {
  430. C.sqlite3_commit_hook(c.db, nil, nil)
  431. } else {
  432. C.sqlite3_commit_hook(c.db, (*[0]byte)(C.commitHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
  433. }
  434. }
  435. // RegisterRollbackHook sets the rollback hook for a connection.
  436. //
  437. // If there is an existing rollback hook for this connection, it will be
  438. // removed. If callback is nil the existing hook (if any) will be removed
  439. // without creating a new one.
  440. func (c *SQLiteConn) RegisterRollbackHook(callback func()) {
  441. if callback == nil {
  442. C.sqlite3_rollback_hook(c.db, nil, nil)
  443. } else {
  444. C.sqlite3_rollback_hook(c.db, (*[0]byte)(C.rollbackHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
  445. }
  446. }
  447. // RegisterUpdateHook sets the update hook for a connection.
  448. //
  449. // The parameters to the callback are the operation (one of the constants
  450. // SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE), the database name, the
  451. // table name, and the rowid.
  452. //
  453. // If there is an existing update hook for this connection, it will be
  454. // removed. If callback is nil the existing hook (if any) will be removed
  455. // without creating a new one.
  456. func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64)) {
  457. if callback == nil {
  458. C.sqlite3_update_hook(c.db, nil, nil)
  459. } else {
  460. C.sqlite3_update_hook(c.db, (*[0]byte)(C.updateHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
  461. }
  462. }
  463. // RegisterAuthorizer sets the authorizer for connection.
  464. //
  465. // The parameters to the callback are the operation (one of the constants
  466. // SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE), and 1 to 3 arguments,
  467. // depending on operation. More details see:
  468. // https://www.sqlite.org/c3ref/c_alter_table.html
  469. func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, string) int) {
  470. if callback == nil {
  471. C.sqlite3_set_authorizer(c.db, nil, nil)
  472. } else {
  473. C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), unsafe.Pointer(newHandle(c, callback)))
  474. }
  475. }
  476. // RegisterFunc makes a Go function available as a SQLite function.
  477. //
  478. // The Go function can have arguments of the following types: any
  479. // numeric type except complex, bool, []byte, string and
  480. // interface{}. interface{} arguments are given the direct translation
  481. // of the SQLite data type: int64 for INTEGER, float64 for FLOAT,
  482. // []byte for BLOB, string for TEXT.
  483. //
  484. // The function can additionally be variadic, as long as the type of
  485. // the variadic argument is one of the above.
  486. //
  487. // If pure is true. SQLite will assume that the function's return
  488. // value depends only on its inputs, and make more aggressive
  489. // optimizations in its queries.
  490. //
  491. // See _example/go_custom_funcs for a detailed example.
  492. func (c *SQLiteConn) RegisterFunc(name string, impl interface{}, pure bool) error {
  493. var fi functionInfo
  494. fi.f = reflect.ValueOf(impl)
  495. t := fi.f.Type()
  496. if t.Kind() != reflect.Func {
  497. return errors.New("Non-function passed to RegisterFunc")
  498. }
  499. if t.NumOut() != 1 && t.NumOut() != 2 {
  500. return errors.New("SQLite functions must return 1 or 2 values")
  501. }
  502. if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
  503. return errors.New("Second return value of SQLite function must be error")
  504. }
  505. numArgs := t.NumIn()
  506. if t.IsVariadic() {
  507. numArgs--
  508. }
  509. for i := 0; i < numArgs; i++ {
  510. conv, err := callbackArg(t.In(i))
  511. if err != nil {
  512. return err
  513. }
  514. fi.argConverters = append(fi.argConverters, conv)
  515. }
  516. if t.IsVariadic() {
  517. conv, err := callbackArg(t.In(numArgs).Elem())
  518. if err != nil {
  519. return err
  520. }
  521. fi.variadicConverter = conv
  522. // Pass -1 to sqlite so that it allows any number of
  523. // arguments. The call helper verifies that the minimum number
  524. // of arguments is present for variadic functions.
  525. numArgs = -1
  526. }
  527. conv, err := callbackRet(t.Out(0))
  528. if err != nil {
  529. return err
  530. }
  531. fi.retConverter = conv
  532. // fi must outlast the database connection, or we'll have dangling pointers.
  533. c.funcs = append(c.funcs, &fi)
  534. cname := C.CString(name)
  535. defer C.free(unsafe.Pointer(cname))
  536. opts := C.SQLITE_UTF8
  537. if pure {
  538. opts |= C.SQLITE_DETERMINISTIC
  539. }
  540. rv := sqlite3CreateFunction(c.db, cname, C.int(numArgs), C.int(opts), newHandle(c, &fi), C.callbackTrampoline, nil, nil)
  541. if rv != C.SQLITE_OK {
  542. return c.lastError()
  543. }
  544. return nil
  545. }
  546. func sqlite3CreateFunction(db *C.sqlite3, zFunctionName *C.char, nArg C.int, eTextRep C.int, pApp uintptr, xFunc unsafe.Pointer, xStep unsafe.Pointer, xFinal unsafe.Pointer) C.int {
  547. return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(pApp), (*[0]byte)(xFunc), (*[0]byte)(xStep), (*[0]byte)(xFinal))
  548. }
  549. // RegisterAggregator makes a Go type available as a SQLite aggregation function.
  550. //
  551. // Because aggregation is incremental, it's implemented in Go with a
  552. // type that has 2 methods: func Step(values) accumulates one row of
  553. // data into the accumulator, and func Done() ret finalizes and
  554. // returns the aggregate value. "values" and "ret" may be any type
  555. // supported by RegisterFunc.
  556. //
  557. // RegisterAggregator takes as implementation a constructor function
  558. // that constructs an instance of the aggregator type each time an
  559. // aggregation begins. The constructor must return a pointer to a
  560. // type, or an interface that implements Step() and Done().
  561. //
  562. // The constructor function and the Step/Done methods may optionally
  563. // return an error in addition to their other return values.
  564. //
  565. // See _example/go_custom_funcs for a detailed example.
  566. func (c *SQLiteConn) RegisterAggregator(name string, impl interface{}, pure bool) error {
  567. var ai aggInfo
  568. ai.constructor = reflect.ValueOf(impl)
  569. t := ai.constructor.Type()
  570. if t.Kind() != reflect.Func {
  571. return errors.New("non-function passed to RegisterAggregator")
  572. }
  573. if t.NumOut() != 1 && t.NumOut() != 2 {
  574. return errors.New("SQLite aggregator constructors must return 1 or 2 values")
  575. }
  576. if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
  577. return errors.New("Second return value of SQLite function must be error")
  578. }
  579. if t.NumIn() != 0 {
  580. return errors.New("SQLite aggregator constructors must not have arguments")
  581. }
  582. agg := t.Out(0)
  583. switch agg.Kind() {
  584. case reflect.Ptr, reflect.Interface:
  585. default:
  586. return errors.New("SQlite aggregator constructor must return a pointer object")
  587. }
  588. stepFn, found := agg.MethodByName("Step")
  589. if !found {
  590. return errors.New("SQlite aggregator doesn't have a Step() function")
  591. }
  592. step := stepFn.Type
  593. if step.NumOut() != 0 && step.NumOut() != 1 {
  594. return errors.New("SQlite aggregator Step() function must return 0 or 1 values")
  595. }
  596. if step.NumOut() == 1 && !step.Out(0).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
  597. return errors.New("type of SQlite aggregator Step() return value must be error")
  598. }
  599. stepNArgs := step.NumIn()
  600. start := 0
  601. if agg.Kind() == reflect.Ptr {
  602. // Skip over the method receiver
  603. stepNArgs--
  604. start++
  605. }
  606. if step.IsVariadic() {
  607. stepNArgs--
  608. }
  609. for i := start; i < start+stepNArgs; i++ {
  610. conv, err := callbackArg(step.In(i))
  611. if err != nil {
  612. return err
  613. }
  614. ai.stepArgConverters = append(ai.stepArgConverters, conv)
  615. }
  616. if step.IsVariadic() {
  617. conv, err := callbackArg(step.In(start + stepNArgs).Elem())
  618. if err != nil {
  619. return err
  620. }
  621. ai.stepVariadicConverter = conv
  622. // Pass -1 to sqlite so that it allows any number of
  623. // arguments. The call helper verifies that the minimum number
  624. // of arguments is present for variadic functions.
  625. stepNArgs = -1
  626. }
  627. doneFn, found := agg.MethodByName("Done")
  628. if !found {
  629. return errors.New("SQlite aggregator doesn't have a Done() function")
  630. }
  631. done := doneFn.Type
  632. doneNArgs := done.NumIn()
  633. if agg.Kind() == reflect.Ptr {
  634. // Skip over the method receiver
  635. doneNArgs--
  636. }
  637. if doneNArgs != 0 {
  638. return errors.New("SQlite aggregator Done() function must have no arguments")
  639. }
  640. if done.NumOut() != 1 && done.NumOut() != 2 {
  641. return errors.New("SQLite aggregator Done() function must return 1 or 2 values")
  642. }
  643. if done.NumOut() == 2 && !done.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
  644. return errors.New("second return value of SQLite aggregator Done() function must be error")
  645. }
  646. conv, err := callbackRet(done.Out(0))
  647. if err != nil {
  648. return err
  649. }
  650. ai.doneRetConverter = conv
  651. ai.active = make(map[int64]reflect.Value)
  652. ai.next = 1
  653. // ai must outlast the database connection, or we'll have dangling pointers.
  654. c.aggregators = append(c.aggregators, &ai)
  655. cname := C.CString(name)
  656. defer C.free(unsafe.Pointer(cname))
  657. opts := C.SQLITE_UTF8
  658. if pure {
  659. opts |= C.SQLITE_DETERMINISTIC
  660. }
  661. rv := sqlite3CreateFunction(c.db, cname, C.int(stepNArgs), C.int(opts), newHandle(c, &ai), nil, C.stepTrampoline, C.doneTrampoline)
  662. if rv != C.SQLITE_OK {
  663. return c.lastError()
  664. }
  665. return nil
  666. }
  667. // AutoCommit return which currently auto commit or not.
  668. func (c *SQLiteConn) AutoCommit() bool {
  669. c.mu.Lock()
  670. defer c.mu.Unlock()
  671. return int(C.sqlite3_get_autocommit(c.db)) != 0
  672. }
  673. func (c *SQLiteConn) lastError() error {
  674. return lastError(c.db)
  675. }
  676. // Note: may be called with db == nil
  677. func lastError(db *C.sqlite3) error {
  678. rv := C.sqlite3_errcode(db) // returns SQLITE_NOMEM if db == nil
  679. if rv == C.SQLITE_OK {
  680. return nil
  681. }
  682. extrv := C.sqlite3_extended_errcode(db) // returns SQLITE_NOMEM if db == nil
  683. errStr := C.GoString(C.sqlite3_errmsg(db)) // returns "out of memory" if db == nil
  684. // https://www.sqlite.org/c3ref/system_errno.html
  685. // sqlite3_system_errno is only meaningful if the error code was SQLITE_CANTOPEN,
  686. // or it was SQLITE_IOERR and the extended code was not SQLITE_IOERR_NOMEM
  687. var systemErrno syscall.Errno
  688. if rv == C.SQLITE_CANTOPEN || (rv == C.SQLITE_IOERR && extrv != C.SQLITE_IOERR_NOMEM) {
  689. systemErrno = syscall.Errno(C.sqlite3_system_errno(db))
  690. }
  691. return Error{
  692. Code: ErrNo(rv),
  693. ExtendedCode: ErrNoExtended(extrv),
  694. SystemErrno: systemErrno,
  695. err: errStr,
  696. }
  697. }
  698. // Exec implements Execer.
  699. func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
  700. list := make([]namedValue, len(args))
  701. for i, v := range args {
  702. list[i] = namedValue{
  703. Ordinal: i + 1,
  704. Value: v,
  705. }
  706. }
  707. return c.exec(context.Background(), query, list)
  708. }
  709. func (c *SQLiteConn) exec(ctx context.Context, query string, args []namedValue) (driver.Result, error) {
  710. start := 0
  711. for {
  712. s, err := c.prepare(ctx, query)
  713. if err != nil {
  714. return nil, err
  715. }
  716. var res driver.Result
  717. if s.(*SQLiteStmt).s != nil {
  718. na := s.NumInput()
  719. if len(args) < na {
  720. s.Close()
  721. return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
  722. }
  723. for i := 0; i < na; i++ {
  724. args[i].Ordinal -= start
  725. }
  726. res, err = s.(*SQLiteStmt).exec(ctx, args[:na])
  727. if err != nil && err != driver.ErrSkip {
  728. s.Close()
  729. return nil, err
  730. }
  731. args = args[na:]
  732. start += na
  733. }
  734. tail := s.(*SQLiteStmt).t
  735. s.Close()
  736. if tail == "" {
  737. return res, nil
  738. }
  739. query = tail
  740. }
  741. }
  742. type namedValue struct {
  743. Name string
  744. Ordinal int
  745. Value driver.Value
  746. }
  747. // Query implements Queryer.
  748. func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
  749. list := make([]namedValue, len(args))
  750. for i, v := range args {
  751. list[i] = namedValue{
  752. Ordinal: i + 1,
  753. Value: v,
  754. }
  755. }
  756. return c.query(context.Background(), query, list)
  757. }
  758. func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) {
  759. start := 0
  760. for {
  761. s, err := c.prepare(ctx, query)
  762. if err != nil {
  763. return nil, err
  764. }
  765. s.(*SQLiteStmt).cls = true
  766. na := s.NumInput()
  767. if len(args) < na {
  768. return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
  769. }
  770. for i := 0; i < na; i++ {
  771. args[i].Ordinal -= start
  772. }
  773. rows, err := s.(*SQLiteStmt).query(ctx, args[:na])
  774. if err != nil && err != driver.ErrSkip {
  775. s.Close()
  776. return rows, err
  777. }
  778. args = args[na:]
  779. start += na
  780. tail := s.(*SQLiteStmt).t
  781. if tail == "" {
  782. return rows, nil
  783. }
  784. rows.Close()
  785. s.Close()
  786. query = tail
  787. }
  788. }
  789. // Begin transaction.
  790. func (c *SQLiteConn) Begin() (driver.Tx, error) {
  791. return c.begin(context.Background())
  792. }
  793. func (c *SQLiteConn) begin(ctx context.Context) (driver.Tx, error) {
  794. if _, err := c.exec(ctx, c.txlock, nil); err != nil {
  795. return nil, err
  796. }
  797. return &SQLiteTx{c}, nil
  798. }
  799. // Open database and return a new connection.
  800. //
  801. // A pragma can take either zero or one argument.
  802. // The argument is may be either in parentheses or it may be separated from
  803. // the pragma name by an equal sign. The two syntaxes yield identical results.
  804. // In many pragmas, the argument is a boolean. The boolean can be one of:
  805. // 1 yes true on
  806. // 0 no false off
  807. //
  808. // You can specify a DSN string using a URI as the filename.
  809. // test.db
  810. // file:test.db?cache=shared&mode=memory
  811. // :memory:
  812. // file::memory:
  813. //
  814. // mode
  815. // Access mode of the database.
  816. // https://www.sqlite.org/c3ref/open.html
  817. // Values:
  818. // - ro
  819. // - rw
  820. // - rwc
  821. // - memory
  822. //
  823. // cache
  824. // SQLite Shared-Cache Mode
  825. // https://www.sqlite.org/sharedcache.html
  826. // Values:
  827. // - shared
  828. // - private
  829. //
  830. // immutable=Boolean
  831. // The immutable parameter is a boolean query parameter that indicates
  832. // that the database file is stored on read-only media. When immutable is set,
  833. // SQLite assumes that the database file cannot be changed,
  834. // even by a process with higher privilege,
  835. // and so the database is opened read-only and all locking and change detection is disabled.
  836. // Caution: Setting the immutable property on a database file that
  837. // does in fact change can result in incorrect query results and/or SQLITE_CORRUPT errors.
  838. //
  839. // go-sqlite3 adds the following query parameters to those used by SQLite:
  840. // _loc=XXX
  841. // Specify location of time format. It's possible to specify "auto".
  842. //
  843. // _mutex=XXX
  844. // Specify mutex mode. XXX can be "no", "full".
  845. //
  846. // _txlock=XXX
  847. // Specify locking behavior for transactions. XXX can be "immediate",
  848. // "deferred", "exclusive".
  849. //
  850. // _auto_vacuum=X | _vacuum=X
  851. // 0 | none - Auto Vacuum disabled
  852. // 1 | full - Auto Vacuum FULL
  853. // 2 | incremental - Auto Vacuum Incremental
  854. //
  855. // _busy_timeout=XXX"| _timeout=XXX
  856. // Specify value for sqlite3_busy_timeout.
  857. //
  858. // _case_sensitive_like=Boolean | _cslike=Boolean
  859. // https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
  860. // Default or disabled the LIKE operation is case-insensitive.
  861. // When enabling this options behaviour of LIKE will become case-sensitive.
  862. //
  863. // _defer_foreign_keys=Boolean | _defer_fk=Boolean
  864. // Defer Foreign Keys until outermost transaction is committed.
  865. //
  866. // _foreign_keys=Boolean | _fk=Boolean
  867. // Enable or disable enforcement of foreign keys.
  868. //
  869. // _ignore_check_constraints=Boolean
  870. // This pragma enables or disables the enforcement of CHECK constraints.
  871. // The default setting is off, meaning that CHECK constraints are enforced by default.
  872. //
  873. // _journal_mode=MODE | _journal=MODE
  874. // Set journal mode for the databases associated with the current connection.
  875. // https://www.sqlite.org/pragma.html#pragma_journal_mode
  876. //
  877. // _locking_mode=X | _locking=X
  878. // Sets the database connection locking-mode.
  879. // The locking-mode is either NORMAL or EXCLUSIVE.
  880. // https://www.sqlite.org/pragma.html#pragma_locking_mode
  881. //
  882. // _query_only=Boolean
  883. // The query_only pragma prevents all changes to database files when enabled.
  884. //
  885. // _recursive_triggers=Boolean | _rt=Boolean
  886. // Enable or disable recursive triggers.
  887. //
  888. // _secure_delete=Boolean|FAST
  889. // When secure_delete is on, SQLite overwrites deleted content with zeros.
  890. // https://www.sqlite.org/pragma.html#pragma_secure_delete
  891. //
  892. // _synchronous=X | _sync=X
  893. // Change the setting of the "synchronous" flag.
  894. // https://www.sqlite.org/pragma.html#pragma_synchronous
  895. //
  896. // _writable_schema=Boolean
  897. // When this pragma is on, the SQLITE_MASTER tables in which database
  898. // can be changed using ordinary UPDATE, INSERT, and DELETE statements.
  899. // Warning: misuse of this pragma can easily result in a corrupt database file.
  900. //
  901. //
  902. func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
  903. if C.sqlite3_threadsafe() == 0 {
  904. return nil, errors.New("sqlite library was not compiled for thread-safe operation")
  905. }
  906. var pkey string
  907. // Options
  908. var loc *time.Location
  909. authCreate := false
  910. authUser := ""
  911. authPass := ""
  912. authCrypt := ""
  913. authSalt := ""
  914. mutex := C.int(C.SQLITE_OPEN_FULLMUTEX)
  915. txlock := "BEGIN"
  916. // PRAGMA's
  917. autoVacuum := -1
  918. busyTimeout := 5000
  919. caseSensitiveLike := -1
  920. deferForeignKeys := -1
  921. foreignKeys := -1
  922. ignoreCheckConstraints := -1
  923. var journalMode string
  924. lockingMode := "NORMAL"
  925. queryOnly := -1
  926. recursiveTriggers := -1
  927. secureDelete := "DEFAULT"
  928. synchronousMode := "NORMAL"
  929. writableSchema := -1
  930. pos := strings.IndexRune(dsn, '?')
  931. if pos >= 1 {
  932. params, err := url.ParseQuery(dsn[pos+1:])
  933. if err != nil {
  934. return nil, err
  935. }
  936. // Authentication
  937. if _, ok := params["_auth"]; ok {
  938. authCreate = true
  939. }
  940. if val := params.Get("_auth_user"); val != "" {
  941. authUser = val
  942. }
  943. if val := params.Get("_auth_pass"); val != "" {
  944. authPass = val
  945. }
  946. if val := params.Get("_auth_crypt"); val != "" {
  947. authCrypt = val
  948. }
  949. if val := params.Get("_auth_salt"); val != "" {
  950. authSalt = val
  951. }
  952. // _loc
  953. if val := params.Get("_loc"); val != "" {
  954. switch strings.ToLower(val) {
  955. case "auto":
  956. loc = time.Local
  957. default:
  958. loc, err = time.LoadLocation(val)
  959. if err != nil {
  960. return nil, fmt.Errorf("Invalid _loc: %v: %v", val, err)
  961. }
  962. }
  963. }
  964. // _mutex
  965. if val := params.Get("_mutex"); val != "" {
  966. switch strings.ToLower(val) {
  967. case "no":
  968. mutex = C.SQLITE_OPEN_NOMUTEX
  969. case "full":
  970. mutex = C.SQLITE_OPEN_FULLMUTEX
  971. default:
  972. return nil, fmt.Errorf("Invalid _mutex: %v", val)
  973. }
  974. }
  975. // _txlock
  976. if val := params.Get("_txlock"); val != "" {
  977. switch strings.ToLower(val) {
  978. case "immediate":
  979. txlock = "BEGIN IMMEDIATE"
  980. case "exclusive":
  981. txlock = "BEGIN EXCLUSIVE"
  982. case "deferred":
  983. txlock = "BEGIN"
  984. default:
  985. return nil, fmt.Errorf("Invalid _txlock: %v", val)
  986. }
  987. }
  988. // Auto Vacuum (_vacuum)
  989. //
  990. // https://www.sqlite.org/pragma.html#pragma_auto_vacuum
  991. //
  992. pkey = "" // Reset pkey
  993. if _, ok := params["_auto_vacuum"]; ok {
  994. pkey = "_auto_vacuum"
  995. }
  996. if _, ok := params["_vacuum"]; ok {
  997. pkey = "_vacuum"
  998. }
  999. if val := params.Get(pkey); val != "" {
  1000. switch strings.ToLower(val) {
  1001. case "0", "none":
  1002. autoVacuum = 0
  1003. case "1", "full":
  1004. autoVacuum = 1
  1005. case "2", "incremental":
  1006. autoVacuum = 2
  1007. default:
  1008. return nil, fmt.Errorf("Invalid _auto_vacuum: %v, expecting value of '0 NONE 1 FULL 2 INCREMENTAL'", val)
  1009. }
  1010. }
  1011. // Busy Timeout (_busy_timeout)
  1012. //
  1013. // https://www.sqlite.org/pragma.html#pragma_busy_timeout
  1014. //
  1015. pkey = "" // Reset pkey
  1016. if _, ok := params["_busy_timeout"]; ok {
  1017. pkey = "_busy_timeout"
  1018. }
  1019. if _, ok := params["_timeout"]; ok {
  1020. pkey = "_timeout"
  1021. }
  1022. if val := params.Get(pkey); val != "" {
  1023. iv, err := strconv.ParseInt(val, 10, 64)
  1024. if err != nil {
  1025. return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
  1026. }
  1027. busyTimeout = int(iv)
  1028. }
  1029. // Case Sensitive Like (_cslike)
  1030. //
  1031. // https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
  1032. //
  1033. pkey = "" // Reset pkey
  1034. if _, ok := params["_case_sensitive_like"]; ok {
  1035. pkey = "_case_sensitive_like"
  1036. }
  1037. if _, ok := params["_cslike"]; ok {
  1038. pkey = "_cslike"
  1039. }
  1040. if val := params.Get(pkey); val != "" {
  1041. switch strings.ToLower(val) {
  1042. case "0", "no", "false", "off":
  1043. caseSensitiveLike = 0
  1044. case "1", "yes", "true", "on":
  1045. caseSensitiveLike = 1
  1046. default:
  1047. return nil, fmt.Errorf("Invalid _case_sensitive_like: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1048. }
  1049. }
  1050. // Defer Foreign Keys (_defer_foreign_keys | _defer_fk)
  1051. //
  1052. // https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys
  1053. //
  1054. pkey = "" // Reset pkey
  1055. if _, ok := params["_defer_foreign_keys"]; ok {
  1056. pkey = "_defer_foreign_keys"
  1057. }
  1058. if _, ok := params["_defer_fk"]; ok {
  1059. pkey = "_defer_fk"
  1060. }
  1061. if val := params.Get(pkey); val != "" {
  1062. switch strings.ToLower(val) {
  1063. case "0", "no", "false", "off":
  1064. deferForeignKeys = 0
  1065. case "1", "yes", "true", "on":
  1066. deferForeignKeys = 1
  1067. default:
  1068. return nil, fmt.Errorf("Invalid _defer_foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1069. }
  1070. }
  1071. // Foreign Keys (_foreign_keys | _fk)
  1072. //
  1073. // https://www.sqlite.org/pragma.html#pragma_foreign_keys
  1074. //
  1075. pkey = "" // Reset pkey
  1076. if _, ok := params["_foreign_keys"]; ok {
  1077. pkey = "_foreign_keys"
  1078. }
  1079. if _, ok := params["_fk"]; ok {
  1080. pkey = "_fk"
  1081. }
  1082. if val := params.Get(pkey); val != "" {
  1083. switch strings.ToLower(val) {
  1084. case "0", "no", "false", "off":
  1085. foreignKeys = 0
  1086. case "1", "yes", "true", "on":
  1087. foreignKeys = 1
  1088. default:
  1089. return nil, fmt.Errorf("Invalid _foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1090. }
  1091. }
  1092. // Ignore CHECK Constrains (_ignore_check_constraints)
  1093. //
  1094. // https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints
  1095. //
  1096. if val := params.Get("_ignore_check_constraints"); val != "" {
  1097. switch strings.ToLower(val) {
  1098. case "0", "no", "false", "off":
  1099. ignoreCheckConstraints = 0
  1100. case "1", "yes", "true", "on":
  1101. ignoreCheckConstraints = 1
  1102. default:
  1103. return nil, fmt.Errorf("Invalid _ignore_check_constraints: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1104. }
  1105. }
  1106. // Journal Mode (_journal_mode | _journal)
  1107. //
  1108. // https://www.sqlite.org/pragma.html#pragma_journal_mode
  1109. //
  1110. pkey = "" // Reset pkey
  1111. if _, ok := params["_journal_mode"]; ok {
  1112. pkey = "_journal_mode"
  1113. }
  1114. if _, ok := params["_journal"]; ok {
  1115. pkey = "_journal"
  1116. }
  1117. if val := params.Get(pkey); val != "" {
  1118. switch strings.ToUpper(val) {
  1119. case "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "OFF":
  1120. journalMode = strings.ToUpper(val)
  1121. case "WAL":
  1122. journalMode = strings.ToUpper(val)
  1123. // For WAL Mode set Synchronous Mode to 'NORMAL'
  1124. // See https://www.sqlite.org/pragma.html#pragma_synchronous
  1125. synchronousMode = "NORMAL"
  1126. default:
  1127. return nil, fmt.Errorf("Invalid _journal: %v, expecting value of 'DELETE TRUNCATE PERSIST MEMORY WAL OFF'", val)
  1128. }
  1129. }
  1130. // Locking Mode (_locking)
  1131. //
  1132. // https://www.sqlite.org/pragma.html#pragma_locking_mode
  1133. //
  1134. pkey = "" // Reset pkey
  1135. if _, ok := params["_locking_mode"]; ok {
  1136. pkey = "_locking_mode"
  1137. }
  1138. if _, ok := params["_locking"]; ok {
  1139. pkey = "_locking"
  1140. }
  1141. if val := params.Get(pkey); val != "" {
  1142. switch strings.ToUpper(val) {
  1143. case "NORMAL", "EXCLUSIVE":
  1144. lockingMode = strings.ToUpper(val)
  1145. default:
  1146. return nil, fmt.Errorf("Invalid _locking_mode: %v, expecting value of 'NORMAL EXCLUSIVE", val)
  1147. }
  1148. }
  1149. // Query Only (_query_only)
  1150. //
  1151. // https://www.sqlite.org/pragma.html#pragma_query_only
  1152. //
  1153. if val := params.Get("_query_only"); val != "" {
  1154. switch strings.ToLower(val) {
  1155. case "0", "no", "false", "off":
  1156. queryOnly = 0
  1157. case "1", "yes", "true", "on":
  1158. queryOnly = 1
  1159. default:
  1160. return nil, fmt.Errorf("Invalid _query_only: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1161. }
  1162. }
  1163. // Recursive Triggers (_recursive_triggers)
  1164. //
  1165. // https://www.sqlite.org/pragma.html#pragma_recursive_triggers
  1166. //
  1167. pkey = "" // Reset pkey
  1168. if _, ok := params["_recursive_triggers"]; ok {
  1169. pkey = "_recursive_triggers"
  1170. }
  1171. if _, ok := params["_rt"]; ok {
  1172. pkey = "_rt"
  1173. }
  1174. if val := params.Get(pkey); val != "" {
  1175. switch strings.ToLower(val) {
  1176. case "0", "no", "false", "off":
  1177. recursiveTriggers = 0
  1178. case "1", "yes", "true", "on":
  1179. recursiveTriggers = 1
  1180. default:
  1181. return nil, fmt.Errorf("Invalid _recursive_triggers: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1182. }
  1183. }
  1184. // Secure Delete (_secure_delete)
  1185. //
  1186. // https://www.sqlite.org/pragma.html#pragma_secure_delete
  1187. //
  1188. if val := params.Get("_secure_delete"); val != "" {
  1189. switch strings.ToLower(val) {
  1190. case "0", "no", "false", "off":
  1191. secureDelete = "OFF"
  1192. case "1", "yes", "true", "on":
  1193. secureDelete = "ON"
  1194. case "fast":
  1195. secureDelete = "FAST"
  1196. default:
  1197. return nil, fmt.Errorf("Invalid _secure_delete: %v, expecting boolean value of '0 1 false true no yes off on fast'", val)
  1198. }
  1199. }
  1200. // Synchronous Mode (_synchronous | _sync)
  1201. //
  1202. // https://www.sqlite.org/pragma.html#pragma_synchronous
  1203. //
  1204. pkey = "" // Reset pkey
  1205. if _, ok := params["_synchronous"]; ok {
  1206. pkey = "_synchronous"
  1207. }
  1208. if _, ok := params["_sync"]; ok {
  1209. pkey = "_sync"
  1210. }
  1211. if val := params.Get(pkey); val != "" {
  1212. switch strings.ToUpper(val) {
  1213. case "0", "OFF", "1", "NORMAL", "2", "FULL", "3", "EXTRA":
  1214. synchronousMode = strings.ToUpper(val)
  1215. default:
  1216. return nil, fmt.Errorf("Invalid _synchronous: %v, expecting value of '0 OFF 1 NORMAL 2 FULL 3 EXTRA'", val)
  1217. }
  1218. }
  1219. // Writable Schema (_writeable_schema)
  1220. //
  1221. // https://www.sqlite.org/pragma.html#pragma_writeable_schema
  1222. //
  1223. if val := params.Get("_writable_schema"); val != "" {
  1224. switch strings.ToLower(val) {
  1225. case "0", "no", "false", "off":
  1226. writableSchema = 0
  1227. case "1", "yes", "true", "on":
  1228. writableSchema = 1
  1229. default:
  1230. return nil, fmt.Errorf("Invalid _writable_schema: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1231. }
  1232. }
  1233. if !strings.HasPrefix(dsn, "file:") {
  1234. dsn = dsn[:pos]
  1235. }
  1236. }
  1237. var db *C.sqlite3
  1238. name := C.CString(dsn)
  1239. defer C.free(unsafe.Pointer(name))
  1240. rv := C._sqlite3_open_v2(name, &db,
  1241. mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
  1242. nil)
  1243. if rv != 0 {
  1244. // Save off the error _before_ closing the database.
  1245. // This is safe even if db is nil.
  1246. err := lastError(db)
  1247. if db != nil {
  1248. C.sqlite3_close_v2(db)
  1249. }
  1250. return nil, err
  1251. }
  1252. if db == nil {
  1253. return nil, errors.New("sqlite succeeded without returning a database")
  1254. }
  1255. rv = C.sqlite3_busy_timeout(db, C.int(busyTimeout))
  1256. if rv != C.SQLITE_OK {
  1257. C.sqlite3_close_v2(db)
  1258. return nil, Error{Code: ErrNo(rv)}
  1259. }
  1260. exec := func(s string) error {
  1261. cs := C.CString(s)
  1262. rv := C.sqlite3_exec(db, cs, nil, nil, nil)
  1263. C.free(unsafe.Pointer(cs))
  1264. if rv != C.SQLITE_OK {
  1265. return lastError(db)
  1266. }
  1267. return nil
  1268. }
  1269. // USER AUTHENTICATION
  1270. //
  1271. // User Authentication is always performed even when
  1272. // sqlite_userauth is not compiled in, because without user authentication
  1273. // the authentication is a no-op.
  1274. //
  1275. // Workflow
  1276. // - Authenticate
  1277. // ON::SUCCESS => Continue
  1278. // ON::SQLITE_AUTH => Return error and exit Open(...)
  1279. //
  1280. // - Activate User Authentication
  1281. // Check if the user wants to activate User Authentication.
  1282. // If so then first create a temporary AuthConn to the database
  1283. // This is possible because we are already successfully authenticated.
  1284. //
  1285. // - Check if `sqlite_user`` table exists
  1286. // YES => Add the provided user from DSN as Admin User and
  1287. // activate user authentication.
  1288. // NO => Continue
  1289. //
  1290. // Create connection to SQLite
  1291. conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
  1292. // Password Cipher has to be registered before authentication
  1293. if len(authCrypt) > 0 {
  1294. switch strings.ToUpper(authCrypt) {
  1295. case "SHA1":
  1296. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA1, true); err != nil {
  1297. return nil, fmt.Errorf("CryptEncoderSHA1: %s", err)
  1298. }
  1299. case "SSHA1":
  1300. if len(authSalt) == 0 {
  1301. return nil, fmt.Errorf("_auth_crypt=ssha1, requires _auth_salt")
  1302. }
  1303. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA1(authSalt), true); err != nil {
  1304. return nil, fmt.Errorf("CryptEncoderSSHA1: %s", err)
  1305. }
  1306. case "SHA256":
  1307. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA256, true); err != nil {
  1308. return nil, fmt.Errorf("CryptEncoderSHA256: %s", err)
  1309. }
  1310. case "SSHA256":
  1311. if len(authSalt) == 0 {
  1312. return nil, fmt.Errorf("_auth_crypt=ssha256, requires _auth_salt")
  1313. }
  1314. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA256(authSalt), true); err != nil {
  1315. return nil, fmt.Errorf("CryptEncoderSSHA256: %s", err)
  1316. }
  1317. case "SHA384":
  1318. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA384, true); err != nil {
  1319. return nil, fmt.Errorf("CryptEncoderSHA384: %s", err)
  1320. }
  1321. case "SSHA384":
  1322. if len(authSalt) == 0 {
  1323. return nil, fmt.Errorf("_auth_crypt=ssha384, requires _auth_salt")
  1324. }
  1325. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA384(authSalt), true); err != nil {
  1326. return nil, fmt.Errorf("CryptEncoderSSHA384: %s", err)
  1327. }
  1328. case "SHA512":
  1329. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA512, true); err != nil {
  1330. return nil, fmt.Errorf("CryptEncoderSHA512: %s", err)
  1331. }
  1332. case "SSHA512":
  1333. if len(authSalt) == 0 {
  1334. return nil, fmt.Errorf("_auth_crypt=ssha512, requires _auth_salt")
  1335. }
  1336. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA512(authSalt), true); err != nil {
  1337. return nil, fmt.Errorf("CryptEncoderSSHA512: %s", err)
  1338. }
  1339. }
  1340. }
  1341. // Preform Authentication
  1342. if err := conn.Authenticate(authUser, authPass); err != nil {
  1343. return nil, err
  1344. }
  1345. // Register: authenticate
  1346. // Authenticate will perform an authentication of the provided username
  1347. // and password against the database.
  1348. //
  1349. // If a database contains the SQLITE_USER table, then the
  1350. // call to Authenticate must be invoked with an
  1351. // appropriate username and password prior to enable read and write
  1352. //access to the database.
  1353. //
  1354. // Return SQLITE_OK on success or SQLITE_ERROR if the username/password
  1355. // combination is incorrect or unknown.
  1356. //
  1357. // If the SQLITE_USER table is not present in the database file, then
  1358. // this interface is a harmless no-op returnning SQLITE_OK.
  1359. if err := conn.RegisterFunc("authenticate", conn.authenticate, true); err != nil {
  1360. return nil, err
  1361. }
  1362. //
  1363. // Register: auth_user_add
  1364. // auth_user_add can be used (by an admin user only)
  1365. // to create a new user. When called on a no-authentication-required
  1366. // database, this routine converts the database into an authentication-
  1367. // required database, automatically makes the added user an
  1368. // administrator, and logs in the current connection as that user.
  1369. // The AuthUserAdd only works for the "main" database, not
  1370. // for any ATTACH-ed databases. Any call to AuthUserAdd by a
  1371. // non-admin user results in an error.
  1372. if err := conn.RegisterFunc("auth_user_add", conn.authUserAdd, true); err != nil {
  1373. return nil, err
  1374. }
  1375. //
  1376. // Register: auth_user_change
  1377. // auth_user_change can be used to change a users
  1378. // login credentials or admin privilege. Any user can change their own
  1379. // login credentials. Only an admin user can change another users login
  1380. // credentials or admin privilege setting. No user may change their own
  1381. // admin privilege setting.
  1382. if err := conn.RegisterFunc("auth_user_change", conn.authUserChange, true); err != nil {
  1383. return nil, err
  1384. }
  1385. //
  1386. // Register: auth_user_delete
  1387. // auth_user_delete can be used (by an admin user only)
  1388. // to delete a user. The currently logged-in user cannot be deleted,
  1389. // which guarantees that there is always an admin user and hence that
  1390. // the database cannot be converted into a no-authentication-required
  1391. // database.
  1392. if err := conn.RegisterFunc("auth_user_delete", conn.authUserDelete, true); err != nil {
  1393. return nil, err
  1394. }
  1395. // Register: auth_enabled
  1396. // auth_enabled can be used to check if user authentication is enabled
  1397. if err := conn.RegisterFunc("auth_enabled", conn.authEnabled, true); err != nil {
  1398. return nil, err
  1399. }
  1400. // Auto Vacuum
  1401. // Moved auto_vacuum command, the user preference for auto_vacuum needs to be implemented directly after
  1402. // the authentication and before the sqlite_user table gets created if the user
  1403. // decides to activate User Authentication because
  1404. // auto_vacuum needs to be set before any tables are created
  1405. // and activating user authentication creates the internal table `sqlite_user`.
  1406. if autoVacuum > -1 {
  1407. if err := exec(fmt.Sprintf("PRAGMA auto_vacuum = %d;", autoVacuum)); err != nil {
  1408. C.sqlite3_close_v2(db)
  1409. return nil, err
  1410. }
  1411. }
  1412. // Check if user wants to activate User Authentication
  1413. if authCreate {
  1414. // Before going any further, we need to check that the user
  1415. // has provided an username and password within the DSN.
  1416. // We are not allowed to continue.
  1417. if len(authUser) == 0 {
  1418. return nil, fmt.Errorf("Missing '_auth_user' while user authentication was requested with '_auth'")
  1419. }
  1420. if len(authPass) == 0 {
  1421. return nil, fmt.Errorf("Missing '_auth_pass' while user authentication was requested with '_auth'")
  1422. }
  1423. // Check if User Authentication is Enabled
  1424. authExists := conn.AuthEnabled()
  1425. if !authExists {
  1426. if err := conn.AuthUserAdd(authUser, authPass, true); err != nil {
  1427. return nil, err
  1428. }
  1429. }
  1430. }
  1431. // Case Sensitive LIKE
  1432. if caseSensitiveLike > -1 {
  1433. if err := exec(fmt.Sprintf("PRAGMA case_sensitive_like = %d;", caseSensitiveLike)); err != nil {
  1434. C.sqlite3_close_v2(db)
  1435. return nil, err
  1436. }
  1437. }
  1438. // Defer Foreign Keys
  1439. if deferForeignKeys > -1 {
  1440. if err := exec(fmt.Sprintf("PRAGMA defer_foreign_keys = %d;", deferForeignKeys)); err != nil {
  1441. C.sqlite3_close_v2(db)
  1442. return nil, err
  1443. }
  1444. }
  1445. // Forgein Keys
  1446. if foreignKeys > -1 {
  1447. if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil {
  1448. C.sqlite3_close_v2(db)
  1449. return nil, err
  1450. }
  1451. }
  1452. // Ignore CHECK Constraints
  1453. if ignoreCheckConstraints > -1 {
  1454. if err := exec(fmt.Sprintf("PRAGMA ignore_check_constraints = %d;", ignoreCheckConstraints)); err != nil {
  1455. C.sqlite3_close_v2(db)
  1456. return nil, err
  1457. }
  1458. }
  1459. // Journal Mode
  1460. if journalMode != "" {
  1461. if err := exec(fmt.Sprintf("PRAGMA journal_mode = %s;", journalMode)); err != nil {
  1462. C.sqlite3_close_v2(db)
  1463. return nil, err
  1464. }
  1465. }
  1466. // Locking Mode
  1467. // Because the default is NORMAL and this is not changed in this package
  1468. // by using the compile time SQLITE_DEFAULT_LOCKING_MODE this PRAGMA can always be executed
  1469. if err := exec(fmt.Sprintf("PRAGMA locking_mode = %s;", lockingMode)); err != nil {
  1470. C.sqlite3_close_v2(db)
  1471. return nil, err
  1472. }
  1473. // Query Only
  1474. if queryOnly > -1 {
  1475. if err := exec(fmt.Sprintf("PRAGMA query_only = %d;", queryOnly)); err != nil {
  1476. C.sqlite3_close_v2(db)
  1477. return nil, err
  1478. }
  1479. }
  1480. // Recursive Triggers
  1481. if recursiveTriggers > -1 {
  1482. if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {
  1483. C.sqlite3_close_v2(db)
  1484. return nil, err
  1485. }
  1486. }
  1487. // Secure Delete
  1488. //
  1489. // Because this package can set the compile time flag SQLITE_SECURE_DELETE with a build tag
  1490. // the default value for secureDelete var is 'DEFAULT' this way
  1491. // you can compile with secure_delete 'ON' and disable it for a specific database connection.
  1492. if secureDelete != "DEFAULT" {
  1493. if err := exec(fmt.Sprintf("PRAGMA secure_delete = %s;", secureDelete)); err != nil {
  1494. C.sqlite3_close_v2(db)
  1495. return nil, err
  1496. }
  1497. }
  1498. // Synchronous Mode
  1499. //
  1500. // Because default is NORMAL this statement is always executed
  1501. if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
  1502. C.sqlite3_close_v2(db)
  1503. return nil, err
  1504. }
  1505. // Writable Schema
  1506. if writableSchema > -1 {
  1507. if err := exec(fmt.Sprintf("PRAGMA writable_schema = %d;", writableSchema)); err != nil {
  1508. C.sqlite3_close_v2(db)
  1509. return nil, err
  1510. }
  1511. }
  1512. if len(d.Extensions) > 0 {
  1513. if err := conn.loadExtensions(d.Extensions); err != nil {
  1514. conn.Close()
  1515. return nil, err
  1516. }
  1517. }
  1518. if d.ConnectHook != nil {
  1519. if err := d.ConnectHook(conn); err != nil {
  1520. conn.Close()
  1521. return nil, err
  1522. }
  1523. }
  1524. runtime.SetFinalizer(conn, (*SQLiteConn).Close)
  1525. return conn, nil
  1526. }
  1527. // Close the connection.
  1528. func (c *SQLiteConn) Close() error {
  1529. rv := C.sqlite3_close_v2(c.db)
  1530. if rv != C.SQLITE_OK {
  1531. return c.lastError()
  1532. }
  1533. deleteHandles(c)
  1534. c.mu.Lock()
  1535. c.db = nil
  1536. c.mu.Unlock()
  1537. runtime.SetFinalizer(c, nil)
  1538. return nil
  1539. }
  1540. func (c *SQLiteConn) dbConnOpen() bool {
  1541. if c == nil {
  1542. return false
  1543. }
  1544. c.mu.Lock()
  1545. defer c.mu.Unlock()
  1546. return c.db != nil
  1547. }
  1548. // Prepare the query string. Return a new statement.
  1549. func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
  1550. return c.prepare(context.Background(), query)
  1551. }
  1552. func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
  1553. pquery := C.CString(query)
  1554. defer C.free(unsafe.Pointer(pquery))
  1555. var s *C.sqlite3_stmt
  1556. var tail *C.char
  1557. rv := C._sqlite3_prepare_v2_internal(c.db, pquery, C.int(-1), &s, &tail)
  1558. if rv != C.SQLITE_OK {
  1559. return nil, c.lastError()
  1560. }
  1561. var t string
  1562. if tail != nil && *tail != '\000' {
  1563. t = strings.TrimSpace(C.GoString(tail))
  1564. }
  1565. ss := &SQLiteStmt{c: c, s: s, t: t}
  1566. runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
  1567. return ss, nil
  1568. }
  1569. // Run-Time Limit Categories.
  1570. // See: http://www.sqlite.org/c3ref/c_limit_attached.html
  1571. const (
  1572. SQLITE_LIMIT_LENGTH = C.SQLITE_LIMIT_LENGTH
  1573. SQLITE_LIMIT_SQL_LENGTH = C.SQLITE_LIMIT_SQL_LENGTH
  1574. SQLITE_LIMIT_COLUMN = C.SQLITE_LIMIT_COLUMN
  1575. SQLITE_LIMIT_EXPR_DEPTH = C.SQLITE_LIMIT_EXPR_DEPTH
  1576. SQLITE_LIMIT_COMPOUND_SELECT = C.SQLITE_LIMIT_COMPOUND_SELECT
  1577. SQLITE_LIMIT_VDBE_OP = C.SQLITE_LIMIT_VDBE_OP
  1578. SQLITE_LIMIT_FUNCTION_ARG = C.SQLITE_LIMIT_FUNCTION_ARG
  1579. SQLITE_LIMIT_ATTACHED = C.SQLITE_LIMIT_ATTACHED
  1580. SQLITE_LIMIT_LIKE_PATTERN_LENGTH = C.SQLITE_LIMIT_LIKE_PATTERN_LENGTH
  1581. SQLITE_LIMIT_VARIABLE_NUMBER = C.SQLITE_LIMIT_VARIABLE_NUMBER
  1582. SQLITE_LIMIT_TRIGGER_DEPTH = C.SQLITE_LIMIT_TRIGGER_DEPTH
  1583. SQLITE_LIMIT_WORKER_THREADS = C.SQLITE_LIMIT_WORKER_THREADS
  1584. )
  1585. // GetFilename returns the absolute path to the file containing
  1586. // the requested schema. When passed an empty string, it will
  1587. // instead use the database's default schema: "main".
  1588. // See: sqlite3_db_filename, https://www.sqlite.org/c3ref/db_filename.html
  1589. func (c *SQLiteConn) GetFilename(schemaName string) string {
  1590. if schemaName == "" {
  1591. schemaName = "main"
  1592. }
  1593. return C.GoString(C.sqlite3_db_filename(c.db, C.CString(schemaName)))
  1594. }
  1595. // GetLimit returns the current value of a run-time limit.
  1596. // See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
  1597. func (c *SQLiteConn) GetLimit(id int) int {
  1598. return int(C._sqlite3_limit(c.db, C.int(id), C.int(-1)))
  1599. }
  1600. // SetLimit changes the value of a run-time limits.
  1601. // Then this method returns the prior value of the limit.
  1602. // See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
  1603. func (c *SQLiteConn) SetLimit(id int, newVal int) int {
  1604. return int(C._sqlite3_limit(c.db, C.int(id), C.int(newVal)))
  1605. }
  1606. // Close the statement.
  1607. func (s *SQLiteStmt) Close() error {
  1608. s.mu.Lock()
  1609. defer s.mu.Unlock()
  1610. if s.closed {
  1611. return nil
  1612. }
  1613. s.closed = true
  1614. if !s.c.dbConnOpen() {
  1615. return errors.New("sqlite statement with already closed database connection")
  1616. }
  1617. rv := C.sqlite3_finalize(s.s)
  1618. s.s = nil
  1619. if rv != C.SQLITE_OK {
  1620. return s.c.lastError()
  1621. }
  1622. runtime.SetFinalizer(s, nil)
  1623. return nil
  1624. }
  1625. // NumInput return a number of parameters.
  1626. func (s *SQLiteStmt) NumInput() int {
  1627. return int(C.sqlite3_bind_parameter_count(s.s))
  1628. }
  1629. type bindArg struct {
  1630. n int
  1631. v driver.Value
  1632. }
  1633. var placeHolder = []byte{0}
  1634. func (s *SQLiteStmt) bind(args []namedValue) error {
  1635. rv := C.sqlite3_reset(s.s)
  1636. if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
  1637. return s.c.lastError()
  1638. }
  1639. for i, v := range args {
  1640. if v.Name != "" {
  1641. cname := C.CString(":" + v.Name)
  1642. args[i].Ordinal = int(C.sqlite3_bind_parameter_index(s.s, cname))
  1643. C.free(unsafe.Pointer(cname))
  1644. }
  1645. }
  1646. for _, arg := range args {
  1647. n := C.int(arg.Ordinal)
  1648. switch v := arg.Value.(type) {
  1649. case nil:
  1650. rv = C.sqlite3_bind_null(s.s, n)
  1651. case string:
  1652. if len(v) == 0 {
  1653. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&placeHolder[0])), C.int(0))
  1654. } else {
  1655. b := []byte(v)
  1656. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
  1657. }
  1658. case int64:
  1659. rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
  1660. case bool:
  1661. if v {
  1662. rv = C.sqlite3_bind_int(s.s, n, 1)
  1663. } else {
  1664. rv = C.sqlite3_bind_int(s.s, n, 0)
  1665. }
  1666. case float64:
  1667. rv = C.sqlite3_bind_double(s.s, n, C.double(v))
  1668. case []byte:
  1669. if v == nil {
  1670. rv = C.sqlite3_bind_null(s.s, n)
  1671. } else {
  1672. ln := len(v)
  1673. if ln == 0 {
  1674. v = placeHolder
  1675. }
  1676. rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
  1677. }
  1678. case time.Time:
  1679. b := []byte(v.Format(SQLiteTimestampFormats[0]))
  1680. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
  1681. }
  1682. if rv != C.SQLITE_OK {
  1683. return s.c.lastError()
  1684. }
  1685. }
  1686. return nil
  1687. }
  1688. // Query the statement with arguments. Return records.
  1689. func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
  1690. list := make([]namedValue, len(args))
  1691. for i, v := range args {
  1692. list[i] = namedValue{
  1693. Ordinal: i + 1,
  1694. Value: v,
  1695. }
  1696. }
  1697. return s.query(context.Background(), list)
  1698. }
  1699. func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) {
  1700. if err := s.bind(args); err != nil {
  1701. return nil, err
  1702. }
  1703. rows := &SQLiteRows{
  1704. s: s,
  1705. nc: int(C.sqlite3_column_count(s.s)),
  1706. cols: nil,
  1707. decltype: nil,
  1708. cls: s.cls,
  1709. closed: false,
  1710. ctx: ctx,
  1711. }
  1712. return rows, nil
  1713. }
  1714. // LastInsertId return last inserted ID.
  1715. func (r *SQLiteResult) LastInsertId() (int64, error) {
  1716. return r.id, nil
  1717. }
  1718. // RowsAffected return how many rows affected.
  1719. func (r *SQLiteResult) RowsAffected() (int64, error) {
  1720. return r.changes, nil
  1721. }
  1722. // Exec execute the statement with arguments. Return result object.
  1723. func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
  1724. list := make([]namedValue, len(args))
  1725. for i, v := range args {
  1726. list[i] = namedValue{
  1727. Ordinal: i + 1,
  1728. Value: v,
  1729. }
  1730. }
  1731. return s.exec(context.Background(), list)
  1732. }
  1733. // exec executes a query that doesn't return rows. Attempts to honor context timeout.
  1734. func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
  1735. if ctx.Done() == nil {
  1736. return s.execSync(args)
  1737. }
  1738. type result struct {
  1739. r driver.Result
  1740. err error
  1741. }
  1742. resultCh := make(chan result)
  1743. go func() {
  1744. r, err := s.execSync(args)
  1745. resultCh <- result{r, err}
  1746. }()
  1747. select {
  1748. case rv := <-resultCh:
  1749. return rv.r, rv.err
  1750. case <-ctx.Done():
  1751. select {
  1752. case <-resultCh: // no need to interrupt
  1753. default:
  1754. // this is still racy and can be no-op if executed between sqlite3_* calls in execSync.
  1755. C.sqlite3_interrupt(s.c.db)
  1756. <-resultCh // ensure goroutine completed
  1757. }
  1758. return nil, ctx.Err()
  1759. }
  1760. }
  1761. func (s *SQLiteStmt) execSync(args []namedValue) (driver.Result, error) {
  1762. if err := s.bind(args); err != nil {
  1763. C.sqlite3_reset(s.s)
  1764. C.sqlite3_clear_bindings(s.s)
  1765. return nil, err
  1766. }
  1767. var rowid, changes C.longlong
  1768. rv := C._sqlite3_step_row_internal(s.s, &rowid, &changes)
  1769. if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
  1770. err := s.c.lastError()
  1771. C.sqlite3_reset(s.s)
  1772. C.sqlite3_clear_bindings(s.s)
  1773. return nil, err
  1774. }
  1775. return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
  1776. }
  1777. // Close the rows.
  1778. func (rc *SQLiteRows) Close() error {
  1779. rc.s.mu.Lock()
  1780. if rc.s.closed || rc.closed {
  1781. rc.s.mu.Unlock()
  1782. return nil
  1783. }
  1784. rc.closed = true
  1785. if rc.cls {
  1786. rc.s.mu.Unlock()
  1787. return rc.s.Close()
  1788. }
  1789. rv := C.sqlite3_reset(rc.s.s)
  1790. if rv != C.SQLITE_OK {
  1791. rc.s.mu.Unlock()
  1792. return rc.s.c.lastError()
  1793. }
  1794. rc.s.mu.Unlock()
  1795. return nil
  1796. }
  1797. // Columns return column names.
  1798. func (rc *SQLiteRows) Columns() []string {
  1799. rc.s.mu.Lock()
  1800. defer rc.s.mu.Unlock()
  1801. if rc.s.s != nil && rc.nc != len(rc.cols) {
  1802. rc.cols = make([]string, rc.nc)
  1803. for i := 0; i < rc.nc; i++ {
  1804. rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
  1805. }
  1806. }
  1807. return rc.cols
  1808. }
  1809. func (rc *SQLiteRows) declTypes() []string {
  1810. if rc.s.s != nil && rc.decltype == nil {
  1811. rc.decltype = make([]string, rc.nc)
  1812. for i := 0; i < rc.nc; i++ {
  1813. rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
  1814. }
  1815. }
  1816. return rc.decltype
  1817. }
  1818. // DeclTypes return column types.
  1819. func (rc *SQLiteRows) DeclTypes() []string {
  1820. rc.s.mu.Lock()
  1821. defer rc.s.mu.Unlock()
  1822. return rc.declTypes()
  1823. }
  1824. // Next move cursor to next. Attempts to honor context timeout from QueryContext call.
  1825. func (rc *SQLiteRows) Next(dest []driver.Value) error {
  1826. rc.s.mu.Lock()
  1827. defer rc.s.mu.Unlock()
  1828. if rc.s.closed {
  1829. return io.EOF
  1830. }
  1831. if rc.ctx.Done() == nil {
  1832. return rc.nextSyncLocked(dest)
  1833. }
  1834. resultCh := make(chan error)
  1835. go func() {
  1836. resultCh <- rc.nextSyncLocked(dest)
  1837. }()
  1838. select {
  1839. case err := <-resultCh:
  1840. return err
  1841. case <-rc.ctx.Done():
  1842. select {
  1843. case <-resultCh: // no need to interrupt
  1844. default:
  1845. // this is still racy and can be no-op if executed between sqlite3_* calls in nextSyncLocked.
  1846. C.sqlite3_interrupt(rc.s.c.db)
  1847. <-resultCh // ensure goroutine completed
  1848. }
  1849. return rc.ctx.Err()
  1850. }
  1851. }
  1852. // nextSyncLocked moves cursor to next; must be called with locked mutex.
  1853. func (rc *SQLiteRows) nextSyncLocked(dest []driver.Value) error {
  1854. rv := C._sqlite3_step_internal(rc.s.s)
  1855. if rv == C.SQLITE_DONE {
  1856. return io.EOF
  1857. }
  1858. if rv != C.SQLITE_ROW {
  1859. rv = C.sqlite3_reset(rc.s.s)
  1860. if rv != C.SQLITE_OK {
  1861. return rc.s.c.lastError()
  1862. }
  1863. return nil
  1864. }
  1865. rc.declTypes()
  1866. for i := range dest {
  1867. switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
  1868. case C.SQLITE_INTEGER:
  1869. val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
  1870. switch rc.decltype[i] {
  1871. case columnTimestamp, columnDatetime, columnDate:
  1872. var t time.Time
  1873. // Assume a millisecond unix timestamp if it's 13 digits -- too
  1874. // large to be a reasonable timestamp in seconds.
  1875. if val > 1e12 || val < -1e12 {
  1876. val *= int64(time.Millisecond) // convert ms to nsec
  1877. t = time.Unix(0, val)
  1878. } else {
  1879. t = time.Unix(val, 0)
  1880. }
  1881. t = t.UTC()
  1882. if rc.s.c.loc != nil {
  1883. t = t.In(rc.s.c.loc)
  1884. }
  1885. dest[i] = t
  1886. case "boolean":
  1887. dest[i] = val > 0
  1888. default:
  1889. dest[i] = val
  1890. }
  1891. case C.SQLITE_FLOAT:
  1892. dest[i] = float64(C.sqlite3_column_double(rc.s.s, C.int(i)))
  1893. case C.SQLITE_BLOB:
  1894. p := C.sqlite3_column_blob(rc.s.s, C.int(i))
  1895. if p == nil {
  1896. dest[i] = []byte{}
  1897. continue
  1898. }
  1899. n := C.sqlite3_column_bytes(rc.s.s, C.int(i))
  1900. dest[i] = C.GoBytes(p, n)
  1901. case C.SQLITE_NULL:
  1902. dest[i] = nil
  1903. case C.SQLITE_TEXT:
  1904. var err error
  1905. var timeVal time.Time
  1906. n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
  1907. s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n))
  1908. switch rc.decltype[i] {
  1909. case columnTimestamp, columnDatetime, columnDate:
  1910. var t time.Time
  1911. s = strings.TrimSuffix(s, "Z")
  1912. for _, format := range SQLiteTimestampFormats {
  1913. if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
  1914. t = timeVal
  1915. break
  1916. }
  1917. }
  1918. if err != nil {
  1919. // The column is a time value, so return the zero time on parse failure.
  1920. t = time.Time{}
  1921. }
  1922. if rc.s.c.loc != nil {
  1923. t = t.In(rc.s.c.loc)
  1924. }
  1925. dest[i] = t
  1926. default:
  1927. dest[i] = s
  1928. }
  1929. }
  1930. }
  1931. return nil
  1932. }