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