@@ -78,7 +78,6 @@ _sqlite3_bind_blob(sqlite3_stmt *stmt, int n, void *p, int np) {
78
78
return sqlite3_bind_blob(stmt, n, p, np, SQLITE_TRANSIENT);
79
79
}
80
80
81
- #include <stdio.h>
82
81
#include <stdint.h>
83
82
84
83
static int
@@ -141,7 +140,7 @@ _sqlite3_prepare_v2_internal(sqlite3 *db, const char *zSql, int nBytes, sqlite3_
141
140
142
141
// Our own implementation of ctype.h's isspace (for simplicity and to avoid
143
142
// whatever locale shenanigans are involved with the Libc's isspace).
144
- static int _sqlite3_isspace(unsigned char c) {
143
+ static inline int _sqlite3_isspace(unsigned char c) {
145
144
return c == ' ' || c - '\t' < 5;
146
145
}
147
146
@@ -209,6 +208,7 @@ static int _sqlite3_prepare_v2(sqlite3 *db, const char *zSql, int nBytes, sqlite
209
208
// _sqlite3_exec_no_args executes all of the statements in zSql. None of the
210
209
// statements are allowed to have positional arguments.
211
210
int _sqlite3_exec_no_args(sqlite3 *db, const char *zSql, int nBytes, int64_t *rowid, int64_t *changes) {
211
+ const char *end = zSql + nBytes;
212
212
while (*zSql && nBytes > 0) {
213
213
sqlite3_stmt *stmt;
214
214
const char *tail;
@@ -226,12 +226,25 @@ int _sqlite3_exec_no_args(sqlite3 *db, const char *zSql, int nBytes, int64_t *ro
226
226
*changes = sqlite3_changes64(db);
227
227
*rowid = sqlite3_last_insert_rowid(db);
228
228
229
- sqlite3_finalize(stmt);
229
+ int rv2 = sqlite3_finalize(stmt);
230
+ if (rv == SQLITE_OK) {
231
+ rv = rv2;
232
+ }
233
+
230
234
if (rv != SQLITE_OK && rv != SQLITE_DONE) {
235
+ // Handle statements that are empty or consist only of a comment.
236
+ if (rv == SQLITE_MISUSE && sqlite3_errcode(db) == 0) {
237
+ rv = SQLITE_OK;
238
+ }
231
239
return rv;
232
240
}
233
241
234
- nBytes -= tail - zSql;
242
+ // Trim leading space to handle statements with trailing whitespace.
243
+ // This can save us an additional call to sqlite3_prepare_v2.
244
+ while (tail < end && _sqlite3_isspace(*tail)) {
245
+ tail++;
246
+ }
247
+ nBytes -= (tail - zSql);
235
248
zSql = tail;
236
249
}
237
250
return SQLITE_OK;
@@ -386,6 +399,21 @@ static go_sqlite3_text_column _sqlite3_column_blob(sqlite3_stmt *stmt, int idx)
386
399
}
387
400
return r;
388
401
}
402
+
403
+ typedef struct {
404
+ const char *msg;
405
+ int code;
406
+ } go_sqlite3_db_error;
407
+
408
+ static go_sqlite3_db_error _sqlite3_db_error(sqlite3 *db) {
409
+ if (db) {
410
+ return (go_sqlite3_db_error){
411
+ .msg = sqlite3_errmsg(db),
412
+ .code = sqlite3_extended_errcode(db)
413
+ };
414
+ }
415
+ return (go_sqlite3_db_error){ 0 };
416
+ }
389
417
*/
390
418
import "C"
391
419
import (
@@ -744,7 +772,7 @@ func (c *SQLiteConn) RegisterCollation(name string, cmp func(string, string) int
744
772
defer C .free (unsafe .Pointer (cname ))
745
773
rv := C .sqlite3_create_collation (c .db , cname , C .SQLITE_UTF8 , handle , (* [0 ]byte )(unsafe .Pointer (C .compareTrampoline )))
746
774
if rv != C .SQLITE_OK {
747
- return c .lastError ()
775
+ return c .lastError (int ( rv ) )
748
776
}
749
777
return nil
750
778
}
@@ -886,7 +914,7 @@ func (c *SQLiteConn) RegisterFunc(name string, impl any, pure bool) error {
886
914
}
887
915
rv := sqlite3CreateFunction (c .db , cname , C .int (numArgs ), C .int (opts ), newHandle (c , & fi ), C .callbackTrampoline , nil , nil )
888
916
if rv != C .SQLITE_OK {
889
- return c .lastError ()
917
+ return c .lastError (int ( rv ) )
890
918
}
891
919
return nil
892
920
}
@@ -1015,7 +1043,7 @@ func (c *SQLiteConn) RegisterAggregator(name string, impl any, pure bool) error
1015
1043
}
1016
1044
rv := sqlite3CreateFunction (c .db , cname , C .int (stepNArgs ), C .int (opts ), newHandle (c , & ai ), nil , C .stepTrampoline , C .doneTrampoline )
1017
1045
if rv != C .SQLITE_OK {
1018
- return c .lastError ()
1046
+ return c .lastError (int ( rv ) )
1019
1047
}
1020
1048
return nil
1021
1049
}
@@ -1027,32 +1055,38 @@ func (c *SQLiteConn) AutoCommit() bool {
1027
1055
return int (C .sqlite3_get_autocommit (c .db )) != 0
1028
1056
}
1029
1057
1030
- func (c * SQLiteConn ) lastError () error {
1031
- return lastError (c .db )
1058
+ func (c * SQLiteConn ) lastError (rv int ) error {
1059
+ return lastError (c .db , rv )
1032
1060
}
1033
1061
1034
- // Note: may be called with db == nil
1035
- func lastError (db * C.sqlite3 ) error {
1036
- rv := C .sqlite3_errcode (db ) // returns SQLITE_NOMEM if db == nil
1037
- if rv == C .SQLITE_OK {
1062
+ func lastError (db * C.sqlite3 , rv int ) error {
1063
+ if rv == SQLITE_OK {
1038
1064
return nil
1039
1065
}
1040
- extrv := C .sqlite3_extended_errcode (db ) // returns SQLITE_NOMEM if db == nil
1041
- errStr := C .GoString (C .sqlite3_errmsg (db )) // returns "out of memory" if db == nil
1066
+ extrv := rv
1067
+ // Convert the extended result code to a basic result code.
1068
+ rv &= ErrNoMask
1042
1069
1043
1070
// https://www.sqlite.org/c3ref/system_errno.html
1044
1071
// sqlite3_system_errno is only meaningful if the error code was SQLITE_CANTOPEN,
1045
1072
// or it was SQLITE_IOERR and the extended code was not SQLITE_IOERR_NOMEM
1046
1073
var systemErrno syscall.Errno
1047
- if rv == C .SQLITE_CANTOPEN || (rv == C .SQLITE_IOERR && extrv != C .SQLITE_IOERR_NOMEM ) {
1074
+ if db != nil && (rv == C .SQLITE_CANTOPEN ||
1075
+ (rv == C .SQLITE_IOERR && extrv != C .SQLITE_IOERR_NOMEM )) {
1048
1076
systemErrno = syscall .Errno (C .sqlite3_system_errno (db ))
1049
1077
}
1050
1078
1079
+ var msg string
1080
+ if db != nil {
1081
+ msg = C .GoString (C .sqlite3_errmsg (db ))
1082
+ } else {
1083
+ msg = errorString (extrv )
1084
+ }
1051
1085
return Error {
1052
1086
Code : ErrNo (rv ),
1053
1087
ExtendedCode : ErrNoExtended (extrv ),
1054
1088
SystemErrno : systemErrno ,
1055
- err : errStr ,
1089
+ err : msg ,
1056
1090
}
1057
1091
}
1058
1092
@@ -1091,7 +1125,7 @@ func (c *SQLiteConn) execArgs(ctx context.Context, query string, args []driver.N
1091
1125
rv := C ._sqlite3_prepare_v2 (c .db , (* C .char )(unsafe .Pointer (stringData (query ))),
1092
1126
C .int (len (query )), & s .s , & sz )
1093
1127
if rv != C .SQLITE_OK {
1094
- return nil , c .lastError ()
1128
+ return nil , c .lastError (int ( rv ) )
1095
1129
}
1096
1130
query = strings .TrimSpace (query [sz :])
1097
1131
@@ -1143,7 +1177,7 @@ func (c *SQLiteConn) execNoArgsSync(query string) (_ driver.Result, err error) {
1143
1177
rv := C ._sqlite3_exec_no_args (c .db , (* C .char )(unsafe .Pointer (stringData (query ))),
1144
1178
C .int (len (query )), & rowid , & changes )
1145
1179
if rv != C .SQLITE_OK {
1146
- err = c .lastError ()
1180
+ err = c .lastError (int ( rv ) )
1147
1181
}
1148
1182
return & SQLiteResult {id : int64 (rowid ), changes : int64 (changes )}, err
1149
1183
}
@@ -1197,8 +1231,6 @@ func (c *SQLiteConn) Query(query string, args []driver.Value) (driver.Rows, erro
1197
1231
return c .query (context .Background (), query , list )
1198
1232
}
1199
1233
1200
- var closedRows = & SQLiteRows {s : & SQLiteStmt {closed : true }}
1201
-
1202
1234
func (c * SQLiteConn ) query (ctx context.Context , query string , args []driver.NamedValue ) (driver.Rows , error ) {
1203
1235
s := SQLiteStmt {c : c , cls : true }
1204
1236
p := stringData (query )
@@ -1208,14 +1240,14 @@ func (c *SQLiteConn) query(ctx context.Context, query string, args []driver.Name
1208
1240
if rv == C .GO_SQLITE_MULTIPLE_QUERIES {
1209
1241
return nil , errors .New ("query contains multiple SQL statements" )
1210
1242
}
1211
- return nil , c .lastError ()
1243
+ return nil , c .lastError (int ( rv ) )
1212
1244
}
1213
1245
1214
1246
// The sqlite3_stmt will be nil if the SQL was valid but did not
1215
1247
// contain a query. For now we're supporting this for the sake of
1216
1248
// backwards compatibility, but that may change in the future.
1217
1249
if s .s == nil {
1218
- return closedRows , nil
1250
+ return & SQLiteRows { s : & SQLiteStmt { closed : true }} , nil
1219
1251
}
1220
1252
1221
1253
na := int (paramCount )
@@ -1744,7 +1776,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
1744
1776
if rv != 0 {
1745
1777
// Save off the error _before_ closing the database.
1746
1778
// This is safe even if db is nil.
1747
- err := lastError (db )
1779
+ err := lastError (db , int ( rv ) )
1748
1780
if db != nil {
1749
1781
C .sqlite3_close_v2 (db )
1750
1782
}
@@ -1753,13 +1785,18 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
1753
1785
if db == nil {
1754
1786
return nil , errors .New ("sqlite succeeded without returning a database" )
1755
1787
}
1788
+ rv = C .sqlite3_extended_result_codes (db , 1 )
1789
+ if rv != SQLITE_OK {
1790
+ C .sqlite3_close_v2 (db )
1791
+ return nil , lastError (db , int (rv ))
1792
+ }
1756
1793
1757
1794
exec := func (s string ) error {
1758
1795
cs := C .CString (s )
1759
1796
rv := C .sqlite3_exec (db , cs , nil , nil , nil )
1760
1797
C .free (unsafe .Pointer (cs ))
1761
1798
if rv != C .SQLITE_OK {
1762
- return lastError (db )
1799
+ return lastError (db , int ( rv ) )
1763
1800
}
1764
1801
return nil
1765
1802
}
@@ -2066,7 +2103,7 @@ func (c *SQLiteConn) Close() (err error) {
2066
2103
runtime .SetFinalizer (c , nil )
2067
2104
rv := C .sqlite3_close_v2 (c .db )
2068
2105
if rv != C .SQLITE_OK {
2069
- err = c . lastError ()
2106
+ err = lastError (nil , int ( rv ) )
2070
2107
}
2071
2108
deleteHandles (c )
2072
2109
c .db = nil
@@ -2092,7 +2129,7 @@ func (c *SQLiteConn) prepare(ctx context.Context, query string) (driver.Stmt, er
2092
2129
var s * C.sqlite3_stmt
2093
2130
rv := C ._sqlite3_prepare_v2_internal (c .db , (* C .char )(unsafe .Pointer (p )), C .int (len (query )), & s , nil )
2094
2131
if rv != C .SQLITE_OK {
2095
- return nil , c .lastError ()
2132
+ return nil , c .lastError (int ( rv ) )
2096
2133
}
2097
2134
ss := & SQLiteStmt {c : c , s : s }
2098
2135
runtime .SetFinalizer (ss , (* SQLiteStmt ).Close )
@@ -2160,7 +2197,7 @@ func (c *SQLiteConn) SetFileControlInt(dbName string, op int, arg int) error {
2160
2197
cArg := C .int (arg )
2161
2198
rv := C .sqlite3_file_control (c .db , cDBName , C .int (op ), unsafe .Pointer (& cArg ))
2162
2199
if rv != C .SQLITE_OK {
2163
- return c .lastError ()
2200
+ return c .lastError (int ( rv ) )
2164
2201
}
2165
2202
return nil
2166
2203
}
@@ -2183,7 +2220,7 @@ func (s *SQLiteStmt) Close() error {
2183
2220
}
2184
2221
rv := C .sqlite3_finalize (stmt )
2185
2222
if rv != C .SQLITE_OK {
2186
- return conn .lastError ()
2223
+ return conn .lastError (int ( rv ) )
2187
2224
}
2188
2225
return nil
2189
2226
}
@@ -2215,7 +2252,7 @@ func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
2215
2252
if s .reset {
2216
2253
rv := C .sqlite3_reset (s .s )
2217
2254
if rv != C .SQLITE_ROW && rv != C .SQLITE_OK && rv != C .SQLITE_DONE {
2218
- return s .c .lastError ()
2255
+ return s .c .lastError (int ( rv ) )
2219
2256
}
2220
2257
} else {
2221
2258
s .reset = true
@@ -2259,7 +2296,7 @@ func (s *SQLiteStmt) bind(args []driver.NamedValue) error {
2259
2296
rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (& b [0 ])), C .int (len (b )))
2260
2297
}
2261
2298
if rv != C .SQLITE_OK {
2262
- return s .c .lastError ()
2299
+ return s .c .lastError (int ( rv ) )
2263
2300
}
2264
2301
}
2265
2302
return nil
@@ -2327,7 +2364,7 @@ func (s *SQLiteStmt) bindIndices(args []driver.NamedValue) error {
2327
2364
rv = C ._sqlite3_bind_text (s .s , n , (* C .char )(unsafe .Pointer (& b [0 ])), C .int (len (b )))
2328
2365
}
2329
2366
if rv != C .SQLITE_OK {
2330
- return s .c .lastError ()
2367
+ return s .c .lastError (int ( rv ) )
2331
2368
}
2332
2369
}
2333
2370
}
@@ -2437,7 +2474,7 @@ func (s *SQLiteStmt) execSync(args []driver.NamedValue) (driver.Result, error) {
2437
2474
var rowid , changes C.longlong
2438
2475
rv := C ._sqlite3_step_row_internal (s .s , & rowid , & changes )
2439
2476
if rv != C .SQLITE_ROW && rv != C .SQLITE_OK && rv != C .SQLITE_DONE {
2440
- err := s .c .lastError ()
2477
+ err := s .c .lastError (int ( rv ) )
2441
2478
C .sqlite3_reset (s .s )
2442
2479
C .sqlite3_clear_bindings (s .s )
2443
2480
return nil , err
@@ -2473,8 +2510,8 @@ func (rc *SQLiteRows) Close() error {
2473
2510
}
2474
2511
rv := C .sqlite3_reset (s .s )
2475
2512
if rv != C .SQLITE_OK {
2476
- s .mu .Unlock ()
2477
- return s .c .lastError ()
2513
+ rc . s .mu .Unlock ()
2514
+ return rc . s .c .lastError (int ( rv ) )
2478
2515
}
2479
2516
s .mu .Unlock ()
2480
2517
return nil
@@ -2573,7 +2610,7 @@ func (rc *SQLiteRows) nextSyncLocked(dest []driver.Value) error {
2573
2610
if rv != C .SQLITE_ROW {
2574
2611
rv = C .sqlite3_reset (rc .s .s )
2575
2612
if rv != C .SQLITE_OK {
2576
- return rc .s .c .lastError ()
2613
+ return rc .s .c .lastError (int ( rv ) )
2577
2614
}
2578
2615
return nil
2579
2616
}
0 commit comments