sqlite3.go 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  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, 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), 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), 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), 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), 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 unsafe.Pointer, xFunc unsafe.Pointer, xStep unsafe.Pointer, xFinal unsafe.Pointer) C.int {
  547. return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(uintptr(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. stmtArgs := make([]namedValue, 0, len(args))
  719. na := s.NumInput()
  720. if len(args)-start < na {
  721. s.Close()
  722. return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
  723. }
  724. // consume the number of arguments used in the current
  725. // statement and append all named arguments not
  726. // contained therein
  727. stmtArgs = append(stmtArgs, args[start:start+na]...)
  728. for i := range args {
  729. if (i < start || i >= na) && args[i].Name != "" {
  730. stmtArgs = append(stmtArgs, args[i])
  731. }
  732. }
  733. for i := range stmtArgs {
  734. stmtArgs[i].Ordinal = i + 1
  735. }
  736. res, err = s.(*SQLiteStmt).exec(ctx, stmtArgs)
  737. if err != nil && err != driver.ErrSkip {
  738. s.Close()
  739. return nil, err
  740. }
  741. start += na
  742. }
  743. tail := s.(*SQLiteStmt).t
  744. s.Close()
  745. if tail == "" {
  746. return res, nil
  747. }
  748. query = tail
  749. }
  750. }
  751. type namedValue struct {
  752. Name string
  753. Ordinal int
  754. Value driver.Value
  755. }
  756. // Query implements Queryer.
  757. func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
  758. list := make([]namedValue, len(args))
  759. for i, v := range args {
  760. list[i] = namedValue{
  761. Ordinal: i + 1,
  762. Value: v,
  763. }
  764. }
  765. return c.query(context.Background(), query, list)
  766. }
  767. func (c *SQLiteConn) query(ctx context.Context, query string, args []namedValue) (driver.Rows, error) {
  768. start := 0
  769. for {
  770. stmtArgs := make([]namedValue, 0, len(args))
  771. s, err := c.prepare(ctx, query)
  772. if err != nil {
  773. return nil, err
  774. }
  775. s.(*SQLiteStmt).cls = true
  776. na := s.NumInput()
  777. if len(args)-start < na {
  778. return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args)-start)
  779. }
  780. // consume the number of arguments used in the current
  781. // statement and append all named arguments not contained
  782. // therein
  783. stmtArgs = append(stmtArgs, args[start:start+na]...)
  784. for i := range args {
  785. if (i < start || i >= na) && args[i].Name != "" {
  786. stmtArgs = append(stmtArgs, args[i])
  787. }
  788. }
  789. for i := range stmtArgs {
  790. stmtArgs[i].Ordinal = i + 1
  791. }
  792. rows, err := s.(*SQLiteStmt).query(ctx, stmtArgs)
  793. if err != nil && err != driver.ErrSkip {
  794. s.Close()
  795. return rows, err
  796. }
  797. start += na
  798. tail := s.(*SQLiteStmt).t
  799. if tail == "" {
  800. return rows, nil
  801. }
  802. rows.Close()
  803. s.Close()
  804. query = tail
  805. }
  806. }
  807. // Begin transaction.
  808. func (c *SQLiteConn) Begin() (driver.Tx, error) {
  809. return c.begin(context.Background())
  810. }
  811. func (c *SQLiteConn) begin(ctx context.Context) (driver.Tx, error) {
  812. if _, err := c.exec(ctx, c.txlock, nil); err != nil {
  813. return nil, err
  814. }
  815. return &SQLiteTx{c}, nil
  816. }
  817. // Open database and return a new connection.
  818. //
  819. // A pragma can take either zero or one argument.
  820. // The argument is may be either in parentheses or it may be separated from
  821. // the pragma name by an equal sign. The two syntaxes yield identical results.
  822. // In many pragmas, the argument is a boolean. The boolean can be one of:
  823. // 1 yes true on
  824. // 0 no false off
  825. //
  826. // You can specify a DSN string using a URI as the filename.
  827. // test.db
  828. // file:test.db?cache=shared&mode=memory
  829. // :memory:
  830. // file::memory:
  831. //
  832. // mode
  833. // Access mode of the database.
  834. // https://www.sqlite.org/c3ref/open.html
  835. // Values:
  836. // - ro
  837. // - rw
  838. // - rwc
  839. // - memory
  840. //
  841. // cache
  842. // SQLite Shared-Cache Mode
  843. // https://www.sqlite.org/sharedcache.html
  844. // Values:
  845. // - shared
  846. // - private
  847. //
  848. // immutable=Boolean
  849. // The immutable parameter is a boolean query parameter that indicates
  850. // that the database file is stored on read-only media. When immutable is set,
  851. // SQLite assumes that the database file cannot be changed,
  852. // even by a process with higher privilege,
  853. // and so the database is opened read-only and all locking and change detection is disabled.
  854. // Caution: Setting the immutable property on a database file that
  855. // does in fact change can result in incorrect query results and/or SQLITE_CORRUPT errors.
  856. //
  857. // go-sqlite3 adds the following query parameters to those used by SQLite:
  858. // _loc=XXX
  859. // Specify location of time format. It's possible to specify "auto".
  860. //
  861. // _mutex=XXX
  862. // Specify mutex mode. XXX can be "no", "full".
  863. //
  864. // _txlock=XXX
  865. // Specify locking behavior for transactions. XXX can be "immediate",
  866. // "deferred", "exclusive".
  867. //
  868. // _auto_vacuum=X | _vacuum=X
  869. // 0 | none - Auto Vacuum disabled
  870. // 1 | full - Auto Vacuum FULL
  871. // 2 | incremental - Auto Vacuum Incremental
  872. //
  873. // _busy_timeout=XXX"| _timeout=XXX
  874. // Specify value for sqlite3_busy_timeout.
  875. //
  876. // _case_sensitive_like=Boolean | _cslike=Boolean
  877. // https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
  878. // Default or disabled the LIKE operation is case-insensitive.
  879. // When enabling this options behaviour of LIKE will become case-sensitive.
  880. //
  881. // _defer_foreign_keys=Boolean | _defer_fk=Boolean
  882. // Defer Foreign Keys until outermost transaction is committed.
  883. //
  884. // _foreign_keys=Boolean | _fk=Boolean
  885. // Enable or disable enforcement of foreign keys.
  886. //
  887. // _ignore_check_constraints=Boolean
  888. // This pragma enables or disables the enforcement of CHECK constraints.
  889. // The default setting is off, meaning that CHECK constraints are enforced by default.
  890. //
  891. // _journal_mode=MODE | _journal=MODE
  892. // Set journal mode for the databases associated with the current connection.
  893. // https://www.sqlite.org/pragma.html#pragma_journal_mode
  894. //
  895. // _locking_mode=X | _locking=X
  896. // Sets the database connection locking-mode.
  897. // The locking-mode is either NORMAL or EXCLUSIVE.
  898. // https://www.sqlite.org/pragma.html#pragma_locking_mode
  899. //
  900. // _query_only=Boolean
  901. // The query_only pragma prevents all changes to database files when enabled.
  902. //
  903. // _recursive_triggers=Boolean | _rt=Boolean
  904. // Enable or disable recursive triggers.
  905. //
  906. // _secure_delete=Boolean|FAST
  907. // When secure_delete is on, SQLite overwrites deleted content with zeros.
  908. // https://www.sqlite.org/pragma.html#pragma_secure_delete
  909. //
  910. // _synchronous=X | _sync=X
  911. // Change the setting of the "synchronous" flag.
  912. // https://www.sqlite.org/pragma.html#pragma_synchronous
  913. //
  914. // _writable_schema=Boolean
  915. // When this pragma is on, the SQLITE_MASTER tables in which database
  916. // can be changed using ordinary UPDATE, INSERT, and DELETE statements.
  917. // Warning: misuse of this pragma can easily result in a corrupt database file.
  918. //
  919. //
  920. func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
  921. if C.sqlite3_threadsafe() == 0 {
  922. return nil, errors.New("sqlite library was not compiled for thread-safe operation")
  923. }
  924. var pkey string
  925. // Options
  926. var loc *time.Location
  927. authCreate := false
  928. authUser := ""
  929. authPass := ""
  930. authCrypt := ""
  931. authSalt := ""
  932. mutex := C.int(C.SQLITE_OPEN_FULLMUTEX)
  933. txlock := "BEGIN"
  934. // PRAGMA's
  935. autoVacuum := -1
  936. busyTimeout := 5000
  937. caseSensitiveLike := -1
  938. deferForeignKeys := -1
  939. foreignKeys := -1
  940. ignoreCheckConstraints := -1
  941. var journalMode string
  942. lockingMode := "NORMAL"
  943. queryOnly := -1
  944. recursiveTriggers := -1
  945. secureDelete := "DEFAULT"
  946. synchronousMode := "NORMAL"
  947. writableSchema := -1
  948. vfsName := ""
  949. pos := strings.IndexRune(dsn, '?')
  950. if pos >= 1 {
  951. params, err := url.ParseQuery(dsn[pos+1:])
  952. if err != nil {
  953. return nil, err
  954. }
  955. // Authentication
  956. if _, ok := params["_auth"]; ok {
  957. authCreate = true
  958. }
  959. if val := params.Get("_auth_user"); val != "" {
  960. authUser = val
  961. }
  962. if val := params.Get("_auth_pass"); val != "" {
  963. authPass = val
  964. }
  965. if val := params.Get("_auth_crypt"); val != "" {
  966. authCrypt = val
  967. }
  968. if val := params.Get("_auth_salt"); val != "" {
  969. authSalt = val
  970. }
  971. // _loc
  972. if val := params.Get("_loc"); val != "" {
  973. switch strings.ToLower(val) {
  974. case "auto":
  975. loc = time.Local
  976. default:
  977. loc, err = time.LoadLocation(val)
  978. if err != nil {
  979. return nil, fmt.Errorf("Invalid _loc: %v: %v", val, err)
  980. }
  981. }
  982. }
  983. // _mutex
  984. if val := params.Get("_mutex"); val != "" {
  985. switch strings.ToLower(val) {
  986. case "no":
  987. mutex = C.SQLITE_OPEN_NOMUTEX
  988. case "full":
  989. mutex = C.SQLITE_OPEN_FULLMUTEX
  990. default:
  991. return nil, fmt.Errorf("Invalid _mutex: %v", val)
  992. }
  993. }
  994. // _txlock
  995. if val := params.Get("_txlock"); val != "" {
  996. switch strings.ToLower(val) {
  997. case "immediate":
  998. txlock = "BEGIN IMMEDIATE"
  999. case "exclusive":
  1000. txlock = "BEGIN EXCLUSIVE"
  1001. case "deferred":
  1002. txlock = "BEGIN"
  1003. default:
  1004. return nil, fmt.Errorf("Invalid _txlock: %v", val)
  1005. }
  1006. }
  1007. // Auto Vacuum (_vacuum)
  1008. //
  1009. // https://www.sqlite.org/pragma.html#pragma_auto_vacuum
  1010. //
  1011. pkey = "" // Reset pkey
  1012. if _, ok := params["_auto_vacuum"]; ok {
  1013. pkey = "_auto_vacuum"
  1014. }
  1015. if _, ok := params["_vacuum"]; ok {
  1016. pkey = "_vacuum"
  1017. }
  1018. if val := params.Get(pkey); val != "" {
  1019. switch strings.ToLower(val) {
  1020. case "0", "none":
  1021. autoVacuum = 0
  1022. case "1", "full":
  1023. autoVacuum = 1
  1024. case "2", "incremental":
  1025. autoVacuum = 2
  1026. default:
  1027. return nil, fmt.Errorf("Invalid _auto_vacuum: %v, expecting value of '0 NONE 1 FULL 2 INCREMENTAL'", val)
  1028. }
  1029. }
  1030. // Busy Timeout (_busy_timeout)
  1031. //
  1032. // https://www.sqlite.org/pragma.html#pragma_busy_timeout
  1033. //
  1034. pkey = "" // Reset pkey
  1035. if _, ok := params["_busy_timeout"]; ok {
  1036. pkey = "_busy_timeout"
  1037. }
  1038. if _, ok := params["_timeout"]; ok {
  1039. pkey = "_timeout"
  1040. }
  1041. if val := params.Get(pkey); val != "" {
  1042. iv, err := strconv.ParseInt(val, 10, 64)
  1043. if err != nil {
  1044. return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
  1045. }
  1046. busyTimeout = int(iv)
  1047. }
  1048. // Case Sensitive Like (_cslike)
  1049. //
  1050. // https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
  1051. //
  1052. pkey = "" // Reset pkey
  1053. if _, ok := params["_case_sensitive_like"]; ok {
  1054. pkey = "_case_sensitive_like"
  1055. }
  1056. if _, ok := params["_cslike"]; ok {
  1057. pkey = "_cslike"
  1058. }
  1059. if val := params.Get(pkey); val != "" {
  1060. switch strings.ToLower(val) {
  1061. case "0", "no", "false", "off":
  1062. caseSensitiveLike = 0
  1063. case "1", "yes", "true", "on":
  1064. caseSensitiveLike = 1
  1065. default:
  1066. return nil, fmt.Errorf("Invalid _case_sensitive_like: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1067. }
  1068. }
  1069. // Defer Foreign Keys (_defer_foreign_keys | _defer_fk)
  1070. //
  1071. // https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys
  1072. //
  1073. pkey = "" // Reset pkey
  1074. if _, ok := params["_defer_foreign_keys"]; ok {
  1075. pkey = "_defer_foreign_keys"
  1076. }
  1077. if _, ok := params["_defer_fk"]; ok {
  1078. pkey = "_defer_fk"
  1079. }
  1080. if val := params.Get(pkey); val != "" {
  1081. switch strings.ToLower(val) {
  1082. case "0", "no", "false", "off":
  1083. deferForeignKeys = 0
  1084. case "1", "yes", "true", "on":
  1085. deferForeignKeys = 1
  1086. default:
  1087. return nil, fmt.Errorf("Invalid _defer_foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1088. }
  1089. }
  1090. // Foreign Keys (_foreign_keys | _fk)
  1091. //
  1092. // https://www.sqlite.org/pragma.html#pragma_foreign_keys
  1093. //
  1094. pkey = "" // Reset pkey
  1095. if _, ok := params["_foreign_keys"]; ok {
  1096. pkey = "_foreign_keys"
  1097. }
  1098. if _, ok := params["_fk"]; ok {
  1099. pkey = "_fk"
  1100. }
  1101. if val := params.Get(pkey); val != "" {
  1102. switch strings.ToLower(val) {
  1103. case "0", "no", "false", "off":
  1104. foreignKeys = 0
  1105. case "1", "yes", "true", "on":
  1106. foreignKeys = 1
  1107. default:
  1108. return nil, fmt.Errorf("Invalid _foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1109. }
  1110. }
  1111. // Ignore CHECK Constrains (_ignore_check_constraints)
  1112. //
  1113. // https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints
  1114. //
  1115. if val := params.Get("_ignore_check_constraints"); val != "" {
  1116. switch strings.ToLower(val) {
  1117. case "0", "no", "false", "off":
  1118. ignoreCheckConstraints = 0
  1119. case "1", "yes", "true", "on":
  1120. ignoreCheckConstraints = 1
  1121. default:
  1122. return nil, fmt.Errorf("Invalid _ignore_check_constraints: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1123. }
  1124. }
  1125. // Journal Mode (_journal_mode | _journal)
  1126. //
  1127. // https://www.sqlite.org/pragma.html#pragma_journal_mode
  1128. //
  1129. pkey = "" // Reset pkey
  1130. if _, ok := params["_journal_mode"]; ok {
  1131. pkey = "_journal_mode"
  1132. }
  1133. if _, ok := params["_journal"]; ok {
  1134. pkey = "_journal"
  1135. }
  1136. if val := params.Get(pkey); val != "" {
  1137. switch strings.ToUpper(val) {
  1138. case "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "OFF":
  1139. journalMode = strings.ToUpper(val)
  1140. case "WAL":
  1141. journalMode = strings.ToUpper(val)
  1142. // For WAL Mode set Synchronous Mode to 'NORMAL'
  1143. // See https://www.sqlite.org/pragma.html#pragma_synchronous
  1144. synchronousMode = "NORMAL"
  1145. default:
  1146. return nil, fmt.Errorf("Invalid _journal: %v, expecting value of 'DELETE TRUNCATE PERSIST MEMORY WAL OFF'", val)
  1147. }
  1148. }
  1149. // Locking Mode (_locking)
  1150. //
  1151. // https://www.sqlite.org/pragma.html#pragma_locking_mode
  1152. //
  1153. pkey = "" // Reset pkey
  1154. if _, ok := params["_locking_mode"]; ok {
  1155. pkey = "_locking_mode"
  1156. }
  1157. if _, ok := params["_locking"]; ok {
  1158. pkey = "_locking"
  1159. }
  1160. if val := params.Get(pkey); val != "" {
  1161. switch strings.ToUpper(val) {
  1162. case "NORMAL", "EXCLUSIVE":
  1163. lockingMode = strings.ToUpper(val)
  1164. default:
  1165. return nil, fmt.Errorf("Invalid _locking_mode: %v, expecting value of 'NORMAL EXCLUSIVE", val)
  1166. }
  1167. }
  1168. // Query Only (_query_only)
  1169. //
  1170. // https://www.sqlite.org/pragma.html#pragma_query_only
  1171. //
  1172. if val := params.Get("_query_only"); val != "" {
  1173. switch strings.ToLower(val) {
  1174. case "0", "no", "false", "off":
  1175. queryOnly = 0
  1176. case "1", "yes", "true", "on":
  1177. queryOnly = 1
  1178. default:
  1179. return nil, fmt.Errorf("Invalid _query_only: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1180. }
  1181. }
  1182. // Recursive Triggers (_recursive_triggers)
  1183. //
  1184. // https://www.sqlite.org/pragma.html#pragma_recursive_triggers
  1185. //
  1186. pkey = "" // Reset pkey
  1187. if _, ok := params["_recursive_triggers"]; ok {
  1188. pkey = "_recursive_triggers"
  1189. }
  1190. if _, ok := params["_rt"]; ok {
  1191. pkey = "_rt"
  1192. }
  1193. if val := params.Get(pkey); val != "" {
  1194. switch strings.ToLower(val) {
  1195. case "0", "no", "false", "off":
  1196. recursiveTriggers = 0
  1197. case "1", "yes", "true", "on":
  1198. recursiveTriggers = 1
  1199. default:
  1200. return nil, fmt.Errorf("Invalid _recursive_triggers: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1201. }
  1202. }
  1203. // Secure Delete (_secure_delete)
  1204. //
  1205. // https://www.sqlite.org/pragma.html#pragma_secure_delete
  1206. //
  1207. if val := params.Get("_secure_delete"); val != "" {
  1208. switch strings.ToLower(val) {
  1209. case "0", "no", "false", "off":
  1210. secureDelete = "OFF"
  1211. case "1", "yes", "true", "on":
  1212. secureDelete = "ON"
  1213. case "fast":
  1214. secureDelete = "FAST"
  1215. default:
  1216. return nil, fmt.Errorf("Invalid _secure_delete: %v, expecting boolean value of '0 1 false true no yes off on fast'", val)
  1217. }
  1218. }
  1219. // Synchronous Mode (_synchronous | _sync)
  1220. //
  1221. // https://www.sqlite.org/pragma.html#pragma_synchronous
  1222. //
  1223. pkey = "" // Reset pkey
  1224. if _, ok := params["_synchronous"]; ok {
  1225. pkey = "_synchronous"
  1226. }
  1227. if _, ok := params["_sync"]; ok {
  1228. pkey = "_sync"
  1229. }
  1230. if val := params.Get(pkey); val != "" {
  1231. switch strings.ToUpper(val) {
  1232. case "0", "OFF", "1", "NORMAL", "2", "FULL", "3", "EXTRA":
  1233. synchronousMode = strings.ToUpper(val)
  1234. default:
  1235. return nil, fmt.Errorf("Invalid _synchronous: %v, expecting value of '0 OFF 1 NORMAL 2 FULL 3 EXTRA'", val)
  1236. }
  1237. }
  1238. // Writable Schema (_writeable_schema)
  1239. //
  1240. // https://www.sqlite.org/pragma.html#pragma_writeable_schema
  1241. //
  1242. if val := params.Get("_writable_schema"); val != "" {
  1243. switch strings.ToLower(val) {
  1244. case "0", "no", "false", "off":
  1245. writableSchema = 0
  1246. case "1", "yes", "true", "on":
  1247. writableSchema = 1
  1248. default:
  1249. return nil, fmt.Errorf("Invalid _writable_schema: %v, expecting boolean value of '0 1 false true no yes off on'", val)
  1250. }
  1251. }
  1252. if val := params.Get("vfs"); val != "" {
  1253. vfsName = val
  1254. }
  1255. if !strings.HasPrefix(dsn, "file:") {
  1256. dsn = dsn[:pos]
  1257. }
  1258. }
  1259. var db *C.sqlite3
  1260. name := C.CString(dsn)
  1261. defer C.free(unsafe.Pointer(name))
  1262. var vfs *C.char
  1263. if vfsName != "" {
  1264. vfs = C.CString(vfsName)
  1265. defer C.free(unsafe.Pointer(vfs))
  1266. }
  1267. rv := C._sqlite3_open_v2(name, &db,
  1268. mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
  1269. vfs)
  1270. if rv != 0 {
  1271. // Save off the error _before_ closing the database.
  1272. // This is safe even if db is nil.
  1273. err := lastError(db)
  1274. if db != nil {
  1275. C.sqlite3_close_v2(db)
  1276. }
  1277. return nil, err
  1278. }
  1279. if db == nil {
  1280. return nil, errors.New("sqlite succeeded without returning a database")
  1281. }
  1282. rv = C.sqlite3_busy_timeout(db, C.int(busyTimeout))
  1283. if rv != C.SQLITE_OK {
  1284. C.sqlite3_close_v2(db)
  1285. return nil, Error{Code: ErrNo(rv)}
  1286. }
  1287. exec := func(s string) error {
  1288. cs := C.CString(s)
  1289. rv := C.sqlite3_exec(db, cs, nil, nil, nil)
  1290. C.free(unsafe.Pointer(cs))
  1291. if rv != C.SQLITE_OK {
  1292. return lastError(db)
  1293. }
  1294. return nil
  1295. }
  1296. // USER AUTHENTICATION
  1297. //
  1298. // User Authentication is always performed even when
  1299. // sqlite_userauth is not compiled in, because without user authentication
  1300. // the authentication is a no-op.
  1301. //
  1302. // Workflow
  1303. // - Authenticate
  1304. // ON::SUCCESS => Continue
  1305. // ON::SQLITE_AUTH => Return error and exit Open(...)
  1306. //
  1307. // - Activate User Authentication
  1308. // Check if the user wants to activate User Authentication.
  1309. // If so then first create a temporary AuthConn to the database
  1310. // This is possible because we are already successfully authenticated.
  1311. //
  1312. // - Check if `sqlite_user`` table exists
  1313. // YES => Add the provided user from DSN as Admin User and
  1314. // activate user authentication.
  1315. // NO => Continue
  1316. //
  1317. // Create connection to SQLite
  1318. conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
  1319. // Password Cipher has to be registered before authentication
  1320. if len(authCrypt) > 0 {
  1321. switch strings.ToUpper(authCrypt) {
  1322. case "SHA1":
  1323. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA1, true); err != nil {
  1324. return nil, fmt.Errorf("CryptEncoderSHA1: %s", err)
  1325. }
  1326. case "SSHA1":
  1327. if len(authSalt) == 0 {
  1328. return nil, fmt.Errorf("_auth_crypt=ssha1, requires _auth_salt")
  1329. }
  1330. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA1(authSalt), true); err != nil {
  1331. return nil, fmt.Errorf("CryptEncoderSSHA1: %s", err)
  1332. }
  1333. case "SHA256":
  1334. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA256, true); err != nil {
  1335. return nil, fmt.Errorf("CryptEncoderSHA256: %s", err)
  1336. }
  1337. case "SSHA256":
  1338. if len(authSalt) == 0 {
  1339. return nil, fmt.Errorf("_auth_crypt=ssha256, requires _auth_salt")
  1340. }
  1341. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA256(authSalt), true); err != nil {
  1342. return nil, fmt.Errorf("CryptEncoderSSHA256: %s", err)
  1343. }
  1344. case "SHA384":
  1345. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA384, true); err != nil {
  1346. return nil, fmt.Errorf("CryptEncoderSHA384: %s", err)
  1347. }
  1348. case "SSHA384":
  1349. if len(authSalt) == 0 {
  1350. return nil, fmt.Errorf("_auth_crypt=ssha384, requires _auth_salt")
  1351. }
  1352. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA384(authSalt), true); err != nil {
  1353. return nil, fmt.Errorf("CryptEncoderSSHA384: %s", err)
  1354. }
  1355. case "SHA512":
  1356. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA512, true); err != nil {
  1357. return nil, fmt.Errorf("CryptEncoderSHA512: %s", err)
  1358. }
  1359. case "SSHA512":
  1360. if len(authSalt) == 0 {
  1361. return nil, fmt.Errorf("_auth_crypt=ssha512, requires _auth_salt")
  1362. }
  1363. if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA512(authSalt), true); err != nil {
  1364. return nil, fmt.Errorf("CryptEncoderSSHA512: %s", err)
  1365. }
  1366. }
  1367. }
  1368. // Preform Authentication
  1369. if err := conn.Authenticate(authUser, authPass); err != nil {
  1370. return nil, err
  1371. }
  1372. // Register: authenticate
  1373. // Authenticate will perform an authentication of the provided username
  1374. // and password against the database.
  1375. //
  1376. // If a database contains the SQLITE_USER table, then the
  1377. // call to Authenticate must be invoked with an
  1378. // appropriate username and password prior to enable read and write
  1379. //access to the database.
  1380. //
  1381. // Return SQLITE_OK on success or SQLITE_ERROR if the username/password
  1382. // combination is incorrect or unknown.
  1383. //
  1384. // If the SQLITE_USER table is not present in the database file, then
  1385. // this interface is a harmless no-op returnning SQLITE_OK.
  1386. if err := conn.RegisterFunc("authenticate", conn.authenticate, true); err != nil {
  1387. return nil, err
  1388. }
  1389. //
  1390. // Register: auth_user_add
  1391. // auth_user_add can be used (by an admin user only)
  1392. // to create a new user. When called on a no-authentication-required
  1393. // database, this routine converts the database into an authentication-
  1394. // required database, automatically makes the added user an
  1395. // administrator, and logs in the current connection as that user.
  1396. // The AuthUserAdd only works for the "main" database, not
  1397. // for any ATTACH-ed databases. Any call to AuthUserAdd by a
  1398. // non-admin user results in an error.
  1399. if err := conn.RegisterFunc("auth_user_add", conn.authUserAdd, true); err != nil {
  1400. return nil, err
  1401. }
  1402. //
  1403. // Register: auth_user_change
  1404. // auth_user_change can be used to change a users
  1405. // login credentials or admin privilege. Any user can change their own
  1406. // login credentials. Only an admin user can change another users login
  1407. // credentials or admin privilege setting. No user may change their own
  1408. // admin privilege setting.
  1409. if err := conn.RegisterFunc("auth_user_change", conn.authUserChange, true); err != nil {
  1410. return nil, err
  1411. }
  1412. //
  1413. // Register: auth_user_delete
  1414. // auth_user_delete can be used (by an admin user only)
  1415. // to delete a user. The currently logged-in user cannot be deleted,
  1416. // which guarantees that there is always an admin user and hence that
  1417. // the database cannot be converted into a no-authentication-required
  1418. // database.
  1419. if err := conn.RegisterFunc("auth_user_delete", conn.authUserDelete, true); err != nil {
  1420. return nil, err
  1421. }
  1422. // Register: auth_enabled
  1423. // auth_enabled can be used to check if user authentication is enabled
  1424. if err := conn.RegisterFunc("auth_enabled", conn.authEnabled, true); err != nil {
  1425. return nil, err
  1426. }
  1427. // Auto Vacuum
  1428. // Moved auto_vacuum command, the user preference for auto_vacuum needs to be implemented directly after
  1429. // the authentication and before the sqlite_user table gets created if the user
  1430. // decides to activate User Authentication because
  1431. // auto_vacuum needs to be set before any tables are created
  1432. // and activating user authentication creates the internal table `sqlite_user`.
  1433. if autoVacuum > -1 {
  1434. if err := exec(fmt.Sprintf("PRAGMA auto_vacuum = %d;", autoVacuum)); err != nil {
  1435. C.sqlite3_close_v2(db)
  1436. return nil, err
  1437. }
  1438. }
  1439. // Check if user wants to activate User Authentication
  1440. if authCreate {
  1441. // Before going any further, we need to check that the user
  1442. // has provided an username and password within the DSN.
  1443. // We are not allowed to continue.
  1444. if len(authUser) == 0 {
  1445. return nil, fmt.Errorf("Missing '_auth_user' while user authentication was requested with '_auth'")
  1446. }
  1447. if len(authPass) == 0 {
  1448. return nil, fmt.Errorf("Missing '_auth_pass' while user authentication was requested with '_auth'")
  1449. }
  1450. // Check if User Authentication is Enabled
  1451. authExists := conn.AuthEnabled()
  1452. if !authExists {
  1453. if err := conn.AuthUserAdd(authUser, authPass, true); err != nil {
  1454. return nil, err
  1455. }
  1456. }
  1457. }
  1458. // Case Sensitive LIKE
  1459. if caseSensitiveLike > -1 {
  1460. if err := exec(fmt.Sprintf("PRAGMA case_sensitive_like = %d;", caseSensitiveLike)); err != nil {
  1461. C.sqlite3_close_v2(db)
  1462. return nil, err
  1463. }
  1464. }
  1465. // Defer Foreign Keys
  1466. if deferForeignKeys > -1 {
  1467. if err := exec(fmt.Sprintf("PRAGMA defer_foreign_keys = %d;", deferForeignKeys)); err != nil {
  1468. C.sqlite3_close_v2(db)
  1469. return nil, err
  1470. }
  1471. }
  1472. // Forgein Keys
  1473. if foreignKeys > -1 {
  1474. if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil {
  1475. C.sqlite3_close_v2(db)
  1476. return nil, err
  1477. }
  1478. }
  1479. // Ignore CHECK Constraints
  1480. if ignoreCheckConstraints > -1 {
  1481. if err := exec(fmt.Sprintf("PRAGMA ignore_check_constraints = %d;", ignoreCheckConstraints)); err != nil {
  1482. C.sqlite3_close_v2(db)
  1483. return nil, err
  1484. }
  1485. }
  1486. // Journal Mode
  1487. if journalMode != "" {
  1488. if err := exec(fmt.Sprintf("PRAGMA journal_mode = %s;", journalMode)); err != nil {
  1489. C.sqlite3_close_v2(db)
  1490. return nil, err
  1491. }
  1492. }
  1493. // Locking Mode
  1494. // Because the default is NORMAL and this is not changed in this package
  1495. // by using the compile time SQLITE_DEFAULT_LOCKING_MODE this PRAGMA can always be executed
  1496. if err := exec(fmt.Sprintf("PRAGMA locking_mode = %s;", lockingMode)); err != nil {
  1497. C.sqlite3_close_v2(db)
  1498. return nil, err
  1499. }
  1500. // Query Only
  1501. if queryOnly > -1 {
  1502. if err := exec(fmt.Sprintf("PRAGMA query_only = %d;", queryOnly)); err != nil {
  1503. C.sqlite3_close_v2(db)
  1504. return nil, err
  1505. }
  1506. }
  1507. // Recursive Triggers
  1508. if recursiveTriggers > -1 {
  1509. if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {
  1510. C.sqlite3_close_v2(db)
  1511. return nil, err
  1512. }
  1513. }
  1514. // Secure Delete
  1515. //
  1516. // Because this package can set the compile time flag SQLITE_SECURE_DELETE with a build tag
  1517. // the default value for secureDelete var is 'DEFAULT' this way
  1518. // you can compile with secure_delete 'ON' and disable it for a specific database connection.
  1519. if secureDelete != "DEFAULT" {
  1520. if err := exec(fmt.Sprintf("PRAGMA secure_delete = %s;", secureDelete)); err != nil {
  1521. C.sqlite3_close_v2(db)
  1522. return nil, err
  1523. }
  1524. }
  1525. // Synchronous Mode
  1526. //
  1527. // Because default is NORMAL this statement is always executed
  1528. if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
  1529. C.sqlite3_close_v2(db)
  1530. return nil, err
  1531. }
  1532. // Writable Schema
  1533. if writableSchema > -1 {
  1534. if err := exec(fmt.Sprintf("PRAGMA writable_schema = %d;", writableSchema)); err != nil {
  1535. C.sqlite3_close_v2(db)
  1536. return nil, err
  1537. }
  1538. }
  1539. if len(d.Extensions) > 0 {
  1540. if err := conn.loadExtensions(d.Extensions); err != nil {
  1541. conn.Close()
  1542. return nil, err
  1543. }
  1544. }
  1545. if d.ConnectHook != nil {
  1546. if err := d.ConnectHook(conn); err != nil {
  1547. conn.Close()
  1548. return nil, err
  1549. }
  1550. }
  1551. runtime.SetFinalizer(conn, (*SQLiteConn).Close)
  1552. return conn, nil
  1553. }
  1554. // Close the connection.
  1555. func (c *SQLiteConn) Close() error {
  1556. rv := C.sqlite3_close_v2(c.db)
  1557. if rv != C.SQLITE_OK {
  1558. return c.lastError()
  1559. }
  1560. deleteHandles(c)
  1561. c.mu.Lock()
  1562. c.db = nil
  1563. c.mu.Unlock()
  1564. runtime.SetFinalizer(c, nil)
  1565. return nil
  1566. }
  1567. func (c *SQLiteConn) dbConnOpen() bool {
  1568. if c == nil {
  1569. return false
  1570. }
  1571. c.mu.Lock()
  1572. defer c.mu.Unlock()
  1573. return c.db != nil
  1574. }
  1575. // Prepare the query string. Return a new statement.
  1576. func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
  1577. return c.prepare(context.Background(), query)
  1578. }
  1579. func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
  1580. pquery := C.CString(query)
  1581. defer C.free(unsafe.Pointer(pquery))
  1582. var s *C.sqlite3_stmt
  1583. var tail *C.char
  1584. rv := C._sqlite3_prepare_v2_internal(c.db, pquery, C.int(-1), &s, &tail)
  1585. if rv != C.SQLITE_OK {
  1586. return nil, c.lastError()
  1587. }
  1588. var t string
  1589. if tail != nil && *tail != '\000' {
  1590. t = strings.TrimSpace(C.GoString(tail))
  1591. }
  1592. ss := &SQLiteStmt{c: c, s: s, t: t}
  1593. runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
  1594. return ss, nil
  1595. }
  1596. // Run-Time Limit Categories.
  1597. // See: http://www.sqlite.org/c3ref/c_limit_attached.html
  1598. const (
  1599. SQLITE_LIMIT_LENGTH = C.SQLITE_LIMIT_LENGTH
  1600. SQLITE_LIMIT_SQL_LENGTH = C.SQLITE_LIMIT_SQL_LENGTH
  1601. SQLITE_LIMIT_COLUMN = C.SQLITE_LIMIT_COLUMN
  1602. SQLITE_LIMIT_EXPR_DEPTH = C.SQLITE_LIMIT_EXPR_DEPTH
  1603. SQLITE_LIMIT_COMPOUND_SELECT = C.SQLITE_LIMIT_COMPOUND_SELECT
  1604. SQLITE_LIMIT_VDBE_OP = C.SQLITE_LIMIT_VDBE_OP
  1605. SQLITE_LIMIT_FUNCTION_ARG = C.SQLITE_LIMIT_FUNCTION_ARG
  1606. SQLITE_LIMIT_ATTACHED = C.SQLITE_LIMIT_ATTACHED
  1607. SQLITE_LIMIT_LIKE_PATTERN_LENGTH = C.SQLITE_LIMIT_LIKE_PATTERN_LENGTH
  1608. SQLITE_LIMIT_VARIABLE_NUMBER = C.SQLITE_LIMIT_VARIABLE_NUMBER
  1609. SQLITE_LIMIT_TRIGGER_DEPTH = C.SQLITE_LIMIT_TRIGGER_DEPTH
  1610. SQLITE_LIMIT_WORKER_THREADS = C.SQLITE_LIMIT_WORKER_THREADS
  1611. )
  1612. // GetFilename returns the absolute path to the file containing
  1613. // the requested schema. When passed an empty string, it will
  1614. // instead use the database's default schema: "main".
  1615. // See: sqlite3_db_filename, https://www.sqlite.org/c3ref/db_filename.html
  1616. func (c *SQLiteConn) GetFilename(schemaName string) string {
  1617. if schemaName == "" {
  1618. schemaName = "main"
  1619. }
  1620. return C.GoString(C.sqlite3_db_filename(c.db, C.CString(schemaName)))
  1621. }
  1622. // GetLimit returns the current value of a run-time limit.
  1623. // See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
  1624. func (c *SQLiteConn) GetLimit(id int) int {
  1625. return int(C._sqlite3_limit(c.db, C.int(id), C.int(-1)))
  1626. }
  1627. // SetLimit changes the value of a run-time limits.
  1628. // Then this method returns the prior value of the limit.
  1629. // See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
  1630. func (c *SQLiteConn) SetLimit(id int, newVal int) int {
  1631. return int(C._sqlite3_limit(c.db, C.int(id), C.int(newVal)))
  1632. }
  1633. // Close the statement.
  1634. func (s *SQLiteStmt) Close() error {
  1635. s.mu.Lock()
  1636. defer s.mu.Unlock()
  1637. if s.closed {
  1638. return nil
  1639. }
  1640. s.closed = true
  1641. if !s.c.dbConnOpen() {
  1642. return errors.New("sqlite statement with already closed database connection")
  1643. }
  1644. rv := C.sqlite3_finalize(s.s)
  1645. s.s = nil
  1646. if rv != C.SQLITE_OK {
  1647. return s.c.lastError()
  1648. }
  1649. runtime.SetFinalizer(s, nil)
  1650. return nil
  1651. }
  1652. // NumInput return a number of parameters.
  1653. func (s *SQLiteStmt) NumInput() int {
  1654. return int(C.sqlite3_bind_parameter_count(s.s))
  1655. }
  1656. var placeHolder = []byte{0}
  1657. func (s *SQLiteStmt) bind(args []namedValue) error {
  1658. rv := C.sqlite3_reset(s.s)
  1659. if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
  1660. return s.c.lastError()
  1661. }
  1662. bindIndices := make([][3]int, len(args))
  1663. prefixes := []string{":", "@", "$"}
  1664. for i, v := range args {
  1665. bindIndices[i][0] = args[i].Ordinal
  1666. if v.Name != "" {
  1667. for j := range prefixes {
  1668. cname := C.CString(prefixes[j] + v.Name)
  1669. bindIndices[i][j] = int(C.sqlite3_bind_parameter_index(s.s, cname))
  1670. C.free(unsafe.Pointer(cname))
  1671. }
  1672. args[i].Ordinal = bindIndices[i][0]
  1673. }
  1674. }
  1675. for i, arg := range args {
  1676. for j := range bindIndices[i] {
  1677. if bindIndices[i][j] == 0 {
  1678. continue
  1679. }
  1680. n := C.int(bindIndices[i][j])
  1681. switch v := arg.Value.(type) {
  1682. case nil:
  1683. rv = C.sqlite3_bind_null(s.s, n)
  1684. case string:
  1685. if len(v) == 0 {
  1686. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&placeHolder[0])), C.int(0))
  1687. } else {
  1688. b := []byte(v)
  1689. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
  1690. }
  1691. case int64:
  1692. rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
  1693. case bool:
  1694. if v {
  1695. rv = C.sqlite3_bind_int(s.s, n, 1)
  1696. } else {
  1697. rv = C.sqlite3_bind_int(s.s, n, 0)
  1698. }
  1699. case float64:
  1700. rv = C.sqlite3_bind_double(s.s, n, C.double(v))
  1701. case []byte:
  1702. if v == nil {
  1703. rv = C.sqlite3_bind_null(s.s, n)
  1704. } else {
  1705. ln := len(v)
  1706. if ln == 0 {
  1707. v = placeHolder
  1708. }
  1709. rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
  1710. }
  1711. case time.Time:
  1712. b := []byte(v.Format(SQLiteTimestampFormats[0]))
  1713. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
  1714. }
  1715. if rv != C.SQLITE_OK {
  1716. return s.c.lastError()
  1717. }
  1718. }
  1719. }
  1720. return nil
  1721. }
  1722. // Query the statement with arguments. Return records.
  1723. func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, 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.query(context.Background(), list)
  1732. }
  1733. func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows, error) {
  1734. if err := s.bind(args); err != nil {
  1735. return nil, err
  1736. }
  1737. rows := &SQLiteRows{
  1738. s: s,
  1739. nc: int(C.sqlite3_column_count(s.s)),
  1740. cols: nil,
  1741. decltype: nil,
  1742. cls: s.cls,
  1743. closed: false,
  1744. ctx: ctx,
  1745. }
  1746. return rows, nil
  1747. }
  1748. // LastInsertId return last inserted ID.
  1749. func (r *SQLiteResult) LastInsertId() (int64, error) {
  1750. return r.id, nil
  1751. }
  1752. // RowsAffected return how many rows affected.
  1753. func (r *SQLiteResult) RowsAffected() (int64, error) {
  1754. return r.changes, nil
  1755. }
  1756. // Exec execute the statement with arguments. Return result object.
  1757. func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
  1758. list := make([]namedValue, len(args))
  1759. for i, v := range args {
  1760. list[i] = namedValue{
  1761. Ordinal: i + 1,
  1762. Value: v,
  1763. }
  1764. }
  1765. return s.exec(context.Background(), list)
  1766. }
  1767. func isInterruptErr(err error) bool {
  1768. sqliteErr, ok := err.(Error)
  1769. if ok {
  1770. return sqliteErr.Code == ErrInterrupt
  1771. }
  1772. return false
  1773. }
  1774. // exec executes a query that doesn't return rows. Attempts to honor context timeout.
  1775. func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result, error) {
  1776. if ctx.Done() == nil {
  1777. return s.execSync(args)
  1778. }
  1779. type result struct {
  1780. r driver.Result
  1781. err error
  1782. }
  1783. resultCh := make(chan result)
  1784. go func() {
  1785. r, err := s.execSync(args)
  1786. resultCh <- result{r, err}
  1787. }()
  1788. var rv result
  1789. select {
  1790. case rv = <-resultCh:
  1791. case <-ctx.Done():
  1792. select {
  1793. case rv = <-resultCh: // no need to interrupt, operation completed in db
  1794. default:
  1795. // this is still racy and can be no-op if executed between sqlite3_* calls in execSync.
  1796. C.sqlite3_interrupt(s.c.db)
  1797. rv = <-resultCh // wait for goroutine completed
  1798. if isInterruptErr(rv.err) {
  1799. return nil, ctx.Err()
  1800. }
  1801. }
  1802. }
  1803. return rv.r, rv.err
  1804. }
  1805. func (s *SQLiteStmt) execSync(args []namedValue) (driver.Result, error) {
  1806. if err := s.bind(args); err != nil {
  1807. C.sqlite3_reset(s.s)
  1808. C.sqlite3_clear_bindings(s.s)
  1809. return nil, err
  1810. }
  1811. var rowid, changes C.longlong
  1812. rv := C._sqlite3_step_row_internal(s.s, &rowid, &changes)
  1813. if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
  1814. err := s.c.lastError()
  1815. C.sqlite3_reset(s.s)
  1816. C.sqlite3_clear_bindings(s.s)
  1817. return nil, err
  1818. }
  1819. return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
  1820. }
  1821. // Close the rows.
  1822. func (rc *SQLiteRows) Close() error {
  1823. rc.s.mu.Lock()
  1824. if rc.s.closed || rc.closed {
  1825. rc.s.mu.Unlock()
  1826. return nil
  1827. }
  1828. rc.closed = true
  1829. if rc.cls {
  1830. rc.s.mu.Unlock()
  1831. return rc.s.Close()
  1832. }
  1833. rv := C.sqlite3_reset(rc.s.s)
  1834. if rv != C.SQLITE_OK {
  1835. rc.s.mu.Unlock()
  1836. return rc.s.c.lastError()
  1837. }
  1838. rc.s.mu.Unlock()
  1839. return nil
  1840. }
  1841. // Columns return column names.
  1842. func (rc *SQLiteRows) Columns() []string {
  1843. rc.s.mu.Lock()
  1844. defer rc.s.mu.Unlock()
  1845. if rc.s.s != nil && rc.nc != len(rc.cols) {
  1846. rc.cols = make([]string, rc.nc)
  1847. for i := 0; i < rc.nc; i++ {
  1848. rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
  1849. }
  1850. }
  1851. return rc.cols
  1852. }
  1853. func (rc *SQLiteRows) declTypes() []string {
  1854. if rc.s.s != nil && rc.decltype == nil {
  1855. rc.decltype = make([]string, rc.nc)
  1856. for i := 0; i < rc.nc; i++ {
  1857. rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
  1858. }
  1859. }
  1860. return rc.decltype
  1861. }
  1862. // DeclTypes return column types.
  1863. func (rc *SQLiteRows) DeclTypes() []string {
  1864. rc.s.mu.Lock()
  1865. defer rc.s.mu.Unlock()
  1866. return rc.declTypes()
  1867. }
  1868. // Next move cursor to next. Attempts to honor context timeout from QueryContext call.
  1869. func (rc *SQLiteRows) Next(dest []driver.Value) error {
  1870. rc.s.mu.Lock()
  1871. defer rc.s.mu.Unlock()
  1872. if rc.s.closed {
  1873. return io.EOF
  1874. }
  1875. if rc.ctx.Done() == nil {
  1876. return rc.nextSyncLocked(dest)
  1877. }
  1878. resultCh := make(chan error)
  1879. go func() {
  1880. resultCh <- rc.nextSyncLocked(dest)
  1881. }()
  1882. select {
  1883. case err := <-resultCh:
  1884. return err
  1885. case <-rc.ctx.Done():
  1886. select {
  1887. case <-resultCh: // no need to interrupt
  1888. default:
  1889. // this is still racy and can be no-op if executed between sqlite3_* calls in nextSyncLocked.
  1890. C.sqlite3_interrupt(rc.s.c.db)
  1891. <-resultCh // ensure goroutine completed
  1892. }
  1893. return rc.ctx.Err()
  1894. }
  1895. }
  1896. // nextSyncLocked moves cursor to next; must be called with locked mutex.
  1897. func (rc *SQLiteRows) nextSyncLocked(dest []driver.Value) error {
  1898. rv := C._sqlite3_step_internal(rc.s.s)
  1899. if rv == C.SQLITE_DONE {
  1900. return io.EOF
  1901. }
  1902. if rv != C.SQLITE_ROW {
  1903. rv = C.sqlite3_reset(rc.s.s)
  1904. if rv != C.SQLITE_OK {
  1905. return rc.s.c.lastError()
  1906. }
  1907. return nil
  1908. }
  1909. rc.declTypes()
  1910. for i := range dest {
  1911. switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
  1912. case C.SQLITE_INTEGER:
  1913. val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
  1914. switch rc.decltype[i] {
  1915. case columnTimestamp, columnDatetime, columnDate:
  1916. var t time.Time
  1917. // Assume a millisecond unix timestamp if it's 13 digits -- too
  1918. // large to be a reasonable timestamp in seconds.
  1919. if val > 1e12 || val < -1e12 {
  1920. val *= int64(time.Millisecond) // convert ms to nsec
  1921. t = time.Unix(0, val)
  1922. } else {
  1923. t = time.Unix(val, 0)
  1924. }
  1925. t = t.UTC()
  1926. if rc.s.c.loc != nil {
  1927. t = t.In(rc.s.c.loc)
  1928. }
  1929. dest[i] = t
  1930. case "boolean":
  1931. dest[i] = val > 0
  1932. default:
  1933. dest[i] = val
  1934. }
  1935. case C.SQLITE_FLOAT:
  1936. dest[i] = float64(C.sqlite3_column_double(rc.s.s, C.int(i)))
  1937. case C.SQLITE_BLOB:
  1938. p := C.sqlite3_column_blob(rc.s.s, C.int(i))
  1939. if p == nil {
  1940. dest[i] = []byte{}
  1941. continue
  1942. }
  1943. n := C.sqlite3_column_bytes(rc.s.s, C.int(i))
  1944. dest[i] = C.GoBytes(p, n)
  1945. case C.SQLITE_NULL:
  1946. dest[i] = nil
  1947. case C.SQLITE_TEXT:
  1948. var err error
  1949. var timeVal time.Time
  1950. n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
  1951. s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n))
  1952. switch rc.decltype[i] {
  1953. case columnTimestamp, columnDatetime, columnDate:
  1954. var t time.Time
  1955. s = strings.TrimSuffix(s, "Z")
  1956. for _, format := range SQLiteTimestampFormats {
  1957. if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
  1958. t = timeVal
  1959. break
  1960. }
  1961. }
  1962. if err != nil {
  1963. // The column is a time value, so return the zero time on parse failure.
  1964. t = time.Time{}
  1965. }
  1966. if rc.s.c.loc != nil {
  1967. t = t.In(rc.s.c.loc)
  1968. }
  1969. dest[i] = t
  1970. default:
  1971. dest[i] = s
  1972. }
  1973. }
  1974. }
  1975. return nil
  1976. }