hasql 0.19.3.1 → 0.19.3.2
raw patch · 4 files changed
+80/−16 lines, 4 files
Files
- hasql.cabal +1/−1
- library/Hasql/Private/Session.hs +8/−2
- tasty/Main.hs +55/−1
- tasty/Main/DSL.hs +16/−12
hasql.cabal view
@@ -1,7 +1,7 @@ name: hasql version:- 0.19.3.1+ 0.19.3.2 category: Hasql, Database, PostgreSQL synopsis:
library/Hasql/Private/Session.hs view
@@ -4,6 +4,7 @@ import Hasql.Prelude import qualified Database.PostgreSQL.LibPQ as LibPQ import qualified Hasql.Decoders.Results as Decoders.Results+import qualified Hasql.Decoders.Result as Decoders.Result import qualified Hasql.Settings as Settings import qualified Hasql.Private.IO as IO import qualified Hasql.Private.Query as Query@@ -30,8 +31,13 @@ sql :: ByteString -> Session () sql sql = Session $ ReaderT $ \(Connection.Connection pqConnectionRef integerDatetimes registry) ->- EitherT $ fmap (mapLeft unsafeCoerce) $ withMVar pqConnectionRef $ \pqConnection ->- IO.sendNonparametricQuery pqConnection sql+ EitherT $ fmap (mapLeft unsafeCoerce) $ withMVar pqConnectionRef $ \pqConnection -> runEitherT $ do+ EitherT $ IO.sendNonparametricQuery pqConnection sql+ EitherT $ IO.getResults pqConnection integerDatetimes decoder+ where+ decoder =+ Decoders.Results.single $+ Decoders.Result.unit -- | -- Parameters and a specification of the parametric query to apply them to.
tasty/Main.hs view
@@ -1,6 +1,6 @@ module Main where -import Main.Prelude hiding (assert, isRight, isLeft)+import Main.Prelude hiding (assert) import Test.QuickCheck.Instances import Test.Tasty import Test.Tasty.Runners@@ -12,6 +12,7 @@ import qualified Hasql.Query as Query import qualified Hasql.Encoders as Encoders import qualified Hasql.Decoders as Decoders+import qualified Hasql.Session as Session main = defaultMain tree@@ -20,6 +21,28 @@ localOption (NumThreads 1) $ testGroup "All tests" [+ testCase "\"another command is already in progress\" bugfix" $+ let+ sumQuery :: Query.Query (Int64, Int64) Int64+ sumQuery =+ Query.statement sql encoder decoder True+ where+ sql =+ "select ($1 + $2)"+ encoder =+ contramap fst (Encoders.value Encoders.int8) <>+ contramap snd (Encoders.value Encoders.int8)+ decoder =+ Decoders.singleRow (Decoders.value Decoders.int8)+ session :: Session.Session Int64+ session =+ do+ Session.sql "begin;"+ s <- Session.query (1,1) sumQuery+ Session.sql "end;"+ return s+ in DSL.session session >>= \x -> assertBool (show x) (isRight x)+ , testCase "Executing the same query twice" $ pure () ,@@ -73,6 +96,37 @@ Encoders.value (Encoders.interval) in DSL.query (10 :: DiffTime) query in actualIO >>= \x -> assertEqual (show x) (Right (10 :: DiffTime)) x+ ,+ testCase "Unknown" $+ let+ actualIO =+ DSL.session $ do+ let+ query =+ Query.statement sql mempty Decoders.unit True+ where+ sql =+ "drop type if exists mood"+ in DSL.query () query+ let+ query =+ Query.statement sql mempty Decoders.unit True+ where+ sql =+ "create type mood as enum ('sad', 'ok', 'happy')"+ in DSL.query () query+ let+ query =+ Query.statement sql encoder decoder True+ where+ sql =+ "select $1 = ('ok' :: mood)"+ decoder =+ (Decoders.singleRow (Decoders.value (Decoders.bool)))+ encoder =+ Encoders.value (Encoders.unknown)+ in DSL.query "ok" query+ in actualIO >>= assertEqual "" (Right True) , testCase "Enum" $ let
tasty/Main/DSL.hs view
@@ -1,4 +1,12 @@-module Main.DSL where+module Main.DSL+(+ Session,+ SessionError(..),+ session,+ Hasql.Session.query,+ Hasql.Session.sql,+)+where import Main.Prelude import qualified Hasql.Connection as HC@@ -8,17 +16,16 @@ import qualified Hasql.Session -newtype Session a =- Session (ReaderT HC.Connection (EitherT Hasql.Session.Error IO) a)- deriving (Functor, Applicative, Monad, MonadIO)+type Session =+ Hasql.Session.Session data SessionError =- ConnectionError (Maybe ByteString) |+ ConnectionError (HC.ConnectionError) | SessionError (Hasql.Session.Error) deriving (Show, Eq) session :: Session a -> IO (Either SessionError a)-session (Session impl) =+session session = runEitherT $ acquire >>= \connection -> use connection <* release connection where acquire =@@ -33,11 +40,8 @@ password = "" database = "postgres" use connection =- bimapEitherT SessionError id $- runReaderT impl connection+ EitherT $+ fmap (mapLeft SessionError) $+ Hasql.Session.run session connection release connection = lift $ HC.release connection--query :: a -> HQ.Query a b -> Session b-query params query =- Session $ ReaderT $ \connection -> EitherT $ flip Hasql.Session.run connection $ Hasql.Session.query params query