callback.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. package sqlite3
  6. // You can't export a Go function to C and have definitions in the C
  7. // preamble in the same file, so we have to have callbackTrampoline in
  8. // its own file. Because we need a separate file anyway, the support
  9. // code for SQLite custom functions is in here.
  10. /*
  11. #ifndef USE_LIBSQLITE3
  12. #include <sqlite3-binding.h>
  13. #else
  14. #include <sqlite3.h>
  15. #endif
  16. #include <stdlib.h>
  17. void _sqlite3_result_text(sqlite3_context* ctx, const char* s);
  18. void _sqlite3_result_blob(sqlite3_context* ctx, const void* b, int l);
  19. */
  20. import "C"
  21. import (
  22. "errors"
  23. "fmt"
  24. "math"
  25. "reflect"
  26. "sync"
  27. "unsafe"
  28. )
  29. //export callbackTrampoline
  30. func callbackTrampoline(ctx *C.sqlite3_context, argc int, argv **C.sqlite3_value) {
  31. args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:argc:argc]
  32. fi := lookupHandle(uintptr(C.sqlite3_user_data(ctx))).(*functionInfo)
  33. fi.Call(ctx, args)
  34. }
  35. //export stepTrampoline
  36. func stepTrampoline(ctx *C.sqlite3_context, argc C.int, argv **C.sqlite3_value) {
  37. args := (*[(math.MaxInt32 - 1) / unsafe.Sizeof((*C.sqlite3_value)(nil))]*C.sqlite3_value)(unsafe.Pointer(argv))[:int(argc):int(argc)]
  38. ai := lookupHandle(uintptr(C.sqlite3_user_data(ctx))).(*aggInfo)
  39. ai.Step(ctx, args)
  40. }
  41. //export doneTrampoline
  42. func doneTrampoline(ctx *C.sqlite3_context) {
  43. handle := uintptr(C.sqlite3_user_data(ctx))
  44. ai := lookupHandle(handle).(*aggInfo)
  45. ai.Done(ctx)
  46. }
  47. //export compareTrampoline
  48. func compareTrampoline(handlePtr uintptr, la C.int, a *C.char, lb C.int, b *C.char) C.int {
  49. cmp := lookupHandle(handlePtr).(func(string, string) int)
  50. return C.int(cmp(C.GoStringN(a, la), C.GoStringN(b, lb)))
  51. }
  52. //export commitHookTrampoline
  53. func commitHookTrampoline(handle uintptr) int {
  54. callback := lookupHandle(handle).(func() int)
  55. return callback()
  56. }
  57. //export rollbackHookTrampoline
  58. func rollbackHookTrampoline(handle uintptr) {
  59. callback := lookupHandle(handle).(func())
  60. callback()
  61. }
  62. //export updateHookTrampoline
  63. func updateHookTrampoline(handle uintptr, op int, db *C.char, table *C.char, rowid int64) {
  64. callback := lookupHandle(handle).(func(int, string, string, int64))
  65. callback(op, C.GoString(db), C.GoString(table), rowid)
  66. }
  67. // Use handles to avoid passing Go pointers to C.
  68. type handleVal struct {
  69. db *SQLiteConn
  70. val interface{}
  71. }
  72. var handleLock sync.Mutex
  73. var handleVals = make(map[uintptr]handleVal)
  74. var handleIndex uintptr = 100
  75. func newHandle(db *SQLiteConn, v interface{}) uintptr {
  76. handleLock.Lock()
  77. defer handleLock.Unlock()
  78. i := handleIndex
  79. handleIndex++
  80. handleVals[i] = handleVal{db, v}
  81. return i
  82. }
  83. func lookupHandle(handle uintptr) interface{} {
  84. handleLock.Lock()
  85. defer handleLock.Unlock()
  86. r, ok := handleVals[handle]
  87. if !ok {
  88. if handle >= 100 && handle < handleIndex {
  89. panic("deleted handle")
  90. } else {
  91. panic("invalid handle")
  92. }
  93. }
  94. return r.val
  95. }
  96. func deleteHandles(db *SQLiteConn) {
  97. handleLock.Lock()
  98. defer handleLock.Unlock()
  99. for handle, val := range handleVals {
  100. if val.db == db {
  101. delete(handleVals, handle)
  102. }
  103. }
  104. }
  105. // This is only here so that tests can refer to it.
  106. type callbackArgRaw C.sqlite3_value
  107. type callbackArgConverter func(*C.sqlite3_value) (reflect.Value, error)
  108. type callbackArgCast struct {
  109. f callbackArgConverter
  110. typ reflect.Type
  111. }
  112. func (c callbackArgCast) Run(v *C.sqlite3_value) (reflect.Value, error) {
  113. val, err := c.f(v)
  114. if err != nil {
  115. return reflect.Value{}, err
  116. }
  117. if !val.Type().ConvertibleTo(c.typ) {
  118. return reflect.Value{}, fmt.Errorf("cannot convert %s to %s", val.Type(), c.typ)
  119. }
  120. return val.Convert(c.typ), nil
  121. }
  122. func callbackArgInt64(v *C.sqlite3_value) (reflect.Value, error) {
  123. if C.sqlite3_value_type(v) != C.SQLITE_INTEGER {
  124. return reflect.Value{}, fmt.Errorf("argument must be an INTEGER")
  125. }
  126. return reflect.ValueOf(int64(C.sqlite3_value_int64(v))), nil
  127. }
  128. func callbackArgBool(v *C.sqlite3_value) (reflect.Value, error) {
  129. if C.sqlite3_value_type(v) != C.SQLITE_INTEGER {
  130. return reflect.Value{}, fmt.Errorf("argument must be an INTEGER")
  131. }
  132. i := int64(C.sqlite3_value_int64(v))
  133. val := false
  134. if i != 0 {
  135. val = true
  136. }
  137. return reflect.ValueOf(val), nil
  138. }
  139. func callbackArgFloat64(v *C.sqlite3_value) (reflect.Value, error) {
  140. if C.sqlite3_value_type(v) != C.SQLITE_FLOAT {
  141. return reflect.Value{}, fmt.Errorf("argument must be a FLOAT")
  142. }
  143. return reflect.ValueOf(float64(C.sqlite3_value_double(v))), nil
  144. }
  145. func callbackArgBytes(v *C.sqlite3_value) (reflect.Value, error) {
  146. switch C.sqlite3_value_type(v) {
  147. case C.SQLITE_BLOB:
  148. l := C.sqlite3_value_bytes(v)
  149. p := C.sqlite3_value_blob(v)
  150. return reflect.ValueOf(C.GoBytes(p, l)), nil
  151. case C.SQLITE_TEXT:
  152. l := C.sqlite3_value_bytes(v)
  153. c := unsafe.Pointer(C.sqlite3_value_text(v))
  154. return reflect.ValueOf(C.GoBytes(c, l)), nil
  155. default:
  156. return reflect.Value{}, fmt.Errorf("argument must be BLOB or TEXT")
  157. }
  158. }
  159. func callbackArgString(v *C.sqlite3_value) (reflect.Value, error) {
  160. switch C.sqlite3_value_type(v) {
  161. case C.SQLITE_BLOB:
  162. l := C.sqlite3_value_bytes(v)
  163. p := (*C.char)(C.sqlite3_value_blob(v))
  164. return reflect.ValueOf(C.GoStringN(p, l)), nil
  165. case C.SQLITE_TEXT:
  166. c := (*C.char)(unsafe.Pointer(C.sqlite3_value_text(v)))
  167. return reflect.ValueOf(C.GoString(c)), nil
  168. default:
  169. return reflect.Value{}, fmt.Errorf("argument must be BLOB or TEXT")
  170. }
  171. }
  172. func callbackArgGeneric(v *C.sqlite3_value) (reflect.Value, error) {
  173. switch C.sqlite3_value_type(v) {
  174. case C.SQLITE_INTEGER:
  175. return callbackArgInt64(v)
  176. case C.SQLITE_FLOAT:
  177. return callbackArgFloat64(v)
  178. case C.SQLITE_TEXT:
  179. return callbackArgString(v)
  180. case C.SQLITE_BLOB:
  181. return callbackArgBytes(v)
  182. case C.SQLITE_NULL:
  183. // Interpret NULL as a nil byte slice.
  184. var ret []byte
  185. return reflect.ValueOf(ret), nil
  186. default:
  187. panic("unreachable")
  188. }
  189. }
  190. func callbackArg(typ reflect.Type) (callbackArgConverter, error) {
  191. switch typ.Kind() {
  192. case reflect.Interface:
  193. if typ.NumMethod() != 0 {
  194. return nil, errors.New("the only supported interface type is interface{}")
  195. }
  196. return callbackArgGeneric, nil
  197. case reflect.Slice:
  198. if typ.Elem().Kind() != reflect.Uint8 {
  199. return nil, errors.New("the only supported slice type is []byte")
  200. }
  201. return callbackArgBytes, nil
  202. case reflect.String:
  203. return callbackArgString, nil
  204. case reflect.Bool:
  205. return callbackArgBool, nil
  206. case reflect.Int64:
  207. return callbackArgInt64, nil
  208. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint:
  209. c := callbackArgCast{callbackArgInt64, typ}
  210. return c.Run, nil
  211. case reflect.Float64:
  212. return callbackArgFloat64, nil
  213. case reflect.Float32:
  214. c := callbackArgCast{callbackArgFloat64, typ}
  215. return c.Run, nil
  216. default:
  217. return nil, fmt.Errorf("don't know how to convert to %s", typ)
  218. }
  219. }
  220. func callbackConvertArgs(argv []*C.sqlite3_value, converters []callbackArgConverter, variadic callbackArgConverter) ([]reflect.Value, error) {
  221. var args []reflect.Value
  222. if len(argv) < len(converters) {
  223. return nil, fmt.Errorf("function requires at least %d arguments", len(converters))
  224. }
  225. for i, arg := range argv[:len(converters)] {
  226. v, err := converters[i](arg)
  227. if err != nil {
  228. return nil, err
  229. }
  230. args = append(args, v)
  231. }
  232. if variadic != nil {
  233. for _, arg := range argv[len(converters):] {
  234. v, err := variadic(arg)
  235. if err != nil {
  236. return nil, err
  237. }
  238. args = append(args, v)
  239. }
  240. }
  241. return args, nil
  242. }
  243. type callbackRetConverter func(*C.sqlite3_context, reflect.Value) error
  244. func callbackRetInteger(ctx *C.sqlite3_context, v reflect.Value) error {
  245. switch v.Type().Kind() {
  246. case reflect.Int64:
  247. case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint:
  248. v = v.Convert(reflect.TypeOf(int64(0)))
  249. case reflect.Bool:
  250. b := v.Interface().(bool)
  251. if b {
  252. v = reflect.ValueOf(int64(1))
  253. } else {
  254. v = reflect.ValueOf(int64(0))
  255. }
  256. default:
  257. return fmt.Errorf("cannot convert %s to INTEGER", v.Type())
  258. }
  259. C.sqlite3_result_int64(ctx, C.sqlite3_int64(v.Interface().(int64)))
  260. return nil
  261. }
  262. func callbackRetFloat(ctx *C.sqlite3_context, v reflect.Value) error {
  263. switch v.Type().Kind() {
  264. case reflect.Float64:
  265. case reflect.Float32:
  266. v = v.Convert(reflect.TypeOf(float64(0)))
  267. default:
  268. return fmt.Errorf("cannot convert %s to FLOAT", v.Type())
  269. }
  270. C.sqlite3_result_double(ctx, C.double(v.Interface().(float64)))
  271. return nil
  272. }
  273. func callbackRetBlob(ctx *C.sqlite3_context, v reflect.Value) error {
  274. if v.Type().Kind() != reflect.Slice || v.Type().Elem().Kind() != reflect.Uint8 {
  275. return fmt.Errorf("cannot convert %s to BLOB", v.Type())
  276. }
  277. i := v.Interface()
  278. if i == nil || len(i.([]byte)) == 0 {
  279. C.sqlite3_result_null(ctx)
  280. } else {
  281. bs := i.([]byte)
  282. C._sqlite3_result_blob(ctx, unsafe.Pointer(&bs[0]), C.int(len(bs)))
  283. }
  284. return nil
  285. }
  286. func callbackRetText(ctx *C.sqlite3_context, v reflect.Value) error {
  287. if v.Type().Kind() != reflect.String {
  288. return fmt.Errorf("cannot convert %s to TEXT", v.Type())
  289. }
  290. C._sqlite3_result_text(ctx, C.CString(v.Interface().(string)))
  291. return nil
  292. }
  293. func callbackRetNil(ctx *C.sqlite3_context, v reflect.Value) error {
  294. return nil
  295. }
  296. func callbackRet(typ reflect.Type) (callbackRetConverter, error) {
  297. switch typ.Kind() {
  298. case reflect.Interface:
  299. errorInterface := reflect.TypeOf((*error)(nil)).Elem()
  300. if typ.Implements(errorInterface) {
  301. return callbackRetNil, nil
  302. }
  303. fallthrough
  304. case reflect.Slice:
  305. if typ.Elem().Kind() != reflect.Uint8 {
  306. return nil, errors.New("the only supported slice type is []byte")
  307. }
  308. return callbackRetBlob, nil
  309. case reflect.String:
  310. return callbackRetText, nil
  311. case reflect.Bool, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Int, reflect.Uint:
  312. return callbackRetInteger, nil
  313. case reflect.Float32, reflect.Float64:
  314. return callbackRetFloat, nil
  315. default:
  316. return nil, fmt.Errorf("don't know how to convert to %s", typ)
  317. }
  318. }
  319. func callbackError(ctx *C.sqlite3_context, err error) {
  320. cstr := C.CString(err.Error())
  321. defer C.free(unsafe.Pointer(cstr))
  322. C.sqlite3_result_error(ctx, cstr, -1)
  323. }
  324. // Test support code. Tests are not allowed to import "C", so we can't
  325. // declare any functions that use C.sqlite3_value.
  326. func callbackSyntheticForTests(v reflect.Value, err error) callbackArgConverter {
  327. return func(*C.sqlite3_value) (reflect.Value, error) {
  328. return v, err
  329. }
  330. }