diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/golite.go | 1496 |
1 files changed, 19 insertions, 1477 deletions
diff --git a/src/golite.go b/src/golite.go index 9732d1f..7c280a0 100644 --- a/src/golite.go +++ b/src/golite.go @@ -2,9 +2,6 @@ package golite import ( "context" - "crypto/sha1" - "crypto/sha256" - "crypto/sha512" "database/sql" "database/sql/driver" "errors" @@ -23,12 +20,19 @@ import ( ) /* -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #endif +Premises: +- FTS5 +- (default) foreign keys +- ICU +- (pre) update hooks +- unlock notify? +- (default) AUTOVACUUM +- SQLITE_THREADSAFE +- column metadata +*/ + +/* -#include <memory.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -41,77 +45,10 @@ import ( // its own file. Because we need a separate file anyway, the support // code for SQLite custom functions is in here. -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif -// #include <stdlib.h> - -// void _sqlite3_result_text(sqlite3_context* ctx, const char* s); -// void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l); - -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif - -#cgo CFLAGS: -std=gnu99 -#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_TRACE_SIZE_LIMIT=15 -#cgo CFLAGS: -DSQLITE_OMIT_DEPRECATED -#cgo CFLAGS: -DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 -#cgo CFLAGS: -DSQLITE_ENABLE_UPDATE_DELETE_LIMIT -#cgo CFLAGS: -Wno-deprecated-declarations -#cgo openbsd CFLAGS: -I/usr/local/include -#cgo openbsd LDFLAGS: -L/usr/local/lib - -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif - -#ifdef __CYGWIN__ -# include <errno.h> -#endif - -#ifndef SQLITE_OPEN_READWRITE -# define SQLITE_OPEN_READWRITE 0 -#endif - -#ifndef SQLITE_OPEN_FULLMUTEX -# define SQLITE_OPEN_FULLMUTEX 0 -#endif - -#ifndef SQLITE_DETERMINISTIC -# define SQLITE_DETERMINISTIC 0 -#endif - -#if defined(HAVE_PREAD64) && defined(HAVE_PWRITE64) -# undef USE_PREAD -# undef USE_PWRITE -# define USE_PREAD64 1 -# define USE_PWRITE64 1 -#elif defined(HAVE_PREAD) && defined(HAVE_PWRITE) -# undef USE_PREAD -# undef USE_PWRITE -# define USE_PREAD64 1 -# define USE_PWRITE64 1 -#endif - static int _sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) { -#ifdef SQLITE_OPEN_URI + // FIXME: remove obligatory flag return sqlite3_open_v2(filename, ppDb, flags | SQLITE_OPEN_URI, zVfs); -#else - return sqlite3_open_v2(filename, ppDb, flags, zVfs); -#endif } static int @@ -133,30 +70,6 @@ _sqlite3_exec(sqlite3* db, const char* pcmd, long long* rowid, long long* change return rv; } -#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY -extern int _sqlite3_step_blocking(sqlite3_stmt *stmt); -extern int _sqlite3_step_row_blocking(sqlite3_stmt* stmt, long long* rowid, long long* changes); -extern int _sqlite3_prepare_v2_blocking(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail); - -static int -_sqlite3_step_internal(sqlite3_stmt *stmt) -{ - return _sqlite3_step_blocking(stmt); -} - -static int -_sqlite3_step_row_internal(sqlite3_stmt* stmt, long long* rowid, long long* changes) -{ - return _sqlite3_step_row_blocking(stmt, rowid, changes); -} - -static int -_sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail) -{ - return _sqlite3_prepare_v2_blocking(db, zSql, nBytes, ppStmt, pzTail); -} - -#else static int _sqlite3_step_internal(sqlite3_stmt *stmt) { @@ -178,7 +91,6 @@ _sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_ { return sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail); } -#endif static void _sqlite3_result_text(sqlite3_context* ctx, const char* s) { @@ -189,9 +101,6 @@ static void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l) { sqlite3_result_blob(ctx, b, l, SQLITE_TRANSIENT); } -#if 0 -#endif - static int _sqlite3_create_function( @@ -218,43 +127,10 @@ void updateHookTrampoline(void*, int, char*, char*, sqlite3_int64); int authorizerTrampoline(void*, int, char*, char*, char*, char*); -#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 } -#if SQLITE_VERSION_NUMBER < 3012000 -static int sqlite3_system_errno(sqlite3 *db) { - return 0; -} -#endif - -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif // These wrappers are necessary because SQLITE_TRANSIENT // is a pointer constant, and cgo doesn't translate them correctly. @@ -266,167 +142,8 @@ static inline void my_result_blob(sqlite3_context *ctx, void *p, int np) { sqlite3_result_blob(ctx, p, np, SQLITE_TRANSIENT); } -#cgo CFLAGS: -DUSE_LIBSQLITE3 -#cgo linux LDFLAGS: -lsqlite3 -#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/sqlite/lib -lsqlite3 -#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/sqlite/include -#cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/sqlite/lib -lsqlite3 -#cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/sqlite/include -#cgo openbsd LDFLAGS: -lsqlite3 -#cgo solaris LDFLAGS: -lsqlite3 -#cgo windows LDFLAGS: -lsqlite3 -#cgo zos LDFLAGS: -lsqlite3 - -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif - -#cgo CFLAGS: -DSQLITE_OMIT_LOAD_EXTENSION - -#cgo CFLAGS: -DSQLITE_ALLOW_URI_AUTHORITY -#cgo LDFLAGS: -lm - -#cgo CFLAGS: -DSQLITE_ENABLE_API_ARMOR -#cgo LDFLAGS: -lm - -// #ifndef USE_LIBSQLITE3 -// #cgo CFLAGS: -DSQLITE_ENABLE_COLUMN_METADATA -// #include <sqlite3-binding.h> -// #else -// #include <sqlite3.h> -// #endif - -#cgo CFLAGS: -DSQLITE_DEFAULT_FOREIGN_KEYS=1 -#cgo LDFLAGS: -lm - -#cgo CFLAGS: -DSQLITE_ENABLE_FTS5 -#cgo LDFLAGS: -lm - -#cgo LDFLAGS: -licuuc -licui18n -#cgo CFLAGS: -DSQLITE_ENABLE_ICU -#cgo darwin,amd64 CFLAGS: -I/usr/local/opt/icu4c/include -#cgo darwin,amd64 LDFLAGS: -L/usr/local/opt/icu4c/lib -#cgo darwin,arm64 CFLAGS: -I/opt/homebrew/opt/icu4c/include -#cgo darwin,arm64 LDFLAGS: -L/opt/homebrew/opt/icu4c/lib -#cgo openbsd LDFLAGS: -lsqlite3 - -#cgo CFLAGS: -DSQLITE_INTROSPECTION_PRAGMAS -#cgo LDFLAGS: -lm - -#cgo CFLAGS: -DSQLITE_ENABLE_MATH_FUNCTIONS -#cgo LDFLAGS: -lm - -#cgo CFLAGS: -DSQLITE_FORCE_OS_TRACE=1 -#cgo CFLAGS: -DSQLITE_DEBUG_OS_TRACE=1 - -#cgo CFLAGS: -DSQLITE_ENABLE_PREUPDATE_HOOK -#cgo LDFLAGS: -lm - -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif - -// void preUpdateHookTrampoline(void*, sqlite3 *, int, char *, char *, sqlite3_int64, sqlite3_int64); - -#cgo CFLAGS: -DSQLITE_SECURE_DELETE=1 -#cgo LDFLAGS: -lm - -#cgo CFLAGS: -DSQLITE_SECURE_DELETE=FAST -#cgo LDFLAGS: -lm - -// #ifndef USE_LIBSQLITE3 -// #include <sqlite3-binding.h> -// #else -// #include <sqlite3.h> -// #endif - -#cgo CFLAGS: -DSQLITE_OMIT_DESERIALIZE - -#cgo CFLAGS: -DSQLITE_ENABLE_STAT4 -#cgo LDFLAGS: -lm - -#cgo CFLAGS: -DSQLITE_ENABLE_UNLOCK_NOTIFY - -// #include <stdlib.h> -// #include "sqlite3-binding.h" - extern void unlock_notify_callback(void *arg, int argc); -#cgo CFLAGS: -DSQLITE_USER_AUTHENTICATION -#cgo LDFLAGS: -lm -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif - -// static int -// _sqlite3_user_authenticate(sqlite3* db, const char* zUsername, const char* aPW, int nPW) -// { -// return sqlite3_user_authenticate(db, zUsername, aPW, nPW); -// } -// -// static int -// _sqlite3_user_add(sqlite3* db, const char* zUsername, const char* aPW, int nPW, int isAdmin) -// { -// return sqlite3_user_add(db, zUsername, aPW, nPW, isAdmin); -// } -// -// static int -// _sqlite3_user_change(sqlite3* db, const char* zUsername, const char* aPW, int nPW, int isAdmin) -// { -// return sqlite3_user_change(db, zUsername, aPW, nPW, isAdmin); -// } -// -// static int -// _sqlite3_user_delete(sqlite3* db, const char* zUsername) -// { -// return sqlite3_user_delete(db, zUsername); -// } -// -// static int -// _sqlite3_auth_enabled(sqlite3* db) -// { -// int exists = -1; -// -// sqlite3_stmt *stmt; -// sqlite3_prepare_v2(db, "select count(type) from sqlite_master WHERE type='table' and name='sqlite_user';", -1, &stmt, NULL); -// -// while ( sqlite3_step(stmt) == SQLITE_ROW) { -// exists = sqlite3_column_int(stmt, 0); -// } -// -// sqlite3_finalize(stmt); -// -// return exists; -// } - -#cgo CFLAGS: -DSQLITE_DEFAULT_AUTOVACUUM=1 -#cgo LDFLAGS: -lm - -#cgo CFLAGS: -DSQLITE_DEFAULT_AUTOVACUUM=2 -#cgo LDFLAGS: -lm - -#cgo CFLAGS: -std=gnu99 -#cgo CFLAGS: -DSQLITE_ENABLE_RTREE -#cgo CFLAGS: -DSQLITE_THREADSAFE -#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_ENABLE_COLUMN_METADATA=1 -#cgo CFLAGS: -Wno-deprecated-declarations - -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif - static inline char *_sqlite3_mprintf(char *zFormat, char *arg) { return sqlite3_mprintf(zFormat, arg); } @@ -497,6 +214,7 @@ static int cXRelease(sqlite3_vtab *pVTab, int isDestroy) { static inline int cXDisconnect(sqlite3_vtab *pVTab) { return cXRelease(pVTab, 0); } + static inline int cXDestroy(sqlite3_vtab *pVTab) { return cXRelease(pVTab, 1); } @@ -664,50 +382,7 @@ static int _sqlite3_create_module_eponymous_only(sqlite3 *db, const char *zName, return sqlite3_create_module_v2(db, zName, &goModuleEponymousOnly, (void*) pClientData, goMDestroy); } -#cgo CFLAGS: -I. -#cgo linux LDFLAGS: -ldl -#cgo linux,ppc LDFLAGS: -lpthread -#cgo linux,ppc64 LDFLAGS: -lpthread -#cgo linux,ppc64le LDFLAGS: -lpthread - -#cgo CFLAGS: -D__EXTENSIONS__=1 -#cgo LDFLAGS: -lc - -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif - int traceCallbackTrampoline(unsigned int traceEventCode, void *ctx, void *p, void *x); - -// #ifndef USE_LIBSQLITE3 -// #include "sqlite3-binding.h" -// #else -// #include <sqlite3.h> -// #endif - -// #include <windows.h> -// -// void usleep(__int64 usec) -// { -// HANDLE timer; -// LARGE_INTEGER ft; -// -// // Convert to 100 nanosecond interval, negative value indicates relative time -// ft.QuadPart = -(10*usec); -// -// timer = CreateWaitableTimer(NULL, TRUE, NULL); -// SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); -// WaitForSingleObject(timer, INFINITE); -// CloseHandle(timer); -// } -// -// #cgo CFLAGS: -I. -// #cgo CFLAGS: -fno-stack-check -// #cgo CFLAGS: -fno-stack-protector -// #cgo CFLAGS: -mno-stack-arg-probe -// #cgo windows,386 CFLAGS: -D_USE_32BIT_TIME_T */ import "C" @@ -777,77 +452,6 @@ func (b *SQLiteBackup) Close() error { } return nil } -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -/* -//export callbackTrampoline -func callbackTrampoline(ctx *C.sqlite3_context, argc int, argv **C.sqlite3_value) { - args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc] - fi := lookupHandle(C.sqlite3_user_data(ctx)).(*functionInfo) - fi.Call(ctx, args) -} - -//export stepTrampoline -func stepTrampoline(ctx *C.sqlite3_context, argc C.int, argv **C.sqlite3_value) { - args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:int(argc):int(argc)] - ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo) - ai.Step(ctx, args) -} - -//export doneTrampoline -func doneTrampoline(ctx *C.sqlite3_context) { - ai := lookupHandle(C.sqlite3_user_data(ctx)).(*aggInfo) - ai.Done(ctx) -} - -//export compareTrampoline -func compareTrampoline(handlePtr unsafe.Pointer, la C.int, a *C.char, lb C.int, b *C.char) C.int { - cmp := lookupHandle(handlePtr).(func(string, string) int) - return C.int(cmp(C.GoStringN(a, la), C.GoStringN(b, lb))) -} - -//export commitHookTrampoline -func commitHookTrampoline(handle unsafe.Pointer) int { - callback := lookupHandle(handle).(func() int) - return callback() -} - -//export rollbackHookTrampoline -func rollbackHookTrampoline(handle unsafe.Pointer) { - callback := lookupHandle(handle).(func()) - callback() -} - -//export updateHookTrampoline -func updateHookTrampoline(handle unsafe.Pointer, op int, db *C.char, table *C.char, rowid int64) { - callback := lookupHandle(handle).(func(int, string, string, int64)) - callback(op, C.GoString(db), C.GoString(table), rowid) -} - -//export authorizerTrampoline -func authorizerTrampoline(handle unsafe.Pointer, op int, arg1 *C.char, arg2 *C.char, arg3 *C.char) int { - callback := lookupHandle(handle).(func(int, string, string, string) int) - return callback(op, C.GoString(arg1), C.GoString(arg2), C.GoString(arg3)) -} - -//export preUpdateHookTrampoline -func preUpdateHookTrampoline(handle unsafe.Pointer, dbHandle uintptr, op int, db *C.char, table *C.char, oldrowid int64, newrowid int64) { - hval := lookupHandleVal(handle) - data := SQLitePreUpdateData{ - Conn: hval.db, - Op: op, - DatabaseName: C.GoString(db), - TableName: C.GoString(table), - OldRowID: oldrowid, - NewRowID: newrowid, - } - callback := hval.val.(func(SQLitePreUpdateData)) - callback(data) -} -*/ // Use handles to avoid passing Go pointers to C. type handleVal struct { @@ -1161,12 +765,8 @@ func callbackSyntheticForTests(v reflect.Value, err error) callbackArgConverter return v, err } } -// Extracted from Go database/sql source code - -// Copyright 2011 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. +// Extracted from Go database/sql source code // Type conversions for Scan. var errNilPtr = errors.New("destination pointer is nil") // embedded in descriptive error @@ -1448,6 +1048,7 @@ func asBytes(buf []byte, rv reflect.Value) (b []byte, ok bool) { } return } + /* Package sqlite3 provides interface to SQLite3 databases. @@ -1503,9 +1104,6 @@ extension for a Regexp matcher operation. } } - #ifdef _WIN32 - __declspec(dllexport) - #endif int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) { SQLITE_EXTENSION_INIT2(api); @@ -1581,10 +1179,6 @@ You can then use the custom driver by passing its name to sql.Open. See the documentation of RegisterFunc for more details. */ -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. // ErrNo inherit errno. type ErrNo int @@ -1719,13 +1313,6 @@ var ( ErrNoticeRecoverRollback = ErrNotice.Extend(2) ErrWarningAutoIndex = ErrWarning.Extend(1) ) -// Copyright (C) 2019 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. - -// //go:build cgo // +build cgo // SQLiteTimestampFormats is timestamp formats understood by both this module // and SQLite. The first format in the slice will be used when saving time @@ -1751,20 +1338,9 @@ const ( columnTimestamp string = "timestamp" ) -// This variable can be replaced with -ldflags like below: -// go build -ldflags="-X 'github.com/mattn/go-sqlite3.driverName=my-sqlite3'" var driverName = "sqlite3" - -/* func init() { - sql.Register("sqlite3", &SQLiteDriver{}) -} -*/ - -func init() { - if driverName != "" { - sql.Register(driverName, &SQLiteDriver{}) - } + sql.Register(driverName, &SQLiteDriver{}) } // Version returns SQLite library version information. @@ -2596,11 +2172,6 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { // Options var loc *time.Location - // authCreate := false - // authUser := "" - // authPass := "" - // authCrypt := "" - // authSalt := "" mutex := C.int(C.SQLITE_OPEN_FULLMUTEX) txlock := "BEGIN" @@ -2628,25 +2199,6 @@ 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 != "" { switch strings.ToLower(val) { @@ -3006,145 +2558,9 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { return nil, err } - // 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 successfully 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 registered 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 @@ -3158,29 +2574,6 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) { } } - /* - // 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 { @@ -3797,10 +3190,6 @@ func (rc *SQLiteRows) nextSyncLocked(dest []driver.Value) error { } return nil } -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. const i64 = unsafe.Sizeof(int(0)) > 4 @@ -3871,124 +3260,6 @@ func (c *SQLiteContext) ResultText(s string) { func (c *SQLiteContext) ResultZeroblob(n int) { C.sqlite3_result_zeroblob((*C.sqlite3_context)(c), C.int(n)) } -// 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. - -// This file provides several different implementations for the -// default embedded sqlite_crypt function. -// This function is uses a caesar-cypher by default -// and is used within the UserAuthentication module to encode -// the password. -// -// The provided functions can be used as an overload to the sqlite_crypt -// function through the use of the RegisterFunc on the connection. -// -// Because the functions can serv a purpose to an end-user -// without using the UserAuthentication module -// the functions are default compiled in. -// -// From SQLITE3 - user-auth.txt -// The sqlite_user.pw field is encoded by a built-in SQL function -// "sqlite_crypt(X,Y)". The two arguments are both BLOBs. The first argument -// is the plaintext password supplied to the sqlite3_user_authenticate() -// interface. The second argument is the sqlite_user.pw value and is supplied -// so that the function can extract the "salt" used by the password encoder. -// The result of sqlite_crypt(X,Y) is another blob which is the value that -// ends up being stored in sqlite_user.pw. To verify credentials X supplied -// by the sqlite3_user_authenticate() routine, SQLite runs: -// -// sqlite_user.pw == sqlite_crypt(X, sqlite_user.pw) -// -// To compute an appropriate sqlite_user.pw value from a new or modified -// password X, sqlite_crypt(X,NULL) is run. A new random salt is selected -// when the second argument is NULL. -// -// The built-in version of of sqlite_crypt() uses a simple Caesar-cypher -// which prevents passwords from being revealed by searching the raw database -// for ASCII text, but is otherwise trivally broken. For better password -// security, the database should be encrypted using the SQLite Encryption -// Extension or similar technology. Or, the application can use the -// sqlite3_create_function() interface to provide an alternative -// implementation of sqlite_crypt() that computes a stronger password hash, -// perhaps using a cryptographic hash function like SHA1. - -// CryptEncoderSHA1 encodes a password with SHA1 -func CryptEncoderSHA1(pass []byte, hash any) []byte { - h := sha1.Sum(pass) - return h[:] -} - -// CryptEncoderSSHA1 encodes a password with SHA1 with the -// configured salt. -func CryptEncoderSSHA1(salt string) func(pass []byte, hash any) []byte { - return func(pass []byte, hash any) []byte { - s := []byte(salt) - p := append(pass, s...) - h := sha1.Sum(p) - return h[:] - } -} - -// CryptEncoderSHA256 encodes a password with SHA256 -func CryptEncoderSHA256(pass []byte, hash any) []byte { - h := sha256.Sum256(pass) - return h[:] -} - -// CryptEncoderSSHA256 encodes a password with SHA256 -// with the configured salt -func CryptEncoderSSHA256(salt string) func(pass []byte, hash any) []byte { - return func(pass []byte, hash any) []byte { - s := []byte(salt) - p := append(pass, s...) - h := sha256.Sum256(p) - return h[:] - } -} - -// CryptEncoderSHA384 encodes a password with SHA384 -func CryptEncoderSHA384(pass []byte, hash any) []byte { - h := sha512.Sum384(pass) - return h[:] -} - -// CryptEncoderSSHA384 encodes a password with SHA384 -// with the configured salt -func CryptEncoderSSHA384(salt string) func(pass []byte, hash any) []byte { - return func(pass []byte, hash any) []byte { - s := []byte(salt) - p := append(pass, s...) - h := sha512.Sum384(p) - return h[:] - } -} - -// CryptEncoderSHA512 encodes a password with SHA512 -func CryptEncoderSHA512(pass []byte, hash any) []byte { - h := sha512.Sum512(pass) - return h[:] -} - -// CryptEncoderSSHA512 encodes a password with SHA512 -// with the configured salt -func CryptEncoderSSHA512(salt string) func(pass []byte, hash any) []byte { - return func(pass []byte, hash any) []byte { - s := []byte(salt) - p := append(pass, s...) - h := sha512.Sum512(p) - return h[:] - } -} - -// EOF -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build cgo && go1.8 // +build cgo,go1.8 // Ping implement Pinger. func (c *SQLiteConn) Ping(ctx context.Context) error { @@ -4028,19 +3299,6 @@ func (s *SQLiteStmt) QueryContext(ctx context.Context, args []driver.NamedValue) func (s *SQLiteStmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) { return s.exec(ctx, args) } -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build libsqlite3 // +build libsqlite3 - -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build !sqlite_omit_load_extension // +build !sqlite_omit_load_extension func (c *SQLiteConn) loadExtensions(extensions []string) error { rv := C.sqlite3_enable_load_extension(c.db, 1) @@ -4103,39 +3361,6 @@ func (c *SQLiteConn) loadExtension(lib string, entry *string) error { return nil } -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build sqlite_omit_load_extension // +build sqlite_omit_load_extension - -/* -func (c *SQLiteConn) loadExtensions(extensions []string) error { - return errors.New("Extensions have been disabled for static builds") -} - -func (c *SQLiteConn) LoadExtension(lib string, entry string) error { - return errors.New("Extensions have been disabled for static builds") -} -*/ -// Copyright (C) 2019 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. - -// //go:build sqlite_allow_uri_authority // +build sqlite_allow_uri_authority - -// Copyright (C) 2019 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. - -// //go:build !windows && sqlite_app_armor // +build !windows,sqlite_app_armor - -// //go:build sqlite_column_metadata // +build sqlite_column_metadata // ColumnTableName returns the table that is the origin of a particular result // column in a SELECT statement. @@ -4144,57 +3369,6 @@ func (c *SQLiteConn) LoadExtension(lib string, entry string) error { func (s *SQLiteStmt) ColumnTableName(n int) string { return C.GoString(C.sqlite3_column_table_name(s.s, C.int(n))) } -// Copyright (C) 2019 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. - -// //go:build sqlite_foreign_keys // +build sqlite_foreign_keys - -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build sqlite_fts5 || fts5 // +build sqlite_fts5 fts5 - -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build sqlite_icu || icu // +build sqlite_icu icu - -// Copyright (C) 2019 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. - -// //go:build sqlite_introspect // +build sqlite_introspect - -// Copyright (C) 2022 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build sqlite_math_functions // +build sqlite_math_functions - -// Copyright (C) 2022 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build sqlite_os_trace // +build sqlite_os_trace - -// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>. -// Copyright (C) 2018 segment.com <friends@segment.com> -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build cgo // +build cgo // SQLitePreUpdateData represents all of the data available during a // pre-update hook call. @@ -4206,15 +3380,7 @@ type SQLitePreUpdateData struct { OldRowID int64 NewRowID int64 } -// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>. -// Copyright (C) 2018 segment.com <friends@segment.com> -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. -// //go:build sqlite_preupdate_hook // +build sqlite_preupdate_hook - -/* // RegisterPreUpdateHook sets the pre-update hook for a connection. // // The callback is passed a SQLitePreUpdateData struct with the data for @@ -4225,116 +3391,12 @@ type SQLitePreUpdateData struct { // without creating a new one. func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) { if callback == nil { - C.sqlite3_preupdate_hook(c.db, nil, nil) + // C.sqlite3_preupdate_hook(c.db, nil, nil) } else { - C.sqlite3_preupdate_hook(c.db, (*[0]byte)(unsafe.Pointer(C.preUpdateHookTrampoline)), unsafe.Pointer(newHandle(c, callback))) - } -} - -// Depth returns the source path of the write, see sqlite3_preupdate_depth() -func (d *SQLitePreUpdateData) Depth() int { - return int(C.sqlite3_preupdate_depth(d.Conn.db)) -} - -// Count returns the number of columns in the row -func (d *SQLitePreUpdateData) Count() int { - return int(C.sqlite3_preupdate_count(d.Conn.db)) -} - -func (d *SQLitePreUpdateData) row(dest []any, new bool) error { - for i := 0; i < d.Count() && i < len(dest); i++ { - var val *C.sqlite3_value - var src any - - // Initially I tried making this just a function pointer argument, but - // it's absurdly complicated to pass C function pointers. - if new { - C.sqlite3_preupdate_new(d.Conn.db, C.int(i), &val) - } else { - C.sqlite3_preupdate_old(d.Conn.db, C.int(i), &val) - } - - switch C.sqlite3_value_type(val) { - case C.SQLITE_INTEGER: - src = int64(C.sqlite3_value_int64(val)) - case C.SQLITE_FLOAT: - src = float64(C.sqlite3_value_double(val)) - case C.SQLITE_BLOB: - len := C.sqlite3_value_bytes(val) - blobptr := C.sqlite3_value_blob(val) - src = C.GoBytes(blobptr, len) - case C.SQLITE_TEXT: - len := C.sqlite3_value_bytes(val) - cstrptr := unsafe.Pointer(C.sqlite3_value_text(val)) - src = C.GoBytes(cstrptr, len) - case C.SQLITE_NULL: - src = nil - } - - err := convertAssign(&dest[i], src) - if err != nil { - return err - } + // C.sqlite3_preupdate_hook(c.db, (*[0]byte)(unsafe.Pointer(C.preUpdateHookTrampoline)), unsafe.Pointer(newHandle(c, callback))) } - - return nil } -// Old populates dest with the row data to be replaced. This works similar to -// database/sql's Rows.Scan() -func (d *SQLitePreUpdateData) Old(dest ...any) error { - if d.Op == SQLITE_INSERT { - return errors.New("There is no old row for INSERT operations") - } - return d.row(dest, false) -} - -// New populates dest with the replacement row data. This works similar to -// database/sql's Rows.Scan() -func (d *SQLitePreUpdateData) New(dest ...any) error { - if d.Op == SQLITE_DELETE { - return errors.New("There is no new row for DELETE operations") - } - return d.row(dest, true) -} -*/ -// Copyright (C) 2019 G.J.R. Timmer <gjr.timmer@gmail.com>. -// Copyright (C) 2018 segment.com <friends@segment.com> -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build !sqlite_preupdate_hook && cgo // +build !sqlite_preupdate_hook,cgo - -// RegisterPreUpdateHook sets the pre-update hook for a connection. -// -// The callback is passed a SQLitePreUpdateData struct with the data for -// the update, as well as methods for fetching copies of impacted data. -// -// If there is an existing preupdate hook for this connection, it will be -// removed. If callback is nil the existing hook (if any) will be removed -// without creating a new one. -func (c *SQLiteConn) RegisterPreUpdateHook(callback func(SQLitePreUpdateData)) { - // NOOP -} -// Copyright (C) 2019 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. - -// //go:build sqlite_secure_delete // +build sqlite_secure_delete - -// Copyright (C) 2019 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. - -// //go:build sqlite_secure_delete_fast // +build sqlite_secure_delete_fast - -// //go:build !libsqlite3 || sqlite_serialize // +build !libsqlite3 sqlite_serialize - // Serialize returns a byte slice that is a serialization of the database. // // See https://www.sqlite.org/c3ref/serialize.html @@ -4395,31 +3457,6 @@ func (c *SQLiteConn) Deserialize(b []byte, schema string) error { } return nil } -// //go:build libsqlite3 && !sqlite_serialize // +build libsqlite3,!sqlite_serialize - -/* -func (c *SQLiteConn) Serialize(schema string) ([]byte, error) { - return nil, errors.New("sqlite3: Serialize requires the sqlite_serialize build tag when using the libsqlite3 build tag") -} - -func (c *SQLiteConn) Deserialize(b []byte, schema string) error { - return errors.New("sqlite3: Deserialize requires the sqlite_serialize build tag when using the libsqlite3 build tag") -} -*/ -// Copyright (C) 2019 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. - -// //go:build sqlite_stat4 // +build sqlite_stat4 - -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build cgo && sqlite_unlock_notify // +build cgo,sqlite_unlock_notify type unlock_notify_table struct { sync.Mutex @@ -4488,416 +3525,6 @@ func unlock_notify_wait(db *C.sqlite3) C.int { return C.SQLITE_OK } -// 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. - -// //go:build sqlite_userauth // +build sqlite_userauth - -/* -const ( - SQLITE_AUTH = C.SQLITE_AUTH -) - -var ( - ErrUnauthorized = errors.New("SQLITE_AUTH: Unauthorized") - ErrAdminRequired = errors.New("SQLITE_AUTH: Unauthorized; Admin Privileges Required") -) - -// 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 returning SQLITE_OK. -func (c *SQLiteConn) Authenticate(username, password string) error { - rv := c.authenticate(username, password) - switch rv { - case C.SQLITE_ERROR, C.SQLITE_AUTH: - return ErrUnauthorized - case C.SQLITE_OK: - return nil - default: - return c.lastError() - } -} - -// authenticate provides the actual authentication to SQLite. -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// C.SQLITE_OK (0) -// C.SQLITE_ERROR (1) -// C.SQLITE_AUTH (23) -func (c *SQLiteConn) authenticate(username, password string) int { - // Allocate C Variables - cuser := C.CString(username) - cpass := C.CString(password) - - // Free C Variables - defer func() { - C.free(unsafe.Pointer(cuser)) - C.free(unsafe.Pointer(cpass)) - }() - - return int(C._sqlite3_user_authenticate(c.db, cuser, cpass, C.int(len(password)))) -} - -// AuthUserAdd 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. -func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error { - isAdmin := 0 - if admin { - isAdmin = 1 - } - - rv := c.authUserAdd(username, password, isAdmin) - switch rv { - case C.SQLITE_ERROR, C.SQLITE_AUTH: - return ErrAdminRequired - case C.SQLITE_OK: - return nil - default: - return c.lastError() - } -} - -// authUserAdd enables the User Authentication if not enabled. -// Otherwise it will add a user. -// -// When user authentication is already enabled then this function -// can only be called by an admin. -// -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// C.SQLITE_OK (0) -// C.SQLITE_ERROR (1) -// C.SQLITE_AUTH (23) -func (c *SQLiteConn) authUserAdd(username, password string, admin int) int { - // Allocate C Variables - cuser := C.CString(username) - cpass := C.CString(password) - - // Free C Variables - defer func() { - C.free(unsafe.Pointer(cuser)) - C.free(unsafe.Pointer(cpass)) - }() - - return int(C._sqlite3_user_add(c.db, cuser, cpass, C.int(len(password)), C.int(admin))) -} - -// AuthUserChange 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. -func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error { - isAdmin := 0 - if admin { - isAdmin = 1 - } - - rv := c.authUserChange(username, password, isAdmin) - switch rv { - case C.SQLITE_ERROR, C.SQLITE_AUTH: - return ErrAdminRequired - case C.SQLITE_OK: - return nil - default: - return c.lastError() - } -} - -// authUserChange allows to modify a user. -// Users can change their own password. -// -// Only admins can change passwords for other users -// and modify the admin flag. -// -// The admin flag of the current logged in user cannot be changed. -// THis ensures that their is always an admin. -// -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// C.SQLITE_OK (0) -// C.SQLITE_ERROR (1) -// C.SQLITE_AUTH (23) -func (c *SQLiteConn) authUserChange(username, password string, admin int) int { - // Allocate C Variables - cuser := C.CString(username) - cpass := C.CString(password) - - // Free C Variables - defer func() { - C.free(unsafe.Pointer(cuser)) - C.free(unsafe.Pointer(cpass)) - }() - - return int(C._sqlite3_user_change(c.db, cuser, cpass, C.int(len(password)), C.int(admin))) -} - -// AuthUserDelete 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. -func (c *SQLiteConn) AuthUserDelete(username string) error { - rv := c.authUserDelete(username) - switch rv { - case C.SQLITE_ERROR, C.SQLITE_AUTH: - return ErrAdminRequired - case C.SQLITE_OK: - return nil - default: - return c.lastError() - } -} - -// authUserDelete can be used to delete a user. -// -// This function can only be executed by an admin. -// -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// C.SQLITE_OK (0) -// C.SQLITE_ERROR (1) -// C.SQLITE_AUTH (23) -func (c *SQLiteConn) authUserDelete(username string) int { - // Allocate C Variables - cuser := C.CString(username) - - // Free C Variables - defer func() { - C.free(unsafe.Pointer(cuser)) - }() - - return int(C._sqlite3_user_delete(c.db, cuser)) -} - -// AuthEnabled checks if the database is protected by user authentication -func (c *SQLiteConn) AuthEnabled() (exists bool) { - rv := c.authEnabled() - if rv == 1 { - exists = true - } - - return -} - -// authEnabled perform the actual check for user authentication. -// -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// 0 - Disabled -// 1 - Enabled -func (c *SQLiteConn) authEnabled() int { - return int(C._sqlite3_auth_enabled(c.db)) -} - -// EOF -// 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. - -// //go:build !sqlite_userauth // +build !sqlite_userauth - -// 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. -func (c *SQLiteConn) Authenticate(username, password string) error { - // NOOP - return nil -} - -// authenticate provides the actual authentication to SQLite. -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// C.SQLITE_OK (0) -// C.SQLITE_ERROR (1) -// C.SQLITE_AUTH (23) -func (c *SQLiteConn) authenticate(username, password string) int { - // NOOP - return 0 -} - -// AuthUserAdd 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. -func (c *SQLiteConn) AuthUserAdd(username, password string, admin bool) error { - // NOOP - return nil -} - -// authUserAdd enables the User Authentication if not enabled. -// Otherwise it will add a user. -// -// When user authentication is already enabled then this function -// can only be called by an admin. -// -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// C.SQLITE_OK (0) -// C.SQLITE_ERROR (1) -// C.SQLITE_AUTH (23) -func (c *SQLiteConn) authUserAdd(username, password string, admin int) int { - // NOOP - return 0 -} - -// AuthUserChange 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. -func (c *SQLiteConn) AuthUserChange(username, password string, admin bool) error { - // NOOP - return nil -} - -// authUserChange allows to modify a user. -// Users can change their own password. -// -// Only admins can change passwords for other users -// and modify the admin flag. -// -// The admin flag of the current logged in user cannot be changed. -// THis ensures that their is always an admin. -// -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// C.SQLITE_OK (0) -// C.SQLITE_ERROR (1) -// C.SQLITE_AUTH (23) -func (c *SQLiteConn) authUserChange(username, password string, admin int) int { - // NOOP - return 0 -} - -// AuthUserDelete 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. -func (c *SQLiteConn) AuthUserDelete(username string) error { - // NOOP - return nil -} - -// authUserDelete can be used to delete a user. -// -// This function can only be executed by an admin. -// -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// C.SQLITE_OK (0) -// C.SQLITE_ERROR (1) -// C.SQLITE_AUTH (23) -func (c *SQLiteConn) authUserDelete(username string) int { - // NOOP - return 0 -} - -// AuthEnabled checks if the database is protected by user authentication -func (c *SQLiteConn) AuthEnabled() (exists bool) { - // NOOP - return false -} - -// authEnabled perform the actual check for user authentication. -// -// This is not exported for usage in Go. -// It is however exported for usage within SQL by the user. -// -// Returns: -// -// 0 - Disabled -// 1 - Enabled -func (c *SQLiteConn) authEnabled() int { - // NOOP - return 0 -} -*/ - -// EOF -// Copyright (C) 2019 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. - -// //go:build sqlite_vacuum_full // +build sqlite_vacuum_full - -// Copyright (C) 2019 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. - -// //go:build sqlite_vacuum_incr // +build sqlite_vacuum_incr - -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build sqlite_vtable || vtable // +build sqlite_vtable vtable type sqliteModule struct { c *SQLiteConn @@ -5344,26 +3971,6 @@ func (c *SQLiteConn) CreateModule(moduleName string, module Module) error { } return nil } -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build !windows // +build !windows - -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build solaris // +build solaris - -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build sqlite_trace || trace // +build sqlite_trace trace // Trace... constants identify the possible events causing callback invocation. // Values are same as the corresponding SQLite Trace Event Codes. @@ -5624,25 +4231,12 @@ func (c *SQLiteConn) setSQLiteTrace(sqliteEventMask uint) error { } return nil } -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. // ColumnTypeDatabaseTypeName implement RowsColumnTypeDatabaseTypeName. func (rc *SQLiteRows) ColumnTypeDatabaseTypeName(i int) string { return C.GoString(C.sqlite3_column_decltype(rc.s.s, C.int(i))) } -/* -func (rc *SQLiteRows) ColumnTypeLength(index int) (length int64, ok bool) { - return 0, false -} - -func (rc *SQLiteRows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) { - return 0, 0, false -} -*/ - // ColumnTypeNullable implement RowsColumnTypeNullable. func (rc *SQLiteRows) ColumnTypeNullable(i int) (nullable, ok bool) { return true, true @@ -5716,59 +4310,7 @@ func databaseTypeConvSqlite(t string) int { return SQLITE_NULL } -// 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. - -// //go:build cgo // +build cgo - -// usleep is a function available on *nix based systems. -// This function is not present in Windows. -// Windows has a sleep function but this works with seconds -// and not with microseconds as usleep. -// -// This code should improve performance on windows because -// without the presence of usleep SQLite waits 1 second. -// -// Source: https://github.com/php/php-src/blob/PHP-5.0/win32/time.c -// License: https://github.com/php/php-src/blob/PHP-5.0/LICENSE -// Details: https://stackoverflow.com/questions/5801813/c-usleep-is-obsolete-workarounds-for-windows-mingw?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa - -// EOF -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build windows // +build windows - -// Copyright (C) 2019 Yasuhiro Matsumoto <mattn.jp@gmail.com>. -// -// Use of this source code is governed by an MIT-style -// license that can be found in the LICENSE file. - -// //go:build !cgo // +build !cgo var errorMsg = errors.New("Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub") - /* -type ( - SQLiteDriver struct { - Extensions []string - ConnectHook func(*SQLiteConn) error - } - SQLiteConn struct{} -) - -func (SQLiteDriver) Open(s string) (driver.Conn, error) { return nil, errorMsg } -func (c *SQLiteConn) RegisterAggregator(string, any, bool) error { return errorMsg } -func (c *SQLiteConn) RegisterAuthorizer(func(int, string, string, string) int) {} -func (c *SQLiteConn) RegisterCollation(string, func(string, string) int) error { return errorMsg } -func (c *SQLiteConn) RegisterCommitHook(func() int) {} -func (c *SQLiteConn) RegisterFunc(string, any, bool) error { return errorMsg } -func (c *SQLiteConn) RegisterRollbackHook(func()) {} -func (c *SQLiteConn) RegisterUpdateHook(func(int, string, string, int64)) {} - */ - var Version = version |