sqlite3.go 25 KB

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