-
Notifications
You must be signed in to change notification settings - Fork 5
/
stmt.go
534 lines (473 loc) · 15.8 KB
/
stmt.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
package sqlite
// #include <stdlib.h>
// #include <string.h>
// #include <sqlite3ext.h>
// #include "unlock_notify.h"
// #include "bridge.h"
//
// // destructor function defined in ./context.go
// extern void pointer_destructor_hook_tramp(void*);
//
// // Use a helper function here to avoid the cgo pointer detection
// // logic treating SQLITE_TRANSIENT as a Go pointer.
// static int transient_bind_blob(sqlite3_stmt* stmt, int col, unsigned char* p, int n) {
// return _sqlite3_bind_blob(stmt, col, p, n, SQLITE_TRANSIENT);
// }
import "C"
import (
"bytes"
"github.com/mattn/go-pointer"
"reflect"
"runtime"
"unsafe"
)
// Stmt is an SQLite3 prepared statement.
//
// A Stmt is attached to a particular Conn
// (and that Conn can only be used by a single goroutine).
//
// When a Stmt is no longer needed it should be cleaned up
// by calling the Finalize method.
type Stmt struct {
conn *Conn
stmt *C.sqlite3_stmt
query string
bindNames map[string]int
colNames map[string]int
bindErr error
lastHasRow bool // last bool returned by Step
}
// Finalize deletes a prepared statement.
//
// Be sure to always call Finalize when done with
// a statement created using Prepare.
//
// Do not call Finalize on a prepared statement that
// you intend to prepare again in the future.
//
// see: https://www.sqlite.org/c3ref/finalize.html
func (stmt *Stmt) Finalize() error {
var res = C._sqlite3_finalize(stmt.stmt)
stmt.conn = nil
return errorIfNotOk(res)
}
// Reset resets a prepared statement so it can be executed again.
//
// Note that any parameter values bound to the statement are retained.
// To clear bound values, call ClearBindings.
//
// see: https://www.sqlite.org/c3ref/reset.html
func (stmt *Stmt) Reset() error {
stmt.lastHasRow = false
var res C.int
for {
res = C._sqlite3_reset(stmt.stmt)
if res != C.SQLITE_LOCKED_SHAREDCACHE {
break
}
// An SQLITE_LOCKED_SHAREDCACHE error has been seen from sqlite3_reset
// in the wild, but so far has eluded exact test case replication.
var err = ErrorCode(C._wait_for_unlock_notify(stmt.conn.db, stmt.conn.unlockNote))
if !err.ok() {
return err
}
}
return errorIfNotOk(res)
}
// ClearBindings clears all bound parameter values on a statement.
//
// see: https://www.sqlite.org/c3ref/clear_bindings.html
func (stmt *Stmt) ClearBindings() error {
return errorIfNotOk(C._sqlite3_clear_bindings(stmt.stmt))
}
// Step moves through the statement cursor using sqlite3_step.
//
// If a row of data is available, rowReturned is reported as true.
// If the statement has reached the end of the available data then
// rowReturned is false. Thus the status codes SQLITE_ROW and
// SQLITE_DONE are reported by the rowReturned bool, and all other
// non-OK status codes are reported as an error.
//
// If an error value is returned, then the statement has been reset.
//
// https://www.sqlite.org/c3ref/step.html
//
// Shared cache
//
// If Shared Cache mode is enabled, this Step method uses sqlite3_unlock_notify
// to handle any SQLITE_LOCKED errors.
//
// Without the shared cache, SQLite will block for
// several seconds while trying to acquire the write lock.
// With the shared cache, it returns SQLITE_LOCKED immediately
// if the write lock is held by another connection in this process.
// Dealing with this correctly makes for an unpleasant programming
// experience, so this package does it automatically by blocking
// Step until the write lock is relinquished.
//
// This means Step can block for a very long time.
//
// For far more details, see: http://www.sqlite.org/unlock_notify.html
func (stmt *Stmt) Step() (rowReturned bool, err error) {
if err = stmt.bindErr; err != nil {
stmt.bindErr = nil
_ = stmt.Reset()
return false, err
}
if rowReturned, err = stmt.step(); err != nil {
C._sqlite3_reset(stmt.stmt)
}
stmt.lastHasRow = rowReturned
return rowReturned, err
}
func (stmt *Stmt) step() (bool, error) {
for {
switch res := C._sqlite3_step(stmt.stmt); uint8(res) { // reduce to non-extended error code
case C.SQLITE_LOCKED:
if res != C.SQLITE_LOCKED_SHAREDCACHE {
// don't call wait_for_unlock_notify as it might deadlock, see:
// see: https://github.com/crawshaw/sqlite/issues/6
return false, ErrorCode(res)
}
if res = C._wait_for_unlock_notify(stmt.conn.db, stmt.conn.unlockNote); res != C.SQLITE_OK {
return false, ErrorCode(res)
}
C._sqlite3_reset(stmt.stmt)
// loop
case C.SQLITE_ROW:
return true, nil
case C.SQLITE_DONE:
return false, nil
default:
return false, ErrorCode(res)
}
}
}
func (stmt *Stmt) handleBindErr(res C.int) {
if err := ErrorCode(res); !err.ok() && stmt.bindErr == nil {
stmt.bindErr = err
}
}
func (stmt *Stmt) findBindName(param string) int {
pos := stmt.bindNames[param]
if pos == 0 && stmt.bindErr == nil {
stmt.bindErr = SQLITE_ERROR
}
return pos
}
// DataCount returns the number of columns in the current row of the result
// set of prepared statement.
//
// see: https://sqlite.org/c3ref/data_count.html
func (stmt *Stmt) DataCount() int {
return int(C._sqlite3_data_count(stmt.stmt))
}
// ColumnCount returns the number of columns in the result set returned by the
// prepared statement.
//
// see: https://sqlite.org/c3ref/column_count.html
func (stmt *Stmt) ColumnCount() int {
return int(C._sqlite3_column_count(stmt.stmt))
}
// ColumnName returns the name assigned to a particular column in the result
// set of a SELECT statement.
//
// see: https://sqlite.org/c3ref/column_name.html
func (stmt *Stmt) ColumnName(col int) string {
return C.GoString((*C.char)(unsafe.Pointer(C._sqlite3_column_name(stmt.stmt, C.int(col)))))
}
// BindName returns the name assigned to a particular parameter in the query.
//
// see: https://www.sqlite.org/c3ref/bind_parameter_name.html
func (stmt *Stmt) BindName(param int) string {
return C.GoString((*C.char)(unsafe.Pointer(C._sqlite3_bind_parameter_name(stmt.stmt, C.int(param)))))
}
// BindParamCount reports the number of parameters in stmt.
//
// see: https://www.sqlite.org/c3ref/bind_parameter_count.html
func (stmt *Stmt) BindParamCount() int {
if stmt.stmt == nil {
return 0
}
return int(C._sqlite3_bind_parameter_count(stmt.stmt))
}
// BindInt64 binds value to a numbered stmt parameter.
func (stmt *Stmt) BindInt64(param int, value int64) {
if stmt.stmt == nil {
return
}
res := C._sqlite3_bind_int64(stmt.stmt, C.int(param), C.sqlite3_int64(value))
stmt.handleBindErr(res)
}
// BindBool binds value (as an integer 0 or 1) to a numbered stmt parameter.
func (stmt *Stmt) BindBool(param int, value bool) {
if stmt.stmt == nil {
return
}
v := 0
if value {
v = 1
}
res := C._sqlite3_bind_int64(stmt.stmt, C.int(param), C.sqlite3_int64(v))
stmt.handleBindErr(res)
}
// BindBytes binds value to a numbered stmt parameter.
// In-memory copies of value are made using this interface.
func (stmt *Stmt) BindBytes(param int, value []byte) {
if stmt.stmt == nil {
return
}
var v *C.uchar
if len(value) != 0 {
v = (*C.uchar)(unsafe.Pointer(&value[0]))
}
res := C.transient_bind_blob(stmt.stmt, C.int(param), v, C.int(len(value)))
runtime.KeepAlive(value)
stmt.handleBindErr(res)
}
var emptyCstr = C.CString("")
// BindText binds value to a numbered stmt parameter.
func (stmt *Stmt) BindText(param int, value string) {
if stmt.stmt == nil {
return
}
var v *C.char
var free *[0]byte
if len(value) == 0 {
v = emptyCstr
} else {
v = C.CString(value)
free = (*[0]byte)(C.free)
}
res := C._sqlite3_bind_text(stmt.stmt, C.int(param), v, C.int(len(value)), free)
stmt.handleBindErr(res)
}
// BindFloat binds value to a numbered stmt parameter.
func (stmt *Stmt) BindFloat(param int, value float64) {
if stmt.stmt == nil {
return
}
res := C._sqlite3_bind_double(stmt.stmt, C.int(param), C.double(value))
stmt.handleBindErr(res)
}
// BindNull binds an SQL NULL value to a numbered stmt parameter.
func (stmt *Stmt) BindNull(param int) {
if stmt.stmt == nil {
return
}
res := C._sqlite3_bind_null(stmt.stmt, C.int(param))
stmt.handleBindErr(res)
}
// BindNull binds a blob of zeros of length len to a numbered stmt parameter.
func (stmt *Stmt) BindZeroBlob(param int, len int64) {
if stmt.stmt == nil {
return
}
res := C._sqlite3_bind_zeroblob64(stmt.stmt, C.int(param), C.sqlite3_uint64(len))
stmt.handleBindErr(res)
}
// BindValue binds an sqlite_value object at given index
func (stmt *Stmt) BindValue(param int, value Value) {
if stmt.stmt == nil {
return
}
res := C._sqlite3_bind_value(stmt.stmt, C.int(param), value.ptr)
stmt.handleBindErr(res)
}
// BindPointer binds any arbitrary Go value with the parameter.
// The value can later be retrieved by custom functions or callbacks, casted back into a Go type,
// and used in golang's environment.
func (stmt *Stmt) BindPointer(param int, arg interface{}) {
if stmt.stmt == nil {
return
}
ptr := pointer.Save(arg)
res := C._sqlite3_bind_pointer(stmt.stmt, C.int(param), ptr, pointerType, (*[0]byte)(C.pointer_destructor_hook_tramp))
stmt.handleBindErr(res)
}
// SetInt64 binds an int64 to a parameter using a column name.
func (stmt *Stmt) SetInt64(param string, value int64) {
stmt.BindInt64(stmt.findBindName(param), value)
}
// SetBool binds a value (as a 0 or 1) to a parameter using a column name.
func (stmt *Stmt) SetBool(param string, value bool) {
stmt.BindBool(stmt.findBindName(param), value)
}
// SetBytes binds bytes to a parameter using a column name.
// An invalid parameter name will cause the call to Step to return an error.
func (stmt *Stmt) SetBytes(param string, value []byte) {
stmt.BindBytes(stmt.findBindName(param), value)
}
// SetText binds text to a parameter using a column name.
// An invalid parameter name will cause the call to Step to return an error.
func (stmt *Stmt) SetText(param string, value string) {
stmt.BindText(stmt.findBindName(param), value)
}
// SetFloat binds a float64 to a parameter using a column name.
// An invalid parameter name will cause the call to Step to return an error.
func (stmt *Stmt) SetFloat(param string, value float64) {
stmt.BindFloat(stmt.findBindName(param), value)
}
// SetNull binds a null to a parameter using a column name.
// An invalid parameter name will cause the call to Step to return an error.
func (stmt *Stmt) SetNull(param string) {
stmt.BindNull(stmt.findBindName(param))
}
// SetZeroBlob binds a zero blob of length len to a parameter using a column name.
// An invalid parameter name will cause the call to Step to return an error.
func (stmt *Stmt) SetZeroBlob(param string, len int64) {
stmt.BindZeroBlob(stmt.findBindName(param), len)
}
// SetValue binds an sqlite3_value to a parameter using a column name.
// An invalid parameter name will cause the call to Step to return an error.
func (stmt *Stmt) SetValue(param string, value Value) {
stmt.BindValue(stmt.findBindName(param), value)
}
// SetPointer binds a golang value to a parameter using a column name.
func (stmt *Stmt) SetPointer(param string, arg interface{}) {
stmt.BindPointer(stmt.findBindName(param), arg)
}
// ColumnInt returns a query result value as an int.
//
// Note: this method calls sqlite3_column_int64 and then converts the
// resulting 64-bits to an int.
func (stmt *Stmt) ColumnInt(col int) int {
return int(stmt.ColumnInt64(col))
}
// ColumnInt32 returns a query result value as an int32.
func (stmt *Stmt) ColumnInt32(col int) int32 {
return int32(C._sqlite3_column_int(stmt.stmt, C.int(col)))
}
// ColumnInt64 returns a query result value as an int64.
func (stmt *Stmt) ColumnInt64(col int) int64 {
return int64(C._sqlite3_column_int64(stmt.stmt, C.int(col)))
}
// ColumnBytes reads a query result into buf.
// It reports the number of bytes read.
func (stmt *Stmt) ColumnBytes(col int, buf []byte) int {
return copy(buf, stmt.columnBytes(col))
}
// ColumnReader creates a byte reader for a query result column.
//
// The reader directly references C-managed memory that stops
// being valid as soon as the statement row resets.
func (stmt *Stmt) ColumnReader(col int) *bytes.Reader {
// Load the C memory directly into the Reader.
// There is no exported method that lets it escape.
return bytes.NewReader(stmt.columnBytes(col))
}
func (stmt *Stmt) columnBytes(col int) []byte {
p := C._sqlite3_column_blob(stmt.stmt, C.int(col))
if p == nil {
return nil
}
n := stmt.ColumnLen(col)
var slice = *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: uintptr(unsafe.Pointer(p)), Len: n, Cap: n}))
return slice
}
// ColumnType returns the datatype code for the initial data
// type of the result column.
func (stmt *Stmt) ColumnType(col int) ColumnType {
return ColumnType(C._sqlite3_column_type(stmt.stmt, C.int(col)))
}
// ColumnText returns a query result as a string.
func (stmt *Stmt) ColumnText(col int) string {
n := stmt.ColumnLen(col)
return C.GoStringN((*C.char)(unsafe.Pointer(C._sqlite3_column_text(stmt.stmt, C.int(col)))), C.int(n))
}
// ColumnFloat returns a query result as a float64.
func (stmt *Stmt) ColumnFloat(col int) float64 {
return float64(C._sqlite3_column_double(stmt.stmt, C.int(col)))
}
// ColumnValue returns a query result as an sqlite_value.
func (stmt *Stmt) ColumnValue(col int) Value {
return Value{ptr: C._sqlite3_column_value(stmt.stmt, C.int(col))}
}
// ColumnLen returns the number of bytes in a query result.
func (stmt *Stmt) ColumnLen(col int) int {
return int(C._sqlite3_column_bytes(stmt.stmt, C.int(col)))
}
func (stmt *Stmt) ColumnDatabaseName(col int) string {
return C.GoString((*C.char)(unsafe.Pointer(C._sqlite3_column_database_name(stmt.stmt, C.int(col)))))
}
func (stmt *Stmt) ColumnTableName(col int) string {
return C.GoString((*C.char)(unsafe.Pointer(C._sqlite3_column_table_name(stmt.stmt, C.int(col)))))
}
func (stmt *Stmt) ColumnOriginName(col int) string {
return C.GoString((*C.char)(unsafe.Pointer(C._sqlite3_column_origin_name(stmt.stmt, C.int(col)))))
}
// ColumnIndex returns the index of the column with the given name.
//
// If there is no column with the given name ColumnIndex returns -1.
func (stmt *Stmt) ColumnIndex(colName string) int {
col, found := stmt.colNames[colName]
if !found {
return -1
}
return col
}
// GetInt64 returns a query result value for colName as an int64.
func (stmt *Stmt) GetInt64(colName string) int64 {
col, found := stmt.colNames[colName]
if !found {
return 0
}
return stmt.ColumnInt64(col)
}
// GetBytes reads a query result for colName into buf.
// It reports the number of bytes read.
func (stmt *Stmt) GetBytes(colName string, buf []byte) int {
col, found := stmt.colNames[colName]
if !found {
return 0
}
return stmt.ColumnBytes(col, buf)
}
// GetReader creates a byte reader for colName.
//
// The reader directly references C-managed memory that stops
// being valid as soon as the statement row resets.
func (stmt *Stmt) GetReader(colName string) *bytes.Reader {
col, found := stmt.colNames[colName]
if !found {
return bytes.NewReader(nil)
}
return stmt.ColumnReader(col)
}
// GetText returns a query result value for colName as a string.
func (stmt *Stmt) GetText(colName string) string {
col, found := stmt.colNames[colName]
if !found {
return ""
}
return stmt.ColumnText(col)
}
// GetFloat returns a query result value for colName as a float64.
func (stmt *Stmt) GetFloat(colName string) float64 {
col, found := stmt.colNames[colName]
if !found {
return 0
}
return stmt.ColumnFloat(col)
}
// GetValue returns a query result value for colName as an sqlite_value.
func (stmt *Stmt) GetValue(colName string) Value {
col, found := stmt.colNames[colName]
if !found {
return Value{}
}
return stmt.ColumnValue(col)
}
// GetLen returns the number of bytes in a query result for colName.
func (stmt *Stmt) GetLen(colName string) int {
col, found := stmt.colNames[colName]
if !found {
return 0
}
return stmt.ColumnLen(col)
}
// Readonly returns true if this statement is readonly and makes no direct changes to the content of the database file.
// See: https://www.sqlite.org/c3ref/stmt_readonly.html
func (stmt *Stmt) Readonly() bool {
return C.int(C._sqlite3_stmt_readonly(stmt.stmt)) != 0
}