-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstmt.go
More file actions
254 lines (216 loc) · 5.44 KB
/
stmt.go
File metadata and controls
254 lines (216 loc) · 5.44 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package sqlite
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"reflect"
"time"
)
type Stmt struct {
conn *Conn
stmt uintptr
query string
closed bool
}
func (s *Stmt) Close() error {
if s.closed {
return nil
}
s.conn.stmts.Delete(s.stmt)
rc := sqlite3_finalize(s.stmt)
if rc != SQLITE_OK {
return fmt.Errorf("finalize failed: %s", errorString(rc))
}
s.closed = true
return nil
}
func (s *Stmt) NumInput() int {
return sqlite3_bind_parameter_count(s.stmt)
}
func (s *Stmt) Exec(args []driver.Value) (driver.Result, error) {
namedArgs := make([]driver.NamedValue, len(args))
for i, arg := range args {
namedArgs[i] = driver.NamedValue{
Ordinal: i + 1,
Value: arg,
}
}
return s.ExecContext(context.Background(), namedArgs)
}
func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if s.closed {
return nil, errors.New("statement closed")
}
if err := s.bind(args); err != nil {
return nil, err
}
rc := sqlite3_step(s.stmt)
defer sqlite3_reset(s.stmt)
if rc != SQLITE_DONE && rc != SQLITE_ROW {
return nil, fmt.Errorf("exec failed: %s", getErrorMessage(s.conn.db))
}
return &Result{
lastInsertID: sqlite3_last_insert_rowid(s.conn.db),
rowsAffected: int64(sqlite3_changes(s.conn.db)),
}, nil
}
func (s *Stmt) Query(args []driver.Value) (driver.Rows, error) {
namedArgs := make([]driver.NamedValue, len(args))
for i, arg := range args {
namedArgs[i] = driver.NamedValue{
Ordinal: i + 1,
Value: arg,
}
}
return s.QueryContext(context.Background(), namedArgs)
}
func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if s.closed {
return nil, errors.New("statement closed")
}
if err := s.bind(args); err != nil {
return nil, err
}
columnCount := sqlite3_column_count(s.stmt)
columns := make([]string, columnCount)
for i := 0; i < columnCount; i++ {
namePtr := sqlite3_column_name(s.stmt, i)
columns[i] = goString(namePtr)
}
return &Rows{
stmt: s,
columns: columns,
ctx: ctx,
}, nil
}
func (s *Stmt) bind(args []driver.NamedValue) error {
expectedArgs := s.NumInput()
if len(args) != expectedArgs {
return fmt.Errorf("expected %d arguments, got %d", expectedArgs, len(args))
}
for _, arg := range args {
idx := arg.Ordinal
if idx <= 0 {
continue
}
value := arg.Value
if valuer, ok := value.(driver.Valuer); ok {
var err error
value, err = valuer.Value()
if err != nil {
return fmt.Errorf("valuer error at position %d: %w", idx, err)
}
}
if err := s.bindValue(idx, value); err != nil {
return err
}
}
return nil
}
func (s *Stmt) bindValue(idx int, value any) error {
var rc int
if value == nil {
rc = sqlite3_bind_null(s.stmt, idx)
if rc != SQLITE_OK {
return fmt.Errorf("bind null failed at position %d", idx)
}
return nil
}
switch v := value.(type) {
case int64:
rc = sqlite3_bind_int64(s.stmt, idx, v)
case int:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case int32:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case int16:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case int8:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case uint64:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case uint32:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case uint16:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case uint8:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case uint:
rc = sqlite3_bind_int64(s.stmt, idx, int64(v))
case bool:
if v {
rc = sqlite3_bind_int64(s.stmt, idx, 1)
} else {
rc = sqlite3_bind_int64(s.stmt, idx, 0)
}
case float64:
rc = sqlite3_bind_double(s.stmt, idx, v)
case float32:
rc = sqlite3_bind_double(s.stmt, idx, float64(v))
case string:
strPtr, pinner := cString(v)
defer unpin(pinner)
rc = sqlite3_bind_text(s.stmt, idx, strPtr, len(v), SQLITE_TRANSIENT)
case []byte:
if len(v) == 0 {
rc = sqlite3_bind_blob(s.stmt, idx, 0, 0, SQLITE_STATIC)
} else {
blobPtr, pinner := allocateBytes(v)
defer unpin(pinner)
rc = sqlite3_bind_blob(s.stmt, idx, blobPtr, len(v), SQLITE_TRANSIENT)
}
case time.Time:
strPtr, pinner := cString(v.Format(time.RFC3339Nano))
defer unpin(pinner)
rc = sqlite3_bind_text(s.stmt, idx, strPtr, -1, SQLITE_TRANSIENT)
default:
return fmt.Errorf("unsupported type %T at position %d", value, idx)
}
if rc != SQLITE_OK {
return fmt.Errorf("bind failed at position %d: %s", idx, getErrorMessage(s.conn.db))
}
return nil
}
func (s *Stmt) CheckNamedValue(nv *driver.NamedValue) error {
return checkNamedValue(nv)
}
func checkNamedValue(nv *driver.NamedValue) error {
if nv.Value == nil {
return nil
}
if _, ok := nv.Value.(driver.Valuer); ok {
return nil
}
v := reflect.ValueOf(nv.Value)
switch v.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64, reflect.String:
return nil
case reflect.Slice:
if v.Type().Elem().Kind() == reflect.Uint8 {
return nil
}
}
if _, ok := nv.Value.(time.Time); ok {
return nil
}
return driver.ErrSkip
}
var (
_ driver.Stmt = (*Stmt)(nil)
_ driver.StmtExecContext = (*Stmt)(nil)
_ driver.StmtQueryContext = (*Stmt)(nil)
_ driver.NamedValueChecker = (*Stmt)(nil)
)