-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconn.go
More file actions
1279 lines (1126 loc) · 33.5 KB
/
conn.go
File metadata and controls
1279 lines (1126 loc) · 33.5 KB
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 2025 The Sqlite 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 // import "modernc.org/sqlite"
import (
"context"
"database/sql/driver"
"fmt"
"strconv"
"strings"
"sync"
"time"
"unsafe"
"modernc.org/libc"
"modernc.org/libc/sys/types"
sqlite3 "modernc.org/sqlite/lib"
)
type conn struct {
db uintptr // *sqlite3.Xsqlite3
tls *libc.TLS
// Context handling can cause conn.Close and conn.interrupt to be invoked
// concurrently.
sync.Mutex
writeTimeFormat string
beginMode string
loc *time.Location
intToTime bool
textToTime bool
integerTimeFormat string
// inMemory records whether the underlying database resides only in
// memory (DSN like ":memory:", "file::memory:", or a shared-cache
// memory URI). For such databases, dropping the *only* connection
// also drops the database itself, so IsValid must not discard the
// connection just because an in-flight query was interrupted.
// See #196.
inMemory bool
}
func newConn(dsn string) (*conn, error) {
var query, vfsName string
// Parse the query parameters from the dsn and them from the dsn if not prefixed by file:
// https://github.com/mattn/go-sqlite3/blob/3392062c729d77820afc1f5cae3427f0de39e954/sqlite3.go#L1046
// https://github.com/mattn/go-sqlite3/blob/3392062c729d77820afc1f5cae3427f0de39e954/sqlite3.go#L1383
pos := strings.IndexRune(dsn, '?')
if pos >= 1 {
query = dsn[pos+1:]
var err error
vfsName, err = getVFSName(query)
if err != nil {
return nil, err
}
if !strings.HasPrefix(dsn, "file:") {
dsn = dsn[:pos]
}
}
c := &conn{tls: libc.NewTLS()}
db, err := c.openV2(
dsn,
vfsName,
sqlite3.SQLITE_OPEN_READWRITE|sqlite3.SQLITE_OPEN_CREATE|
sqlite3.SQLITE_OPEN_FULLMUTEX|
sqlite3.SQLITE_OPEN_URI,
)
if err != nil {
c.tls.Close()
return nil, err
}
c.db = db
if err = c.extendedResultCodes(true); err != nil {
c.Close()
return nil, err
}
// sqlite3_db_filename returns an empty string for databases that
// are not backed by a file (":memory:", "file::memory:", shared-cache
// memory URIs, temporary databases). Cache the answer once so we
// don't have to re-derive it on every IsValid call.
zMain, mainErr := libc.CString("main")
if mainErr != nil {
c.Close()
return nil, mainErr
}
defer libc.Xfree(c.tls, zMain)
c.inMemory = libc.GoString(sqlite3.Xsqlite3_db_filename(c.tls, c.db, zMain)) == ""
if err = applyQueryParams(c, query); err != nil {
c.Close()
return nil, err
}
return c, nil
}
// parseTime attempts to parse s as a time encoding. If hintIdx is a valid
// index into parseTimeFormats, that format is tried before the rest of the
// list; otherwise the search runs in declaration order. The returned int is
// the index of the format that matched, or -1 if the parseTimeString
// (t.String()) branch matched or all formats failed. Callers that scan many
// rows of a same-format column can feed the previous match back as hintIdx
// to skip the redundant time.Parse attempts that would otherwise run for
// every row.
//
// Return value contract is preserved: (parsed-value, ok). On failure the
// value is the original input string and ok is false.
func (c *conn) parseTime(s string, hintIdx int) (interface{}, bool, int) {
if v, ok := c.parseTimeString(s, strings.Index(s, "m=")); ok {
return v, true, -1
}
ts, hadZ := strings.CutSuffix(s, "Z")
tryFormat := func(f string) (time.Time, error) {
if c.loc != nil && !hadZ {
return time.ParseInLocation(f, ts, c.loc)
}
return time.Parse(f, ts)
}
// Try the caller's hint first, if any.
if hintIdx >= 0 && hintIdx < len(parseTimeFormats) {
if t, err := tryFormat(parseTimeFormats[hintIdx]); err == nil {
return c.applyTimezone(t), true, hintIdx
}
}
// Sequential fallthrough, skipping the hint we already tried.
for i, f := range parseTimeFormats {
if i == hintIdx {
continue
}
if t, err := tryFormat(f); err == nil {
return c.applyTimezone(t), true, i
}
}
return s, false, -1
}
// Attempt to parse s as a time string produced by t.String(). If x > 0 it's
// the index of substring "m=" within s. Return (s, false) if s is
// not recognized as a valid time encoding.
// This intentionally uses time.Parse, not time.ParseInLocation,
// because the format already contains timezone information (-0700 MST).
func (c *conn) parseTimeString(s0 string, x int) (interface{}, bool) {
s := s0
if x > 0 {
s = s[:x] // "2006-01-02 15:04:05.999999999 -0700 MST m=+9999" -> "2006-01-02 15:04:05.999999999 -0700 MST "
}
s = strings.TrimSpace(s)
if t, err := time.Parse("2006-01-02 15:04:05.999999999 -0700 MST", s); err == nil {
return c.applyTimezone(t), true
}
return s0, false
}
func (c *conn) applyTimezone(t time.Time) time.Time {
if c.loc == nil {
return t
}
return t.In(c.loc)
}
// writeTimeFormats are the names and formats supported
// by the `_time_format` DSN query param.
var writeTimeFormats = map[string]string{
"sqlite": parseTimeFormats[0],
"datetime": "2006-01-02 15:04:05",
}
func (c *conn) formatTime(t time.Time) string {
t = c.applyTimezone(t)
// Before configurable write time formats were supported,
// time.Time.String was used. Maintain that default to
// keep existing driver users formatting times the same.
if c.writeTimeFormat == "" {
return t.String()
}
return t.Format(c.writeTimeFormat)
}
// C documentation
//
// const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
func (c *conn) columnBlob(pstmt uintptr, iCol int) (v []byte, err error) {
p := sqlite3.Xsqlite3_column_blob(c.tls, pstmt, int32(iCol))
len, err := c.columnBytes(pstmt, iCol)
if err != nil {
return nil, err
}
if p == 0 || len == 0 {
return nil, nil
}
v = make([]byte, len)
copy(v, (*libc.RawMem)(unsafe.Pointer(p))[:len:len])
return v, nil
}
// C documentation
//
// int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
func (c *conn) columnBytes(pstmt uintptr, iCol int) (_ int, err error) {
v := sqlite3.Xsqlite3_column_bytes(c.tls, pstmt, int32(iCol))
return int(v), nil
}
// C documentation
//
// const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
func (c *conn) columnText(pstmt uintptr, iCol int) (v string, err error) {
p := sqlite3.Xsqlite3_column_text(c.tls, pstmt, int32(iCol))
len, err := c.columnBytes(pstmt, iCol)
if err != nil {
return "", err
}
if p == 0 || len == 0 {
return "", nil
}
// Copy the SQLite-owned UTF-8 bytes into a fresh Go-owned buffer, then
// reinterpret that buffer as a string without a second copy. The default
// string(b) conversion calls runtime.slicebytetostring, which mallocs a
// new backing array and memcpys b into it because the compiler must
// assume the caller could mutate b. Here b is local to this function and
// is never written to again after the copy above, so it is safe to view
// it as the string's backing memory directly. The string is immutable
// from Go's perspective, b becomes unreachable as a []byte after we
// return, and the GC keeps the underlying array alive for as long as the
// returned string is reachable.
b := make([]byte, len)
copy(b, (*libc.RawMem)(unsafe.Pointer(p))[:len:len])
return unsafe.String(unsafe.SliceData(b), len), nil
}
// C documentation
//
// double sqlite3_column_double(sqlite3_stmt*, int iCol);
func (c *conn) columnDouble(pstmt uintptr, iCol int) (v float64, err error) {
v = sqlite3.Xsqlite3_column_double(c.tls, pstmt, int32(iCol))
return v, nil
}
// C documentation
//
// sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
func (c *conn) columnInt64(pstmt uintptr, iCol int) (v int64, err error) {
v = sqlite3.Xsqlite3_column_int64(c.tls, pstmt, int32(iCol))
return v, nil
}
// C documentation
//
// int sqlite3_column_type(sqlite3_stmt*, int iCol);
func (c *conn) columnType(pstmt uintptr, iCol int) (_ int, err error) {
v := sqlite3.Xsqlite3_column_type(c.tls, pstmt, int32(iCol))
return int(v), nil
}
// C documentation
//
// const char *sqlite3_column_decltype(sqlite3_stmt*,int);
func (c *conn) columnDeclType(pstmt uintptr, iCol int) string {
return libc.GoString(sqlite3.Xsqlite3_column_decltype(c.tls, pstmt, int32(iCol)))
}
// C documentation
//
// const char *sqlite3_column_name(sqlite3_stmt*, int N);
func (c *conn) columnName(pstmt uintptr, n int) (string, error) {
p := sqlite3.Xsqlite3_column_name(c.tls, pstmt, int32(n))
return libc.GoString(p), nil
}
// ColumnInfo returns column information for query.
// It does not execute query.
//
// For output columns that are expressions, function calls, or constants —
// or otherwise do not resolve to a single column — the DatabaseName,
// TableName, and OriginName fields of the corresponding ColumnInfo are
// empty, per the sqlite3 contract.
//
// Sample usage:
//
// err := conn.Raw(func(driverConn any) error {
// ci, ok := driverConn.(interface{ ColumnInfo(query string) ([]sqlite.ColumnInfo, error) })
// if !ok {
// return fmt.Errorf("driver does not support ColumnInfo")
// }
// info, err := ci.ColumnInfo(query)
// if err != nil {
// return err
// }
// // use info
// return nil
// })
func (c *conn) ColumnInfo(query string) (_ []ColumnInfo, err error) {
p, err := libc.CString(query)
if err != nil {
return nil, err
}
defer c.free(p)
psql := p
pstmt, err := c.prepareV2(&psql)
if err != nil {
return nil, err
}
if pstmt == 0 {
// Empty or comment-only query: no columns to describe.
return nil, nil
}
defer func() {
if e := c.finalize(pstmt); err == nil {
err = e
}
}()
n, err := c.columnCount(pstmt)
if err != nil {
return nil, err
}
info := make([]ColumnInfo, n)
for i := range n {
name, err := c.columnName(pstmt, i)
if err != nil {
return nil, err
}
info[i] = ColumnInfo{
Name: name,
DeclType: c.columnDeclType(pstmt, i),
DatabaseName: libc.GoString(sqlite3.Xsqlite3_column_database_name(c.tls, pstmt, int32(i))),
TableName: libc.GoString(sqlite3.Xsqlite3_column_table_name(c.tls, pstmt, int32(i))),
OriginName: libc.GoString(sqlite3.Xsqlite3_column_origin_name(c.tls, pstmt, int32(i))),
}
}
return info, nil
}
// C documentation
//
// int sqlite3_column_count(sqlite3_stmt *pStmt);
func (c *conn) columnCount(pstmt uintptr) (_ int, err error) {
v := sqlite3.Xsqlite3_column_count(c.tls, pstmt)
return int(v), nil
}
// C documentation
//
// sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
func (c *conn) lastInsertRowID() (v int64, _ error) {
return sqlite3.Xsqlite3_last_insert_rowid(c.tls, c.db), nil
}
// C documentation
//
// int sqlite3_changes(sqlite3*);
func (c *conn) changes() (int, error) {
v := sqlite3.Xsqlite3_changes(c.tls, c.db)
return int(v), nil
}
// C documentation
//
// int sqlite3_step(sqlite3_stmt*);
func (c *conn) step(pstmt uintptr) (int, error) {
for {
switch rc := sqlite3.Xsqlite3_step(c.tls, pstmt); rc {
case sqliteLockedSharedcache:
if err := c.retry(pstmt); err != nil {
return sqlite3.SQLITE_LOCKED, err
}
case
sqlite3.SQLITE_DONE,
sqlite3.SQLITE_ROW:
return int(rc), nil
default:
return int(rc), c.errstr(rc)
}
}
}
func (c *conn) retry(pstmt uintptr) error {
mu := mutexAlloc(c.tls)
(*mutex)(unsafe.Pointer(mu)).Lock()
rc := sqlite3.Xsqlite3_unlock_notify(
c.tls,
c.db,
*(*uintptr)(unsafe.Pointer(&struct {
f func(*libc.TLS, uintptr, int32)
}{unlockNotify})),
mu,
)
if rc == sqlite3.SQLITE_LOCKED { // Deadlock, see https://www.sqlite.org/c3ref/unlock_notify.html
(*mutex)(unsafe.Pointer(mu)).Unlock()
mutexFree(c.tls, mu)
return c.errstr(rc)
}
(*mutex)(unsafe.Pointer(mu)).Lock()
(*mutex)(unsafe.Pointer(mu)).Unlock()
mutexFree(c.tls, mu)
if pstmt != 0 {
sqlite3.Xsqlite3_reset(c.tls, pstmt)
}
return nil
}
func (c *conn) bind(pstmt uintptr, n int, args []driver.NamedValue) (allocs []uintptr, err error) {
defer func() {
if err == nil {
return
}
c.freeAllocs(allocs)
allocs = nil
}()
for i := 1; i <= n; i++ {
name, err := c.bindParameterName(pstmt, i)
if err != nil {
return allocs, err
}
var found bool
var v driver.NamedValue
for _, v = range args {
if name != "" {
// For ?NNN and $NNN params, match if NNN == v.Ordinal.
//
// Supporting this for $NNN is a special case that makes eg
// `select $1, $2, $3 ...` work without needing to use
// sql.Named.
if (name[0] == '?' || name[0] == '$') && name[1:] == strconv.Itoa(v.Ordinal) {
found = true
break
}
// sqlite supports '$', '@' and ':' prefixes for string
// identifiers and '?' for numeric, so we cannot
// combine different prefixes with the same name
// because `database/sql` requires variable names
// to start with a letter
if name[1:] == v.Name[:] {
found = true
break
}
} else {
if v.Ordinal == i {
found = true
break
}
}
}
if !found {
if name != "" {
return allocs, fmt.Errorf("missing named argument %q", name[1:])
}
return allocs, fmt.Errorf("missing argument with index %d", i)
}
var p uintptr
switch x := v.Value.(type) {
case int64:
if err := c.bindInt64(pstmt, i, x); err != nil {
return allocs, err
}
case float64:
if err := c.bindDouble(pstmt, i, x); err != nil {
return allocs, err
}
case bool:
v := 0
if x {
v = 1
}
if err := c.bindInt(pstmt, i, v); err != nil {
return allocs, err
}
case []byte:
if p, err = c.bindBlob(pstmt, i, x); err != nil {
return allocs, err
}
case string:
if p, err = c.bindText(pstmt, i, x); err != nil {
return allocs, err
}
case time.Time:
switch c.integerTimeFormat {
case "unix":
if err := c.bindInt64(pstmt, i, x.Unix()); err != nil {
return allocs, err
}
case "unix_milli":
if err := c.bindInt64(pstmt, i, x.UnixMilli()); err != nil {
return allocs, err
}
case "unix_micro":
if err := c.bindInt64(pstmt, i, x.UnixMicro()); err != nil {
return allocs, err
}
case "unix_nano":
if err := c.bindInt64(pstmt, i, x.UnixNano()); err != nil {
return allocs, err
}
default:
if p, err = c.bindText(pstmt, i, c.formatTime(x)); err != nil {
return allocs, err
}
}
case nil:
if p, err = c.bindNull(pstmt, i); err != nil {
return allocs, err
}
default:
return allocs, fmt.Errorf("sqlite: invalid driver.Value type %T", x)
}
if p != 0 {
allocs = append(allocs, p)
}
}
return allocs, nil
}
// C documentation
//
// int sqlite3_bind_null(sqlite3_stmt*, int);
func (c *conn) bindNull(pstmt uintptr, idx1 int) (uintptr, error) {
if rc := sqlite3.Xsqlite3_bind_null(c.tls, pstmt, int32(idx1)); rc != sqlite3.SQLITE_OK {
return 0, c.errstr(rc)
}
return 0, nil
}
// C documentation
//
// int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
func (c *conn) bindText(pstmt uintptr, idx1 int, value string) (uintptr, error) {
p, err := libc.CString(value)
if err != nil {
return 0, err
}
if rc := sqlite3.Xsqlite3_bind_text(c.tls, pstmt, int32(idx1), p, int32(len(value)), 0); rc != sqlite3.SQLITE_OK {
c.free(p)
return 0, c.errstr(rc)
}
return p, nil
}
// C documentation
//
// int sqlite3_bind_int(sqlite3_stmt*, int, int);
func (c *conn) bindInt(pstmt uintptr, idx1, value int) (err error) {
if rc := sqlite3.Xsqlite3_bind_int(c.tls, pstmt, int32(idx1), int32(value)); rc != sqlite3.SQLITE_OK {
return c.errstr(rc)
}
return nil
}
// C documentation
//
// int sqlite3_bind_double(sqlite3_stmt*, int, double);
func (c *conn) bindDouble(pstmt uintptr, idx1 int, value float64) (err error) {
if rc := sqlite3.Xsqlite3_bind_double(c.tls, pstmt, int32(idx1), value); rc != 0 {
return c.errstr(rc)
}
return nil
}
// C documentation
//
// int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
func (c *conn) bindInt64(pstmt uintptr, idx1 int, value int64) (err error) {
if rc := sqlite3.Xsqlite3_bind_int64(c.tls, pstmt, int32(idx1), value); rc != sqlite3.SQLITE_OK {
return c.errstr(rc)
}
return nil
}
// C documentation
//
// const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
func (c *conn) bindParameterName(pstmt uintptr, i int) (string, error) {
p := sqlite3.Xsqlite3_bind_parameter_name(c.tls, pstmt, int32(i))
return libc.GoString(p), nil
}
// C documentation
//
// int sqlite3_bind_parameter_count(sqlite3_stmt*);
func (c *conn) bindParameterCount(pstmt uintptr) (_ int, err error) {
r := sqlite3.Xsqlite3_bind_parameter_count(c.tls, pstmt)
return int(r), nil
}
// C documentation
//
// int sqlite3_finalize(sqlite3_stmt *pStmt);
func (c *conn) finalize(pstmt uintptr) error {
if rc := sqlite3.Xsqlite3_finalize(c.tls, pstmt); rc != sqlite3.SQLITE_OK {
return c.errstr(rc)
}
return nil
}
// C documentation
//
// int sqlite3_prepare_v2(
// sqlite3 *db, /* Database handle */
// const char *zSql, /* SQL statement, UTF-8 encoded */
// int nByte, /* Maximum length of zSql in bytes. */
// sqlite3_stmt **ppStmt, /* OUT: Statement handle */
// const char **pzTail /* OUT: Pointer to unused portion of zSql */
// );
func (c *conn) prepareV2(zSQL *uintptr) (pstmt uintptr, err error) {
var ppstmt, pptail uintptr
defer func() {
c.free(ppstmt)
c.free(pptail)
}()
if ppstmt, err = c.malloc(int(ptrSize)); err != nil {
return 0, err
}
if pptail, err = c.malloc(int(ptrSize)); err != nil {
return 0, err
}
for {
// https://gitlab.com/cznic/sqlite/-/issues/236
// trc("Xsqlite3_prepare_v2(`%s`)", libc.GoString(*zSQL))
switch rc := sqlite3.Xsqlite3_prepare_v2(c.tls, c.db, *zSQL, -1, ppstmt, pptail); rc {
case sqlite3.SQLITE_OK:
*zSQL = *(*uintptr)(unsafe.Pointer(pptail))
return *(*uintptr)(unsafe.Pointer(ppstmt)), nil
case sqliteLockedSharedcache:
if err := c.retry(0); err != nil {
return 0, err
}
default:
return 0, c.errstr(rc)
}
}
}
// C documentation
//
// void sqlite3_interrupt(sqlite3*);
func (c *conn) interrupt(pdb uintptr) (err error) {
c.Lock() // Defend against race with .Close invoked by context handling.
defer c.Unlock()
if c.tls != nil {
sqlite3.Xsqlite3_interrupt(c.tls, pdb)
}
return nil
}
// C documentation
//
// int sqlite3_extended_result_codes(sqlite3*, int onoff);
func (c *conn) extendedResultCodes(on bool) error {
if rc := sqlite3.Xsqlite3_extended_result_codes(c.tls, c.db, libc.Bool32(on)); rc != sqlite3.SQLITE_OK {
return c.errstr(rc)
}
return nil
}
// C documentation
//
// int sqlite3_open_v2(
// const char *filename, /* Database filename (UTF-8) */
// sqlite3 **ppDb, /* OUT: SQLite db handle */
// int flags, /* Flags */
// const char *zVfs /* Name of VFS module to use */
// );
func (c *conn) openV2(name, vfsName string, flags int32) (uintptr, error) {
var p, s, vfs uintptr
defer func() {
if p != 0 {
c.free(p)
}
if s != 0 {
c.free(s)
}
if vfs != 0 {
c.free(vfs)
}
}()
p, err := c.malloc(int(ptrSize))
if err != nil {
return 0, err
}
if s, err = libc.CString(name); err != nil {
return 0, err
}
if vfsName != "" {
if vfs, err = libc.CString(vfsName); err != nil {
return 0, err
}
}
if rc := sqlite3.Xsqlite3_open_v2(c.tls, s, p, flags, vfs); rc != sqlite3.SQLITE_OK {
dbh := *(*uintptr)(unsafe.Pointer(p))
// Per SQLite docs, sqlite3_open_v2 may allocate a handle even on
// failure. The error message is stored on that handle, and it must
// be closed to avoid leaking resources.
var err error
if dbh != 0 {
err = errstrForDB(c.tls, rc, dbh)
sqlite3.Xsqlite3_close_v2(c.tls, dbh)
} else {
err = c.errstr(rc)
}
return 0, err
}
return *(*uintptr)(unsafe.Pointer(p)), nil
}
func (c *conn) malloc(n int) (uintptr, error) {
if p := libc.Xmalloc(c.tls, types.Size_t(n)); p != 0 || n == 0 {
return p, nil
}
return 0, fmt.Errorf("sqlite: cannot allocate %d bytes of memory", n)
}
func (c *conn) free(p uintptr) {
if p != 0 {
libc.Xfree(c.tls, p)
}
}
func (c *conn) freeAllocs(allocs []uintptr) {
for _, v := range allocs {
c.free(v)
}
}
// C documentation
//
// const char *sqlite3_errstr(int);
func (c *conn) errstr(rc int32) error {
return errstrForDB(c.tls, rc, c.db)
}
func errstrForDB(tls *libc.TLS, rc int32, db uintptr) error {
pStr := sqlite3.Xsqlite3_errstr(tls, rc)
str := libc.GoString(pStr)
var s string
if rc == sqlite3.SQLITE_BUSY {
s = " (SQLITE_BUSY)"
}
if db == 0 {
return &Error{msg: fmt.Sprintf("%s (%v)%s", str, rc, s), code: int(rc)}
}
pMsg := sqlite3.Xsqlite3_errmsg(tls, db)
switch msg := libc.GoString(pMsg); {
case msg == str:
return &Error{msg: fmt.Sprintf("%s (%v)%s", str, rc, s), code: int(rc)}
default:
return &Error{msg: fmt.Sprintf("%s: %s (%v)%s", str, msg, rc, s), code: int(rc)}
}
}
// Begin starts a transaction.
//
// Deprecated: Drivers should implement ConnBeginTx instead (or additionally).
func (c *conn) Begin() (dt driver.Tx, err error) {
if dmesgs {
defer func() {
dmesg("conn %p: (driver.Tx %p, err %v)", c, dt, err)
}()
}
return c.begin(context.Background(), driver.TxOptions{})
}
func (c *conn) begin(ctx context.Context, opts driver.TxOptions) (t driver.Tx, err error) {
return newTx(ctx, c, opts)
}
// Close invalidates and potentially stops any current prepared statements and
// transactions, marking this connection as no longer in use.
//
// Because the sql package maintains a free pool of connections and only calls
// Close when there's a surplus of idle connections, it shouldn't be necessary
// for drivers to do their own connection caching.
func (c *conn) Close() (err error) {
if dmesgs {
defer func() {
dmesg("conn %p: err %v", c, err)
}()
}
c.Lock() // Defend against race with .interrupt invoked by context handling.
defer c.Unlock()
if c.db != 0 {
if err := c.closeV2(c.db); err != nil {
return err
}
c.db = 0
}
if c.tls != nil {
c.tls.Close()
c.tls = nil
}
return nil
}
// C documentation
//
// int sqlite3_close_v2(sqlite3*);
func (c *conn) closeV2(db uintptr) error {
if rc := sqlite3.Xsqlite3_close_v2(c.tls, db); rc != sqlite3.SQLITE_OK {
return c.errstr(rc)
}
return nil
}
// ResetSession is called prior to executing a query on the connection if the
// connection has been used before. If the driver returns ErrBadConn the
// connection is discarded.
func (c *conn) ResetSession(ctx context.Context) error {
if !c.usable() {
return driver.ErrBadConn
}
return nil
}
// IsValid is called prior to placing the connection into the connection pool.
// The connection will be discarded if false is returned.
func (c *conn) IsValid() bool {
return c.usable()
}
func (c *conn) usable() bool {
if c.db == 0 {
return false
}
// For in-memory databases the connection is the database: discarding
// it because the previous query was interrupted destroys all the data.
// Treat an interrupted in-memory connection as still valid so that
// database/sql returns it to the pool instead of dropping it. See #196
// (regressed by the fix for #198 which added the is_interrupted check).
if c.inMemory {
return true
}
return sqlite3.Xsqlite3_is_interrupted(c.tls, c.db) == 0
}
type userDefinedFunction struct {
zFuncName uintptr
nArg int32
eTextRep int32
pApp uintptr
scalar bool
freeOnce sync.Once
}
func (c *conn) createFunctionInternal(fun *userDefinedFunction) error {
var rc int32
if fun.scalar {
rc = sqlite3.Xsqlite3_create_function(
c.tls,
c.db,
fun.zFuncName,
fun.nArg,
fun.eTextRep,
fun.pApp,
cFuncPointer(funcTrampoline),
0,
0,
)
} else {
rc = sqlite3.Xsqlite3_create_window_function(
c.tls,
c.db,
fun.zFuncName,
fun.nArg,
fun.eTextRep,
fun.pApp,
cFuncPointer(stepTrampoline),
cFuncPointer(finalTrampoline),
cFuncPointer(valueTrampoline),
cFuncPointer(inverseTrampoline),
0,
)
}
if rc != sqlite3.SQLITE_OK {
return c.errstr(rc)
}
return nil
}
func (c *conn) createCollationInternal(coll *collation) error {
rc := sqlite3.Xsqlite3_create_collation_v2(
c.tls,
c.db,
coll.zName,
coll.enc,
coll.pApp,
cFuncPointer(collationTrampoline),
0,
)
if rc != sqlite3.SQLITE_OK {
return c.errstr(rc)
}
return nil
}
// Execer is an optional interface that may be implemented by a Conn.
//
// If a Conn does not implement Execer, the sql package's DB.Exec will first
// prepare a query, execute the statement, and then close the statement.
//
// Exec may return ErrSkip.
//
// Deprecated: Drivers should implement ExecerContext instead.
func (c *conn) Exec(query string, args []driver.Value) (dr driver.Result, err error) {
if dmesgs {
defer func() {
dmesg("conn %p, query %q, args %v: (driver.Result %p, err %v)", c, query, args, dr, err)
}()
}
return c.exec(context.Background(), query, toNamedValues(args))
}
func (c *conn) exec(ctx context.Context, query string, args []driver.NamedValue) (r driver.Result, err error) {
s, err := c.prepare(ctx, query)
if err != nil {
return nil, err
}
defer func() {
if err2 := s.Close(); err2 != nil && err == nil {
err = err2
}
}()
return s.(*stmt).exec(ctx, args)
}
// Prepare returns a prepared statement, bound to this connection.
func (c *conn) Prepare(query string) (ds driver.Stmt, err error) {
if dmesgs {
defer func() {
dmesg("conn %p, query %q: (driver.Stmt %p, err %v)", c, query, ds, err)
}()
}
return c.prepare(context.Background(), query)
}
func (c *conn) prepare(ctx context.Context, query string) (s driver.Stmt, err error) {
//TODO use ctx
return newStmt(c, query)
}
// Queryer is an optional interface that may be implemented by a Conn.
//