From 5914e834377cc77e991e27be4575d3c3f386684a Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Wed, 20 Apr 2022 23:21:04 -0400 Subject: [PATCH] implement sqlite logging, disable broken ffi.gc --- diet-sqlite/codes.lua | 2 ++ diet-sqlite/init.fnl | 12 +++++++++--- diet-sqlite/sqlite3_ffi.lua | 3 --- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/diet-sqlite/codes.lua b/diet-sqlite/codes.lua index 4a15cac..2d90f8d 100644 --- a/diet-sqlite/codes.lua +++ b/diet-sqlite/codes.lua @@ -5,6 +5,8 @@ return { SQLITE_TRANSIENT = ffi.cast('void(*)(void*)', -1); SQLITE_STATIC = ffi.cast('void(*)(void*)', 0); +log_callback_type = 'void(*)(void*,int,const char*)'; + -- error codes SQLITE_EMPTY = 16; SQLITE_CANTOPEN = 14; diff --git a/diet-sqlite/init.fnl b/diet-sqlite/init.fnl index 55f1379..6fab1fc 100644 --- a/diet-sqlite/init.fnl +++ b/diet-sqlite/init.fnl @@ -4,16 +4,18 @@ (local sqlffi (require :diet-sqlite.sqlite3_ffi)) (local ffi (require :diet-sqlite.ffi)) (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 - :reset sqlffi.sqlite3_reset}) + :reset sqlffi.sqlite3_reset + :shutdown sqlffi.sqlite3_shutdown}) (fn sql.open [dbname] (let [lpdb (ffi.new "sqlite3*[1]") rc (sqlffi.sqlite3_open dbname lpdb) 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)))) (fn sql.close [db] @@ -58,4 +60,8 @@ (if (= rc-expect rc) ... (error (.. "Expected " rc-expect ", got " rc)))) (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 diff --git a/diet-sqlite/sqlite3_ffi.lua b/diet-sqlite/sqlite3_ffi.lua index dd1cfd7..9acbadb 100644 --- a/diet-sqlite/sqlite3_ffi.lua +++ b/diet-sqlite/sqlite3_ffi.lua @@ -862,8 +862,5 @@ struct sqlite3_rtree_geometry { local Lib = ffi.load(ffi.os == "Windows" and "bin/sqlite3" or "sqlite3") --- initialize the library -Lib.sqlite3_initialize(); - return Lib;