sqlite3_context.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /*
  7. #ifndef USE_LIBSQLITE3
  8. #include <sqlite3-binding.h>
  9. #else
  10. #include <sqlite3.h>
  11. #endif
  12. #include <stdlib.h>
  13. // These wrappers are necessary because SQLITE_TRANSIENT
  14. // is a pointer constant, and cgo doesn't translate them correctly.
  15. static inline void my_result_text(sqlite3_context *ctx, char *p, int np) {
  16. sqlite3_result_text(ctx, p, np, SQLITE_TRANSIENT);
  17. }
  18. static inline void my_result_blob(sqlite3_context *ctx, void *p, int np) {
  19. sqlite3_result_blob(ctx, p, np, SQLITE_TRANSIENT);
  20. }
  21. */
  22. import "C"
  23. import (
  24. "math"
  25. "reflect"
  26. "unsafe"
  27. )
  28. const i64 = unsafe.Sizeof(int(0)) > 4
  29. // SQLiteContext behave sqlite3_context
  30. type SQLiteContext C.sqlite3_context
  31. // ResultBool sets the result of an SQL function.
  32. func (c *SQLiteContext) ResultBool(b bool) {
  33. if b {
  34. c.ResultInt(1)
  35. } else {
  36. c.ResultInt(0)
  37. }
  38. }
  39. // ResultBlob sets the result of an SQL function.
  40. // See: sqlite3_result_blob, http://sqlite.org/c3ref/result_blob.html
  41. func (c *SQLiteContext) ResultBlob(b []byte) {
  42. if i64 && len(b) > math.MaxInt32 {
  43. C.sqlite3_result_error_toobig((*C.sqlite3_context)(c))
  44. return
  45. }
  46. var p *byte
  47. if len(b) > 0 {
  48. p = &b[0]
  49. }
  50. C.my_result_blob((*C.sqlite3_context)(c), unsafe.Pointer(p), C.int(len(b)))
  51. }
  52. // ResultDouble sets the result of an SQL function.
  53. // See: sqlite3_result_double, http://sqlite.org/c3ref/result_blob.html
  54. func (c *SQLiteContext) ResultDouble(d float64) {
  55. C.sqlite3_result_double((*C.sqlite3_context)(c), C.double(d))
  56. }
  57. // ResultInt sets the result of an SQL function.
  58. // See: sqlite3_result_int, http://sqlite.org/c3ref/result_blob.html
  59. func (c *SQLiteContext) ResultInt(i int) {
  60. if i64 && (i > math.MaxInt32 || i < math.MinInt32) {
  61. C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i))
  62. } else {
  63. C.sqlite3_result_int((*C.sqlite3_context)(c), C.int(i))
  64. }
  65. }
  66. // ResultInt64 sets the result of an SQL function.
  67. // See: sqlite3_result_int64, http://sqlite.org/c3ref/result_blob.html
  68. func (c *SQLiteContext) ResultInt64(i int64) {
  69. C.sqlite3_result_int64((*C.sqlite3_context)(c), C.sqlite3_int64(i))
  70. }
  71. // ResultNull sets the result of an SQL function.
  72. // See: sqlite3_result_null, http://sqlite.org/c3ref/result_blob.html
  73. func (c *SQLiteContext) ResultNull() {
  74. C.sqlite3_result_null((*C.sqlite3_context)(c))
  75. }
  76. // ResultText sets the result of an SQL function.
  77. // See: sqlite3_result_text, http://sqlite.org/c3ref/result_blob.html
  78. func (c *SQLiteContext) ResultText(s string) {
  79. h := (*reflect.StringHeader)(unsafe.Pointer(&s))
  80. cs, l := (*C.char)(unsafe.Pointer(h.Data)), C.int(h.Len)
  81. C.my_result_text((*C.sqlite3_context)(c), cs, l)
  82. }
  83. // ResultZeroblob sets the result of an SQL function.
  84. // See: sqlite3_result_zeroblob, http://sqlite.org/c3ref/result_blob.html
  85. func (c *SQLiteContext) ResultZeroblob(n int) {
  86. C.sqlite3_result_zeroblob((*C.sqlite3_context)(c), C.int(n))
  87. }