forked from kuroneko/gosqlite3
-
Notifications
You must be signed in to change notification settings - Fork 5
/
statement.go
132 lines (116 loc) · 2.28 KB
/
statement.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package sqlite3
// #include <sqlite3.h>
import "C"
type Statement struct {
db *Database
cptr *C.sqlite3_stmt
timestamp int64
}
func (s *Statement) Parameters() int {
return int(C.sqlite3_bind_parameter_count(s.cptr))
}
func (s *Statement) Columns() int {
return int(C.sqlite3_column_count(s.cptr))
}
func (s *Statement) ColumnName(column int) string {
return ResultColumn(column).Name(s)
}
func (s *Statement) ColumnType(column int) int {
return ResultColumn(column).Type(s)
}
func (s *Statement) Column(column int) (value interface{}) {
return ResultColumn(column).Value(s)
}
func (s *Statement) Row() (values []interface{}) {
for i := 0; i < s.Columns(); i++ {
values = append(values, s.Column(i))
}
return
}
func (s *Statement) Bind(start_column int, values ...interface{}) (e error, index int) {
column := QueryParameter(start_column)
for i, v := range values {
column++
if e = column.Bind(s, v); e != nil {
index = i
return
}
}
return
}
func (s *Statement) BindAll(values ...interface{}) (e error, index int) {
return s.Bind(0, values...)
}
func (s *Statement) SQLSource() (sql string) {
if s.cptr != nil {
sql = C.GoString(C.sqlite3_sql(s.cptr))
}
return
}
func (s *Statement) Finalize() error {
if e := Errno(C.sqlite3_finalize(s.cptr)); e != OK {
return e
}
return nil
}
func (s *Statement) Step(f ...func(*Statement, ...interface{})) (e error) {
r := Errno(C.sqlite3_step(s.cptr))
switch r {
case DONE:
e = nil
case ROW:
if f != nil {
defer func() {
switch x := recover().(type) {
case nil:
e = ROW
case error:
e = x
default:
e = MISUSE
}
}()
r := s.Row()
for _, fn := range f {
fn(s, r...)
}
}
default:
e = r
}
return
}
func (s *Statement) All(f ...func(*Statement, ...interface{})) (c int, e error) {
for {
if e = s.Step(f...); e != nil {
if r, ok := e.(Errno); ok {
switch r {
case ROW:
c++
continue
default:
e = r
break
}
}
} else {
break
}
}
if e == nil {
s.Finalize()
}
return
}
func (s *Statement) Reset() error {
if e := Errno(C.sqlite3_reset(s.cptr)); e != OK {
return e
}
return nil
}
func (s *Statement) ClearBindings() error {
if e := Errno(C.sqlite3_clear_bindings(s.cptr)); e != OK {
return e
}
return nil
}