Skip to content

Commit 19b6a89

Browse files
committed
added debug-queries flag
1 parent f78aae9 commit 19b6a89

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

postgresql-simple-bind.cabal

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ flag older-call-syntax
2828
manual: True
2929
default: True
3030

31+
flag debug-queries
32+
description: Turn on output of executed queries.
33+
manual: False
34+
default: False
35+
3136
source-repository head
3237
type: git
3338
location: https://github.com/zohl/postgresql-simple-bind.git
@@ -65,6 +70,8 @@ library
6570
if flag(older-call-syntax)
6671
cpp-options: -DOlderCallSyntax
6772

73+
if flag(debug-queries)
74+
cpp-options: -DDebugQueries
6875

6976
test-suite tests
7077
type: exitcode-stdio-1.0

src/Database/PostgreSQL/Simple/Bind/Implementation.hs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ module Database.PostgreSQL.Simple.Bind.Implementation (
3333
) where
3434

3535
import Control.Exception (throw)
36+
37+
#ifdef DebugQueries
38+
import Debug.Trace (traceId, traceShowId)
39+
#endif
40+
3641
import Data.List (intersperse)
3742
import Data.Maybe (catMaybes, maybeToList)
3843
import Data.Text (Text)
@@ -59,8 +64,17 @@ bindFunction opt s = parsePGFunction s >>= mkFunction opt
5964
mkFunction :: PostgresBindOptions -> PGFunction -> Q [Dec]
6065
mkFunction opt f = sequence $ (($ f) . ($ opt)) <$> [mkFunctionT, mkFunctionE]
6166

67+
#ifdef DebugQueries
68+
data Argument = forall a . (Show a, ToField a) => MandatoryArg String a
69+
| forall a . (Show a, ToField a) => OptionalArg String (Maybe a)
70+
71+
instance Show Argument where
72+
show (MandatoryArg name value) = "mandatory: " ++ name ++ " => " ++ show value
73+
show (OptionalArg name value) = "optional: " ++ name ++ " => " ++ show value
74+
#else
6275
data Argument = forall a . (ToField a) => MandatoryArg String a
6376
| forall a . (ToField a) => OptionalArg String (Maybe a)
77+
#endif
6478

6579
instance ToField Argument where
6680
toField (MandatoryArg _ x) = toField x
@@ -168,12 +182,20 @@ mkFunctionT opt@(PostgresBindOptions {..}) f@(PGFunction _schema fname args ret)
168182
return $ SigD (mkName $ pboFunctionName f) $ ForallT vars context clause
169183

170184

185+
traceIdWrapE :: Exp -> Exp
186+
#ifdef DebugQueries
187+
traceIdWrapE q = (VarE 'traceId) `AppE` q
188+
#else
189+
traceIdWrapE = id
190+
#endif
191+
171192
-- | Example:
172193
-- (PGFunction "public" "foo"
173194
-- [PGArgument "x" "varchar" True, PGArgument "y" "bigint" False] (PGSingle "varchar"))
174195
-- args -> { Query $ BS.pack $ concat ["select public.foo (", (formatArguments args), ")"] }
175196
mkSqlQuery :: PostgresBindOptions -> PGFunction -> Maybe Name -> Exp
176-
mkSqlQuery opt (PGFunction schema fname _args ret) argsName = toQuery $ AppE (VarE 'concat) $ ListE [
197+
mkSqlQuery opt (PGFunction schema fname _args ret) argsName =
198+
toQuery . traceIdWrapE . AppE (VarE 'concat) . ListE $ [
177199
mkStrLit $ concat [prefix opt, " ", functionName, "("]
178200
, maybe (mkStrLit "") (\args -> (VarE 'formatArguments) `AppE` (VarE args)) argsName
179201
, mkStrLit ")"] where
@@ -207,6 +229,13 @@ unwrapE _ (PGSingle _) q = (VarE 'fmap) `AppE` (VarE 'unwrapRow) `AppE` q
207229
unwrapE opt (PGSetOf tname) q = unwrapE' (pboSetOfReturnType opt tname) q
208230
unwrapE _ (PGTable _) q = unwrapE' AsRow q
209231

232+
traceShowIdWrapE :: Exp -> Exp
233+
#ifdef DebugQueries
234+
traceShowIdWrapE q = (VarE 'traceShowId) `AppE` q
235+
#else
236+
traceShowIdWrapE = id
237+
#endif
238+
210239
-- | Example: (PGFunction "public" "foo"
211240
-- [PGArgument "x" "varchar" True, PGArgument "y" "bigint" False] (PGSingle "varchar")) -> {
212241
-- foo conn x1 x2 = query conn
@@ -233,9 +262,8 @@ mkFunctionE opt@(PostgresBindOptions {..}) f@(PGFunction _schema _fname args ret
233262
VarE $ maybe 'query_ (const 'query) argsName
234263
, VarE connName
235264
, mkSqlQuery opt f argsName
236-
] ++ (const argsExpr <$> maybeToList argsName)
265+
] ++ (const (traceShowIdWrapE argsExpr) <$> maybeToList argsName)
237266

238267
let decl = (\name -> ValD (VarP name) (NormalB argsExpr) []) <$> maybeToList argsName
239268
return $ FunD funcName [Clause funcArgs funcBody decl]
240269

241-

0 commit comments

Comments
 (0)