Skip to content

Commit 1b99f6b

Browse files
committed
Merge pull request #8 from helium/feature/query-only
Fix parameter order and bump to 1.0.0.
2 parents 86890cc + 64d49f2 commit 1b99f6b

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

postgresql-transactional.cabal

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: postgresql-transactional
2-
version: 0.2.0.0
2+
version: 1.0.0
33
synopsis: a transactional monad on top of postgresql-simple
44
license: MIT
55
license-file: LICENSE
@@ -9,15 +9,21 @@ copyright: 2015 Helium
99
category: Database
1010
build-type: Simple
1111
cabal-version: >=1.10
12+
description:
13+
This package is a simple monadic wrapper around the SQL primitives
14+
provided by the postgresql-simple package. It provides simple and
15+
predictable semantics for database options, enforces awareness of
16+
Postgres's transactional nature at API boundaries, and obviates
17+
the need for SQL boilerplate in transactional queries.
1218

1319
source-repository head
1420
type: git
1521
location: https://github.com/helium/postgresql-transactional.git
1622

1723
library
18-
exposed-modules: Database.PostgreSQL.TransactionalStore
24+
exposed-modules: Database.PostgreSQL.Transaction
1925
hs-source-dirs: src
20-
ghc-options: -Wall -Werror
26+
ghc-options: -Wall
2127
-- other-extensions:
2228
build-depends: base >= 4 && < 5
2329
, postgresql-simple >= 0.4

