podman

Форк
0
2281 строка · 65.4 Кб
1
// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
2
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
3
//
4
// Use of this source code is governed by an MIT-style
5
// license that can be found in the LICENSE file.
6

7
//go:build cgo
8
// +build cgo
9

10
package sqlite3
11

12
/*
13
#cgo CFLAGS: -std=gnu99
14
#cgo CFLAGS: -DSQLITE_ENABLE_RTREE
15
#cgo CFLAGS: -DSQLITE_THREADSAFE=1
16
#cgo CFLAGS: -DHAVE_USLEEP=1
17
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3
18
#cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS
19
#cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15
20
#cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED
21
#cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
22
#cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT
23
#cgo CFLAGS: -Wno-deprecated-declarations
24
#cgo openbsd CFLAGS: -I/usr/local/include
25
#cgo openbsd LDFLAGS: -L/usr/local/lib
26
#ifndef USE_LIBSQLITE3
27
#include "sqlite3-binding.h"
28
#else
29
#include <sqlite3.h>
30
#endif
31
#include <stdlib.h>
32
#include <string.h>
33

34
#ifdef __CYGWIN__
35
# include <errno.h>
36
#endif
37

38
#ifndef SQLITE_OPEN_READWRITE
39
# define SQLITE_OPEN_READWRITE 0
40
#endif
41

42
#ifndef SQLITE_OPEN_FULLMUTEX
43
# define SQLITE_OPEN_FULLMUTEX 0
44
#endif
45

46
#ifndef SQLITE_DETERMINISTIC
47
# define SQLITE_DETERMINISTIC 0
48
#endif
49

50
#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64)
51
# undef USE_PREAD
52
# undef USE_PWRITE
53
# define USE_PREAD64 1
54
# define USE_PWRITE64 1
55
#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE)
56
# undef USE_PREAD
57
# undef USE_PWRITE
58
# define USE_PREAD64 1
59
# define USE_PWRITE64 1
60
#endif
61

62
static int
63
_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
64
#ifdef SQLITE_OPEN_URI
65
  return sqlite3_open_v2(filename, ppDb, flags | SQLITE_OPEN_URI, zVfs);
66
#else
67
  return sqlite3_open_v2(filename, ppDb, flags, zVfs);
68
#endif
69
}
70

71
static int
72
_sqlite3_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
73
  return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
74
}
75

76
static int
77
_sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
78
  return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
79
}
80

81
#include <stdio.h>
82
#include <stdint.h>
83

84
static int
85
_sqlite3_exec(sqlite3* db, const char* pcmd, long long* rowid, long long* changes)
86
{
87
  int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
88
  *rowid = (long long) sqlite3_last_insert_rowid(db);
89
  *changes = (long long) sqlite3_changes(db);
90
  return rv;
91
}
92

93
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
94
extern int _sqlite3_step_blocking(sqlite3_stmt *stmt);
95
extern int _sqlite3_step_row_blocking(sqlite3_stmt* stmt, long long* rowid, long long* changes);
96
extern int _sqlite3_prepare_v2_blocking(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail);
97

98
static int
99
_sqlite3_step_internal(sqlite3_stmt *stmt)
100
{
101
  return _sqlite3_step_blocking(stmt);
102
}
103

104
static int
105
_sqlite3_step_row_internal(sqlite3_stmt* stmt, long long* rowid, long long* changes)
106
{
107
  return _sqlite3_step_row_blocking(stmt, rowid, changes);
108
}
109

110
static int
111
_sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail)
112
{
113
  return _sqlite3_prepare_v2_blocking(db, zSql, nBytes, ppStmt, pzTail);
114
}
115

116
#else
117
static int
118
_sqlite3_step_internal(sqlite3_stmt *stmt)
119
{
120
  return sqlite3_step(stmt);
121
}
122

123
static int
124
_sqlite3_step_row_internal(sqlite3_stmt* stmt, long long* rowid, long long* changes)
125
{
126
  int rv = sqlite3_step(stmt);
127
  sqlite3* db = sqlite3_db_handle(stmt);
128
  *rowid = (long long) sqlite3_last_insert_rowid(db);
129
  *changes = (long long) sqlite3_changes(db);
130
  return rv;
131
}
132

133
static int
134
_sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail)
135
{
136
  return sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail);
137
}
138
#endif
139

140
void _sqlite3_result_text(sqlite3_context* ctx, const char* s) {
141
  sqlite3_result_text(ctx, s, -1, &free);
142
}
143

144
void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l) {
145
  sqlite3_result_blob(ctx, b, l, SQLITE_TRANSIENT);
146
}
147

148

149
int _sqlite3_create_function(
150
  sqlite3 *db,
151
  const char *zFunctionName,
152
  int nArg,
153
  int eTextRep,
154
  uintptr_t pApp,
155
  void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
156
  void (*xStep)(sqlite3_context*,int,sqlite3_value**),
157
  void (*xFinal)(sqlite3_context*)
158
) {
159
  return sqlite3_create_function(db, zFunctionName, nArg, eTextRep, (void*) pApp, xFunc, xStep, xFinal);
160
}
161

162
void callbackTrampoline(sqlite3_context*, int, sqlite3_value**);
163
void stepTrampoline(sqlite3_context*, int, sqlite3_value**);
164
void doneTrampoline(sqlite3_context*);
165

166
int compareTrampoline(void*, int, char*, int, char*);
167
int commitHookTrampoline(void*);
168
void rollbackHookTrampoline(void*);
169
void updateHookTrampoline(void*, int, char*, char*, sqlite3_int64);
170

171
int authorizerTrampoline(void*, int, char*, char*, char*, char*);
172

173
#ifdef SQLITE_LIMIT_WORKER_THREADS
174
# define _SQLITE_HAS_LIMIT
175
# define SQLITE_LIMIT_LENGTH                    0
176
# define SQLITE_LIMIT_SQL_LENGTH                1
177
# define SQLITE_LIMIT_COLUMN                    2
178
# define SQLITE_LIMIT_EXPR_DEPTH                3
179
# define SQLITE_LIMIT_COMPOUND_SELECT           4
180
# define SQLITE_LIMIT_VDBE_OP                   5
181
# define SQLITE_LIMIT_FUNCTION_ARG              6
182
# define SQLITE_LIMIT_ATTACHED                  7
183
# define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
184
# define SQLITE_LIMIT_VARIABLE_NUMBER           9
185
# define SQLITE_LIMIT_TRIGGER_DEPTH            10
186
# define SQLITE_LIMIT_WORKER_THREADS           11
187
# else
188
# define SQLITE_LIMIT_WORKER_THREADS           11
189
#endif
190

191
static int _sqlite3_limit(sqlite3* db, int limitId, int newLimit) {
192
#ifndef _SQLITE_HAS_LIMIT
193
  return -1;
194
#else
195
  return sqlite3_limit(db, limitId, newLimit);
196
#endif
197
}
198

199
#if SQLITE_VERSION_NUMBER < 3012000
200
static int sqlite3_system_errno(sqlite3 *db) {
201
  return 0;
202
}
203
#endif
204
*/
205
import "C"
206
import (
207
	"context"
208
	"database/sql"
209
	"database/sql/driver"
210
	"errors"
211
	"fmt"
212
	"io"
213
	"net/url"
214
	"reflect"
215
	"runtime"
216
	"strconv"
217
	"strings"
218
	"sync"
219
	"syscall"
220
	"time"
221
	"unsafe"
222
)
223

224
// SQLiteTimestampFormats is timestamp formats understood by both this module
225
// and SQLite.  The first format in the slice will be used when saving time
226
// values into the database. When parsing a string from a timestamp or datetime
227
// column, the formats are tried in order.
228
var SQLiteTimestampFormats = []string{
229
	// By default, store timestamps with whatever timezone they come with.
230
	// When parsed, they will be returned with the same timezone.
231
	"2006-01-02 15:04:05.999999999-07:00",
232
	"2006-01-02T15:04:05.999999999-07:00",
233
	"2006-01-02 15:04:05.999999999",
234
	"2006-01-02T15:04:05.999999999",
235
	"2006-01-02 15:04:05",
236
	"2006-01-02T15:04:05",
237
	"2006-01-02 15:04",
238
	"2006-01-02T15:04",
239
	"2006-01-02",
240
}
241

242
const (
243
	columnDate      string = "date"
244
	columnDatetime  string = "datetime"
245
	columnTimestamp string = "timestamp"
246
)
247

248
// This variable can be replaced with -ldflags like below:
249
// go build -ldflags="-X 'github.com/mattn/go-sqlite3.driverName=my-sqlite3'"
250
var driverName = "sqlite3"
251

252
func init() {
253
	if driverName != "" {
254
		sql.Register(driverName, &SQLiteDriver{})
255
	}
256
}
257

258
// Version returns SQLite library version information.
259
func Version() (libVersion string, libVersionNumber int, sourceID string) {
260
	libVersion = C.GoString(C.sqlite3_libversion())
261
	libVersionNumber = int(C.sqlite3_libversion_number())
262
	sourceID = C.GoString(C.sqlite3_sourceid())
263
	return libVersion, libVersionNumber, sourceID
264
}
265

266
const (
267
	// used by authorizer and pre_update_hook
268
	SQLITE_DELETE = C.SQLITE_DELETE
269
	SQLITE_INSERT = C.SQLITE_INSERT
270
	SQLITE_UPDATE = C.SQLITE_UPDATE
271

272
	// used by authorzier - as return value
273
	SQLITE_OK     = C.SQLITE_OK
274
	SQLITE_IGNORE = C.SQLITE_IGNORE
275
	SQLITE_DENY   = C.SQLITE_DENY
276

277
	// different actions query tries to do - passed as argument to authorizer
278
	SQLITE_CREATE_INDEX        = C.SQLITE_CREATE_INDEX
279
	SQLITE_CREATE_TABLE        = C.SQLITE_CREATE_TABLE
280
	SQLITE_CREATE_TEMP_INDEX   = C.SQLITE_CREATE_TEMP_INDEX
281
	SQLITE_CREATE_TEMP_TABLE   = C.SQLITE_CREATE_TEMP_TABLE
282
	SQLITE_CREATE_TEMP_TRIGGER = C.SQLITE_CREATE_TEMP_TRIGGER
283
	SQLITE_CREATE_TEMP_VIEW    = C.SQLITE_CREATE_TEMP_VIEW
284
	SQLITE_CREATE_TRIGGER      = C.SQLITE_CREATE_TRIGGER
285
	SQLITE_CREATE_VIEW         = C.SQLITE_CREATE_VIEW
286
	SQLITE_CREATE_VTABLE       = C.SQLITE_CREATE_VTABLE
287
	SQLITE_DROP_INDEX          = C.SQLITE_DROP_INDEX
288
	SQLITE_DROP_TABLE          = C.SQLITE_DROP_TABLE
289
	SQLITE_DROP_TEMP_INDEX     = C.SQLITE_DROP_TEMP_INDEX
290
	SQLITE_DROP_TEMP_TABLE     = C.SQLITE_DROP_TEMP_TABLE
291
	SQLITE_DROP_TEMP_TRIGGER   = C.SQLITE_DROP_TEMP_TRIGGER
292
	SQLITE_DROP_TEMP_VIEW      = C.SQLITE_DROP_TEMP_VIEW
293
	SQLITE_DROP_TRIGGER        = C.SQLITE_DROP_TRIGGER
294
	SQLITE_DROP_VIEW           = C.SQLITE_DROP_VIEW
295
	SQLITE_DROP_VTABLE         = C.SQLITE_DROP_VTABLE
296
	SQLITE_PRAGMA              = C.SQLITE_PRAGMA
297
	SQLITE_READ                = C.SQLITE_READ
298
	SQLITE_SELECT              = C.SQLITE_SELECT
299
	SQLITE_TRANSACTION         = C.SQLITE_TRANSACTION
300
	SQLITE_ATTACH              = C.SQLITE_ATTACH
301
	SQLITE_DETACH              = C.SQLITE_DETACH
302
	SQLITE_ALTER_TABLE         = C.SQLITE_ALTER_TABLE
303
	SQLITE_REINDEX             = C.SQLITE_REINDEX
304
	SQLITE_ANALYZE             = C.SQLITE_ANALYZE
305
	SQLITE_FUNCTION            = C.SQLITE_FUNCTION
306
	SQLITE_SAVEPOINT           = C.SQLITE_SAVEPOINT
307
	SQLITE_COPY                = C.SQLITE_COPY
308
	/*SQLITE_RECURSIVE           = C.SQLITE_RECURSIVE*/
309
)
310

