Skip to content

Commit acc3cb0

Browse files
committed
moved older-call-syntax flag to options
1 parent ac5f44b commit acc3cb0

4 files changed

Lines changed: 32 additions & 35 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
### Added
55
- Changelog.
66
- Explicit type casts options.
7+
### Changed
8+
- Flag `older-call-syntax` is an option now.
79

810
## [0.3.0] - 2016-10-29
911
### Added

postgresql-simple-bind.cabal

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ flag db-tests
2424
manual: False
2525
default: False
2626

27-
flag older-call-syntax
28-
description: Turn on usage of (:=) in queries instead of (=>). This
29-
is necessary for PostgreSQL < 9.5.
30-
manual: True
31-
default: True
32-
3327
flag debug-queries
3428
description: Turn on output of executed queries.
3529
manual: False
@@ -69,9 +63,6 @@ library
6963
else
7064
ghc-options: -O2 -Wall
7165

72-
if flag(older-call-syntax)
73-
cpp-options: -DOlderCallSyntax
74-
7566
if flag(debug-queries)
7667
cpp-options: -DDebugQueries
7768

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
{-# LANGUAGE ScopedTypeVariables #-}
1414
{-# LANGUAGE FlexibleInstances #-}
1515

16+
1617
{-|
1718
Module: Database.PostgreSQL.Simple.Bind.Common
1819
Copyright: (c) 2016 Al Zohali
@@ -48,8 +49,11 @@ data PostgresBindOptions = PostgresBindOptions {
4849
-- ^ Which columns in returned tables can be null.
4950
, pboSetOfReturnType :: String -> ReturnType
5051
-- ^ How to process type in "setof" clause.
51-
, pboExplicitCasts :: Bool
52+
, pboExplicitCasts :: Bool
5253
-- ^ Whether to add explicit type casts to arguments.
54+
, pboOlderCallSyntax :: Bool
55+
-- ^ Whether to use old-style call syntax (:=) instead of (=>). This is
56+
-- necessary for PostgreSQL < 9.5.
5357
}
5458

5559
instance Default PostgresBindOptions where
@@ -58,6 +62,7 @@ instance Default PostgresBindOptions where
5862
, pboIsNullable = \_fname _column -> False
5963
, pboSetOfReturnType = \_tname -> AsField
6064
, pboExplicitCasts = True
65+
, pboOlderCallSyntax = True
6166
}
6267

6368
-- | Remove 'Only' constructor.

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

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ import Database.PostgreSQL.Simple.FromRow (FromRow(..))
5252
import Database.PostgreSQL.Simple.Types (Query(..))
5353
import GHC.TypeLits (Symbol)
5454
import Language.Haskell.TH.Syntax (Q, Dec(..), Exp(..), Type(..), Clause(..), Body(..), Pat(..))
55-
import Language.Haskell.TH.Syntax (Name, mkName, newName, Lit(..), TyLit(..), TyVarBndr(..), lift)
55+
import Language.Haskell.TH.Syntax (Name, mkName, newName, Lit(..), TyLit(..), TyVarBndr(..))
5656
import qualified Data.ByteString.Char8 as BS
5757

5858
-- | Mapping from PostgreSQL types to Haskell types.
@@ -83,28 +83,28 @@ instance ToField Argument where
8383
toField (OptionalArg name _atype Nothing) = throw . DefaultValueNotFound $ name
8484

8585

86-
callExpression :: String
87-
#ifdef OlderCallSyntax
88-
callExpression = " := "
89-
#else
90-
callExpression = " => "
91-
#endif
86+
untypedPlaceholder, typedPlaceholder :: String -> String
87+
untypedPlaceholder = const "?"
88+
typedPlaceholder atype = "(?)::" ++ atype
89+
9290

93-
formatArgument :: Bool -> Argument -> Maybe String
94-
formatArgument cast = format where
91+
formatArgument :: String -> (String -> String) -> Argument -> Maybe String
92+
formatArgument callSyntax placeholder = format where
9593
format = \case
9694
(MandatoryArg _name atype _value) -> Just $ placeholder atype
9795

9896
(OptionalArg name atype (Just _value)) -> Just . concat $ [
99-
name , callExpression, placeholder atype]
97+
name, callSyntax, placeholder atype]
10098

10199
(OptionalArg _name _atype Nothing) -> Nothing
102100

103-
placeholder atype = if cast then ("(?)::" ++ atype) else "?"
104101

102+
formatArguments :: String -> (String -> String) -> [Argument] -> String
103+
formatArguments callSyntax placeholder = concat
104+
. (intersperse ",")
105+
. catMaybes
106+
. (map $ formatArgument callSyntax placeholder)
105107

106-
formatArguments :: Bool -> [Argument] -> String
107-
formatArguments cast = concat . (intersperse ",") . catMaybes . (map $ formatArgument cast)
108108

109109
filterArguments :: [Argument] -> [Argument]
110110
filterArguments = filter isPresented where
@@ -204,17 +204,17 @@ traceIdWrapE = id
204204
-- (PGFunction "public" "foo"
205205
-- [PGArgument "x" "varchar" True, PGArgument "y" "bigint" False] (PGSingle "varchar"))
206206
-- args -> { Query $ BS.pack $ concat ["select public.foo (", (formatArguments args), ")"] }
207-
mkSqlQuery :: PostgresBindOptions -> PGFunction -> Maybe Name -> Q Exp
207+
mkSqlQuery :: PostgresBindOptions -> PGFunction -> Maybe Name -> Exp
208208
mkSqlQuery opt (PGFunction schema fname _args ret) argsName =
209-
lift (pboExplicitCasts opt) >>= \cast ->
210-
return . toQuery . traceIdWrapE . AppE (VarE 'concat) . ListE $ [
211-
mkStrLit $ concat [prefix opt, " ", functionName, "("]
212-
, maybe (mkStrLit "") (\args -> foldl1 AppE [
213-
VarE 'formatArguments
214-
, cast
215-
, VarE args
216-
]) argsName
217-
, mkStrLit ")"] where
209+
toQuery . traceIdWrapE . AppE (VarE 'concat) . ListE $ [
210+
mkStrLit $ concat [prefix opt, " ", functionName, "("]
211+
, maybe (mkStrLit "") (\args -> foldl1 AppE [
212+
VarE 'formatArguments
213+
, mkStrLit $ if (pboOlderCallSyntax opt) then " := " else " => "
214+
, VarE $ if (pboExplicitCasts opt) then 'typedPlaceholder else 'untypedPlaceholder
215+
, VarE args
216+
]) argsName
217+
, mkStrLit ")"] where
218218

219219
prefix (PostgresBindOptions {..}) = case ret of
220220
PGTable _ -> mkSelect AsRow
@@ -274,11 +274,10 @@ mkFunctionE opt@(PostgresBindOptions {..}) f@(PGFunction _schema _fname args ret
274274

275275
let funcName = mkName $ pboFunctionName f
276276
let funcArgs = (VarP connName):(map VarP names)
277-
sqlQuery <- mkSqlQuery opt f argsName
278277
let funcBody = NormalB $ unwrapE opt ret $ foldl1 AppE $ [
279278
VarE $ maybe 'query_ (const 'query) argsName
280279
, VarE connName
281-
, sqlQuery
280+
, mkSqlQuery opt f argsName
282281
] ++ (const (traceShowIdWrapE argsExpr) <$> maybeToList argsName)
283282

284283
let decl = (\name -> ValD (VarP name) (NormalB argsExpr) []) <$> maybeToList argsName

0 commit comments

Comments
 (0)