sqlite3.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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. #include <sqlite3.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #ifdef __CYGWIN__
  11. # include <errno.h>
  12. #endif
  13. #ifndef SQLITE_OPEN_READWRITE
  14. # define SQLITE_OPEN_READWRITE 0
  15. #endif
  16. #ifndef SQLITE_OPEN_FULLMUTEX
  17. # define SQLITE_OPEN_FULLMUTEX 0
  18. #endif
  19. static int
  20. _sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
  21. #ifdef SQLITE_OPEN_URI
  22. return sqlite3_open_v2(filename, ppDb, flags | SQLITE_OPEN_URI, zVfs);
  23. #else
  24. return sqlite3_open_v2(filename, ppDb, flags, zVfs);
  25. #endif
  26. }
  27. static int
  28. _sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
  29. return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
  30. }
  31. static int
  32. _sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
  33. return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
  34. }
  35. #include <stdio.h>
  36. #include <stdint.h>
  37. static long
  38. _sqlite3_last_insert_rowid(sqlite3* db) {
  39. return (long) sqlite3_last_insert_rowid(db);
  40. }
  41. static long
  42. _sqlite3_changes(sqlite3* db) {
  43. return (long) sqlite3_changes(db);
  44. }
  45. */
  46. import "C"
  47. import (
  48. "database/sql"
  49. "database/sql/driver"
  50. "errors"
  51. "io"
  52. "runtime"
  53. "strings"
  54. "time"
  55. "unsafe"
  56. )
  57. // Timestamp formats understood by both this module and SQLite.
  58. // The first format in the slice will be used when saving time values
  59. // into the database. When parsing a string from a timestamp or
  60. // datetime column, the formats are tried in order.
  61. var SQLiteTimestampFormats = []string{
  62. "2006-01-02 15:04:05.999999999",
  63. "2006-01-02T15:04:05.999999999",
  64. "2006-01-02 15:04:05",
  65. "2006-01-02T15:04:05",
  66. "2006-01-02 15:04",
  67. "2006-01-02T15:04",
  68. "2006-01-02",
  69. "2006-01-02 15:04:05-07:00",
  70. }
  71. func init() {
  72. sql.Register("sqlite3", &SQLiteDriver{})
  73. }
  74. // Return SQLite library Version information.
  75. func Version() (libVersion string, libVersionNumber int, sourceId string) {
  76. libVersion = C.GoString(C.sqlite3_libversion())
  77. libVersionNumber = int(C.sqlite3_libversion_number())
  78. sourceId = C.GoString(C.sqlite3_sourceid())
  79. return libVersion, libVersionNumber, sourceId
  80. }
  81. // Driver struct.
  82. type SQLiteDriver struct {
  83. Extensions []string
  84. ConnectHook func(*SQLiteConn) error
  85. }
  86. // Conn struct.
  87. type SQLiteConn struct {
  88. db *C.sqlite3
  89. }
  90. // Tx struct.
  91. type SQLiteTx struct {
  92. c *SQLiteConn
  93. }
  94. // Stmt struct.
  95. type SQLiteStmt struct {
  96. c *SQLiteConn
  97. s *C.sqlite3_stmt
  98. t string
  99. closed bool
  100. cls bool
  101. }
  102. // Result struct.
  103. type SQLiteResult struct {
  104. id int64
  105. changes int64
  106. }
  107. // Rows struct.
  108. type SQLiteRows struct {
  109. s *SQLiteStmt
  110. nc int
  111. cols []string
  112. decltype []string
  113. cls bool
  114. }
  115. // Commit transaction.
  116. func (tx *SQLiteTx) Commit() error {
  117. _, err := tx.c.exec("COMMIT")
  118. return err
  119. }
  120. // Rollback transaction.
  121. func (tx *SQLiteTx) Rollback() error {
  122. _, err := tx.c.exec("ROLLBACK")
  123. return err
  124. }
  125. // AutoCommit return which currently auto commit or not.
  126. func (c *SQLiteConn) AutoCommit() bool {
  127. return int(C.sqlite3_get_autocommit(c.db)) != 0
  128. }
  129. func (c *SQLiteConn) lastError() Error {
  130. return Error{
  131. Code: ErrNo(C.sqlite3_errcode(c.db)),
  132. ExtendedCode: ErrNoExtended(C.sqlite3_extended_errcode(c.db)),
  133. err: C.GoString(C.sqlite3_errmsg(c.db)),
  134. }
  135. }
  136. // Implements Execer
  137. func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
  138. if len(args) == 0 {
  139. return c.exec(query)
  140. }
  141. for {
  142. s, err := c.Prepare(query)
  143. if err != nil {
  144. return nil, err
  145. }
  146. var res driver.Result
  147. if s.(*SQLiteStmt).s != nil {
  148. na := s.NumInput()
  149. if len(args) < na {
  150. return nil, errors.New("args is not enough to execute query")
  151. }
  152. res, err = s.Exec(args[:na])
  153. if err != nil && err != driver.ErrSkip {
  154. s.Close()
  155. return nil, err
  156. }
  157. args = args[na:]
  158. }
  159. tail := s.(*SQLiteStmt).t
  160. s.Close()
  161. if tail == "" {
  162. return res, nil
  163. }
  164. query = tail
  165. }
  166. }
  167. // Implements Queryer
  168. func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
  169. for {
  170. s, err := c.Prepare(query)
  171. if err != nil {
  172. return nil, err
  173. }
  174. s.(*SQLiteStmt).cls = true
  175. na := s.NumInput()
  176. rows, err := s.Query(args[:na])
  177. if err != nil && err != driver.ErrSkip {
  178. s.Close()
  179. return nil, err
  180. }
  181. args = args[na:]
  182. tail := s.(*SQLiteStmt).t
  183. if tail == "" {
  184. return rows, nil
  185. }
  186. rows.Close()
  187. s.Close()
  188. query = tail
  189. }
  190. }
  191. func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {
  192. pcmd := C.CString(cmd)
  193. defer C.free(unsafe.Pointer(pcmd))
  194. rv := C.sqlite3_exec(c.db, pcmd, nil, nil, nil)
  195. if rv != C.SQLITE_OK {
  196. return nil, c.lastError()
  197. }
  198. return &SQLiteResult{
  199. int64(C._sqlite3_last_insert_rowid(c.db)),
  200. int64(C._sqlite3_changes(c.db)),
  201. }, nil
  202. }
  203. // Begin transaction.
  204. func (c *SQLiteConn) Begin() (driver.Tx, error) {
  205. if _, err := c.exec("BEGIN"); err != nil {
  206. return nil, err
  207. }
  208. return &SQLiteTx{c}, nil
  209. }
  210. func errorString(err Error) string {
  211. return C.GoString(C.sqlite3_errstr(C.int(err.Code)))
  212. }
  213. // Open database and return a new connection.
  214. // You can specify DSN string with URI filename.
  215. // test.db
  216. // file:test.db?cache=shared&mode=memory
  217. // :memory:
  218. // file::memory:
  219. func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
  220. if C.sqlite3_threadsafe() == 0 {
  221. return nil, errors.New("sqlite library was not compiled for thread-safe operation")
  222. }
  223. var db *C.sqlite3
  224. name := C.CString(dsn)
  225. defer C.free(unsafe.Pointer(name))
  226. rv := C._sqlite3_open_v2(name, &db,
  227. C.SQLITE_OPEN_FULLMUTEX|
  228. C.SQLITE_OPEN_READWRITE|
  229. C.SQLITE_OPEN_CREATE,
  230. nil)
  231. if rv != 0 {
  232. return nil, Error{Code: ErrNo(rv)}
  233. }
  234. if db == nil {
  235. return nil, errors.New("sqlite succeeded without returning a database")
  236. }
  237. rv = C.sqlite3_busy_timeout(db, 5000)
  238. if rv != C.SQLITE_OK {
  239. return nil, Error{Code: ErrNo(rv)}
  240. }
  241. conn := &SQLiteConn{db}
  242. if len(d.Extensions) > 0 {
  243. rv = C.sqlite3_enable_load_extension(db, 1)
  244. if rv != C.SQLITE_OK {
  245. return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
  246. }
  247. stmt, err := conn.Prepare("SELECT load_extension(?);")
  248. if err != nil {
  249. return nil, err
  250. }
  251. for _, extension := range d.Extensions {
  252. if _, err = stmt.Exec([]driver.Value{extension}); err != nil {
  253. return nil, err
  254. }
  255. }
  256. if err = stmt.Close(); err != nil {
  257. return nil, err
  258. }
  259. rv = C.sqlite3_enable_load_extension(db, 0)
  260. if rv != C.SQLITE_OK {
  261. return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
  262. }
  263. }
  264. if d.ConnectHook != nil {
  265. if err := d.ConnectHook(conn); err != nil {
  266. return nil, err
  267. }
  268. }
  269. runtime.SetFinalizer(conn, (*SQLiteConn).Close)
  270. return conn, nil
  271. }
  272. // Close the connection.
  273. func (c *SQLiteConn) Close() error {
  274. rv := C.sqlite3_close_v2(c.db)
  275. if rv != C.SQLITE_OK {
  276. return c.lastError()
  277. }
  278. c.db = nil
  279. runtime.SetFinalizer(c, nil)
  280. return nil
  281. }
  282. // Prepare query string. Return a new statement.
  283. func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
  284. pquery := C.CString(query)
  285. defer C.free(unsafe.Pointer(pquery))
  286. var s *C.sqlite3_stmt
  287. var tail *C.char
  288. rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
  289. if rv != C.SQLITE_OK {
  290. return nil, c.lastError()
  291. }
  292. var t string
  293. if tail != nil && C.strlen(tail) > 0 {
  294. t = strings.TrimSpace(C.GoString(tail))
  295. }
  296. ss := &SQLiteStmt{c: c, s: s, t: t}
  297. runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
  298. return ss, nil
  299. }
  300. // Close the statement.
  301. func (s *SQLiteStmt) Close() error {
  302. if s.closed {
  303. return nil
  304. }
  305. s.closed = true
  306. if s.c == nil || s.c.db == nil {
  307. return errors.New("sqlite statement with already closed database connection")
  308. }
  309. rv := C.sqlite3_finalize(s.s)
  310. if rv != C.SQLITE_OK {
  311. return s.c.lastError()
  312. }
  313. runtime.SetFinalizer(s, nil)
  314. return nil
  315. }
  316. // Return a number of parameters.
  317. func (s *SQLiteStmt) NumInput() int {
  318. return int(C.sqlite3_bind_parameter_count(s.s))
  319. }
  320. func (s *SQLiteStmt) bind(args []driver.Value) error {
  321. rv := C.sqlite3_reset(s.s)
  322. if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
  323. return s.c.lastError()
  324. }
  325. for i, v := range args {
  326. n := C.int(i + 1)
  327. switch v := v.(type) {
  328. case nil:
  329. rv = C.sqlite3_bind_null(s.s, n)
  330. case string:
  331. if len(v) == 0 {
  332. b := []byte{0}
  333. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(0))
  334. } else {
  335. b := []byte(v)
  336. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
  337. }
  338. case int64:
  339. rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
  340. case bool:
  341. if bool(v) {
  342. rv = C.sqlite3_bind_int(s.s, n, 1)
  343. } else {
  344. rv = C.sqlite3_bind_int(s.s, n, 0)
  345. }
  346. case float64:
  347. rv = C.sqlite3_bind_double(s.s, n, C.double(v))
  348. case []byte:
  349. var p *byte
  350. if len(v) > 0 {
  351. p = &v[0]
  352. }
  353. rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(p), C.int(len(v)))
  354. case time.Time:
  355. b := []byte(v.UTC().Format(SQLiteTimestampFormats[0]))
  356. rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
  357. }
  358. if rv != C.SQLITE_OK {
  359. return s.c.lastError()
  360. }
  361. }
  362. return nil
  363. }
  364. // Query the statement with arguments. Return records.
  365. func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
  366. if err := s.bind(args); err != nil {
  367. return nil, err
  368. }
  369. return &SQLiteRows{s, int(C.sqlite3_column_count(s.s)), nil, nil, s.cls}, nil
  370. }
  371. // Return last inserted ID.
  372. func (r *SQLiteResult) LastInsertId() (int64, error) {
  373. return r.id, nil
  374. }
  375. // Return how many rows affected.
  376. func (r *SQLiteResult) RowsAffected() (int64, error) {
  377. return r.changes, nil
  378. }
  379. // Execute the statement with arguments. Return result object.
  380. func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
  381. if err := s.bind(args); err != nil {
  382. C.sqlite3_reset(s.s)
  383. return nil, err
  384. }
  385. rv := C.sqlite3_step(s.s)
  386. if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
  387. C.sqlite3_reset(s.s)
  388. return nil, s.c.lastError()
  389. }
  390. res := &SQLiteResult{
  391. int64(C._sqlite3_last_insert_rowid(s.c.db)),
  392. int64(C._sqlite3_changes(s.c.db)),
  393. }
  394. return res, nil
  395. }
  396. // Close the rows.
  397. func (rc *SQLiteRows) Close() error {
  398. if rc.s.closed {
  399. return nil
  400. }
  401. if rc.cls {
  402. return rc.s.Close()
  403. }
  404. rv := C.sqlite3_reset(rc.s.s)
  405. if rv != C.SQLITE_OK {
  406. return rc.s.c.lastError()
  407. }
  408. return nil
  409. }
  410. // Return column names.
  411. func (rc *SQLiteRows) Columns() []string {
  412. if rc.nc != len(rc.cols) {
  413. rc.cols = make([]string, rc.nc)
  414. for i := 0; i < rc.nc; i++ {
  415. rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
  416. }
  417. }
  418. return rc.cols
  419. }
  420. // Move cursor to next.
  421. func (rc *SQLiteRows) Next(dest []driver.Value) error {
  422. rv := C.sqlite3_step(rc.s.s)
  423. if rv == C.SQLITE_DONE {
  424. return io.EOF
  425. }
  426. if rv != C.SQLITE_ROW {
  427. rv = C.sqlite3_reset(rc.s.s)
  428. if rv != C.SQLITE_OK {
  429. return rc.s.c.lastError()
  430. }
  431. return nil
  432. }
  433. if rc.decltype == nil {
  434. rc.decltype = make([]string, rc.nc)
  435. for i := 0; i < rc.nc; i++ {
  436. rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
  437. }
  438. }
  439. for i := range dest {
  440. switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
  441. case C.SQLITE_INTEGER:
  442. val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
  443. switch rc.decltype[i] {
  444. case "timestamp", "datetime", "date":
  445. dest[i] = time.Unix(val, 0).Local()
  446. case "boolean":
  447. dest[i] = val > 0
  448. default:
  449. dest[i] = val
  450. }
  451. case C.SQLITE_FLOAT:
  452. dest[i] = float64(C.sqlite3_column_double(rc.s.s, C.int(i)))
  453. case C.SQLITE_BLOB:
  454. p := C.sqlite3_column_blob(rc.s.s, C.int(i))
  455. if p == nil {
  456. dest[i] = nil
  457. continue
  458. }
  459. n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
  460. switch dest[i].(type) {
  461. case sql.RawBytes:
  462. dest[i] = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
  463. default:
  464. slice := make([]byte, n)
  465. copy(slice[:], (*[1 << 30]byte)(unsafe.Pointer(p))[0:n])
  466. dest[i] = slice
  467. }
  468. case C.SQLITE_NULL:
  469. dest[i] = nil
  470. case C.SQLITE_TEXT:
  471. var err error
  472. var timeVal time.Time
  473. s := C.GoString((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))))
  474. switch rc.decltype[i] {
  475. case "timestamp", "datetime", "date":
  476. for _, format := range SQLiteTimestampFormats {
  477. if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
  478. dest[i] = timeVal.Local()
  479. break
  480. }
  481. }
  482. if err != nil {
  483. // The column is a time value, so return the zero time on parse failure.
  484. dest[i] = time.Time{}
  485. }
  486. default:
  487. dest[i] = []byte(s)
  488. }
  489. }
  490. }
  491. return nil
  492. }