aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/mattn/go-sqlite3/sqlite3.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/mattn/go-sqlite3/sqlite3.go')
-rw-r--r--vendor/github.com/mattn/go-sqlite3/sqlite3.go973
1 files changed, 896 insertions, 77 deletions
diff --git a/vendor/github.com/mattn/go-sqlite3/sqlite3.go b/vendor/github.com/mattn/go-sqlite3/sqlite3.go
index 1ff58c3..d8fe4b0 100644
--- a/vendor/github.com/mattn/go-sqlite3/sqlite3.go
+++ b/vendor/github.com/mattn/go-sqlite3/sqlite3.go
@@ -1,17 +1,28 @@
// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
+// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
+// +build cgo
+
package sqlite3
/*
#cgo CFLAGS: -std=gnu99
-#cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE=1
-#cgo CFLAGS: -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61
+#cgo CFLAGS: -DSQLITE_ENABLE_RTREE
+#cgo CFLAGS: -DSQLITE_THREADSAFE=1
+#cgo CFLAGS: -DHAVE_USLEEP=1
+#cgo CFLAGS: -DSQLITE_ENABLE_FTS3
+#cgo CFLAGS: -DSQLITE_ENABLE_FTS3_PARENTHESIS
+#cgo CFLAGS: -DSQLITE_ENABLE_FTS4_UNICODE61
#cgo CFLAGS: -DSQLITE_TRACE_SIZE_LIMIT=15
+#cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED
#cgo CFLAGS: -DSQLITE_DISABLE_INTRINSIC
+#cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1
+#cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT
#cgo CFLAGS: -Wno-deprecated-declarations
+#cgo linux,!android CFLAGS: -DHAVE_PREAD64=1 -DHAVE_PWRITE64=1
#ifndef USE_LIBSQLITE3
#include <sqlite3-binding.h>
#else
@@ -100,14 +111,43 @@ int _sqlite3_create_function(
}
void callbackTrampoline(sqlite3_context*, int, sqlite3_value**);
+void stepTrampoline(sqlite3_context*, int, sqlite3_value**);
+void doneTrampoline(sqlite3_context*);
int compareTrampoline(void*, int, char*, int, char*);
int commitHookTrampoline(void*);
void rollbackHookTrampoline(void*);
void updateHookTrampoline(void*, int, char*, char*, sqlite3_int64);
+
+#ifdef SQLITE_LIMIT_WORKER_THREADS
+# define _SQLITE_HAS_LIMIT
+# define SQLITE_LIMIT_LENGTH 0
+# define SQLITE_LIMIT_SQL_LENGTH 1
+# define SQLITE_LIMIT_COLUMN 2
+# define SQLITE_LIMIT_EXPR_DEPTH 3
+# define SQLITE_LIMIT_COMPOUND_SELECT 4
+# define SQLITE_LIMIT_VDBE_OP 5
+# define SQLITE_LIMIT_FUNCTION_ARG 6
+# define SQLITE_LIMIT_ATTACHED 7
+# define SQLITE_LIMIT_LIKE_PATTERN_LENGTH 8
+# define SQLITE_LIMIT_VARIABLE_NUMBER 9
+# define SQLITE_LIMIT_TRIGGER_DEPTH 10
+# define SQLITE_LIMIT_WORKER_THREADS 11
+# else
+# define SQLITE_LIMIT_WORKER_THREADS 11
+#endif
+
+static int _sqlite3_limit(sqlite3* db, int limitId, int newLimit) {
+#ifndef _SQLITE_HAS_LIMIT
+ return -1;
+#else
+ return sqlite3_limit(db, limitId, newLimit);
+#endif
+}
*/
import "C"
import (
+ "context"
"database/sql"
"database/sql/driver"
"errors"
@@ -121,8 +161,6 @@ import (
"sync"
"time"
"unsafe"
-
- "golang.org/x/net/context"
)
// SQLiteTimestampFormats is timestamp formats understood by both this module
@@ -143,6 +181,12 @@ var SQLiteTimestampFormats = []string{
"2006-01-02",
}
+const (
+ columnDate string = "date"
+ columnDatetime string = "datetime"
+ columnTimestamp string = "timestamp"
+)
+
func init() {
sql.Register("sqlite3", &SQLiteDriver{})
}
@@ -362,7 +406,7 @@ func (c *SQLiteConn) RegisterCommitHook(callback func() int) {
if callback == nil {
C.sqlite3_commit_hook(c.db, nil, nil)
} else {
- C.sqlite3_commit_hook(c.db, (*[0]byte)(unsafe.Pointer(C.commitHookTrampoline)), unsafe.Pointer(newHandle(c, callback)))
+ C.sqlite3_commit_hook(c.db, (*[0]byte)(C.commitHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
}
}
@@ -375,7 +419,7 @@ func (c *SQLiteConn) RegisterRollbackHook(callback func()) {
if callback == nil {
C.sqlite3_rollback_hook(c.db, nil, nil)
} else {
- C.sqlite3_rollback_hook(c.db, (*[0]byte)(unsafe.Pointer(C.rollbackHookTrampoline)), unsafe.Pointer(newHandle(c, callback)))
+ C.sqlite3_rollback_hook(c.db, (*[0]byte)(C.rollbackHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
}
}
@@ -392,7 +436,7 @@ func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64
if callback == nil {
C.sqlite3_update_hook(c.db, nil, nil)
} else {
- C.sqlite3_update_hook(c.db, (*[0]byte)(unsafe.Pointer(C.updateHookTrampoline)), unsafe.Pointer(newHandle(c, callback)))
+ C.sqlite3_update_hook(c.db, (*[0]byte)(C.updateHookTrampoline), unsafe.Pointer(newHandle(c, callback)))
}
}
@@ -474,7 +518,132 @@ func (c *SQLiteConn) RegisterFunc(name string, impl interface{}, pure bool) erro
}
func sqlite3CreateFunction(db *C.sqlite3, zFunctionName *C.char, nArg C.int, eTextRep C.int, pApp uintptr, xFunc unsafe.Pointer, xStep unsafe.Pointer, xFinal unsafe.Pointer) C.int {
- return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(pApp), (*[0]byte)(unsafe.Pointer(xFunc)), (*[0]byte)(unsafe.Pointer(xStep)), (*[0]byte)(unsafe.Pointer(xFinal)))
+ return C._sqlite3_create_function(db, zFunctionName, nArg, eTextRep, C.uintptr_t(pApp), (*[0]byte)(xFunc), (*[0]byte)(xStep), (*[0]byte)(xFinal))
+}
+
+// RegisterAggregator makes a Go type available as a SQLite aggregation function.
+//
+// Because aggregation is incremental, it's implemented in Go with a
+// type that has 2 methods: func Step(values) accumulates one row of
+// data into the accumulator, and func Done() ret finalizes and
+// returns the aggregate value. "values" and "ret" may be any type
+// supported by RegisterFunc.
+//
+// RegisterAggregator takes as implementation a constructor function
+// that constructs an instance of the aggregator type each time an
+// aggregation begins. The constructor must return a pointer to a
+// type, or an interface that implements Step() and Done().
+//
+// The constructor function and the Step/Done methods may optionally
+// return an error in addition to their other return values.
+//
+// See _example/go_custom_funcs for a detailed example.
+func (c *SQLiteConn) RegisterAggregator(name string, impl interface{}, pure bool) error {
+ var ai aggInfo
+ ai.constructor = reflect.ValueOf(impl)
+ t := ai.constructor.Type()
+ if t.Kind() != reflect.Func {
+ return errors.New("non-function passed to RegisterAggregator")
+ }
+ if t.NumOut() != 1 && t.NumOut() != 2 {
+ return errors.New("SQLite aggregator constructors must return 1 or 2 values")
+ }
+ if t.NumOut() == 2 && !t.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
+ return errors.New("Second return value of SQLite function must be error")
+ }
+ if t.NumIn() != 0 {
+ return errors.New("SQLite aggregator constructors must not have arguments")
+ }
+
+ agg := t.Out(0)
+ switch agg.Kind() {
+ case reflect.Ptr, reflect.Interface:
+ default:
+ return errors.New("SQlite aggregator constructor must return a pointer object")
+ }
+ stepFn, found := agg.MethodByName("Step")
+ if !found {
+ return errors.New("SQlite aggregator doesn't have a Step() function")
+ }
+ step := stepFn.Type
+ if step.NumOut() != 0 && step.NumOut() != 1 {
+ return errors.New("SQlite aggregator Step() function must return 0 or 1 values")
+ }
+ if step.NumOut() == 1 && !step.Out(0).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
+ return errors.New("type of SQlite aggregator Step() return value must be error")
+ }
+
+ stepNArgs := step.NumIn()
+ start := 0
+ if agg.Kind() == reflect.Ptr {
+ // Skip over the method receiver
+ stepNArgs--
+ start++
+ }
+ if step.IsVariadic() {
+ stepNArgs--
+ }
+ for i := start; i < start+stepNArgs; i++ {
+ conv, err := callbackArg(step.In(i))
+ if err != nil {
+ return err
+ }
+ ai.stepArgConverters = append(ai.stepArgConverters, conv)
+ }
+ if step.IsVariadic() {
+ conv, err := callbackArg(t.In(start + stepNArgs).Elem())
+ if err != nil {
+ return err
+ }
+ ai.stepVariadicConverter = conv
+ // Pass -1 to sqlite so that it allows any number of
+ // arguments. The call helper verifies that the minimum number
+ // of arguments is present for variadic functions.
+ stepNArgs = -1
+ }
+
+ doneFn, found := agg.MethodByName("Done")
+ if !found {
+ return errors.New("SQlite aggregator doesn't have a Done() function")
+ }
+ done := doneFn.Type
+ doneNArgs := done.NumIn()
+ if agg.Kind() == reflect.Ptr {
+ // Skip over the method receiver
+ doneNArgs--
+ }
+ if doneNArgs != 0 {
+ return errors.New("SQlite aggregator Done() function must have no arguments")
+ }
+ if done.NumOut() != 1 && done.NumOut() != 2 {
+ return errors.New("SQLite aggregator Done() function must return 1 or 2 values")
+ }
+ if done.NumOut() == 2 && !done.Out(1).Implements(reflect.TypeOf((*error)(nil)).Elem()) {
+ return errors.New("second return value of SQLite aggregator Done() function must be error")
+ }
+
+ conv, err := callbackRet(done.Out(0))
+ if err != nil {
+ return err
+ }
+ ai.doneRetConverter = conv
+ ai.active = make(map[int64]reflect.Value)
+ ai.next = 1
+
+ // ai must outlast the database connection, or we'll have dangling pointers.
+ c.aggregators = append(c.aggregators, &ai)
+
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+ opts := C.SQLITE_UTF8
+ if pure {
+ opts |= C.SQLITE_DETERMINISTIC
+ }
+ rv := sqlite3CreateFunction(c.db, cname, C.int(stepNArgs), C.int(opts), newHandle(c, &ai), nil, C.stepTrampoline, C.doneTrampoline)
+ if rv != C.SQLITE_OK {
+ return c.lastError()
+ }
+ return nil
}
// AutoCommit return which currently auto commit or not.
@@ -611,33 +780,140 @@ func errorString(err Error) string {
}
// Open database and return a new connection.
+//
+// A pragma can take either zero or one argument.
+// The argument is may be either in parentheses or it may be separated from
+// the pragma name by an equal sign. The two syntaxes yield identical results.
+// In many pragmas, the argument is a boolean. The boolean can be one of:
+// 1 yes true on
+// 0 no false off
+//
// You can specify a DSN string using a URI as the filename.
// test.db
// file:test.db?cache=shared&mode=memory
// :memory:
// file::memory:
+//
+// mode
+// Access mode of the database.
+// https://www.sqlite.org/c3ref/open.html
+// Values:
+// - ro
+// - rw
+// - rwc
+// - memory
+//
+// shared
+// SQLite Shared-Cache Mode
+// https://www.sqlite.org/sharedcache.html
+// Values:
+// - shared
+// - private
+//
+// immutable=Boolean
+// The immutable parameter is a boolean query parameter that indicates
+// that the database file is stored on read-only media. When immutable is set,
+// SQLite assumes that the database file cannot be changed,
+// even by a process with higher privilege,
+// and so the database is opened read-only and all locking and change detection is disabled.
+// Caution: Setting the immutable property on a database file that
+// does in fact change can result in incorrect query results and/or SQLITE_CORRUPT errors.
+//
// go-sqlite3 adds the following query parameters to those used by SQLite:
// _loc=XXX
// Specify location of time format. It's possible to specify "auto".
-// _busy_timeout=XXX
-// Specify value for sqlite3_busy_timeout.
+//
+// _mutex=XXX
+// Specify mutex mode. XXX can be "no", "full".
+//
// _txlock=XXX
// Specify locking behavior for transactions. XXX can be "immediate",
// "deferred", "exclusive".
-// _foreign_keys=X
-// Enable or disable enforcement of foreign keys. X can be 1 or 0.
-// _recursive_triggers=X
-// Enable or disable recursive triggers. X can be 1 or 0.
+//
+// _auto_vacuum=X | _vacuum=X
+// 0 | none - Auto Vacuum disabled
+// 1 | full - Auto Vacuum FULL
+// 2 | incremental - Auto Vacuum Incremental
+//
+// _busy_timeout=XXX"| _timeout=XXX
+// Specify value for sqlite3_busy_timeout.
+//
+// _case_sensitive_like=Boolean | _cslike=Boolean
+// https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
+// Default or disabled the LIKE operation is case-insensitive.
+// When enabling this options behaviour of LIKE will become case-sensitive.
+//
+// _defer_foreign_keys=Boolean | _defer_fk=Boolean
+// Defer Foreign Keys until outermost transaction is committed.
+//
+// _foreign_keys=Boolean | _fk=Boolean
+// Enable or disable enforcement of foreign keys.
+//
+// _ignore_check_constraints=Boolean
+// This pragma enables or disables the enforcement of CHECK constraints.
+// The default setting is off, meaning that CHECK constraints are enforced by default.
+//
+// _journal_mode=MODE | _journal=MODE
+// Set journal mode for the databases associated with the current connection.
+// https://www.sqlite.org/pragma.html#pragma_journal_mode
+//
+// _locking_mode=X | _locking=X
+// Sets the database connection locking-mode.
+// The locking-mode is either NORMAL or EXCLUSIVE.
+// https://www.sqlite.org/pragma.html#pragma_locking_mode
+//
+// _query_only=Boolean
+// The query_only pragma prevents all changes to database files when enabled.
+//
+// _recursive_triggers=Boolean | _rt=Boolean
+// Enable or disable recursive triggers.
+//
+// _secure_delete=Boolean|FAST
+// When secure_delete is on, SQLite overwrites deleted content with zeros.
+// https://www.sqlite.org/pragma.html#pragma_secure_delete
+//
+// _synchronous=X | _sync=X
+// Change the setting of the "synchronous" flag.
+// https://www.sqlite.org/pragma.html#pragma_synchronous
+//
+// _writable_schema=Boolean
+// When this pragma is on, the SQLITE_MASTER tables in which database
+// can be changed using ordinary UPDATE, INSERT, and DELETE statements.
+// Warning: misuse of this pragma can easily result in a corrupt database file.
+//
+//
func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
if C.sqlite3_threadsafe() == 0 {
return nil, errors.New("sqlite library was not compiled for thread-safe operation")
}
+ var pkey string
+
+ // Options
var loc *time.Location
+ authCreate := false
+ authUser := ""
+ authPass := ""
+ authCrypt := ""
+ authSalt := ""
+ mutex := C.int(C.SQLITE_OPEN_FULLMUTEX)
txlock := "BEGIN"
+
+ // PRAGMA's
+ autoVacuum := -1
busyTimeout := 5000
+ caseSensitiveLike := -1
+ deferForeignKeys := -1
foreignKeys := -1
+ ignoreCheckConstraints := -1
+ journalMode := "DELETE"
+ lockingMode := "NORMAL"
+ queryOnly := -1
recursiveTriggers := -1
+ secureDelete := "DEFAULT"
+ synchronousMode := "NORMAL"
+ writableSchema := -1
+
pos := strings.IndexRune(dsn, '?')
if pos >= 1 {
params, err := url.ParseQuery(dsn[pos+1:])
@@ -645,11 +921,29 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
return nil, err
}
+ // Authentication
+ if _, ok := params["_auth"]; ok {
+ authCreate = true
+ }
+ if val := params.Get("_auth_user"); val != "" {
+ authUser = val
+ }
+ if val := params.Get("_auth_pass"); val != "" {
+ authPass = val
+ }
+ if val := params.Get("_auth_crypt"); val != "" {
+ authCrypt = val
+ }
+ if val := params.Get("_auth_salt"); val != "" {
+ authSalt = val
+ }
+
// _loc
if val := params.Get("_loc"); val != "" {
- if val == "auto" {
+ switch strings.ToLower(val) {
+ case "auto":
loc = time.Local
- } else {
+ default:
loc, err = time.LoadLocation(val)
if err != nil {
return nil, fmt.Errorf("Invalid _loc: %v: %v", val, err)
@@ -657,18 +951,21 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
}
}
- // _busy_timeout
- if val := params.Get("_busy_timeout"); val != "" {
- iv, err := strconv.ParseInt(val, 10, 64)
- if err != nil {
- return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
+ // _mutex
+ if val := params.Get("_mutex"); val != "" {
+ switch strings.ToLower(val) {
+ case "no":
+ mutex = C.SQLITE_OPEN_NOMUTEX
+ case "full":
+ mutex = C.SQLITE_OPEN_FULLMUTEX
+ default:
+ return nil, fmt.Errorf("Invalid _mutex: %v", val)
}
- busyTimeout = int(iv)
}
// _txlock
if val := params.Get("_txlock"); val != "" {
- switch val {
+ switch strings.ToLower(val) {
case "immediate":
txlock = "BEGIN IMMEDIATE"
case "exclusive":
@@ -680,27 +977,262 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
}
}
- // _foreign_keys
- if val := params.Get("_foreign_keys"); val != "" {
- switch val {
- case "1":
- foreignKeys = 1
- case "0":
+ // Auto Vacuum (_vacuum)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_auto_vacuum
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_auto_vacuum"]; ok {
+ pkey = "_auto_vacuum"
+ }
+ if _, ok := params["_vacuum"]; ok {
+ pkey = "_vacuum"
+ }
+ if val := params.Get(pkey); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "none":
+ autoVacuum = 0
+ case "1", "full":
+ autoVacuum = 1
+ case "2", "incremental":
+ autoVacuum = 2
+ default:
+ return nil, fmt.Errorf("Invalid _auto_vacuum: %v, expecting value of '0 NONE 1 FULL 2 INCREMENTAL'", val)
+ }
+ }
+
+ // Busy Timeout (_busy_timeout)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_busy_timeout
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_busy_timeout"]; ok {
+ pkey = "_busy_timeout"
+ }
+ if _, ok := params["_timeout"]; ok {
+ pkey = "_timeout"
+ }
+ if val := params.Get(pkey); val != "" {
+ iv, err := strconv.ParseInt(val, 10, 64)
+ if err != nil {
+ return nil, fmt.Errorf("Invalid _busy_timeout: %v: %v", val, err)
+ }
+ busyTimeout = int(iv)
+ }
+
+ // Case Sensitive Like (_cslike)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_case_sensitive_like
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_case_sensitive_like"]; ok {
+ pkey = "_case_sensitive_like"
+ }
+ if _, ok := params["_cslike"]; ok {
+ pkey = "_cslike"
+ }
+ if val := params.Get(pkey); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
+ caseSensitiveLike = 0
+ case "1", "yes", "true", "on":
+ caseSensitiveLike = 1
+ default:
+ return nil, fmt.Errorf("Invalid _case_sensitive_like: %v, expecting boolean value of '0 1 false true no yes off on'", val)
+ }
+ }
+
+ // Defer Foreign Keys (_defer_foreign_keys | _defer_fk)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_defer_foreign_keys"]; ok {
+ pkey = "_defer_foreign_keys"
+ }
+ if _, ok := params["_defer_fk"]; ok {
+ pkey = "_defer_fk"
+ }
+ if val := params.Get(pkey); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
+ deferForeignKeys = 0
+ case "1", "yes", "true", "on":
+ deferForeignKeys = 1
+ default:
+ return nil, fmt.Errorf("Invalid _defer_foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
+ }
+ }
+
+ // Foreign Keys (_foreign_keys | _fk)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_foreign_keys
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_foreign_keys"]; ok {
+ pkey = "_foreign_keys"
+ }
+ if _, ok := params["_fk"]; ok {
+ pkey = "_fk"
+ }
+ if val := params.Get(pkey); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
foreignKeys = 0
+ case "1", "yes", "true", "on":
+ foreignKeys = 1
default:
- return nil, fmt.Errorf("Invalid _foreign_keys: %v", val)
+ return nil, fmt.Errorf("Invalid _foreign_keys: %v, expecting boolean value of '0 1 false true no yes off on'", val)
}
}
- // _recursive_triggers
- if val := params.Get("_recursive_triggers"); val != "" {
- switch val {
- case "1":
- recursiveTriggers = 1
- case "0":
+ // Ignore CHECK Constrains (_ignore_check_constraints)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints
+ //
+ if val := params.Get("_ignore_check_constraints"); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
+ ignoreCheckConstraints = 0
+ case "1", "yes", "true", "on":
+ ignoreCheckConstraints = 1
+ default:
+ return nil, fmt.Errorf("Invalid _ignore_check_constraints: %v, expecting boolean value of '0 1 false true no yes off on'", val)
+ }
+ }
+
+ // Journal Mode (_journal_mode | _journal)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_journal_mode
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_journal_mode"]; ok {
+ pkey = "_journal_mode"
+ }
+ if _, ok := params["_journal"]; ok {
+ pkey = "_journal"
+ }
+ if val := params.Get(pkey); val != "" {
+ switch strings.ToUpper(val) {
+ case "DELETE", "TRUNCATE", "PERSIST", "MEMORY", "OFF":
+ journalMode = strings.ToUpper(val)
+ case "WAL":
+ journalMode = strings.ToUpper(val)
+
+ // For WAL Mode set Synchronous Mode to 'NORMAL'
+ // See https://www.sqlite.org/pragma.html#pragma_synchronous
+ synchronousMode = "NORMAL"
+ default:
+ return nil, fmt.Errorf("Invalid _journal: %v, expecting value of 'DELETE TRUNCATE PERSIST MEMORY WAL OFF'", val)
+ }
+ }
+
+ // Locking Mode (_locking)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_locking_mode
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_locking_mode"]; ok {
+ pkey = "_locking_mode"
+ }
+ if _, ok := params["_locking"]; ok {
+ pkey = "_locking"
+ }
+ if val := params.Get("_locking"); val != "" {
+ switch strings.ToUpper(val) {
+ case "NORMAL", "EXCLUSIVE":
+ lockingMode = strings.ToUpper(val)
+ default:
+ return nil, fmt.Errorf("Invalid _locking_mode: %v, expecting value of 'NORMAL EXCLUSIVE", val)
+ }
+ }
+
+ // Query Only (_query_only)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_query_only
+ //
+ if val := params.Get("_query_only"); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
+ queryOnly = 0
+ case "1", "yes", "true", "on":
+ queryOnly = 1
+ default:
+ return nil, fmt.Errorf("Invalid _query_only: %v, expecting boolean value of '0 1 false true no yes off on'", val)
+ }
+ }
+
+ // Recursive Triggers (_recursive_triggers)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_recursive_triggers
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_recursive_triggers"]; ok {
+ pkey = "_recursive_triggers"
+ }
+ if _, ok := params["_rt"]; ok {
+ pkey = "_rt"
+ }
+ if val := params.Get(pkey); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
recursiveTriggers = 0
+ case "1", "yes", "true", "on":
+ recursiveTriggers = 1
+ default:
+ return nil, fmt.Errorf("Invalid _recursive_triggers: %v, expecting boolean value of '0 1 false true no yes off on'", val)
+ }
+ }
+
+ // Secure Delete (_secure_delete)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_secure_delete
+ //
+ if val := params.Get("_secure_delete"); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
+ secureDelete = "OFF"
+ case "1", "yes", "true", "on":
+ secureDelete = "ON"
+ case "fast":
+ secureDelete = "FAST"
default:
- return nil, fmt.Errorf("Invalid _recursive_triggers: %v", val)
+ return nil, fmt.Errorf("Invalid _secure_delete: %v, expecting boolean value of '0 1 false true no yes off on fast'", val)
+ }
+ }
+
+ // Synchronous Mode (_synchronous | _sync)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_synchronous
+ //
+ pkey = "" // Reset pkey
+ if _, ok := params["_synchronous"]; ok {
+ pkey = "_synchronous"
+ }
+ if _, ok := params["_sync"]; ok {
+ pkey = "_sync"
+ }
+ if val := params.Get(pkey); val != "" {
+ switch strings.ToUpper(val) {
+ case "0", "OFF", "1", "NORMAL", "2", "FULL", "3", "EXTRA":
+ synchronousMode = strings.ToUpper(val)
+ default:
+ return nil, fmt.Errorf("Invalid _synchronous: %v, expecting value of '0 OFF 1 NORMAL 2 FULL 3 EXTRA'", val)
+ }
+ }
+
+ // Writable Schema (_writeable_schema)
+ //
+ // https://www.sqlite.org/pragma.html#pragma_writeable_schema
+ //
+ if val := params.Get("_writable_schema"); val != "" {
+ switch strings.ToLower(val) {
+ case "0", "no", "false", "off":
+ writableSchema = 0
+ case "1", "yes", "true", "on":
+ writableSchema = 1
+ default:
+ return nil, fmt.Errorf("Invalid _writable_schema: %v, expecting boolean value of '0 1 false true no yes off on'", val)
}
}
@@ -713,9 +1245,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
name := C.CString(dsn)
defer C.free(unsafe.Pointer(name))
rv := C._sqlite3_open_v2(name, &db,
- C.SQLITE_OPEN_FULLMUTEX|
- C.SQLITE_OPEN_READWRITE|
- C.SQLITE_OPEN_CREATE,
+ mutex|C.SQLITE_OPEN_READWRITE|C.SQLITE_OPEN_CREATE,
nil)
if rv != 0 {
return nil, Error{Code: ErrNo(rv)}
@@ -739,30 +1269,268 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
}
return nil
}
- if foreignKeys == 0 {
- if err := exec("PRAGMA foreign_keys = OFF;"); err != nil {
+
+ // USER AUTHENTICATION
+ //
+ // User Authentication is always performed even when
+ // sqlite_userauth is not compiled in, because without user authentication
+ // the authentication is a no-op.
+ //
+ // Workflow
+ // - Authenticate
+ // ON::SUCCESS => Continue
+ // ON::SQLITE_AUTH => Return error and exit Open(...)
+ //
+ // - Activate User Authentication
+ // Check if the user wants to activate User Authentication.
+ // If so then first create a temporary AuthConn to the database
+ // This is possible because we are already succesfully authenticated.
+ //
+ // - Check if `sqlite_user`` table exists
+ // YES => Add the provided user from DSN as Admin User and
+ // activate user authentication.
+ // NO => Continue
+ //
+
+ // Create connection to SQLite
+ conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
+
+ // Password Cipher has to be registerd before authentication
+ if len(authCrypt) > 0 {
+ switch strings.ToUpper(authCrypt) {
+ case "SHA1":
+ if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA1, true); err != nil {
+ return nil, fmt.Errorf("CryptEncoderSHA1: %s", err)
+ }
+ case "SSHA1":
+ if len(authSalt) == 0 {
+ return nil, fmt.Errorf("_auth_crypt=ssha1, requires _auth_salt")
+ }
+ if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA1(authSalt), true); err != nil {
+ return nil, fmt.Errorf("CryptEncoderSSHA1: %s", err)
+ }
+ case "SHA256":
+ if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA256, true); err != nil {
+ return nil, fmt.Errorf("CryptEncoderSHA256: %s", err)
+ }
+ case "SSHA256":
+ if len(authSalt) == 0 {
+ return nil, fmt.Errorf("_auth_crypt=ssha256, requires _auth_salt")
+ }
+ if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA256(authSalt), true); err != nil {
+ return nil, fmt.Errorf("CryptEncoderSSHA256: %s", err)
+ }
+ case "SHA384":
+ if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA384, true); err != nil {
+ return nil, fmt.Errorf("CryptEncoderSHA384: %s", err)
+ }
+ case "SSHA384":
+ if len(authSalt) == 0 {
+ return nil, fmt.Errorf("_auth_crypt=ssha384, requires _auth_salt")
+ }
+ if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA384(authSalt), true); err != nil {
+ return nil, fmt.Errorf("CryptEncoderSSHA384: %s", err)
+ }
+ case "SHA512":
+ if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSHA512, true); err != nil {
+ return nil, fmt.Errorf("CryptEncoderSHA512: %s", err)
+ }
+ case "SSHA512":
+ if len(authSalt) == 0 {
+ return nil, fmt.Errorf("_auth_crypt=ssha512, requires _auth_salt")
+ }
+ if err := conn.RegisterFunc("sqlite_crypt", CryptEncoderSSHA512(authSalt), true); err != nil {
+ return nil, fmt.Errorf("CryptEncoderSSHA512: %s", err)
+ }
+ }
+ }
+
+ // Preform Authentication
+ if err := conn.Authenticate(authUser, authPass); err != nil {
+ return nil, err
+ }
+
+ // Register: authenticate
+ // Authenticate will perform an authentication of the provided username
+ // and password against the database.
+ //
+ // If a database contains the SQLITE_USER table, then the
+ // call to Authenticate must be invoked with an
+ // appropriate username and password prior to enable read and write
+ //access to the database.
+ //
+ // Return SQLITE_OK on success or SQLITE_ERROR if the username/password
+ // combination is incorrect or unknown.
+ //
+ // If the SQLITE_USER table is not present in the database file, then
+ // this interface is a harmless no-op returnning SQLITE_OK.
+ if err := conn.RegisterFunc("authenticate", conn.authenticate, true); err != nil {
+ return nil, err
+ }
+ //
+ // Register: auth_user_add
+ // auth_user_add can be used (by an admin user only)
+ // to create a new user. When called on a no-authentication-required
+ // database, this routine converts the database into an authentication-
+ // required database, automatically makes the added user an
+ // administrator, and logs in the current connection as that user.
+ // The AuthUserAdd only works for the "main" database, not
+ // for any ATTACH-ed databases. Any call to AuthUserAdd by a
+ // non-admin user results in an error.
+ if err := conn.RegisterFunc("auth_user_add", conn.authUserAdd, true); err != nil {
+ return nil, err
+ }
+ //
+ // Register: auth_user_change
+ // auth_user_change can be used to change a users
+ // login credentials or admin privilege. Any user can change their own
+ // login credentials. Only an admin user can change another users login
+ // credentials or admin privilege setting. No user may change their own
+ // admin privilege setting.
+ if err := conn.RegisterFunc("auth_user_change", conn.authUserChange, true); err != nil {
+ return nil, err
+ }
+ //
+ // Register: auth_user_delete
+ // auth_user_delete can be used (by an admin user only)
+ // to delete a user. The currently logged-in user cannot be deleted,
+ // which guarantees that there is always an admin user and hence that
+ // the database cannot be converted into a no-authentication-required
+ // database.
+ if err := conn.RegisterFunc("auth_user_delete", conn.authUserDelete, true); err != nil {
+ return nil, err
+ }
+
+ // Register: auth_enabled
+ // auth_enabled can be used to check if user authentication is enabled
+ if err := conn.RegisterFunc("auth_enabled", conn.authEnabled, true); err != nil {
+ return nil, err
+ }
+
+ // Auto Vacuum
+ // Moved auto_vacuum command, the user preference for auto_vacuum needs to be implemented directly after
+ // the authentication and before the sqlite_user table gets created if the user
+ // decides to activate User Authentication because
+ // auto_vacuum needs to be set before any tables are created
+ // and activating user authentication creates the internal table `sqlite_user`.
+ if autoVacuum > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA auto_vacuum = %d;", autoVacuum)); err != nil {
C.sqlite3_close_v2(db)
return nil, err
}
- } else if foreignKeys == 1 {
- if err := exec("PRAGMA foreign_keys = ON;"); err != nil {
+ }
+
+ // Check if user wants to activate User Authentication
+ if authCreate {
+ // Before going any further, we need to check that the user
+ // has provided an username and password within the DSN.
+ // We are not allowed to continue.
+ if len(authUser) < 0 {
+ return nil, fmt.Errorf("Missing '_auth_user' while user authentication was requested with '_auth'")
+ }
+ if len(authPass) < 0 {
+ return nil, fmt.Errorf("Missing '_auth_pass' while user authentication was requested with '_auth'")
+ }
+
+ // Check if User Authentication is Enabled
+ authExists := conn.AuthEnabled()
+ if !authExists {
+ if err := conn.AuthUserAdd(authUser, authPass, true); err != nil {
+ return nil, err
+ }
+ }
+ }
+
+ // Case Sensitive LIKE
+ if caseSensitiveLike > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA case_sensitive_like = %d;", caseSensitiveLike)); err != nil {
C.sqlite3_close_v2(db)
return nil, err
}
}
- if recursiveTriggers == 0 {
- if err := exec("PRAGMA recursive_triggers = OFF;"); err != nil {
+
+ // Defer Foreign Keys
+ if deferForeignKeys > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA defer_foreign_keys = %d;", deferForeignKeys)); err != nil {
C.sqlite3_close_v2(db)
return nil, err
}
- } else if recursiveTriggers == 1 {
- if err := exec("PRAGMA recursive_triggers = ON;"); err != nil {
+ }
+
+ // Forgein Keys
+ if foreignKeys > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA foreign_keys = %d;", foreignKeys)); err != nil {
C.sqlite3_close_v2(db)
return nil, err
}
}
- conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
+ // Ignore CHECK Constraints
+ if ignoreCheckConstraints > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA ignore_check_constraints = %d;", ignoreCheckConstraints)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+ }
+
+ // Journal Mode
+ // Because default Journal Mode is DELETE this PRAGMA can always be executed.
+ if err := exec(fmt.Sprintf("PRAGMA journal_mode = %s;", journalMode)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+
+ // Locking Mode
+ // Because the default is NORMAL and this is not changed in this package
+ // by using the compile time SQLITE_DEFAULT_LOCKING_MODE this PRAGMA can always be executed
+ if err := exec(fmt.Sprintf("PRAGMA locking_mode = %s;", lockingMode)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+
+ // Query Only
+ if queryOnly > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA query_only = %d;", queryOnly)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+ }
+
+ // Recursive Triggers
+ if recursiveTriggers > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA recursive_triggers = %d;", recursiveTriggers)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+ }
+
+ // Secure Delete
+ //
+ // Because this package can set the compile time flag SQLITE_SECURE_DELETE with a build tag
+ // the default value for secureDelete var is 'DEFAULT' this way
+ // you can compile with secure_delete 'ON' and disable it for a specific database connection.
+ if secureDelete != "DEFAULT" {
+ if err := exec(fmt.Sprintf("PRAGMA secure_delete = %s;", secureDelete)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+ }
+
+ // Synchronous Mode
+ //
+ // Because default is NORMAL this statement is always executed
+ if err := exec(fmt.Sprintf("PRAGMA synchronous = %s;", synchronousMode)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+
+ // Writable Schema
+ if writableSchema > -1 {
+ if err := exec(fmt.Sprintf("PRAGMA writable_schema = %d;", writableSchema)); err != nil {
+ C.sqlite3_close_v2(db)
+ return nil, err
+ }
+ }
if len(d.Extensions) > 0 {
if err := conn.loadExtensions(d.Extensions); err != nil {
@@ -827,6 +1595,47 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
return ss, nil
}
+// Run-Time Limit Categories.
+// See: http://www.sqlite.org/c3ref/c_limit_attached.html
+const (
+ SQLITE_LIMIT_LENGTH = C.SQLITE_LIMIT_LENGTH
+ SQLITE_LIMIT_SQL_LENGTH = C.SQLITE_LIMIT_SQL_LENGTH
+ SQLITE_LIMIT_COLUMN = C.SQLITE_LIMIT_COLUMN
+ SQLITE_LIMIT_EXPR_DEPTH = C.SQLITE_LIMIT_EXPR_DEPTH
+ SQLITE_LIMIT_COMPOUND_SELECT = C.SQLITE_LIMIT_COMPOUND_SELECT
+ SQLITE_LIMIT_VDBE_OP = C.SQLITE_LIMIT_VDBE_OP
+ SQLITE_LIMIT_FUNCTION_ARG = C.SQLITE_LIMIT_FUNCTION_ARG
+ SQLITE_LIMIT_ATTACHED = C.SQLITE_LIMIT_ATTACHED
+ SQLITE_LIMIT_LIKE_PATTERN_LENGTH = C.SQLITE_LIMIT_LIKE_PATTERN_LENGTH
+ SQLITE_LIMIT_VARIABLE_NUMBER = C.SQLITE_LIMIT_VARIABLE_NUMBER
+ SQLITE_LIMIT_TRIGGER_DEPTH = C.SQLITE_LIMIT_TRIGGER_DEPTH
+ SQLITE_LIMIT_WORKER_THREADS = C.SQLITE_LIMIT_WORKER_THREADS
+)
+
+// GetFilename returns the absolute path to the file containing
+// the requested schema. When passed an empty string, it will
+// instead use the database's default schema: "main".
+// See: sqlite3_db_filename, https://www.sqlite.org/c3ref/db_filename.html
+func (c *SQLiteConn) GetFilename(schemaName string) string {
+ if schemaName == "" {
+ schemaName = "main"
+ }
+ return C.GoString(C.sqlite3_db_filename(c.db, C.CString(schemaName)))
+}
+
+// GetLimit returns the current value of a run-time limit.
+// See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
+func (c *SQLiteConn) GetLimit(id int) int {
+ return int(C._sqlite3_limit(c.db, C.int(id), -1))
+}
+
+// SetLimit changes the value of a run-time limits.
+// Then this method returns the prior value of the limit.
+// See: sqlite3_limit, http://www.sqlite.org/c3ref/limit.html
+func (c *SQLiteConn) SetLimit(id int, newVal int) int {
+ return int(C._sqlite3_limit(c.db, C.int(id), C.int(newVal)))
+}
+
// Close the statement.
func (s *SQLiteStmt) Close() error {
s.mu.Lock()
@@ -888,7 +1697,7 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
case int64:
rv = C.sqlite3_bind_int64(s.s, n, C.sqlite3_int64(v))
case bool:
- if bool(v) {
+ if v {
rv = C.sqlite3_bind_int(s.s, n, 1)
} else {
rv = C.sqlite3_bind_int(s.s, n, 0)
@@ -896,11 +1705,15 @@ func (s *SQLiteStmt) bind(args []namedValue) error {
case float64:
rv = C.sqlite3_bind_double(s.s, n, C.double(v))
case []byte:
- ln := len(v)
- if ln == 0 {
- v = placeHolder
+ if v == nil {
+ rv = C.sqlite3_bind_null(s.s, n)
+ } else {
+ ln := len(v)
+ if ln == 0 {
+ v = placeHolder
+ }
+ rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
}
- rv = C._sqlite3_bind_blob(s.s, n, unsafe.Pointer(&v[0]), C.int(ln))
case time.Time:
b := []byte(v.Format(SQLiteTimestampFormats[0]))
rv = C._sqlite3_bind_text(s.s, n, (*C.char)(unsafe.Pointer(&b[0])), C.int(len(b)))
@@ -939,18 +1752,20 @@ func (s *SQLiteStmt) query(ctx context.Context, args []namedValue) (driver.Rows,
done: make(chan struct{}),
}
- go func(db *C.sqlite3) {
- select {
- case <-ctx.Done():
+ if ctxdone := ctx.Done(); ctxdone != nil {
+ go func(db *C.sqlite3) {
select {
+ case <-ctxdone:
+ select {
+ case <-rows.done:
+ default:
+ C.sqlite3_interrupt(db)
+ rows.Close()
+ }
case <-rows.done:
- default:
- C.sqlite3_interrupt(db)
- rows.Close()
}
- case <-rows.done:
- }
- }(s.c.db)
+ }(s.c.db)
+ }
return rows, nil
}
@@ -984,15 +1799,21 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
return nil, err
}
- done := make(chan struct{})
- defer close(done)
- go func(db *C.sqlite3) {
- select {
- case <-ctx.Done():
- C.sqlite3_interrupt(db)
- case <-done:
- }
- }(s.c.db)
+ if ctxdone := ctx.Done(); ctxdone != nil {
+ done := make(chan struct{})
+ defer close(done)
+ go func(db *C.sqlite3) {
+ select {
+ case <-done:
+ case <-ctxdone:
+ select {
+ case <-done:
+ default:
+ C.sqlite3_interrupt(db)
+ }
+ }
+ }(s.c.db)
+ }
var rowid, changes C.longlong
rv := C._sqlite3_step(s.s, &rowid, &changes)
@@ -1086,7 +1907,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
case C.SQLITE_INTEGER:
val := int64(C.sqlite3_column_int64(rc.s.s, C.int(i)))
switch rc.decltype[i] {
- case "timestamp", "datetime", "date":
+ case columnTimestamp, columnDatetime, columnDate:
var t time.Time
// Assume a millisecond unix timestamp if it's 13 digits -- too
// large to be a reasonable timestamp in seconds.
@@ -1116,11 +1937,9 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
}
n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
switch dest[i].(type) {
- case sql.RawBytes:
- dest[i] = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
default:
slice := make([]byte, n)
- copy(slice[:], (*[1 << 30]byte)(unsafe.Pointer(p))[0:n])
+ copy(slice[:], (*[1 << 30]byte)(p)[0:n])
dest[i] = slice
}
case C.SQLITE_NULL:
@@ -1133,7 +1952,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
s := C.GoStringN((*C.char)(unsafe.Pointer(C.sqlite3_column_text(rc.s.s, C.int(i)))), C.int(n))
switch rc.decltype[i] {
- case "timestamp", "datetime", "date":
+ case columnTimestamp, columnDatetime, columnDate:
var t time.Time
s = strings.TrimSuffix(s, "Z")
for _, format := range SQLiteTimestampFormats {