sqlite3.go 62 KB

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