311
// Standard File Control Opcodes
312
// See: https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html
313
const (
314
	SQLITE_FCNTL_LOCKSTATE             = int(1)
315
	SQLITE_FCNTL_GET_LOCKPROXYFILE     = int(2)
316
	SQLITE_FCNTL_SET_LOCKPROXYFILE     = int(3)
317
	SQLITE_FCNTL_LAST_ERRNO            = int(4)
318
	SQLITE_FCNTL_SIZE_HINT             = int(5)
319
	SQLITE_FCNTL_CHUNK_SIZE            = int(6)
320
	SQLITE_FCNTL_FILE_POINTER          = int(7)
321
	SQLITE_FCNTL_SYNC_OMITTED          = int(8)
322
	SQLITE_FCNTL_WIN32_AV_RETRY        = int(9)
323
	SQLITE_FCNTL_PERSIST_WAL           = int(10)
324
	SQLITE_FCNTL_OVERWRITE             = int(11)
325
	SQLITE_FCNTL_VFSNAME               = int(12)
326
	SQLITE_FCNTL_POWERSAFE_OVERWRITE   = int(13)
327
	SQLITE_FCNTL_PRAGMA                = int(14)
328
	SQLITE_FCNTL_BUSYHANDLER           = int(15)
329
	SQLITE_FCNTL_TEMPFILENAME          = int(16)
330
	SQLITE_FCNTL_MMAP_SIZE             = int(18)
331
	SQLITE_FCNTL_TRACE                 = int(19)
332
	SQLITE_FCNTL_HAS_MOVED             = int(20)
333
	SQLITE_FCNTL_SYNC                  = int(21)
334
	SQLITE_FCNTL_COMMIT_PHASETWO       = int(22)
335
	SQLITE_FCNTL_WIN32_SET_HANDLE      = int(23)
336
	SQLITE_FCNTL_WAL_BLOCK             = int(24)
337
	SQLITE_FCNTL_ZIPVFS                = int(25)
338
	SQLITE_FCNTL_RBU                   = int(26)
339
	SQLITE_FCNTL_VFS_POINTER           = int(27)
340
	SQLITE_FCNTL_JOURNAL_POINTER       = int(28)
341
	SQLITE_FCNTL_WIN32_GET_HANDLE      = int(29)
342
	SQLITE_FCNTL_PDB                   = int(30)
343
	SQLITE_FCNTL_BEGIN_ATOMIC_WRITE    = int(31)
344
	SQLITE_FCNTL_COMMIT_ATOMIC_WRITE   = int(32)
345
	SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE = int(33)
346
	SQLITE_FCNTL_LOCK_TIMEOUT          = int(34)
347
	SQLITE_FCNTL_DATA_VERSION          = int(35)
348
	SQLITE_FCNTL_SIZE_LIMIT            = int(36)
349
	SQLITE_FCNTL_CKPT_DONE             = int(37)
350
	SQLITE_FCNTL_RESERVE_BYTES         = int(38)
351
	SQLITE_FCNTL_CKPT_START            = int(39)
352
	SQLITE_FCNTL_EXTERNAL_READER       = int(40)
353
	SQLITE_FCNTL_CKSM_FILE             = int(41)
354
)
355

356
// SQLiteDriver implements driver.Driver.
357
type SQLiteDriver struct {
358
	Extensions  []string
359
	ConnectHook func(*SQLiteConn) error
360
}
361

362
// SQLiteConn implements driver.Conn.
363
type SQLiteConn struct {
364
	mu          sync.Mutex
365
	db          *C.sqlite3
366
	loc         *time.Location
367
	txlock      string
368
	funcs       []*functionInfo
369
	aggregators []*aggInfo
370
}
371

372
// SQLiteTx implements driver.Tx.
373
type SQLiteTx struct {
374
	c *SQLiteConn
375
}
376

377
// SQLiteStmt implements driver.Stmt.
378
type SQLiteStmt struct {
379
	mu     sync.Mutex
380
	c      *SQLiteConn
381
	s      *C.sqlite3_stmt
382
	t      string
383
	closed bool
384
	cls    bool
385
}
386

387
// SQLiteResult implements sql.Result.
388
type SQLiteResult struct {
389
	id      int64
390
	changes int64
391
}
392

393
// SQLiteRows implements driver.Rows.
394
type SQLiteRows struct {
395
	s        *SQLiteStmt
396
	nc       int
397
	cols     []string
398
	decltype []string
399
	cls      bool
400
	closed   bool
401
	ctx      context.Context // no better alternative to pass context into Next() method
402
}
403

404
type functionInfo struct {
405
	f                 reflect.Value
406
	argConverters     []callbackArgConverter
407
	variadicConverter callbackArgConverter
408
	retConverter      callbackRetConverter
409
}
410

411
func (fi *functionInfo) Call(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
412
	args, err := callbackConvertArgs(argv, fi.argConverters, fi.variadicConverter)
413
	if err != nil {
414
		callbackError(ctx, err)
415
		return
416
	}
417

418
	ret := fi.f.Call(args)
419

420
	if len(ret) == 2 && ret[1].Interface() != nil {
421
		callbackError(ctx, ret[1].Interface().(error))
422
		return
423
	}
424

425
	err = fi.retConverter(ctx, ret[0])
426
	if err != nil {
427
		callbackError(ctx, err)
428
		return
429
	}
430
}
431

432
type aggInfo struct {
433
	constructor reflect.Value
434

435
	// Active aggregator objects for aggregations in flight. The
436
	// aggregators are indexed by a counter stored in the aggregation
437
	// user data space provided by sqlite.
438
	active map[int64]reflect.Value
439
	next   int64
440

441
	stepArgConverters     []callbackArgConverter
442
	stepVariadicConverter callbackArgConverter
443

444
	doneRetConverter callbackRetConverter
445
}
446

447
func (ai *aggInfo) agg(ctx *C.sqlite3_context) (int64, reflect.Value, error) {
448
	aggIdx := (*int64)(C.sqlite3_aggregate_context(ctx, C.int(8)))
449
	if *aggIdx == 0 {
450
		*aggIdx = ai.next
451
		ret := ai.constructor.Call(nil)
452
		if len(ret) == 2 && ret[1].Interface() != nil {
453
			return 0, reflect.Value{}, ret[1].Interface().(error)
454
		}
455
		if ret[0].IsNil() {
456
			return 0, reflect.Value{}, errors.New("aggregator constructor returned nil state")
457
		}
458
		ai.next++
459
		ai.active[*aggIdx] = ret[0]
460
	}
461
	return *aggIdx, ai.active[*aggIdx], nil
462
}
463

464
func (ai *aggInfo) Step(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
465
	_, agg, err := ai.agg(ctx)
466
	if err != nil {
467
		callbackError(ctx, err)
468
		return
469
	}
470

471
	args, err := callbackConvertArgs(argv, ai.stepArgConverters, ai.stepVariadicConverter)
472
	if err != nil {
473
		callbackError(ctx, err)
474
		return
475
	}
476

477
	ret := agg.MethodByName("Step").Call(args)
478
	if len(ret) == 1 && ret[0].Interface() != nil {
479
		callbackError(ctx, ret[0].Interface().(error))
480
		return
481
	}
482
}
483

484
func (ai *aggInfo) Done(ctx *C.sqlite3_context) {
485
	idx, agg, err := ai.agg(ctx)
486
	if err != nil {
487
		callbackError(ctx, err)
488
		return
489
	}
490
	defer func() { delete(ai.active, idx) }()
491

492
	ret := agg.MethodByName("Done").Call(nil)
493
	if len(ret) == 2 && ret[1].Interface() != nil {
494
		callbackError(ctx, ret[1].Interface().(error))
495
		return
496
	}
497

498
	err = ai.doneRetConverter(ctx, ret[0])
499
	if err != nil {
500
		callbackError(ctx, err)
501
		return
502
	}
503
}
504

505
// Commit transaction.
506
func (tx *SQLiteTx) Commit() error {
507
	_, err := tx.c.exec(context.Background(), "COMMIT", nil)
508
	if err != nil {
509
		// sqlite3 may leave the transaction open in this scenario.
510
		// However, database/sql considers the transaction complete once we
511
		// return from Commit() - we must clean up to honour its semantics.
512
		// We don't know if the ROLLBACK is strictly necessary, but according
513
		// to sqlite's docs, there is no harm in calling ROLLBACK unnecessarily.
514
		tx.c.exec(context.Background(), "ROLLBACK", nil)
515
	}
516
	return err
517
}
518

519
// Rollback transaction.
520
func (tx *SQLiteTx) Rollback() error {
521
	_, err := tx.c.exec(context.Background(), "ROLLBACK", nil)
522
	return err
523
}
524

525
// RegisterCollation makes a Go function available as a collation.
526
//
527
// cmp receives two UTF-8 strings, a and b. The result should be 0 if
528
// a==b, -1 if a < b, and +1 if a > b.
529
//
530
// cmp must always return the same result given the same
531
// inputs. Additionally, it must have the following properties for all
532
// strings A, B and C: if A==B then B==A; if A==B and B==C then A==C;
533
// if A<B then B>A; if A<B and B<C then A<C.
534
//
535
// If cmp does not obey these constraints, sqlite3's behavior is
536
// undefined when the collation is used.
537
func (c *SQLiteConn) RegisterCollation(name string, cmp func(string, string) int) error {
538
	handle := newHandle(c, cmp)
539
	cname := C.CString(name)
540
	defer C.free(unsafe.Pointer(cname))
541
	rv := C.sqlite3_create_collation(c.db, cname, C.SQLITE_UTF8, handle, (*[0]byte)(unsafe.Pointer(C.compareTrampoline)))
542
	if rv != C.SQLITE_OK {
543
		return c.lastError()
544
	}
545
	return nil
546
}
547

