Skip to content

Commit 7a760cc

Browse files
authored
Merge pull request #2 from obsidiansystems/aa/iquery
Add iquery, iexecute, iexecute_ convenience functions
2 parents 920ad26 + 497583a commit 7a760cc

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/Database/PostgreSQL/Simple/SqlQQ/Interpolated.hs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
{-# LANGUAGE TemplateHaskell #-}
22

33
-- | Interpolated SQL queries
4-
module Database.PostgreSQL.Simple.SqlQQ.Interpolated (isql, quoteInterpolatedSql) where
4+
module Database.PostgreSQL.Simple.SqlQQ.Interpolated
5+
( isql
6+
, quoteInterpolatedSql
7+
, iquery
8+
, iexecute
9+
, iexecute_
10+
) where
511

612
import Language.Haskell.TH (Exp, Q, appE, listE, sigE, tupE, varE)
713
import Language.Haskell.TH.Quote (QuasiQuoter (..))
814
import Database.PostgreSQL.Simple.ToField (Action, toField)
915
import Database.PostgreSQL.Simple.SqlQQ (sql)
1016
import Text.Parsec (ParseError)
17+
import Database.PostgreSQL.Simple
1118

1219
import Database.PostgreSQL.Simple.SqlQQ.Interpolated.Parser (StringPart (..), parseInterpolated)
1320

@@ -66,8 +73,21 @@ quoteInterpolatedSql :: String -> Q Exp
6673
quoteInterpolatedSql s = either (handleError s) applySql (parseInterpolated s)
6774

6875
handleError :: String -> ParseError -> Q Exp
69-
handleError expStr parseError = error $
70-
"Failed to parse interpolated expression in string: "
71-
++ expStr
72-
++ "\n"
73-
++ show parseError
76+
handleError expStr parseError = error $ mconcat
77+
[ "Failed to parse interpolated expression in string: "
78+
, expStr
79+
, "\n"
80+
, show parseError
81+
]
82+
83+
-- | Invokes 'query' with arguments provided by 'isql'
84+
iquery :: QuasiQuoter
85+
iquery = isql { quoteExp = appE [| uncurry query |] . quoteInterpolatedSql }
86+
87+
-- | Invokes 'execute' with arguments provided by 'isql'
88+
iexecute :: QuasiQuoter
89+
iexecute = isql { quoteExp = appE [| uncurry execute |] . quoteInterpolatedSql }
90+
91+
-- | Invokes 'execute_' with arguments provided by 'isql'
92+
iexecute_ :: QuasiQuoter
93+
iexecute_ = isql { quoteExp = appE [| uncurry execute_ |] . quoteInterpolatedSql }

0 commit comments

Comments
 (0)