@@ -59,6 +59,13 @@ package sqlite3
59
59
# define USE_PWRITE64 1
60
60
#endif
61
61
62
+ void errorLogTrampoline(void *userPtr, int errCode, const char *msg);
63
+
64
+ static int
65
+ _sqlite3_config_log() {
66
+ return sqlite3_config(SQLITE_CONFIG_LOG, &errorLogTrampoline, NULL);
67
+ }
68
+
62
69
static int
63
70
_sqlite3_open_v2(const char *filename, sqlite3 **ppDb, int flags, const char *zVfs) {
64
71
#ifdef SQLITE_OPEN_URI
@@ -263,14 +270,35 @@ func Version() (libVersion string, libVersionNumber int, sourceID string) {
263
270
return libVersion , libVersionNumber , sourceID
264
271
}
265
272
273
+ // SetErrorLog registers the given callback to be invoked with a message whenever SQLite detects an
274
+ // anomaly. It is good practice to redirect such messages to the application log. See
275
+ // https://sqlite.org/errlog.html.
276
+ // The provided callback function receives an SQLite error object, denoting the broad category of
277
+ // error, and a message string. It must not call any SQLite functions; in fact, the SQLite docs
278
+ // recommend treating the callback function like a signal handler, minimizing the work done in it.
279
+ // SetErrorLog must not be called while any other goroutine is running that might be calling into
280
+ // the SQLite library.
281
+ func SetErrorLog (callback func (err Error , msg string )) error {
282
+ errorLogCallback .Store (callback )
283
+ if rc := C ._sqlite3_config_log (); rc == 0 {
284
+ return nil
285
+ } else {
286
+ return errorFromCode (rc )
287
+ }
288
+ }
289
+
266
290
const (
291
+ // some common return codes
292
+ SQLITE_OK = C .SQLITE_OK
293
+ SQLITE_NOTICE = C .SQLITE_NOTICE
294
+ SQLITE_WARNING = C .SQLITE_WARNING
295
+
267
296
// used by authorizer and pre_update_hook
268
297
SQLITE_DELETE = C .SQLITE_DELETE
269
298
SQLITE_INSERT = C .SQLITE_INSERT
270
299
SQLITE_UPDATE = C .SQLITE_UPDATE
271
300
272
- // used by authorzier - as return value
273
- SQLITE_OK = C .SQLITE_OK
301
+ // used by authorizer as return value, in addition to SQLITE_OK
274
302
SQLITE_IGNORE = C .SQLITE_IGNORE
275
303
SQLITE_DENY = C .SQLITE_DENY
276
304
@@ -845,6 +873,13 @@ func lastError(db *C.sqlite3) error {
845
873
}
846
874
}
847
875
876
+ func errorFromCode (rc C.int ) Error {
877
+ return Error {
878
+ Code : ErrNo (rc & ErrNoMask ),
879
+ ExtendedCode : ErrNoExtended (rc ),
880
+ }
881
+ }
882
+
848
883
// Exec implements Execer.
849
884
func (c * SQLiteConn ) Exec (query string , args []driver.Value ) (driver.Result , error ) {
850
885
list := make ([]driver.NamedValue , len (args ))
0 commit comments