Skip to content

Commit f590551

Browse files
vrom911Dmitrii Kovanikov
authored andcommitted
[#5] Write some README (#14)
* [#5] Write some README Resolves #5 * Update src/PgNamed.hs Co-Authored-By: Dmitrii Kovanikov <[email protected]> * Update README.md Co-Authored-By: Dmitrii Kovanikov <[email protected]> * Update README.md Co-Authored-By: Dmitrii Kovanikov <[email protected]> * Update README.md Co-Authored-By: Dmitrii Kovanikov <[email protected]> * Fix after review
1 parent 707766a commit f590551

File tree

3 files changed

+85
-20
lines changed

3 files changed

+85
-20
lines changed

README.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,70 @@
11
# postgresql-simple-named
22

3+
[![Build status](https://img.shields.io/travis/Holmusk/postgresql-simple-named.svg?logo=travis)](https://travis-ci.org/Holmusk/postgresql-simple-named)
34
[![Hackage](https://img.shields.io/hackage/v/postgresql-simple-named.svg?logo=haskell)](https://hackage.haskell.org/package/postgresql-simple-named)
4-
[![MPL-2.0 license](https://img.shields.io/badge/license-MPL--2.0-blue.svg)](LICENSE)
55
[![Stackage Lts](http://stackage.org/package/postgresql-simple-named/badge/lts)](http://stackage.org/lts/package/postgresql-simple-named)
66
[![Stackage Nightly](http://stackage.org/package/postgresql-simple-named/badge/nightly)](http://stackage.org/nightly/package/postgresql-simple-named)
7-
[![Build status](https://img.shields.io/travis/Holmusk/postgresql-simple-named.svg?logo=travis)](https://travis-ci.org/Holmusk/postgresql-simple-named)
7+
[![MPL-2.0 license](https://img.shields.io/badge/license-MPL--2.0-blue.svg)](LICENSE)
8+
9+
This library introduces the implementation of named parameters for the
10+
[`postgresql-simple`][pgs] library. `postgresql-simple-named` is considered to
11+
be used along with the [`postgresql-simple`][pgs] library, so you could refer
12+
there for the original documentation of primary functions. This package solves
13+
exclusively one particular problem — gives the ability to use named parameters
14+
instead of `?` in quasi-quoter queries and offers essential functions for substituting
15+
variables in queries (`queryNamed`, `executeNamed`).
16+
17+
## Example
18+
19+
Operator `=?` binds named parameters with the corresponding values. Named
20+
parameters inside SQL query start with the '?' character and can contain
21+
lowercase and uppercase letters, digits and underscore. Below you can find a
22+
basic example of how query with named parameters could look like:
23+
24+
```haskell
25+
queryNamed dbConnection [sql|
26+
SELECT
27+
id, name, city
28+
FROM users
29+
WHERE name = ?nameParam
30+
AND age = ?ageParam
831

9-
Implementation of named parameters for `postgresql-simple` library
32+
|] [ "nameParam" =? "John"
33+
, "ageParam" =? 42
34+
]
35+
```
36+
37+
This feature can be extremely helpful when the query uses some parameters more than once:
38+
39+
```haskell
40+
query dbConnection [sql|
41+
SELECT
42+
col1, col2
43+
FROM my_table
44+
WHERE id = ?
45+
AND (? IS NULL OR id > ? )
46+
AND (? IS NULL OR id < ? )
47+
48+
|] (someId, minId, minId, maxId, maxId)
49+
```
50+
51+
This is how the query looks like with the `postgresql-simple` library. You can
52+
rewrite it the following way using the `postgresql-simple-named` library:
53+
54+
```haskell
55+
queryNamed dbConnection [sql|
56+
SELECT
57+
col1, col2
58+
FROM my_table
59+
WHERE id = ?someId
60+
AND (?minId IS NULL OR id > ?minId )
61+
AND (?maxId IS NULL OR id < ?maxId )
62+
63+
|] [ "someId" =? 42
64+
, "minId" =? 1
65+
, "maxId" =? 100
66+
]
67+
```
1068

1169
## How to build
1270

@@ -19,3 +77,6 @@ Build the library with either `cabal new-build` or `stack build`.
1977
docker run -p 5432\:5432 -e POSTGRES_USER=postgres -e POSTGRES_DB=pg_named postgres\:10.5-alpine
2078
```
2179
* Run tests using `cabal new-test` or `stack test`
80+
81+
82+
[pgs]: https://hackage.haskell.org/package/postgresql-simple

postgresql-simple-named.cabal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: postgresql-simple-named
33
version: 0.0.0.0
44
synopsis: Implementation of named parameters for `postgresql-simple` library
55
description:
6-
Implementation of named parameters for `postgresql-simple` library.
6+
Implementation of named parameters for @postgresql-simple@ library.
77
.
88
Here is an exaple of how it could be used in your code:
99
.
@@ -21,8 +21,8 @@ homepage: https://github.com/Holmusk/postgresql-simple-named
2121
bug-reports: https://github.com/Holmusk/postgresql-simple-named/issues
2222
license: MPL-2.0
2323
license-file: LICENSE
24-
author: Holmusk
25-
maintainer: Holmusk<[email protected]>
24+
author: Dmitrii Kovanikov, Veronika Romashkina
25+
maintainer: Holmusk <[email protected]>
2626
copyright: 2019 Holmusk
2727
category: Database
2828
build-type: Simple

src/PgNamed.hs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
{- | Introduces named parameters for @postgresql-simple@ library.
66
It 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
7173
data 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

8486
instance 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
99101
SELECT 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
105107
For 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+
-}
136140
namesToRow
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
-}
178182
queryNamed
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
-}
199203
executeNamed
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.
210214
withNamedArgs
211-
:: WithError m
215+
:: WithNamedError m
212216
=> PG.Query
213217
-> [NamedParam]
214218
-> m (PG.Query, NonEmpty PG.Action)

0 commit comments

Comments
 (0)