gemini-search

A simple search engine for Geminispace
git clone git://git.laack.co/gemini-search.git
Log | Files | Refs | README

sqlite3.go (76314B)


      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 typedef struct {
     82   int typ;
     83   sqlite3_int64 i64;
     84   double f64;
     85   const void *ptr;
     86   int n;
     87 } sqlite3_go_col;
     88 
     89 static void
     90 _sqlite3_column_values(sqlite3_stmt *stmt, int ncol, sqlite3_go_col *cols) {
     91   for (int i = 0; i < ncol; i++) {
     92     sqlite3_go_col *col = &cols[i];
     93     col->typ = sqlite3_column_type(stmt, i);
     94     col->ptr = 0;
     95     col->n = 0;
     96     switch (col->typ) {
     97     case SQLITE_INTEGER:
     98       col->i64 = sqlite3_column_int64(stmt, i);
     99       break;
    100     case SQLITE_FLOAT:
    101       col->f64 = sqlite3_column_double(stmt, i);
    102       break;
    103     case SQLITE_BLOB:
    104       col->ptr = sqlite3_column_blob(stmt, i);
    105       col->n = sqlite3_column_bytes(stmt, i);
    106       break;
    107     case SQLITE_TEXT:
    108       col->ptr = sqlite3_column_text(stmt, i);
    109       col->n = sqlite3_column_bytes(stmt, i);
    110       break;
    111     default:
    112       break;
    113     }
    114   }
    115 }
    116 
    117 #include <stdio.h>
    118 #include <stdint.h>
    119 
    120 static int
    121 _sqlite3_exec(sqlite3* db, const char* pcmd, long long* rowid, long long* changes)
    122 {
    123   int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
    124   *rowid = (long long) sqlite3_last_insert_rowid(db);
    125   *changes = (long long) sqlite3_changes(db);
    126   return rv;
    127 }
    128 
    129 // Combined reset + clear_bindings in a single C call to reduce CGO crossings.
    130 static int
    131 _sqlite3_reset_clear(sqlite3_stmt* stmt)
    132 {
    133   int rv = sqlite3_reset(stmt);
    134   sqlite3_clear_bindings(stmt);
    135   return rv;
    136 }
    137 
    138 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
    139 extern int _sqlite3_step_blocking(sqlite3_stmt *stmt);
    140 extern int _sqlite3_step_row_blocking(sqlite3_stmt* stmt, long long* rowid, long long* changes);
    141 extern int _sqlite3_prepare_v2_blocking(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail);
    142 #endif
    143 
    144 // Combined prepare+step+finalize for simple exec without parameters.
    145 // Reduces CGO crossings from ~6 to 1 for the common no-args exec case.
    146 static int
    147 _sqlite3_exec_no_args(sqlite3* db, const char* zSql, int nBytes, long long* rowid, long long* changes, const char** pzTail)
    148 {
    149   sqlite3_stmt *stmt = 0;
    150   const char *tail = 0;
    151 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
    152   int rv = _sqlite3_prepare_v2_blocking(db, zSql, nBytes, &stmt, &tail);
    153 #else
    154   int rv = sqlite3_prepare_v2(db, zSql, nBytes, &stmt, &tail);
    155 #endif
    156   if (rv != SQLITE_OK) {
    157     *pzTail = 0;
    158     return rv;
    159   }
    160   if (stmt == 0) {
    161     // Empty statement
    162     *rowid = 0;
    163     *changes = 0;
    164     *pzTail = tail;
    165     return SQLITE_OK;
    166   }
    167 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
    168   rv = _sqlite3_step_row_blocking(stmt, rowid, changes);
    169 #else
    170   rv = sqlite3_step(stmt);
    171   *rowid = (long long) sqlite3_last_insert_rowid(db);
    172   *changes = (long long) sqlite3_changes(db);
    173 #endif
    174   sqlite3_finalize(stmt);
    175   *pzTail = tail;
    176   return rv;
    177 }
    178 
    179 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
    180 
    181 static int
    182 _sqlite3_step_internal(sqlite3_stmt *stmt)
    183 {
    184   return _sqlite3_step_blocking(stmt);
    185 }
    186 
    187 static int
    188 _sqlite3_step_row_internal(sqlite3_stmt* stmt, long long* rowid, long long* changes)
    189 {
    190   return _sqlite3_step_row_blocking(stmt, rowid, changes);
    191 }
    192 
    193 static int
    194 _sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail)
    195 {
    196   return _sqlite3_prepare_v2_blocking(db, zSql, nBytes, ppStmt, pzTail);
    197 }
    198 
    199 #else
    200 static int
    201 _sqlite3_step_internal(sqlite3_stmt *stmt)
    202 {
    203   return sqlite3_step(stmt);
    204 }
    205 
    206 static int
    207 _sqlite3_step_row_internal(sqlite3_stmt* stmt, long long* rowid, long long* changes)
    208 {
    209   int rv = sqlite3_step(stmt);
    210   sqlite3* db = sqlite3_db_handle(stmt);
    211   *rowid = (long long) sqlite3_last_insert_rowid(db);
    212   *changes = (long long) sqlite3_changes(db);
    213   return rv;
    214 }
    215 
    216 static int
    217 _sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail)
    218 {
    219   return sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail);
    220 }
    221 #endif
    222 
    223 void _sqlite3_result_text(sqlite3_context* ctx, const char* s) {
    224   sqlite3_result_text(ctx, s, -1, &free);
    225 }
    226 
    227 void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l) {
    228   sqlite3_result_blob(ctx, b, l, SQLITE_TRANSIENT);
    229 }
    230 
    231 
    232 int _sqlite3_create_function(
    233   sqlite3 *db,
    234   const char *zFunctionName,
    235   int nArg,
    236   int eTextRep,
    237   uintptr_t pApp,
    238   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
    239   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
    240   void (*xFinal)(sqlite3_context*)
    241 ) {
    242   return sqlite3_create_function(db, zFunctionName, nArg, eTextRep, (void*) pApp, xFunc, xStep, xFinal);
    243 }
    244 
    245 void callbackTrampoline(sqlite3_context*, int, sqlite3_value**);
    246 void stepTrampoline(sqlite3_context*, int, sqlite3_value**);
    247 void doneTrampoline(sqlite3_context*);
    248 
    249 int compareTrampoline(void*, int, char*, int, char*);
    250 int commitHookTrampoline(void*);
    251 void rollbackHookTrampoline(void*);
    252 void updateHookTrampoline(void*, int, char*, char*, sqlite3_int64);
    253 
    254 int authorizerTrampoline(void*, int, char*, char*, char*, char*);
    255 
    256 #ifdef SQLITE_LIMIT_WORKER_THREADS
    257 # define _SQLITE_HAS_LIMIT
    258 # define SQLITE_LIMIT_LENGTH                    0
    259 # define SQLITE_LIMIT_SQL_LENGTH                1
    260 # define SQLITE_LIMIT_COLUMN                    2
    261 # define SQLITE_LIMIT_EXPR_DEPTH                3
    262 # define SQLITE_LIMIT_COMPOUND_SELECT           4
    263 # define SQLITE_LIMIT_VDBE_OP                   5
    264 # define SQLITE_LIMIT_FUNCTION_ARG              6
    265 # define SQLITE_LIMIT_ATTACHED                  7
    266 # define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
    267 # define SQLITE_LIMIT_VARIABLE_NUMBER           9
    268 # define SQLITE_LIMIT_TRIGGER_DEPTH            10
    269 # define SQLITE_LIMIT_WORKER_THREADS           11
    270 # else
    271 # define SQLITE_LIMIT_WORKER_THREADS           11
    272 #endif
    273 
    274 static int _sqlite3_limit(sqlite3* db, int limitId, int newLimit) {
    275 #ifndef _SQLITE_HAS_LIMIT
    276   return -1;
    277 #else
    278   return sqlite3_limit(db, limitId, newLimit);
    279 #endif
    280 }
    281 
    282 #if SQLITE_VERSION_NUMBER < 3012000
    283 static int sqlite3_system_errno(sqlite3 *db) {
    284   return 0;
    285 }
    286 #endif
    287 */
    288 import "C"
    289 import (
    290 	"context"
    291 	"database/sql"
    292 	"database/sql/driver"
    293 	"errors"
    294 	"fmt"
    295 	"io"
    296 	"math"
    297 	"net/url"
    298 	"reflect"
    299 	"runtime"
    300 	"strconv"
    301 	"strings"
    302 	"sync"
    303 	"syscall"
    304 	"time"
    305 	"unsafe"
    306 )
    307 
    308 // SQLiteTimestampFormats is timestamp formats understood by both this module
    309 // and SQLite.  The first format in the slice will be used when saving time
    310 // values into the database. When parsing a string from a timestamp or datetime
    311 // column, the formats are tried in order.
    312 var SQLiteTimestampFormats = []string{
    313 	// By default, store timestamps with whatever timezone they come with.
    314 	// When parsed, they will be returned with the same timezone.
    315 	"2006-01-02 15:04:05.999999999-07:00",
    316 	"2006-01-02T15:04:05.999999999-07:00",
    317 	"2006-01-02 15:04:05.999999999",
    318 	"2006-01-02T15:04:05.999999999",
    319 	"2006-01-02 15:04:05",
    320 	"2006-01-02T15:04:05",
    321 	"2006-01-02 15:04",
    322 	"2006-01-02T15:04",
    323 	"2006-01-02",
    324 }
    325 
    326 const (
    327 	columnDate      string = "date"
    328 	columnDatetime  string = "datetime"
    329 	columnTimestamp string = "timestamp"
    330 )
    331 
    332 // This variable can be replaced with -ldflags like below:
    333 // go build -ldflags="-X 'github.com/mattn/go-sqlite3.driverName=my-sqlite3'"
    334 var driverName = "sqlite3"
    335 
    336 func init() {
    337 	if driverName != "" {
    338 		sql.Register(driverName, &SQLiteDriver{})
    339 	}
    340 }
    341 
    342 // Version returns SQLite library version information.
    343 func Version() (libVersion string, libVersionNumber int, sourceID string) {
    344 	libVersion = C.GoString(C.sqlite3_libversion())
    345 	libVersionNumber = int(C.sqlite3_libversion_number())
    346 	sourceID = C.GoString(C.sqlite3_sourceid())
    347 	return libVersion, libVersionNumber, sourceID
    348 }
    349 
    350 const (
    351 	// used by authorizer and pre_update_hook
    352 	SQLITE_DELETE = C.SQLITE_DELETE
    353 	SQLITE_INSERT = C.SQLITE_INSERT
    354 	SQLITE_UPDATE = C.SQLITE_UPDATE
    355 
    356 	// used by authorzier - as return value
    357 	SQLITE_OK     = C.SQLITE_OK
    358 	SQLITE_IGNORE = C.SQLITE_IGNORE
    359 	SQLITE_DENY   = C.SQLITE_DENY
    360 
    361 	// different actions query tries to do - passed as argument to authorizer
    362 	SQLITE_CREATE_INDEX        = C.SQLITE_CREATE_INDEX
    363 	SQLITE_CREATE_TABLE        = C.SQLITE_CREATE_TABLE
    364 	SQLITE_CREATE_TEMP_INDEX   = C.SQLITE_CREATE_TEMP_INDEX
    365 	SQLITE_CREATE_TEMP_TABLE   = C.SQLITE_CREATE_TEMP_TABLE
    366 	SQLITE_CREATE_TEMP_TRIGGER = C.SQLITE_CREATE_TEMP_TRIGGER
    367 	SQLITE_CREATE_TEMP_VIEW    = C.SQLITE_CREATE_TEMP_VIEW
    368 	SQLITE_CREATE_TRIGGER      = C.SQLITE_CREATE_TRIGGER
    369 	SQLITE_CREATE_VIEW         = C.SQLITE_CREATE_VIEW
    370 	SQLITE_CREATE_VTABLE       = C.SQLITE_CREATE_VTABLE
    371 	SQLITE_DROP_INDEX          = C.SQLITE_DROP_INDEX
    372 	SQLITE_DROP_TABLE          = C.SQLITE_DROP_TABLE
    373 	SQLITE_DROP_TEMP_INDEX     = C.SQLITE_DROP_TEMP_INDEX
    374 	SQLITE_DROP_TEMP_TABLE     = C.SQLITE_DROP_TEMP_TABLE
    375 	SQLITE_DROP_TEMP_TRIGGER   = C.SQLITE_DROP_TEMP_TRIGGER
    376 	SQLITE_DROP_TEMP_VIEW      = C.SQLITE_DROP_TEMP_VIEW
    377 	SQLITE_DROP_TRIGGER        = C.SQLITE_DROP_TRIGGER
    378 	SQLITE_DROP_VIEW           = C.SQLITE_DROP_VIEW
    379 	SQLITE_DROP_VTABLE         = C.SQLITE_DROP_VTABLE
    380 	SQLITE_PRAGMA              = C.SQLITE_PRAGMA
    381 	SQLITE_READ                = C.SQLITE_READ
    382 	SQLITE_SELECT              = C.SQLITE_SELECT
    383 	SQLITE_TRANSACTION         = C.SQLITE_TRANSACTION
    384 	SQLITE_ATTACH              = C.SQLITE_ATTACH
    385 	SQLITE_DETACH              = C.SQLITE_DETACH
    386 	SQLITE_ALTER_TABLE         = C.SQLITE_ALTER_TABLE
    387 	SQLITE_REINDEX             = C.SQLITE_REINDEX
    388 	SQLITE_ANALYZE             = C.SQLITE_ANALYZE
    389 	SQLITE_FUNCTION            = C.SQLITE_FUNCTION
    390 	SQLITE_SAVEPOINT           = C.SQLITE_SAVEPOINT
    391 	SQLITE_COPY                = C.SQLITE_COPY
    392 	/*SQLITE_RECURSIVE           = C.SQLITE_RECURSIVE*/
    393 )
    394 
    395 // Standard File Control Opcodes
    396 // See: https://www.sqlite.org/c3ref/c_fcntl_begin_atomic_write.html
    397 const (
    398 	SQLITE_FCNTL_LOCKSTATE             = int(1)
    399 	SQLITE_FCNTL_GET_LOCKPROXYFILE     = int(2)
    400 	SQLITE_FCNTL_SET_LOCKPROXYFILE     = int(3)
    401 	SQLITE_FCNTL_LAST_ERRNO            = int(4)
    402 	SQLITE_FCNTL_SIZE_HINT             = int(5)
    403 	SQLITE_FCNTL_CHUNK_SIZE            = int(6)
    404 	SQLITE_FCNTL_FILE_POINTER          = int(7)
    405 	SQLITE_FCNTL_SYNC_OMITTED          = int(8)
    406 	SQLITE_FCNTL_WIN32_AV_RETRY        = int(9)
    407 	SQLITE_FCNTL_PERSIST_WAL           = int(10)
    408 	SQLITE_FCNTL_OVERWRITE             = int(11)
    409 	SQLITE_FCNTL_VFSNAME               = int(12)
    410 	SQLITE_FCNTL_POWERSAFE_OVERWRITE   = int(13)
    411 	SQLITE_FCNTL_PRAGMA                = int(14)
    412 	SQLITE_FCNTL_BUSYHANDLER           = int(15)
    413 	SQLITE_FCNTL_TEMPFILENAME          = int(16)
    414 	SQLITE_FCNTL_MMAP_SIZE             = int(18)
    415 	SQLITE_FCNTL_TRACE                 = int(19)
    416 	SQLITE_FCNTL_HAS_MOVED             = int(20)
    417 	SQLITE_FCNTL_SYNC                  = int(21)
    418 	SQLITE_FCNTL_COMMIT_PHASETWO       = int(22)
    419 	SQLITE_FCNTL_WIN32_SET_HANDLE      = int(23)
    420 	SQLITE_FCNTL_WAL_BLOCK             = int(24)
    421 	SQLITE_FCNTL_ZIPVFS                = int(25)
    422 	SQLITE_FCNTL_RBU                   = int(26)
    423 	SQLITE_FCNTL_VFS_POINTER           = int(27)
    424 	SQLITE_FCNTL_JOURNAL_POINTER       = int(28)
    425 	SQLITE_FCNTL_WIN32_GET_HANDLE      = int(29)
    426 	SQLITE_FCNTL_PDB                   = int(30)
    427 	SQLITE_FCNTL_BEGIN_ATOMIC_WRITE    = int(31)
    428 	SQLITE_FCNTL_COMMIT_ATOMIC_WRITE   = int(32)
    429 	SQLITE_FCNTL_ROLLBACK_ATOMIC_WRITE = int(33)
    430 	SQLITE_FCNTL_LOCK_TIMEOUT          = int(34)
    431 	SQLITE_FCNTL_DATA_VERSION          = int(35)
    432 	SQLITE_FCNTL_SIZE_LIMIT            = int(36)
    433 	SQLITE_FCNTL_CKPT_DONE             = int(37)
    434 	SQLITE_FCNTL_RESERVE_BYTES         = int(38)
    435 	SQLITE_FCNTL_CKPT_START            = int(39)
    436 	SQLITE_FCNTL_EXTERNAL_READER       = int(40)
    437 	SQLITE_FCNTL_CKSM_FILE             = int(41)
    438 )
    439 
    440 // SQLiteDriver implements driver.Driver.
    441 type SQLiteDriver struct {
    442 	Extensions  []string
    443 	ConnectHook func(*SQLiteConn) error
    444 }
    445 
    446 // SQLiteConn implements driver.Conn.
    447 type SQLiteConn struct {
    448 	mu             sync.Mutex
    449 	db             *C.sqlite3
    450 	loc            *time.Location
    451 	txlock         string
    452 	funcs          []*functionInfo
    453 	aggregators    []*aggInfo
    454 	// Prepared-statement cache. The slice is allocated at Open with a
    455 	// fixed capacity equal to the configured cache size; cap bounds the
    456 	// cache, len is the live count, and entries are ordered LRU-first
    457 	// (index 0 is the oldest, the tail is most recently put). Access
    458 	// requires mu; stmtCacheEnabled is immutable after Open and is the
    459 	// only field safe to read without the lock.
    460 	stmtCache        []*SQLiteStmt
    461 	stmtCacheEnabled bool
    462 }
    463 
    464 // SQLiteTx implements driver.Tx.
    465 type SQLiteTx struct {
    466 	c *SQLiteConn
    467 }
    468 
    469 // SQLiteStmt implements driver.Stmt.
    470 type SQLiteStmt struct {
    471 	mu          sync.Mutex
    472 	c           *SQLiteConn
    473 	s           *C.sqlite3_stmt
    474 	t           string
    475 	closed      bool
    476 	cls         bool // True if the statement was created by SQLiteConn.Query
    477 	namedParams map[string][3]int
    478 	cacheKey    string
    479 }
    480 
    481 // SQLiteResult implements sql.Result.
    482 type SQLiteResult struct {
    483 	id      int64
    484 	changes int64
    485 }
    486 
    487 // SQLiteRows implements driver.Rows.
    488 type SQLiteRows struct {
    489 	s        *SQLiteStmt
    490 	nc       int32 // Number of columns
    491 	cls      bool  // True if we need to close the parent statement in Close
    492 	cols     []string
    493 	decltype []string
    494 	colvals  *C.sqlite3_go_col
    495 	ctx      context.Context // no better alternative to pass context into Next() method
    496 	closemu  sync.Mutex
    497 }
    498 
    499 type functionInfo struct {
    500 	f                 reflect.Value
    501 	argConverters     []callbackArgConverter
    502 	variadicConverter callbackArgConverter
    503 	retConverter      callbackRetConverter
    504 }
    505 
    506 func (fi *functionInfo) Call(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
    507 	args, err := callbackConvertArgs(argv, fi.argConverters, fi.variadicConverter)
    508 	if err != nil {
    509 		callbackError(ctx, err)
    510 		return
    511 	}
    512 
    513 	ret := fi.f.Call(args)
    514 
    515 	if len(ret) == 2 && ret[1].Interface() != nil {
    516 		callbackError(ctx, ret[1].Interface().(error))
    517 		return
    518 	}
    519 
    520 	err = fi.retConverter(ctx, ret[0])
    521 	if err != nil {
    522 		callbackError(ctx, err)
    523 		return
    524 	}
    525 }
    526 
    527 type aggInfo struct {
    528 	constructor reflect.Value
    529 
    530 	// Active aggregator objects for aggregations in flight. The
    531 	// aggregators are indexed by a counter stored in the aggregation
    532 	// user data space provided by sqlite.
    533 	active map[int64]reflect.Value
    534 	next   int64
    535 
    536 	stepArgConverters     []callbackArgConverter
    537 	stepVariadicConverter callbackArgConverter
    538 
    539 	doneRetConverter callbackRetConverter
    540 }
    541 
    542 func (ai *aggInfo) agg(ctx *C.sqlite3_context) (int64, reflect.Value, error) {
    543 	aggIdx := (*int64)(C.sqlite3_aggregate_context(ctx, C.int(8)))
    544 	if *aggIdx == 0 {
    545 		*aggIdx = ai.next
    546 		ret := ai.constructor.Call(nil)
    547 		if len(ret) == 2 && ret[1].Interface() != nil {
    548 			return 0, reflect.Value{}, ret[1].Interface().(error)
    549 		}
    550 		if ret[0].IsNil() {
    551 			return 0, reflect.Value{}, errors.New("aggregator constructor returned nil state")
    552 		}
    553 		ai.next++
    554 		ai.active[*aggIdx] = ret[0]
    555 	}
    556 	return *aggIdx, ai.active[*aggIdx], nil
    557 }
    558 
    559 func (ai *aggInfo) Step(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
    560 	_, agg, err := ai.agg(ctx)
    561 	if err != nil {
    562 		callbackError(ctx, err)
    563 		return
    564 	}
    565 
    566 	args, err := callbackConvertArgs(argv, ai.stepArgConverters, ai.stepVariadicConverter)
    567 	if err != nil {
    568 		callbackError(ctx, err)
    569 		return
    570 	}
    571 
    572 	ret := agg.MethodByName("Step").Call(args)
    573 	if len(ret) == 1 && ret[0].Interface() != nil {
    574 		callbackError(ctx, ret[0].Interface().(error))
    575 		return
    576 	}
    577 }
    578 
    579 func (ai *aggInfo) Done(ctx *C.sqlite3_context) {
    580 	idx, agg, err := ai.agg(ctx)
    581 	if err != nil {
    582 		callbackError(ctx, err)
    583 		return
    584 	}
    585 	defer func() { delete(ai.active, idx) }()
    586 
    587 	ret := agg.MethodByName("Done").Call(nil)
    588 	if len(ret) == 2 && ret[1].Interface() != nil {
    589 		callbackError(ctx, ret[1].Interface().(error))
    590 		return
    591 	}
    592 
    593 	err = ai.doneRetConverter(ctx, ret[0])
    594 	if err != nil {
    595 		callbackError(ctx, err)
    596 		return
    597 	}
    598 }
    599 
    600 // Commit transaction.
    601 func (tx *SQLiteTx) Commit() error {
    602 	_, err := tx.c.exec(context.Background(), "COMMIT", nil)
    603 	if err != nil {
    604 		// sqlite3 may leave the transaction open in this scenario.
    605 		// However, database/sql considers the transaction complete once we
    606 		// return from Commit() - we must clean up to honour its semantics.
    607 		// We don't know if the ROLLBACK is strictly necessary, but according
    608 		// to sqlite's docs, there is no harm in calling ROLLBACK unnecessarily.
    609 		tx.c.exec(context.Background(), "ROLLBACK", nil)
    610 	}
    611 	return err
    612 }
    613 
    614 // Rollback transaction.
    615 func (tx *SQLiteTx) Rollback() error {
    616 	_, err := tx.c.exec(context.Background(), "ROLLBACK", nil)
    617 	return err
    618 }
    619 
    620 // RegisterCollation makes a Go function available as a collation.
    621 //
    622 // cmp receives two UTF-8 strings, a and b. The result should be 0 if
    623 // a==b, -1 if a < b, and +1 if a > b.
    624 //
    625 // cmp must always return the same result given the same
    626 // inputs. Additionally, it must have the following properties for all
    627 // strings A, B and C: if A==B then B==A; if A==B and B==C then A==C;
    628 // if A<B then B>A; if A<B and B<C then A<C.
    629 //
    630 // If cmp does not obey these constraints, sqlite3's behavior is
    631 // undefined when the collation is used.
    632 func (c *SQLiteConn) RegisterCollation(name string, cmp func(string, string) int) error {
    633 	handle := newHandle(c, cmp)
    634 	cname := C.CString(name)
    635 	defer C.free(unsafe.Pointer(cname))
    636 	rv := C.sqlite3_create_collation(c.db, cname, C.SQLITE_UTF8, handle, (*[0]byte)(unsafe.Pointer(C.compareTrampoline)))
    637 	if rv != C.SQLITE_OK {
    638 		return c.lastError()
    639 	}
    640 	return nil
    641 }
    642 
    643 // RegisterCommitHook sets the commit hook for a connection.
    644 //
    645 // If the callback returns non-zero the transaction will become a rollback.
    646 //
    647 // If there is an existing commit hook for this connection, it will be
    648 // removed. If callback is nil the existing hook (if any) will be removed
    649 // without creating a new one.
    650 func (c *SQLiteConn) RegisterCommitHook(callback func() int) {
    651 	if callback == nil {
    652 		C.sqlite3_commit_hook(c.db, nil, nil)
    653 	} else {
    654 		C.sqlite3_commit_hook(c.db, (*[0]byte)(C.commitHookTrampoline), newHandle(c, callback))
    655 	}
    656 }
    657 
    658 // RegisterRollbackHook sets the rollback hook for a connection.
    659 //
    660 // If there is an existing rollback hook for this connection, it will be
    661 // removed. If callback is nil the existing hook (if any) will be removed
    662 // without creating a new one.
    663 func (c *SQLiteConn) RegisterRollbackHook(callback func()) {
    664 	if callback == nil {
    665 		C.sqlite3_rollback_hook(c.db, nil, nil)
    666 	} else {
    667 		C.sqlite3_rollback_hook(c.db, (*[0]byte)(C.rollbackHookTrampoline), newHandle(c, callback))
    668 	}
    669 }
    670 
    671 // RegisterUpdateHook sets the update hook for a connection.
    672 //
    673 // The parameters to the callback are the operation (one of the constants
    674 // SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE), the database name, the
    675 // table name, and the rowid.
    676 //
    677 // If there is an existing update hook for this connection, it will be
    678 // removed. If callback is nil the existing hook (if any) will be removed
    679 // without creating a new one.
    680 func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64)) {
    681 	if callback == nil {
    682 		C.sqlite3_update_hook(c.db, nil, nil)
    683 	} else {
    684 		C.sqlite3_update_hook(c.db, (*[0]byte)(C.updateHookTrampoline), newHandle(c, callback))
    685 	}
    686 }
    687 
    688 // RegisterAuthorizer sets the authorizer for connection.
    689 //
    690 // The parameters to the callback are the operation (one of the constants
    691 // SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE), and 1 to 3 arguments,
    692 // depending on operation. More details see:
    693 // https://www.sqlite.org/c3ref/c_alter_table.html
    694 func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, string) int) {
    695 	if callback == nil {
    696 		C.sqlite3_set_authorizer(c.db, nil, nil)
    697 	} else {
    698 		C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), newHandle(c, callback))
    699 	}
    700 }
    701 
    702 // RegisterFunc makes a Go function available as a SQLite function.
    703 //
    704 // The Go function can have arguments of the following types: any
    705 // numeric type except complex, bool, []byte, string and any.
    706 // any arguments are given the direct translation of the SQLite data type:
    707 // int64 for INTEGER, float64 for FLOAT, []byte for BLOB, string for TEXT.
    708 //
    709 // The function can additionally be variadic, as long as the type of
    710 // the variadic argument is one of the above.
    711 //
    712 // If pure is true. SQLite will assume that the function's return
    713 // value depends only on its inputs, and make more aggressive
    714 // optimizations in its queries.
    715 //
    716 // See _example/go_custom_funcs for a detailed example.
    717 func (c *SQLiteConn) RegisterFunc(name string, impl any, pure bool) error {
    718 	var fi functionInfo
    719 	fi.f = reflect.ValueOf(impl)
    720 	t := fi.f.Type()
    721 	if t.Kind() != reflect.Func {
    722 		return errors.New("Non-function passed to RegisterFunc")
    723 	}
    724 	if t.NumOut() != 1 && t.NumOut() != 2 {
    725 		return errors.New("SQLite functions must return 1 or 2 values")
    726 	}
    727 	if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
    728 		return errors.New("Second return value of SQLite function must be error")
    729 	}
    730 
    731 	numArgs := t.NumIn()
    732 	if t.IsVariadic() {
    733 		numArgs--
    734 	}
    735 
    736 	for i := 0; i < numArgs; i++ {
    737 		conv, err := callbackArg(t.In(i))
    738 		if err != nil {
    739 			return err
    740 		}
    741 		fi.argConverters = append(fi.argConverters, conv)
    742 	}
    743 
    744 	if t.IsVariadic() {
    745 		conv, err := callbackArg(t.In(numArgs).Elem())
    746 		if err != nil {
    747 			return err
    748 		}
    749 		fi.variadicConverter = conv
    750 		// Pass -1 to sqlite so that it allows any number of
    751 		// arguments. The call helper verifies that the minimum number
    752 		// of arguments is present for variadic functions.
    753 		numArgs = -1
    754 	}
    755 
    756 	conv, err := callbackRet(t.Out(0))
    757 	if err != nil {
    758 		return err
    759 	}
    760 	fi.retConverter = conv
    761 
    762 	// fi must outlast the database connection, or we'll have dangling pointers.
    763 	c.funcs = append(c.funcs, &fi)
    764 
    765 	cname := C.CString(name)
    766 	defer C.free(unsafe.Pointer(cname))
    767 	opts := C.SQLITE_UTF8
    768 	if pure {
    769 		opts |= C.SQLITE_DETERMINISTIC
    770 	}
    771 	rv := sqlite3CreateFunction(c.db, cname, C.int(numArgs), C.int(opts), newHandle(c, &fi), C.callbackTrampoline, nil, nil)
    772 	if rv != C.SQLITE_OK {
    773 		return c.lastError()
    774 	}
    775 	return nil
    776 }
    777 
    778 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 {
    779 	return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(uintptr(pApp)), (*[0]byte)(xFunc), (*[0]byte)(xStep), (*[0]byte)(xFinal))
    780 }
    781 
    782 // RegisterAggregator makes a Go type available as a SQLite aggregation function.
    783 //
    784 // Because aggregation is incremental, it's implemented in Go with a
    785 // type that has 2 methods: func Step(values) accumulates one row of
    786 // data into the accumulator, and func Done() ret finalizes and
    787 // returns the aggregate value. "values" and "ret" may be any type
    788 // supported by RegisterFunc.
    789 //
    790 // RegisterAggregator takes as implementation a constructor function
    791 // that constructs an instance of the aggregator type each time an
    792 // aggregation begins. The constructor must return a pointer to a
    793 // type, or an interface that implements Step() and Done().
    794 //
    795 // The constructor function and the Step/Done methods may optionally
    796 // return an error in addition to their other return values.
    797 //
    798 // See _example/go_custom_funcs for a detailed example.
    799 func (c *SQLiteConn) RegisterAggregator(name string, impl any, pure bool) error {
    800 	var ai aggInfo
    801 	ai.constructor = reflect.ValueOf(impl)
    802 	t := ai.constructor.Type()
    803 	if t.Kind() != reflect.Func {
    804 		return errors.New("non-function passed to RegisterAggregator")
    805 	}
    806 	if t.NumOut() != 1 && t.NumOut() != 2 {
    807 		return errors.New("SQLite aggregator constructors must return 1 or 2 values")
    808 	}
    809 	if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
    810 		return errors.New("Second return value of SQLite function must be error")
    811 	}
    812 	if t.NumIn() != 0 {
    813 		return errors.New("SQLite aggregator constructors must not have arguments")
    814 	}
    815 
    816 	agg := t.Out(0)
    817 	switch agg.Kind() {
    818 	case reflect.Ptr, reflect.Interface:
    819 	default:
    820 		return errors.New("SQlite aggregator constructor must return a pointer object")
    821 	}
    822 	stepFn, found := agg.MethodByName("Step")
    823 	if !found {
    824 		return errors.New("SQlite aggregator doesn't have a Step() function")
    825 	}
    826 	step := stepFn.Type
    827 	if step.NumOut() != 0 && step.NumOut() != 1 {
    828 		return errors.New("SQlite aggregator Step() function must return 0 or 1 values")
    829 	}
    830 	if step.NumOut() == 1 && !step.Out(0).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
    831 		return errors.New("type of SQlite aggregator Step() return value must be error")
    832 	}
    833 
    834 	stepNArgs := step.NumIn()
    835 	start := 0
    836 	if agg.Kind() == reflect.Ptr {
    837 		// Skip over the method receiver
    838 		stepNArgs--
    839 		start++
    840 	}
    841 	if step.IsVariadic() {
    842 		stepNArgs--
    843 	}
    844 	for i := start; i < start+stepNArgs; i++ {
    845 		conv, err := callbackArg(step.In(i))
    846 		if err != nil {
    847 			return err
    848 		}
    849 		ai.stepArgConverters = append(ai.stepArgConverters, conv)
    850 	}
    851 	if step.IsVariadic() {
    852 		conv, err := callbackArg(step.In(start + stepNArgs).Elem())
    853 		if err != nil {
    854 			return err
    855 		}
    856 		ai.stepVariadicConverter = conv
    857 		// Pass -1 to sqlite so that it allows any number of
    858 		// arguments. The call helper verifies that the minimum number
    859 		// of arguments is present for variadic functions.
    860 		stepNArgs = -1
    861 	}
    862 
    863 	doneFn, found := agg.MethodByName("Done")
    864 	if !found {
    865 		return errors.New("SQlite aggregator doesn't have a Done() function")
    866 	}
    867 	done := doneFn.Type
    868 	doneNArgs := done.NumIn()
    869 	if agg.Kind() == reflect.Ptr {
    870 		// Skip over the method receiver
    871 		doneNArgs--
    872 	}
    873 	if doneNArgs != 0 {
    874 		return errors.New("SQlite aggregator Done() function must have no arguments")
    875 	}
    876 	if done.NumOut() != 1 && done.NumOut() != 2 {
    877 		return errors.New("SQLite aggregator Done() function must return 1 or 2 values")
    878 	}
    879 	if done.NumOut() == 2 && !done.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
    880 		return errors.New("second return value of SQLite aggregator Done() function must be error")
    881 	}
    882 
    883 	conv, err := callbackRet(done.Out(0))
    884 	if err != nil {
    885 		return err
    886 	}
    887 	ai.doneRetConverter = conv
    888 	ai.active = make(map[int64]reflect.Value)
    889 	ai.next = 1
    890 
    891 	// ai must outlast the database connection, or we'll have dangling pointers.
    892 	c.aggregators = append(c.aggregators, &ai)
    893 
    894 	cname := C.CString(name)
    895 	defer C.free(unsafe.Pointer(cname))
    896 	opts := C.SQLITE_UTF8
    897 	if pure {
    898 		opts |= C.SQLITE_DETERMINISTIC
    899 	}
    900 	rv := sqlite3CreateFunction(c.db, cname, C.int(stepNArgs), C.int(opts), newHandle(c, &ai), nil, C.stepTrampoline, C.doneTrampoline)
    901 	if rv != C.SQLITE_OK {
    902 		return c.lastError()
    903 	}
    904 	return nil
    905 }
    906 
    907 // AutoCommit return which currently auto commit or not.
    908 func (c *SQLiteConn) AutoCommit() bool {
    909 	c.mu.Lock()
    910 	defer c.mu.Unlock()
    911 	return int(C.sqlite3_get_autocommit(c.db)) != 0
    912 }
    913 
    914 func (c *SQLiteConn) lastError() error {
    915 	return lastError(c.db)
    916 }
    917 
    918 // Note: may be called with db == nil
    919 func lastError(db *C.sqlite3) error {
    920 	rv := C.sqlite3_errcode(db) // returns SQLITE_NOMEM if db == nil
    921 	if rv == C.SQLITE_OK {
    922 		return nil
    923 	}
    924 	extrv := C.sqlite3_extended_errcode(db)    // returns SQLITE_NOMEM if db == nil
    925 	errStr := C.GoString(C.sqlite3_errmsg(db)) // returns "out of memory" if db == nil
    926 
    927 	// https://www.sqlite.org/c3ref/system_errno.html
    928 	// sqlite3_system_errno is only meaningful if the error code was SQLITE_CANTOPEN,
    929 	// or it was SQLITE_IOERR and the extended code was not SQLITE_IOERR_NOMEM
    930 	var systemErrno syscall.Errno
    931 	if rv == C.SQLITE_CANTOPEN || (rv == C.SQLITE_IOERR && extrv != C.SQLITE_IOERR_NOMEM) {
    932 		systemErrno = syscall.Errno(C.sqlite3_system_errno(db))
    933 	}
    934 
    935 	return Error{
    936 		Code:         ErrNo(rv),
    937 		ExtendedCode: ErrNoExtended(extrv),
    938 		SystemErrno:  systemErrno,
    939 		err:          errStr,
    940 	}
    941 }
    942 
    943 // Exec implements Execer.
    944 func (c *SQLiteConn) Exec(query string, args []driver.Value) (driver.Result, error) {
    945 	return c.exec(context.Background(), query, valueToNamedValue(args))
    946 }
    947 
    948 func (c *SQLiteConn) exec(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
    949 	// Fast path: no args, no context cancellation → single CGO call per statement
    950 	if len(args) == 0 && ctx.Done() == nil {
    951 		return c.execNoArgs(query)
    952 	}
    953 
    954 	start := 0
    955 	for {
    956 		s, err := c.prepareWithCache(ctx, query)
    957 		if err != nil {
    958 			return nil, err
    959 		}
    960 		var res driver.Result
    961 		if s.(*SQLiteStmt).s != nil {
    962 			na := s.NumInput()
    963 			if len(args)-start < na {
    964 				s.Close()
    965 				return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args))
    966 			}
    967 			stmtArgs := stmtArgs(args, start, na)
    968 			res, err = s.(*SQLiteStmt).exec(ctx, stmtArgs)
    969 			if err != nil && err != driver.ErrSkip {
    970 				s.Close()
    971 				return nil, err
    972 			}
    973 			start += na
    974 		}
    975 		tail := s.(*SQLiteStmt).t
    976 		s.Close()
    977 		if tail == "" {
    978 			if res == nil {
    979 				// https://github.com/mattn/go-sqlite3/issues/963
    980 				res = &SQLiteResult{0, 0}
    981 			}
    982 			return res, nil
    983 		}
    984 		query = tail
    985 	}
    986 }
    987 
    988 // execNoArgs executes a query with no parameters in a single CGO call per statement.
    989 func (c *SQLiteConn) execNoArgs(query string) (driver.Result, error) {
    990 	var res *SQLiteResult
    991 	for len(query) > 0 {
    992 		var rowid, changes C.longlong
    993 		var tail *C.char
    994 		pquery := C.CString(query)
    995 		rv := C._sqlite3_exec_no_args(c.db, pquery, C.int(len(query)), &rowid, &changes, &tail)
    996 		if tail != nil && *tail != '\000' {
    997 			query = strings.TrimSpace(C.GoString(tail))
    998 		} else {
    999 			query = ""
   1000 		}
   1001 		C.free(unsafe.Pointer(pquery))
   1002 		if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
   1003 			return nil, c.lastError()
   1004 		}
   1005 		res = &SQLiteResult{id: int64(rowid), changes: int64(changes)}
   1006 	}
   1007 	if res == nil {
   1008 		res = &SQLiteResult{0, 0}
   1009 	}
   1010 	return res, nil
   1011 }
   1012 
   1013 // Query implements Queryer.
   1014 func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, error) {
   1015 	return c.query(context.Background(), query, valueToNamedValue(args))
   1016 }
   1017 
   1018 func (c *SQLiteConn) query(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
   1019 	start := 0
   1020 	for {
   1021 		s, err := c.prepareWithCache(ctx, query)
   1022 		if err != nil {
   1023 			return nil, err
   1024 		}
   1025 		ss := s.(*SQLiteStmt)
   1026 		ss.cls = true
   1027 		// sqlite3_prepare_v2 returns SQLITE_OK with a NULL statement handle
   1028 		// when the input is empty or contains only whitespace/comments.
   1029 		if ss.s == nil {
   1030 			tail := ss.t
   1031 			ss.Close()
   1032 			if tail == "" {
   1033 				return &SQLiteRows{cls: true, ctx: ctx}, nil
   1034 			}
   1035 			query = tail
   1036 			continue
   1037 		}
   1038 		na := s.NumInput()
   1039 		if len(args)-start < na {
   1040 			ss.Close()
   1041 			return nil, fmt.Errorf("not enough args to execute query: want %d got %d", na, len(args)-start)
   1042 		}
   1043 		stmtArgs := stmtArgs(args, start, na)
   1044 		rows, err := ss.query(ctx, stmtArgs)
   1045 		if err != nil && err != driver.ErrSkip {
   1046 			ss.Close()
   1047 			return rows, err
   1048 		}
   1049 		start += na
   1050 		tail := ss.t
   1051 		if tail == "" {
   1052 			return rows, nil
   1053 		}
   1054 		rows.Close()
   1055 		ss.Close()
   1056 		query = tail
   1057 	}
   1058 }
   1059 
   1060 // Begin transaction.
   1061 func (c *SQLiteConn) Begin() (driver.Tx, error) {
   1062 	return c.begin(context.Background())
   1063 }
   1064 
   1065 func (c *SQLiteConn) begin(ctx context.Context) (driver.Tx, error) {
   1066 	if _, err := c.exec(ctx, c.txlock, nil); err != nil {
   1067 		return nil, err
   1068 	}
   1069 	return &SQLiteTx{c}, nil
   1070 }
   1071 
   1072 // Open database and return a new connection.
   1073 //
   1074 // A pragma can take either zero or one argument.
   1075 // The argument is may be either in parentheses or it may be separated from
   1076 // the pragma name by an equal sign. The two syntaxes yield identical results.
   1077 // In many pragmas, the argument is a boolean. The boolean can be one of:
   1078 //
   1079 //	1 yes true on
   1080 //	0 no false off
   1081 //
   1082 // You can specify a DSN string using a URI as the filename.
   1083 //
   1084 //	test.db
   1085 //	file:test.db?cache=shared&mode=memory
   1086 //	:memory:
   1087 //	file::memory:
   1088 //
   1089 //	mode
   1090 //	  Access mode of the database.
   1091 //	  https://www.sqlite.org/c3ref/open.html
   1092 //	  Values:
   1093 //	   - ro
   1094 //	   - rw
   1095 //	   - rwc
   1096 //	   - memory
   1097 //
   1098 //	cache
   1099 //	  SQLite Shared-Cache Mode
   1100 //	  https://www.sqlite.org/sharedcache.html
   1101 //	  Values:
   1102 //	    - shared
   1103 //	    - private
   1104 //
   1105 //	immutable=Boolean
   1106 //	  The immutable parameter is a boolean query parameter that indicates
   1107 //	  that the database file is stored on read-only media. When immutable is set,
   1108 //	  SQLite assumes that the database file cannot be changed,
   1109 //	  even by a process with higher privilege,
   1110 //	  and so the database is opened read-only and all locking and change detection is disabled.
   1111 //	  Caution: Setting the immutable property on a database file that
   1112 //	  does in fact change can result in incorrect query results and/or SQLITE_CORRUPT errors.
   1113 //
   1114 // go-sqlite3 adds the following query parameters to those used by SQLite:
   1115 //
   1116 //	_loc=XXX
   1117 //	  Specify location of time format. It's possible to specify "auto".
   1118 //
   1119 //	_mutex=XXX
   1120 //	  Specify mutex mode. XXX can be "no", "full".
   1121 //
   1122 //	_txlock=XXX
   1123 //	  Specify locking behavior for transactions.  XXX can be "immediate",
   1124 //	  "deferred", "exclusive".
   1125 //
   1126 //	_auto_vacuum=X | _vacuum=X
   1127 //	  0 | none - Auto Vacuum disabled
   1128 //	  1 | full - Auto Vacuum FULL
   1129 //	  2 | incremental - Auto Vacuum Incremental
   1130 //
   1131 //	_busy_timeout=XXX"| _timeout=XXX
   1132 //	  Specify value for sqlite3_busy_timeout.
   1133 //
   1134 //	_case_sensitive_like=Boolean | _cslike=Boolean
   1135 //	  https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
   1136 //	  Default or disabled the LIKE operation is case-insensitive.
   1137 //	  When enabling this options behaviour of LIKE will become case-sensitive.
   1138 //
   1139 //	_defer_foreign_keys=Boolean | _defer_fk=Boolean
   1140 //	  Defer Foreign Keys until outermost transaction is committed.
   1141 //
   1142 //	_foreign_keys=Boolean | _fk=Boolean
   1143 //	  Enable or disable enforcement of foreign keys.
   1144 //
   1145 //	_ignore_check_constraints=Boolean
   1146 //	  This pragma enables or disables the enforcement of CHECK constraints.
   1147 //	  The default setting is off, meaning that CHECK constraints are enforced by default.
   1148 //
   1149 //	_journal_mode=MODE | _journal=MODE
   1150 //	  Set journal mode for the databases associated with the current connection.
   1151 //	  https://www.sqlite.org/pragma.html#pragma_journal_mode
   1152 //
   1153 //	_locking_mode=X | _locking=X
   1154 //	  Sets the database connection locking-mode.
   1155 //	  The locking-mode is either NORMAL or EXCLUSIVE.
   1156 //	  https://www.sqlite.org/pragma.html#pragma_locking_mode
   1157 //
   1158 //	_query_only=Boolean
   1159 //	  The query_only pragma prevents all changes to database files when enabled.
   1160 //
   1161 //	_recursive_triggers=Boolean | _rt=Boolean
   1162 //	  Enable or disable recursive triggers.
   1163 //
   1164 //	_secure_delete=Boolean|FAST
   1165 //	  When secure_delete is on, SQLite overwrites deleted content with zeros.
   1166 //	  https://www.sqlite.org/pragma.html#pragma_secure_delete
   1167 //
   1168 //	_synchronous=X | _sync=X
   1169 //	  Change the setting of the "synchronous" flag.
   1170 //	  https://www.sqlite.org/pragma.html#pragma_synchronous
   1171 //
   1172 //	_writable_schema=Boolean
   1173 //	  When this pragma is on, the SQLITE_MASTER tables in which database
   1174 //	  can be changed using ordinary UPDATE, INSERT, and DELETE statements.
   1175 //	  Warning: misuse of this pragma can easily result in a corrupt database file.
   1176 func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
   1177 	if C.sqlite3_threadsafe() == 0 {
   1178 		return nil, errors.New("sqlite library was not compiled for thread-safe operation")
   1179 	}
   1180 
   1181 	var pkey string
   1182 
   1183 	// Options
   1184 	var loc *time.Location
   1185 	authCreate := false
   1186 	authUser := ""
   1187 	authPass := ""
   1188 	authCrypt := ""
   1189 	authSalt := ""
   1190 	mutex := C.int(C.SQLITE_OPEN_FULLMUTEX)
   1191 	txlock := "BEGIN"
   1192 
   1193 	// PRAGMA's
   1194 	autoVacuum := -1
   1195 	busyTimeout := 5000
   1196 	caseSensitiveLike := -1
   1197 	deferForeignKeys := -1
   1198 	foreignKeys := -1
   1199 	ignoreCheckConstraints := -1
   1200 	var journalMode string
   1201 	lockingMode := "NORMAL"
   1202 	queryOnly := -1
   1203 	recursiveTriggers := -1
   1204 	secureDelete := "DEFAULT"
   1205 	synchronousMode := "NORMAL"
   1206 	writableSchema := -1
   1207 	vfsName := ""
   1208 	var cacheSize *int64
   1209 	stmtCacheSize := 0
   1210 
   1211 	pos := strings.IndexRune(dsn, '?')
   1212 	if pos >= 1 {
   1213 		params, err := url.ParseQuery(dsn[pos+1:])
   1214 		if err != nil {
   1215 			return nil, err
   1216 		}
   1217 
   1218 		// Authentication
   1219 		if _, ok := params["_auth"]; ok {
   1220 			authCreate = true
   1221 		}
   1222 		if val := params.Get("_auth_user"); val != "" {
   1223 			authUser = val
   1224 		}
   1225 		if val := params.Get("_auth_pass"); val != "" {
   1226 			authPass = val
   1227 		}
   1228 		if val := params.Get("_auth_crypt"); val != "" {
   1229 			authCrypt = val
   1230 		}
   1231 		if val := params.Get("_auth_salt"); val != "" {
   1232 			authSalt = val
   1233 		}
   1234 
   1235 		// _loc
   1236 		if val := params.Get("_loc"); val != "" {
   1237 			switch strings.ToLower(val) {
   1238 			case "auto":
   1239 				loc = time.Local
   1240 			default:
   1241 				loc, err = time.LoadLocation(val)
   1242 				if err != nil {
   1243 					return nil, fmt.Errorf("Invalid _loc: %v: %v", val, err)
   1244 				}
   1245 			}
   1246 		}
   1247 
   1248 		// _mutex
   1249 		if val := params.Get("_mutex"); val != "" {
   1250 			switch strings.ToLower(val) {
   1251 			case "no":
   1252 				mutex = C.SQLITE_OPEN_NOMUTEX
   1253 			case "full":
   1254 				mutex = C.SQLITE_OPEN_FULLMUTEX
   1255 			default:
   1256 				return nil, fmt.Errorf("Invalid _mutex: %v", val)
   1257 			}
   1258 		}
   1259 
   1260 		// _txlock
   1261 		if val := params.Get("_txlock"); val != "" {
   1262 			switch strings.ToLower(val) {
   1263 			case "immediate":
   1264 				txlock = "BEGIN IMMEDIATE"
   1265 			case "exclusive":
   1266 				txlock = "BEGIN EXCLUSIVE"
   1267 			case "deferred":
   1268 				txlock = "BEGIN"
   1269 			default:
   1270 				return nil, fmt.Errorf("Invalid _txlock: %v", val)
   1271 			}
   1272 		}
   1273 
   1274 		// Auto Vacuum (_vacuum)
   1275 		//
   1276 		// https://www.sqlite.org/pragma.html#pragma_auto_vacuum
   1277 		//
   1278 		pkey = "" // Reset pkey
   1279 		if _, ok := params["_auto_vacuum"]; ok {
   1280 			pkey = "_auto_vacuum"
   1281 		}
   1282 		if _, ok := params["_vacuum"]; ok {
   1283 			pkey = "_vacuum"
   1284 		}
   1285 		if val := params.Get(pkey); val != "" {
   1286 			switch strings.ToLower(val) {
   1287 			case "0", "none":
   1288 				autoVacuum = 0
   1289 			case "1", "full":
   1290 				autoVacuum = 1
   1291 			case "2", "incremental":
   1292 				autoVacuum = 2
   1293 			default:
   1294 				return nil, fmt.Errorf("Invalid _auto_vacuum: %v, expecting value of '0 NONE 1 FULL 2 INCREMENTAL'", val)
   1295 			}
   1296 		}
   1297 
   1298 		// Busy Timeout (_busy_timeout)
   1299 		//
   1300 		// https://www.sqlite.org/pragma.html#pragma_busy_timeout
   1301 		//
   1302 		pkey = "" // Reset pkey
   1303 		if _, ok := params["_busy_timeout"]; ok {
   1304 			pkey = "_busy_timeout"
   1305 		}
   1306 		if _, ok := params["_timeout"]; ok {
   1307 			pkey = "_timeout"
   1308 		}
   1309 		if val := params.Get(pkey); val != "" {
   1310 			iv, err := strconv.ParseInt(val, 10, 64)
   1311 			if err != nil {
   1312 				return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
   1313 			}
   1314 			busyTimeout = int(iv)
   1315 		}
   1316 
   1317 		// Case Sensitive Like (_cslike)
   1318 		//
   1319 		// https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
   1320 		//
   1321 		pkey = "" // Reset pkey
   1322 		if _, ok := params["_case_sensitive_like"]; ok {
   1323 			pkey = "_case_sensitive_like"
   1324 		}
   1325 		if _, ok := params["_cslike"]; ok {
   1326 			pkey = "_cslike"
   1327 		}
   1328 		if val := params.Get(pkey); val != "" {
   1329 			switch strings.ToLower(val) {
   1330 			case "0", "no", "false", "off":
   1331 				caseSensitiveLike = 0
   1332 			case "1", "yes", "true", "on":
   1333 				caseSensitiveLike = 1
   1334 			default:
   1335 				return nil, fmt.Errorf("Invalid _case_sensitive_like: %v, expecting boolean value of '0 1 false true no yes off on'", val)
   1336 			}
   1337 		}
   1338 
   1339 		// Defer Foreign Keys (_defer_foreign_keys | _defer_fk)
   1340 		//
   1341 		// https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys
   1342 		//
   1343 		pkey = "" // Reset pkey
   1344 		if _, ok := params["_defer_foreign_keys"]; ok {
   1345 			pkey = "_defer_foreign_keys"
   1346 		}
   1347 		if _, ok := params["_defer_fk"]; ok {
   1348 			pkey = "_defer_fk"
   1349 		}
   1350 		if val := params.Get(pkey); val != "" {
   1351 			switch strings.ToLower(val) {
   1352 			case "0", "no", "false", "off":
   1353 				deferForeignKeys = 0
   1354 			case "1", "yes", "true", "on":
   1355 				deferForeignKeys = 1
   1356 			default:
   1357 				return nil, fmt.Errorf("Invalid _defer_foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
   1358 			}
   1359 		}
   1360 
   1361 		// Foreign Keys (_foreign_keys | _fk)
   1362 		//
   1363 		// https://www.sqlite.org/pragma.html#pragma_foreign_keys
   1364 		//
   1365 		pkey = "" // Reset pkey
   1366 		if _, ok := params["_foreign_keys"]; ok {
   1367 			pkey = "_foreign_keys"
   1368 		}
   1369 		if _, ok := params["_fk"]; ok {
   1370 			pkey = "_fk"
   1371 		}
   1372 		if val := params.Get(pkey); val != "" {
   1373 			switch strings.ToLower(val) {
   1374 			case "0", "no", "false", "off":
   1375 				foreignKeys = 0
   1376 			case "1", "yes", "true", "on":
   1377 				foreignKeys = 1
   1378 			default:
   1379 				return nil, fmt.Errorf("Invalid _foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
   1380 			}
   1381 		}
   1382 
   1383 		// Ignore CHECK Constrains (_ignore_check_constraints)
   1384 		//
   1385 		// https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints
   1386 		//
   1387 		if val := params.Get("_ignore_check_constraints"); val != "" {
   1388 			switch strings.ToLower(val) {
   1389 			case "0", "no", "false", "off":
   1390 				ignoreCheckConstraints = 0
   1391 			case "1", "yes", "true", "on":
   1392 				ignoreCheckConstraints = 1
   1393 			default:
   1394 				return nil, fmt.Errorf("Invalid _ignore_check_constraints: %v, expecting boolean value of '0 1 false true no yes off on'", val)
   1395 			}
   1396 		}
   1397 
   1398 		// Journal Mode (_journal_mode | _journal)
   1399 		//
   1400 		// https://www.sqlite.org/pragma.html#pragma_journal_mode
   1401 		//
   1402 		pkey = "" // Reset pkey
   1403 		if _, ok := params["_journal_mode"]; ok {
   1404 			pkey = "_journal_mode"
   1405 		}
   1406 		if _, ok := params["_journal"]; ok {
   1407 			pkey = "_journal"
   1408 		}
   1409 		if val := params.Get(pkey); val != "" {
   1410 			switch strings.ToUpper(val) {
   1411 			case "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "OFF":
   1412 				journalMode = strings.ToUpper(val)
   1413 			case "WAL":
   1414 				journalMode = strings.ToUpper(val)
   1415 
   1416 				// For WAL Mode set Synchronous Mode to 'NORMAL'
   1417 				// See https://www.sqlite.org/pragma.html#pragma_synchronous
   1418 				synchronousMode = "NORMAL"
   1419 			default:
   1420 				return nil, fmt.Errorf("Invalid _journal: %v, expecting value of 'DELETE TRUNCATE PERSIST MEMORY WAL OFF'", val)
   1421 			}
   1422 		}
   1423 
   1424 		// Locking Mode (_locking)
   1425 		//
   1426 		// https://www.sqlite.org/pragma.html#pragma_locking_mode
   1427 		//
   1428 		pkey = "" // Reset pkey
   1429 		if _, ok := params["_locking_mode"]; ok {
   1430 			pkey = "_locking_mode"
   1431 		}
   1432 		if _, ok := params["_locking"]; ok {
   1433 			pkey = "_locking"
   1434 		}
   1435 		if val := params.Get(pkey); val != "" {
   1436 			switch strings.ToUpper(val) {
   1437 			case "NORMAL", "EXCLUSIVE":
   1438 				lockingMode = strings.ToUpper(val)
   1439 			default:
   1440 				return nil, fmt.Errorf("Invalid _locking_mode: %v, expecting value of 'NORMAL EXCLUSIVE", val)
   1441 			}
   1442 		}
   1443 
   1444 		// Query Only (_query_only)
   1445 		//
   1446 		// https://www.sqlite.org/pragma.html#pragma_query_only
   1447 		//
   1448 		if val := params.Get("_query_only"); val != "" {
   1449 			switch strings.ToLower(val) {
   1450 			case "0", "no", "false", "off":
   1451 				queryOnly = 0
   1452 			case "1", "yes", "true", "on":
   1453 				queryOnly = 1
   1454 			default:
   1455 				return nil, fmt.Errorf("Invalid _query_only: %v, expecting boolean value of '0 1 false true no yes off on'", val)
   1456 			}
   1457 		}
   1458 
   1459 		// Recursive Triggers (_recursive_triggers)
   1460 		//
   1461 		// https://www.sqlite.org/pragma.html#pragma_recursive_triggers
   1462 		//
   1463 		pkey = "" // Reset pkey
   1464 		if _, ok := params["_recursive_triggers"]; ok {
   1465 			pkey = "_recursive_triggers"
   1466 		}
   1467 		if _, ok := params["_rt"]; ok {
   1468 			pkey = "_rt"
   1469 		}
   1470 		if val := params.Get(pkey); val != "" {
   1471 			switch strings.ToLower(val) {
   1472 			case "0", "no", "false", "off":
   1473 				recursiveTriggers = 0
   1474 			case "1", "yes", "true", "on":
   1475 				recursiveTriggers = 1
   1476 			default:
   1477 				return nil, fmt.Errorf("Invalid _recursive_triggers: %v, expecting boolean value of '0 1 false true no yes off on'", val)
   1478 			}
   1479 		}
   1480 
   1481 		// Secure Delete (_secure_delete)
   1482 		//
   1483 		// https://www.sqlite.org/pragma.html#pragma_secure_delete
   1484 		//
   1485 		if val := params.Get("_secure_delete"); val != "" {
   1486 			switch strings.ToLower(val) {
   1487 			case "0", "no", "false", "off":
   1488 				secureDelete = "OFF"
   1489 			case "1", "yes", "true", "on":
   1490 				secureDelete = "ON"
   1491 			case "fast":
   1492 				secureDelete = "FAST"
   1493 			default:
   1494 				return nil, fmt.Errorf("Invalid _secure_delete: %v, expecting boolean value of '0 1 false true no yes off on fast'", val)
   1495 			}
   1496 		}
   1497 
   1498 		// Synchronous Mode (_synchronous | _sync)
   1499 		//
   1500 		// https://www.sqlite.org/pragma.html#pragma_synchronous
   1501 		//
   1502 		pkey = "" // Reset pkey
   1503 		if _, ok := params["_synchronous"]; ok {
   1504 			pkey = "_synchronous"
   1505 		}
   1506 		if _, ok := params["_sync"]; ok {
   1507 			pkey = "_sync"
   1508 		}
   1509 		if val := params.Get(pkey); val != "" {
   1510 			switch strings.ToUpper(val) {
   1511 			case "0", "OFF", "1", "NORMAL", "2", "FULL", "3", "EXTRA":
   1512 				synchronousMode = strings.ToUpper(val)
   1513 			default:
   1514 				return nil, fmt.Errorf("Invalid _synchronous: %v, expecting value of '0 OFF 1 NORMAL 2 FULL 3 EXTRA'", val)
   1515 			}
   1516 		}
   1517 
   1518 		// Writable Schema (_writeable_schema)
   1519 		//
   1520 		// https://www.sqlite.org/pragma.html#pragma_writeable_schema
   1521 		//
   1522 		if val := params.Get("_writable_schema"); val != "" {
   1523 			switch strings.ToLower(val) {
   1524 			case "0", "no", "false", "off":
   1525 				writableSchema = 0
   1526 			case "1", "yes", "true", "on":
   1527 				writableSchema = 1
   1528 			default:
   1529 				return nil, fmt.Errorf("Invalid _writable_schema: %v, expecting boolean value of '0 1 false true no yes off on'", val)
   1530 			}
   1531 		}
   1532 
   1533 		// Cache size (_cache_size)
   1534 		//
   1535 		// https://sqlite.org/pragma.html#pragma_cache_size
   1536 		//
   1537 		if val := params.Get("_cache_size"); val != "" {
   1538 			iv, err := strconv.ParseInt(val, 10, 64)
   1539 			if err != nil {
   1540 				return nil, fmt.Errorf("Invalid _cache_size: %v: %v", val, err)
   1541 			}
   1542 			cacheSize = &iv
   1543 		}
   1544 
   1545 		// _stmt_cache_size sets the maximum number of prepared statements
   1546 		// cached per connection. Note that sql.DB is a connection pool, so
   1547 		// each connection maintains its own independent cache.
   1548 		if val := params.Get("_stmt_cache_size"); val != "" {
   1549 			iv, err := strconv.Atoi(val)
   1550 			if err != nil {
   1551 				return nil, fmt.Errorf("Invalid _stmt_cache_size: %v: %v", val, err)
   1552 			}
   1553 			if iv < 0 {
   1554 				return nil, fmt.Errorf("Invalid _stmt_cache_size: %v, expecting non-negative integer", val)
   1555 			}
   1556 			stmtCacheSize = iv
   1557 		}
   1558 
   1559 		if val := params.Get("vfs"); val != "" {
   1560 			vfsName = val
   1561 		}
   1562 
   1563 		if !strings.HasPrefix(dsn, "file:") {
   1564 			dsn = dsn[:pos]
   1565 		}
   1566 	}
   1567 
   1568 	var db *C.sqlite3
   1569 	name := C.CString(dsn)
   1570 	defer C.free(unsafe.Pointer(name))
   1571 	var vfs *C.char
   1572 	if vfsName != "" {
   1573 		vfs = C.CString(vfsName)
   1574 		defer C.free(unsafe.Pointer(vfs))
   1575 	}
   1576 	rv := C._sqlite3_open_v2(name, &db,
   1577 		mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
   1578 		vfs)
   1579 	if rv != 0 {
   1580 		// Save off the error _before_ closing the database.
   1581 		// This is safe even if db is nil.
   1582 		err := lastError(db)
   1583 		if db != nil {
   1584 			C.sqlite3_close_v2(db)
   1585 		}
   1586 		return nil, err
   1587 	}
   1588 	if db == nil {
   1589 		return nil, errors.New("sqlite succeeded without returning a database")
   1590 	}
   1591 
   1592 	exec := func(s string) error {
   1593 		cs := C.CString(s)
   1594 		rv := C.sqlite3_exec(db, cs, nil, nil, nil)
   1595 		C.free(unsafe.Pointer(cs))
   1596 		if rv != C.SQLITE_OK {
   1597 			return lastError(db)
   1598 		}
   1599 		return nil
   1600 	}
   1601 
   1602 	// Busy timeout
   1603 	if err := exec(fmt.Sprintf("PRAGMA busy_timeout = %d;", busyTimeout)); err != nil {
   1604 		C.sqlite3_close_v2(db)
   1605 		return nil, err
   1606 	}
   1607 
   1608 	// USER AUTHENTICATION
   1609 	//
   1610 	// User Authentication is always performed even when
   1611 	// sqlite_userauth is not compiled in, because without user authentication
   1612 	// the authentication is a no-op.
   1613 	//
   1614 	// Workflow
   1615 	//	- Authenticate
   1616 	//		ON::SUCCESS		=> Continue
   1617 	//		ON::SQLITE_AUTH => Return error and exit Open(...)
   1618 	//
   1619 	//  - Activate User Authentication
   1620 	//		Check if the user wants to activate User Authentication.
   1621 	//		If so then first create a temporary AuthConn to the database
   1622 	//		This is possible because we are already successfully authenticated.
   1623 	//
   1624 	//	- Check if `sqlite_user`` table exists
   1625 	//		YES				=> Add the provided user from DSN as Admin User and
   1626 	//						   activate user authentication.
   1627 	//		NO				=> Continue
   1628 	//
   1629 
   1630 	// Create connection to SQLite
   1631 	conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
   1632 	if stmtCacheSize > 0 {
   1633 		conn.stmtCache = make([]*SQLiteStmt, 0, stmtCacheSize)
   1634 		conn.stmtCacheEnabled = true
   1635 	}
   1636 
   1637 	// Password Cipher has to be registered before authentication
   1638 	if len(authCrypt) > 0 {
   1639 		switch strings.ToUpper(authCrypt) {
   1640 		case "SHA1":
   1641 			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA1, true); err != nil {
   1642 				return nil, fmt.Errorf("CryptEncoderSHA1: %s", err)
   1643 			}
   1644 		case "SSHA1":
   1645 			if len(authSalt) == 0 {
   1646 				return nil, fmt.Errorf("_auth_crypt=ssha1, requires _auth_salt")
   1647 			}
   1648 			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA1(authSalt), true); err != nil {
   1649 				return nil, fmt.Errorf("CryptEncoderSSHA1: %s", err)
   1650 			}
   1651 		case "SHA256":
   1652 			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA256, true); err != nil {
   1653 				return nil, fmt.Errorf("CryptEncoderSHA256: %s", err)
   1654 			}
   1655 		case "SSHA256":
   1656 			if len(authSalt) == 0 {
   1657 				return nil, fmt.Errorf("_auth_crypt=ssha256, requires _auth_salt")
   1658 			}
   1659 			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA256(authSalt), true); err != nil {
   1660 				return nil, fmt.Errorf("CryptEncoderSSHA256: %s", err)
   1661 			}
   1662 		case "SHA384":
   1663 			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA384, true); err != nil {
   1664 				return nil, fmt.Errorf("CryptEncoderSHA384: %s", err)
   1665 			}
   1666 		case "SSHA384":
   1667 			if len(authSalt) == 0 {
   1668 				return nil, fmt.Errorf("_auth_crypt=ssha384, requires _auth_salt")
   1669 			}
   1670 			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA384(authSalt), true); err != nil {
   1671 				return nil, fmt.Errorf("CryptEncoderSSHA384: %s", err)
   1672 			}
   1673 		case "SHA512":
   1674 			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA512, true); err != nil {
   1675 				return nil, fmt.Errorf("CryptEncoderSHA512: %s", err)
   1676 			}
   1677 		case "SSHA512":
   1678 			if len(authSalt) == 0 {
   1679 				return nil, fmt.Errorf("_auth_crypt=ssha512, requires _auth_salt")
   1680 			}
   1681 			if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA512(authSalt), true); err != nil {
   1682 				return nil, fmt.Errorf("CryptEncoderSSHA512: %s", err)
   1683 			}
   1684 		}
   1685 	}
   1686 
   1687 	// Preform Authentication
   1688 	if err := conn.Authenticate(authUser, authPass); err != nil {
   1689 		return nil, err
   1690 	}
   1691 
   1692 	// Register: authenticate
   1693 	// Authenticate will perform an authentication of the provided username
   1694 	// and password against the database.
   1695 	//
   1696 	// If a database contains the SQLITE_USER table, then the
   1697 	// call to Authenticate must be invoked with an
   1698 	// appropriate username and password prior to enable read and write
   1699 	//access to the database.
   1700 	//
   1701 	// Return SQLITE_OK on success or SQLITE_ERROR if the username/password
   1702 	// combination is incorrect or unknown.
   1703 	//
   1704 	// If the SQLITE_USER table is not present in the database file, then
   1705 	// this interface is a harmless no-op returnning SQLITE_OK.
   1706 	if err := conn.RegisterFunc("authenticate", conn.authenticate, true); err != nil {
   1707 		return nil, err
   1708 	}
   1709 	//
   1710 	// Register: auth_user_add
   1711 	// auth_user_add can be used (by an admin user only)
   1712 	// to create a new user. When called on a no-authentication-required
   1713 	// database, this routine converts the database into an authentication-
   1714 	// required database, automatically makes the added user an
   1715 	// administrator, and logs in the current connection as that user.
   1716 	// The AuthUserAdd only works for the "main" database, not
   1717 	// for any ATTACH-ed databases. Any call to AuthUserAdd by a
   1718 	// non-admin user results in an error.
   1719 	if err := conn.RegisterFunc("auth_user_add", conn.authUserAdd, true); err != nil {
   1720 		return nil, err
   1721 	}
   1722 	//
   1723 	// Register: auth_user_change
   1724 	// auth_user_change can be used to change a users
   1725 	// login credentials or admin privilege.  Any user can change their own
   1726 	// login credentials. Only an admin user can change another users login
   1727 	// credentials or admin privilege setting. No user may change their own
   1728 	// admin privilege setting.
   1729 	if err := conn.RegisterFunc("auth_user_change", conn.authUserChange, true); err != nil {
   1730 		return nil, err
   1731 	}
   1732 	//
   1733 	// Register: auth_user_delete
   1734 	// auth_user_delete can be used (by an admin user only)
   1735 	// to delete a user. The currently logged-in user cannot be deleted,
   1736 	// which guarantees that there is always an admin user and hence that
   1737 	// the database cannot be converted into a no-authentication-required
   1738 	// database.
   1739 	if err := conn.RegisterFunc("auth_user_delete", conn.authUserDelete, true); err != nil {
   1740 		return nil, err
   1741 	}
   1742 
   1743 	// Register: auth_enabled
   1744 	// auth_enabled can be used to check if user authentication is enabled
   1745 	if err := conn.RegisterFunc("auth_enabled", conn.authEnabled, true); err != nil {
   1746 		return nil, err
   1747 	}
   1748 
   1749 	// Auto Vacuum
   1750 	// Moved auto_vacuum command, the user preference for auto_vacuum needs to be implemented directly after
   1751 	// the authentication and before the sqlite_user table gets created if the user
   1752 	// decides to activate User Authentication because
   1753 	// auto_vacuum needs to be set before any tables are created
   1754 	// and activating user authentication creates the internal table `sqlite_user`.
   1755 	if autoVacuum > -1 {
   1756 		if err := exec(fmt.Sprintf("PRAGMA auto_vacuum = %d;", autoVacuum)); err != nil {
   1757 			C.sqlite3_close_v2(db)
   1758 			return nil, err
   1759 		}
   1760 	}
   1761 
   1762 	// Check if user wants to activate User Authentication
   1763 	if authCreate {
   1764 		// Before going any further, we need to check that the user
   1765 		// has provided an username and password within the DSN.
   1766 		// We are not allowed to continue.
   1767 		if len(authUser) == 0 {
   1768 			return nil, fmt.Errorf("Missing '_auth_user' while user authentication was requested with '_auth'")
   1769 		}
   1770 		if len(authPass) == 0 {
   1771 			return nil, fmt.Errorf("Missing '_auth_pass' while user authentication was requested with '_auth'")
   1772 		}
   1773 
   1774 		// Check if User Authentication is Enabled
   1775 		authExists := conn.AuthEnabled()
   1776 		if !authExists {
   1777 			if err := conn.AuthUserAdd(authUser, authPass, true); err != nil {
   1778 				return nil, err
   1779 			}
   1780 		}
   1781 	}
   1782 
   1783 	// Case Sensitive LIKE
   1784 	if caseSensitiveLike > -1 {
   1785 		if err := exec(fmt.Sprintf("PRAGMA case_sensitive_like = %d;", caseSensitiveLike)); err != nil {
   1786 			C.sqlite3_close_v2(db)
   1787 			return nil, err
   1788 		}
   1789 	}
   1790 
   1791 	// Defer Foreign Keys
   1792 	if deferForeignKeys > -1 {
   1793 		if err := exec(fmt.Sprintf("PRAGMA defer_foreign_keys = %d;", deferForeignKeys)); err != nil {
   1794 			C.sqlite3_close_v2(db)
   1795 			return nil, err
   1796 		}
   1797 	}
   1798 
   1799 	// Foreign Keys
   1800 	if foreignKeys > -1 {
   1801 		if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil {
   1802 			C.sqlite3_close_v2(db)
   1803 			return nil, err
   1804 		}
   1805 	}
   1806 
   1807 	// Ignore CHECK Constraints
   1808 	if ignoreCheckConstraints > -1 {
   1809 		if err := exec(fmt.Sprintf("PRAGMA ignore_check_constraints = %d;", ignoreCheckConstraints)); err != nil {
   1810 			C.sqlite3_close_v2(db)
   1811 			return nil, err
   1812 		}
   1813 	}
   1814 
   1815 	// Journal Mode
   1816 	if journalMode != "" {
   1817 		if err := exec(fmt.Sprintf("PRAGMA journal_mode = %s;", journalMode)); err != nil {
   1818 			C.sqlite3_close_v2(db)
   1819 			return nil, err
   1820 		}
   1821 	}
   1822 
   1823 	// Locking Mode
   1824 	// Because the default is NORMAL and this is not changed in this package
   1825 	// by using the compile time SQLITE_DEFAULT_LOCKING_MODE this PRAGMA can always be executed
   1826 	if err := exec(fmt.Sprintf("PRAGMA locking_mode = %s;", lockingMode)); err != nil {
   1827 		C.sqlite3_close_v2(db)
   1828 		return nil, err
   1829 	}
   1830 
   1831 	// Query Only
   1832 	if queryOnly > -1 {
   1833 		if err := exec(fmt.Sprintf("PRAGMA query_only = %d;", queryOnly)); err != nil {
   1834 			C.sqlite3_close_v2(db)
   1835 			return nil, err
   1836 		}
   1837 	}
   1838 
   1839 	// Recursive Triggers
   1840 	if recursiveTriggers > -1 {
   1841 		if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {
   1842 			C.sqlite3_close_v2(db)
   1843 			return nil, err
   1844 		}
   1845 	}
   1846 
   1847 	// Secure Delete
   1848 	//
   1849 	// Because this package can set the compile time flag SQLITE_SECURE_DELETE with a build tag
   1850 	// the default value for secureDelete var is 'DEFAULT' this way
   1851 	// you can compile with secure_delete 'ON' and disable it for a specific database connection.
   1852 	if secureDelete != "DEFAULT" {
   1853 		if err := exec(fmt.Sprintf("PRAGMA secure_delete = %s;", secureDelete)); err != nil {
   1854 			C.sqlite3_close_v2(db)
   1855 			return nil, err
   1856 		}
   1857 	}
   1858 
   1859 	// Synchronous Mode
   1860 	//
   1861 	// Because default is NORMAL this statement is always executed
   1862 	if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
   1863 		conn.Close()
   1864 		return nil, err
   1865 	}
   1866 
   1867 	// Writable Schema
   1868 	if writableSchema > -1 {
   1869 		if err := exec(fmt.Sprintf("PRAGMA writable_schema = %d;", writableSchema)); err != nil {
   1870 			C.sqlite3_close_v2(db)
   1871 			return nil, err
   1872 		}
   1873 	}
   1874 
   1875 	// Cache Size
   1876 	if cacheSize != nil {
   1877 		if err := exec(fmt.Sprintf("PRAGMA cache_size = %d;", *cacheSize)); err != nil {
   1878 			C.sqlite3_close_v2(db)
   1879 			return nil, err
   1880 		}
   1881 	}
   1882 
   1883 	if len(d.Extensions) > 0 {
   1884 		if err := conn.loadExtensions(d.Extensions); err != nil {
   1885 			conn.Close()
   1886 			return nil, err
   1887 		}
   1888 	}
   1889 
   1890 	if d.ConnectHook != nil {
   1891 		if err := d.ConnectHook(conn); err != nil {
   1892 			conn.Close()
   1893 			return nil, err
   1894 		}
   1895 	}
   1896 	runtime.SetFinalizer(conn, (*SQLiteConn).Close)
   1897 	return conn, nil
   1898 }
   1899 
   1900 // Close the connection.
   1901 func (c *SQLiteConn) Close() error {
   1902 	c.mu.Lock()
   1903 	defer c.mu.Unlock()
   1904 	if c.db == nil {
   1905 		return nil
   1906 	}
   1907 	runtime.SetFinalizer(c, nil)
   1908 	c.closeCachedStmtsLocked()
   1909 	rv := C.sqlite3_close_v2(c.db)
   1910 	if rv != C.SQLITE_OK {
   1911 		return lastError(c.db)
   1912 	}
   1913 	deleteHandles(c)
   1914 	c.db = nil
   1915 	return nil
   1916 }
   1917 
   1918 func (c *SQLiteConn) dbConnOpen() bool {
   1919 	if c == nil {
   1920 		return false
   1921 	}
   1922 	c.mu.Lock()
   1923 	defer c.mu.Unlock()
   1924 	return c.db != nil
   1925 }
   1926 
   1927 func (c *SQLiteConn) takeCachedStmt(query string) *SQLiteStmt {
   1928 	if c == nil || query == "" || !c.stmtCacheEnabled {
   1929 		return nil
   1930 	}
   1931 
   1932 	c.mu.Lock()
   1933 	defer c.mu.Unlock()
   1934 
   1935 	if c.db == nil {
   1936 		return nil
   1937 	}
   1938 	// Scan from the MRU end (tail) so that a stmt put just before is
   1939 	// found immediately.
   1940 	for i := len(c.stmtCache) - 1; i >= 0; i-- {
   1941 		s := c.stmtCache[i]
   1942 		if s.cacheKey != query {
   1943 			continue
   1944 		}
   1945 		n := len(c.stmtCache)
   1946 		copy(c.stmtCache[i:n-1], c.stmtCache[i+1:n])
   1947 		c.stmtCache[n-1] = nil
   1948 		c.stmtCache = c.stmtCache[:n-1]
   1949 		// The stmt was marked closed by Close before being cached, and
   1950 		// cls may have been set if Query opened it; reset both so the
   1951 		// caller gets a stmt equivalent to a fresh Prepare.
   1952 		s.closed = false
   1953 		s.cls = false
   1954 		return s
   1955 	}
   1956 	return nil
   1957 }
   1958 
   1959 func (c *SQLiteConn) putCachedStmt(s *SQLiteStmt) bool {
   1960 	if c == nil || s == nil || s.s == nil || s.cacheKey == "" {
   1961 		return false
   1962 	}
   1963 
   1964 	c.mu.Lock()
   1965 	defer c.mu.Unlock()
   1966 
   1967 	if c.db == nil {
   1968 		return false
   1969 	}
   1970 	rv := C._sqlite3_reset_clear(s.s)
   1971 	if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
   1972 		return false
   1973 	}
   1974 	// If full, finalize the LRU entry at index 0 and shift left; the
   1975 	// freed tail slot is immediately reused by the append below.
   1976 	if len(c.stmtCache) == cap(c.stmtCache) {
   1977 		finalizeCachedStmt(c.stmtCache[0])
   1978 		copy(c.stmtCache, c.stmtCache[1:])
   1979 		c.stmtCache = c.stmtCache[:len(c.stmtCache)-1]
   1980 	}
   1981 	c.stmtCache = append(c.stmtCache, s)
   1982 	return true
   1983 }
   1984 
   1985 func (c *SQLiteConn) closeCachedStmtsLocked() {
   1986 	for i, s := range c.stmtCache {
   1987 		c.stmtCache[i] = nil
   1988 		finalizeCachedStmt(s)
   1989 	}
   1990 	c.stmtCache = c.stmtCache[:0]
   1991 }
   1992 
   1993 // finalizeCachedStmt tears down a stmt that was sitting in the connection's
   1994 // stmt cache. The caller must hold c.mu. It is safe to pass a nil stmt or a
   1995 // stmt whose handle has already been released.
   1996 func finalizeCachedStmt(s *SQLiteStmt) {
   1997 	if s == nil {
   1998 		return
   1999 	}
   2000 	runtime.SetFinalizer(s, nil)
   2001 	if s.s != nil {
   2002 		C.sqlite3_finalize(s.s)
   2003 		s.s = nil
   2004 	}
   2005 	s.c = nil
   2006 	s.closed = true
   2007 }
   2008 
   2009 // Prepare the query string. Return a new statement.
   2010 func (c *SQLiteConn) Prepare(query string) (driver.Stmt, error) {
   2011 	return c.prepare(context.Background(), query)
   2012 }
   2013 
   2014 func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
   2015 	pquery := C.CString(query)
   2016 	defer C.free(unsafe.Pointer(pquery))
   2017 	var s *C.sqlite3_stmt
   2018 	var tail *C.char
   2019 	rv := C._sqlite3_prepare_v2_internal(c.db, pquery, C.int(len(query)), &s, &tail)
   2020 	if rv != C.SQLITE_OK {
   2021 		return nil, c.lastError()
   2022 	}
   2023 	var t string
   2024 	if tail != nil && *tail != '\000' {
   2025 		t = strings.TrimSpace(C.GoString(tail))
   2026 	}
   2027 	ss := &SQLiteStmt{c: c, s: s, t: t}
   2028 	runtime.SetFinalizer(ss, (*SQLiteStmt).Close)
   2029 	return ss, nil
   2030 }
   2031 
   2032 func (c *SQLiteConn) prepareWithCache(ctx context.Context, query string) (driver.Stmt, error) {
   2033 	if stmt := c.takeCachedStmt(query); stmt != nil {
   2034 		return stmt, nil
   2035 	}
   2036 	stmt, err := c.prepare(ctx, query)
   2037 	if err != nil {
   2038 		return nil, err
   2039 	}
   2040 	ss := stmt.(*SQLiteStmt)
   2041 	if ss.t == "" && c.stmtCacheEnabled {
   2042 		ss.cacheKey = query
   2043 	}
   2044 	return ss, nil
   2045 }
   2046 
   2047 // Run-Time Limit Categories.
   2048 // See: http://www.sqlite.org/c3ref/c_limit_attached.html
   2049 const (
   2050 	SQLITE_LIMIT_LENGTH              = C.SQLITE_LIMIT_LENGTH
   2051 	SQLITE_LIMIT_SQL_LENGTH          = C.SQLITE_LIMIT_SQL_LENGTH
   2052 	SQLITE_LIMIT_COLUMN              = C.SQLITE_LIMIT_COLUMN
   2053 	SQLITE_LIMIT_EXPR_DEPTH          = C.SQLITE_LIMIT_EXPR_DEPTH
   2054 	SQLITE_LIMIT_COMPOUND_SELECT     = C.SQLITE_LIMIT_COMPOUND_SELECT
   2055 	SQLITE_LIMIT_VDBE_OP             = C.SQLITE_LIMIT_VDBE_OP
   2056 	SQLITE_LIMIT_FUNCTION_ARG        = C.SQLITE_LIMIT_FUNCTION_ARG
   2057 	SQLITE_LIMIT_ATTACHED            = C.SQLITE_LIMIT_ATTACHED
   2058 	SQLITE_LIMIT_LIKE_PATTERN_LENGTH = C.SQLITE_LIMIT_LIKE_PATTERN_LENGTH
   2059 	SQLITE_LIMIT_VARIABLE_NUMBER     = C.SQLITE_LIMIT_VARIABLE_NUMBER
   2060 	SQLITE_LIMIT_TRIGGER_DEPTH       = C.SQLITE_LIMIT_TRIGGER_DEPTH
   2061 	SQLITE_LIMIT_WORKER_THREADS      = C.SQLITE_LIMIT_WORKER_THREADS
   2062 )
   2063 
   2064 // GetFilename returns the absolute path to the file containing
   2065 // the requested schema. When passed an empty string, it will
   2066 // instead use the database's default schema: "main".
   2067 // See: sqlite3_db_filename, https://www.sqlite.org/c3ref/db_filename.html
   2068 func (c *SQLiteConn) GetFilename(schemaName string) string {
   2069 	if schemaName == "" {
   2070 		schemaName = "main"
   2071 	}
   2072 	return C.GoString(C.sqlite3_db_filename(c.db, C.CString(schemaName)))
   2073 }
   2074 
   2075 // GetLimit returns the current value of a run-time limit.
   2076 // See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
   2077 func (c *SQLiteConn) GetLimit(id int) int {
   2078 	return int(C._sqlite3_limit(c.db, C.int(id), C.int(-1)))
   2079 }
   2080 
   2081 // SetLimit changes the value of a run-time limits.
   2082 // Then this method returns the prior value of the limit.
   2083 // See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
   2084 func (c *SQLiteConn) SetLimit(id int, newVal int) int {
   2085 	return int(C._sqlite3_limit(c.db, C.int(id), C.int(newVal)))
   2086 }
   2087 
   2088 // SetFileControlInt invokes the xFileControl method on a given database. The
   2089 // dbName is the name of the database. It will default to "main" if left blank.
   2090 // The op is one of the opcodes prefixed by "SQLITE_FCNTL_". The arg argument
   2091 // and return code are both opcode-specific. Please see the SQLite documentation.
   2092 //
   2093 // This method is not thread-safe as the returned error code can be changed by
   2094 // another call if invoked concurrently.
   2095 //
   2096 // Use SetFileControlInt64 instead if the argument for the opcode is documented
   2097 // as a pointer to a sqlite3_int64.
   2098 //
   2099 // See: sqlite3_file_control, https://www.sqlite.org/c3ref/file_control.html
   2100 func (c *SQLiteConn) SetFileControlInt(dbName string, op int, arg int) error {
   2101 	if dbName == "" {
   2102 		dbName = "main"
   2103 	}
   2104 
   2105 	cDBName := C.CString(dbName)
   2106 	defer C.free(unsafe.Pointer(cDBName))
   2107 
   2108 	cArg := C.int(arg)
   2109 	rv := C.sqlite3_file_control(c.db, cDBName, C.int(op), unsafe.Pointer(&cArg))
   2110 	if rv != C.SQLITE_OK {
   2111 		return c.lastError()
   2112 	}
   2113 	return nil
   2114 }
   2115 
   2116 // SetFileControlInt64 invokes the xFileControl method on a given database. The
   2117 // dbName is the name of the database. It will default to "main" if left blank.
   2118 // The op is one of the opcodes prefixed by "SQLITE_FCNTL_". The arg argument
   2119 // and return code are both opcode-specific. Please see the SQLite documentation.
   2120 //
   2121 // This method is not thread-safe as the returned error code can be changed by
   2122 // another call if invoked concurrently.
   2123 //
   2124 // Only use this method if the argument for the opcode is documented as a pointer
   2125 // to a sqlite3_int64.
   2126 //
   2127 // See: sqlite3_file_control, https://www.sqlite.org/c3ref/file_control.html
   2128 func (c *SQLiteConn) SetFileControlInt64(dbName string, op int, arg int64) error {
   2129 	if dbName == "" {
   2130 		dbName = "main"
   2131 	}
   2132 
   2133 	cDBName := C.CString(dbName)
   2134 	defer C.free(unsafe.Pointer(cDBName))
   2135 
   2136 	cArg := C.sqlite3_int64(arg)
   2137 	rv := C.sqlite3_file_control(c.db, cDBName, C.int(op), unsafe.Pointer(&cArg))
   2138 	if rv != C.SQLITE_OK {
   2139 		return c.lastError()
   2140 	}
   2141 	return nil
   2142 }
   2143 
   2144 // Close the statement.
   2145 func (s *SQLiteStmt) Close() error {
   2146 	s.mu.Lock()
   2147 	defer s.mu.Unlock()
   2148 	if s.closed {
   2149 		return nil
   2150 	}
   2151 	s.closed = true
   2152 	runtime.SetFinalizer(s, nil)
   2153 	conn := s.c
   2154 	stmt := s.s
   2155 	if stmt == nil {
   2156 		s.c = nil
   2157 		return nil
   2158 	}
   2159 	if !conn.dbConnOpen() {
   2160 		return errors.New("sqlite statement with already closed database connection")
   2161 	}
   2162 	if s.cacheKey != "" && conn.putCachedStmt(s) {
   2163 		return nil
   2164 	}
   2165 	s.s = nil
   2166 	s.c = nil
   2167 	rv := C.sqlite3_finalize(stmt)
   2168 	if rv != C.SQLITE_OK {
   2169 		return conn.lastError()
   2170 	}
   2171 	return nil
   2172 }
   2173 
   2174 // NumInput return a number of parameters.
   2175 func (s *SQLiteStmt) NumInput() int {
   2176 	return int(C.sqlite3_bind_parameter_count(s.s))
   2177 }
   2178 
   2179 var placeHolder = []byte{0}
   2180 
   2181 func bindText(s *C.sqlite3_stmt, n C.int, v string) C.int {
   2182 	if len(v) == 0 {
   2183 		return C._sqlite3_bind_text(s, n, (*C.char)(unsafe.Pointer(&placeHolder[0])), C.int(0))
   2184 	}
   2185 	return C._sqlite3_bind_text(s, n, (*C.char)(unsafe.Pointer(unsafe.StringData(v))), C.int(len(v)))
   2186 }
   2187 
   2188 func bindValue(s *C.sqlite3_stmt, n C.int, value driver.Value) C.int {
   2189 	switch v := value.(type) {
   2190 	case nil:
   2191 		return C.sqlite3_bind_null(s, n)
   2192 	case string:
   2193 		return bindText(s, n, v)
   2194 	case int64:
   2195 		return C.sqlite3_bind_int64(s, n, C.sqlite3_int64(v))
   2196 	case bool:
   2197 		if v {
   2198 			return C.sqlite3_bind_int(s, n, 1)
   2199 		}
   2200 		return C.sqlite3_bind_int(s, n, 0)
   2201 	case float64:
   2202 		return C.sqlite3_bind_double(s, n, C.double(v))
   2203 	case []byte:
   2204 		if v == nil {
   2205 			return C.sqlite3_bind_null(s, n)
   2206 		}
   2207 		ln := len(v)
   2208 		if ln == 0 {
   2209 			v = placeHolder
   2210 		}
   2211 		return C._sqlite3_bind_blob(s, n, unsafe.Pointer(&v[0]), C.int(ln))
   2212 	case time.Time:
   2213 		var buf [64]byte
   2214 		b := v.AppendFormat(buf[:0], SQLiteTimestampFormats[0])
   2215 		if len(b) == 0 {
   2216 			return C._sqlite3_bind_text(s, n, (*C.char)(unsafe.Pointer(&placeHolder[0])), C.int(0))
   2217 		}
   2218 		return C._sqlite3_bind_text(s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
   2219 	default:
   2220 		return C.SQLITE_MISUSE
   2221 	}
   2222 }
   2223 
   2224 func (s *SQLiteStmt) bindNamedIndices(name string) [3]int {
   2225 	if s.namedParams == nil {
   2226 		s.namedParams = make(map[string][3]int)
   2227 	} else if indices, ok := s.namedParams[name]; ok {
   2228 		return indices
   2229 	}
   2230 
   2231 	// Build ":name\0" once and rewrite prefix byte to avoid 3 C.CString allocs.
   2232 	buf := make([]byte, 1+len(name)+1) // prefix + name + null terminator
   2233 	copy(buf[1:], name)
   2234 	buf[len(buf)-1] = 0
   2235 	cname := (*C.char)(unsafe.Pointer(&buf[0]))
   2236 
   2237 	var indices [3]int
   2238 	prefixes := [3]byte{':', '@', '$'}
   2239 	for i, p := range prefixes {
   2240 		buf[0] = p
   2241 		indices[i] = int(C.sqlite3_bind_parameter_index(s.s, cname))
   2242 	}
   2243 	s.namedParams[name] = indices
   2244 	return indices
   2245 }
   2246 
   2247 func stmtArgs(args []driver.NamedValue, start, na int) []driver.NamedValue {
   2248 	if na == 0 {
   2249 		return nil
   2250 	}
   2251 
   2252 	end := start + na
   2253 	hasNamedOutside := false
   2254 	for i := range args {
   2255 		if args[i].Name != "" && (i < start || i >= end) {
   2256 			hasNamedOutside = true
   2257 			break
   2258 		}
   2259 	}
   2260 	if start == 0 && !hasNamedOutside {
   2261 		return args[start:end]
   2262 	}
   2263 
   2264 	stmtArgs := make([]driver.NamedValue, 0, len(args))
   2265 	stmtArgs = append(stmtArgs, args[start:end]...)
   2266 	for i := range args {
   2267 		if args[i].Name != "" && (i < start || i >= end) {
   2268 			stmtArgs = append(stmtArgs, args[i])
   2269 		}
   2270 	}
   2271 	for i := range stmtArgs {
   2272 		stmtArgs[i].Ordinal = i + 1
   2273 	}
   2274 	return stmtArgs
   2275 }
   2276 
   2277 func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
   2278 	rv := C._sqlite3_reset_clear(s.s)
   2279 	if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
   2280 		return s.c.lastError()
   2281 	}
   2282 
   2283 	hasNamed := false
   2284 	for i := range args {
   2285 		if args[i].Name != "" {
   2286 			hasNamed = true
   2287 			break
   2288 		}
   2289 	}
   2290 
   2291 	if !hasNamed {
   2292 		for _, arg := range args {
   2293 			n := C.int(arg.Ordinal)
   2294 			rv = bindValue(s.s, n, arg.Value)
   2295 			if rv != C.SQLITE_OK {
   2296 				return s.c.lastError()
   2297 			}
   2298 		}
   2299 		return nil
   2300 	}
   2301 
   2302 	for _, arg := range args {
   2303 		if arg.Name == "" {
   2304 			rv = bindValue(s.s, C.int(arg.Ordinal), arg.Value)
   2305 			if rv != C.SQLITE_OK {
   2306 				return s.c.lastError()
   2307 			}
   2308 			continue
   2309 		}
   2310 		indices := s.bindNamedIndices(arg.Name)
   2311 		for _, idx := range indices {
   2312 			if idx == 0 {
   2313 				continue
   2314 			}
   2315 			rv = bindValue(s.s, C.int(idx), arg.Value)
   2316 			if rv != C.SQLITE_OK {
   2317 				return s.c.lastError()
   2318 			}
   2319 		}
   2320 	}
   2321 	return nil
   2322 }
   2323 
   2324 // Query the statement with arguments. Return records.
   2325 func (s *SQLiteStmt) Query(args []driver.Value) (driver.Rows, error) {
   2326 	return s.query(context.Background(), valueToNamedValue(args))
   2327 }
   2328 
   2329 func (s *SQLiteStmt) query(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
   2330 	if err := s.bind(args); err != nil {
   2331 		return nil, err
   2332 	}
   2333 
   2334 	rows := &SQLiteRows{
   2335 		s:        s,
   2336 		nc:       int32(C.sqlite3_column_count(s.s)),
   2337 		cls:      s.cls,
   2338 		cols:     nil,
   2339 		decltype: nil,
   2340 		colvals:  nil,
   2341 		ctx:      ctx,
   2342 	}
   2343 	if rows.nc > 0 {
   2344 		rows.colvals = (*C.sqlite3_go_col)(C.malloc(C.size_t(rows.nc) * C.size_t(unsafe.Sizeof(C.sqlite3_go_col{}))))
   2345 		if rows.colvals == nil {
   2346 			return nil, errors.New("sqlite3: failed to allocate row buffer")
   2347 		}
   2348 	}
   2349 
   2350 	return rows, nil
   2351 }
   2352 
   2353 // LastInsertId return last inserted ID.
   2354 func (r *SQLiteResult) LastInsertId() (int64, error) {
   2355 	return r.id, nil
   2356 }
   2357 
   2358 // RowsAffected return how many rows affected.
   2359 func (r *SQLiteResult) RowsAffected() (int64, error) {
   2360 	return r.changes, nil
   2361 }
   2362 
   2363 // Exec execute the statement with arguments. Return result object.
   2364 func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
   2365 	return s.exec(context.Background(), valueToNamedValue(args))
   2366 }
   2367 
   2368 func valueToNamedValue(args []driver.Value) []driver.NamedValue {
   2369 	list := make([]driver.NamedValue, len(args))
   2370 	for i, v := range args {
   2371 		list[i] = driver.NamedValue{
   2372 			Ordinal: i + 1,
   2373 			Value:   v,
   2374 		}
   2375 	}
   2376 	return list
   2377 }
   2378 
   2379 func isInterruptErr(err error) bool {
   2380 	sqliteErr, ok := err.(Error)
   2381 	if ok {
   2382 		return sqliteErr.Code == ErrInterrupt
   2383 	}
   2384 	return false
   2385 }
   2386 
   2387 // exec executes a query that doesn't return rows. Attempts to honor context timeout.
   2388 func (s *SQLiteStmt) exec(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
   2389 	if ctx.Done() == nil {
   2390 		return s.execSync(args)
   2391 	}
   2392 
   2393 	sema := make(chan struct{})
   2394 	var r driver.Result
   2395 	var err error
   2396 	go func() {
   2397 		r, err = s.execSync(args)
   2398 		close(sema)
   2399 	}()
   2400 	select {
   2401 	case <-sema:
   2402 		return r, err
   2403 	case <-ctx.Done():
   2404 		select {
   2405 		case <-sema: // no need to interrupt, operation completed in db
   2406 			return r, err
   2407 		default:
   2408 			// this is still racy and can be no-op if executed between sqlite3_* calls in execSync.
   2409 			C.sqlite3_interrupt(s.c.db)
   2410 			<-sema // wait for goroutine completed
   2411 			if isInterruptErr(err) {
   2412 				return nil, ctx.Err()
   2413 			}
   2414 			return r, err
   2415 		}
   2416 	}
   2417 }
   2418 
   2419 func (s *SQLiteStmt) execSync(args []driver.NamedValue) (driver.Result, error) {
   2420 	if err := s.bind(args); err != nil {
   2421 		C._sqlite3_reset_clear(s.s)
   2422 		return nil, err
   2423 	}
   2424 
   2425 	var rowid, changes C.longlong
   2426 	rv := C._sqlite3_step_row_internal(s.s, &rowid, &changes)
   2427 	if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
   2428 		err := s.c.lastError()
   2429 		C._sqlite3_reset_clear(s.s)
   2430 		return nil, err
   2431 	}
   2432 
   2433 	return &SQLiteResult{id: int64(rowid), changes: int64(changes)}, nil
   2434 }
   2435 
   2436 // Readonly reports if this statement is considered readonly by SQLite.
   2437 //
   2438 // See: https://sqlite.org/c3ref/stmt_readonly.html
   2439 func (s *SQLiteStmt) Readonly() bool {
   2440 	return C.sqlite3_stmt_readonly(s.s) == 1
   2441 }
   2442 
   2443 // Close the rows.
   2444 func (rc *SQLiteRows) Close() error {
   2445 	rc.closemu.Lock()
   2446 	defer rc.closemu.Unlock()
   2447 	s := rc.s
   2448 	if s == nil {
   2449 		if rc.colvals != nil {
   2450 			C.free(unsafe.Pointer(rc.colvals))
   2451 			rc.colvals = nil
   2452 		}
   2453 		return nil
   2454 	}
   2455 	rc.s = nil // remove reference to SQLiteStmt
   2456 	if rc.colvals != nil {
   2457 		C.free(unsafe.Pointer(rc.colvals))
   2458 		rc.colvals = nil
   2459 	}
   2460 	s.mu.Lock()
   2461 	if s.closed {
   2462 		s.mu.Unlock()
   2463 		return nil
   2464 	}
   2465 	if rc.cls {
   2466 		s.mu.Unlock()
   2467 		return s.Close()
   2468 	}
   2469 	rv := C.sqlite3_reset(s.s)
   2470 	if rv != C.SQLITE_OK {
   2471 		s.mu.Unlock()
   2472 		return s.c.lastError()
   2473 	}
   2474 	s.mu.Unlock()
   2475 	return nil
   2476 }
   2477 
   2478 // Columns return column names.
   2479 func (rc *SQLiteRows) Columns() []string {
   2480 	if rc.s == nil {
   2481 		return rc.cols
   2482 	}
   2483 	rc.s.mu.Lock()
   2484 	defer rc.s.mu.Unlock()
   2485 	if rc.s.s != nil && int(rc.nc) != len(rc.cols) {
   2486 		rc.cols = make([]string, rc.nc)
   2487 		for i := range rc.cols {
   2488 			rc.cols[i] = C.GoString(C.sqlite3_column_name(rc.s.s, C.int(i)))
   2489 		}
   2490 	}
   2491 	return rc.cols
   2492 }
   2493 
   2494 func (rc *SQLiteRows) declTypes() []string {
   2495 	if rc.s.s != nil && rc.decltype == nil {
   2496 		rc.decltype = make([]string, rc.nc)
   2497 		for i := range rc.decltype {
   2498 			rc.decltype[i] = strings.ToLower(C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))))
   2499 		}
   2500 	}
   2501 	return rc.decltype
   2502 }
   2503 
   2504 // DeclTypes return column types.
   2505 func (rc *SQLiteRows) DeclTypes() []string {
   2506 	if rc.s == nil {
   2507 		return rc.decltype
   2508 	}
   2509 	rc.s.mu.Lock()
   2510 	defer rc.s.mu.Unlock()
   2511 	return rc.declTypes()
   2512 }
   2513 
   2514 // Next move cursor to next. Attempts to honor context timeout from QueryContext call.
   2515 func (rc *SQLiteRows) Next(dest []driver.Value) error {
   2516 	if rc.s == nil {
   2517 		return io.EOF
   2518 	}
   2519 	rc.s.mu.Lock()
   2520 	defer rc.s.mu.Unlock()
   2521 
   2522 	if rc.s.closed {
   2523 		return io.EOF
   2524 	}
   2525 
   2526 	if rc.ctx.Done() == nil {
   2527 		return rc.nextSyncLocked(dest)
   2528 	}
   2529 	sema := make(chan struct{})
   2530 	var err error
   2531 	go func() {
   2532 		err = rc.nextSyncLocked(dest)
   2533 		close(sema)
   2534 	}()
   2535 	select {
   2536 	case <-sema:
   2537 		return err
   2538 	case <-rc.ctx.Done():
   2539 		select {
   2540 		case <-sema: // no need to interrupt
   2541 		default:
   2542 			// this is still racy and can be no-op if executed between sqlite3_* calls in nextSyncLocked.
   2543 			C.sqlite3_interrupt(rc.s.c.db)
   2544 			<-sema // ensure goroutine completed
   2545 		}
   2546 		return rc.ctx.Err()
   2547 	}
   2548 }
   2549 
   2550 // nextSyncLocked moves cursor to next; must be called with locked mutex.
   2551 func (rc *SQLiteRows) nextSyncLocked(dest []driver.Value) error {
   2552 	rv := C._sqlite3_step_internal(rc.s.s)
   2553 	if rv == C.SQLITE_DONE {
   2554 		return io.EOF
   2555 	}
   2556 	if rv != C.SQLITE_ROW {
   2557 		rv = C.sqlite3_reset(rc.s.s)
   2558 		if rv != C.SQLITE_OK {
   2559 			return rc.s.c.lastError()
   2560 		}
   2561 		return nil
   2562 	}
   2563 
   2564 	rc.declTypes()
   2565 	if len(dest) == 0 {
   2566 		return nil
   2567 	}
   2568 	C._sqlite3_column_values(rc.s.s, C.int(len(dest)), rc.colvals)
   2569 	colvals := (*[(math.MaxInt32 - 1) / unsafe.Sizeof(C.sqlite3_go_col{})]C.sqlite3_go_col)(unsafe.Pointer(rc.colvals))[:len(dest):len(dest)]
   2570 
   2571 	decltype := rc.decltype
   2572 	_ = decltype[len(dest)-1]
   2573 	for i := range dest {
   2574 		col := &colvals[i]
   2575 		switch col.typ {
   2576 		case C.SQLITE_INTEGER:
   2577 			val := int64(col.i64)
   2578 			switch decltype[i] {
   2579 			case columnTimestamp, columnDatetime, columnDate:
   2580 				var t time.Time
   2581 				// Assume a millisecond unix timestamp if it's 13 digits -- too
   2582 				// large to be a reasonable timestamp in seconds.
   2583 				if val > 1e12 || val < -1e12 {
   2584 					val *= int64(time.Millisecond) // convert ms to nsec
   2585 					t = time.Unix(0, val)
   2586 				} else {
   2587 					t = time.Unix(val, 0)
   2588 				}
   2589 				t = t.UTC()
   2590 				if rc.s.c.loc != nil {
   2591 					t = t.In(rc.s.c.loc)
   2592 				}
   2593 				dest[i] = t
   2594 			case "boolean":
   2595 				dest[i] = val > 0
   2596 			default:
   2597 				dest[i] = val
   2598 			}
   2599 		case C.SQLITE_FLOAT:
   2600 			dest[i] = float64(col.f64)
   2601 		case C.SQLITE_BLOB:
   2602 			p := col.ptr
   2603 			if p == nil {
   2604 				dest[i] = []byte{}
   2605 				continue
   2606 			}
   2607 			n := col.n
   2608 			dest[i] = C.GoBytes(p, n)
   2609 		case C.SQLITE_NULL:
   2610 			dest[i] = nil
   2611 		case C.SQLITE_TEXT:
   2612 			var err error
   2613 			var timeVal time.Time
   2614 
   2615 			n := int(col.n)
   2616 			s := C.GoStringN((*C.char)(unsafe.Pointer(col.ptr)), C.int(n))
   2617 
   2618 			switch decltype[i] {
   2619 			case columnTimestamp, columnDatetime, columnDate:
   2620 				var t time.Time
   2621 				s = strings.TrimSuffix(s, "Z")
   2622 				for _, format := range SQLiteTimestampFormats {
   2623 					if timeVal, err = time.ParseInLocation(format, s, time.UTC); err == nil {
   2624 						t = timeVal
   2625 						break
   2626 					}
   2627 				}
   2628 				if err != nil {
   2629 					// The column is a time value, so return the zero time on parse failure.
   2630 					t = time.Time{}
   2631 				}
   2632 				if rc.s.c.loc != nil {
   2633 					t = t.In(rc.s.c.loc)
   2634 				}
   2635 				dest[i] = t
   2636 			default:
   2637 				dest[i] = s
   2638 			}
   2639 		}
   2640 	}
   2641 	return nil
   2642 }