-
Notifications
You must be signed in to change notification settings - Fork 13
/
fsqlite.go
436 lines (394 loc) · 11.3 KB
/
fsqlite.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
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package sqlite provides access to the SQLite library, version 3.
package fsqlite
/*
#include <sqlite3.h>
#include <stdlib.h>
// These wrappers are necessary because SQLITE_TRANSIENT
// is a pointer constant, and cgo doesn't translate them correctly.
// The definition in sqlite3.h is:
//
// typedef void (*sqlite3_destructor_type)(void*);
// #define SQLITE_STATIC ((sqlite3_destructor_type)0)
// #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
static int my_bind_text(sqlite3_stmt *stmt, int n, char *p, int np) {
return sqlite3_bind_text(stmt, n, p, np, SQLITE_TRANSIENT);
}
static int my_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
}
*/
import "C"
import (
"fmt"
"os"
"reflect"
"strconv"
"unsafe"
"time"
)
type Errno int
func (e Errno) String() string {
s := errText[e]
if s == "" {
return fmt.Sprintf("errno %d", e)
}
return s
}
var (
ErrError os.Error = Errno(1) // /* SQL error or missing database */
ErrInternal os.Error = Errno(2) // /* Internal logic error in SQLite */
ErrPerm os.Error = Errno(3) // /* Access permission denied */
ErrAbort os.Error = Errno(4) // /* Callback routine requested an abort */
ErrBusy os.Error = Errno(5) // /* The database file is locked */
ErrLocked os.Error = Errno(6) // /* A table in the database is locked */
ErrNoMem os.Error = Errno(7) // /* A malloc() failed */
ErrReadOnly os.Error = Errno(8) // /* Attempt to write a readonly database */
ErrInterrupt os.Error = Errno(9) // /* Operation terminated by sqlite3_interrupt()*/
ErrIOErr os.Error = Errno(10) // /* Some kind of disk I/O error occurred */
ErrCorrupt os.Error = Errno(11) // /* The database disk image is malformed */
ErrFull os.Error = Errno(13) // /* Insertion failed because database is full */
ErrCantOpen os.Error = Errno(14) // /* Unable to open the database file */
ErrEmpty os.Error = Errno(16) // /* Database is empty */
ErrSchema os.Error = Errno(17) // /* The database schema changed */
ErrTooBig os.Error = Errno(18) // /* String or BLOB exceeds size limit */
ErrConstraint os.Error = Errno(19) // /* Abort due to constraint violation */
ErrMismatch os.Error = Errno(20) // /* Data type mismatch */
ErrMisuse os.Error = Errno(21) // /* Library used incorrectly */
ErrNolfs os.Error = Errno(22) // /* Uses OS features not supported on host */
ErrAuth os.Error = Errno(23) // /* Authorization denied */
ErrFormat os.Error = Errno(24) // /* Auxiliary database format error */
ErrRange os.Error = Errno(25) // /* 2nd parameter to sqlite3_bind out of range */
ErrNotDB os.Error = Errno(26) // /* File opened that is not a database file */
Row = Errno(100) // /* sqlite3_step() has another row ready */
Done = Errno(101) // /* sqlite3_step() has finished executing */
)
var errText = map[Errno]string{
1: "SQL error or missing database",
2: "Internal logic error in SQLite",
3: "Access permission denied",
4: "Callback routine requested an abort",
5: "The database file is locked",
6: "A table in the database is locked",
7: "A malloc() failed",
8: "Attempt to write a readonly database",
9: "Operation terminated by sqlite3_interrupt()*/",
10: "Some kind of disk I/O error occurred",
11: "The database disk image is malformed",
12: "NOT USED. Table or record not found",
13: "Insertion failed because database is full",
14: "Unable to open the database file",
15: "NOT USED. Database lock protocol error",
16: "Database is empty",
17: "The database schema changed",
18: "String or BLOB exceeds size limit",
19: "Abort due to constraint violation",
20: "Data type mismatch",
21: "Library used incorrectly",
22: "Uses OS features not supported on host",
23: "Authorization denied",
24: "Auxiliary database format error",
25: "2nd parameter to sqlite3_bind out of range",
26: "File opened that is not a database file",
100: "sqlite3_step() has another row ready",
101: "sqlite3_step() has finished executing",
}
func (c *Conn) error(rv C.int) os.Error {
if c == nil || c.db == nil {
return os.NewError("nil sqlite database")
}
if rv == 21 { // misuse
return Errno(rv)
}
return os.NewError(Errno(rv).String() + ": " + C.GoString(C.sqlite3_errmsg(c.db)))
}
type Conn struct {
db *C.sqlite3
}
func Version() string {
p := C.sqlite3_libversion()
return C.GoString(p)
}
func Open(filename string) (*Conn, os.Error) {
if C.sqlite3_threadsafe() == 0 {
return nil, os.NewError("sqlite library was not compiled for thread-safe operation")
}
var db *C.sqlite3
name := C.CString(filename)
defer C.free(unsafe.Pointer(name))
rv := C.sqlite3_open_v2(name, &db,
C.SQLITE_OPEN_FULLMUTEX|
C.SQLITE_OPEN_READWRITE|
C.SQLITE_OPEN_CREATE,
nil)
if rv != 0 {
return nil, Errno(rv)
}
if db == nil {
return nil, os.NewError("sqlite succeeded without returning a database")
}
return &Conn{db}, nil
}
func (c *Conn) Exec(cmd string, args ...interface{}) os.Error {
s, err := c.Prepare(cmd)
if err != nil {
return err
}
defer s.Finalize()
err = s.Exec(args...)
if err != nil {
return err
}
rv := C.sqlite3_step(s.stmt)
if Errno(rv) != Done {
return c.error(rv)
}
return nil
}
type Stmt struct {
c *Conn
stmt *C.sqlite3_stmt
err os.Error
t0 int64
sql string
args string
}
func (c *Conn) Prepare(cmd string) (*Stmt, os.Error) {
if c == nil || c.db == nil {
return nil, os.NewError("nil sqlite database")
}
cmdstr := C.CString(cmd)
defer C.free(unsafe.Pointer(cmdstr))
var stmt *C.sqlite3_stmt
var tail *C.char
rv := C.sqlite3_prepare_v2(c.db, cmdstr, C.int(len(cmd)+1), &stmt, &tail)
if rv != 0 {
return nil, c.error(rv)
}
return &Stmt{c: c, stmt: stmt, sql: cmd, t0: time.Nanoseconds()}, nil
}
func (s *Stmt) Exec(args ...interface{}) os.Error {
s.args = fmt.Sprintf(" %v", []interface{}(args))
rv := C.sqlite3_reset(s.stmt)
if rv != 0 {
return s.c.error(rv)
}
n := int(C.sqlite3_bind_parameter_count(s.stmt))
if n != len(args) {
return os.NewError(fmt.Sprintf("incorrect argument count for Stmt.Exec: have %d want %d", len(args), n))
}
for i, v := range args {
var str string
switch v := v.(type) {
case []byte:
var p *byte
if len(v) > 0 {
p = &v[0]
}
if rv := C.my_bind_blob(s.stmt, C.int(i+1), unsafe.Pointer(p), C.int(len(v))); rv != 0 {
return s.c.error(rv)
}
continue
case bool:
if v {
str = "1"
} else {
str = "0"
}
default:
str = fmt.Sprint(v)
}
cstr := C.CString(str)
rv := C.my_bind_text(s.stmt, C.int(i+1), cstr, C.int(len(str)))
C.free(unsafe.Pointer(cstr))
if rv != 0 {
return s.c.error(rv)
}
}
return nil
}
func (s *Stmt) Exec2(args ...interface{}) os.Error {
s.args = fmt.Sprintf(" %v", []interface{}(args))
rv := C.sqlite3_reset(s.stmt)
if rv != 0 {
return s.c.error(rv)
}
n := int(C.sqlite3_bind_parameter_count(s.stmt))
if n != len(args) {
return os.NewError(fmt.Sprintf("incorrect argument count for Stmt.Exec: have %d want %d", len(args), n))
}
for i, v := range args {
var str string
switch v := v.(type) {
case int:
if rv := C.sqlite3_bind_int(s.stmt, C.int(i+1), C.int(v)); rv != 0 {
return s.c.error(rv)
}
continue
case int64:
if rv := C.sqlite3_bind_int64(s.stmt, C.int(i+1), C.sqlite3_int64(v)); rv != 0 {
return s.c.error(rv)
}
continue
case float64:
if rv := C.sqlite3_bind_double(s.stmt, C.int(i+1), C.double(v)); rv != 0 {
return s.c.error(rv)
}
continue
case []byte:
var p *byte
if len(v) > 0 {
p = &v[0]
}
if rv := C.my_bind_blob(s.stmt, C.int(i+1), unsafe.Pointer(p), C.int(len(v))); rv != 0 {
return s.c.error(rv)
}
continue
case bool:
if v {
str = "1"
} else {
str = "0"
}
default:
str = fmt.Sprint(v)
}
cstr := C.CString(str)
rv := C.my_bind_text(s.stmt, C.int(i+1), cstr, C.int(len(str)))
C.free(unsafe.Pointer(cstr))
if rv != 0 {
return s.c.error(rv)
}
}
return nil
}
func (s *Stmt) Error() os.Error {
return s.err
}
func (s *Stmt) Next() bool {
rv := C.sqlite3_step(s.stmt)
err := Errno(rv)
if err == Row {
return true
}
if err != Done {
s.err = s.c.error(rv)
}
return false
}
func (s *Stmt) Scan(args ...interface{}) os.Error {
n := int(C.sqlite3_column_count(s.stmt))
if n != len(args) {
return os.NewError(fmt.Sprintf("incorrect argument count for Stmt.Scan: have %d want %d", len(args), n))
}
for i, v := range args {
n := C.sqlite3_column_bytes(s.stmt, C.int(i))
p := C.sqlite3_column_blob(s.stmt, C.int(i))
if p == nil && n > 0 {
return os.NewError("got nil blob")
}
var data []byte
if n > 0 {
data = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
}
switch v := v.(type) {
case *[]byte:
*v = data
case *string:
*v = string(data)
case *bool:
*v = string(data) == "1"
case *int:
x, err := strconv.Atoi(string(data))
if err != nil {
return os.NewError("arg " + strconv.Itoa(i) + " as int: " + err.String())
}
*v = x
case *int64:
x, err := strconv.Atoi64(string(data))
if err != nil {
return os.NewError("arg " + strconv.Itoa(i) + " as int64: " + err.String())
}
*v = x
case *float64:
x, err := strconv.Atof64(string(data))
if err != nil {
return os.NewError("arg " + strconv.Itoa(i) + " as float64: " + err.String())
}
*v = x
default:
return os.NewError("unsupported type in Scan: " + reflect.Typeof(v).String())
}
}
return nil
}
func (s *Stmt) Scan2(args ...interface{}) os.Error {
n := int(C.sqlite3_column_count(s.stmt))
if n != len(args) {
return os.NewError(fmt.Sprintf("incorrect argument count for Stmt.Scan: have %d want %d", len(args), n))
}
for i, v := range args {
/*
n := C.sqlite3_column_bytes(s.stmt, C.int(i))
p := C.sqlite3_column_blob(s.stmt, C.int(i))
if p == nil && n > 0 {
return os.NewError("got nil blob")
}
var data []byte
if n > 0 {
data = (*[1 << 30]byte)(unsafe.Pointer(p))[0:n]
}
*/
switch v := v.(type) {
/*
case *[]byte:
*v = data
case *string:
*v = string(data)
case *bool:
*v = string(data) == "1"
case *int:
x, err := strconv.Atoi(string(data))
if err != nil {
return os.NewError("arg " + strconv.Itoa(i) + " as int: " + err.String())
}
*v = x
*/
case *int64:
x := C.sqlite3_column_int64(s.stmt, C.int(i))
*v = int64(x)
case *float64:
x := C.sqlite3_column_double(s.stmt, C.int(i))
*v = float64(x)
default:
return os.NewError("unsupported type in Scan: " + reflect.Typeof(v).String())
}
}
return nil
}
func (s *Stmt) SQL() string {
return s.sql + s.args
}
func (s *Stmt) Nanoseconds() int64 {
return time.Nanoseconds() - s.t0
}
func (s *Stmt) Finalize() os.Error {
rv := C.sqlite3_finalize(s.stmt)
if rv != 0 {
return s.c.error(rv)
}
return nil
}
func (c *Conn) Close() os.Error {
if c == nil || c.db == nil {
return os.NewError("nil sqlite database")
}
rv := C.sqlite3_close(c.db)
if rv != 0 {
return c.error(rv)
}
c.db = nil
return nil
}