forked from cznic/sqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite.go
1086 lines (923 loc) · 24.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 2017 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 (
"bytes"
"database/sql"
"database/sql/driver"
"fmt"
"io"
"math"
"os"
"runtime"
"sync"
"time"
"unsafe"
"github.com/cznic/ccgo/crt"
"github.com/cznic/sqlite/internal/bin"
"golang.org/x/net/context"
)
var (
_ driver.Conn = (*conn)(nil)
_ driver.Driver = (*Driver)(nil)
_ driver.Execer = (*conn)(nil)
_ driver.Queryer = (*conn)(nil)
_ driver.Result = (*result)(nil)
_ driver.Rows = (*rows)(nil)
_ driver.Stmt = (*stmt)(nil)
_ driver.Tx = (*tx)(nil)
)
const (
driverName = "sqlite"
ptrSize = 1 << (^uintptr(0)>>32&1 + ^uintptr(0)>>16&1 + ^uintptr(0)>>8&1 + 3) / 8
)
func init() {
tls := crt.NewTLS()
crt.X__register_stdfiles(tls, bin.Xstdin, bin.Xstdout, bin.Xstderr)
if bin.Xsqlite3_threadsafe(tls) == 0 {
panic(fmt.Errorf("sqlite: thread safety configuration error"))
}
if bin.Xsqlite3_config(
tls,
bin.XSQLITE_CONFIG_LOG,
func(tls *crt.TLS, pArg unsafe.Pointer, iErrCode int32, zMsg *int8) {
fmt.Fprintf(os.Stderr, "%v(%#x): %s\n", iErrCode, iErrCode, crt.GoString(zMsg))
},
unsafe.Pointer(nil),
) != 0 {
panic("sqlite: cannot configure error log callback")
}
sql.Register(driverName, newDrv())
}
func tracer(rx interface{}, format string, args ...interface{}) {
var b bytes.Buffer
_, file, line, _ := runtime.Caller(1)
fmt.Fprintf(&b, "%v:%v: (%[3]T)(%[3]p).", file, line, rx)
fmt.Fprintf(&b, format, args...)
fmt.Fprintf(os.Stderr, "%s\n", b.Bytes())
}
type result struct {
*stmt
lastInsertID int64
rowsAffected int
}
func (r *result) String() string {
return fmt.Sprintf("&%T@%p{stmt: %p, LastInsertId: %v, RowsAffected: %v}", *r, r, r.stmt, r.lastInsertID, r.rowsAffected)
}
func newResult(s *stmt) (_ *result, err error) {
r := &result{stmt: s}
if r.rowsAffected, err = r.changes(); err != nil {
return nil, err
}
if r.lastInsertID, err = r.lastInsertRowID(); err != nil {
return nil, err
}
return r, nil
}
// sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
func (r *result) lastInsertRowID() (v int64, _ error) {
return bin.Xsqlite3_last_insert_rowid(r.tls, r.pdb()), nil
}
// int sqlite3_changes(sqlite3*);
func (r *result) changes() (int, error) {
v := bin.Xsqlite3_changes(r.tls, r.pdb())
return int(v), nil
}
// LastInsertId returns the database's auto-generated ID after, for example, an
// INSERT into a table with primary key.
func (r *result) LastInsertId() (int64, error) {
if r == nil {
return 0, nil
}
return r.lastInsertID, nil
}
// RowsAffected returns the number of rows affected by the query.
func (r *result) RowsAffected() (int64, error) {
if r == nil {
return 0, nil
}
return int64(r.rowsAffected), nil
}
type rows struct {
*stmt
columns []string
rc0 int
pstmt unsafe.Pointer
doStep bool
}
func (r *rows) String() string {
return fmt.Sprintf("&%T@%p{stmt: %p, columns: %v, rc0: %v, pstmt: %#x, doStep: %v}", *r, r, r.stmt, r.columns, r.rc0, r.pstmt, r.doStep)
}
func newRows(s *stmt, pstmt unsafe.Pointer, rc0 int) (*rows, error) {
r := &rows{
stmt: s,
pstmt: pstmt,
rc0: rc0,
}
n, err := r.columnCount()
if err != nil {
return nil, err
}
r.columns = make([]string, n)
for i := range r.columns {
if r.columns[i], err = r.columnName(i); err != nil {
return nil, err
}
}
return r, nil
}
// Columns returns the names of the columns. The number of columns of the
// result is inferred from the length of the slice. If a particular column name
// isn't known, an empty string should be returned for that entry.
func (r *rows) Columns() (c []string) {
if trace {
defer func() {
tracer(r, "Columns(): %v", c)
}()
}
return r.columns
}
// Close closes the rows iterator.
func (r *rows) Close() (err error) {
if trace {
defer func() {
tracer(r, "Close(): %v", err)
}()
}
return r.finalize(r.pstmt)
}
// Next is called to populate the next row of data into the provided slice. The
// provided slice will be the same size as the Columns() are wide.
//
// Next should return io.EOF when there are no more rows.
func (r *rows) Next(dest []driver.Value) (err error) {
if trace {
defer func() {
tracer(r, "Next(%v): %v", dest, err)
}()
}
rc := r.rc0
if r.doStep {
if rc, err = r.step(r.pstmt); err != nil {
return err
}
}
r.doStep = true
switch rc {
case bin.XSQLITE_ROW:
if g, e := len(dest), len(r.columns); g != e {
return fmt.Errorf("Next(): have %v destination values, expected %v", g, e)
}
for i := range dest {
ct, err := r.columnType(i)
if err != nil {
return err
}
switch ct {
case bin.XSQLITE_INTEGER:
v, err := r.columnInt64(i)
if err != nil {
return err
}
dest[i] = v
case bin.XSQLITE_FLOAT:
v, err := r.columnDouble(i)
if err != nil {
return err
}
dest[i] = v
case bin.XSQLITE_TEXT:
v, err := r.columnText(i)
if err != nil {
return err
}
dest[i] = v
case bin.XSQLITE_BLOB:
v, err := r.columnBlob(i)
if err != nil {
return err
}
dest[i] = v
case bin.XSQLITE_NULL:
dest[i] = nil
default:
panic("internal error")
}
}
return nil
case bin.XSQLITE_DONE:
return io.EOF
default:
return r.errstr(int32(rc))
}
}
// int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
func (r *rows) columnBytes(iCol int) (_ int, err error) {
v := bin.Xsqlite3_column_bytes(r.tls, r.pstmt, int32(iCol))
return int(v), nil
}
// const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
func (r *rows) columnBlob(iCol int) (v []byte, err error) {
p := bin.Xsqlite3_column_blob(r.tls, r.pstmt, int32(iCol))
len, err := r.columnBytes(iCol)
if err != nil {
return nil, err
}
return crt.GoBytesLen((*int8)(p), len), nil
}
// const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
func (r *rows) columnText(iCol int) (v string, err error) {
p := bin.Xsqlite3_column_text(r.tls, r.pstmt, int32(iCol))
len, err := r.columnBytes(iCol)
if err != nil {
return "", err
}
return crt.GoStringLen((*int8)(unsafe.Pointer(p)), len), nil
}
// double sqlite3_column_double(sqlite3_stmt*, int iCol);
func (r *rows) columnDouble(iCol int) (v float64, err error) {
v = bin.Xsqlite3_column_double(r.tls, r.pstmt, int32(iCol))
return v, nil
}
// sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
func (r *rows) columnInt64(iCol int) (v int64, err error) {
v = bin.Xsqlite3_column_int64(r.tls, r.pstmt, int32(iCol))
return v, nil
}
// int sqlite3_column_type(sqlite3_stmt*, int iCol);
func (r *rows) columnType(iCol int) (_ int, err error) {
v := bin.Xsqlite3_column_type(r.tls, r.pstmt, int32(iCol))
return int(v), nil
}
// int sqlite3_column_count(sqlite3_stmt *pStmt);
func (r *rows) columnCount() (_ int, err error) {
v := bin.Xsqlite3_column_count(r.tls, r.pstmt)
return int(v), nil
}
// const char *sqlite3_column_name(sqlite3_stmt*, int N);
func (r *rows) columnName(n int) (string, error) {
p := bin.Xsqlite3_column_name(r.tls, r.pstmt, int32(n))
return crt.GoString(p), nil
}
type stmt struct {
*conn
allocs []unsafe.Pointer
psql *int8
ppstmt *unsafe.Pointer
pzTail **int8
}
func (s *stmt) String() string {
return fmt.Sprintf("&%T@%p{conn: %p, alloc %v, psql: %#x, ppstmt: %#x, pzTail: %#x}", *s, s, s.conn, s.allocs, s.psql, s.ppstmt, s.pzTail)
}
func newStmt(c *conn, sql string) (*stmt, error) {
s := &stmt{conn: c}
psql, err := s.cString(sql)
if err != nil {
return nil, err
}
s.psql = psql
ppstmt, err := s.malloc(ptrSize)
if err != nil {
s.free(unsafe.Pointer(psql))
return nil, err
}
s.ppstmt = (*unsafe.Pointer)(ppstmt)
pzTail, err := s.malloc(ptrSize)
if err != nil {
s.free(unsafe.Pointer(psql))
s.free(ppstmt)
return nil, err
}
s.pzTail = (**int8)(pzTail)
return s, nil
}
// Close closes the statement.
//
// As of Go 1.1, a Stmt will not be closed if it's in use by any queries.
func (s *stmt) Close() (err error) {
if trace {
defer func() {
tracer(s, "Close(): %v", err)
}()
}
if s.psql != nil {
err = s.free(unsafe.Pointer(s.psql))
s.psql = nil
}
if s.ppstmt != nil {
if err2 := s.free(unsafe.Pointer(s.ppstmt)); err2 != nil && err == nil {
err = err2
}
s.ppstmt = nil
}
if s.pzTail != nil {
if err2 := s.free(unsafe.Pointer(s.pzTail)); err2 != nil && err == nil {
err = err2
}
s.pzTail = nil
}
for _, v := range s.allocs {
if err2 := s.free(v); err2 != nil && err == nil {
err = err2
}
}
s.allocs = nil
return err
}
// NumInput returns the number of placeholder parameters.
//
// If NumInput returns >= 0, the sql package will sanity check argument counts
// from callers and return errors to the caller before the statement's Exec or
// Query methods are called.
//
// NumInput may also return -1, if the driver doesn't know its number of
// placeholders. In that case, the sql package will not sanity check Exec or
// Query argument counts.
func (s *stmt) NumInput() (n int) {
if trace {
defer func() {
tracer(s, "NumInput(): %v", n)
}()
}
return -1
}
// Exec executes a query that doesn't return rows, such as an INSERT or UPDATE.
func (s *stmt) Exec(args []driver.Value) (driver.Result, error) {
return s.exec(context.Background(), toNamedValues(args))
}
func (s *stmt) exec(ctx context.Context, args []namedValue) (r driver.Result, err error) {
if trace {
defer func(args []namedValue) {
tracer(s, "Exec(%v): (%v, %v)", args, r, err)
}(args)
}
var pstmt unsafe.Pointer
donech := make(chan struct{})
defer close(donech)
go func() {
select {
case <-ctx.Done():
if pstmt != nil {
s.interrupt(s.pdb())
}
case <-donech:
}
}()
for psql := s.psql; *psql != 0; psql = *s.pzTail {
if err := s.prepareV2(psql); err != nil {
return nil, err
}
pstmt = *s.ppstmt
if pstmt == nil {
continue
}
n, err := s.bindParameterCount(pstmt)
if err != nil {
return nil, err
}
if n != 0 {
if err = s.bind(pstmt, n, args); err != nil {
return nil, err
}
}
rc, err := s.step(pstmt)
if err != nil {
s.finalize(pstmt)
return nil, err
}
switch rc & 0xff {
case bin.XSQLITE_DONE, bin.XSQLITE_ROW:
if err := s.finalize(pstmt); err != nil {
return nil, err
}
default:
err = s.errstr(int32(rc))
s.finalize(pstmt)
return nil, err
}
}
return newResult(s)
}
func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
return s.query(context.Background(), toNamedValues(args))
}
func (s *stmt) query(ctx context.Context, args []namedValue) (r driver.Rows, err error) {
if trace {
defer func(args []namedValue) {
tracer(s, "Query(%v): (%v, %v)", args, r, err)
}(args)
}
var pstmt, rowStmt unsafe.Pointer
var rc0 int
donech := make(chan struct{})
defer close(donech)
go func() {
select {
case <-ctx.Done():
if pstmt != nil {
s.interrupt(s.pdb())
}
case <-donech:
}
}()
for psql := s.psql; *psql != 0; psql = *s.pzTail {
if err := s.prepareV2(psql); err != nil {
return nil, err
}
pstmt = *s.ppstmt
if pstmt == nil {
continue
}
n, err := s.bindParameterCount(pstmt)
if err != nil {
return nil, err
}
if n != 0 {
if err = s.bind(pstmt, n, args); err != nil {
return nil, err
}
}
rc, err := s.step(pstmt)
if err != nil {
s.finalize(pstmt)
return nil, err
}
switch rc {
case bin.XSQLITE_ROW:
if rowStmt != nil {
if err := s.finalize(pstmt); err != nil {
return nil, err
}
return nil, fmt.Errorf("query contains multiple select statements")
}
rowStmt = pstmt
rc0 = rc
case bin.XSQLITE_DONE:
if rowStmt == nil {
rc0 = rc
}
default:
err = s.errstr(int32(rc))
s.finalize(pstmt)
return nil, err
}
}
return newRows(s, rowStmt, rc0)
}
// int sqlite3_bind_double(sqlite3_stmt*, int, double);
func (s *stmt) bindDouble(pstmt unsafe.Pointer, idx1 int, value float64) (err error) {
if rc := bin.Xsqlite3_bind_double(s.tls, pstmt, int32(idx1), value); rc != 0 {
return s.errstr(rc)
}
return nil
}
// int sqlite3_bind_int(sqlite3_stmt*, int, int);
func (s *stmt) bindInt(pstmt unsafe.Pointer, idx1, value int) (err error) {
if rc := bin.Xsqlite3_bind_int(s.tls, pstmt, int32(idx1), int32(value)); rc != bin.XSQLITE_OK {
return s.errstr(rc)
}
return nil
}
// int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
func (s *stmt) bindInt64(pstmt unsafe.Pointer, idx1 int, value int64) (err error) {
if rc := bin.Xsqlite3_bind_int64(s.tls, pstmt, int32(idx1), value); rc != bin.XSQLITE_OK {
return s.errstr(rc)
}
return nil
}
// int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
func (s *stmt) bindBlob(pstmt unsafe.Pointer, idx1 int, value []byte) (err error) {
p, err := s.malloc(len(value))
if err != nil {
return err
}
s.allocs = append(s.allocs, p)
crt.CopyBytes(p, value, false)
if rc := bin.Xsqlite3_bind_blob(s.tls, pstmt, int32(idx1), p, int32(len(value)), nil); rc != bin.XSQLITE_OK {
return s.errstr(rc)
}
return nil
}
// int sqlite3_bind_text(sqlite3_stmt*,int,const char*,int,void(*)(void*));
func (s *stmt) bindText(pstmt unsafe.Pointer, idx1 int, value string) (err error) {
p, err := s.cString(value)
if err != nil {
return err
}
s.allocs = append(s.allocs, unsafe.Pointer(p))
if rc := bin.Xsqlite3_bind_text(s.tls, pstmt, int32(idx1), p, int32(len(value)), nil); rc != bin.XSQLITE_OK {
return s.errstr(rc)
}
return nil
}
func (s *stmt) bind(pstmt unsafe.Pointer, n int, args []namedValue) error {
for i := 1; i <= n; i++ {
name, err := s.bindParameterName(pstmt, i)
if err != nil {
return err
}
var v namedValue
for _, v = range args {
if name != "" {
// 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[:] {
break
}
} else {
if v.Ordinal == i {
break
}
}
}
if v.Ordinal == 0 {
if name != "" {
return fmt.Errorf("missing named argument %q", name[1:])
}
return fmt.Errorf("missing argument with %d index", i)
}
switch x := v.Value.(type) {
case int64:
if err := s.bindInt64(pstmt, i, x); err != nil {
return err
}
case float64:
if err := s.bindDouble(pstmt, i, x); err != nil {
return err
}
case bool:
v := 0
if x {
v = 1
}
if err := s.bindInt(pstmt, i, v); err != nil {
return err
}
case []byte:
if err := s.bindBlob(pstmt, i, x); err != nil {
return err
}
case string:
if err := s.bindText(pstmt, i, x); err != nil {
return err
}
case time.Time:
if err := s.bindText(pstmt, i, x.String()); err != nil {
return err
}
default:
return fmt.Errorf("invalid driver.Value type %T", x)
}
}
return nil
}
// int sqlite3_bind_parameter_count(sqlite3_stmt*);
func (s *stmt) bindParameterCount(pstmt unsafe.Pointer) (_ int, err error) {
r := bin.Xsqlite3_bind_parameter_count(s.tls, pstmt)
return int(r), nil
}
// const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
func (s *stmt) bindParameterName(pstmt unsafe.Pointer, i int) (string, error) {
p := bin.Xsqlite3_bind_parameter_name(s.tls, pstmt, int32(i))
return crt.GoString(p), nil
}
// int sqlite3_finalize(sqlite3_stmt *pStmt);
func (s *stmt) finalize(pstmt unsafe.Pointer) error {
if rc := bin.Xsqlite3_finalize(s.tls, pstmt); rc != bin.XSQLITE_OK {
return s.errstr(rc)
}
return nil
}
// int sqlite3_step(sqlite3_stmt*);
func (s *stmt) step(pstmt unsafe.Pointer) (int, error) {
r := bin.Xsqlite3_step(s.tls, pstmt)
return int(r), nil
}
// 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 (s *stmt) prepareV2(zSQL *int8) error {
if rc := bin.Xsqlite3_prepare_v2(s.tls, s.pdb(), zSQL, -1, s.ppstmt, s.pzTail); rc != bin.XSQLITE_OK {
return s.errstr(rc)
}
return nil
}
type tx struct {
*conn
}
func (t *tx) String() string { return fmt.Sprintf("&%T@%p{conn: %p}", *t, t, t.conn) }
func newTx(c *conn) (*tx, error) {
t := &tx{conn: c}
if err := t.exec(context.Background(), "begin"); err != nil {
return nil, err
}
return t, nil
}
// Commit implements driver.Tx.
func (t *tx) Commit() (err error) {
if trace {
defer func() {
tracer(t, "Commit(): %v", err)
}()
}
return t.exec(context.Background(), "commit")
}
// Rollback implements driver.Tx.
func (t *tx) Rollback() (err error) {
if trace {
defer func() {
tracer(t, "Rollback(): %v", err)
}()
}
return t.exec(context.Background(), "rollback")
}
// int sqlite3_exec(
// sqlite3*, /* An open database */
// const char *sql, /* SQL to be evaluated */
// int (*callback)(void*,int,char**,char**), /* Callback function */
// void *, /* 1st argument to callback */
// char **errmsg /* Error msg written here */
// );
func (t *tx) exec(ctx context.Context, sql string) (err error) {
psql, err := t.cString(sql)
if err != nil {
return err
}
defer t.free(unsafe.Pointer(psql))
// TODO: use t.conn.ExecContext() instead
donech := make(chan struct{})
defer close(donech)
go func() {
select {
case <-ctx.Done():
t.interrupt(t.pdb())
case <-donech:
}
}()
if rc := bin.Xsqlite3_exec(t.tls, t.pdb(), psql, nil, nil, nil); rc != bin.XSQLITE_OK {
return t.errstr(rc)
}
return nil
}
type conn struct {
*Driver
ppdb **bin.Xsqlite3
tls *crt.TLS
}
func (c *conn) String() string {
return fmt.Sprintf("&%T@%p{sqlite: %p, Thread: %p, ppdb: %#x}", *c, c, c.Driver, c.tls, c.ppdb)
}
func newConn(s *Driver, name string) (_ *conn, err error) {
c := &conn{Driver: s}
defer func() {
if err != nil {
c.close()
}
}()
c.Lock()
defer c.Unlock()
c.tls = crt.NewTLS()
if err = c.openV2(
name,
bin.XSQLITE_OPEN_READWRITE|bin.XSQLITE_OPEN_CREATE|
bin.XSQLITE_OPEN_FULLMUTEX|
bin.XSQLITE_OPEN_URI,
); err != nil {
return nil, err
}
if err = c.extendedResultCodes(true); err != nil {
return nil, err
}
return c, nil
}
// Prepare returns a prepared statement, bound to this connection.
func (c *conn) Prepare(query string) (s driver.Stmt, err error) {
return c.prepare(context.Background(), query)
}
func (c *conn) prepare(ctx context.Context, query string) (s driver.Stmt, err error) {
if trace {
defer func() {
tracer(c, "Prepare(%s): (%v, %v)", query, s, err)
}()
}
return newStmt(c, query)
}
// 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 trace {
defer func() {
tracer(c, "Close(): %v", err)
}()
}
return c.close()
}
// Begin starts a transaction.
func (c *conn) Begin() (driver.Tx, error) {
return c.begin(context.Background(), txOptions{})
}
// copy of driver.TxOptions
type txOptions struct {
Isolation int // driver.IsolationLevel
ReadOnly bool
}
func (c *conn) begin(ctx context.Context, opts txOptions) (t driver.Tx, err error) {
if trace {
defer func() {
tracer(c, "BeginTx(): (%v, %v)", t, err)
}()
}
return newTx(c)
}
// 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.
func (c *conn) Exec(query string, args []driver.Value) (driver.Result, error) {
return c.exec(context.Background(), query, toNamedValues(args))
}
func (c *conn) exec(ctx context.Context, query string, args []namedValue) (r driver.Result, err error) {
if trace {
defer func() {
tracer(c, "ExecContext(%s, %v): (%v, %v)", query, args, r, err)
}()
}
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)
}
// copy of driver.NameValue
type namedValue struct {
Name string
Ordinal int
Value driver.Value
}
// toNamedValues converts []driver.Value to []namedValue
func toNamedValues(vals []driver.Value) []namedValue {
args := make([]namedValue, 0, len(vals))
for i, val := range vals {
args = append(args, namedValue{Value: val, Ordinal: i + 1})
}
return args
}
// Queryer is an optional interface that may be implemented by a Conn.
//
// If a Conn does not implement Queryer, the sql package's DB.Query will first
// prepare a query, execute the statement, and then close the statement.
//
// Query may return ErrSkip.
func (c *conn) Query(query string, args []driver.Value) (driver.Rows, error) {
return c.query(context.Background(), query, toNamedValues(args))
}
func (c *conn) query(ctx context.Context, query string, args []namedValue) (r driver.Rows, err error) {
if trace {
defer func() {
tracer(c, "Query(%s, %v): (%v, %v)", query, args, r, err)
}()
}
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).query(ctx, args)
}
func (c *conn) pdb() *bin.Xsqlite3 { return *c.ppdb }
// int sqlite3_extended_result_codes(sqlite3*, int onoff);
func (c *conn) extendedResultCodes(on bool) (err error) {
var v int32
if on {
v = 1
}
if rc := bin.Xsqlite3_extended_result_codes(c.tls, c.pdb(), v); rc != bin.XSQLITE_OK {
return c.errstr(rc)
}
return nil
}
// void *sqlite3_malloc(int);
func (c *conn) malloc(n int) (r unsafe.Pointer, err error) {
if n > math.MaxInt32 {
panic("internal error")
}
r = bin.Xsqlite3_malloc(c.tls, int32(n))
if r == nil {
return nil, fmt.Errorf("malloc(%v) failed", n)
}
return r, nil
}
func (c *conn) cString(s string) (*int8, error) {
n := len(s)
p, err := c.malloc(n + 1)
if err != nil {
return nil, err
}
crt.CopyString(p, s, true)
return (*int8)(p), nil
}
// 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 string, flags int32) error {
filename, err := c.cString(name)
if err != nil {
return err
}