548
// RegisterCommitHook sets the commit hook for a connection.
549
//
550
// If the callback returns non-zero the transaction will become a rollback.
551
//
552
// If there is an existing commit hook for this connection, it will be
553
// removed. If callback is nil the existing hook (if any) will be removed
554
// without creating a new one.
555
func (c *SQLiteConn) RegisterCommitHook(callback func() int) {
556
	if callback == nil {
557
		C.sqlite3_commit_hook(c.db, nil, nil)
558
	} else {
559
		C.sqlite3_commit_hook(c.db, (*[0]byte)(C.commitHookTrampoline), newHandle(c, callback))
560
	}
561
}
562

563
// RegisterRollbackHook sets the rollback hook for a connection.
564
//
565
// If there is an existing rollback hook for this connection, it will be
566
// removed. If callback is nil the existing hook (if any) will be removed
567
// without creating a new one.
568
func (c *SQLiteConn) RegisterRollbackHook(callback func()) {
569
	if callback == nil {
570
		C.sqlite3_rollback_hook(c.db, nil, nil)
571
	} else {
572
		C.sqlite3_rollback_hook(c.db, (*[0]byte)(C.rollbackHookTrampoline), newHandle(c, callback))
573
	}
574
}
575

576
// RegisterUpdateHook sets the update hook for a connection.
577
//
578
// The parameters to the callback are the operation (one of the constants
579
// SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE), the database name, the
580
// table name, and the rowid.
581
//
582
// If there is an existing update hook for this connection, it will be
583
// removed. If callback is nil the existing hook (if any) will be removed
584
// without creating a new one.
585
func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64)) {
586
	if callback == nil {
587
		C.sqlite3_update_hook(c.db, nil, nil)
588
	} else {
589
		C.sqlite3_update_hook(c.db, (*[0]byte)(C.updateHookTrampoline), newHandle(c, callback))
590
	}
591
}
592

593
// RegisterAuthorizer sets the authorizer for connection.
594
//
595
// The parameters to the callback are the operation (one of the constants
596
// SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE), and 1 to 3 arguments,
597
// depending on operation. More details see:
598
// https://www.sqlite.org/c3ref/c_alter_table.html
599
func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, string) int) {
600
	if callback == nil {
601
		C.sqlite3_set_authorizer(c.db, nil, nil)
602
	} else {
603
		C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), newHandle(c, callback))
604
	}
605
}
606

607
// RegisterFunc makes a Go function available as a SQLite function.
608
//
609
// The Go function can have arguments of the following types: any
610
// numeric type except complex, bool, []byte, string and any.
611
// any arguments are given the direct translation of the SQLite data type:
612
// int64 for INTEGER, float64 for FLOAT, []byte for BLOB, string for TEXT.
613
//
614
// The function can additionally be variadic, as long as the type of
615
// the variadic argument is one of the above.
616
//
617
// If pure is true. SQLite will assume that the function's return
618
// value depends only on its inputs, and make more aggressive
619
// optimizations in its queries.
620
//
621
// See _example/go_custom_funcs for a detailed example.
622
func (c *SQLiteConn) RegisterFunc(name string, impl any, pure bool) error {
623
	var fi functionInfo
624
	fi.f = reflect.ValueOf(impl)
625
	t := fi.f.Type()
626
	if t.Kind() != reflect.Func {
627
		return errors.New("Non-function passed to RegisterFunc")
628
	}
629
	if t.NumOut() != 1 && t.NumOut() != 2 {
630
		return errors.New("SQLite functions must return 1 or 2 values")
631
	}
632
	if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
633
		return errors.New("Second return value of SQLite function must be error")
634
	}
635

636
	numArgs := t.NumIn()
637
	if t.IsVariadic() {
638
		numArgs--
639
	}
640

641
	for i := 0; i < numArgs; i++ {
642
		conv, err := callbackArg(t.In(i))
643
		if err != nil {
644
			return err
645
		}
646
		fi.argConverters = append(fi.argConverters, conv)
647
	}
648

649
	if t.IsVariadic() {
650
		conv, err := callbackArg(t.In(numArgs).Elem())
651
		if err != nil {
652
			return err
653
		}
654
		fi.variadicConverter = conv
655
		// Pass -1 to sqlite so that it allows any number of
656
		// arguments. The call helper verifies that the minimum number
657
		// of arguments is present for variadic functions.
658
		numArgs = -1
659
	}
660

661
	conv, err := callbackRet(t.Out(0))
662
	if err != nil {
663
		return err
664
	}
665
	fi.retConverter = conv
666

667
	// fi must outlast the database connection, or we'll have dangling pointers.
668
	c.funcs = append(c.funcs, &fi)
669

670
	cname := C.CString(name)
671
	defer C.free(unsafe.Pointer(cname))
672
	opts := C.SQLITE_UTF8
673
	if pure {
674
		opts |= C.SQLITE_DETERMINISTIC
675
	}
676
	rv := sqlite3CreateFunction(c.db, cname, C.int(numArgs), C.int(opts), newHandle(c, &fi), C.callbackTrampoline, nil, nil)
677
	if rv != C.SQLITE_OK {
678
		return c.lastError()
679
	}
680
	return nil
681
}
682

683
func sqlite3CreateFunction(db *C.sqlite3, zFunctionName *C.char, nArg C.int, eTextRep C.int, pApp unsafe.Pointer, xFunc unsafe.Pointer, xStep unsafe.Pointer, xFinal unsafe.Pointer) C.int {
684
	return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(uintptr(pApp)), (*[0]byte)(xFunc), (*[0]byte)(xStep), (*[0]byte)(xFinal))
685
}
686

687
// RegisterAggregator makes a Go type available as a SQLite aggregation function.
688
//
689
// Because aggregation is incremental, it's implemented in Go with a
690
// type that has 2 methods: func Step(values) accumulates one row of
691
// data into the accumulator, and func Done() ret finalizes and
692
// returns the aggregate value. "values" and "ret" may be any type
693
// supported by RegisterFunc.
694
//
695
// RegisterAggregator takes as implementation a constructor function
696
// that constructs an instance of the aggregator type each time an
697
// aggregation begins. The constructor must return a pointer to a
698
// type, or an interface that implements Step() and Done().
699
//
700
// The constructor function and the Step/Done methods may optionally
701
// return an error in addition to their other return values.
702
//
703
// See _example/go_custom_funcs for a detailed example.
704
func (c *SQLiteConn) RegisterAggregator(name string, impl any, pure bool) error {
705
	var ai aggInfo
706
	ai.constructor = reflect.ValueOf(impl)
707
	t := ai.constructor.Type()
708
	if t.Kind() != reflect.Func {
709
		return errors.New("non-function passed to RegisterAggregator")
710
	}
711
	if t.NumOut() != 1 && t.NumOut() != 2 {
712
		return errors.New("SQLite aggregator constructors must return 1 or 2 values")
713
	}
714
	if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
715
		return errors.New("Second return value of SQLite function must be error")
716
	}
717
	if t.NumIn() != 0 {
718
		return errors.New("SQLite aggregator constructors must not have arguments")
719
	}
720

721
	agg := t.Out(0)
722
	switch agg.Kind() {
723
	case reflect.Ptr, reflect.Interface:
724
	default:
725
		return errors.New("SQlite aggregator constructor must return a pointer object")
726
	}
727
	stepFn, found := agg.MethodByName("Step")
728
	if !found {
729
		return errors.New("SQlite aggregator doesn't have a Step() function")
730
	}
731
	step := stepFn.Type
732
	if step.NumOut() != 0 && step.NumOut() != 1 {
733
		return errors.New("SQlite aggregator Step() function must return 0 or 1 values")
734
	}
735
	if step.NumOut() == 1 && !step.Out(0).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
736
		return errors.New("type of SQlite aggregator Step() return value must be error")
737
	}
738

739
	stepNArgs := step.NumIn()
740
	start := 0
741
	if agg.Kind() == reflect.Ptr {
742
		// Skip over the method receiver
743
		stepNArgs--
744
		start++
745
	}
746
	if step.IsVariadic() {
747
		stepNArgs--
748
	}
749
	for i := start; i < start+stepNArgs; i++ {
750
		conv, err := callbackArg(step.In(i))
751
		if err != nil {
752
			return err
753
		}
754
		ai.stepArgConverters = append(ai.stepArgConverters, conv)
755
	}
756
	if step.IsVariadic() {
757
		conv, err := callbackArg(step.In(start + stepNArgs).Elem())
758
		if err != nil {
759
			return err
760
		}
761
		ai.stepVariadicConverter = conv
762
		// Pass -1 to sqlite so that it allows any number of
763
		// arguments. The call helper verifies that the minimum number
764
		// of arguments is present for variadic functions.
765
		stepNArgs = -1
766
	}
767

768
	doneFn, found := agg.MethodByName("Done")
769
	if !found {
770
		return errors.New("SQlite aggregator doesn't have a Done() function")
771
	}
772
	done := doneFn.Type
773
	doneNArgs := done.NumIn()
774
	if agg.Kind() == reflect.Ptr {
775
		// Skip over the method receiver
776
		doneNArgs--
777
	}
778
	if doneNArgs != 0 {
779
		return errors.New("SQlite aggregator Done() function must have no arguments")
780
	}
781
	if done.NumOut() != 1 && done.NumOut() != 2 {
782
		return errors.New("SQLite aggregator Done() function must return 1 or 2 values")
783
	}
784
	if done.NumOut() == 2 && !done.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
785
		return errors.New("second return value of SQLite aggregator Done() function must be error")
786
	}
787

788
	conv, err := callbackRet(done.Out(0))
789
	if err != nil {
790
		return err
791
	}
792
	ai.doneRetConverter = conv
793
	ai.active = make(map[int64]reflect.Value)
794
	ai.next = 1
795

796
	// ai must outlast the database connection, or we'll have dangling pointers.
797
	c.aggregators = append(c.aggregators, &ai)
798

799
	cname := C.CString(name)
800
	defer C.free(unsafe.Pointer(cname))
801
	opts := C.SQLITE_UTF8
802
	if pure {
803
		opts |= C.SQLITE_DETERMINISTIC
804
	}
805
	rv := sqlite3CreateFunction(c.db, cname, C.int(stepNArgs), C.int(opts), newHandle(c, &ai), nil, C.stepTrampoline, C.doneTrampoline)
806
	if rv != C.SQLITE_OK {
807
		return c.lastError()
808
	}
809
	return nil
810
}
811

812
// AutoCommit return which currently auto commit or not.
813
func (c *SQLiteConn) AutoCommit() bool {
814
	c.mu.Lock()
815
	defer c.mu.Unlock()
816
	return int(C.sqlite3_get_autocommit(c.db)) != 0
817
}
818

819
func (c *SQLiteConn) lastError() error {
820
	return lastError(c.db)
821
}
822

