sqlite3.go 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. package sqlite3
  6. /*
  7. #cgo CFLAGS: -std=gnu99
  8. #cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE
  9. #cgo CFLAGS: -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61
  10. #ifndef USE_LIBSQLITE3
  11. #include <sqlite3-binding.h>
  12. #else
  13. #include <sqlite3.h>
  14. #endif
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #ifdef __CYGWIN__
  18. # include <errno.h>
  19. #endif
  20. #ifndef SQLITE_OPEN_READWRITE
  21. # define SQLITE_OPEN_READWRITE 0
  22. #endif
  23. #ifndef SQLITE_OPEN_FULLMUTEX
  24. # define SQLITE_OPEN_FULLMUTEX 0
  25. #endif
  26. #ifndef SQLITE_DETERMINISTIC
  27. # define SQLITE_DETERMINISTIC 0
  28. #endif
  29. static int
  30. _sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
  31. #ifdef SQLITE_OPEN_URI
  32. return sqlite3_open_v2(filename, ppDb, flags | SQLITE_OPEN_URI, zVfs);
  33. #else
  34. return sqlite3_open_v2(filename, ppDb, flags, zVfs);
  35. #endif
  36. }
  37. static int
  38. _sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
  39. return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
  40. }
  41. static int
  42. _sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
  43. return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
  44. }
  45. #include <stdio.h>
  46. #include <stdint.h>
  47. static int
  48. _sqlite3_exec(sqlite3* db, const char* pcmd, long long* rowid, long long* changes)
  49. {
  50. int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
  51. *rowid = (long long) sqlite3_last_insert_rowid(db);
  52. *changes = (long long) sqlite3_changes(db);
  53. return rv;
  54. }
  55. static int
  56. _sqlite3_step(sqlite3_stmt* stmt, long long* rowid, long long* changes)
  57. {
  58. int rv = sqlite3_step(stmt);
  59. sqlite3* db = sqlite3_db_handle(stmt);
  60. *rowid = (long long) sqlite3_last_insert_rowid(db);
  61. *changes = (long long) sqlite3_changes(db);
  62. return rv;
  63. }
  64. void _sqlite3_result_text(sqlite3_context* ctx, const char* s) {
  65. sqlite3_result_text(ctx, s, -1, &free);
  66. }
  67. void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l) {
  68. sqlite3_result_blob(ctx, b, l, SQLITE_TRANSIENT);
  69. }
  70. int _sqlite3_create_function(
  71. sqlite3 *db,
  72. const char *zFunctionName,
  73. int nArg,
  74. int eTextRep,
  75. uintptr_t pApp,
  76. void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
  77. void (*xStep)(sqlite3_context*,int,sqlite3_value**),
  78. void (*xFinal)(sqlite3_context*)
  79. ) {
  80. return sqlite3_create_function(db, zFunctionName, nArg, eTextRep, (void*) pApp, xFunc, xStep, xFinal);
  81. }
  82. void callbackTrampoline(sqlite3_context*, int, sqlite3_value**);
  83. void stepTrampoline(sqlite3_context*, int, sqlite3_value**);
  84. void doneTrampoline(sqlite3_context*);
  85. */
  86. import "C"
  87. import (
  88. "database/sql"
  89. "database/sql/driver"
  90. "errors"
  91. "fmt"
  92. "io"
  93. "net/url"
  94. "reflect"
  95. "runtime"
  96. "strconv"
  97. "strings"
  98. "time"
  99. "unsafe"
  100. )
  101. // Timestamp formats understood by both this module and SQLite.
  102. // The first format in the slice will be used when saving time values
  103. // into the database. When parsing a string from a timestamp or
  104. // datetime column, the formats are tried in order.
  105. var SQLiteTimestampFormats = []string{
  106. // By default, store timestamps with whatever timezone they come with.
  107. // When parsed, they will be returned with the same timezone.
  108. "2006-01-02 15:04:05.999999999-07:00",
  109. "2006-01-02T15:04:05.999999999-07:00",
  110. "2006-01-02 15:04:05.999999999",
  111. "2006-01-02T15:04:05.999999999",
  112. "2006-01-02 15:04:05",
  113. "2006-01-02T15:04:05",
  114. "2006-01-02 15:04",
  115. "2006-01-02T15:04",
  116. "2006-01-02",
  117. }
  118. func init() {
  119. sql.Register("sqlite3", &SQLiteDriver{})
  120. }
  121. // Version returns SQLite library version information.
  122. func Version() (libVersion string, libVersionNumber int, sourceId string) {
  123. libVersion = C.GoString(C.sqlite3_libversion())
  124. libVersionNumber = int(C.sqlite3_libversion_number())
  125. sourceId = C.GoString(C.sqlite3_sourceid())
  126. return libVersion, libVersionNumber, sourceId
  127. }
  128. // Driver struct.
  129. type SQLiteDriver struct {
  130. Extensions []string
  131. ConnectHook func(*SQLiteConn) error
  132. }
  133. // Conn struct.
  134. type SQLiteConn struct {
  135. db *C.sqlite3
  136. loc *time.Location
  137. txlock string
  138. funcs []*functionInfo
  139. aggregators []*aggInfo
  140. }
  141. // Tx struct.
  142. type SQLiteTx struct {
  143. c *SQLiteConn
  144. }
  145. // Stmt struct.
  146. type SQLiteStmt struct {
  147. c *SQLiteConn
  148. s *C.sqlite3_stmt
  149. nv int
  150. nn []string
  151. t string
  152. closed bool
  153. cls bool
  154. }
  155. // Result struct.
  156. type SQLiteResult struct {
  157. id int64
  158. changes int64
  159. }
  160. // Rows struct.
  161. type SQLiteRows struct {
  162. s *SQLiteStmt
  163. nc int
  164. cols []string
  165. decltype []string
  166. cls bool
  167. }
  168. type functionInfo struct {
  169. f reflect.Value
  170. argConverters []callbackArgConverter
  171. variadicConverter callbackArgConverter
  172. retConverter callbackRetConverter
  173. }
  174. func (fi *functionInfo) Call(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
  175. args, err := callbackConvertArgs(argv, fi.argConverters, fi.variadicConverter)
  176. if err != nil {
  177. callbackError(ctx, err)
  178. return
  179. }
  180. ret := fi.f.Call(args)
  181. if len(ret) == 2 && ret[1].Interface() != nil {
  182. callbackError(ctx, ret[1].Interface().(error))
  183. return
  184. }
  185. err = fi.retConverter(ctx, ret[0])
  186. if err != nil {
  187. callbackError(ctx, err)
  188. return
  189. }
  190. }
  191. type aggInfo struct {
  192. constructor reflect.Value
  193. // Active aggregator objects for aggregations in flight. The
  194. // aggregators are indexed by a counter stored in the aggregation
  195. // user data space provided by sqlite.
  196. active map[int64]reflect.Value
  197. next int64
  198. stepArgConverters []callbackArgConverter
  199. stepVariadicConverter callbackArgConverter
  200. doneRetConverter callbackRetConverter
  201. }
  202. func (ai *aggInfo) agg(ctx *C.sqlite3_context) (int64, reflect.Value, error) {
  203. aggIdx := (*int64)(C.sqlite3_aggregate_context(ctx, C.int(8)))
  204. if *aggIdx == 0 {
  205. *aggIdx = ai.next
  206. ret := ai.constructor.Call(nil)
  207. if len(ret) == 2 && ret[1].Interface() != nil {
  208. return 0, reflect.Value{}, ret[1].Interface().(error)
  209. }
  210. if ret[0].IsNil() {
  211. return 0, reflect.Value{}, errors.New("aggregator constructor returned nil state")
  212. }
  213. ai.next++
  214. ai.active[*aggIdx] = ret[0]
  215. }
  216. return *aggIdx, ai.active[*aggIdx], nil
  217. }
  218. func (ai *aggInfo) Step(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
  219. _, agg, err := ai.agg(ctx)
  220. if err != nil {
  221. callbackError(ctx, err)
  222. return
  223. }
  224. args, err := callbackConvertArgs(argv, ai.stepArgConverters, ai.stepVariadicConverter)
  225. if err != nil {
  226. callbackError(ctx, err)
  227. return
  228. }
  229. ret := agg.MethodByName("Step").Call(args)
  230. if len(ret) == 1 && ret[0].Interface() != nil {
  231. callbackError(ctx, ret[0].Interface().(error))
  232. return
  233. }
  234. }
  235. func (ai *aggInfo) Done(ctx *C.sqlite3_context) {
  236. idx, agg, err := ai.agg(ctx)
  237. if err != nil {
  238. callbackError(ctx, err)
  239. return
  240. }
  241. defer func() { delete(ai.active, idx) }()
  242. ret := agg.MethodByName("Done").Call(nil)
  243. if len(ret) == 2 && ret[1].Interface() != nil {
  244. callbackError(ctx, ret[1].Interface().(error))
  245. return
  246. }
  247. err = ai.doneRetConverter(ctx, ret[0])
  248. if err != nil {
  249. callbackError(ctx, err)
  250. return
  251. }
  252. }
  253. // Commit transaction.
  254. func (tx *SQLiteTx) Commit() error {
  255. _, err := tx.c.exec("COMMIT")
  256. return err
  257. }
  258. // Rollback transaction.
  259. func (tx *SQLiteTx) Rollback() error {
  260. _, err := tx.c.exec("ROLLBACK")
  261. return err
  262. }
  263. // RegisterFunc makes a Go function available as a SQLite function.
  264. //
  265. // The Go function can have arguments of the following types: any
  266. // numeric type except complex, bool, []byte, string and
  267. // interface{}. interface{} arguments are given the direct translation
  268. // of the SQLite data type: int64 for INTEGER, float64 for FLOAT,
  269. // []byte for BLOB, string for TEXT.
  270. //
  271. // The function can additionally be variadic, as long as the type of
  272. // the variadic argument is one of the above.
  273. //
  274. // If pure is true. SQLite will assume that the function's return
  275. // value depends only on its inputs, and make more aggressive
  276. // optimizations in its queries.
  277. //
  278. // See _example/go_custom_funcs for a detailed example.
  279. func (c *SQLiteConn) RegisterFunc(name string, impl interface{}, pure bool) error {
  280. var fi functionInfo
  281. fi.f = reflect.ValueOf(impl)
  282. t := fi.f.Type()
  283. if t.Kind() != reflect.Func {
  284. return errors.New("Non-function passed to RegisterFunc")
  285. }
  286. if t.NumOut() != 1 && t.NumOut() != 2 {
  287. return errors.New("SQLite functions must return 1 or 2 values")
  288. }
  289. if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
  290. return errors.New("Second return value of SQLite function must be error")
  291. }
  292. numArgs := t.NumIn()
  293. if t.IsVariadic() {
  294. numArgs--
  295. }
  296. for i := 0; i < numArgs; i++ {
  297. conv, err := callbackArg(t.In(i))
  298. if err != nil {
  299. return err
  300. }
  301. fi.argConverters = append(fi.argConverters, conv)
  302. }
  303. if t.IsVariadic() {
  304. conv, err := callbackArg(t.In(numArgs).Elem())
  305. if err != nil {
  306. return err
  307. }
  308. fi.variadicConverter = conv
  309. // Pass -1 to sqlite so that it allows any number of
  310. // arguments. The call helper verifies that the minimum number
  311. // of arguments is present for variadic functions.
  312. numArgs = -1
  313. }
  314. conv, err := callbackRet(t.Out(0))
  315. if err != nil {
  316. return err
  317. }
  318. fi.retConverter = conv
  319. // fi must outlast the database connection, or we'll have dangling pointers.
  320. c.funcs = append(c.funcs, &fi)
  321. cname := C.CString(name)
  322. defer C.free(unsafe.Pointer(cname))
  323. opts := C.SQLITE_UTF8
  324. if pure {
  325. opts |= C.SQLITE_DETERMINISTIC
  326. }
  327. rv := C._sqlite3_create_function(c.db, cname, C.int(numArgs), C.int(opts), C.uintptr_t(newHandle(c, &fi)), (*[0]byte)(unsafe.Pointer(C.callbackTrampoline)), nil, nil)
  328. if rv != C.SQLITE_OK {
  329. return c.lastError()
  330. }
  331. return nil
  332. }
  333. // RegisterAggregator makes a Go type available as a SQLite aggregation function.
  334. //
  335. // Because aggregation is incremental, it's implemented in Go with a
  336. // type that has 2 methods: func Step(values) accumulates one row of
  337. // data into the accumulator, and func Done() ret finalizes and
  338. // returns the aggregate value. "values" and "ret" may be any type
  339. // supported by RegisterFunc.
  340. //
  341. // RegisterAggregator takes as implementation a constructor function
  342. // that constructs an instance of the aggregator type each time an
  343. // aggregation begins. The constructor must return a pointer to a
  344. // type, or an interface that implements Step() and Done().
  345. //
  346. // The constructor function and the Step/Done methods may optionally
  347. // return an error in addition to their other return values.
  348. //
  349. // See _example/go_custom_funcs for a detailed example.
  350. func (c *SQLiteConn) RegisterAggregator(name string, impl interface{}, pure bool) error {
  351. var ai aggInfo
  352. ai.constructor = reflect.ValueOf(impl)
  353. t := ai.constructor.Type()
  354. if t.Kind() != reflect.Func {
  355. return errors.New("non-function passed to RegisterAggregator")
  356. }
  357. if t.NumOut() != 1 && t.NumOut() != 2 {
  358. return errors.New("SQLite aggregator constructors must return 1 or 2 values")
  359. }
  360. if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
  361. return errors.New("Second return value of SQLite function must be error")
  362. }
  363. if t.NumIn() != 0 {
  364. return errors.New("SQLite aggregator constructors must not have arguments")
  365. }
  366. agg := t.Out(0)
  367. switch agg.Kind() {
  368. case reflect.Ptr, reflect.Interface:
  369. default:
  370. return errors.New("SQlite aggregator constructor must return a pointer object")
  371. }
  372. stepFn, found := agg.MethodByName("Step")
  373. if !found {
  374. return errors.New("SQlite aggregator doesn't have a Step() function")
  375. }
  376. step := stepFn.Type
  377. if step.NumOut() != 0 && step.NumOut() != 1 {
  378. return errors.New("SQlite aggregator Step() function must return 0 or 1 values")
  379. }
  380. if step.NumOut() == 1 && !step.Out(0).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
  381. return errors.New("type of SQlite aggregator Step() return value must be error")
  382. }
  383. stepNArgs := step.NumIn()
  384. start := 0
  385. if agg.Kind() == reflect.Ptr {
  386. // Skip over the method receiver
  387. stepNArgs--
  388. start++
  389. }
  390. if step.IsVariadic() {
  391. stepNArgs--
  392. }
  393. for i := start; i < start+stepNArgs; i++ {
  394. conv, err := callbackArg(step.In(i))
  395. if err != nil {
  396. return err
  397. }
  398. ai.stepArgConverters = append(ai.stepArgConverters, conv)
  399. }
  400. if step.IsVariadic() {
  401. conv, err := callbackArg(t.In(start + stepNArgs).Elem())
  402. if err != nil {
  403. return err
  404. }
  405. ai.stepVariadicConverter = conv
  406. // Pass -1 to sqlite so that it allows any number of
  407. // arguments. The call helper verifies that the minimum number
  408. // of arguments is present for variadic functions.
  409. stepNArgs = -1
  410. }
  411. doneFn, found := agg.MethodByName("Done")
  412. if !found {
  413. return errors.New("SQlite aggregator doesn't have a Done() function")
  414. }
  415. done := doneFn.Type
  416. doneNArgs := done.NumIn()
  417. if agg.Kind() == reflect.Ptr {
  418. // Skip over the method receiver
  419. doneNArgs--
  420. }
  421. if doneNArgs != 0 {
  422. return errors.New("SQlite aggregator Done() function must have no arguments")
  423. }
  424. if done.NumOut() != 1 && done.NumOut() != 2 {
  425. return errors.New("SQLite aggregator Done() function must return 1 or 2 values")
  426. }
  427. if done.NumOut() == 2 && !done.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
  428. return errors.New("second return value of SQLite aggregator Done() function must be error")
  429. }
  430. conv, err := callbackRet(done.Out(0))
  431. if err != nil {
  432. return err
  433. }
  434. ai.doneRetConverter = conv
  435. ai.active = make(map[int64]reflect.Value)
  436. ai.next = 1
  437. // ai must outlast the database connection, or we'll have dangling pointers.
  438. c.aggregators = append(c.aggregators, &ai)
  439. cname := C.CString(name)
  440. defer C.free(unsafe.Pointer(cname))
  441. opts := C.SQLITE_UTF8
  442. if pure {
  443. opts |= C.SQLITE_DETERMINISTIC
  444. }
  445. rv := C._sqlite3_create_function(c.db, cname, C.int(stepNArgs), C.int(opts), C.uintptr_t(newHandle(c, &ai)), nil, (*[0]byte)(unsafe.Pointer(C.stepTrampoline)), (*[0]byte)(unsafe.Pointer(C.doneTrampoline)))
  446. if rv != C.SQLITE_OK {
  447. return c.lastError()
  448. }
  449. return nil
  450. }
  451. // AutoCommit return which currently auto commit or not.
  452. func (c *SQLiteConn) AutoCommit() bool {
  453. return int(C.sqlite3_get_autocommit(c.db)) != 0
  454. }
  455. func (c *SQLiteConn) lastError() Error {
  456. return Error{
  457. Code: ErrNo(C.sqlite3_errcode(c.db)),
  458. ExtendedCode: ErrNoExtended(C.sqlite3_extended_errcode(c.db)),
  459. err: C.GoString(C.sqlite3_errmsg(c.db)),
  460. }
  461. }
  462. // Implements Execer
  463. func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
  464. if len(args) == 0 {
  465. return c.exec(query)
  466. }
  467. for {
  468. s, err := c.Prepare(query)
  469. if err != nil {
  470. return nil, err
  471. }
  472. var res driver.Result
  473. if s.(*SQLiteStmt).s != nil {
  474. na := s.NumInput()
  475. if len(args) < na {
  476. return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args))
  477. }
  478. res, err = s.Exec(args[:na])
  479. if err != nil && err != driver.ErrSkip {
  480. s.Close()
  481. return nil, err
  482. }
  483. args = args[na:]
  484. }
  485. tail := s.(*SQLiteStmt).t
  486. s.Close()
  487. if tail == "" {
  488. return res, nil
  489. }
  490. query = tail
  491. }
  492. }
  493. // Implements Queryer
  494. func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
  495. for {
  496. s, err := c.Prepare(query)
  497. if err != nil {
  498. return nil, err
  499. }
  500. s.(*SQLiteStmt).cls = true
  501. na := s.NumInput()
  502. if len(args) < na {
  503. return nil, fmt.Errorf("Not enough args to execute query. Expected %d, got %d.", na, len(args))
  504. }
  505. rows, err := s.Query(args[:na])
  506. if err != nil && err != driver.ErrSkip {
  507. s.Close()
  508. return nil, err
  509. }
  510. args = args[na:]
  511. tail := s.(*SQLiteStmt).t
  512. if tail == "" {
  513. return rows, nil
  514. }
  515. rows.Close()
  516. s.Close()
  517. query = tail
  518. }
  519. }
  520. func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {
  521. pcmd := C.CString(cmd)
  522. defer C.free(unsafe.Pointer(pcmd))
  523. var rowid, changes C.longlong
  524. rv := C._sqlite3_exec(c.db, pcmd, &rowid, &changes)
  525. if rv != C.SQLITE_OK {
  526. return nil, c.lastError()
  527. }
  528. return &SQLiteResult{int64(rowid), int64(changes)}, nil
  529. }
  530. // Begin transaction.
  531. func (c *SQLiteConn) Begin() (driver.Tx, error) {
  532. if _, err := c.exec(c.txlock); err != nil {
  533. return nil, err
  534. }
  535. return &SQLiteTx{c}, nil
  536. }
  537. func errorString(err Error) string {
  538. return C.GoString(C.sqlite3_errstr(C.int(err.Code)))
  539. }
  540. // Open database and return a new connection.
  541. // You can specify a DSN string using a URI as the filename.
  542. // test.db
  543. // file:test.db?cache=shared&mode=memory
  544. // :memory:
  545. // file::memory:
  546. // go-sqlite3 adds the following query parameters to those used by SQLite:
  547. // _loc=XXX
  548. // Specify location of time format. It's possible to specify "auto".
  549. // _busy_timeout=XXX
  550. // Specify value for sqlite3_busy_timeout.
  551. // _txlock=XXX
  552. // Specify locking behavior for transactions. XXX can be "immediate",
  553. // "deferred", "exclusive".
  554. func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
  555. if C.sqlite3_threadsafe() == 0 {
  556. return nil, errors.New("sqlite library was not compiled for thread-safe operation")
  557. }
  558. var loc *time.Location
  559. txlock := "BEGIN"
  560. busy_timeout := 5000
  561. pos := strings.IndexRune(dsn, '?')
  562. if pos >= 1 {
  563. params, err := url.ParseQuery(dsn[pos+1:])
  564. if err != nil {
  565. return nil, err
  566. }
  567. // _loc
  568. if val := params.Get("_loc"); val != "" {
  569. if val == "auto" {
  570. loc = time.Local
  571. } else {
  572. loc, err = time.LoadLocation(val)
  573. if err != nil {
  574. return nil, fmt.Errorf("Invalid _loc: %v: %v", val, err)
  575. }
  576. }
  577. }
  578. // _busy_timeout
  579. if val := params.Get("_busy_timeout"); val != "" {
  580. iv, err := strconv.ParseInt(val, 10, 64)
  581. if err != nil {
  582. return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
  583. }
  584. busy_timeout = int(iv)
  585. }
  586. // _txlock
  587. if val := params.Get("_txlock"); val != "" {
  588. switch val {
  589. case "immediate":
  590. txlock = "BEGIN IMMEDIATE"
  591. case "exclusive":
  592. txlock = "BEGIN EXCLUSIVE"
  593. case "deferred":
  594. txlock = "BEGIN"
  595. default:
  596. return nil, fmt.Errorf("Invalid _txlock: %v", val)
  597. }
  598. }
  599. if !strings.HasPrefix(dsn, "file:") {
  600. dsn = dsn[:pos]
  601. }
  602. }
  603. var db *C.sqlite3
  604. name := C.CString(dsn)
  605. defer C.free(unsafe.Pointer(name))
  606. rv := C._sqlite3_open_v2(name, &db,
  607. C.SQLITE_OPEN_FULLMUTEX|
  608. C.SQLITE_OPEN_READWRITE|
  609. C.SQLITE_OPEN_CREATE,
  610. nil)
  611. if rv != 0 {
  612. return nil, Error{Code: ErrNo(rv)}
  613. }
  614. if db == nil {
  615. return nil, errors.New("sqlite succeeded without returning a database")
  616. }
  617. rv = C.sqlite3_busy_timeout(db, C.int(busy_timeout))
  618. if rv != C.SQLITE_OK {
  619. return nil, Error{Code: ErrNo(rv)}
  620. }
  621. conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
  622. if len(d.Extensions) > 0 {
  623. if err := conn.loadExtensions(d.Extensions); err != nil {
  624. return nil, err
  625. }
  626. }
  627. if d.ConnectHook != nil {
  628. if err := d.ConnectHook(conn); err != nil {
  629. return nil, err
  630. }
  631. }
  632. runtime.SetFinalizer(conn, (*SQLiteConn).Close)
  633. return conn, nil
  634. }
  635. // Close the connection.
  636. func (c *SQLiteConn) Close() error {
  637. deleteHandles(c)
  638. rv := C.sqlite3_close_v2(c.db)
  639. if rv != C.SQLITE_OK {
  640. return c.lastError()
  641. }
  642. c.db = nil
  643. runtime.SetFinalizer(c, nil)
  644. return nil
  645. }
  646. // Prepare the query string. Return a new statement.
  647. func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
  648. pquery := C.CString(query)
  649. defer C.free(unsafe.Pointer(pquery))
  650. var s *C.sqlite3_stmt
  651. var tail *C.char
  652. rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
  653. if rv != C.SQLITE_OK {
  654. return nil, c.lastError()
  655. }
  656. var t string
  657. if tail != nil && *tail != '\000' {
  658. t = strings.TrimSpace(C.GoString(tail))
  659. }
  660. nv := int(C.sqlite3_bind_parameter_count(s))
  661. var nn []string
  662. for i := 0; i < nv; i++ {
  663. pn := C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1)))
  664. if len(pn) > 1 && pn[0] == '$' && 48 <= pn[1] && pn[1] <= 57 {
  665. nn = append(nn, C.GoString(C.sqlite3_bind_parameter_name(s, C.int(i+1))))
  666. }
  667. }
  668. ss := &SQLiteStmt{c: c, s: s, nv: nv, nn: nn, t: t}
  669. runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
  670. return ss, nil
  671. }
  672. // Close the statement.
  673. func (s *SQLiteStmt) Close() error {
  674. if s.closed {
  675. return nil
  676. }
  677. s.closed = true
  678. if s.c == nil || s.c.db == nil {
  679. return errors.New("sqlite statement with already closed database connection")
  680. }
  681. rv := C.sqlite3_finalize(s.s)
  682. if rv != C.SQLITE_OK {
  683. return s.c.lastError()
  684. }
  685. runtime.SetFinalizer(s, nil)
  686. return nil
  687. }
  688. // Return a number of parameters.
  689. func (s *SQLiteStmt) NumInput() int {
  690. return s.nv
  691. }
  692. type bindArg struct {
  693. n int
  694. v driver.Value
  695. }
  696. func (s *SQLiteStmt) bind(args []driver.Value) error {
  697. rv := C.sqlite3_reset(s.s)
  698. if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
  699. return s.c.lastError()
  700. }
  701. var vargs []bindArg
  702. narg := len(args)
  703. vargs = make([]bindArg, narg)
  704. if len(s.nn) > 0 {
  705. for i, v := range s.nn {
  706. if pi, err := strconv.Atoi(v[1:]); err == nil {
  707. vargs[i] = bindArg{pi, args[i]}
  708. }
  709. }
  710. } else {
  711. for i, v := range args {
  712. vargs[i] = bindArg{i + 1, v}
  713. }
  714. }
  715. for _, varg := range vargs {
  716. n := C.int(varg.n)
  717. v := varg.v
  718. switch v := v.(type) {
  719. case nil:
  720. rv = C.sqlite3_bind_null(s.s, n)
  721. case string:
  722. if len(v) == 0 {
  723. b := []byte{0}
  724. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(0))
  725. } else {
  726. b := []byte(v)
  727. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
  728. }
  729. case int64:
  730. rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
  731. case bool:
  732. if bool(v) {
  733. rv = C.sqlite3_bind_int(s.s, n, 1)
  734. } else {
  735. rv = C.sqlite3_bind_int(s.s, n, 0)
  736. }
  737. case float64:
  738. rv = C.sqlite3_bind_double(s.s, n, C.double(v))
  739. case []byte:
  740. if len(v) == 0 {
  741. rv = C._sqlite3_bind_blob(s.s, n, nil, 0)
  742. } else {
  743. rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(len(v)))
  744. }
  745. case time.Time:
  746. b := []byte(v.Format(SQLiteTimestampFormats[0]))
  747. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
  748. }
  749. if rv != C.SQLITE_OK {
  750. return s.c.lastError()
  751. }
  752. }
  753. return nil
  754. }
  755. // Query the statement with arguments. Return records.
  756. func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
  757. if err := s.bind(args); err != nil {
  758. return nil, err
  759. }
  760. return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil
  761. }
  762. // Return last inserted ID.
  763. func (r *SQLiteResult) LastInsertId() (int64, error) {
  764. return r.id, nil
  765. }
  766. // Return how many rows affected.
  767. func (r *SQLiteResult) RowsAffected() (int64, error) {
  768. return r.changes, nil
  769. }
  770. // Execute the statement with arguments. Return result object.
  771. func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
  772. if err := s.bind(args); err != nil {
  773. C.sqlite3_reset(s.s)
  774. C.sqlite3_clear_bindings(s.s)
  775. return nil, err
  776. }
  777. var rowid, changes C.longlong
  778. rv := C._sqlite3_step(s.s, &rowid, &changes)
  779. if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
  780. err := s.c.lastError()
  781. C.sqlite3_reset(s.s)
  782. C.sqlite3_clear_bindings(s.s)
  783. return nil, err
  784. }
  785. return &SQLiteResult{int64(rowid), int64(changes)}, nil
  786. }
  787. // Close the rows.
  788. func (rc *SQLiteRows) Close() error {
  789. if rc.s.closed {
  790. return nil
  791. }
  792. if rc.cls {
  793. return rc.s.Close()
  794. }
  795. rv := C.sqlite3_reset(rc.s.s)
  796. if rv != C.SQLITE_OK {
  797. return rc.s.c.lastError()
  798. }
  799. return nil
  800. }
  801. // Return column names.
  802. func (rc *SQLiteRows) Columns() []string {
  803. if rc.nc != len(rc.cols) {
  804. rc.cols = make([]string, rc.nc)
  805. for i := 0; i < rc.nc; i++ {
  806. rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
  807. }
  808. }
  809. return rc.cols
  810. }
  811. // Return column types.
  812. func (rc *SQLiteRows) DeclTypes() []string {
  813. if rc.decltype == nil {
  814. rc.decltype = make([]string, rc.nc)
  815. for i := 0; i < rc.nc; i++ {
  816. rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
  817. }
  818. }
  819. return rc.decltype
  820. }
  821. // Move cursor to next.
  822. func (rc *SQLiteRows) Next(dest []driver.Value) error {
  823. rv := C.sqlite3_step(rc.s.s)
  824. if rv == C.SQLITE_DONE {
  825. return io.EOF
  826. }
  827. if rv != C.SQLITE_ROW {
  828. rv = C.sqlite3_reset(rc.s.s)
  829. if rv != C.SQLITE_OK {
  830. return rc.s.c.lastError()
  831. }
  832. return nil
  833. }
  834. rc.DeclTypes()
  835. for i := range dest {
  836. switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
  837. case C.SQLITE_INTEGER:
  838. val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
  839. switch rc.decltype[i] {
  840. case "timestamp", "datetime", "date":
  841. var t time.Time
  842. // Assume a millisecond unix timestamp if it's 13 digits -- too
  843. // large to be a reasonable timestamp in seconds.
  844. if val > 1e12 || val < -1e12 {
  845. val *= int64(time.Millisecond) // convert ms to nsec
  846. } else {
  847. val *= int64(time.Second) // convert sec to nsec
  848. }
  849. t = time.Unix(0, val).UTC()
  850. if rc.s.c.loc != nil {
  851. t = t.In(rc.s.c.loc)
  852. }
  853. dest[i] = t
  854. case "boolean":
  855. dest[i] = val > 0
  856. default:
  857. dest[i] = val
  858. }
  859. case C.SQLITE_FLOAT:
  860. dest[i] = float64(C.sqlite3_column_double(rc.s.s, C.int(i)))
  861. case C.SQLITE_BLOB:
  862. p := C.sqlite3_column_blob(rc.s.s, C.int(i))
  863. if p == nil {
  864. dest[i] = nil
  865. continue
  866. }
  867. n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
  868. switch dest[i].(type) {
  869. case sql.RawBytes:
  870. dest[i] = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
  871. default:
  872. slice := make([]byte, n)
  873. copy(slice[:], (*[1 << 30]byte)(unsafe.Pointer(p))[0:n])
  874. dest[i] = slice
  875. }
  876. case C.SQLITE_NULL:
  877. dest[i] = nil
  878. case C.SQLITE_TEXT:
  879. var err error
  880. var timeVal time.Time
  881. n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
  882. s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n))
  883. switch rc.decltype[i] {
  884. case "timestamp", "datetime", "date":
  885. var t time.Time
  886. s = strings.TrimSuffix(s, "Z")
  887. for _, format := range SQLiteTimestampFormats {
  888. if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
  889. t = timeVal
  890. break
  891. }
  892. }
  893. if err != nil {
  894. // The column is a time value, so return the zero time on parse failure.
  895. t = time.Time{}
  896. }
  897. if rc.s.c.loc != nil {
  898. t = t.In(rc.s.c.loc)
  899. }
  900. dest[i] = t
  901. default:
  902. dest[i] = []byte(s)
  903. }
  904. }
  905. }
  906. return nil
  907. }