-
Notifications
You must be signed in to change notification settings - Fork 24
/
result_column.go
68 lines (59 loc) · 1.77 KB
/
result_column.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
64
65
66
67
68
package sqlite3
// #include <sqlite3.h>
import "C"
import (
"bytes"
"encoding/gob"
"unsafe"
)
const(
INTEGER = 1
FLOAT = 2
TEXT = 3
BLOB = 4
NULL = 5
)
// ResultColumn implements the high level view of the SQLite result column.
type ResultColumn int
func (c ResultColumn) make_buffer(s *Statement, addr interface{}) (buffer string) {
switch addr := addr.(type) {
case *C.uchar:
buffer = C.GoStringN((*C.char)(unsafe.Pointer(addr)), C.int(c.ByteCount(s)))
case unsafe.Pointer:
buffer = C.GoStringN((*C.char)(addr), C.int(c.ByteCount(s)))
}
return
}
// Name returns the name assigned to a particular column in the result set of
// a SELECT statement.
func (c ResultColumn) Name(s *Statement) string {
return C.GoString(C.sqlite3_column_name(s.cptr, C.int(c)))
}
// Type returns the datatype code for the initial data type of the column.
func (c ResultColumn) Type(s *Statement) int {
return int(C.sqlite3_column_type(s.cptr, C.int(c)))
}
// ByteCount returns the number of bytes of the BLOB or string without the
// zero terminators at the end of the string.
func (c ResultColumn) ByteCount(s *Statement) int {
return int(C.sqlite3_column_bytes(s.cptr, C.int(c)))
}
// Value returns the value of the ResultColumn converted to a Go type.
func (c ResultColumn) Value(s *Statement) (value interface{}) {
switch c.Type(s) {
case INTEGER:
value = int64(C.sqlite3_int64(C.sqlite3_column_int64(s.cptr, C.int(c))))
case FLOAT:
value = float64(C.sqlite3_column_double(s.cptr, C.int(c)))
case TEXT:
value = c.make_buffer(s, C.sqlite3_column_text(s.cptr, C.int(c)))
case BLOB:
buffer := c.make_buffer(s, C.sqlite3_column_blob(s.cptr, C.int(c)))
value = gob.NewDecoder(bytes.NewBuffer([]byte(buffer)))
case NULL:
value = nil
default:
panic("unknown column type")
}
return
}