123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- package sqlite3
- import "C"
- import (
- "errors"
- "fmt"
- "reflect"
- "strings"
- "sync"
- "unsafe"
- )
- const (
- TraceStmt = C.SQLITE_TRACE_STMT
- TraceProfile = C.SQLITE_TRACE_PROFILE
- TraceRow = C.SQLITE_TRACE_ROW
- TraceClose = C.SQLITE_TRACE_CLOSE
- )
- type TraceInfo struct {
-
-
-
- EventCode uint32
- AutoCommit bool
- ConnHandle uintptr
-
-
- StmtHandle uintptr
-
-
-
-
- StmtOrTrigger string
- ExpandedSQL string
-
-
- RunTimeNanosec int64
- DBError Error
- }
- type TraceUserCallback func(TraceInfo) int
- type TraceConfig struct {
- Callback TraceUserCallback
- EventMask C.uint
- WantExpandedSQL bool
- }
- func fillDBError(dbErr *Error, db *C.sqlite3) {
-
- dbErr.Code = ErrNo(C.sqlite3_errcode(db))
- dbErr.ExtendedCode = ErrNoExtended(C.sqlite3_extended_errcode(db))
- dbErr.err = C.GoString(C.sqlite3_errmsg(db))
- }
- func fillExpandedSQL(info *TraceInfo, db *C.sqlite3, pStmt unsafe.Pointer) {
- if pStmt == nil {
- panic("No SQLite statement pointer in P arg of trace_v2 callback")
- }
- expSQLiteCStr := C.sqlite3_expanded_sql((*C.sqlite3_stmt)(pStmt))
- if expSQLiteCStr == nil {
- fillDBError(&info.DBError, db)
- return
- }
- info.ExpandedSQL = C.GoString(expSQLiteCStr)
- }
- func traceCallbackTrampoline(
- traceEventCode C.uint,
- // Parameter named 'C' in SQLite docs = Context given at registration:
- ctx unsafe.Pointer,
- // Parameter named 'P' in SQLite docs (Primary event data?):
- p unsafe.Pointer,
-
- xValue unsafe.Pointer) C.int {
- if ctx == nil {
- panic(fmt.Sprintf("No context (ev 0x%x)", traceEventCode))
- }
- contextDB := (*C.sqlite3)(ctx)
- connHandle := uintptr(ctx)
- var traceConf TraceConfig
- var found bool
- if traceEventCode == TraceClose {
-
- traceConf, found = popTraceMapping(connHandle)
- } else {
- traceConf, found = lookupTraceMapping(connHandle)
- }
- if !found {
- panic(fmt.Sprintf("Mapping not found for handle 0x%x (ev 0x%x)",
- connHandle, traceEventCode))
- }
- var info TraceInfo
- info.EventCode = uint32(traceEventCode)
- info.AutoCommit = (int(C.sqlite3_get_autocommit(contextDB)) != 0)
- info.ConnHandle = connHandle
- switch traceEventCode {
- case TraceStmt:
- info.StmtHandle = uintptr(p)
- var xStr string
- if xValue != nil {
- xStr = C.GoString((*C.char)(xValue))
- }
- info.StmtOrTrigger = xStr
- if !strings.HasPrefix(xStr, "--") {
-
-
-
-
- if traceConf.WantExpandedSQL {
- fillExpandedSQL(&info, contextDB, p)
- }
- }
- case TraceProfile:
- info.StmtHandle = uintptr(p)
- if xValue == nil {
- panic("NULL pointer in X arg of trace_v2 callback for SQLITE_TRACE_PROFILE event")
- }
- info.RunTimeNanosec = *(*int64)(xValue)
-
- fillDBError(&info.DBError, contextDB)
- case TraceRow:
- info.StmtHandle = uintptr(p)
- case TraceClose:
- handle := uintptr(p)
- if handle != info.ConnHandle {
- panic(fmt.Sprintf("Different conn handle 0x%x (expected 0x%x) in SQLITE_TRACE_CLOSE event.",
- handle, info.ConnHandle))
- }
- default:
-
-
- }
-
-
-
-
-
- if traceConf.EventMask&traceEventCode == 0 {
- return 0
- }
- r := 0
- if traceConf.Callback != nil {
- r = traceConf.Callback(info)
- }
- return C.int(r)
- }
- type traceMapEntry struct {
- config TraceConfig
- }
- var traceMapLock sync.Mutex
- var traceMap = make(map[uintptr]traceMapEntry)
- func addTraceMapping(connHandle uintptr, traceConf TraceConfig) {
- traceMapLock.Lock()
- defer traceMapLock.Unlock()
- oldEntryCopy, found := traceMap[connHandle]
- if found {
- panic(fmt.Sprintf("Adding trace config %v: handle 0x%x already registered (%v).",
- traceConf, connHandle, oldEntryCopy.config))
- }
- traceMap[connHandle] = traceMapEntry{config: traceConf}
- fmt.Printf("Added trace config %v: handle 0x%x.\n", traceConf, connHandle)
- }
- func lookupTraceMapping(connHandle uintptr) (TraceConfig, bool) {
- traceMapLock.Lock()
- defer traceMapLock.Unlock()
- entryCopy, found := traceMap[connHandle]
- return entryCopy.config, found
- }
- func popTraceMapping(connHandle uintptr) (TraceConfig, bool) {
- traceMapLock.Lock()
- defer traceMapLock.Unlock()
- entryCopy, found := traceMap[connHandle]
- if found {
- delete(traceMap, connHandle)
- fmt.Printf("Pop handle 0x%x: deleted trace config %v.\n", connHandle, entryCopy.config)
- }
- return entryCopy.config, found
- }
- 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 {
-
- 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
-
-
-
- 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 {
-
- 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
-
- 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 := sqlite3_create_function(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
- }
- func (c *SQLiteConn) SetTrace(requested *TraceConfig) error {
- connHandle := uintptr(unsafe.Pointer(c.db))
- _, _ = popTraceMapping(connHandle)
- if requested == nil {
-
-
- err := c.setSQLiteTrace(0)
- return err
- }
- reqCopy := *requested
-
-
-
- if reqCopy.EventMask&TraceStmt == 0 {
- reqCopy.WantExpandedSQL = false
- }
- addTraceMapping(connHandle, reqCopy)
-
-
-
- actualEventMask := uint(reqCopy.EventMask | TraceClose)
- err := c.setSQLiteTrace(actualEventMask)
- return err
- }
- func (c *SQLiteConn) setSQLiteTrace(sqliteEventMask uint) error {
- rv := C.sqlite3_trace_v2(c.db,
- C.uint(sqliteEventMask),
- (*[0]byte)(unsafe.Pointer(C.traceCallbackTrampoline)),
- unsafe.Pointer(c.db))
-
- if rv != C.SQLITE_OK {
- return c.lastError()
- }
- return nil
- }
|