forked from kuroneko/gosqlite3
-
Notifications
You must be signed in to change notification settings - Fork 5
/
query_parameter.go
55 lines (49 loc) · 1.53 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
package sqlite3
// #include <sqlite3.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"
import "encoding/gob"
import "unsafe"
type QueryParameter int
func (p QueryParameter) bind_blob(s *Statement, v []byte) error {
if e := Errno(C.gosqlite3_bind_blob(s.cptr, C.int(p), unsafe.Pointer(C.CString(string(v))), C.int(len(v)))); e != OK {
return e
}
return nil
}
func (p QueryParameter) Bind(s *Statement, value interface{}) (e error) {
var rv Errno
switch v := value.(type) {
case nil:
rv = Errno(C.sqlite3_bind_null(s.cptr, C.int(p)))
case int:
rv = Errno(C.sqlite3_bind_int(s.cptr, C.int(p), C.int(v)))
case string:
rv = Errno(C.gosqlite3_bind_text(s.cptr, C.int(p), C.CString(v), C.int(len(v))))
case int64:
rv = Errno(C.sqlite3_bind_int64(s.cptr, C.int(p), C.sqlite3_int64(v)))
case float32:
rv = Errno(C.sqlite3_bind_double(s.cptr, C.int(p), C.double(v)))
case float64:
rv = Errno(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 {
rv = ENCODER
} else {
rawbuffer := string(buffer.Bytes())
rv = Errno(C.gosqlite3_bind_blob(s.cptr, C.int(p), unsafe.Pointer(C.CString(rawbuffer)), C.int(len(rawbuffer))))
}
}
if rv != OK {
e = rv
}
return
}