sqlite.go 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  1. // Copyright 2017 The Sqlite Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:generate go run generator.go -full-path-comments
  5. package sqlite // import "modernc.org/sqlite"
  6. import (
  7. "context"
  8. "database/sql"
  9. "database/sql/driver"
  10. "fmt"
  11. "io"
  12. "math"
  13. "net/url"
  14. "reflect"
  15. "strconv"
  16. "strings"
  17. "sync"
  18. "sync/atomic"
  19. "time"
  20. "unsafe"
  21. "modernc.org/libc"
  22. "modernc.org/libc/sys/types"
  23. sqlite3 "modernc.org/sqlite/lib"
  24. )
  25. var (
  26. _ driver.Conn = (*conn)(nil)
  27. _ driver.Driver = (*Driver)(nil)
  28. //lint:ignore SA1019 TODO implement ExecerContext
  29. _ driver.Execer = (*conn)(nil)
  30. //lint:ignore SA1019 TODO implement QueryerContext
  31. _ driver.Queryer = (*conn)(nil)
  32. _ driver.Result = (*result)(nil)
  33. _ driver.Rows = (*rows)(nil)
  34. _ driver.RowsColumnTypeDatabaseTypeName = (*rows)(nil)
  35. _ driver.RowsColumnTypeLength = (*rows)(nil)
  36. _ driver.RowsColumnTypeNullable = (*rows)(nil)
  37. _ driver.RowsColumnTypePrecisionScale = (*rows)(nil)
  38. _ driver.RowsColumnTypeScanType = (*rows)(nil)
  39. _ driver.Stmt = (*stmt)(nil)
  40. _ driver.Tx = (*tx)(nil)
  41. _ error = (*Error)(nil)
  42. )
  43. const (
  44. driverName = "sqlite"
  45. ptrSize = unsafe.Sizeof(uintptr(0))
  46. sqliteLockedSharedcache = sqlite3.SQLITE_LOCKED | (1 << 8)
  47. )
  48. // Error represents sqlite library error code.
  49. type Error struct {
  50. msg string
  51. code int
  52. }
  53. // Error implements error.
  54. func (e *Error) Error() string { return e.msg }
  55. // Code returns the sqlite result code for this error.
  56. func (e *Error) Code() int { return e.code }
  57. var (
  58. // ErrorCodeString maps Error.Code() to its string representation.
  59. ErrorCodeString = map[int]string{
  60. sqlite3.SQLITE_ABORT: "Callback routine requested an abort (SQLITE_ABORT)",
  61. sqlite3.SQLITE_AUTH: "Authorization denied (SQLITE_AUTH)",
  62. sqlite3.SQLITE_BUSY: "The database file is locked (SQLITE_BUSY)",
  63. sqlite3.SQLITE_CANTOPEN: "Unable to open the database file (SQLITE_CANTOPEN)",
  64. sqlite3.SQLITE_CONSTRAINT: "Abort due to constraint violation (SQLITE_CONSTRAINT)",
  65. sqlite3.SQLITE_CORRUPT: "The database disk image is malformed (SQLITE_CORRUPT)",
  66. sqlite3.SQLITE_DONE: "sqlite3_step() has finished executing (SQLITE_DONE)",
  67. sqlite3.SQLITE_EMPTY: "Internal use only (SQLITE_EMPTY)",
  68. sqlite3.SQLITE_ERROR: "Generic error (SQLITE_ERROR)",
  69. sqlite3.SQLITE_FORMAT: "Not used (SQLITE_FORMAT)",
  70. sqlite3.SQLITE_FULL: "Insertion failed because database is full (SQLITE_FULL)",
  71. sqlite3.SQLITE_INTERNAL: "Internal logic error in SQLite (SQLITE_INTERNAL)",
  72. sqlite3.SQLITE_INTERRUPT: "Operation terminated by sqlite3_interrupt()(SQLITE_INTERRUPT)",
  73. sqlite3.SQLITE_IOERR | (1 << 8): "(SQLITE_IOERR_READ)",
  74. sqlite3.SQLITE_IOERR | (10 << 8): "(SQLITE_IOERR_DELETE)",
  75. sqlite3.SQLITE_IOERR | (11 << 8): "(SQLITE_IOERR_BLOCKED)",
  76. sqlite3.SQLITE_IOERR | (12 << 8): "(SQLITE_IOERR_NOMEM)",
  77. sqlite3.SQLITE_IOERR | (13 << 8): "(SQLITE_IOERR_ACCESS)",
  78. sqlite3.SQLITE_IOERR | (14 << 8): "(SQLITE_IOERR_CHECKRESERVEDLOCK)",
  79. sqlite3.SQLITE_IOERR | (15 << 8): "(SQLITE_IOERR_LOCK)",
  80. sqlite3.SQLITE_IOERR | (16 << 8): "(SQLITE_IOERR_CLOSE)",
  81. sqlite3.SQLITE_IOERR | (17 << 8): "(SQLITE_IOERR_DIR_CLOSE)",
  82. sqlite3.SQLITE_IOERR | (2 << 8): "(SQLITE_IOERR_SHORT_READ)",
  83. sqlite3.SQLITE_IOERR | (3 << 8): "(SQLITE_IOERR_WRITE)",
  84. sqlite3.SQLITE_IOERR | (4 << 8): "(SQLITE_IOERR_FSYNC)",
  85. sqlite3.SQLITE_IOERR | (5 << 8): "(SQLITE_IOERR_DIR_FSYNC)",
  86. sqlite3.SQLITE_IOERR | (6 << 8): "(SQLITE_IOERR_TRUNCATE)",
  87. sqlite3.SQLITE_IOERR | (7 << 8): "(SQLITE_IOERR_FSTAT)",
  88. sqlite3.SQLITE_IOERR | (8 << 8): "(SQLITE_IOERR_UNLOCK)",
  89. sqlite3.SQLITE_IOERR | (9 << 8): "(SQLITE_IOERR_RDLOCK)",
  90. sqlite3.SQLITE_IOERR: "Some kind of disk I/O error occurred (SQLITE_IOERR)",
  91. sqlite3.SQLITE_LOCKED | (1 << 8): "(SQLITE_LOCKED_SHAREDCACHE)",
  92. sqlite3.SQLITE_LOCKED: "A table in the database is locked (SQLITE_LOCKED)",
  93. sqlite3.SQLITE_MISMATCH: "Data type mismatch (SQLITE_MISMATCH)",
  94. sqlite3.SQLITE_MISUSE: "Library used incorrectly (SQLITE_MISUSE)",
  95. sqlite3.SQLITE_NOLFS: "Uses OS features not supported on host (SQLITE_NOLFS)",
  96. sqlite3.SQLITE_NOMEM: "A malloc() failed (SQLITE_NOMEM)",
  97. sqlite3.SQLITE_NOTADB: "File opened that is not a database file (SQLITE_NOTADB)",
  98. sqlite3.SQLITE_NOTFOUND: "Unknown opcode in sqlite3_file_control() (SQLITE_NOTFOUND)",
  99. sqlite3.SQLITE_NOTICE: "Notifications from sqlite3_log() (SQLITE_NOTICE)",
  100. sqlite3.SQLITE_PERM: "Access permission denied (SQLITE_PERM)",
  101. sqlite3.SQLITE_PROTOCOL: "Database lock protocol error (SQLITE_PROTOCOL)",
  102. sqlite3.SQLITE_RANGE: "2nd parameter to sqlite3_bind out of range (SQLITE_RANGE)",
  103. sqlite3.SQLITE_READONLY: "Attempt to write a readonly database (SQLITE_READONLY)",
  104. sqlite3.SQLITE_ROW: "sqlite3_step() has another row ready (SQLITE_ROW)",
  105. sqlite3.SQLITE_SCHEMA: "The database schema changed (SQLITE_SCHEMA)",
  106. sqlite3.SQLITE_TOOBIG: "String or BLOB exceeds size limit (SQLITE_TOOBIG)",
  107. sqlite3.SQLITE_WARNING: "Warnings from sqlite3_log() (SQLITE_WARNING)",
  108. }
  109. )
  110. func init() {
  111. sql.Register(driverName, newDriver())
  112. }
  113. type result struct {
  114. lastInsertID int64
  115. rowsAffected int
  116. }
  117. func newResult(c *conn) (_ *result, err error) {
  118. r := &result{}
  119. if r.rowsAffected, err = c.changes(); err != nil {
  120. return nil, err
  121. }
  122. if r.lastInsertID, err = c.lastInsertRowID(); err != nil {
  123. return nil, err
  124. }
  125. return r, nil
  126. }
  127. // LastInsertId returns the database's auto-generated ID after, for example, an
  128. // INSERT into a table with primary key.
  129. func (r *result) LastInsertId() (int64, error) {
  130. if r == nil {
  131. return 0, nil
  132. }
  133. return r.lastInsertID, nil
  134. }
  135. // RowsAffected returns the number of rows affected by the query.
  136. func (r *result) RowsAffected() (int64, error) {
  137. if r == nil {
  138. return 0, nil
  139. }
  140. return int64(r.rowsAffected), nil
  141. }
  142. type rows struct {
  143. allocs []uintptr
  144. c *conn
  145. columns []string
  146. pstmt uintptr
  147. doStep bool
  148. empty bool
  149. }
  150. func newRows(c *conn, pstmt uintptr, allocs []uintptr, empty bool) (r *rows, err error) {
  151. r = &rows{c: c, pstmt: pstmt, allocs: allocs, empty: empty}
  152. defer func() {
  153. if err != nil {
  154. r.Close()
  155. r = nil
  156. }
  157. }()
  158. n, err := c.columnCount(pstmt)
  159. if err != nil {
  160. return nil, err
  161. }
  162. r.columns = make([]string, n)
  163. for i := range r.columns {
  164. if r.columns[i], err = r.c.columnName(pstmt, i); err != nil {
  165. return nil, err
  166. }
  167. }
  168. return r, nil
  169. }
  170. // Close closes the rows iterator.
  171. func (r *rows) Close() (err error) {
  172. for _, v := range r.allocs {
  173. r.c.free(v)
  174. }
  175. r.allocs = nil
  176. return r.c.finalize(r.pstmt)
  177. }
  178. // Columns returns the names of the columns. The number of columns of the
  179. // result is inferred from the length of the slice. If a particular column name
  180. // isn't known, an empty string should be returned for that entry.
  181. func (r *rows) Columns() (c []string) {
  182. return r.columns
  183. }
  184. // Next is called to populate the next row of data into the provided slice. The
  185. // provided slice will be the same size as the Columns() are wide.
  186. //
  187. // Next should return io.EOF when there are no more rows.
  188. func (r *rows) Next(dest []driver.Value) (err error) {
  189. if r.empty {
  190. return io.EOF
  191. }
  192. rc := sqlite3.SQLITE_ROW
  193. if r.doStep {
  194. if rc, err = r.c.step(r.pstmt); err != nil {
  195. return err
  196. }
  197. }
  198. r.doStep = true
  199. switch rc {
  200. case sqlite3.SQLITE_ROW:
  201. if g, e := len(dest), len(r.columns); g != e {
  202. return fmt.Errorf("sqlite: Next: have %v destination values, expected %v", g, e)
  203. }
  204. for i := range dest {
  205. ct, err := r.c.columnType(r.pstmt, i)
  206. if err != nil {
  207. return err
  208. }
  209. switch ct {
  210. case sqlite3.SQLITE_INTEGER:
  211. v, err := r.c.columnInt64(r.pstmt, i)
  212. if err != nil {
  213. return err
  214. }
  215. dest[i] = v
  216. case sqlite3.SQLITE_FLOAT:
  217. v, err := r.c.columnDouble(r.pstmt, i)
  218. if err != nil {
  219. return err
  220. }
  221. dest[i] = v
  222. case sqlite3.SQLITE_TEXT:
  223. v, err := r.c.columnText(r.pstmt, i)
  224. if err != nil {
  225. return err
  226. }
  227. switch r.ColumnTypeDatabaseTypeName(i) {
  228. case "DATE", "DATETIME", "TIMESTAMP":
  229. dest[i], _ = r.c.parseTime(v)
  230. default:
  231. dest[i] = v
  232. }
  233. case sqlite3.SQLITE_BLOB:
  234. v, err := r.c.columnBlob(r.pstmt, i)
  235. if err != nil {
  236. return err
  237. }
  238. dest[i] = v
  239. case sqlite3.SQLITE_NULL:
  240. dest[i] = nil
  241. default:
  242. return fmt.Errorf("internal error: rc %d", rc)
  243. }
  244. }
  245. return nil
  246. case sqlite3.SQLITE_DONE:
  247. return io.EOF
  248. default:
  249. return r.c.errstr(int32(rc))
  250. }
  251. }
  252. // Inspired by mattn/go-sqlite3: https://github.com/mattn/go-sqlite3/blob/ab91e934/sqlite3.go#L210-L226
  253. //
  254. // These time.Parse formats handle formats 1 through 7 listed at https://www.sqlite.org/lang_datefunc.html.
  255. var parseTimeFormats = []string{
  256. "2006-01-02 15:04:05.999999999-07:00",
  257. "2006-01-02T15:04:05.999999999-07:00",
  258. "2006-01-02 15:04:05.999999999",
  259. "2006-01-02T15:04:05.999999999",
  260. "2006-01-02 15:04",
  261. "2006-01-02T15:04",
  262. "2006-01-02",
  263. }
  264. // Attempt to parse s as a time. Return (s, false) if s is not
  265. // recognized as a valid time encoding.
  266. func (c *conn) parseTime(s string) (interface{}, bool) {
  267. if v, ok := c.parseTimeString(s, strings.Index(s, "m=")); ok {
  268. return v, true
  269. }
  270. ts := strings.TrimSuffix(s, "Z")
  271. for _, f := range parseTimeFormats {
  272. t, err := time.Parse(f, ts)
  273. if err == nil {
  274. return t, true
  275. }
  276. }
  277. return s, false
  278. }
  279. // Attempt to parse s as a time string produced by t.String(). If x > 0 it's
  280. // the index of substring "m=" within s. Return (s, false) if s is
  281. // not recognized as a valid time encoding.
  282. func (c *conn) parseTimeString(s0 string, x int) (interface{}, bool) {
  283. s := s0
  284. if x > 0 {
  285. s = s[:x] // "2006-01-02 15:04:05.999999999 -0700 MST m=+9999" -> "2006-01-02 15:04:05.999999999 -0700 MST "
  286. }
  287. s = strings.TrimSpace(s)
  288. if t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", s); err == nil {
  289. return t, true
  290. }
  291. return s0, false
  292. }
  293. // writeTimeFormats are the names and formats supported
  294. // by the `_time_format` DSN query param.
  295. var writeTimeFormats = map[string]string{
  296. "sqlite": parseTimeFormats[0],
  297. }
  298. func (c *conn) formatTime(t time.Time) string {
  299. // Before configurable write time formats were supported,
  300. // time.Time.String was used. Maintain that default to
  301. // keep existing driver users formatting times the same.
  302. if c.writeTimeFormat == "" {
  303. return t.String()
  304. }
  305. return t.Format(c.writeTimeFormat)
  306. }
  307. // RowsColumnTypeDatabaseTypeName may be implemented by Rows. It should return
  308. // the database system type name without the length. Type names should be
  309. // uppercase. Examples of returned types: "VARCHAR", "NVARCHAR", "VARCHAR2",
  310. // "CHAR", "TEXT", "DECIMAL", "SMALLINT", "INT", "BIGINT", "BOOL", "[]BIGINT",
  311. // "JSONB", "XML", "TIMESTAMP".
  312. func (r *rows) ColumnTypeDatabaseTypeName(index int) string {
  313. return strings.ToUpper(r.c.columnDeclType(r.pstmt, index))
  314. }
  315. // RowsColumnTypeLength may be implemented by Rows. It should return the length
  316. // of the column type if the column is a variable length type. If the column is
  317. // not a variable length type ok should return false. If length is not limited
  318. // other than system limits, it should return math.MaxInt64. The following are
  319. // examples of returned values for various types:
  320. //
  321. // TEXT (math.MaxInt64, true)
  322. // varchar(10) (10, true)
  323. // nvarchar(10) (10, true)
  324. // decimal (0, false)
  325. // int (0, false)
  326. // bytea(30) (30, true)
  327. func (r *rows) ColumnTypeLength(index int) (length int64, ok bool) {
  328. t, err := r.c.columnType(r.pstmt, index)
  329. if err != nil {
  330. return 0, false
  331. }
  332. switch t {
  333. case sqlite3.SQLITE_INTEGER:
  334. return 0, false
  335. case sqlite3.SQLITE_FLOAT:
  336. return 0, false
  337. case sqlite3.SQLITE_TEXT:
  338. return math.MaxInt64, true
  339. case sqlite3.SQLITE_BLOB:
  340. return math.MaxInt64, true
  341. case sqlite3.SQLITE_NULL:
  342. return 0, false
  343. default:
  344. return 0, false
  345. }
  346. }
  347. // RowsColumnTypeNullable may be implemented by Rows. The nullable value should
  348. // be true if it is known the column may be null, or false if the column is
  349. // known to be not nullable. If the column nullability is unknown, ok should be
  350. // false.
  351. func (r *rows) ColumnTypeNullable(index int) (nullable, ok bool) {
  352. return true, true
  353. }
  354. // RowsColumnTypePrecisionScale may be implemented by Rows. It should return
  355. // the precision and scale for decimal types. If not applicable, ok should be
  356. // false. The following are examples of returned values for various types:
  357. //
  358. // decimal(38, 4) (38, 4, true)
  359. // int (0, 0, false)
  360. // decimal (math.MaxInt64, math.MaxInt64, true)
  361. func (r *rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
  362. return 0, 0, false
  363. }
  364. // RowsColumnTypeScanType may be implemented by Rows. It should return the
  365. // value type that can be used to scan types into. For example, the database
  366. // column type "bigint" this should return "reflect.TypeOf(int64(0))".
  367. func (r *rows) ColumnTypeScanType(index int) reflect.Type {
  368. t, err := r.c.columnType(r.pstmt, index)
  369. if err != nil {
  370. return reflect.TypeOf("")
  371. }
  372. switch t {
  373. case sqlite3.SQLITE_INTEGER:
  374. switch strings.ToLower(r.c.columnDeclType(r.pstmt, index)) {
  375. case "boolean":
  376. return reflect.TypeOf(false)
  377. case "date", "datetime", "time", "timestamp":
  378. return reflect.TypeOf(time.Time{})
  379. default:
  380. return reflect.TypeOf(int64(0))
  381. }
  382. case sqlite3.SQLITE_FLOAT:
  383. return reflect.TypeOf(float64(0))
  384. case sqlite3.SQLITE_TEXT:
  385. return reflect.TypeOf("")
  386. case sqlite3.SQLITE_BLOB:
  387. return reflect.SliceOf(reflect.TypeOf([]byte{}))
  388. case sqlite3.SQLITE_NULL:
  389. return reflect.TypeOf(nil)
  390. default:
  391. return reflect.TypeOf("")
  392. }
  393. }
  394. type stmt struct {
  395. c *conn
  396. psql uintptr
  397. }
  398. func newStmt(c *conn, sql string) (*stmt, error) {
  399. p, err := libc.CString(sql)
  400. if err != nil {
  401. return nil, err
  402. }
  403. stm := stmt{c: c, psql: p}
  404. return &stm, nil
  405. }
  406. // Close closes the statement.
  407. //
  408. // As of Go 1.1, a Stmt will not be closed if it's in use by any queries.
  409. func (s *stmt) Close() (err error) {
  410. s.c.free(s.psql)
  411. s.psql = 0
  412. return nil
  413. }
  414. // Exec executes a query that doesn't return rows, such as an INSERT or UPDATE.
  415. //
  416. //
  417. // Deprecated: Drivers should implement StmtExecContext instead (or
  418. // additionally).
  419. func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { //TODO StmtExecContext
  420. return s.exec(context.Background(), toNamedValues(args))
  421. }
  422. // toNamedValues converts []driver.Value to []driver.NamedValue
  423. func toNamedValues(vals []driver.Value) (r []driver.NamedValue) {
  424. r = make([]driver.NamedValue, len(vals))
  425. for i, val := range vals {
  426. r[i] = driver.NamedValue{Value: val, Ordinal: i + 1}
  427. }
  428. return r
  429. }
  430. func (s *stmt) exec(ctx context.Context, args []driver.NamedValue) (r driver.Result, err error) {
  431. var pstmt uintptr
  432. var done int32
  433. if ctx != nil && ctx.Done() != nil {
  434. defer interruptOnDone(ctx, s.c, &done)()
  435. }
  436. for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0 && atomic.LoadInt32(&done) == 0; {
  437. if pstmt, err = s.c.prepareV2(&psql); err != nil {
  438. return nil, err
  439. }
  440. if pstmt == 0 {
  441. continue
  442. }
  443. err = func() (err error) {
  444. n, err := s.c.bindParameterCount(pstmt)
  445. if err != nil {
  446. return err
  447. }
  448. if n != 0 {
  449. allocs, err := s.c.bind(pstmt, n, args)
  450. if err != nil {
  451. return err
  452. }
  453. if len(allocs) != 0 {
  454. defer func() {
  455. for _, v := range allocs {
  456. s.c.free(v)
  457. }
  458. }()
  459. }
  460. }
  461. rc, err := s.c.step(pstmt)
  462. if err != nil {
  463. return err
  464. }
  465. switch rc & 0xff {
  466. case sqlite3.SQLITE_DONE, sqlite3.SQLITE_ROW:
  467. // nop
  468. default:
  469. return s.c.errstr(int32(rc))
  470. }
  471. return nil
  472. }()
  473. if e := s.c.finalize(pstmt); e != nil && err == nil {
  474. err = e
  475. }
  476. if err != nil {
  477. return nil, err
  478. }
  479. }
  480. return newResult(s.c)
  481. }
  482. // NumInput returns the number of placeholder parameters.
  483. //
  484. // If NumInput returns >= 0, the sql package will sanity check argument counts
  485. // from callers and return errors to the caller before the statement's Exec or
  486. // Query methods are called.
  487. //
  488. // NumInput may also return -1, if the driver doesn't know its number of
  489. // placeholders. In that case, the sql package will not sanity check Exec or
  490. // Query argument counts.
  491. func (s *stmt) NumInput() (n int) {
  492. return -1
  493. }
  494. // Query executes a query that may return rows, such as a
  495. // SELECT.
  496. //
  497. // Deprecated: Drivers should implement StmtQueryContext instead (or
  498. // additionally).
  499. func (s *stmt) Query(args []driver.Value) (driver.Rows, error) { //TODO StmtQueryContext
  500. return s.query(context.Background(), toNamedValues(args))
  501. }
  502. func (s *stmt) query(ctx context.Context, args []driver.NamedValue) (r driver.Rows, err error) {
  503. var pstmt uintptr
  504. var done int32
  505. if ctx != nil && ctx.Done() != nil {
  506. defer interruptOnDone(ctx, s.c, &done)()
  507. }
  508. var allocs []uintptr
  509. for psql := s.psql; *(*byte)(unsafe.Pointer(psql)) != 0 && atomic.LoadInt32(&done) == 0; {
  510. if pstmt, err = s.c.prepareV2(&psql); err != nil {
  511. return nil, err
  512. }
  513. if pstmt == 0 {
  514. continue
  515. }
  516. err = func() (err error) {
  517. n, err := s.c.bindParameterCount(pstmt)
  518. if err != nil {
  519. return err
  520. }
  521. if n != 0 {
  522. if allocs, err = s.c.bind(pstmt, n, args); err != nil {
  523. return err
  524. }
  525. }
  526. rc, err := s.c.step(pstmt)
  527. if err != nil {
  528. return err
  529. }
  530. switch rc & 0xff {
  531. case sqlite3.SQLITE_ROW:
  532. if r != nil {
  533. r.Close()
  534. }
  535. if r, err = newRows(s.c, pstmt, allocs, false); err != nil {
  536. return err
  537. }
  538. pstmt = 0
  539. return nil
  540. case sqlite3.SQLITE_DONE:
  541. if r == nil {
  542. if r, err = newRows(s.c, pstmt, allocs, true); err != nil {
  543. return err
  544. }
  545. pstmt = 0
  546. return nil
  547. }
  548. // nop
  549. default:
  550. return s.c.errstr(int32(rc))
  551. }
  552. if *(*byte)(unsafe.Pointer(psql)) == 0 {
  553. if r != nil {
  554. r.Close()
  555. }
  556. if r, err = newRows(s.c, pstmt, allocs, true); err != nil {
  557. return err
  558. }
  559. pstmt = 0
  560. }
  561. return nil
  562. }()
  563. if e := s.c.finalize(pstmt); e != nil && err == nil {
  564. err = e
  565. }
  566. if err != nil {
  567. return nil, err
  568. }
  569. }
  570. return r, err
  571. }
  572. type tx struct {
  573. c *conn
  574. }
  575. func newTx(c *conn, opts driver.TxOptions) (*tx, error) {
  576. r := &tx{c: c}
  577. sql := "begin"
  578. if !opts.ReadOnly && c.beginMode != "" {
  579. sql = "begin " + c.beginMode
  580. }
  581. if err := r.exec(context.Background(), sql); err != nil {
  582. return nil, err
  583. }
  584. return r, nil
  585. }
  586. // Commit implements driver.Tx.
  587. func (t *tx) Commit() (err error) {
  588. return t.exec(context.Background(), "commit")
  589. }
  590. // Rollback implements driver.Tx.
  591. func (t *tx) Rollback() (err error) {
  592. return t.exec(context.Background(), "rollback")
  593. }
  594. func (t *tx) exec(ctx context.Context, sql string) (err error) {
  595. psql, err := libc.CString(sql)
  596. if err != nil {
  597. return err
  598. }
  599. defer t.c.free(psql)
  600. //TODO use t.conn.ExecContext() instead
  601. if ctx != nil && ctx.Done() != nil {
  602. defer interruptOnDone(ctx, t.c, nil)()
  603. }
  604. if rc := sqlite3.Xsqlite3_exec(t.c.tls, t.c.db, psql, 0, 0, 0); rc != sqlite3.SQLITE_OK {
  605. return t.c.errstr(rc)
  606. }
  607. return nil
  608. }
  609. // interruptOnDone sets up a goroutine to interrupt the provided db when the
  610. // context is canceled, and returns a function the caller must defer so it
  611. // doesn't interrupt after the caller finishes.
  612. func interruptOnDone(
  613. ctx context.Context,
  614. c *conn,
  615. done *int32,
  616. ) func() {
  617. if done == nil {
  618. var d int32
  619. done = &d
  620. }
  621. donech := make(chan struct{})
  622. go func() {
  623. select {
  624. case <-ctx.Done():
  625. // don't call interrupt if we were already done: it indicates that this
  626. // call to exec is no longer running and we would be interrupting
  627. // nothing, or even possibly an unrelated later call to exec.
  628. if atomic.AddInt32(done, 1) == 1 {
  629. c.interrupt(c.db)
  630. }
  631. case <-donech:
  632. }
  633. }()
  634. // the caller is expected to defer this function
  635. return func() {
  636. // set the done flag so that a context cancellation right after the caller
  637. // returns doesn't trigger a call to interrupt for some other statement.
  638. atomic.AddInt32(done, 1)
  639. close(donech)
  640. }
  641. }
  642. type conn struct {
  643. db uintptr // *sqlite3.Xsqlite3
  644. tls *libc.TLS
  645. // Context handling can cause conn.Close and conn.interrupt to be invoked
  646. // concurrently.
  647. sync.Mutex
  648. writeTimeFormat string
  649. beginMode string
  650. }
  651. func newConn(dsn string) (*conn, error) {
  652. var query, vfsName string
  653. // Parse the query parameters from the dsn and them from the dsn if not prefixed by file:
  654. // https://github.com/mattn/go-sqlite3/blob/3392062c729d77820afc1f5cae3427f0de39e954/sqlite3.go#L1046
  655. // https://github.com/mattn/go-sqlite3/blob/3392062c729d77820afc1f5cae3427f0de39e954/sqlite3.go#L1383
  656. pos := strings.IndexRune(dsn, '?')
  657. if pos >= 1 {
  658. query = dsn[pos+1:]
  659. var err error
  660. vfsName, err = getVFSName(query)
  661. if err != nil {
  662. return nil, err
  663. }
  664. if !strings.HasPrefix(dsn, "file:") {
  665. dsn = dsn[:pos]
  666. }
  667. }
  668. c := &conn{tls: libc.NewTLS()}
  669. db, err := c.openV2(
  670. dsn,
  671. vfsName,
  672. sqlite3.SQLITE_OPEN_READWRITE|sqlite3.SQLITE_OPEN_CREATE|
  673. sqlite3.SQLITE_OPEN_FULLMUTEX|
  674. sqlite3.SQLITE_OPEN_URI,
  675. )
  676. if err != nil {
  677. return nil, err
  678. }
  679. c.db = db
  680. if err = c.extendedResultCodes(true); err != nil {
  681. c.Close()
  682. return nil, err
  683. }
  684. if err = applyQueryParams(c, query); err != nil {
  685. c.Close()
  686. return nil, err
  687. }
  688. return c, nil
  689. }
  690. func getVFSName(query string) (r string, err error) {
  691. q, err := url.ParseQuery(query)
  692. if err != nil {
  693. return "", err
  694. }
  695. for _, v := range q["vfs"] {
  696. if r != "" && r != v {
  697. return "", fmt.Errorf("conflicting vfs query parameters: %v", q["vfs"])
  698. }
  699. r = v
  700. }
  701. return r, nil
  702. }
  703. func applyQueryParams(c *conn, query string) error {
  704. q, err := url.ParseQuery(query)
  705. if err != nil {
  706. return err
  707. }
  708. for _, v := range q["_pragma"] {
  709. cmd := "pragma " + v
  710. _, err := c.exec(context.Background(), cmd, nil)
  711. if err != nil {
  712. return err
  713. }
  714. }
  715. if v := q.Get("_time_format"); v != "" {
  716. f, ok := writeTimeFormats[v]
  717. if !ok {
  718. return fmt.Errorf("unknown _time_format %q", v)
  719. }
  720. c.writeTimeFormat = f
  721. }
  722. if v := q.Get("_txlock"); v != "" {
  723. lower := strings.ToLower(v)
  724. if lower != "deferred" && lower != "immediate" && lower != "exclusive" {
  725. return fmt.Errorf("unknown _txlock %q", v)
  726. }
  727. c.beginMode = v
  728. }
  729. return nil
  730. }
  731. // const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
  732. func (c *conn) columnBlob(pstmt uintptr, iCol int) (v []byte, err error) {
  733. p := sqlite3.Xsqlite3_column_blob(c.tls, pstmt, int32(iCol))
  734. len, err := c.columnBytes(pstmt, iCol)
  735. if err != nil {
  736. return nil, err
  737. }
  738. if p == 0 || len == 0 {
  739. return nil, nil
  740. }
  741. v = make([]byte, len)
  742. copy(v, (*libc.RawMem)(unsafe.Pointer(p))[:len:len])
  743. return v, nil
  744. }
  745. // int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
  746. func (c *conn) columnBytes(pstmt uintptr, iCol int) (_ int, err error) {
  747. v := sqlite3.Xsqlite3_column_bytes(c.tls, pstmt, int32(iCol))
  748. return int(v), nil
  749. }
  750. // const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  751. func (c *conn) columnText(pstmt uintptr, iCol int) (v string, err error) {
  752. p := sqlite3.Xsqlite3_column_text(c.tls, pstmt, int32(iCol))
  753. len, err := c.columnBytes(pstmt, iCol)
  754. if err != nil {
  755. return "", err
  756. }
  757. if p == 0 || len == 0 {
  758. return "", nil
  759. }
  760. b := make([]byte, len)
  761. copy(b, (*libc.RawMem)(unsafe.Pointer(p))[:len:len])
  762. return string(b), nil
  763. }
  764. // double sqlite3_column_double(sqlite3_stmt*, int iCol);
  765. func (c *conn) columnDouble(pstmt uintptr, iCol int) (v float64, err error) {
  766. v = sqlite3.Xsqlite3_column_double(c.tls, pstmt, int32(iCol))
  767. return v, nil
  768. }
  769. // sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
  770. func (c *conn) columnInt64(pstmt uintptr, iCol int) (v int64, err error) {
  771. v = sqlite3.Xsqlite3_column_int64(c.tls, pstmt, int32(iCol))
  772. return v, nil
  773. }
  774. // int sqlite3_column_type(sqlite3_stmt*, int iCol);
  775. func (c *conn) columnType(pstmt uintptr, iCol int) (_ int, err error) {
  776. v := sqlite3.Xsqlite3_column_type(c.tls, pstmt, int32(iCol))
  777. return int(v), nil
  778. }
  779. // const char *sqlite3_column_decltype(sqlite3_stmt*,int);
  780. func (c *conn) columnDeclType(pstmt uintptr, iCol int) string {
  781. return libc.GoString(sqlite3.Xsqlite3_column_decltype(c.tls, pstmt, int32(iCol)))
  782. }
  783. // const char *sqlite3_column_name(sqlite3_stmt*, int N);
  784. func (c *conn) columnName(pstmt uintptr, n int) (string, error) {
  785. p := sqlite3.Xsqlite3_column_name(c.tls, pstmt, int32(n))
  786. return libc.GoString(p), nil
  787. }
  788. // int sqlite3_column_count(sqlite3_stmt *pStmt);
  789. func (c *conn) columnCount(pstmt uintptr) (_ int, err error) {
  790. v := sqlite3.Xsqlite3_column_count(c.tls, pstmt)
  791. return int(v), nil
  792. }
  793. // sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
  794. func (c *conn) lastInsertRowID() (v int64, _ error) {
  795. return sqlite3.Xsqlite3_last_insert_rowid(c.tls, c.db), nil
  796. }
  797. // int sqlite3_changes(sqlite3*);
  798. func (c *conn) changes() (int, error) {
  799. v := sqlite3.Xsqlite3_changes(c.tls, c.db)
  800. return int(v), nil
  801. }
  802. // int sqlite3_step(sqlite3_stmt*);
  803. func (c *conn) step(pstmt uintptr) (int, error) {
  804. for {
  805. switch rc := sqlite3.Xsqlite3_step(c.tls, pstmt); rc {
  806. case sqliteLockedSharedcache:
  807. if err := c.retry(pstmt); err != nil {
  808. return sqlite3.SQLITE_LOCKED, err
  809. }
  810. case
  811. sqlite3.SQLITE_DONE,
  812. sqlite3.SQLITE_ROW:
  813. return int(rc), nil
  814. default:
  815. return int(rc), c.errstr(rc)
  816. }
  817. }
  818. }
  819. func (c *conn) retry(pstmt uintptr) error {
  820. mu := mutexAlloc(c.tls)
  821. (*mutex)(unsafe.Pointer(mu)).Lock()
  822. rc := sqlite3.Xsqlite3_unlock_notify(
  823. c.tls,
  824. c.db,
  825. *(*uintptr)(unsafe.Pointer(&struct {
  826. f func(*libc.TLS, uintptr, int32)
  827. }{unlockNotify})),
  828. mu,
  829. )
  830. if rc == sqlite3.SQLITE_LOCKED { // Deadlock, see https://www.sqlite.org/c3ref/unlock_notify.html
  831. (*mutex)(unsafe.Pointer(mu)).Unlock()
  832. mutexFree(c.tls, mu)
  833. return c.errstr(rc)
  834. }
  835. (*mutex)(unsafe.Pointer(mu)).Lock()
  836. (*mutex)(unsafe.Pointer(mu)).Unlock()
  837. mutexFree(c.tls, mu)
  838. if pstmt != 0 {
  839. sqlite3.Xsqlite3_reset(c.tls, pstmt)
  840. }
  841. return nil
  842. }
  843. func unlockNotify(t *libc.TLS, ppArg uintptr, nArg int32) {
  844. for i := int32(0); i < nArg; i++ {
  845. mu := *(*uintptr)(unsafe.Pointer(ppArg))
  846. (*mutex)(unsafe.Pointer(mu)).Unlock()
  847. ppArg += ptrSize
  848. }
  849. }
  850. func (c *conn) bind(pstmt uintptr, n int, args []driver.NamedValue) (allocs []uintptr, err error) {
  851. defer func() {
  852. if err == nil {
  853. return
  854. }
  855. for _, v := range allocs {
  856. c.free(v)
  857. }
  858. allocs = nil
  859. }()
  860. for i := 1; i <= n; i++ {
  861. name, err := c.bindParameterName(pstmt, i)
  862. if err != nil {
  863. return allocs, err
  864. }
  865. var found bool
  866. var v driver.NamedValue
  867. for _, v = range args {
  868. if name != "" {
  869. // For ?NNN and $NNN params, match if NNN == v.Ordinal.
  870. //
  871. // Supporting this for $NNN is a special case that makes eg
  872. // `select $1, $2, $3 ...` work without needing to use
  873. // sql.Named.
  874. if (name[0] == '?' || name[0] == '$') && name[1:] == strconv.Itoa(v.Ordinal) {
  875. found = true
  876. break
  877. }
  878. // sqlite supports '$', '@' and ':' prefixes for string
  879. // identifiers and '?' for numeric, so we cannot
  880. // combine different prefixes with the same name
  881. // because `database/sql` requires variable names
  882. // to start with a letter
  883. if name[1:] == v.Name[:] {
  884. found = true
  885. break
  886. }
  887. } else {
  888. if v.Ordinal == i {
  889. found = true
  890. break
  891. }
  892. }
  893. }
  894. if !found {
  895. if name != "" {
  896. return allocs, fmt.Errorf("missing named argument %q", name[1:])
  897. }
  898. return allocs, fmt.Errorf("missing argument with index %d", i)
  899. }
  900. var p uintptr
  901. switch x := v.Value.(type) {
  902. case int64:
  903. if err := c.bindInt64(pstmt, i, x); err != nil {
  904. return allocs, err
  905. }
  906. case float64:
  907. if err := c.bindDouble(pstmt, i, x); err != nil {
  908. return allocs, err
  909. }
  910. case bool:
  911. v := 0
  912. if x {
  913. v = 1
  914. }
  915. if err := c.bindInt(pstmt, i, v); err != nil {
  916. return allocs, err
  917. }
  918. case []byte:
  919. if p, err = c.bindBlob(pstmt, i, x); err != nil {
  920. return allocs, err
  921. }
  922. case string:
  923. if p, err = c.bindText(pstmt, i, x); err != nil {
  924. return allocs, err
  925. }
  926. case time.Time:
  927. if p, err = c.bindText(pstmt, i, c.formatTime(x)); err != nil {
  928. return allocs, err
  929. }
  930. case nil:
  931. if p, err = c.bindNull(pstmt, i); err != nil {
  932. return allocs, err
  933. }
  934. default:
  935. return allocs, fmt.Errorf("sqlite: invalid driver.Value type %T", x)
  936. }
  937. if p != 0 {
  938. allocs = append(allocs, p)
  939. }
  940. }
  941. return allocs, nil
  942. }
  943. // int sqlite3_bind_null(sqlite3_stmt*, int);
  944. func (c *conn) bindNull(pstmt uintptr, idx1 int) (uintptr, error) {
  945. if rc := sqlite3.Xsqlite3_bind_null(c.tls, pstmt, int32(idx1)); rc != sqlite3.SQLITE_OK {
  946. return 0, c.errstr(rc)
  947. }
  948. return 0, nil
  949. }
  950. // int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
  951. func (c *conn) bindText(pstmt uintptr, idx1 int, value string) (uintptr, error) {
  952. p, err := libc.CString(value)
  953. if err != nil {
  954. return 0, err
  955. }
  956. if rc := sqlite3.Xsqlite3_bind_text(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK {
  957. c.free(p)
  958. return 0, c.errstr(rc)
  959. }
  960. return p, nil
  961. }
  962. // int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
  963. func (c *conn) bindBlob(pstmt uintptr, idx1 int, value []byte) (uintptr, error) {
  964. if value != nil && len(value) == 0 {
  965. if rc := sqlite3.Xsqlite3_bind_zeroblob(c.tls, pstmt, int32(idx1), 0); rc != sqlite3.SQLITE_OK {
  966. return 0, c.errstr(rc)
  967. }
  968. return 0, nil
  969. }
  970. p, err := c.malloc(len(value))
  971. if err != nil {
  972. return 0, err
  973. }
  974. if len(value) != 0 {
  975. copy((*libc.RawMem)(unsafe.Pointer(p))[:len(value):len(value)], value)
  976. }
  977. if rc := sqlite3.Xsqlite3_bind_blob(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK {
  978. c.free(p)
  979. return 0, c.errstr(rc)
  980. }
  981. return p, nil
  982. }
  983. // int sqlite3_bind_int(sqlite3_stmt*, int, int);
  984. func (c *conn) bindInt(pstmt uintptr, idx1, value int) (err error) {
  985. if rc := sqlite3.Xsqlite3_bind_int(c.tls, pstmt, int32(idx1), int32(value)); rc != sqlite3.SQLITE_OK {
  986. return c.errstr(rc)
  987. }
  988. return nil
  989. }
  990. // int sqlite3_bind_double(sqlite3_stmt*, int, double);
  991. func (c *conn) bindDouble(pstmt uintptr, idx1 int, value float64) (err error) {
  992. if rc := sqlite3.Xsqlite3_bind_double(c.tls, pstmt, int32(idx1), value); rc != 0 {
  993. return c.errstr(rc)
  994. }
  995. return nil
  996. }
  997. // int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
  998. func (c *conn) bindInt64(pstmt uintptr, idx1 int, value int64) (err error) {
  999. if rc := sqlite3.Xsqlite3_bind_int64(c.tls, pstmt, int32(idx1), value); rc != sqlite3.SQLITE_OK {
  1000. return c.errstr(rc)
  1001. }
  1002. return nil
  1003. }
  1004. // const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
  1005. func (c *conn) bindParameterName(pstmt uintptr, i int) (string, error) {
  1006. p := sqlite3.Xsqlite3_bind_parameter_name(c.tls, pstmt, int32(i))
  1007. return libc.GoString(p), nil
  1008. }
  1009. // int sqlite3_bind_parameter_count(sqlite3_stmt*);
  1010. func (c *conn) bindParameterCount(pstmt uintptr) (_ int, err error) {
  1011. r := sqlite3.Xsqlite3_bind_parameter_count(c.tls, pstmt)
  1012. return int(r), nil
  1013. }
  1014. // int sqlite3_finalize(sqlite3_stmt *pStmt);
  1015. func (c *conn) finalize(pstmt uintptr) error {
  1016. if rc := sqlite3.Xsqlite3_finalize(c.tls, pstmt); rc != sqlite3.SQLITE_OK {
  1017. return c.errstr(rc)
  1018. }
  1019. return nil
  1020. }
  1021. // int sqlite3_prepare_v2(
  1022. // sqlite3 *db, /* Database handle */
  1023. // const char *zSql, /* SQL statement, UTF-8 encoded */
  1024. // int nByte, /* Maximum length of zSql in bytes. */
  1025. // sqlite3_stmt **ppStmt, /* OUT: Statement handle */
  1026. // const char **pzTail /* OUT: Pointer to unused portion of zSql */
  1027. // );
  1028. func (c *conn) prepareV2(zSQL *uintptr) (pstmt uintptr, err error) {
  1029. var ppstmt, pptail uintptr
  1030. defer func() {
  1031. c.free(ppstmt)
  1032. c.free(pptail)
  1033. }()
  1034. if ppstmt, err = c.malloc(int(ptrSize)); err != nil {
  1035. return 0, err
  1036. }
  1037. if pptail, err = c.malloc(int(ptrSize)); err != nil {
  1038. return 0, err
  1039. }
  1040. for {
  1041. switch rc := sqlite3.Xsqlite3_prepare_v2(c.tls, c.db, *zSQL, -1, ppstmt, pptail); rc {
  1042. case sqlite3.SQLITE_OK:
  1043. *zSQL = *(*uintptr)(unsafe.Pointer(pptail))
  1044. return *(*uintptr)(unsafe.Pointer(ppstmt)), nil
  1045. case sqliteLockedSharedcache:
  1046. if err := c.retry(0); err != nil {
  1047. return 0, err
  1048. }
  1049. default:
  1050. return 0, c.errstr(rc)
  1051. }
  1052. }
  1053. }
  1054. // void sqlite3_interrupt(sqlite3*);
  1055. func (c *conn) interrupt(pdb uintptr) (err error) {
  1056. c.Lock() // Defend against race with .Close invoked by context handling.
  1057. defer c.Unlock()
  1058. if c.tls != nil {
  1059. sqlite3.Xsqlite3_interrupt(c.tls, pdb)
  1060. }
  1061. return nil
  1062. }
  1063. // int sqlite3_extended_result_codes(sqlite3*, int onoff);
  1064. func (c *conn) extendedResultCodes(on bool) error {
  1065. if rc := sqlite3.Xsqlite3_extended_result_codes(c.tls, c.db, libc.Bool32(on)); rc != sqlite3.SQLITE_OK {
  1066. return c.errstr(rc)
  1067. }
  1068. return nil
  1069. }
  1070. // int sqlite3_open_v2(
  1071. // const char *filename, /* Database filename (UTF-8) */
  1072. // sqlite3 **ppDb, /* OUT: SQLite db handle */
  1073. // int flags, /* Flags */
  1074. // const char *zVfs /* Name of VFS module to use */
  1075. // );
  1076. func (c *conn) openV2(name, vfsName string, flags int32) (uintptr, error) {
  1077. var p, s, vfs uintptr
  1078. defer func() {
  1079. if p != 0 {
  1080. c.free(p)
  1081. }
  1082. if s != 0 {
  1083. c.free(s)
  1084. }
  1085. if vfs != 0 {
  1086. c.free(vfs)
  1087. }
  1088. }()
  1089. p, err := c.malloc(int(ptrSize))
  1090. if err != nil {
  1091. return 0, err
  1092. }
  1093. if s, err = libc.CString(name); err != nil {
  1094. return 0, err
  1095. }
  1096. if vfsName != "" {
  1097. if vfs, err = libc.CString(vfsName); err != nil {
  1098. return 0, err
  1099. }
  1100. }
  1101. if rc := sqlite3.Xsqlite3_open_v2(c.tls, s, p, flags, vfs); rc != sqlite3.SQLITE_OK {
  1102. return 0, c.errstr(rc)
  1103. }
  1104. return *(*uintptr)(unsafe.Pointer(p)), nil
  1105. }
  1106. func (c *conn) malloc(n int) (uintptr, error) {
  1107. if p := libc.Xmalloc(c.tls, types.Size_t(n)); p != 0 || n == 0 {
  1108. return p, nil
  1109. }
  1110. return 0, fmt.Errorf("sqlite: cannot allocate %d bytes of memory", n)
  1111. }
  1112. func (c *conn) free(p uintptr) {
  1113. if p != 0 {
  1114. libc.Xfree(c.tls, p)
  1115. }
  1116. }
  1117. // const char *sqlite3_errstr(int);
  1118. func (c *conn) errstr(rc int32) error {
  1119. p := sqlite3.Xsqlite3_errstr(c.tls, rc)
  1120. str := libc.GoString(p)
  1121. p = sqlite3.Xsqlite3_errmsg(c.tls, c.db)
  1122. var s string
  1123. if rc == sqlite3.SQLITE_BUSY {
  1124. s = " (SQLITE_BUSY)"
  1125. }
  1126. switch msg := libc.GoString(p); {
  1127. case msg == str:
  1128. return &Error{msg: fmt.Sprintf("%s (%v)%s", str, rc, s), code: int(rc)}
  1129. default:
  1130. return &Error{msg: fmt.Sprintf("%s: %s (%v)%s", str, msg, rc, s), code: int(rc)}
  1131. }
  1132. }
  1133. // Begin starts a transaction.
  1134. //
  1135. // Deprecated: Drivers should implement ConnBeginTx instead (or additionally).
  1136. func (c *conn) Begin() (driver.Tx, error) {
  1137. return c.begin(context.Background(), driver.TxOptions{})
  1138. }
  1139. func (c *conn) begin(ctx context.Context, opts driver.TxOptions) (t driver.Tx, err error) {
  1140. return newTx(c, opts)
  1141. }
  1142. // Close invalidates and potentially stops any current prepared statements and
  1143. // transactions, marking this connection as no longer in use.
  1144. //
  1145. // Because the sql package maintains a free pool of connections and only calls
  1146. // Close when there's a surplus of idle connections, it shouldn't be necessary
  1147. // for drivers to do their own connection caching.
  1148. func (c *conn) Close() error {
  1149. c.Lock() // Defend against race with .interrupt invoked by context handling.
  1150. defer c.Unlock()
  1151. if c.db != 0 {
  1152. if err := c.closeV2(c.db); err != nil {
  1153. return err
  1154. }
  1155. c.db = 0
  1156. }
  1157. if c.tls != nil {
  1158. c.tls.Close()
  1159. c.tls = nil
  1160. }
  1161. return nil
  1162. }
  1163. // int sqlite3_close_v2(sqlite3*);
  1164. func (c *conn) closeV2(db uintptr) error {
  1165. if rc := sqlite3.Xsqlite3_close_v2(c.tls, db); rc != sqlite3.SQLITE_OK {
  1166. return c.errstr(rc)
  1167. }
  1168. return nil
  1169. }
  1170. type userDefinedFunction struct {
  1171. zFuncName uintptr
  1172. nArg int32
  1173. eTextRep int32
  1174. xFunc func(*libc.TLS, uintptr, int32, uintptr)
  1175. freeOnce sync.Once
  1176. }
  1177. func (c *conn) createFunctionInternal(fun *userDefinedFunction) error {
  1178. if rc := sqlite3.Xsqlite3_create_function(
  1179. c.tls,
  1180. c.db,
  1181. fun.zFuncName,
  1182. fun.nArg,
  1183. fun.eTextRep,
  1184. 0,
  1185. *(*uintptr)(unsafe.Pointer(&fun.xFunc)),
  1186. 0,
  1187. 0,
  1188. ); rc != sqlite3.SQLITE_OK {
  1189. return c.errstr(rc)
  1190. }
  1191. return nil
  1192. }
  1193. // Execer is an optional interface that may be implemented by a Conn.
  1194. //
  1195. // If a Conn does not implement Execer, the sql package's DB.Exec will first
  1196. // prepare a query, execute the statement, and then close the statement.
  1197. //
  1198. // Exec may return ErrSkip.
  1199. //
  1200. // Deprecated: Drivers should implement ExecerContext instead.
  1201. func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
  1202. return c.exec(context.Background(), query, toNamedValues(args))
  1203. }
  1204. func (c *conn) exec(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) {
  1205. s, err := c.prepare(ctx, query)
  1206. if err != nil {
  1207. return nil, err
  1208. }
  1209. defer func() {
  1210. if err2 := s.Close(); err2 != nil && err == nil {
  1211. err = err2
  1212. }
  1213. }()
  1214. return s.(*stmt).exec(ctx, args)
  1215. }
  1216. // Prepare returns a prepared statement, bound to this connection.
  1217. func (c *conn) Prepare(query string) (driver.Stmt, error) {
  1218. return c.prepare(context.Background(), query)
  1219. }
  1220. func (c *conn) prepare(ctx context.Context, query string) (s driver.Stmt, err error) {
  1221. //TODO use ctx
  1222. return newStmt(c, query)
  1223. }
  1224. // Queryer is an optional interface that may be implemented by a Conn.
  1225. //
  1226. // If a Conn does not implement Queryer, the sql package's DB.Query will first
  1227. // prepare a query, execute the statement, and then close the statement.
  1228. //
  1229. // Query may return ErrSkip.
  1230. //
  1231. // Deprecated: Drivers should implement QueryerContext instead.
  1232. func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
  1233. return c.query(context.Background(), query, toNamedValues(args))
  1234. }
  1235. func (c *conn) query(ctx context.Context, query string, args []driver.NamedValue) (r driver.Rows, err error) {
  1236. s, err := c.prepare(ctx, query)
  1237. if err != nil {
  1238. return nil, err
  1239. }
  1240. defer func() {
  1241. if err2 := s.Close(); err2 != nil && err == nil {
  1242. err = err2
  1243. }
  1244. }()
  1245. return s.(*stmt).query(ctx, args)
  1246. }
  1247. // Driver implements database/sql/driver.Driver.
  1248. type Driver struct {
  1249. // user defined functions that are added to every new connection on Open
  1250. udfs map[string]*userDefinedFunction
  1251. }
  1252. var d = &Driver{udfs: make(map[string]*userDefinedFunction)}
  1253. func newDriver() *Driver { return d }
  1254. // Open returns a new connection to the database. The name is a string in a
  1255. // driver-specific format.
  1256. //
  1257. // Open may return a cached connection (one previously closed), but doing so is
  1258. // unnecessary; the sql package maintains a pool of idle connections for
  1259. // efficient re-use.
  1260. //
  1261. // The returned connection is only used by one goroutine at a time.
  1262. //
  1263. // If name contains a '?', what follows is treated as a query string. This
  1264. // driver supports the following query parameters:
  1265. //
  1266. // _pragma: Each value will be run as a "PRAGMA ..." statement (with the PRAGMA
  1267. // keyword added for you). May be specified more than once. Example:
  1268. // "_pragma=foreign_keys(1)" will enable foreign key enforcement. More
  1269. // information on supported PRAGMAs is available from the SQLite documentation:
  1270. // https://www.sqlite.org/pragma.html
  1271. //
  1272. // _time_format: The name of a format to use when writing time values to the
  1273. // database. Currently the only supported value is "sqlite", which corresponds
  1274. // to format 7 from https://www.sqlite.org/lang_datefunc.html#time_values,
  1275. // including the timezone specifier. If this parameter is not specified, then
  1276. // the default String() format will be used.
  1277. //
  1278. // _txlock: The locking behavior to use when beginning a transaction. May be
  1279. // "deferred", "immediate", or "exclusive" (case insensitive). The default is to
  1280. // not specify one, which SQLite maps to "deferred". More information is
  1281. // available at
  1282. // https://www.sqlite.org/lang_transaction.html#deferred_immediate_and_exclusive_transactions
  1283. func (d *Driver) Open(name string) (driver.Conn, error) {
  1284. c, err := newConn(name)
  1285. if err != nil {
  1286. return nil, err
  1287. }
  1288. for _, udf := range d.udfs {
  1289. if err = c.createFunctionInternal(udf); err != nil {
  1290. c.Close()
  1291. return nil, err
  1292. }
  1293. }
  1294. return c, nil
  1295. }
  1296. // FunctionContext represents the context user defined functions execute in.
  1297. // Fields and/or methods of this type may get addedd in the future.
  1298. type FunctionContext struct{}
  1299. const sqliteValPtrSize = unsafe.Sizeof(&sqlite3.Sqlite3_value{})
  1300. // RegisterScalarFunction registers a scalar function named zFuncName with nArg
  1301. // arguments. Passing -1 for nArg indicates the function is variadic.
  1302. //
  1303. // The new function will be available to all new connections opened after
  1304. // executing RegisterScalarFunction.
  1305. func RegisterScalarFunction(
  1306. zFuncName string,
  1307. nArg int32,
  1308. xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
  1309. ) error {
  1310. return registerScalarFunction(zFuncName, nArg, sqlite3.SQLITE_UTF8, xFunc)
  1311. }
  1312. // MustRegisterScalarFunction is like RegisterScalarFunction but panics on
  1313. // error.
  1314. func MustRegisterScalarFunction(
  1315. zFuncName string,
  1316. nArg int32,
  1317. xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
  1318. ) {
  1319. if err := RegisterScalarFunction(zFuncName, nArg, xFunc); err != nil {
  1320. panic(err)
  1321. }
  1322. }
  1323. // MustRegisterDeterministicScalarFunction is like
  1324. // RegisterDeterministicScalarFunction but panics on error.
  1325. func MustRegisterDeterministicScalarFunction(
  1326. zFuncName string,
  1327. nArg int32,
  1328. xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
  1329. ) {
  1330. if err := RegisterDeterministicScalarFunction(zFuncName, nArg, xFunc); err != nil {
  1331. panic(err)
  1332. }
  1333. }
  1334. // RegisterDeterministicScalarFunction registers a deterministic scalar
  1335. // function named zFuncName with nArg arguments. Passing -1 for nArg indicates
  1336. // the function is variadic. A deterministic function means that the function
  1337. // always gives the same output when the input parameters are the same.
  1338. //
  1339. // The new function will be available to all new connections opened after
  1340. // executing RegisterDeterministicScalarFunction.
  1341. func RegisterDeterministicScalarFunction(
  1342. zFuncName string,
  1343. nArg int32,
  1344. xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
  1345. ) error {
  1346. return registerScalarFunction(zFuncName, nArg, sqlite3.SQLITE_UTF8|sqlite3.SQLITE_DETERMINISTIC, xFunc)
  1347. }
  1348. func registerScalarFunction(
  1349. zFuncName string,
  1350. nArg int32,
  1351. eTextRep int32,
  1352. xFunc func(ctx *FunctionContext, args []driver.Value) (driver.Value, error),
  1353. ) error {
  1354. if _, ok := d.udfs[zFuncName]; ok {
  1355. return fmt.Errorf("a function named %q is already registered", zFuncName)
  1356. }
  1357. // dont free, functions registered on the driver live as long as the program
  1358. name, err := libc.CString(zFuncName)
  1359. if err != nil {
  1360. return err
  1361. }
  1362. udf := &userDefinedFunction{
  1363. zFuncName: name,
  1364. nArg: nArg,
  1365. eTextRep: eTextRep,
  1366. xFunc: func(tls *libc.TLS, ctx uintptr, argc int32, argv uintptr) {
  1367. setErrorResult := func(res error) {
  1368. errmsg, cerr := libc.CString(res.Error())
  1369. if cerr != nil {
  1370. panic(cerr)
  1371. }
  1372. defer libc.Xfree(tls, errmsg)
  1373. sqlite3.Xsqlite3_result_error(tls, ctx, errmsg, -1)
  1374. sqlite3.Xsqlite3_result_error_code(tls, ctx, sqlite3.SQLITE_ERROR)
  1375. }
  1376. args := make([]driver.Value, argc)
  1377. for i := int32(0); i < argc; i++ {
  1378. valPtr := *(*uintptr)(unsafe.Pointer(argv + uintptr(i)*sqliteValPtrSize))
  1379. switch valType := sqlite3.Xsqlite3_value_type(tls, valPtr); valType {
  1380. case sqlite3.SQLITE_TEXT:
  1381. args[i] = libc.GoString(sqlite3.Xsqlite3_value_text(tls, valPtr))
  1382. case sqlite3.SQLITE_INTEGER:
  1383. args[i] = sqlite3.Xsqlite3_value_int64(tls, valPtr)
  1384. case sqlite3.SQLITE_FLOAT:
  1385. args[i] = sqlite3.Xsqlite3_value_double(tls, valPtr)
  1386. case sqlite3.SQLITE_NULL:
  1387. args[i] = nil
  1388. case sqlite3.SQLITE_BLOB:
  1389. size := sqlite3.Xsqlite3_value_bytes(tls, valPtr)
  1390. blobPtr := sqlite3.Xsqlite3_value_blob(tls, valPtr)
  1391. v := make([]byte, size)
  1392. copy(v, (*libc.RawMem)(unsafe.Pointer(blobPtr))[:size:size])
  1393. args[i] = v
  1394. default:
  1395. panic(fmt.Sprintf("unexpected argument type %q passed by sqlite", valType))
  1396. }
  1397. }
  1398. res, err := xFunc(&FunctionContext{}, args)
  1399. if err != nil {
  1400. setErrorResult(err)
  1401. return
  1402. }
  1403. switch resTyped := res.(type) {
  1404. case nil:
  1405. sqlite3.Xsqlite3_result_null(tls, ctx)
  1406. case int64:
  1407. sqlite3.Xsqlite3_result_int64(tls, ctx, resTyped)
  1408. case float64:
  1409. sqlite3.Xsqlite3_result_double(tls, ctx, resTyped)
  1410. case bool:
  1411. sqlite3.Xsqlite3_result_int(tls, ctx, libc.Bool32(resTyped))
  1412. case time.Time:
  1413. sqlite3.Xsqlite3_result_int64(tls, ctx, resTyped.Unix())
  1414. case string:
  1415. size := int32(len(resTyped))
  1416. cstr, err := libc.CString(resTyped)
  1417. if err != nil {
  1418. panic(err)
  1419. }
  1420. defer libc.Xfree(tls, cstr)
  1421. sqlite3.Xsqlite3_result_text(tls, ctx, cstr, size, sqlite3.SQLITE_TRANSIENT)
  1422. case []byte:
  1423. size := int32(len(resTyped))
  1424. if size == 0 {
  1425. sqlite3.Xsqlite3_result_zeroblob(tls, ctx, 0)
  1426. return
  1427. }
  1428. p := libc.Xmalloc(tls, types.Size_t(size))
  1429. if p == 0 {
  1430. panic(fmt.Sprintf("unable to allocate space for blob: %d", size))
  1431. }
  1432. defer libc.Xfree(tls, p)
  1433. copy((*libc.RawMem)(unsafe.Pointer(p))[:size:size], resTyped)
  1434. sqlite3.Xsqlite3_result_blob(tls, ctx, p, size, sqlite3.SQLITE_TRANSIENT)
  1435. default:
  1436. setErrorResult(fmt.Errorf("function did not return a valid driver.Value: %T", resTyped))
  1437. return
  1438. }
  1439. },
  1440. }
  1441. d.udfs[zFuncName] = udf
  1442. return nil
  1443. }