@@ -33,6 +33,11 @@ module Database.PostgreSQL.Simple.Bind.Implementation (
3333 ) where
3434
3535import Control.Exception (throw )
36+
37+ #ifdef DebugQueries
38+ import Debug.Trace (traceId , traceShowId )
39+ #endif
40+
3641import Data.List (intersperse )
3742import Data.Maybe (catMaybes , maybeToList )
3843import Data.Text (Text )
@@ -59,8 +64,17 @@ bindFunction opt s = parsePGFunction s >>= mkFunction opt
5964mkFunction :: PostgresBindOptions -> PGFunction -> Q [Dec ]
6065mkFunction 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
6275data Argument = forall a . (ToField a ) => MandatoryArg String a
6376 | forall a . (ToField a ) => OptionalArg String (Maybe a )
77+ #endif
6478
6579instance 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), ")"] }
175196mkSqlQuery :: 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
207229unwrapE opt (PGSetOf tname) q = unwrapE' (pboSetOfReturnType opt tname) q
208230unwrapE _ (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