44
55{- | Introduces named parameters for @postgresql-simple@ library.
66It uses @?@ question mark symbol as the indicator of the named parameter which
7- is replaced with the standard syntax with question marks. Check out the example
8- of usage:
7+ is replaced with the standard syntax with question marks.
8+
9+ Check out the example of usage:
910
1011@
11- queryNamed [sql|
12+ ' queryNamed' [sql|
1213 SELECT *
1314 FROM users
1415 WHERE foo = ?foo
1516 AND bar = ?bar
1617 AND baz = ?foo
17- |] [ "foo" =? "fooBar"
18- , "bar" =? "barVar"
18+ |] [ "foo" '=?' "fooBar"
19+ , "bar" '=?' "barVar"
1920 ]
2021@
2122-}
@@ -28,6 +29,7 @@ module PgNamed
2829
2930 -- * Errors
3031 , PgNamedError (.. )
32+ , WithNamedError
3133
3234 -- * Functions to deal with named parameters
3335 , extractNames
@@ -71,15 +73,15 @@ data NamedParam = NamedParam
7173data PgNamedError
7274 -- | Named parameter is not specified.
7375 = PgNamedParam Name
74- -- | Query has no names inside but was called with named functions,
76+ -- | Query has no names inside but was called with named functions.
7577 | PgNoNames PG. Query
7678 -- | Query contains an empty name.
7779 | PgEmptyName PG. Query
7880 deriving (Eq )
7981
8082
8183-- | Type alias for monads that can throw errors of the 'PgNamedError' type.
82- type WithError = MonadError PgNamedError
84+ type WithNamedError = MonadError PgNamedError
8385
8486instance Show PgNamedError where
8587 show e = " PostgreSQL named parameter error: " ++ case e of
@@ -99,8 +101,8 @@ lookupName n = lookup n . map (\NamedParam{..} -> (namedParamName, namedParamPar
99101SELECT name, user FROM users WHERE id = ?id
100102@
101103
102- and returns either the error or query with all all names replaced by
103- questiosn marks @?@ with list of the names in the order of their appearance.
104+ and returns either the error or the query with all names replaced by
105+ question marks @?@ with the list of the names in the order of their appearance.
104106
105107For example:
106108
@@ -132,9 +134,11 @@ extractNames qr = go (PG.fromQuery qr) >>= \case
132134 isNameChar c = isAlphaNum c || c == ' _'
133135
134136
135- -- | Returns the list of values to use in query by given list of 'Name's.
137+ {- | Returns the list of values to use in query by given list of 'Name's.
138+ Throws 'PgNamedError' if any named parameter is not specified.
139+ -}
136140namesToRow
137- :: forall m . WithError m
141+ :: forall m . WithNamedError m
138142 => NonEmpty Name -- ^ List of the names used in query
139143 -> [NamedParam ] -- ^ List of the named parameters
140144 -> m (NonEmpty PG. Action )
@@ -176,7 +180,7 @@ queryNamed dbConnection [sql|
176180@
177181-}
178182queryNamed
179- :: (MonadIO m , WithError m , PG. FromRow res )
183+ :: (MonadIO m , WithNamedError m , PG. FromRow res )
180184 => PG. Connection -- ^ Database connection
181185 -> PG. Query -- ^ Query with named parameters inside
182186 -> [NamedParam ] -- ^ The list of named parameters to be used in the query
@@ -197,7 +201,7 @@ executeNamed dbConnection [sql|
197201@
198202-}
199203executeNamed
200- :: (MonadIO m , WithError m )
204+ :: (MonadIO m , WithNamedError m )
201205 => PG. Connection -- ^ Database connection
202206 -> PG. Query -- ^ Query with named parameters inside
203207 -> [NamedParam ] -- ^ The list of named parameters to be used in the query
@@ -208,7 +212,7 @@ executeNamed conn qNamed params =
208212
209213-- | Helper to use named parameters.
210214withNamedArgs
211- :: WithError m
215+ :: WithNamedError m
212216 => PG. Query
213217 -> [NamedParam ]
214218 -> m (PG. Query , NonEmpty PG. Action )
0 commit comments