823
// Note: may be called with db == nil
824
func lastError(db *C.sqlite3) error {
825
	rv := C.sqlite3_errcode(db) // returns SQLITE_NOMEM if db == nil
826
	if rv == C.SQLITE_OK {
827
		return nil
828
	}
829
	extrv := C.sqlite3_extended_errcode(db)    // returns SQLITE_NOMEM if db == nil
830
	errStr := C.GoString(C.sqlite3_errmsg(db)) // returns "out of memory" if db == nil
831

832
	// https://www.sqlite.org/c3ref/system_errno.html
833
	// sqlite3_system_errno is only meaningful if the error code was SQLITE_CANTOPEN,
834
	// or it was SQLITE_IOERR and the extended code was not SQLITE_IOERR_NOMEM
835
	var systemErrno syscall.Errno
836
	if rv == C.SQLITE_CANTOPEN || (rv == C.SQLITE_IOERR && extrv != C.SQLITE_IOERR_NOMEM) {
837
		systemErrno = syscall.Errno(C.sqlite3_system_errno(db))
838
	}
839

840
	return Error{
841
		Code:         ErrNo(rv),
842
		ExtendedCode: ErrNoExtended(extrv),
843
		SystemErrno:  systemErrno,
844
		err:          errStr,
845
	}
846
}
847

848
// Exec implements Execer.
849
func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
850
	list := make([]driver.NamedValue, len(args))
851
	for i, v := range args {
852
		list[i] = driver.NamedValue{
853
			Ordinal: i + 1,
854
			Value:   v,
855
		}
856
	}
857
	return c.exec(context.Background(), query, list)
858
}
859

860
func (c *SQLiteConn) exec(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
861
	start := 0
862
	for {
863
		s, err := c.prepare(ctx, query)
864
		if err != nil {
865
			return nil, err
866
		}
867
		var res driver.Result
868
		if s.(*SQLiteStmt).s != nil {
869
			stmtArgs := make([]driver.NamedValue, 0, len(args))
870
			na := s.NumInput()
871
			if len(args)-start < na {
872
				s.Close()
873
				return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
874
			}
875
			// consume the number of arguments used in the current
876
			// statement and append all named arguments not
877
			// contained therein
878
			if len(args[start:start+na]) > 0 {
879
				stmtArgs = append(stmtArgs, args[start:start+na]...)
880
				for i := range args {
881
					if (i < start || i >= na) && args[i].Name != "" {
882
						stmtArgs = append(stmtArgs, args[i])
883
					}
884
				}
885
				for i := range stmtArgs {
886
					stmtArgs[i].Ordinal = i + 1
887
				}
888
			}
889
			res, err = s.(*SQLiteStmt).exec(ctx, stmtArgs)
890
			if err != nil && err != driver.ErrSkip {
891
				s.Close()
892
				return nil, err
893
			}
894
			start += na
895
		}
896
		tail := s.(*SQLiteStmt).t
897
		s.Close()
898
		if tail == "" {
899
			if res == nil {
900
				// https://github.com/mattn/go-sqlite3/issues/963
901
				res = &SQLiteResult{0, 0}
902
			}
903
			return res, nil
904
		}
905
		query = tail
906
	}
907
}
908

909
// Query implements Queryer.
910
func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
911
	list := make([]driver.NamedValue, len(args))
912
	for i, v := range args {
913
		list[i] = driver.NamedValue{
914
			Ordinal: i + 1,
915
			Value:   v,
916
		}
917
	}
918
	return c.query(context.Background(), query, list)
919
}
920

921
func (c *SQLiteConn) query(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
922
	start := 0
923
	for {
924
		stmtArgs := make([]driver.NamedValue, 0, len(args))
925
		s, err := c.prepare(ctx, query)
926
		if err != nil {
927
			return nil, err
928
		}
929
		s.(*SQLiteStmt).cls = true
930
		na := s.NumInput()
931
		if len(args)-start < na {
932
			return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args)-start)
933
		}
934
		// consume the number of arguments used in the current
935
		// statement and append all named arguments not contained
936
		// therein
937
		stmtArgs = append(stmtArgs, args[start:start+na]...)
938
		for i := range args {
939
			if (i < start || i >= na) && args[i].Name != "" {
940
				stmtArgs = append(stmtArgs, args[i])
941
			}
942
		}
943
		for i := range stmtArgs {
944
			stmtArgs[i].Ordinal = i + 1
945
		}
946
		rows, err := s.(*SQLiteStmt).query(ctx, stmtArgs)
947
		if err != nil && err != driver.ErrSkip {
948
			s.Close()
949
			return rows, err
950
		}
951
		start += na
952
		tail := s.(*SQLiteStmt).t
953
		if tail == "" {
954
			return rows, nil
955
		}
956
		rows.Close()
957
		s.Close()
958
		query = tail
959
	}
960
}
961

962
// Begin transaction.
963
func (c *SQLiteConn) Begin() (driver.Tx, error) {
964
	return c.begin(context.Background())
965
}
966

967
func (c *SQLiteConn) begin(ctx context.Context) (driver.Tx, error) {
968
	if _, err := c.exec(ctx, c.txlock, nil); err != nil {
969
		return nil, err
970
	}
971
	return &SQLiteTx{c}, nil
972
}
973

974
// Open database and return a new connection.
975
//
976
// A pragma can take either zero or one argument.
977
// The argument is may be either in parentheses or it may be separated from
978
// the pragma name by an equal sign. The two syntaxes yield identical results.
979
// In many pragmas, the argument is a boolean. The boolean can be one of:
980
//
981
//	1 yes true on
982
//	0 no false off
983
//
984
// You can specify a DSN string using a URI as the filename.
985
//
986
//	test.db
987
//	file:test.db?cache=shared&mode=memory
988
//	:memory:
989
//	file::memory:
990
//
991
//	mode
992
//	  Access mode of the database.
993
//	  https://www.sqlite.org/c3ref/open.html
994
//	  Values:
995
//	   - ro
996
//	   - rw
997
//	   - rwc
998
//	   - memory
999
//
1000
//	cache
1001
//	  SQLite Shared-Cache Mode
1002
//	  https://www.sqlite.org/sharedcache.html
1003
//	  Values:
1004
//	    - shared
1005
//	    - private
1006
//
1007
//	immutable=Boolean
1008
//	  The immutable parameter is a boolean query parameter that indicates
1009
//	  that the database file is stored on read-only media. When immutable is set,
1010
//	  SQLite assumes that the database file cannot be changed,
1011
//	  even by a process with higher privilege,
1012
//	  and so the database is opened read-only and all locking and change detection is disabled.
1013
//	  Caution: Setting the immutable property on a database file that
1014
//	  does in fact change can result in incorrect query results and/or SQLITE_CORRUPT errors.
1015
//
1016
// go-sqlite3 adds the following query parameters to those used by SQLite:
1017
//
1018
//	_loc=XXX
1019
//	  Specify location of time format. It's possible to specify "auto".
1020
//
1021
//	_mutex=XXX
1022
//	  Specify mutex mode. XXX can be "no", "full".
1023
//
1024
//	_txlock=XXX
1025
//	  Specify locking behavior for transactions.  XXX can be "immediate",
1026
//	  "deferred", "exclusive".
1027
//
1028
//	_auto_vacuum=X | _vacuum=X
1029
//	  0 | none - Auto Vacuum disabled
1030
//	  1 | full - Auto Vacuum FULL
1031
//	  2 | incremental - Auto Vacuum Incremental
1032
//
1033
//	_busy_timeout=XXX"| _timeout=XXX
1034
//	  Specify value for sqlite3_busy_timeout.
1035
//
1036
//	_case_sensitive_like=Boolean | _cslike=Boolean
1037
//	  https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
1038
//	  Default or disabled the LIKE operation is case-insensitive.
1039
//	  When enabling this options behaviour of LIKE will become case-sensitive.
1040
//
1041
//	_defer_foreign_keys=Boolean | _defer_fk=Boolean
1042
//	  Defer Foreign Keys until outermost transaction is committed.
1043
//
1044
//	_foreign_keys=Boolean | _fk=Boolean
1045
//	  Enable or disable enforcement of foreign keys.
1046
//
1047
//	_ignore_check_constraints=Boolean
1048
//	  This pragma enables or disables the enforcement of CHECK constraints.
1049
//	  The default setting is off, meaning that CHECK constraints are enforced by default.
1050
//
1051
//	_journal_mode=MODE | _journal=MODE
1052
//	  Set journal mode for the databases associated with the current connection.
1053
//	  https://www.sqlite.org/pragma.html#pragma_journal_mode
1054
//
1055
//	_locking_mode=X | _locking=X
1056
//	  Sets the database connection locking-mode.
1057
//	  The locking-mode is either NORMAL or EXCLUSIVE.
1058
//	  https://www.sqlite.org/pragma.html#pragma_locking_mode
1059
//
1060
//	_query_only=Boolean
1061
//	  The query_only pragma prevents all changes to database files when enabled.
1062
//
1063
//	_recursive_triggers=Boolean | _rt=Boolean
1064
//	  Enable or disable recursive triggers.
1065
//
1066
//	_secure_delete=Boolean|FAST
1067
//	  When secure_delete is on, SQLite overwrites deleted content with zeros.
1068
//	  https://www.sqlite.org/pragma.html#pragma_secure_delete
1069
//
1070
//	_synchronous=X | _sync=X
1071
//	  Change the setting of the "synchronous" flag.
1072
//	  https://www.sqlite.org/pragma.html#pragma_synchronous
1073
//
1074
//	_writable_schema=Boolean
1075
//	  When this pragma is on, the SQLITE_MASTER tables in which database
1076
//	  can be changed using ordinary UPDATE, INSERT, and DELETE statements.
1077
//	  Warning: misuse of this pragma can easily result in a corrupt database file.
1078
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
1079
	if C.sqlite3_threadsafe() == 0 {
1080
		return nil, errors.New("sqlite library was not compiled for thread-safe operation")
1081
	}
1082

1083
	var pkey string
1084

1085
	// Options
1086
	var loc *time.Location
1087
	authCreate := false
1088
	authUser := ""
1089
	authPass := ""
1090
	authCrypt := ""
1091
	authSalt := ""
1092
	mutex := C.int(C.SQLITE_OPEN_FULLMUTEX)
1093
	txlock := "BEGIN"
1094

1095
	// PRAGMA's
1096
	autoVacuum := -1
1097
	busyTimeout := 5000
1098
	caseSensitiveLike := -1
1099
	deferForeignKeys := -1
1100
	foreignKeys := -1
1101
	ignoreCheckConstraints := -1
1102
	var journalMode string
1103
	lockingMode := "NORMAL"
1104
	queryOnly := -1
1105
	recursiveTriggers := -1
1106
	secureDelete := "DEFAULT"
1107
	synchronousMode := "NORMAL"
1108
	writableSchema := -1
1109
	vfsName := ""
1110
	var cacheSize *int64
1111

1112
	pos := strings.IndexRune(dsn, '?')
