Permalink
Browse files

Add config option for enabling sql tracing

Helpful feature for debugging performance and functionality of sqlite
queries.

You shouldn't turn this on in production.

fixes #7
  • Loading branch information...
1 parent 2b9302b commit 671cf852f798b405dea384b24358507c6365d6dd @nurpax committed Feb 23, 2013
Showing with 14 additions and 2 deletions.
  1. +7 −0 resources/db/devel.cfg
  2. +7 −2 src/Snap/Snaplet/SqliteSimple.hs
View
@@ -1 +1,8 @@
db = "test.db"
+
+# Enable SQLite query tracing
+# (http://www.sqlite.org/c3ref/profile.html). NEVER turn this on in
+# production, logging SQL queries and their parameters is be a
+# security risk.
+
+enableSqlTracing = false
@@ -95,6 +95,7 @@ import Control.Monad.Trans.Writer
import qualified Data.Configurator as C
import Data.List
import Data.Maybe
+import qualified Data.Text as T
import Database.SQLite.Simple.ToRow
import Database.SQLite.Simple.FromRow
import qualified Database.SQLite.Simple as S
@@ -164,13 +165,17 @@ sqliteInit = makeSnaplet "sqlite-simple" description datadir $ do
db <- logErr "Must specify db filename" $ C.lookup config "db"
return $ db
let ci = fromMaybe (error $ intercalate "\n" errs) mci
-
- conn <- liftIO $ (S.open ci >>= newMVar)
+ tracing <- liftIO $ C.lookupDefault False config "enableSqlTracing"
+ conn <- liftIO $ (S.open ci >>= setTracing tracing >>= newMVar)
return $ Sqlite conn
where
description = "Sqlite abstraction"
+
datadir = Just $ liftM (++"/resources/db") getDataDir
+ setTracing tracing conn = do
+ when tracing (S.setTrace conn (Just (putStrLn . T.unpack)))
+ return conn
------------------------------------------------------------------------------
-- | Convenience function for executing a function that needs a database

0 comments on commit 671cf85

Please sign in to comment.