Skip to content

Commit 1de39be

Browse files
vrom911Dmitrii Kovanikov
authored andcommitted
[#8] Use Connection instead of Pool (#9)
Resolves #8
1 parent 22687cd commit 1de39be

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

postgresql-simple-named.cabal

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ library
6969
build-depends: bytestring ^>= 0.10.8
7070
, mtl ^>= 2.2
7171
, postgresql-simple ^>= 0.6.2
72-
, resource-pool ^>= 0.2.3.2
7372
, text ^>= 1.2
7473

7574
test-suite postgresql-simple-named-test
@@ -82,7 +81,7 @@ test-suite postgresql-simple-named-test
8281
, hspec >= 2.6 && < 2.8
8382
, postgresql-simple-named
8483
, postgresql-simple
85-
, resource-pool
84+
, resource-pool ^>= 0.2.3.2
8685
, transformers
8786

8887
ghc-options: -threaded -rtsopts -with-rtsopts=-N

src/PgNamed.hs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,11 @@ import Data.Char (isAlphaNum)
4646
import Data.Int (Int64)
4747
import Data.List (lookup)
4848
import Data.List.NonEmpty (NonEmpty (..), toList)
49-
import Data.Pool (Pool)
5049
import Data.Text (Text)
5150
import Data.Text.Encoding (decodeUtf8)
5251
import GHC.Exts (IsString)
5352

5453
import qualified Data.ByteString.Char8 as BS
55-
import qualified Data.Pool as Pool
5654
import qualified Database.PostgreSQL.Simple as PG
5755
import qualified Database.PostgreSQL.Simple.ToField as PG
5856
import qualified Database.PostgreSQL.Simple.Types as PG
@@ -171,27 +169,27 @@ n =? a = NamedParam n $ PG.toField a
171169
and expects a list of rows in return.
172170
173171
@
174-
queryNamed dbPool [sql|
172+
queryNamed dbConnection [sql|
175173
SELECT id FROM table
176174
WHERE foo = ?foo
177175
|] [ "foo" '=?' "bar" ]
178176
@
179177
-}
180178
queryNamed
181179
:: (MonadIO m, WithError m, PG.FromRow res)
182-
=> Pool PG.Connection -- ^ Database connection pool
180+
=> PG.Connection -- ^ Database connection
183181
-> PG.Query -- ^ Query with named parameters inside
184182
-> [NamedParam] -- ^ The list of named parameters to be used in the query
185183
-> m [res] -- ^ Resulting rows
186-
queryNamed pool qNamed params =
184+
queryNamed conn qNamed params =
187185
withNamedArgs qNamed params >>= \(q, actions) ->
188-
liftIO $ Pool.withResource pool (\conn -> PG.query conn q (toList actions))
186+
liftIO $ PG.query conn q (toList actions)
189187

190188
{- | Modifies the database with a given query and named parameters
191189
and expects a number of the rows affected.
192190
193191
@
194-
executeNamed dbPool [sql|
192+
executeNamed dbConnection [sql|
195193
UPDATE table
196194
SET foo = 'bar'
197195
WHERE id = ?id
@@ -200,13 +198,13 @@ executeNamed dbPool [sql|
200198
-}
201199
executeNamed
202200
:: (MonadIO m, WithError m)
203-
=> Pool PG.Connection -- ^ Database connection pool
201+
=> PG.Connection -- ^ Database connection
204202
-> PG.Query -- ^ Query with named parameters inside
205203
-> [NamedParam] -- ^ The list of named parameters to be used in the query
206204
-> m Int64 -- ^ Number of the rows affected by the given query
207-
executeNamed pool qNamed params =
205+
executeNamed conn qNamed params =
208206
withNamedArgs qNamed params >>= \(q, actions) ->
209-
liftIO $ Pool.withResource pool (\conn -> PG.execute conn q (toList actions))
207+
liftIO $ PG.execute conn q (toList actions)
210208

211209
-- | Helper to use named parameters.
212210
withNamedArgs

test/Spec.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import GHC.Generics (Generic)
1010
import System.IO (hSetEncoding, stderr, stdout, utf8)
1111
import Test.Hspec (Spec, describe, hspec, it, shouldReturn)
1212

13-
import PgNamed (PgNamedError (..), queryNamed, (=?))
13+
import PgNamed (NamedParam, PgNamedError (..), queryNamed, (=?))
1414

1515
import qualified Data.Pool as Pool
1616
import qualified Database.PostgreSQL.Simple as Sql
@@ -38,20 +38,23 @@ unitTests dbPool = describe "Testing: postgresql-simple-named" $ do
3838
queryTestValue `shouldReturn` Right (TestValue 42 42 "baz")
3939
where
4040
missingNamedParam :: IO (Either PgNamedError TestValue)
41-
missingNamedParam = runNamedQuery $ queryNamed dbPool "SELECT ?foo, ?bar" ["foo" =? True]
41+
missingNamedParam = run "SELECT ?foo, ?bar" ["foo" =? True]
4242

4343
noNamedParams :: IO (Either PgNamedError TestValue)
44-
noNamedParams = runNamedQuery $ queryNamed dbPool "SELECT 42" []
44+
noNamedParams = run "SELECT 42" []
4545

4646
emptyName :: IO (Either PgNamedError TestValue)
47-
emptyName = runNamedQuery $ queryNamed dbPool "SELECT ?foo, ?" ["foo" =? True]
47+
emptyName = run "SELECT ?foo, ?" ["foo" =? True]
4848

4949
queryTestValue :: IO (Either PgNamedError TestValue)
50-
queryTestValue = runNamedQuery $ queryNamed dbPool "SELECT ?intVal, ?intVal, ?txtVal"
50+
queryTestValue = run "SELECT ?intVal, ?intVal, ?txtVal"
5151
[ "intVal" =? (42 :: Int)
5252
, "txtVal" =? ("baz" :: ByteString)
5353
]
5454

55+
run :: Sql.Query -> [NamedParam] -> IO (Either PgNamedError TestValue)
56+
run q params = runNamedQuery $ Pool.withResource dbPool (\conn -> queryNamed conn q params)
57+
5558
runNamedQuery :: ExceptT PgNamedError IO [TestValue] -> IO (Either PgNamedError TestValue)
5659
runNamedQuery = fmap (second head) . runExceptT
5760

0 commit comments

Comments
 (0)