src/Database/PostgreSQL/TransactionalStore.hs renamed to src/Database/PostgreSQL/Transaction.hs

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
55
{-# LANGUAGE MultiParamTypeClasses #-}
66

7-
module Database.PostgreSQL.TransactionalStore
7+
module Database.PostgreSQL.Transaction
88
( PGTransaction
9-
, runPGTransaction
10-
, runPGTransaction'
9+
, runPGTransactionT
10+
, runPGTransactionT'
1111
, runPGTransactionIO
1212
, query
1313
, query_
@@ -16,6 +16,7 @@ module Database.PostgreSQL.TransactionalStore
1616
, executeMany
1717
, returning
1818
, queryHead
19+
, queryOnly
1920
, formatQuery
2021
) where
2122

@@ -26,6 +27,7 @@ import Control.Monad.Reader
2627
import Control.Monad.Trans.Control
2728
import Data.Int
2829
import qualified Database.PostgreSQL.Simple as Postgres
30+
import Database.PostgreSQL.Simple.FromField
2931
import Database.PostgreSQL.Simple.FromRow
3032
import Database.PostgreSQL.Simple.ToRow
3133
import qualified Database.PostgreSQL.Simple.Transaction as Postgres.Transaction
@@ -43,36 +45,41 @@ newtype PGTransactionT m a =
4345

4446
type PGTransaction = PGTransactionT IO
4547

46-
runPGTransaction' :: MonadBaseControl IO m
47-
=> Postgres.Transaction.IsolationLevel
48-
-> PGTransactionT m a
49-
-> Postgres.Connection -> m a
50-
runPGTransaction' isolation (PGTransactionT pgTrans) conn =
48+
runPGTransactionT' :: MonadBaseControl IO m
49+
=> Postgres.Transaction.IsolationLevel
50+
-> PGTransactionT m a
51+
-> Postgres.Connection
52+
-> m a
53+
runPGTransactionT' isolation (PGTransactionT pgTrans) conn =
5154
let runTransaction run =
5255
Postgres.Transaction.withTransactionLevel isolation conn (run pgTrans)
5356
in control runTransaction `runReaderT` conn
5457

55-
runPGTransaction :: MonadBaseControl IO m
56-
=> PGTransactionT m a
57-
-> Postgres.Connection
58-
-> m a
59-
runPGTransaction = runPGTransaction' Postgres.Transaction.DefaultIsolationLevel
58+
runPGTransactionT :: MonadBaseControl IO m
59+
=> PGTransactionT m a
60+
-> Postgres.Connection
61+
-> m a
62+
runPGTransactionT = runPGTransactionT' Postgres.Transaction.DefaultIsolationLevel
6063

6164

6265
-- | Convenience function when there are no embedded monadic effects, only IO.
6366
runPGTransactionIO :: MonadIO m
6467
=> PGTransaction a
6568
-> Postgres.Connection
6669
-> m a
67-
runPGTransactionIO = (liftIO .) . runPGTransaction
70+
runPGTransactionIO = (liftIO .) . runPGTransactionT
6871

6972

7073
-- | Issue an SQL query, taking a 'ToRow' input and yielding 'FromRow' outputs.
74+
-- Please note that the parameter order is different from that in the parent
75+
-- postgresql-simple library; this is an intentional choice to improve the aesthetics
76+
-- when using the SQL quasiquoter (making the query parameters come first means that
77+
-- there is more room for the query string).
7178
query :: (ToRow input, FromRow output, MonadIO m)
72-
=> Postgres.Query
73-
-> input
79+
=> input
80+
-> Postgres.Query
7481
-> PGTransactionT m [output]
75-
query q params = ask >>= (\conn -> liftIO $ Postgres.query conn q params)
82+
query params q = ask >>= (\conn -> liftIO $ Postgres.query conn q params)
7683

7784
-- | As 'query', but for queries that take no arguments.
7885
query_ :: (FromRow output, MonadIO m)
@@ -82,30 +89,30 @@ query_ q = ask >>= liftIO . (`Postgres.query_` q)
8289

8390
-- | Run a single SQL action and return success.
8491
execute :: (ToRow input, MonadIO m)
85-
=> Postgres.Query
86-
-> input
92+
=> input
93+
-> Postgres.Query
8794
-> PGTransactionT m Int64
88-
execute q params = ask >>= (\conn -> liftIO $ Postgres.execute conn q params)
95+
execute params q = ask >>= (\conn -> liftIO $ Postgres.execute conn q params)
8996

9097
executeMany :: (ToRow input, MonadIO m)
91-
=> Postgres.Query
92-
-> [input]
98+
=> [input]
99+
-> Postgres.Query
93100
-> PGTransactionT m Int64
94-
executeMany q params = ask >>= (\conn -> liftIO $ Postgres.executeMany conn q params)
101+
executeMany params q = ask >>= (\conn -> liftIO $ Postgres.executeMany conn q params)
95102

96103
returning :: (ToRow input, FromRow output, MonadIO m)
97-
=> Postgres.Query
98-
-> [input]
104+
=> [input]
105+
-> Postgres.Query
99106
-> PGTransactionT m [output]
100-
returning q params = ask >>= (\conn -> liftIO $ Postgres.returning conn q params)
107+
returning params q = ask >>= (\conn -> liftIO $ Postgres.returning conn q params)
101108

102109
-- | Run a query and return 'Just' the first result found or 'Nothing'.
103110
queryHead :: (ToRow input, FromRow output, MonadIO m)
104111
=> input
105112
-> Postgres.Query
106113
-> PGTransactionT m (Maybe output)
107114
queryHead params q = do
108-
results <- query q params
115+
results <- query params q
109116
return $ case results of
110117
(a:_) -> Just a
111118
_ -> Nothing
@@ -115,14 +122,19 @@ executeOne :: (ToRow input, MonadIO m)
115122
=> input
116123
-> Postgres.Query
117124
-> PGTransactionT m Bool
118-
executeOne params q = do
119-
results <- execute q params
120-
return (results == 1)
125+
executeOne params q = (== 1) <$> execute params q
126+
127+
-- | Lookup a single FromField value. This takes care of handling 'Only' for you.
128+
queryOnly :: (ToRow input, FromField f, MonadIO m)
129+
=> input
130+
-> Postgres.Query
131+
-> PGTransactionT m (Maybe f)
132+
queryOnly params q = fmap Postgres.fromOnly <$> queryHead params q
121133

122-
formatQuery :: (ToRow q, MonadIO m)
123-
=> Postgres.Query
124-
-> q
134+
formatQuery :: (ToRow input, MonadIO m)
135+
=> input
136+
-> Postgres.Query
125137
-> PGTransactionT m Postgres.Query
126-
formatQuery q params = do
138+
formatQuery params q = do
127139
conn <- ask
128140
liftIO (PGTypes.Query <$> Postgres.formatQuery conn q params)

0 commit comments

Comments
 (0)