1113
	if pos >= 1 {
1114
		params, err := url.ParseQuery(dsn[pos+1:])
1115
		if err != nil {
1116
			return nil, err
1117
		}
1118

1119
		// Authentication
1120
		if _, ok := params["_auth"]; ok {
1121
			authCreate = true
1122
		}
1123
		if val := params.Get("_auth_user"); val != "" {
1124
			authUser = val
1125
		}
1126
		if val := params.Get("_auth_pass"); val != "" {
1127
			authPass = val
1128
		}
1129
		if val := params.Get("_auth_crypt"); val != "" {
1130
			authCrypt = val
1131
		}
1132
		if val := params.Get("_auth_salt"); val != "" {
1133
			authSalt = val
1134
		}
1135

1136
		// _loc
1137
		if val := params.Get("_loc"); val != "" {
1138
			switch strings.ToLower(val) {
1139
			case "auto":
1140
				loc = time.Local
1141
			default:
1142
				loc, err = time.LoadLocation(val)
1143
				if err != nil {
1144
					return nil, fmt.Errorf("Invalid _loc: %v: %v", val, err)
1145
				}
1146
			}
1147
		}
1148

1149
		// _mutex
1150
		if val := params.Get("_mutex"); val != "" {
1151
			switch strings.ToLower(val) {
1152
			case "no":
1153
				mutex = C.SQLITE_OPEN_NOMUTEX
1154
			case "full":
1155
				mutex = C.SQLITE_OPEN_FULLMUTEX
1156
			default:
1157
				return nil, fmt.Errorf("Invalid _mutex: %v", val)
1158
			}
1159
		}
1160

1161
		// _txlock
1162
		if val := params.Get("_txlock"); val != "" {
1163
			switch strings.ToLower(val) {
1164
			case "immediate":
1165
				txlock = "BEGIN IMMEDIATE"
1166
			case "exclusive":
1167
				txlock = "BEGIN EXCLUSIVE"
1168
			case "deferred":
1169
				txlock = "BEGIN"
1170
			default:
1171
				return nil, fmt.Errorf("Invalid _txlock: %v", val)
1172
			}
1173
		}
1174

1175
		// Auto Vacuum (_vacuum)
1176
		//
1177
		// https://www.sqlite.org/pragma.html#pragma_auto_vacuum
1178
		//
1179
		pkey = "" // Reset pkey
1180
		if _, ok := params["_auto_vacuum"]; ok {
1181
			pkey = "_auto_vacuum"
1182
		}
1183
		if _, ok := params["_vacuum"]; ok {
1184
			pkey = "_vacuum"
1185
		}
1186
		if val := params.Get(pkey); val != "" {
1187
			switch strings.ToLower(val) {
1188
			case "0", "none":
1189
				autoVacuum = 0
1190
			case "1", "full":
1191
				autoVacuum = 1
1192
			case "2", "incremental":
1193
				autoVacuum = 2
1194
			default:
1195
				return nil, fmt.Errorf("Invalid _auto_vacuum: %v, expecting value of '0 NONE 1 FULL 2 INCREMENTAL'", val)
1196
			}
1197
		}
1198

1199
		// Busy Timeout (_busy_timeout)
1200
		//
1201
		// https://www.sqlite.org/pragma.html#pragma_busy_timeout
1202
		//
1203
		pkey = "" // Reset pkey
1204
		if _, ok := params["_busy_timeout"]; ok {
1205
			pkey = "_busy_timeout"
1206
		}
1207
		if _, ok := params["_timeout"]; ok {
1208
			pkey = "_timeout"
1209
		}
1210
		if val := params.Get(pkey); val != "" {
1211
			iv, err := strconv.ParseInt(val, 10, 64)
1212
			if err != nil {
1213
				return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
1214
			}
1215
			busyTimeout = int(iv)
1216
		}
1217

1218
		// Case Sensitive Like (_cslike)
1219
		//
1220
		// https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
1221
		//
1222
		pkey = "" // Reset pkey
1223
		if _, ok := params["_case_sensitive_like"]; ok {
1224
			pkey = "_case_sensitive_like"
1225
		}
1226
		if _, ok := params["_cslike"]; ok {
1227
			pkey = "_cslike"
1228
		}
1229
		if val := params.Get(pkey); val != "" {
1230
			switch strings.ToLower(val) {
1231
			case "0", "no", "false", "off":
1232
				caseSensitiveLike = 0
1233
			case "1", "yes", "true", "on":
1234
				caseSensitiveLike = 1
1235
			default:
1236
				return nil, fmt.Errorf("Invalid _case_sensitive_like: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1237
			}
1238
		}
1239

1240
		// Defer Foreign Keys (_defer_foreign_keys | _defer_fk)
1241
		//
1242
		// https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys
1243
		//
1244
		pkey = "" // Reset pkey
1245
		if _, ok := params["_defer_foreign_keys"]; ok {
1246
			pkey = "_defer_foreign_keys"
1247
		}
1248
		if _, ok := params["_defer_fk"]; ok {
1249
			pkey = "_defer_fk"
1250
		}
1251
		if val := params.Get(pkey); val != "" {
1252
			switch strings.ToLower(val) {
1253
			case "0", "no", "false", "off":
1254
				deferForeignKeys = 0
1255
			case "1", "yes", "true", "on":
1256
				deferForeignKeys = 1
1257
			default:
1258
				return nil, fmt.Errorf("Invalid _defer_foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1259
			}
1260
		}
1261

1262
		// Foreign Keys (_foreign_keys | _fk)
1263
		//
1264
		// https://www.sqlite.org/pragma.html#pragma_foreign_keys
1265
		//
1266
		pkey = "" // Reset pkey
1267
		if _, ok := params["_foreign_keys"]; ok {
1268
			pkey = "_foreign_keys"
1269
		}
1270
		if _, ok := params["_fk"]; ok {
1271
			pkey = "_fk"
1272
		}
1273
		if val := params.Get(pkey); val != "" {
1274
			switch strings.ToLower(val) {
1275
			case "0", "no", "false", "off":
1276
				foreignKeys = 0
1277
			case "1", "yes", "true", "on":
1278
				foreignKeys = 1
1279
			default:
1280
				return nil, fmt.Errorf("Invalid _foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1281
			}
1282
		}
1283

1284
		// Ignore CHECK Constrains (_ignore_check_constraints)
1285
		//
1286
		// https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints
1287
		//
1288
		if val := params.Get("_ignore_check_constraints"); val != "" {
1289
			switch strings.ToLower(val) {
1290
			case "0", "no", "false", "off":
1291
				ignoreCheckConstraints = 0
1292
			case "1", "yes", "true", "on":
1293
				ignoreCheckConstraints = 1
1294
			default:
1295
				return nil, fmt.Errorf("Invalid _ignore_check_constraints: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1296
			}
1297
		}
1298

1299
		// Journal Mode (_journal_mode | _journal)
1300
		//
1301
		// https://www.sqlite.org/pragma.html#pragma_journal_mode
1302
		//
1303
		pkey = "" // Reset pkey
1304
		if _, ok := params["_journal_mode"]; ok {
1305
			pkey = "_journal_mode"
1306
		}
1307
		if _, ok := params["_journal"]; ok {
1308
			pkey = "_journal"
1309
		}
1310
		if val := params.Get(pkey); val != "" {
1311
			switch strings.ToUpper(val) {
1312
			case "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "OFF":
1313
				journalMode = strings.ToUpper(val)
1314
			case "WAL":
1315
				journalMode = strings.ToUpper(val)
1316

1317
				// For WAL Mode set Synchronous Mode to 'NORMAL'
1318
				// See https://www.sqlite.org/pragma.html#pragma_synchronous
1319
				synchronousMode = "NORMAL"
1320
			default:
1321
				return nil, fmt.Errorf("Invalid _journal: %v, expecting value of 'DELETE TRUNCATE PERSIST MEMORY WAL OFF'", val)
1322
			}
1323
		}
1324

1325
		// Locking Mode (_locking)
1326
		//
1327
		// https://www.sqlite.org/pragma.html#pragma_locking_mode
1328
		//
1329
		pkey = "" // Reset pkey
1330
		if _, ok := params["_locking_mode"]; ok {
1331
			pkey = "_locking_mode"
1332
		}
1333
		if _, ok := params["_locking"]; ok {
1334
			pkey = "_locking"
1335
		}
1336
		if val := params.Get(pkey); val != "" {
1337
			switch strings.ToUpper(val) {
1338
			case "NORMAL", "EXCLUSIVE":
1339
				lockingMode = strings.ToUpper(val)
1340
			default:
1341
				return nil, fmt.Errorf("Invalid _locking_mode: %v, expecting value of 'NORMAL EXCLUSIVE", val)
1342
			}
1343
		}
1344

1345
		// Query Only (_query_only)
1346
		//
1347
		// https://www.sqlite.org/pragma.html#pragma_query_only
1348
		//
1349
		if val := params.Get("_query_only"); val != "" {
1350
			switch strings.ToLower(val) {
1351
			case "0", "no", "false", "off":
1352
				queryOnly = 0
1353
			case "1", "yes", "true", "on":
1354
				queryOnly = 1
1355
			default:
1356
				return nil, fmt.Errorf("Invalid _query_only: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1357
			}
1358
		}
1359

1360
		// Recursive Triggers (_recursive_triggers)
1361
		//
1362
		// https://www.sqlite.org/pragma.html#pragma_recursive_triggers
1363
		//
1364
		pkey = "" // Reset pkey
1365
		if _, ok := params["_recursive_triggers"]; ok {
1366
			pkey = "_recursive_triggers"
1367
		}
1368
		if _, ok := params["_rt"]; ok {
1369
			pkey = "_rt"
1370
		}
1371
		if val := params.Get(pkey); val != "" {
1372
			switch strings.ToLower(val) {
1373
			case "0", "no", "false", "off":
1374
				recursiveTriggers = 0
1375
			case "1", "yes", "true", "on":
1376
				recursiveTriggers = 1
1377
			default:
1378
				return nil, fmt.Errorf("Invalid _recursive_triggers: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1379
			}
1380
		}
1381

1382
		// Secure Delete (_secure_delete)
1383
		//
1384
		// https://www.sqlite.org/pragma.html#pragma_secure_delete
1385
		//
1386
		if val := params.Get("_secure_delete"); val != "" {
1387
			switch strings.ToLower(val) {
1388
			case "0", "no", "false", "off":
1389
				secureDelete = "OFF"
1390
			case "1", "yes", "true", "on":
1391
				secureDelete = "ON"
1392
			case "fast":
1393
				secureDelete = "FAST"
1394
			default:
1395
				return nil, fmt.Errorf("Invalid _secure_delete: %v, expecting boolean value of '0 1 false true no yes off on fast'", val)
1396
			}
1397
		}
1398

1399
		// Synchronous Mode (_synchronous | _sync)
1400
		//
1401
		// https://www.sqlite.org/pragma.html#pragma_synchronous
1402
		//
1403
		pkey = "" // Reset pkey
1404
		if _, ok := params["_synchronous"]; ok {
1405
			pkey = "_synchronous"
1406
		}
1407
		if _, ok := params["_sync"]; ok {
1408
			pkey = "_sync"
1409
		}
1410
		if val := params.Get(pkey); val != "" {
1411
			switch strings.ToUpper(val) {
1412
			case "0", "OFF", "1", "NORMAL", "2", "FULL", "3", "EXTRA":
1413
				synchronousMode = strings.ToUpper(val)
1414
			default:
1415
				return nil, fmt.Errorf("Invalid _synchronous: %v, expecting value of '0 OFF 1 NORMAL 2 FULL 3 EXTRA'", val)
1416
			}
1417
		}
1418

1419
		// Writable Schema (_writeable_schema)
1420
		//
1421
		// https://www.sqlite.org/pragma.html#pragma_writeable_schema
1422
		//
1423
		if val := params.Get("_writable_schema"); val != "" {
1424
			switch strings.ToLower(val) {
1425
			case "0", "no", "false", "off":
1426
				writableSchema = 0
1427
			case "1", "yes", "true", "on":
1428
				writableSchema = 1
1429
			default:
1430
				return nil, fmt.Errorf("Invalid _writable_schema: %v, expecting boolean value of '0 1 false true no yes off on'", val)
1431
			}
1432
		}
1433

1434
		// Cache size (_cache_size)
1435
		//
1436
		// https://sqlite.org/pragma.html#pragma_cache_size
1437
		//
1438
		if val := params.Get("_cache_size"); val != "" {
1439
			iv, err := strconv.ParseInt(val, 10, 64)
1440
			if err != nil {
1441
				return nil, fmt.Errorf("Invalid _cache_size: %v: %v", val, err)
1442
			}
1443
			cacheSize = &iv
1444
		}
1445

1446
		if val := params.Get("vfs"); val != "" {
1447
			vfsName = val
1448
		}
1449

1450
		if !strings.HasPrefix(dsn, "file:") {
1451
			dsn = dsn[:pos]
1452
		}
1453
	}
1454

1455
	var db *C.sqlite3
1456
	name := C.CString(dsn)
1457
	defer C.free(unsafe.Pointer(name))
1458
	var vfs *C.char
1459
	if vfsName != "" {
1460
		vfs = C.CString(vfsName)
1461
		defer C.free(unsafe.Pointer(vfs))
1462
	}
1463
	rv := C._sqlite3_open_v2(name, &db,
1464
		mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
1465
		vfs)
1466
	if rv != 0 {
1467
		// Save off the error _before_ closing the database.
1468
		// This is safe even if db is nil.
1469
		err := lastError(db)
1470
		if db != nil {
1471
			C.sqlite3_close_v2(db)
1472
		}
1473
		return nil, err
1474
	}
1475
	if db == nil {
1476
		return nil, errors.New("sqlite succeeded without returning a database")
1477
	}
1478

1479
	exec := func(s string) error {
1480
		cs := C.CString(s)
1481
		rv := C.sqlite3_exec(db, cs, nil, nil, nil)
1482
		C.free(unsafe.Pointer(cs))
1483
		if rv != C.SQLITE_OK {
1484
			return lastError(db)
1485
		}
1486
		return nil
1487
	}
1488

1489
	// Busy timeout
1490
	if err := exec(fmt.Sprintf("PRAGMA busy_timeout = %d;", busyTimeout)); err != nil {
1491
		C.sqlite3_close_v2(db)
1492
		return nil, err
1493
	}
1494

1495
	// USER AUTHENTICATION
1496
	//
1497
	// User Authentication is always performed even when
1498
	// sqlite_userauth is not compiled in, because without user authentication
1499
	// the authentication is a no-op.
1500
	//
1501
	// Workflow
1502
	//	- Authenticate
1503
	//		ON::SUCCESS		=> Continue
1504
	//		ON::SQLITE_AUTH => Return error and exit Open(...)
1505
	//
1506
	//  - Activate User Authentication
1507
	//		Check if the user wants to activate User Authentication.
1508
	//		If so then first create a temporary AuthConn to the database
1509
	//		This is possible because we are already successfully authenticated.
1510
	//
1511
	//	- Check if `sqlite_user`` table exists
1512
	//		YES				=> Add the provided user from DSN as Admin User and
1513
	//						   activate user authentication.
1514
	//		NO				=> Continue
1515
	//
1516

1517
	// Create connection to SQLite
1518
	conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
1519

1520
	// Password Cipher has to be registered before authentication
1521
	if len(authCrypt) > 0 {
1522
		switch strings.ToUpper(authCrypt) {
1523
		case "SHA1":
1524
			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA1, true); err != nil {
1525
				return nil, fmt.Errorf("CryptEncoderSHA1: %s", err)
1526
			}
1527
		case "SSHA1":
1528
			if len(authSalt) == 0 {
1529
				return nil, fmt.Errorf("_auth_crypt=ssha1, requires _auth_salt")
1530
			}
1531
			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA1(authSalt), true); err != nil {
1532
				return nil, fmt.Errorf("CryptEncoderSSHA1: %s", err)
1533
			}
1534
		case "SHA256":
1535
			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA256, true); err != nil {
1536
				return nil, fmt.Errorf("CryptEncoderSHA256: %s", err)
1537
			}
1538
		case "SSHA256":
1539
			if len(authSalt) == 0 {
1540
				return nil, fmt.Errorf("_auth_crypt=ssha256, requires _auth_salt")
1541
			}
1542
			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA256(authSalt), true); err != nil {
1543
				return nil, fmt.Errorf("CryptEncoderSSHA256: %s", err)
1544
			}
1545
		case "SHA384":
1546
			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA384, true); err != nil {
1547
				return nil, fmt.Errorf("CryptEncoderSHA384: %s", err)
1548
			}
1549
		case "SSHA384":
1550
			if len(authSalt) == 0 {
1551
				return nil, fmt.Errorf("_auth_crypt=ssha384, requires _auth_salt")
1552
			}
1553
			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA384(authSalt), true); err != nil {
1554
				return nil, fmt.Errorf("CryptEncoderSSHA384: %s", err)
1555
			}
1556
		case "SHA512":
1557
			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA512, true); err != nil {
1558
				return nil, fmt.Errorf("CryptEncoderSHA512: %s", err)
1559
			}
1560
		case "SSHA512":
1561
			if len(authSalt) == 0 {
1562
				return nil, fmt.Errorf("_auth_crypt=ssha512, requires _auth_salt")
1563
			}
1564
			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA512(authSalt), true); err != nil {
1565
				return nil, fmt.Errorf("CryptEncoderSSHA512: %s", err)
1566
			}
