persistent-sqlite 2.1.3 → 2.1.4
raw patch · 4 files changed
+70/−2 lines, 4 files
Files
- ChangeLog.md +4/−0
- Database/Sqlite.hs +56/−1
- cbits/config.c +6/−0
- persistent-sqlite.cabal +4/−1
ChangeLog.md view
@@ -1,3 +1,7 @@+## 2.1.4++* Add log support to persistent-sqlite [#381](https://github.com/yesodweb/persistent/pull/381)+ ## 2.1.3 * Added a `Show` instance for `SqliteConf`.
Database/Sqlite.hs view
@@ -10,6 +10,8 @@ SqliteException(..), StepResult(Row, Done),+ Config(ConfigLogFn),+ LogFunction, open, close, prepare,@@ -25,7 +27,10 @@ bind, column, columns,- changes+ changes,+ mkLogFunction,+ freeLogFunction,+ config ) where @@ -473,3 +478,53 @@ changes :: Connection -> IO Int64 changes (Connection _ c) = fmap fromIntegral $ changesC c++-- | Log function callback. Arguments are error code and log message.+--+-- Since 2.1.4+type RawLogFunction = Ptr () -> Int -> CString -> IO ()++foreign import ccall "wrapper"+ mkRawLogFunction :: RawLogFunction -> IO (FunPtr RawLogFunction)++-- |+-- Since 2.1.4+newtype LogFunction = LogFunction (FunPtr RawLogFunction)++-- | Wraps a given function to a 'LogFunction' to be further used with 'ConfigLogFn'.+-- First argument of given function will take error code, second - log message.+-- Returned value should be released with 'freeLogFunction' when no longer required.+mkLogFunction :: (Int -> String -> IO ()) -> IO LogFunction+mkLogFunction fn = fmap LogFunction . mkRawLogFunction $ \_ errCode cmsg -> do+ msg <- peekCString cmsg+ fn errCode msg++-- | Releases a native FunPtr for the 'LogFunction'.+--+-- Since 2.1.4+freeLogFunction :: LogFunction -> IO ()+freeLogFunction (LogFunction fn) = freeHaskellFunPtr fn++-- | Configuration option for SQLite to be used together with the 'config' function.+--+-- Since 2.1.4+data Config+ -- | A function to be used for logging+ = ConfigLogFn LogFunction++foreign import ccall "persistent_sqlite_set_log"+ set_logC :: FunPtr RawLogFunction -> Ptr () -> IO Int++-- | Sets SQLite global configuration parameter. See SQLite documentation for the <https://www.sqlite.org/c3ref/config.html sqlite3_config> function.+-- In short, this must be called prior to any other SQLite function if you want the call to succeed.+--+-- Since 2.1.4+config :: Config -> IO ()+config c = case c of+ ConfigLogFn (LogFunction rawLogFn) -> do+ e <- fmap decodeError $ set_logC rawLogFn nullPtr+ case e of+ ErrorOK -> return ()+ _ -> sqlError Nothing "sqlite3_config" e++
+ cbits/config.c view
@@ -0,0 +1,6 @@+/* This file defines auxiliary functions to help dealing with sqlite vararg stuff. */+#include <sqlite3.h>++int persistent_sqlite_set_log(void (*logFn)(void*, int, const char*), void* arg) {+ return sqlite3_config(SQLITE_CONFIG_LOG, logFn, arg);+}
persistent-sqlite.cabal view
@@ -1,5 +1,5 @@ name: persistent-sqlite-version: 2.1.3+version: 2.1.4 license: MIT license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>@@ -42,7 +42,10 @@ extra-libraries: sqlite3 else c-sources: cbits/sqlite3.c+ include-dirs: cbits cc-options: -fPIC++ c-sources: cbits/config.c if !os(windows) extra-libraries: pthread