callback.go 9.3 KB

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