1567
		}
1568
	}
1569

1570
	// Preform Authentication
1571
	if err := conn.Authenticate(authUser, authPass); err != nil {
1572
		return nil, err
1573
	}
1574

1575
	// Register: authenticate
1576
	// Authenticate will perform an authentication of the provided username
1577
	// and password against the database.
1578
	//
1579
	// If a database contains the SQLITE_USER table, then the
1580
	// call to Authenticate must be invoked with an
1581
	// appropriate username and password prior to enable read and write
1582
	//access to the database.
1583
	//
1584
	// Return SQLITE_OK on success or SQLITE_ERROR if the username/password
1585
	// combination is incorrect or unknown.
1586
	//
1587
	// If the SQLITE_USER table is not present in the database file, then
1588
	// this interface is a harmless no-op returnning SQLITE_OK.
1589
	if err := conn.RegisterFunc("authenticate", conn.authenticate, true); err != nil {
1590
		return nil, err
1591
	}
1592
	//
1593
	// Register: auth_user_add
1594
	// auth_user_add can be used (by an admin user only)
1595
	// to create a new user. When called on a no-authentication-required
1596
	// database, this routine converts the database into an authentication-
1597
	// required database, automatically makes the added user an
1598
	// administrator, and logs in the current connection as that user.
1599
	// The AuthUserAdd only works for the "main" database, not
1600
	// for any ATTACH-ed databases. Any call to AuthUserAdd by a
1601
	// non-admin user results in an error.
1602
	if err := conn.RegisterFunc("auth_user_add", conn.authUserAdd, true); err != nil {
1603
		return nil, err
1604
	}
1605
	//
1606
	// Register: auth_user_change
1607
	// auth_user_change can be used to change a users
1608
	// login credentials or admin privilege.  Any user can change their own
1609
	// login credentials. Only an admin user can change another users login
1610
	// credentials or admin privilege setting. No user may change their own
1611
	// admin privilege setting.
1612
	if err := conn.RegisterFunc("auth_user_change", conn.authUserChange, true); err != nil {
1613
		return nil, err
1614
	}
1615
	//
1616
	// Register: auth_user_delete
1617
	// auth_user_delete can be used (by an admin user only)
1618
	// to delete a user. The currently logged-in user cannot be deleted,
1619
	// which guarantees that there is always an admin user and hence that
1620
	// the database cannot be converted into a no-authentication-required
1621
	// database.
1622
	if err := conn.RegisterFunc("auth_user_delete", conn.authUserDelete, true); err != nil {
1623
		return nil, err
1624
	}
1625

1626
	// Register: auth_enabled
1627
	// auth_enabled can be used to check if user authentication is enabled
1628
	if err := conn.RegisterFunc("auth_enabled", conn.authEnabled, true); err != nil {
1629
		return nil, err
1630
	}
1631

1632
	// Auto Vacuum
1633
	// Moved auto_vacuum command, the user preference for auto_vacuum needs to be implemented directly after
1634
	// the authentication and before the sqlite_user table gets created if the user
1635
	// decides to activate User Authentication because
1636
	// auto_vacuum needs to be set before any tables are created
1637
	// and activating user authentication creates the internal table `sqlite_user`.
1638
	if autoVacuum > -1 {
1639
		if err := exec(fmt.Sprintf("PRAGMA auto_vacuum = %d;", autoVacuum)); err != nil {
1640
			C.sqlite3_close_v2(db)
1641
			return nil, err
1642
		}
1643
	}
1644

1645
	// Check if user wants to activate User Authentication
1646
	if authCreate {
1647
		// Before going any further, we need to check that the user
1648
		// has provided an username and password within the DSN.
1649
		// We are not allowed to continue.
1650
		if len(authUser) == 0 {
1651
			return nil, fmt.Errorf("Missing '_auth_user' while user authentication was requested with '_auth'")
1652
		}
1653
		if len(authPass) == 0 {
1654
			return nil, fmt.Errorf("Missing '_auth_pass' while user authentication was requested with '_auth'")
1655
		}
1656

1657
		// Check if User Authentication is Enabled
1658
		authExists := conn.AuthEnabled()
1659
		if !authExists {
1660
			if err := conn.AuthUserAdd(authUser, authPass, true); err != nil {
1661
				return nil, err
1662
			}
1663
		}
1664
	}
1665

1666
	// Case Sensitive LIKE
1667
	if caseSensitiveLike > -1 {
1668
		if err := exec(fmt.Sprintf("PRAGMA case_sensitive_like = %d;", caseSensitiveLike)); err != nil {
1669
			C.sqlite3_close_v2(db)
1670
			return nil, err
1671
		}
1672
	}
1673

1674
	// Defer Foreign Keys
1675
	if deferForeignKeys > -1 {
1676
		if err := exec(fmt.Sprintf("PRAGMA defer_foreign_keys = %d;", deferForeignKeys)); err != nil {
1677
			C.sqlite3_close_v2(db)
1678
			return nil, err
1679
		}
1680
	}
1681

1682
	// Forgein Keys
1683
	if foreignKeys > -1 {
1684
		if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil {
1685
			C.sqlite3_close_v2(db)
1686
			return nil, err
1687
		}
1688
	}
