-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsqlite.go
1058 lines (962 loc) · 28.4 KB
/
sqlite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sqlite implements a database/sql driver for SQLite3.
//
// This driver requires a file: URI always be used to open a database.
// For details see https://sqlite.org/c3ref/open.html#urifilenames.
//
// # Initializing connections or tracing
//
// If you want to do initial configuration of a connection, or enable
// tracing, use the Connector function:
//
// connInitFunc := func(ctx context.Context, conn driver.ConnPrepareContext) error {
// return sqlite.ExecScript(conn.(sqlite.SQLConn), "PRAGMA journal_mode=WAL;")
// }
// db, err = sql.OpenDB(sqlite.Connector(sqliteURI, connInitFunc, nil))
//
// # Memory Mode
//
// In-memory databases are popular for tests.
// Use the "memdb" VFS (*not* the legacy in-memory modes) to be compatible
// with the database/sql connection pool:
//
// file:/dbname?vfs=memdb
//
// Use a different dbname for each memory database opened.
//
// # Binding Types
//
// SQLite is flexible about type conversions, and so is this driver.
// Almost all "basic" Go types (int, float64, string) are accepted and
// directly mapped into SQLite, even if they are named Go types.
// The time.Time type is also accepted (described below).
// Values that implement encoding.TextMarshaler or json.Marshaler are
// stored in SQLite in their marshaled form.
//
// # Binding Time
//
// While SQLite3 has no strict time datatype, it does have a series of built-in
// functions that operate on timestamps that expect columns to be in one of many
// formats: https://sqlite.org/lang_datefunc.html
//
// When encoding a time.Time into one of SQLite's preferred formats, we use the
// shortest timestamp format that can accurately represent the time.Time.
// The supported formats are:
//
// 2. YYYY-MM-DD HH:MM
// 3. YYYY-MM-DD HH:MM:SS
// 4. YYYY-MM-DD HH:MM:SS.SSS
//
// If the time.Time is not UTC (strongly consider storing times in UTC!),
// we follow SQLite's norm of appending "[+-]HH:MM" to the above formats.
//
// It is common in SQLite to store "Unix time", seconds-since-epoch in an
// INTEGER column. This is understood by the date and time functions documented
// in the link above. If you want to do that, pass the result of time.Time.Unix
// to the driver.
//
// # Reading Time
//
// In general, time is hard to extract from SQLite as a time.Time.
// If a column is defined as DATE or DATETIME, then text data is parsed
// as TimeFormat and returned as a time.Time. Integer data is parsed as
// seconds since epoch and returned as a time.Time.
package sqlite
import (
"context"
"database/sql"
"database/sql/driver"
"encoding"
"errors"
"expvar"
"fmt"
"io"
"reflect"
"strings"
"sync/atomic"
"time"
"github.com/tailscale/sqlite/sqliteh"
)
var Open sqliteh.OpenFunc = func(string, sqliteh.OpenFlags, string) (sqliteh.DB, error) {
return nil, fmt.Errorf("cgosqlite.Open is missing")
}
// ConnInitFunc is a function called by the driver on new connections.
//
// The conn can be used to execute queries, and implements SQLConn.
// Any error return closes the conn and passes the error to database/sql.
type ConnInitFunc func(ctx context.Context, conn driver.ConnPrepareContext) error
// TimeFormat is the string format this driver uses to store
// microsecond-precision time in SQLite in text format.
const TimeFormat = "2006-01-02 15:04:05.000-0700"
func init() {
sql.Register("sqlite3", drv{})
}
var maxConnID atomic.Int32
// UsesAfterClose is a metric that is incremented every time an operation is
// attempted on a connection after Close has already been called. The keys are
// internal identifiers for the code path that incremented a counter.
var UsesAfterClose expvar.Map
// ErrClosed is returned when an operation is attempted on a connection after
// Close has already been called.
var ErrClosed = errors.New("sqlite3: already closed")
type drv struct{}
func (drv) Open(name string) (driver.Conn, error) { panic("deprecated, unused") }
func (drv) OpenConnector(name string) (driver.Connector, error) {
return &connector{name: name}, nil
}
func Connector(sqliteURI string, connInitFunc ConnInitFunc, tracer sqliteh.Tracer) driver.Connector {
return &connector{
name: sqliteURI,
tracer: tracer,
connInitFunc: connInitFunc,
}
}
type connector struct {
name string
tracer sqliteh.Tracer
connInitFunc ConnInitFunc
}
func (p *connector) Driver() driver.Driver { return drv{} }
func (p *connector) Connect(ctx context.Context) (driver.Conn, error) {
db, err := Open(p.name, sqliteh.OpenFlagsDefault, "")
if err != nil {
if ec, ok := err.(sqliteh.ErrCode); ok {
e := &Error{
Code: sqliteh.Code(ec),
Loc: "Open",
}
if db != nil {
e.Msg = db.ErrMsg()
}
err = e
}
if db != nil {
db.Close()
}
return nil, err
}
c := &conn{
db: db,
tracer: p.tracer,
id: sqliteh.TraceConnID(maxConnID.Add(1)),
}
if p.connInitFunc != nil {
if err := p.connInitFunc(ctx, c); err != nil {
db.Close()
return nil, fmt.Errorf("sqlite.ConnInitFunc: %w", err)
}
}
return c, nil
}
type txState int
const (
txStateNone = txState(0) // connection is not connected to a Tx
txStateInit = txState(1) // BeginTx called, but "BEGIN;" not yet executed
txStateBegun = txState(2) // "BEGIN;" has been executed
)
type conn struct {
db sqliteh.DB
id sqliteh.TraceConnID
tracer sqliteh.Tracer
stmts map[string]*stmt // persisted statements
txState txState
readOnly bool
closed atomic.Bool
}
func (c *conn) Prepare(query string) (driver.Stmt, error) { panic("deprecated, unused") }
func (c *conn) Begin() (driver.Tx, error) { panic("deprecated, unused") }
func (c *conn) Close() error {
// Don't double-close
if !c.closed.CompareAndSwap(false, true) {
UsesAfterClose.Add("Close", 1)
return nil
}
for q, s := range c.stmts {
s.stmt.Finalize()
s.closed.Store(true)
delete(c.stmts, q)
}
err := reserr(c.db, "Conn.Close", "", c.db.Close())
return err
}
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
persist := ctx.Value(persistQuery{}) != nil
return c.prepare(ctx, query, persist)
}
func (c *conn) prepare(ctx context.Context, query string, persist bool) (s *stmt, err error) {
if c.closed.Load() {
UsesAfterClose.Add("prepare", 1)
return nil, ErrClosed
}
query = strings.TrimSpace(query)
if s := c.stmts[query]; s != nil {
// don't hand the same statement out twice; this is re-added on s.Close
delete(c.stmts, query)
s.prepCtx = ctx
if !s.closed.CompareAndSwap(true, false) {
// We'd previously set this to 'false', indicating that
// this stmt is in-use. Return an error instead of
// reusing the stmt.
return nil, ErrClosed
}
return s, nil
}
if c.tracer != nil {
// Not a hot path. Any high-load environment should use
// WithPersist so this is rare.
start := time.Now()
defer func() {
if err != nil {
c.tracer.Query(ctx, c.id, query, time.Since(start), err)
}
}()
}
var flags sqliteh.PrepareFlags
if persist {
flags = sqliteh.SQLITE_PREPARE_PERSISTENT
}
cstmt, rem, err := c.db.Prepare(query, flags)
if err != nil {
return nil, reserr(c.db, "Prepare", query, err)
}
if rem != "" {
cstmt.Finalize()
return nil, &Error{
Code: sqliteh.SQLITE_MISUSE,
Loc: "Prepare",
Query: query,
Msg: fmt.Sprintf("query has trailing text: %q", rem),
}
}
s = &stmt{
conn: c,
stmt: cstmt,
query: query,
persist: persist,
numInput: -1,
prepCtx: ctx,
}
if !persist {
return s, nil
}
// NOTE: don't add the statement to c.stmts here, since we could return
// it to another caller before Close is called; it's added to the
// c.stmts map on Close.
if c.stmts == nil {
c.stmts = make(map[string]*stmt)
}
return s, nil
}
func (c *conn) execInternal(ctx context.Context, query string) error {
s, err := c.prepare(ctx, query, true)
if err != nil {
if e, _ := err.(*Error); e != nil {
e.Loc = "internal:" + e.Loc
}
return err
}
if _, err := s.ExecContext(ctx, nil); err != nil {
return err
}
s.Close()
return nil
}
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
if c.closed.Load() {
UsesAfterClose.Add("BeginTx", 1)
return nil, ErrClosed
}
const LevelSerializable = 6 // matches the sql package constant
if opts.Isolation != 0 && opts.Isolation != LevelSerializable {
return nil, errors.New("github.com/tailscale/sqlite driver only supports serializable isolation level")
}
c.readOnly = opts.ReadOnly
c.txState = txStateInit
if c.tracer != nil {
c.tracer.BeginTx(ctx, c.id, "", c.readOnly, nil)
}
if err := c.txInit(ctx); err != nil {
return nil, err
}
return &connTx{conn: c}, nil
}
// Raw is so ConnInitFunc can cast to SQLConn.
func (c *conn) Raw(fn func(any) error) error { return fn(c) }
type readOnlyKey struct{}
// ReadOnly applies the query_only pragma to the connection.
func ReadOnly(ctx context.Context) context.Context {
return context.WithValue(ctx, readOnlyKey{}, true)
}
// IsReadOnly reports whether the context has the ReadOnly key.
func IsReadOnly(ctx context.Context) bool {
return ctx.Value(readOnlyKey{}) != nil
}
func (c *conn) txInit(ctx context.Context) error {
if c.txState != txStateInit {
return nil
}
c.txState = txStateBegun
if c.readOnly || IsReadOnly(ctx) {
if err := c.execInternal(ctx, "BEGIN"); err != nil {
return err
}
if err := c.execInternal(ctx, "PRAGMA query_only=true"); err != nil {
return err
}
} else {
// TODO(crawshaw): offer BEGIN DEFERRED (and BEGIN CONCURRENT?)
// semantics via a context annotation function.
if err := c.execInternal(ctx, "BEGIN IMMEDIATE"); err != nil {
return err
}
}
return nil
}
func (c *conn) txEnd(ctx context.Context, endStmt string) error {
state, readOnly := c.txState, c.readOnly
c.txState = txStateNone
c.readOnly = false
if state != txStateBegun {
return nil
}
err := c.execInternal(context.Background(), endStmt)
if readOnly {
if err2 := c.execInternal(ctx, "PRAGMA query_only=false"); err == nil {
err = err2
}
}
return err
}
type connTx struct {
conn *conn
}
func (tx *connTx) Commit() error {
if tx.conn.closed.Load() {
UsesAfterClose.Add("tx.Commit", 1)
return ErrClosed
}
err := tx.conn.txEnd(context.Background(), "COMMIT")
if tx.conn.tracer != nil {
tx.conn.tracer.Commit(tx.conn.id, err)
}
return err
}
func (tx *connTx) Rollback() error {
if tx.conn.closed.Load() {
UsesAfterClose.Add("tx.Rollback", 1)
return ErrClosed
}
err := tx.conn.txEnd(context.Background(), "ROLLBACK")
if tx.conn.tracer != nil {
tx.conn.tracer.Rollback(tx.conn.id, err)
}
return err
}
func reserr(db sqliteh.DB, loc, query string, err error) error {
if err == nil {
return nil
}
e := &Error{
Code: sqliteh.Code(err.(sqliteh.ErrCode)),
Loc: loc,
Query: query,
}
// TODO(crawshaw): consider an API to expose this. sqlite.DebugErrMsg(db)?
if true {
e.Msg = db.ErrMsg()
}
return e
}
type stmt struct {
conn *conn
stmt sqliteh.Stmt
query string
persist bool // true if stmt is cached and lives beyond Close
bound bool // true if stmt has parameters bound
closed atomic.Bool // true after Close if persist==false
numInput int // filled on first NumInput only if persist==true
prepCtx context.Context // the context provided to prepare, for tracing
// filled on first step only if persist==true
colDeclTypes []colDeclType
colNames []string
}
func (s *stmt) reserr(loc string, err error) error { return reserr(s.conn.db, loc, s.query, err) }
func (s *stmt) NumInput() int {
if s.closed.Load() {
UsesAfterClose.Add("stmt.NumInput", 1)
return 0
}
if s.persist {
if s.numInput == -1 {
s.numInput = s.stmt.BindParameterCount()
}
return s.numInput
}
return s.stmt.BindParameterCount()
}
func (s *stmt) Close() error {
// Always set the 'closed' boolean, even for a persisted query; this is
// set from false -> true in prepare(), above.
if s.conn.closed.Load() {
UsesAfterClose.Add("Stmt.Close_conn", 1)
return nil
}
if !s.closed.CompareAndSwap(false, true) {
UsesAfterClose.Add("Stmt.Close", 1)
return nil
}
// We return this statement to the conn only if it's persistent, and
// only if there's not already a statement with the same query already
// cached there.
shouldPersist := s.persist
if shouldPersist {
if _, alreadyPersisted := s.conn.stmts[s.query]; alreadyPersisted {
shouldPersist = false
}
}
if shouldPersist {
err := s.reserr("Stmt.Close", s.resetAndClear())
if err == nil {
s.conn.stmts[s.query] = s
}
return err
}
return s.reserr("Stmt.Close", s.stmt.Finalize())
}
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) { panic("deprecated, unused") }
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) { panic("deprecated, unused") }
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
if s.closed.Load() {
UsesAfterClose.Add("stmt.ExecContext", 1)
return nil, ErrClosed
}
if err := s.resetAndClear(); err != nil {
return nil, s.reserr("Stmt.Exec(Reset)", err)
}
if err := s.bindAll(args); err != nil {
return nil, s.reserr("Stmt.Exec(Bind)", err)
}
if ctx.Value(queryCancelKey{}) != nil {
done := make(chan struct{})
pctx, pcancel := context.WithCancel(ctx)
db := s.stmt.DBHandle()
context.AfterFunc(pctx, func() {
defer close(done)
// Note: We respond to cancellation on the primary context (ctx) not
// the cleanup context (pctx).
if ctx.Err() != nil {
db.Interrupt()
}
})
// We must wait prior to returning to ensure a cancellation context (if
// present) has completed, so that a cancellation cannot outlast this
// request and fire during a later execution.
defer func() { pcancel(); <-done }()
}
row, lastInsertRowID, changes, duration, err := s.stmt.StepResult()
s.bound = false // StepResult resets the query
err = s.reserr("Stmt.Exec", err)
if s.conn.tracer != nil {
s.conn.tracer.Query(s.prepCtx, s.conn.id, s.query, duration, err)
}
if err != nil {
return nil, err
}
_ = row // TODO: return error if exec on query which returns rows?
return getStmtResult(lastInsertRowID, changes), nil
}
var (
stmtResultZeroRows = &stmtResult{}
stmtResultOneRow = &stmtResult{rowsAffected: 1}
)
func getStmtResult(lastInsertID int64, rowsAffected int64) *stmtResult {
// Some common cases to avoid allocs:
if lastInsertID == 0 {
switch rowsAffected {
case 0:
return stmtResultZeroRows
case 1:
return stmtResultOneRow
}
}
return &stmtResult{lastInsertID: lastInsertID, rowsAffected: rowsAffected}
}
type stmtResult struct {
lastInsertID int64
rowsAffected int64
}
func (res *stmtResult) LastInsertId() (int64, error) { return res.lastInsertID, nil }
func (res *stmtResult) RowsAffected() (int64, error) { return res.rowsAffected, nil }
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
if s.closed.Load() {
UsesAfterClose.Add("stmt.QueryContext", 1)
return nil, ErrClosed
}
if err := s.resetAndClear(); err != nil {
return nil, s.reserr("Stmt.Query(Reset)", err)
}
if err := s.bindAll(args); err != nil {
return nil, err
}
cancel := func() {}
var done chan struct{}
if ctx.Value(queryCancelKey{}) != nil {
done = make(chan struct{})
pctx, pcancel := context.WithCancel(ctx)
cancel = pcancel
db := s.stmt.DBHandle()
context.AfterFunc(pctx, func() {
defer close(done)
// Note: We respond to cancellation on the primary context (ctx) not
// the cleanup context (pctx).
if ctx.Err() != nil {
db.Interrupt()
}
})
// In this case we do not have an early exit, so we don't need to
// dissociate the cancellation handler: If the caller gets an error, it
// will explicitly trigger the cancellation and wait in (*rows).Close.
}
return &rows{stmt: s, cancel: cancel, done: done}, nil
}
func (s *stmt) resetAndClear() error {
if !s.bound {
return nil
}
s.bound = false
duration, err := s.stmt.ResetAndClear()
if s.conn.tracer != nil {
s.conn.tracer.Query(s.prepCtx, s.conn.id, s.query, duration, err)
}
return err
}
func (s *stmt) bindAll(args []driver.NamedValue) error {
if s.bound {
panic("sqlite: impossible state, query already running: " + s.query)
}
s.bound = true
if s.conn.tracer != nil {
s.stmt.StartTimer()
}
for _, arg := range args {
if err := s.bind(arg); err != nil {
return err
}
}
return nil
}
func (s *stmt) bind(arg driver.NamedValue) error {
// TODO(crawshaw): could use a union-ish data type for debugName
// to avoid the allocation.
var debugName any
if arg.Name == "" {
debugName = arg.Ordinal
} else {
debugName = arg.Name
index := s.stmt.BindParameterIndexSearch(arg.Name)
if index == 0 {
return &Error{
Code: sqliteh.SQLITE_MISUSE,
Loc: "Bind",
Query: s.query,
Msg: fmt.Sprintf("unknown parameter name %q", arg.Name),
}
}
arg.Ordinal = index
}
// Start with obvious types, including time.Time before TextMarshaler.
found, err := s.bindBasic(debugName, arg.Ordinal, arg.Value)
if err != nil {
return err
} else if found {
return nil
}
if m, _ := arg.Value.(encoding.TextMarshaler); m != nil {
b, err := m.MarshalText()
if err != nil {
// TODO: modify Error to carry an error so we can %w?
return &Error{
Code: sqliteh.SQLITE_MISUSE,
Loc: "Bind",
Query: s.query,
Msg: fmt.Sprintf("Bind:%v: cannot marshal %T: %v", debugName, arg.Value, err),
}
}
_, err = s.bindBasic(debugName, arg.Ordinal, b)
return err
}
// Look for named basic types or other convertible types.
val := reflect.ValueOf(arg.Value)
typ := reflect.TypeOf(arg.Value)
switch typ.Kind() {
case reflect.Bool:
b := int64(0)
if val.Bool() {
b = 1
}
_, err := s.bindBasic(debugName, arg.Ordinal, b)
return err
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
_, err := s.bindBasic(debugName, arg.Ordinal, val.Int())
return err
case reflect.Uint, reflect.Uint64:
return &Error{
Code: sqliteh.SQLITE_MISUSE,
Loc: "Bind",
Query: s.query,
Msg: fmt.Sprintf("Bind:%v: sqlite does not support uint64 (try a string or TextMarshaler)", debugName),
}
case reflect.Uint8, reflect.Uint16, reflect.Uint32:
_, err := s.bindBasic(debugName, arg.Ordinal, int64(val.Uint()))
return err
case reflect.Float32, reflect.Float64:
_, err := s.bindBasic(debugName, arg.Ordinal, val.Float())
return err
case reflect.String:
// TODO(crawshaw): decompose bindBasic somehow.
// But first: more tests that the errors make sense for each type.
_, err := s.bindBasic(debugName, arg.Ordinal, val.String())
return err
}
return &Error{
Code: sqliteh.SQLITE_MISUSE,
Loc: "Bind",
Query: s.query,
Msg: fmt.Sprintf("Bind:%v: unknown value type %T (try a string or TextMarshaler)", debugName, arg.Value),
}
}
func (s *stmt) bindBasic(debugName any, ordinal int, v any) (found bool, err error) {
defer func() {
if err != nil {
err = s.reserr(fmt.Sprintf("Bind:%v:%T", debugName, v), err)
}
}()
switch v := v.(type) {
case nil:
return true, s.stmt.BindNull(ordinal)
case string:
return true, s.stmt.BindText64(ordinal, v)
case int:
return true, s.stmt.BindInt64(ordinal, int64(v))
case int64:
return true, s.stmt.BindInt64(ordinal, v)
case float64:
return true, s.stmt.BindDouble(ordinal, v)
case []byte:
if len(v) == 0 {
return true, s.stmt.BindZeroBlob64(ordinal, 0)
} else {
return true, s.stmt.BindBlob64(ordinal, v)
}
case time.Time:
// Shortest of:
// YYYY-MM-DD HH:MM
// YYYY-MM-DD HH:MM:SS
// YYYY-MM-DD HH:MM:SS.SSS
str := v.Format(TimeFormat)
str = strings.TrimSuffix(str, "-0000")
str = strings.TrimSuffix(str, ".000")
str = strings.TrimSuffix(str, ":00")
return true, s.stmt.BindText64(ordinal, str)
default:
return false, nil
}
}
// colDeclType is whether and how the declared SQLite column type should
// map to any special handling (as a date, or as a boolean, etc).
type colDeclType byte
const (
declTypeUnknown colDeclType = iota
declTypeDateOrTime
declTypeBoolean
)
func colDeclTypeFromString(s string) colDeclType {
if strings.EqualFold(s, "DATETIME") || strings.EqualFold(s, "DATE") {
return declTypeDateOrTime
}
if strings.EqualFold(s, "BOOLEAN") {
return declTypeBoolean
}
return declTypeUnknown
}
type rows struct {
stmt *stmt
closed bool
cancel context.CancelFunc // call when query ends
done chan struct{} // either nil, or closed when cancellation is done
// colType is the column types for Step to fill on each row. We only use 23
// as it packs well with the closed bool byte above (24 bytes total, same as
// a slice) and it's uncommon for queries to select so many columns. But if
// they do, we still work: we just query the column type via cgo on each
// row. So a bit slower, but fine.
colType [23]sqliteh.ColumnType
colNames []string // filled on call to Columns
// Filled on first call to Next.
colDeclTypes []colDeclType
}
func (r *rows) Columns() []string {
if r.closed {
panic("Columns called after Rows was closed")
}
if r.stmt.closed.Load() {
UsesAfterClose.Add("rows.Columns", 1)
return nil
}
if r.colNames == nil {
if r.stmt.colNames != nil {
r.colNames = r.stmt.colNames
} else {
r.colNames = make([]string, r.stmt.stmt.ColumnCount())
for i := range r.colNames {
r.colNames[i] = r.stmt.stmt.ColumnName(i)
}
if r.stmt.persist {
r.stmt.colNames = r.colNames
}
}
}
return append([]string{}, r.colNames...)
}
func (r *rows) Close() error {
if r.closed {
return errors.New("sqlite rows result already closed")
}
if r.stmt.closed.Load() {
UsesAfterClose.Add("rows.Close", 1)
return ErrClosed
}
r.closed = true
r.cancel()
if r.done != nil {
<-r.done
}
if err := r.stmt.resetAndClear(); err != nil {
return r.stmt.reserr("Rows.Close(Reset)", err)
}
return nil
}
func (r *rows) Next(dest []driver.Value) error {
if r.closed {
return errors.New("sqlite rows result already closed")
}
if r.stmt.closed.Load() {
UsesAfterClose.Add("rows.Next", 1)
return ErrClosed
}
hasRow, err := r.stmt.stmt.Step(r.colType[:])
if err != nil {
return r.stmt.reserr("Rows.Next", err)
}
if !hasRow {
return io.EOF
}
if r.colDeclTypes == nil {
r.colDeclTypes = r.stmt.colDeclTypes
}
if r.colDeclTypes == nil {
colCount := r.stmt.stmt.ColumnCount()
r.colDeclTypes = make([]colDeclType, colCount)
for i := range r.colDeclTypes {
r.colDeclTypes[i] = colDeclTypeFromString(r.stmt.stmt.ColumnDeclType(i))
}
if r.stmt.persist {
r.stmt.colDeclTypes = r.colDeclTypes
}
}
for i := range dest {
var colType sqliteh.ColumnType
if i < len(r.colType) {
// Common case, for the first couple dozen columns.
colType = r.colType[i]
} else {
// If it's a really wide query, then call into
// cgo for columns past the length of
// r.colType.
colType = r.stmt.stmt.ColumnType(i)
}
if r.colDeclTypes[i] == declTypeDateOrTime {
switch colType {
case sqliteh.SQLITE_INTEGER:
v := r.stmt.stmt.ColumnInt64(i)
dest[i] = time.Unix(v, 0)
case sqliteh.SQLITE_FLOAT:
dest[i] = r.stmt.stmt.ColumnDouble(i)
// TODO: treat as time?
case sqliteh.SQLITE_TEXT:
v := r.stmt.stmt.ColumnText(i)
format := TimeFormat
if len(format) > len(v) {
format = strings.TrimSuffix(format, "-0700")
}
if len(format) > len(v) {
format = strings.TrimSuffix(format, ".000")
}
if len(format) > len(v) {
format = strings.TrimSuffix(format, ":05")
}
t, err := time.Parse(format, v)
if err != nil {
return fmt.Errorf("cannot parse time from column %d: %v", i, err)
}
dest[i] = t
}
continue
}
switch colType {
case sqliteh.SQLITE_INTEGER:
val := r.stmt.stmt.ColumnInt64(i)
if r.colDeclTypes[i] == declTypeBoolean {
dest[i] = val > 0
} else {
dest[i] = val
}
case sqliteh.SQLITE_FLOAT:
dest[i] = r.stmt.stmt.ColumnDouble(i)
case sqliteh.SQLITE_BLOB, sqliteh.SQLITE_TEXT:
dest[i] = r.stmt.stmt.ColumnBlob(i)
case sqliteh.SQLITE_NULL:
dest[i] = nil
}
}
return nil
}
// Error is an error produced by SQLite.
type Error struct {
Code sqliteh.Code // SQLite extended error code (SQLITE_OK is an invalid value)
Loc string // method name that generated the error
Query string // original SQL query text
Msg string // value of sqlite3_errmsg, set sqlite.ErrMsg = true
}
func (err Error) Error() string {
b := new(strings.Builder)
b.WriteString("sqlite")
if err.Loc != "" {
b.WriteByte('.')
b.WriteString(err.Loc)
}
b.WriteString(": ")
b.WriteString(err.Code.String())
if err.Msg != "" {
b.WriteString(": ")
b.WriteString(err.Msg)
}
if err.Query != "" {
b.WriteString(" (")
b.WriteString(err.Query)
b.WriteByte(')')
}
return b.String()
}
// SQLConn is a database/sql.Conn.
// (We cannot create a circular package dependency here.)
type SQLConn interface {
Raw(func(driverConn any) error) error
}
// ExecScript executes a set of SQL queries on an sql.Conn.
// It stops on the first error.
// It is recommended you wrap your script in a BEGIN; ... COMMIT; block.
//
// Usage:
//
// c, err := db.Conn(ctx)
// if err != nil {
// // handle err
// }
// if err := sqlite.ExecScript(c, queries); err != nil {
// // handle err
// }
// c.Close() // return sql.Conn to pool
func ExecScript(sqlconn SQLConn, queries string) error {
return sqlconn.Raw(func(driverConn any) error {
c, ok := driverConn.(*conn)
if !ok {
return fmt.Errorf("sqlite.ExecScript: sql.Conn is not the sqlite driver: %T", driverConn)
}
for {
queries = strings.TrimSpace(queries)
if queries == "" {
return nil
}
cstmt, rem, err := c.db.Prepare(queries, 0)
if err != nil {
return reserr(c.db, "ExecScript", queries, err)
}
queries = rem
_, err = cstmt.Step(nil)
cstmt.Finalize()
if err != nil {
// TODO(crawshaw): consider checking sqlite3_txn_state
// here and issuing a rollback, incase this script was:
// BEGIN; BAD-SQL; COMMIT;
// So we don't leave the connection open.
return reserr(c.db, "ExecScript", queries, err)
}
}
})
}
// BusyTimeout calls sqlite3_busy_timeout on the underlying connection.
func BusyTimeout(sqlconn SQLConn, d time.Duration) error {
return sqlconn.Raw(func(driverConn any) error {
c, ok := driverConn.(*conn)
if !ok {
return fmt.Errorf("sqlite.BusyTimeout: sql.Conn is not the sqlite driver: %T", driverConn)
}
c.db.BusyTimeout(d)
return nil
})
}
// SetWALHook calls sqlite3_wal_hook.