aboutsummaryrefslogtreecommitdiff
path: root/sqlite3.go
diff options
context:
space:
mode:
Diffstat (limited to 'sqlite3.go')
-rw-r--r--sqlite3.go360
1 files changed, 331 insertions, 29 deletions
diff --git a/sqlite3.go b/sqlite3.go
index db65af5..2179cf2 100644
--- a/sqlite3.go
+++ b/sqlite3.go
@@ -8,7 +8,7 @@ package sqlite3
/*
#cgo CFLAGS: -std=gnu99
#cgo CFLAGS: -DSQLITE_ENABLE_RTREE -DSQLITE_THREADSAFE
-#cgo CFLAGS: -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS
+#cgo CFLAGS: -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4_UNICODE61
#include <sqlite3-binding.h>
#include <stdlib.h>
#include <string.h>
@@ -48,24 +48,35 @@ _sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
#include <stdint.h>
static int
-_sqlite3_exec(sqlite3* db, const char* pcmd, long* rowid, long* changes)
+_sqlite3_exec(sqlite3* db, const char* pcmd, long long* rowid, long long* changes)
{
int rv = sqlite3_exec(db, pcmd, 0, 0, 0);
- *rowid = (long) sqlite3_last_insert_rowid(db);
- *changes = (long) sqlite3_changes(db);
+ *rowid = (long long) sqlite3_last_insert_rowid(db);
+ *changes = (long long) sqlite3_changes(db);
return rv;
}
static int
-_sqlite3_step(sqlite3_stmt* stmt, long* rowid, long* changes)
+_sqlite3_step(sqlite3_stmt* stmt, long long* rowid, long long* changes)
{
int rv = sqlite3_step(stmt);
sqlite3* db = sqlite3_db_handle(stmt);
- *rowid = (long) sqlite3_last_insert_rowid(db);
- *changes = (long) sqlite3_changes(db);
+ *rowid = (long long) sqlite3_last_insert_rowid(db);
+ *changes = (long long) sqlite3_changes(db);
return rv;
}
+void _sqlite3_result_text(sqlite3_context* ctx, const char* s) {
+ sqlite3_result_text(ctx, s, -1, &free);
+}
+
+void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l) {
+ sqlite3_result_blob(ctx, b, l, SQLITE_TRANSIENT);
+}
+
+void callbackTrampoline(sqlite3_context*, int, sqlite3_value**);
+void stepTrampoline(sqlite3_context*, int, sqlite3_value**);
+void doneTrampoline(sqlite3_context*);
*/
import "C"
import (
@@ -75,6 +86,7 @@ import (
"fmt"
"io"
"net/url"
+ "reflect"
"runtime"
"strconv"
"strings"
@@ -117,9 +129,11 @@ type SQLiteDriver struct {
// Conn struct.
type SQLiteConn struct {
- db *C.sqlite3
- loc *time.Location
- txlock string
+ db *C.sqlite3
+ loc *time.Location
+ txlock string
+ funcs []*functionInfo
+ aggregators []*aggInfo
}
// Tx struct.
@@ -153,6 +167,107 @@ type SQLiteRows struct {
cls bool
}
+type functionInfo struct {
+ f reflect.Value
+ argConverters []callbackArgConverter
+ variadicConverter callbackArgConverter
+ retConverter callbackRetConverter
+}
+
+func (fi *functionInfo) Call(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
+ args, err := callbackConvertArgs(argv, fi.argConverters, fi.variadicConverter)
+ if err != nil {
+ callbackError(ctx, err)
+ return
+ }
+
+ ret := fi.f.Call(args)
+
+ if len(ret) == 2 && ret[1].Interface() != nil {
+ callbackError(ctx, ret[1].Interface().(error))
+ return
+ }
+
+ err = fi.retConverter(ctx, ret[0])
+ if err != nil {
+ callbackError(ctx, err)
+ return
+ }
+}
+
+type aggInfo struct {
+ constructor reflect.Value
+
+ // Active aggregator objects for aggregations in flight. The
+ // aggregators are indexed by a counter stored in the aggregation
+ // user data space provided by sqlite.
+ active map[int64]reflect.Value
+ next int64
+
+ stepArgConverters []callbackArgConverter
+ stepVariadicConverter callbackArgConverter
+
+ doneRetConverter callbackRetConverter
+}
+
+func (ai *aggInfo) agg(ctx *C.sqlite3_context) (int64, reflect.Value, error) {
+ aggIdx := (*int64)(C.sqlite3_aggregate_context(ctx, C.int(8)))
+ if *aggIdx == 0 {
+ *aggIdx = ai.next
+ ret := ai.constructor.Call(nil)
+ if len(ret) == 2 && ret[1].Interface() != nil {
+ return 0, reflect.Value{}, ret[1].Interface().(error)
+ }
+ if ret[0].IsNil() {
+ return 0, reflect.Value{}, errors.New("aggregator constructor returned nil state")
+ }
+ ai.next++
+ ai.active[*aggIdx] = ret[0]
+ }
+ return *aggIdx, ai.active[*aggIdx], nil
+}
+
+func (ai *aggInfo) Step(ctx *C.sqlite3_context, argv []*C.sqlite3_value) {
+ _, agg, err := ai.agg(ctx)
+ if err != nil {
+ callbackError(ctx, err)
+ return
+ }
+
+ args, err := callbackConvertArgs(argv, ai.stepArgConverters, ai.stepVariadicConverter)
+ if err != nil {
+ callbackError(ctx, err)
+ return
+ }
+
+ ret := agg.MethodByName("Step").Call(args)
+ if len(ret) == 1 && ret[0].Interface() != nil {
+ callbackError(ctx, ret[0].Interface().(error))
+ return
+ }
+}
+
+func (ai *aggInfo) Done(ctx *C.sqlite3_context) {
+ idx, agg, err := ai.agg(ctx)
+ if err != nil {
+ callbackError(ctx, err)
+ return
+ }
+ defer func() { delete(ai.active, idx) }()
+
+ ret := agg.MethodByName("Done").Call(nil)
+ if len(ret) == 2 && ret[1].Interface() != nil {
+ callbackError(ctx, ret[1].Interface().(error))
+ return
+ }
+
+ err = ai.doneRetConverter(ctx, ret[0])
+ if err != nil {
+ callbackError(ctx, err)
+ return
+ }
+}
+
// Commit transaction.
func (tx *SQLiteTx) Commit() error {
_, err := tx.c.exec("COMMIT")
@@ -165,6 +280,208 @@ func (tx *SQLiteTx) Rollback() error {
return err
}
+// RegisterFunc makes a Go function available as a SQLite function.
+//
+// The Go function can have arguments of the following types: any
+// numeric type except complex, bool, []byte, string and
+// interface{}. interface{} arguments are given the direct translation
+// of the SQLite data type: int64 for INTEGER, float64 for FLOAT,
+// []byte for BLOB, string for TEXT.
+//
+// The function can additionally be variadic, as long as the type of
+// the variadic argument is one of the above.
+//
+// If pure is true. SQLite will assume that the function's return
+// value depends only on its inputs, and make more aggressive
+// optimizations in its queries.
+//
+// See _example/go_custom_funcs for a detailed example.
+func (c *SQLiteConn) RegisterFunc(name string, impl interface{}, pure bool) error {
+ var fi functionInfo
+ fi.f = reflect.ValueOf(impl)
+ t := fi.f.Type()
+ if t.Kind() != reflect.Func {
+ return errors.New("Non-function passed to RegisterFunc")
+ }
+ if t.NumOut() != 1 && t.NumOut() != 2 {
+ return errors.New("SQLite functions 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")
+ }
+
+ numArgs := t.NumIn()
+ if t.IsVariadic() {
+ numArgs--
+ }
+
+ for i := 0; i < numArgs; i++ {
+ conv, err := callbackArg(t.In(i))
+ if err != nil {
+ return err
+ }
+ fi.argConverters = append(fi.argConverters, conv)
+ }
+
+ if t.IsVariadic() {
+ conv, err := callbackArg(t.In(numArgs).Elem())
+ if err != nil {
+ return err
+ }
+ fi.variadicConverter = 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.
+ numArgs = -1
+ }
+
+ conv, err := callbackRet(t.Out(0))
+ if err != nil {
+ return err
+ }
+ fi.retConverter = conv
+
+ // fi must outlast the database connection, or we'll have dangling pointers.
+ c.funcs = append(c.funcs, &fi)
+
+ cname := C.CString(name)
+ defer C.free(unsafe.Pointer(cname))
+ opts := C.SQLITE_UTF8
+ if pure {
+ opts |= C.SQLITE_DETERMINISTIC
+ }
+ rv := C.sqlite3_create_function(c.db, cname, C.int(numArgs), C.int(opts), unsafe.Pointer(&fi), (*[0]byte)(unsafe.Pointer(C.callbackTrampoline)), nil, nil)
+ if rv != C.SQLITE_OK {
+ return c.lastError()
+ }
+ return nil
+}
+
+// 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 := C.sqlite3_create_function(c.db, cname, C.int(stepNArgs), C.int(opts), unsafe.Pointer(&ai), nil, (*[0]byte)(unsafe.Pointer(C.stepTrampoline)), (*[0]byte)(unsafe.Pointer(C.doneTrampoline)))
+ if rv != C.SQLITE_OK {
+ return c.lastError()
+ }
+ return nil
+}
+
// AutoCommit return which currently auto commit or not.
func (c *SQLiteConn) AutoCommit() bool {
return int(C.sqlite3_get_autocommit(c.db)) != 0
@@ -243,7 +560,7 @@ func (c *SQLiteConn) exec(cmd string) (driver.Result, error) {
pcmd := C.CString(cmd)
defer C.free(unsafe.Pointer(pcmd))
- var rowid, changes C.long
+ var rowid, changes C.longlong
rv := C._sqlite3_exec(c.db, pcmd, &rowid, &changes)
if rv != C.SQLITE_OK {
return nil, c.lastError()
@@ -355,23 +672,8 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
conn := &SQLiteConn{db: db, loc: loc, txlock: txlock}
if len(d.Extensions) > 0 {
- rv = C.sqlite3_enable_load_extension(db, 1)
- if rv != C.SQLITE_OK {
- return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
- }
-
- for _, extension := range d.Extensions {
- cext := C.CString(extension)
- defer C.free(unsafe.Pointer(cext))
- rv = C.sqlite3_load_extension(db, cext, nil, nil)
- if rv != C.SQLITE_OK {
- return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
- }
- }
-
- rv = C.sqlite3_enable_load_extension(db, 0)
- if rv != C.SQLITE_OK {
- return nil, errors.New(C.GoString(C.sqlite3_errmsg(db)))
+ if err := conn.loadExtensions(d.Extensions); err != nil {
+ return nil, err
}
}
@@ -536,7 +838,7 @@ func (s *SQLiteStmt) Exec(args []driver.Value) (driver.Result, error) {
C.sqlite3_clear_bindings(s.s)
return nil, err
}
- var rowid, changes C.long
+ var rowid, changes C.longlong
rv := C._sqlite3_step(s.s, &rowid, &changes)
if rv != C.SQLITE_ROW && rv != C.SQLITE_OK && rv != C.SQLITE_DONE {
err := s.c.lastError()