1689

1690
	// Ignore CHECK Constraints
1691
	if ignoreCheckConstraints > -1 {
1692
		if err := exec(fmt.Sprintf("PRAGMA ignore_check_constraints = %d;", ignoreCheckConstraints)); err != nil {
1693
			C.sqlite3_close_v2(db)
1694
			return nil, err
1695
		}
1696
	}
1697

1698
	// Journal Mode
1699
	if journalMode != "" {
1700
		if err := exec(fmt.Sprintf("PRAGMA journal_mode = %s;", journalMode)); err != nil {
1701
			C.sqlite3_close_v2(db)
1702
			return nil, err
1703
		}
1704
	}
1705

1706
	// Locking Mode
1707
	// Because the default is NORMAL and this is not changed in this package
1708
	// by using the compile time SQLITE_DEFAULT_LOCKING_MODE this PRAGMA can always be executed
1709
	if err := exec(fmt.Sprintf("PRAGMA locking_mode = %s;", lockingMode)); err != nil {
1710
		C.sqlite3_close_v2(db)
1711
		return nil, err
1712
	}
1713

1714
	// Query Only
1715
	if queryOnly > -1 {
1716
		if err := exec(fmt.Sprintf("PRAGMA query_only = %d;", queryOnly)); err != nil {
1717
			C.sqlite3_close_v2(db)
1718
			return nil, err
1719
		}
1720
	}
1721

1722
	// Recursive Triggers
1723
	if recursiveTriggers > -1 {
1724
		if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {
1725
			C.sqlite3_close_v2(db)
1726
			return nil, err
1727
		}
1728
	}
1729

1730
	// Secure Delete
1731
	//
1732
	// Because this package can set the compile time flag SQLITE_SECURE_DELETE with a build tag
1733
	// the default value for secureDelete var is 'DEFAULT' this way
1734
	// you can compile with secure_delete 'ON' and disable it for a specific database connection.
1735
	if secureDelete != "DEFAULT" {
1736
		if err := exec(fmt.Sprintf("PRAGMA secure_delete = %s;", secureDelete)); err != nil {
1737
			C.sqlite3_close_v2(db)
1738
			return nil, err
1739
		}
1740
	}
1741

1742
	// Synchronous Mode
1743
	//
1744
	// Because default is NORMAL this statement is always executed
1745
	if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
1746
		conn.Close()
1747
		return nil, err
1748
	}
1749

1750
	// Writable Schema
1751
	if writableSchema > -1 {
1752
		if err := exec(fmt.Sprintf("PRAGMA writable_schema = %d;", writableSchema)); err != nil {
1753
			C.sqlite3_close_v2(db)
1754
			return nil, err
1755
		}
1756
	}
1757

1758
	// Cache Size
1759
	if cacheSize != nil {
1760
		if err := exec(fmt.Sprintf("PRAGMA cache_size = %d;", *cacheSize)); err != nil {
1761
			C.sqlite3_close_v2(db)
1762
			return nil, err
1763
		}
1764
	}
1765

1766
	if len(d.Extensions) > 0 {
1767
		if err := conn.loadExtensions(d.Extensions); err != nil {
1768
			conn.Close()
1769
			return nil, err
1770
		}
1771
	}
1772

1773
	if d.ConnectHook != nil {
1774
		if err := d.ConnectHook(conn); err != nil {
1775
			conn.Close()
1776
			return nil, err
1777
		}
1778
	}
1779
	runtime.SetFinalizer(conn, (*SQLiteConn).Close)
1780
	return conn, nil
1781
}
1782

1783
// Close the connection.
1784
func (c *SQLiteConn) Close() error {
1785
	rv := C.sqlite3_close_v2(c.db)
1786
	if rv != C.SQLITE_OK {
1787
		return c.lastError()
1788
	}
1789
	deleteHandles(c)
1790
	c.mu.Lock()
1791
	c.db = nil
1792
	c.mu.Unlock()
1793
	runtime.SetFinalizer(c, nil)
1794
	return nil
1795
}
1796

1797
func (c *SQLiteConn) dbConnOpen() bool {
1798
	if c == nil {
1799
		return false
1800
	}
1801
	c.mu.Lock()
1802
	defer c.mu.Unlock()
1803
	return c.db != nil
1804
}
1805

1806
// Prepare the query string. Return a new statement.
1807
func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
1808
	return c.prepare(context.Background(), query)
1809
}
1810

1811
func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
1812
	pquery := C.CString(query)
1813
	defer C.free(unsafe.Pointer(pquery))
1814
	var s *C.sqlite3_stmt
1815
	var tail *C.char
1816
	rv := C._sqlite3_prepare_v2_internal(c.db, pquery, C.int(-1), &s, &tail)
1817
	if rv != C.SQLITE_OK {
1818
		return nil, c.lastError()
1819
	}
1820
	var t string
1821
	if tail != nil && *tail != '\000' {
1822
		t = strings.TrimSpace(C.GoString(tail))
1823
	}
1824
	ss := &SQLiteStmt{c: c, s: s, t: t}
1825
	runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
1826
	return ss, nil
1827
}
1828

1829
// Run-Time Limit Categories.
1830
// See: http://www.sqlite.org/c3ref/c_limit_attached.html
1831
const (
1832
	SQLITE_LIMIT_LENGTH              = C.SQLITE_LIMIT_LENGTH
1833
	SQLITE_LIMIT_SQL_LENGTH          = C.SQLITE_LIMIT_SQL_LENGTH
1834
	SQLITE_LIMIT_COLUMN              = C.SQLITE_LIMIT_COLUMN
1835
	SQLITE_LIMIT_EXPR_DEPTH          = C.SQLITE_LIMIT_EXPR_DEPTH
1836
	SQLITE_LIMIT_COMPOUND_SELECT     = C.SQLITE_LIMIT_COMPOUND_SELECT
1837
	SQLITE_LIMIT_VDBE_OP             = C.SQLITE_LIMIT_VDBE_OP
1838
	SQLITE_LIMIT_FUNCTION_ARG        = C.SQLITE_LIMIT_FUNCTION_ARG
1839
	SQLITE_LIMIT_ATTACHED            = C.SQLITE_LIMIT_ATTACHED
1840
	SQLITE_LIMIT_LIKE_PATTERN_LENGTH = C.SQLITE_LIMIT_LIKE_PATTERN_LENGTH
1841
	SQLITE_LIMIT_VARIABLE_NUMBER     = C.SQLITE_LIMIT_VARIABLE_NUMBER
1842
	SQLITE_LIMIT_TRIGGER_DEPTH       = C.SQLITE_LIMIT_TRIGGER_DEPTH
1843
	SQLITE_LIMIT_WORKER_THREADS      = C.SQLITE_LIMIT_WORKER_THREADS
1844
)
1845

1846
// GetFilename returns the absolute path to the file containing
1847
// the requested schema. When passed an empty string, it will
1848
// instead use the database's default schema: "main".
1849
// See: sqlite3_db_filename, https://www.sqlite.org/c3ref/db_filename.html
1850
func (c *SQLiteConn) GetFilename(schemaName string) string {
1851
	if schemaName == "" {
1852
		schemaName = "main"
1853
	}
1854
	return C.GoString(C.sqlite3_db_filename(c.db, C.CString(schemaName)))
1855
}
1856

1857
// GetLimit returns the current value of a run-time limit.
1858
// See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
1859
func (c *SQLiteConn) GetLimit(id int) int {
1860
	return int(C._sqlite3_limit(c.db, C.int(id), C.int(-1)))
1861
}
1862

1863
// SetLimit changes the value of a run-time limits.
1864
// Then this method returns the prior value of the limit.
1865
// See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
1866
func (c *SQLiteConn) SetLimit(id int, newVal int) int {
1867
	return int(C._sqlite3_limit(c.db, C.int(id), C.int(newVal)))
1868
}
1869

1870
// SetFileControlInt invokes the xFileControl method on a given database. The
1871
// dbName is the name of the database. It will default to "main" if left blank.
1872
// The op is one of the opcodes prefixed by "SQLITE_FCNTL_". The arg argument
1873
// and return code are both opcode-specific. Please see the SQLite documentation.
1874
//
1875
// This method is not thread-safe as the returned error code can be changed by
1876
// another call if invoked concurrently.
1877
//
1878
// See: sqlite3_file_control, https://www.sqlite.org/c3ref/file_control.html
1879
func (c *SQLiteConn) SetFileControlInt(dbName string, op int, arg int) error {
1880
	if dbName == "" {
1881
		dbName = "main"
1882
	}
1883

1884
	cDBName := C.CString(dbName)
1885
	defer C.free(unsafe.Pointer(cDBName))
1886

1887
	cArg := C.int(arg)
1888
	rv := C.sqlite3_file_control(c.db, cDBName, C.int(op), unsafe.Pointer(&cArg))
1889
	if rv != C.SQLITE_OK {
1890
		return c.lastError()
1891
	}
1892
	return nil
1893
}
1894

1895
// Close the statement.
1896
func (s *SQLiteStmt) Close() error {
1897
	s.mu.Lock()
1898
	defer s.mu.Unlock()
1899
	if s.closed {
1900
		return nil
1901
	}
1902
	s.closed = true
1903
	if !s.c.dbConnOpen() {
1904
		return errors.New("sqlite statement with already closed database connection")
1905
	}
1906
	rv := C.sqlite3_finalize(s.s)
1907
	s.s = nil
1908
	if rv != C.SQLITE_OK {
1909
		return s.c.lastError()
1910
	}
1911
	s.c = nil
1912
	runtime.SetFinalizer(s, nil)
1913
	return nil
1914
}
1915

1916
// NumInput return a number of parameters.
1917
func (s *SQLiteStmt) NumInput() int {
1918
	return int(C.sqlite3_bind_parameter_count(s.s))
1919
}
1920

1921
var placeHolder = []byte{0}
1922

1923
func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
1924
	rv := C.sqlite3_reset(s.s)
1925
	if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
1926
		return s.c.lastError()
1927
	}
1928

1929
	bindIndices := make([][3]int, len(args))
1930
	prefixes := []string{":", "@", "$"}
1931
	for i, v := range args {
1932
		bindIndices[i][0] = args[i].Ordinal
1933
		if v.Name != "" {
1934
			for j := range prefixes {
1935
				cname := C.CString(prefixes[j] + v.Name)
1936
				bindIndices[i][j] = int(C.sqlite3_bind_parameter_index(s.s, cname))
1937
				C.free(unsafe.Pointer(cname))
1938
			}
1939
			args[i].Ordinal = bindIndices[i][0]
1940
		}
1941
	}
1942

1943
	for i, arg := range args {
1944
		for j := range bindIndices[i] {
1945
			if bindIndices[i][j] == 0 {
1946
				continue
1947
			}
1948
			n := C.int(bindIndices[i][j])
1949
			switch v := arg.Value.(type) {
1950
			case nil:
1951
				rv = C.sqlite3_bind_null(s.s, n)
1952
			case string:
1953
				if len(v) == 0 {
1954
					rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&placeHolder[0])), C.int(0))
1955
				} else {
1956
					b := []byte(v)
1957
					rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
1958
				}
1959
			case int64:
1960
				rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
1961
			case bool:
1962
				if v {
1963
					rv = C.sqlite3_bind_int(s.s, n, 1)
1964
				} else {
1965
					rv = C.sqlite3_bind_int(s.s, n, 0)
1966
				}
1967
			case float64:
1968
				rv = C.sqlite3_bind_double(s.s, n, C.double(v))
1969
			case []byte:
1970
				if v == nil {
1971
					rv = C.sqlite3_bind_null(s.s, n)
1972
				} else {
1973
					ln := len(v)
1974
					if ln == 0 {
1975
						v = placeHolder
1976
					}
1977
					rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
1978
				}
1979
			case time.Time:
1980
				b := []byte(v.Format(SQLiteTimestampFormats[0]))
1981
				rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
1982
			}
1983
			if rv != C.SQLITE_OK {
1984
				return s.c.lastError()
1985
			}
1986
		}
1987
	}
1988
	return nil
1989
}
1990

1991
// Query the statement with arguments. Return records.
1992
func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
1993
	list := make([]driver.NamedValue, len(args))
1994
	for i, v := range args {
1995
		list[i] = driver.NamedValue{
1996
			Ordinal: i + 1,
1997
			Value:   v,
1998
		}
1999
	}
2000
	return s.query(context.Background(), list)
2001
}
2002

2003
func (s *SQLiteStmt) query(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
2004
	if err := s.bind(args); err != nil {
2005
		return nil, err
2006
	}
2007

2008
	rows := &SQLiteRows{
2009
		s:        s,
2010
		nc:       int(C.sqlite3_column_count(s.s)),
2011
		cols:     nil,
2012
		decltype: nil,
2013
		cls:      s.cls,
2014
		closed:   false,
2015
		ctx:      ctx,
2016
	}
2017
	runtime.SetFinalizer(rows, (*SQLiteRows).Close)
2018

2019
	return rows, nil
2020
}
2021

2022
// LastInsertId return last inserted ID.
2023
func (r *SQLiteResult) LastInsertId() (int64, error) {
2024
	return r.id, nil
2025
}
2026

2027
// RowsAffected return how many rows affected.
2028
func (r *SQLiteResult) RowsAffected() (int64, error) {
2029
	return r.changes, nil
2030
}
2031

2032
// Exec execute the statement with arguments. Return result object.
2033
func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
2034
	list := make([]driver.NamedValue, len(args))
2035
	for i, v := range args {
2036
		list[i] = driver.NamedValue{
2037
			Ordinal: i + 1,
2038
			Value:   v,
2039
		}
2040
	}
2041
	return s.exec(context.Background(), list)
2042
}
2043

2044
func isInterruptErr(err error) bool {
2045
	sqliteErr, ok := err.(Error)
2046
	if ok {
2047
		return sqliteErr.Code == ErrInterrupt
2048
	}
2049
	return false
2050
}
2051

2052
// exec executes a query that doesn't return rows. Attempts to honor context timeout.
2053
func (s *SQLiteStmt) exec(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
2054
	if ctx.Done() == nil {
2055
		return s.execSync(args)
2056
	}
2057

2058
	type result struct {
2059
		r   driver.Result
2060
		err error
2061
	}
2062
	resultCh := make(chan result)
2063
	defer close(resultCh)
2064
	go func() {
2065
		r, err := s.execSync(args)
2066
		resultCh <- result{r, err}
2067
	}()
2068
	var rv result
2069
	select {
2070
	case rv = <-resultCh:
2071
	case <-ctx.Done():
2072
		select {
2073
		case rv = <-resultCh: // no need to interrupt, operation completed in db
2074
		default:
2075
			// this is still racy and can be no-op if executed between sqlite3_* calls in execSync.
2076
			C.sqlite3_interrupt(s.c.db)
2077
			rv = <-resultCh // wait for goroutine completed
2078
			if isInterruptErr(rv.err) {
2079
				return nil, ctx.Err()
2080
			}
2081
		}
2082
	}
2083
	return rv.r, rv.err
2084
}
2085

2086
func (s *SQLiteStmt) execSync(args []driver.NamedValue) (driver.Result, error) {
2087
	if err := s.bind(args); err != nil {
2088
		C.sqlite3_reset(s.s)
2089
		C.sqlite3_clear_bindings(s.s)
2090
		return nil, err
2091
	}
2092

2093
	var rowid, changes C.longlong
2094
	rv := C._sqlite3_step_row_internal(s.s, &rowid, &changes)
2095
	if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
2096
		err := s.c.lastError()
2097
		C.sqlite3_reset(s.s)
2098
		C.sqlite3_clear_bindings(s.s)
2099
		return nil, err
2100
	}
2101

2102
	return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
2103
}
2104

2105
// Readonly reports if this statement is considered readonly by SQLite.
2106
//
2107
// See: https://sqlite.org/c3ref/stmt_readonly.html
2108
func (s *SQLiteStmt) Readonly() bool {
2109
	return C.sqlite3_stmt_readonly(s.s) == 1
2110
}
2111

2112
// Close the rows.
2113
func (rc *SQLiteRows) Close() error {
2114
	rc.s.mu.Lock()
2115
	if rc.s.closed || rc.closed {
2116
		rc.s.mu.Unlock()
2117
		return nil
2118
	}
2119
	rc.closed = true
2120
	if rc.cls {
2121
		rc.s.mu.Unlock()
2122
		return rc.s.Close()
2123
	}
2124
	rv := C.sqlite3_reset(rc.s.s)
2125
	if rv != C.SQLITE_OK {
2126
		rc.s.mu.Unlock()
2127
		return rc.s.c.lastError()
2128
	}
2129
	rc.s.mu.Unlock()
2130
	rc.s = nil
2131
	runtime.SetFinalizer(rc, nil)
2132
	return nil
2133
}
2134

2135
// Columns return column names.
2136
func (rc *SQLiteRows) Columns() []string {
2137
	rc.s.mu.Lock()
2138
	defer rc.s.mu.Unlock()
2139
	if rc.s.s != nil && rc.nc != len(rc.cols) {
2140
		rc.cols = make([]string, rc.nc)
2141
		for i := 0; i < rc.nc; i++ {
2142
			rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
2143
		}
2144
	}
2145
	return rc.cols
2146
}
2147

2148
func (rc *SQLiteRows) declTypes() []string {
2149
	if rc.s.s != nil && rc.decltype == nil {
2150
		rc.decltype = make([]string, rc.nc)
2151
		for i := 0; i < rc.nc; i++ {
2152
			rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
2153
		}
2154
	}
2155
	return rc.decltype
2156
}
2157

2158
// DeclTypes return column types.
2159
func (rc *SQLiteRows) DeclTypes() []string {
2160
	rc.s.mu.Lock()
2161
	defer rc.s.mu.Unlock()
2162
	return rc.declTypes()
2163
}
2164

2165
// Next move cursor to next. Attempts to honor context timeout from QueryContext call.
2166
func (rc *SQLiteRows) Next(dest []driver.Value) error {
2167
	rc.s.mu.Lock()
2168
	defer rc.s.mu.Unlock()
2169

2170
	if rc.s.closed {
2171
		return io.EOF
2172
	}
2173

2174
	if rc.ctx.Done() == nil {
2175
		return rc.nextSyncLocked(dest)
2176
	}
2177
	resultCh := make(chan error)
2178
	defer close(resultCh)
2179
	go func() {
2180
		resultCh <- rc.nextSyncLocked(dest)
2181
	}()
2182
	select {
2183
	case err := <-resultCh:
2184
		return err
2185
	case <-rc.ctx.Done():
2186
		select {
2187
		case <-resultCh: // no need to interrupt
2188
		default:
2189
			// this is still racy and can be no-op if executed between sqlite3_* calls in nextSyncLocked.
2190
			C.sqlite3_interrupt(rc.s.c.db)
2191
			<-resultCh // ensure goroutine completed
2192
		}
2193
		return rc.ctx.Err()
2194
	}
2195
}
2196

2197
// nextSyncLocked moves cursor to next; must be called with locked mutex.
2198
func (rc *SQLiteRows) nextSyncLocked(dest []driver.Value) error {
2199
	rv := C._sqlite3_step_internal(rc.s.s)
2200
	if rv == C.SQLITE_DONE {
2201
		return io.EOF
2202
	}
2203
	if rv != C.SQLITE_ROW {
2204
		rv = C.sqlite3_reset(rc.s.s)
2205
		if rv != C.SQLITE_OK {
2206
			return rc.s.c.lastError()
2207
		}
2208
		return nil
2209
	}
2210

2211
	rc.declTypes()
2212

2213
	for i := range dest {
2214
		switch C.sqlite3_column_type(rc.s.s, C.int(i)) {
2215
		case C.SQLITE_INTEGER:
2216
			val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
2217
			switch rc.decltype[i] {
2218
			case columnTimestamp, columnDatetime, columnDate:
2219
				var t time.Time
2220
				// Assume a millisecond unix timestamp if it's 13 digits -- too
2221
				// large to be a reasonable timestamp in seconds.
2222
				if val > 1e12 || val < -1e12 {
2223
					val *= int64(time.Millisecond) // convert ms to nsec
2224
					t = time.Unix(0, val)
2225
				} else {
2226
					t = time.Unix(val, 0)
2227
				}
2228
				t = t.UTC()
2229
				if rc.s.c.loc != nil {
2230
					t = t.In(rc.s.c.loc)
2231
				}
2232
				dest[i] = t
2233
			case "boolean":
2234
				dest[i] = val > 0
2235
			default:
2236
				dest[i] = val
2237
			}
2238
		case C.SQLITE_FLOAT:
2239
			dest[i] = float64(C.sqlite3_column_double(rc.s.s, C.int(i)))
2240
		case C.SQLITE_BLOB:
2241
			p := C.sqlite3_column_blob(rc.s.s, C.int(i))
2242
			if p == nil {
2243
				dest[i] = []byte{}
2244
				continue
2245
			}
2246
			n := C.sqlite3_column_bytes(rc.s.s, C.int(i))
2247
			dest[i] = C.GoBytes(p, n)
2248
		case C.SQLITE_NULL:
2249
			dest[i] = nil
2250
		case C.SQLITE_TEXT:
2251
			var err error
2252
			var timeVal time.Time
2253

2254
			n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
2255
			s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n))
2256

2257
			switch rc.decltype[i] {
2258
			case columnTimestamp, columnDatetime, columnDate:
2259
				var t time.Time
2260
				s = strings.TrimSuffix(s, "Z")
2261
				for _, format := range SQLiteTimestampFormats {
2262
					if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
2263
						t = timeVal
2264
						break
2265
					}
2266
				}
2267
				if err != nil {
2268
					// The column is a time value, so return the zero time on parse failure.
2269
					t = time.Time{}
2270
				}
2271
				if rc.s.c.loc != nil {
2272
					t = t.In(rc.s.c.loc)
2273
				}
2274
				dest[i] = t
2275
			default:
2276
				dest[i] = s
2277
			}
2278
		}
2279
	}
2280
	return nil
2281
}
2282

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.