aboutsummaryrefslogtreecommitdiff
path: root/sqlite3.go
diff options
context:
space:
mode:
Diffstat (limited to 'sqlite3.go')
-rw-r--r--sqlite3.go117
1 files changed, 102 insertions, 15 deletions
diff --git a/sqlite3.go b/sqlite3.go
index e48594f..4e94256 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -78,8 +78,38 @@ _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)
+{
+ return sqlite3_step(stmt);
+}
+
static int
-_sqlite3_step(sqlite3_stmt* stmt, long long* rowid, long long* changes)
+_sqlite3_step_row_internal(sqlite3_stmt* stmt, long long* rowid, long long* changes)
{
int rv = sqlite3_step(stmt);
sqlite3* db = sqlite3_db_handle(stmt);
@@ -88,6 +118,13 @@ _sqlite3_step(sqlite3_stmt* stmt, long long* rowid, long long* changes)
return rv;
}
+static int
+_sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_stmt **ppStmt, const char **pzTail)
+{
+ return sqlite3_prepare_v2(db, zSql, nBytes, ppStmt, pzTail);
+}
+#endif
+
void _sqlite3_result_text(sqlite3_context* ctx, const char* s) {
sqlite3_result_text(ctx, s, -1, &free);
}
@@ -119,6 +156,8 @@ int commitHookTrampoline(void*);
void rollbackHookTrampoline(void*);
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
@@ -200,9 +239,48 @@ func Version() (libVersion string, libVersionNumber int, sourceID string) {
}
const (
+ // used by authorizer and pre_update_hook
SQLITE_DELETE = C.SQLITE_DELETE
SQLITE_INSERT = C.SQLITE_INSERT
SQLITE_UPDATE = C.SQLITE_UPDATE
+
+ // used by authorzier - as return value
+ SQLITE_OK = C.SQLITE_OK
+ SQLITE_IGNORE = C.SQLITE_IGNORE
+ SQLITE_DENY = C.SQLITE_DENY
+
+ // different actions query tries to do - passed as argument to authorizer
+ SQLITE_CREATE_INDEX = C.SQLITE_CREATE_INDEX
+ SQLITE_CREATE_TABLE = C.SQLITE_CREATE_TABLE
+ SQLITE_CREATE_TEMP_INDEX = C.SQLITE_CREATE_TEMP_INDEX
+ SQLITE_CREATE_TEMP_TABLE = C.SQLITE_CREATE_TEMP_TABLE
+ SQLITE_CREATE_TEMP_TRIGGER = C.SQLITE_CREATE_TEMP_TRIGGER
+ SQLITE_CREATE_TEMP_VIEW = C.SQLITE_CREATE_TEMP_VIEW
+ SQLITE_CREATE_TRIGGER = C.SQLITE_CREATE_TRIGGER
+ SQLITE_CREATE_VIEW = C.SQLITE_CREATE_VIEW
+ SQLITE_CREATE_VTABLE = C.SQLITE_CREATE_VTABLE
+ SQLITE_DROP_INDEX = C.SQLITE_DROP_INDEX
+ SQLITE_DROP_TABLE = C.SQLITE_DROP_TABLE
+ SQLITE_DROP_TEMP_INDEX = C.SQLITE_DROP_TEMP_INDEX
+ SQLITE_DROP_TEMP_TABLE = C.SQLITE_DROP_TEMP_TABLE
+ SQLITE_DROP_TEMP_TRIGGER = C.SQLITE_DROP_TEMP_TRIGGER
+ SQLITE_DROP_TEMP_VIEW = C.SQLITE_DROP_TEMP_VIEW
+ SQLITE_DROP_TRIGGER = C.SQLITE_DROP_TRIGGER
+ SQLITE_DROP_VIEW = C.SQLITE_DROP_VIEW
+ SQLITE_DROP_VTABLE = C.SQLITE_DROP_VTABLE
+ SQLITE_PRAGMA = C.SQLITE_PRAGMA
+ SQLITE_READ = C.SQLITE_READ
+ SQLITE_SELECT = C.SQLITE_SELECT
+ SQLITE_TRANSACTION = C.SQLITE_TRANSACTION
+ SQLITE_ATTACH = C.SQLITE_ATTACH
+ SQLITE_DETACH = C.SQLITE_DETACH
+ SQLITE_ALTER_TABLE = C.SQLITE_ALTER_TABLE
+ SQLITE_REINDEX = C.SQLITE_REINDEX
+ SQLITE_ANALYZE = C.SQLITE_ANALYZE
+ SQLITE_FUNCTION = C.SQLITE_FUNCTION
+ SQLITE_SAVEPOINT = C.SQLITE_SAVEPOINT
+ SQLITE_COPY = C.SQLITE_COPY
+ /*SQLITE_RECURSIVE = C.SQLITE_RECURSIVE*/
)
// SQLiteDriver implements driver.Driver.
@@ -440,6 +518,20 @@ func (c *SQLiteConn) RegisterUpdateHook(callback func(int, string, string, int64
}
}
+// RegisterAuthorizer sets the authorizer for connection.
+//
+// The parameters to the callback are the operation (one of the constants
+// SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE), and 1 to 3 arguments,
+// depending on operation. More details see:
+// https://www.sqlite.org/c3ref/c_alter_table.html
+func (c *SQLiteConn) RegisterAuthorizer(callback func(int, string, string, string) int) {
+ if callback == nil {
+ C.sqlite3_set_authorizer(c.db, nil, nil)
+ } else {
+ C.sqlite3_set_authorizer(c.db, (*[0]byte)(C.authorizerTrampoline), unsafe.Pointer(newHandle(c, callback)))
+ }
+}
+
// RegisterFunc makes a Go function available as a SQLite function.
//
// The Go function can have arguments of the following types: any
@@ -1286,7 +1378,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
// - 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.
+ // 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
@@ -1297,7 +1389,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
// Create connection to SQLite
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
- // Password Cipher has to be registerd before authentication
+ // Password Cipher has to be registered before authentication
if len(authCrypt) > 0 {
switch strings.ToUpper(authCrypt) {
case "SHA1":
@@ -1584,7 +1676,7 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
defer C.free(unsafe.Pointer(pquery))
var s *C.sqlite3_stmt
var tail *C.char
- rv := C.sqlite3_prepare_v2(c.db, pquery, -1, &s, &tail)
+ rv := C._sqlite3_prepare_v2_internal(c.db, pquery, C.int(-1), &s, &tail)
if rv != C.SQLITE_OK {
return nil, c.lastError()
}
@@ -1628,7 +1720,7 @@ func (c *SQLiteConn) GetFilename(schemaName string) string {
// 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))
+ return int(C._sqlite3_limit(c.db, C.int(id), C.int(-1)))
}
// SetLimit changes the value of a run-time limits.
@@ -1818,7 +1910,7 @@ func (s *SQLiteStmt) exec(ctx context.Context, args []namedValue) (driver.Result
}
var rowid, changes C.longlong
- rv := C._sqlite3_step(s.s, &rowid, &changes)
+ rv := C._sqlite3_step_row_internal(s.s, &rowid, &changes)
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
err := s.c.lastError()
C.sqlite3_reset(s.s)
@@ -1890,7 +1982,7 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
if rc.s.closed {
return io.EOF
}
- rv := C.sqlite3_step(rc.s.s)
+ rv := C._sqlite3_step_internal(rc.s.s)
if rv == C.SQLITE_DONE {
return io.EOF
}
@@ -1934,16 +2026,11 @@ func (rc *SQLiteRows) Next(dest []driver.Value) error {
case C.SQLITE_BLOB:
p := C.sqlite3_column_blob(rc.s.s, C.int(i))
if p == nil {
- dest[i] = nil
+ dest[i] = []byte{}
continue
}
- n := int(C.sqlite3_column_bytes(rc.s.s, C.int(i)))
- switch dest[i].(type) {
- default:
- slice := make([]byte, n)
- copy(slice[:], (*[1 << 30]byte)(p)[0:n])
- dest[i] = slice
- }
+ n := C.sqlite3_column_bytes(rc.s.s, C.int(i))
+ dest[i] = C.GoBytes(p, n)
case C.SQLITE_NULL:
dest[i] = nil
case C.SQLITE_TEXT: