implement sqlite logging, disable broken ffi.gc

This commit is contained in:
Jeremy Penner 2022-04-20 23:21:04 -04:00
parent 64312e57fa
commit 5914e83437
3 changed files with 11 additions and 6 deletions

View file

@ -5,6 +5,8 @@ return {
SQLITE_TRANSIENT = ffi.cast('void(*)(void*)', -1); SQLITE_TRANSIENT = ffi.cast('void(*)(void*)', -1);
SQLITE_STATIC = ffi.cast('void(*)(void*)', 0); SQLITE_STATIC = ffi.cast('void(*)(void*)', 0);
log_callback_type = 'void(*)(void*,int,const char*)';
-- error codes -- error codes
SQLITE_EMPTY = 16; SQLITE_EMPTY = 16;
SQLITE_CANTOPEN = 14; SQLITE_CANTOPEN = 14;

View file

@ -4,16 +4,18 @@
(local sqlffi (require :diet-sqlite.sqlite3_ffi)) (local sqlffi (require :diet-sqlite.sqlite3_ffi))
(local ffi (require :diet-sqlite.ffi)) (local ffi (require :diet-sqlite.ffi))
(local util (require :lib.util)) (local util (require :lib.util))
(local {: SQLITE_OK : SQLITE_INTEGER : SQLITE_FLOAT : SQLITE_NULL : SQLITE_BLOB : SQLITE_TEXT : SQLITE_TRANSIENT} (require :diet-sqlite.codes)) (local {: SQLITE_OK : SQLITE_INTEGER : SQLITE_FLOAT : SQLITE_NULL : SQLITE_BLOB : SQLITE_TEXT : SQLITE_TRANSIENT
: SQLITE_CONFIG_LOG : log_callback_type} (require :diet-sqlite.codes))
(local sql {:step sqlffi.sqlite3_step (local sql {:step sqlffi.sqlite3_step
:reset sqlffi.sqlite3_reset}) :reset sqlffi.sqlite3_reset
:shutdown sqlffi.sqlite3_shutdown})
(fn sql.open [dbname] (fn sql.open [dbname]
(let [lpdb (ffi.new "sqlite3*[1]") (let [lpdb (ffi.new "sqlite3*[1]")
rc (sqlffi.sqlite3_open dbname lpdb) rc (sqlffi.sqlite3_open dbname lpdb)
db (. lpdb 0)] db (. lpdb 0)]
(when (not= db nil) (ffi.gc db sqlffi.sqlite3_close_v2)) ; (when (not= db nil) (ffi.gc db sqlffi.sqlite3_close_v2))
(values rc (. lpdb 0)))) (values rc (. lpdb 0))))
(fn sql.close [db] (fn sql.close [db]
@ -58,4 +60,8 @@
(if (= rc-expect rc) ... (error (.. "Expected " rc-expect ", got " rc)))) (if (= rc-expect rc) ... (error (.. "Expected " rc-expect ", got " rc))))
(fn sql.assert [...] (sql.assert-rc SQLITE_OK ...)) (fn sql.assert [...] (sql.assert-rc SQLITE_OK ...))
(fn sql.register-logger [logfn]
(let [callback (ffi.cast log_callback_type (fn [pArg iErrCode zMsg] (logfn (ffi.string zMsg) iErrCode)))]
(sql.assert (sqlffi.sqlite3_config SQLITE_CONFIG_LOG callback (ffi.cast "void *" nil)))))
sql sql

View file

@ -862,8 +862,5 @@ struct sqlite3_rtree_geometry {
local Lib = ffi.load(ffi.os == "Windows" and "bin/sqlite3" or "sqlite3") local Lib = ffi.load(ffi.os == "Windows" and "bin/sqlite3" or "sqlite3")
-- initialize the library
Lib.sqlite3_initialize();
return Lib; return Lib;