-
Notifications
You must be signed in to change notification settings - Fork 24
/
query_parameter.go
63 lines (60 loc) · 1.93 KB
/
query_parameter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package sqlite3
// #include <sqlite3.h>
// #include <stdlib.h>
// int gosqlite3_bind_text(sqlite3_stmt* s, int p, const char* q, int n) {
// return sqlite3_bind_text(s, p, q, n, SQLITE_TRANSIENT);
// }
// int gosqlite3_bind_blob(sqlite3_stmt* s, int p, const void* q, int n) {
// return sqlite3_bind_blob(s, p, q, n, SQLITE_TRANSIENT);
// }
import "C"
import (
"bytes"
"encoding/gob"
"unsafe"
)
type QueryParameter int
func (p QueryParameter) bind_blob(s *Statement, v []byte) error {
cs := C.CString(string(v))
defer C.free(unsafe.Pointer(cs))
return SQLiteError(C.gosqlite3_bind_blob(s.cptr, C.int(p), unsafe.Pointer(cs), C.int(len(v))))
}
// Bind replaces the literals placed in the SQL statement with the actual
// values supplied to the function.
//
// The following templates may be replaced by the values:
// - ?
// - ?NNN
// - :VVV
// - @VVV
// - $VVV
// In the templates above, NNN represents an integer literal, VVV represents
// an alphanumeric identifier.
func (p QueryParameter) Bind(s *Statement, value interface{}) (e error) {
switch v := value.(type) {
case nil:
e = SQLiteError(C.sqlite3_bind_null(s.cptr, C.int(p)))
case int:
e = SQLiteError(C.sqlite3_bind_int(s.cptr, C.int(p), C.int(v)))
case string:
e = SQLiteError(C.gosqlite3_bind_text(s.cptr, C.int(p), C.CString(v), C.int(len(v))))
case int64:
e = SQLiteError(C.sqlite3_bind_int64(s.cptr, C.int(p), C.sqlite3_int64(v)))
case float32:
e = SQLiteError(C.sqlite3_bind_double(s.cptr, C.int(p), C.double(v)))
case float64:
e = SQLiteError(C.sqlite3_bind_double(s.cptr, C.int(p), C.double(v)))
default:
buffer := new(bytes.Buffer)
encoder := gob.NewEncoder(buffer)
if encoder.Encode(value) != nil {
e = ENCODER
} else {
rawbuffer := string(buffer.Bytes())
cs := C.CString(rawbuffer)
defer C.free(unsafe.Pointer(cs))
e = SQLiteError(C.gosqlite3_bind_blob(s.cptr, C.int(p), unsafe.Pointer(cs), C.int(len(rawbuffer))))
}
}
return
}