-
Notifications
You must be signed in to change notification settings - Fork 35
/
sqlite3.go
1339 lines (1202 loc) · 41.1 KB
/
sqlite3.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 2018 The go-sqlite-lite Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// go-sqlite-lite is a SQLite driver for the Go programming language. It is
// designed to be simple, lightweight, performant, understandable,
// unsurprising, debuggable, ergonomic, and fully featured. This driver does
// not provide a database/sql interface.
package sqlite3
/*
// SQLite compilation options.
// https://www.sqlite.org/compile.html
// https://www.sqlite.org/footprint.html
#cgo CFLAGS: -std=gnu99
#cgo CFLAGS: -Os
#cgo CFLAGS: -DNDEBUG=1
#cgo CFLAGS: -DSQLITE_CORE=1
#cgo CFLAGS: -DSQLITE_ENABLE_API_ARMOR=1
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3=1
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS=1
#cgo CFLAGS: -DSQLITE_ENABLE_FTS4=1
#cgo CFLAGS: -DSQLITE_ENABLE_FTS5=1
#cgo CFLAGS: -DSQLITE_ENABLE_GEOPOLY=1
#cgo CFLAGS: -DSQLITE_ENABLE_JSON1=1
#cgo CFLAGS: -DSQLITE_ENABLE_PREUPDATE_HOOK
#cgo CFLAGS: -DSQLITE_ENABLE_RTREE=1
#cgo CFLAGS: -DSQLITE_ENABLE_SESSION
#cgo CFLAGS: -DSQLITE_ENABLE_STAT4=1
#cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY
#cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT=1
#cgo CFLAGS: -DSQLITE_OMIT_AUTOINIT=1
#cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED=1
#cgo CFLAGS: -DSQLITE_OMIT_PROGRESS_CALLBACK=1
#cgo CFLAGS: -DSQLITE_OMIT_LOAD_EXTENSION=1
#cgo CFLAGS: -DSQLITE_OMIT_TRACE=1
#cgo CFLAGS: -DSQLITE_OMIT_UTF16=1
#cgo CFLAGS: -DSQLITE_SOUNDEX=1
#cgo CFLAGS: -DSQLITE_TEMP_STORE=2
#cgo CFLAGS: -DSQLITE_THREADSAFE=2
#cgo CFLAGS: -DSQLITE_USE_ALLOCA=1
#cgo CFLAGS: -DSQLITE_USE_URI=1
#cgo linux LDFLAGS: -lm
#cgo openbsd LDFLAGS: -lm
#cgo linux,!android CFLAGS: -DHAVE_FDATASYNC=1
#cgo linux,!android CFLAGS: -DHAVE_PREAD=1 -DHAVE_PWRITE=1
#cgo darwin CFLAGS: -DHAVE_FDATASYNC=1
#cgo darwin CFLAGS: -DHAVE_PREAD=1 -DHAVE_PWRITE=1
#cgo windows LDFLAGS: -Wl,-Bstatic -lwinpthread -Wl,-Bdynamic
// Fix for BusyTimeout on *nix systems.
#cgo !windows CFLAGS: -DHAVE_USLEEP=1
// Fix "_localtime32(0): not defined" linker error.
#cgo windows,386 CFLAGS: -D_localtime32=localtime
#include <assert.h>
#include <pthread.h>
#include "sqlite3.h"
// cgo doesn't handle variadic functions.
static void set_temp_dir(const char *path) {
sqlite3_temp_directory = sqlite3_mprintf("%s", path);
}
// cgo doesn't handle SQLITE_{STATIC,TRANSIENT} pointer constants.
static int bind_text(sqlite3_stmt *s, int i, const char *p, int n, int copy) {
if (n > 0) {
return sqlite3_bind_text(s, i, p, n,
(copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
}
return sqlite3_bind_text(s, i, "", 0, SQLITE_STATIC);
}
static int bind_blob(sqlite3_stmt *s, int i, const void *p, int n, int copy) {
if (n > 0) {
return sqlite3_bind_blob(s, i, p, n,
(copy ? SQLITE_TRANSIENT : SQLITE_STATIC));
}
return sqlite3_bind_zeroblob(s, i, 0);
}
// Faster retrieval of column data types (1 cgo call instead of n).
static void column_types(sqlite3_stmt *s, unsigned char p[], int n) {
int i = 0;
for (; i < n; ++i, ++p) {
*p = sqlite3_column_type(s, i);
}
}
// Macro for creating callback setter functions.
#define SET(x) \
static void set_##x(sqlite3 *db, void *data, int enable) { \
(enable ? sqlite3_##x(db, go_##x, data) : sqlite3_##x(db, 0, 0)); \
}
// util.go exports.
int go_busy_handler(void*,int);
int go_commit_hook(void*);
void go_rollback_hook(void*);
void go_update_hook(void* data, int op,const char *db, const char *tbl, sqlite3_int64 row);
int go_set_authorizer(void* data, int op, const char *arg1, const char *arg2, const char *db, const char *entity);
SET(busy_handler)
SET(commit_hook)
SET(rollback_hook)
SET(update_hook)
SET(set_authorizer)
// A pointer to an instance of this structure is passed as the user-context
// pointer when registering for an unlock-notify callback.
typedef struct UnlockNotification UnlockNotification;
struct UnlockNotification {
int fired; // True after unlock event has occurred
pthread_cond_t cond; // Condition variable to wait on
pthread_mutex_t mutex; // Mutex to protect structure
};
// This function is an unlock-notify callback registered with SQLite.
static void unlock_notify_cb(void **apArg, int nArg){
int i;
for(i=0; i<nArg; i++){
UnlockNotification *p = (UnlockNotification *)apArg[i];
pthread_mutex_lock(&p->mutex);
p->fired = 1;
pthread_cond_signal(&p->cond);
pthread_mutex_unlock(&p->mutex);
}
}
// This function assumes that an SQLite API call (either sqlite3_prepare_v2()
// or sqlite3_step()) has just returned SQLITE_LOCKED. The argument is the
// associated database connection.
//
// This function calls sqlite3_unlock_notify() to register for an
// unlock-notify callback, then blocks until that callback is delivered
// and returns SQLITE_OK. The caller should then retry the failed operation.
//
// Or, if sqlite3_unlock_notify() indicates that to block would deadlock
// the system, then this function returns SQLITE_LOCKED immediately. In
// this case the caller should not retry the operation and should roll
// back the current transaction (if any).
static int wait_for_unlock_notify(sqlite3 *db){
int rc;
UnlockNotification un;
// Initialize the UnlockNotification structure.
un.fired = 0;
pthread_mutex_init(&un.mutex, 0);
pthread_cond_init(&un.cond, 0);
// Register for an unlock-notify callback.
rc = sqlite3_unlock_notify(db, unlock_notify_cb, (void *)&un);
assert( rc==SQLITE_LOCKED || rc==SQLITE_OK );
// The call to sqlite3_unlock_notify() always returns either SQLITE_LOCKED
// or SQLITE_OK.
//
// If SQLITE_LOCKED was returned, then the system is deadlocked. In this
// case this function needs to return SQLITE_LOCKED to the caller so
// that the current transaction can be rolled back. Otherwise, block
// until the unlock-notify callback is invoked, then return SQLITE_OK.
if( rc==SQLITE_OK ){
pthread_mutex_lock(&un.mutex);
if( !un.fired ){
pthread_cond_wait(&un.cond, &un.mutex);
}
pthread_mutex_unlock(&un.mutex);
}
// Destroy the mutex and condition variables.
pthread_cond_destroy(&un.cond);
pthread_mutex_destroy(&un.mutex);
return rc;
}
// This function is a wrapper around the SQLite function sqlite3_step().
// It functions in the same way as step(), except that if a required
// shared-cache lock cannot be obtained, this function may block waiting for
// the lock to become available. In this scenario the normal API step()
// function always returns SQLITE_LOCKED.
//
// If this function returns SQLITE_LOCKED, the caller should rollback
// the current transaction (if any) and try again later. Otherwise, the
// system may become deadlocked.
int sqlite3_blocking_step(sqlite3 *db, sqlite3_stmt *pStmt){
int rc;
for (;;) {
rc = sqlite3_step(pStmt);
if( rc != SQLITE_LOCKED ) {
break;
}
if( sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE ) {
break;
}
rc = wait_for_unlock_notify(sqlite3_db_handle(pStmt));
if( rc!=SQLITE_OK ) {
break;
}
sqlite3_reset(pStmt);
}
return rc;
}
// This function is a wrapper around the SQLite function sqlite3_prepare_v2().
// It functions in the same way as prepare_v2(), except that if a required
// shared-cache lock cannot be obtained, this function may block waiting for
// the lock to become available. In this scenario the normal API prepare_v2()
// function always returns SQLITE_LOCKED.
//
// If this function returns SQLITE_LOCKED, the caller should rollback
// the current transaction (if any) and try again later. Otherwise, the
// system may become deadlocked.
int sqlite3_blocking_prepare_v2(
sqlite3 *db, // Database handle.
const char *zSql, // UTF-8 encoded SQL statement.
int nSql, // Length of zSql in bytes.
sqlite3_stmt **ppStmt, // OUT: A pointer to the prepared statement
const char **pz // OUT: End of parsed string
){
int rc;
for (;;) {
rc = sqlite3_prepare_v2(db, zSql, nSql, ppStmt, pz);
if( rc != SQLITE_LOCKED ){
break;
}
if( sqlite3_extended_errcode(db) != SQLITE_LOCKED_SHAREDCACHE ) {
break;
}
rc = wait_for_unlock_notify(db);
if( rc!=SQLITE_OK ) {
break;
}
}
return rc;
}
*/
import "C"
import (
"fmt"
"io"
"os"
"time"
"unsafe"
)
// initErr indicates a SQLite initialization error, which disables this package.
var initErr error
// emptyByteSlice is what we might return on ColumnRawBytes to avoid
// allocation. Since they are not allowed to modify the slice, this is safe.
var emptyByteSlice = []byte{}
var busyRegistry = newRegistry()
var commitRegistry = newRegistry()
var rollbackRegistry = newRegistry()
var updateRegistry = newRegistry()
var authorizerRegistry = newRegistry()
func init() {
// Initialize SQLite (required with SQLITE_OMIT_AUTOINIT).
// https://www.sqlite.org/c3ref/initialize.html
if rc := C.sqlite3_initialize(); rc != OK {
initErr = errStr(rc)
return
}
// Use the same temporary directory as Go.
// https://www.sqlite.org/c3ref/temp_directory.html
tmp := os.TempDir() + "\x00"
C.set_temp_dir(cStr(tmp))
}
// Conn is a connection handle, which may have multiple databases attached to it
// by using the ATTACH SQL statement.
// https://www.sqlite.org/c3ref/sqlite3.html
type Conn struct {
db *C.sqlite3
busyIdx int
commitIdx int
rollbackIdx int
updateIdx int
authorizerIdx int
}
// Open creates a new connection to a SQLite database. The name can be 1) a path
// to a file, which is created if it does not exist, 2) a URI using the syntax
// described at https://www.sqlite.org/uri.html, 3) the string ":memory:",
// which creates a temporary in-memory database, or 4) an empty string, which
// creates a temporary on-disk database (deleted when closed) in the directory
// returned by os.TempDir(). Flags to Open can optionally be provided. If no
// flags are provided, the default flags of OPEN_READWRITE|OPEN_CREATE are
// used.
// https://www.sqlite.org/c3ref/open.html
func Open(name string, flagArgs ...int) (*Conn, error) {
if len(flagArgs) > 1 {
return nil, pkgErr(MISUSE, "too many arguments provided to Open")
}
if initErr != nil {
return nil, initErr
}
name += "\x00"
var db *C.sqlite3
flags := C.SQLITE_OPEN_READWRITE | C.SQLITE_OPEN_CREATE
if len(flagArgs) == 1 {
flags = flagArgs[0]
}
rc := C.sqlite3_open_v2(cStr(name), &db, C.int(flags), nil)
if rc != OK {
err := libErr(rc, db)
C.sqlite3_close(db)
return nil, err
}
c := &Conn{db: db}
C.sqlite3_extended_result_codes(db, 1)
return c, nil
}
// Close releases all resources associated with the connection. If any prepared
// statements, incremental I/O operations, or backup operations are still
// active, the connection becomes an unusable "zombie" and is closed after all
// remaining statements and operations are destroyed. A BUSY error code is
// returned if the connection is left in this "zombie" status, which may
// indicate a programming error where some previously allocated resource is not
// properly released.
// https://www.sqlite.org/c3ref/close.html
func (c *Conn) Close() error {
if db := c.db; db != nil {
c.db = nil
// Unregister all of the globally registered callbacks
if c.busyIdx != 0 {
busyRegistry.unregister(c.busyIdx)
}
if c.commitIdx != 0 {
commitRegistry.unregister(c.commitIdx)
}
if c.rollbackIdx != 0 {
rollbackRegistry.unregister(c.rollbackIdx)
}
if c.updateIdx != 0 {
updateRegistry.unregister(c.updateIdx)
}
if c.authorizerIdx != 0 {
authorizerRegistry.unregister(c.authorizerIdx)
}
if rc := C.sqlite3_close(db); rc != OK {
err := libErr(rc, db)
if rc == BUSY {
C.sqlite3_close_v2(db)
}
return err
}
}
return nil
}
// Prepare compiles the first statement in sql. Any remaining text after the
// first statement is saved in s.Tail. This function may return a nil stmt and
// a nil error, if the sql string contains nothing to do. For convenience,
// this function can also bind arguments to the returned statement. If an
// error occurs during binding, the statement is closed/finalized and the error
// is returned.
// https://www.sqlite.org/c3ref/prepare.html
func (c *Conn) Prepare(sql string, args ...interface{}) (s *Stmt, err error) {
zSQL := sql + "\x00"
var stmt *C.sqlite3_stmt
var cTail *C.char
rc := C.sqlite3_blocking_prepare_v2(c.db, cStr(zSQL), -1, &stmt, &cTail)
if rc != OK {
return nil, libErr(rc, c.db)
}
if stmt == nil {
return nil, nil
}
var tail string
if cTail != nil {
n := cStrOffset(zSQL, cTail)
if n >= 0 && n < len(sql) {
tail = sql[n:]
}
}
s = &Stmt{stmt: stmt, db: c.db, Tail: tail}
if len(args) > 0 {
if err = s.Bind(args...); err != nil {
s.Close()
return nil, err
}
}
return s, nil
}
// Exec is a convenience function that will call sqlite3_exec if no argument
// are given. If arguments are given, it's simply a convenient way to
// Prepare a statement, Bind arguments, Step the statement to completion,
// and Close/finalize the statement.
// https://www.sqlite.org/c3ref/exec.html
func (c *Conn) Exec(sql string, args ...interface{}) error {
// Fast path via sqlite3_exec, which doesn't support parameter binding
if len(args) == 0 {
sql += "\x00"
return c.exec(cStr(sql))
}
s, err := c.Prepare(sql)
if err != nil {
return err
}
if s == nil {
return nil
}
defer s.Close()
if err = s.Bind(args...); err != nil {
return err
}
if err = s.StepToCompletion(); err != nil {
return err
}
return nil
}
// Begin starts a new deferred transaction. This is equivalent to
// c.Exec("BEGIN")
// https://www.sqlite.org/lang_transaction.html
func (c *Conn) Begin() error {
return c.exec(cStr("BEGIN\x00"))
}
// BeginImmediate starts a new immediate transaction. This is equivalent to
// c.Exec("BEGIN IMMEDIATE")
// https://www.sqlite.org/lang_transaction.html
func (c *Conn) BeginImmediate() error {
return c.exec(cStr("BEGIN IMMEDIATE\x00"))
}
// BeginExclusive starts a new exclusive transaction. This is equivalent to
// c.Exec("BEGIN EXCLUSIVE")
// https://www.sqlite.org/lang_transaction.html
func (c *Conn) BeginExclusive() error {
return c.exec(cStr("BEGIN EXCLUSIVE\x00"))
}
// Commit saves all changes made within a transaction to the database.
func (c *Conn) Commit() error {
return c.exec(cStr("COMMIT\x00"))
}
// Rollback aborts the current transaction without saving any changes.
func (c *Conn) Rollback() error {
return c.exec(cStr("ROLLBACK\x00"))
}
// WithTx is a convenience method that begins a deferred transaction, calls the
// function f, and will commit the transaction if f does not return an error,
// and will roll back the transaction if f does return an error.
func (c *Conn) WithTx(f func() error) error {
if err := c.Begin(); err != nil {
return fmt.Errorf("failed to begin transaction: %v", err)
}
// Perform work inside the transaction
err := f()
if err != nil {
err2 := c.Rollback()
if err2 == nil {
return err
}
return fmt.Errorf("%v, additionally rolling back transaction failed: %v", err, err2)
}
if err = c.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %v", err)
}
return nil
}
// WithTxImmediate is a convenience method that begins an immediate
// transaction, calls the function f, and will commit the transaction if f does
// not return an error, and will roll back the transaction if f does return an
// error.
func (c *Conn) WithTxImmediate(f func() error) error {
if err := c.BeginImmediate(); err != nil {
return fmt.Errorf("failed to begin immediate transaction: %v", err)
}
// Perform work inside the transaction
err := f()
if err != nil {
err2 := c.Rollback()
if err2 == nil {
return err
}
return fmt.Errorf("%v, additionally rolling back transaction failed: %v", err, err2)
}
if err = c.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %v", err)
}
return nil
}
// WithTxExclusive is a convenience method that begins a exclusive transaction,
// calls the function f, and will commit the transaction if f does not return
// an error, and will roll back the transaction if f does return an error.
func (c *Conn) WithTxExclusive(f func() error) error {
if err := c.BeginExclusive(); err != nil {
return fmt.Errorf("failed to begin exclusive transaction: %v", err)
}
// Perform work inside the transaction
err := f()
if err != nil {
err2 := c.Rollback()
if err2 == nil {
return err
}
return fmt.Errorf("%v, additionally rolling back transaction failed: %v", err, err2)
}
if err = c.Commit(); err != nil {
return fmt.Errorf("failed to commit transaction: %v", err)
}
return nil
}
// Interrupt causes any pending database operation to abort and return at its
// earliest opportunity. It is safe to call this method from a goroutine
// different from the one that is currently running the database operation, but
// it is not safe to call this method on a connection that might close before
// the call returns.
// https://www.sqlite.org/c3ref/interrupt.html
func (c *Conn) Interrupt() {
if db := c.db; db != nil {
C.sqlite3_interrupt(db)
}
}
// AutoCommit returns true if the database connection is in auto-commit mode
// (i.e. outside of an explicit transaction started by BEGIN).
// https://www.sqlite.org/c3ref/get_autocommit.html
func (c *Conn) AutoCommit() bool {
return C.sqlite3_get_autocommit(c.db) != 0
}
// LastInsertRowID returns the ROWID of the most recent successful INSERT
// statement.
// https://www.sqlite.org/c3ref/last_insert_rowid.html
func (c *Conn) LastInsertRowID() int64 {
return int64(C.sqlite3_last_insert_rowid(c.db))
}
// Changes returns the number of rows that were changed, inserted, or deleted
// by the most recent statement. Auxiliary changes caused by triggers or
// foreign key actions are not counted.
// https://www.sqlite.org/c3ref/changes.html
func (c *Conn) Changes() int {
return int(C.sqlite3_changes(c.db))
}
// TotalChanges returns the number of rows that were changed, inserted, or
// deleted since the database connection was opened, including changes caused by
// trigger and foreign key actions.
// https://www.sqlite.org/c3ref/total_changes.html
func (c *Conn) TotalChanges() int {
return int(C.sqlite3_total_changes(c.db))
}
// Backup starts an online database backup of c.srcName into dst.dstName.
// Connections c and dst must be distinct. All existing contents of the
// destination database are overwritten.
//
// A read lock is acquired on the source database only while it is being read
// during a call to Backup.Step. The source connection may be used for other
// purposes between these calls. The destination connection must not be used for
// anything until the backup is closed.
// https://www.sqlite.org/backup.html
func (c *Conn) Backup(srcName string, dst *Conn, dstName string) (*Backup, error) {
if c == dst || dst == nil {
return nil, ErrBadConn
}
return newBackup(c, srcName, dst, dstName)
}
// BlobIO opens a BLOB or TEXT value for incremental I/O, allowing the value to
// be treated as a file for reading and/or writing. The value is located as if
// by the following query:
//
// SELECT col FROM db.tbl WHERE rowid=row
//
// If rw is true, the value is opened with read-write access, otherwise it is
// read-only. It is not possible to open a column that is part of an index or
// primary key for writing. If foreign key constraints are enabled, it is not
// possible to open a column that is part of a child key for writing.
// https://www.sqlite.org/c3ref/blob_open.html
func (c *Conn) BlobIO(db, tbl, col string, row int64, rw bool) (*BlobIO, error) {
return newBlobIO(c, db, tbl, col, row, rw)
}
// BusyTimeout enables the built-in busy handler, which retries the table
// locking operation for the specified duration before aborting. The busy
// handler is disabled if d is negative or zero.
// https://www.sqlite.org/c3ref/busy_timeout.html
func (c *Conn) BusyTimeout(d time.Duration) {
C.sqlite3_busy_timeout(c.db, C.int(d/time.Millisecond))
}
// BusyFunc registers a function that is invoked by SQLite when it is unable to
// acquire a lock on a table. The function f should return true to make another
// lock acquisition attempt, or false to let the operation fail with BUSY or
// IOERR_BLOCKED error code.
// https://www.sqlite.org/c3ref/busy_handler.html
func (c *Conn) BusyFunc(f BusyFunc) {
idx := busyRegistry.register(f)
c.busyIdx = idx
C.set_busy_handler(c.db, unsafe.Pointer(&c.busyIdx), cBool(f != nil))
}
// FileName returns the full file path of an attached database. An empty string
// is returned for temporary databases.
// https://www.sqlite.org/c3ref/db_filename.html
func (c *Conn) FileName(db string) string {
db += "\x00"
if path := C.sqlite3_db_filename(c.db, cStr(db)); path != nil {
return C.GoString(path)
}
return ""
}
// Status returns the current and peak values of a connection performance
// counter, specified by one of the DBSTATUS constants. If reset is true, the
// peak value is reset back down to the current value after retrieval.
// https://www.sqlite.org/c3ref/db_status.html
func (c *Conn) Status(op int, reset bool) (cur, peak int, err error) {
var cCur, cPeak C.int
rc := C.sqlite3_db_status(c.db, C.int(op), &cCur, &cPeak, cBool(reset))
if rc != OK {
return 0, 0, pkgErr(MISUSE, "invalid connection status op (%d)", op)
}
return int(cCur), int(cPeak), nil
}
// Limit changes a per-connection resource usage or performance limit, specified
// by one of the LIMIT constants, returning its previous value. If the new value
// is negative, the limit is left unchanged and its current value is returned.
// https://www.sqlite.org/c3ref/limit.html
func (c *Conn) Limit(id, value int) (prev int) {
prev = int(C.sqlite3_limit(c.db, C.int(id), C.int(value)))
return
}
// exec calls sqlite3_exec on sql, which must be a null-terminated C string.
func (c *Conn) exec(sql *C.char) error {
if rc := C.sqlite3_exec(c.db, sql, nil, nil, nil); rc != OK {
return libErr(rc, c.db)
}
return nil
}
// Stmt is a prepared statement handle.
// https://www.sqlite.org/c3ref/stmt.html
type Stmt struct {
Tail string
stmt *C.sqlite3_stmt
db *C.sqlite3
// Data type codes for all columns in the current row. This is
// unfortunately absolutely necessary to keep around. Column types are
// required in order to differentiate NULL from error conditions, and
// sqlite3_column_type is undefined after a type conversion happens.
colTypes []uint8
haveColTypes bool
}
// Close releases all resources associated with the prepared statement. This
// method can be called at any point in the statement's life cycle.
// https://www.sqlite.org/c3ref/finalize.html
func (s *Stmt) Close() error {
rc := C.sqlite3_finalize(s.stmt)
s.stmt = nil
if rc != OK {
return libErr(rc, s.db)
}
return nil
}
// Busy returns true if the prepared statement is in the middle of execution
// with a row available for scanning.
func (s *Stmt) Busy() bool {
return C.sqlite3_stmt_busy(s.stmt) != 0
}
// ReadOnly returns true if the prepared statement makes no direct changes to
// the content of the database file.
// https://www.sqlite.org/c3ref/stmt_readonly.html
func (s *Stmt) ReadOnly() bool {
return C.sqlite3_stmt_readonly(s.stmt) != 0
}
// BindParameterCount returns the number of SQL parameters in the prepared
// statement.
// https://www.sqlite.org/c3ref/bind_parameter_count.html
func (s *Stmt) BindParameterCount() int {
return int(C.sqlite3_bind_parameter_count(s.stmt))
}
// ColumnCount returns the number of columns produced by the prepared
// statement.
// https://www.sqlite.org/c3ref/column_count.html
func (s *Stmt) ColumnCount() int {
return int(C.sqlite3_column_count(s.stmt))
}
// // Params returns the names of bound parameters in the prepared statement. Nil
// // is returned if the statement does not use named parameters.
// // https://www.sqlite.org/c3ref/bind_parameter_name.html
// func (s *Stmt) Params() []string {
// if s.varNames == nil {
// var names []string
// for i := 0; i < s.nVars; i++ {
// name := C.sqlite3_bind_parameter_name(s.stmt, C.int(i+1))
// if name == nil {
// names = unnamedVars
// break
// }
// if names == nil {
// names = make([]string, s.nVars)
// }
// names[i] = C.GoString(name)
// }
// s.varNames = names
// }
// if len(s.varNames) == 0 {
// return nil // unnamedVars -> nil
// }
// return s.varNames
// }
// ColumnName returns the name of column produced by the prepared statement.
// The leftmost column is number 0.
// https://www.sqlite.org/c3ref/column_name.html
func (s *Stmt) ColumnName(i int) string {
return C.GoString(C.sqlite3_column_name(s.stmt, C.int(i)))
}
// ColumnNames returns the names of columns produced by the prepared
// statement.
// https://www.sqlite.org/c3ref/column_name.html
func (s *Stmt) ColumnNames() []string {
nCols := s.ColumnCount()
names := make([]string, nCols)
for i := range names {
names[i] = s.ColumnName(i)
}
return names
}
// DeclType returns the type declaration of columns produced by the prepared
// statement. The leftmost column is number 0.
// https://www.sqlite.org/c3ref/column_decltype.html
func (s *Stmt) DeclType(i int) string {
return C.GoString(C.sqlite3_column_decltype(s.stmt, C.int(i)))
}
// DeclTypes returns the type declarations of columns produced by the prepared
// statement.
// https://www.sqlite.org/c3ref/column_decltype.html
func (s *Stmt) DeclTypes() []string {
nCols := s.ColumnCount()
declTypes := make([]string, nCols)
for i := range declTypes {
declTypes[i] = s.DeclType(i)
}
return declTypes
}
// Exec is a convenience method that binds the given arguments to the statement
// then steps the statement to completion and resets the prepared statement. No
// rows are returned. Reset is always called, even in error cases. Note that
// bindings are not cleared.
func (s *Stmt) Exec(args ...interface{}) error {
err := s.Bind(args...)
if err != nil {
s.Reset()
return err
}
if err = s.StepToCompletion(); err != nil {
s.Reset()
return err
}
if err = s.Reset(); err != nil {
return err
}
return err
}
// Bind binds either the named arguments or unnamed arguments depending on the
// type of arguments passed
func (s *Stmt) Bind(args ...interface{}) error {
for i, v := range args {
var rc C.int
if v == nil {
rc = C.sqlite3_bind_null(s.stmt, C.int(i+1))
if rc != OK {
return errStr(rc)
}
continue
}
switch v := v.(type) {
case int:
rc = C.sqlite3_bind_int64(s.stmt, C.int(i+1), C.sqlite3_int64(v))
case int64:
rc = C.sqlite3_bind_int64(s.stmt, C.int(i+1), C.sqlite3_int64(v))
case float64:
rc = C.sqlite3_bind_double(s.stmt, C.int(i+1), C.double(v))
case bool:
rc = C.sqlite3_bind_int64(s.stmt, C.int(i+1), C.sqlite3_int64(cBool(v)))
case string:
rc = C.bind_text(s.stmt, C.int(i+1), cStr(v), C.int(len(v)), 1)
case []byte:
// This is a strange case. nil byte arrays should be treated as inserting NULL
if []byte(v) == nil {
rc = C.sqlite3_bind_null(s.stmt, C.int(i+1))
} else {
rc = C.bind_blob(s.stmt, C.int(i+1), cBytes(v), C.int(len(v)), 1)
}
case RawString:
rc = C.bind_text(s.stmt, C.int(i+1), cStr(string(v)), C.int(len(v)), 0)
case RawBytes:
rc = C.bind_blob(s.stmt, C.int(i+1), cBytes(v), C.int(len(v)), 0)
case ZeroBlob:
rc = C.sqlite3_bind_zeroblob(s.stmt, C.int(i+1), C.int(v))
case NamedArgs:
if i != 0 || len(args) != 1 {
return pkgErr(MISUSE, "NamedArgs must be used as the only argument to Bind()")
}
return s.bindNamed(v)
default:
return pkgErr(MISUSE, "unsupported type at index %d (%T)", i, v)
}
if rc != OK {
return errStr(rc)
}
}
return nil
}
// Scan retrieves data from the current row, storing successive column values
// into successive arguments. The same row may be scanned multiple times. Nil
// arguments are silently skipped.
// https://www.sqlite.org/c3ref/column_blob.html
func (s *Stmt) Scan(dst ...interface{}) error {
n := len(dst)
if n == 0 {
return nil
}
for i, v := range dst[:n] {
if v != nil {
if err := s.scan(i, v); err != nil {
return err
}
}
}
return nil
}
// Reset returns the prepared statement to its initial state, ready to be
// re-executed. This should be done when the remaining rows returned by a query
// are not needed, which releases some resources that would otherwise persist
// until the next call to Exec or Query.
// https://www.sqlite.org/c3ref/reset.html
func (s *Stmt) Reset() error {
s.colTypes = s.colTypes[:0]
s.haveColTypes = false
if rc := C.sqlite3_reset(s.stmt); rc != OK {
return errStr(rc)
}
return nil
}
// ClearBindings clears the bindings on a prepared statement. Reset does not
// clear bindings.
// https://www.sqlite.org/c3ref/clear_bindings.html
func (s *Stmt) ClearBindings() error {
if rc := C.sqlite3_clear_bindings(s.stmt); rc != OK {
return errStr(rc)
}
return nil
}
// Status returns the current value of a statement performance counter,
// specified by one of the STMTSTATUS constants. If reset is true, the value is
// reset back down to 0 after retrieval.
// https://www.sqlite.org/c3ref/stmt_status.html
func (s *Stmt) Status(op int, reset bool) int {
return int(C.sqlite3_stmt_status(s.stmt, C.int(op), cBool(reset)))
}
// bindNamed binds statement parameters using the name/value pairs in args.
func (s *Stmt) bindNamed(args NamedArgs) error {
for name, v := range args {
zName := name + "\x00"
i := C.sqlite3_bind_parameter_index(s.stmt, cStr(zName))
if i == 0 {
// The name wasn't found in the prepared statement
continue
}
var rc C.int
if v == nil {
rc = C.sqlite3_bind_null(s.stmt, i)
if rc != OK {
return errStr(rc)
}
continue
}
switch v := v.(type) {
case int:
rc = C.sqlite3_bind_int64(s.stmt, i, C.sqlite3_int64(v))
case int64:
rc = C.sqlite3_bind_int64(s.stmt, i, C.sqlite3_int64(v))
case float64:
rc = C.sqlite3_bind_double(s.stmt, i, C.double(v))
case bool:
rc = C.sqlite3_bind_int64(s.stmt, i, C.sqlite3_int64(cBool(v)))
case string:
rc = C.bind_text(s.stmt, i, cStr(v), C.int(len(v)), 1)
case []byte:
// This is a strange case. nil byte arrays should be treated as inserting NULL
if []byte(v) == nil {
rc = C.sqlite3_bind_null(s.stmt, i)
} else {
rc = C.bind_blob(s.stmt, i, cBytes(v), C.int(len(v)), 1)
}
case RawString:
rc = C.bind_text(s.stmt, i, cStr(string(v)), C.int(len(v)), 0)
case RawBytes:
rc = C.bind_blob(s.stmt, i, cBytes(v), C.int(len(v)), 0)
case ZeroBlob:
rc = C.sqlite3_bind_zeroblob(s.stmt, i, C.int(v))
default:
return pkgErr(MISUSE, "unsupported type for %s (%T)", name, v)
}
if rc != OK {
return errStr(rc)
}
}
return nil
}
// Step evaluates the next step in the statement's program. It returns true if
// if a new row of data is ready for processing.
// https://www.sqlite.org/c3ref/step.html
func (s *Stmt) Step() (bool, error) {
s.colTypes = s.colTypes[:0]
s.haveColTypes = false
rc := C.sqlite3_blocking_step(s.db, s.stmt)
if rc == ROW {
return true, nil
}
if rc == DONE {
return false, nil
}
return false, errStr(rc)
}
// StepToCompletion is a convenience method that repeatedly calls Step until no
// more rows are returned or an error occurs.
// https://www.sqlite.org/c3ref/step.html
func (s *Stmt) StepToCompletion() error {
s.colTypes = s.colTypes[:0]
s.haveColTypes = false
for {
rc := C.sqlite3_blocking_step(s.db, s.stmt)
if rc == ROW {
continue
} else if rc == DONE {
break
} else {
return errStr(rc)