hasql 0.1.0 → 0.1.1
raw patch · 3 files changed
+129/−13 lines, 3 filesdep +hasqldep +hasql-postgresdep +transformersPVP ok
version bump matches the API change (PVP)
Dependencies added: hasql, hasql-postgres, transformers
API changes (from Hackage documentation)
Files
- demo/Main.hs +67/−0
- hasql.cabal +54/−10
- library/Hasql.hs +8/−3
+ demo/Main.hs view
@@ -0,0 +1,67 @@+-- You can execute this file with 'cabal bench demo'.+{-# LANGUAGE QuasiQuotes, ScopedTypeVariables, OverloadedStrings #-}++import Control.Monad+import Control.Monad.IO.Class+import Control.Monad.Trans.Class+import Control.Monad.Trans.Maybe+import Data.Functor.Identity++-- Import the API from the "hasql" library+import qualified Hasql as H++-- Import the backend settings from the "hasql-postgres" library+import qualified Hasql.Postgres as H+++main = do++ let postgresSettings = H.Postgres "localhost" 5432 "postgres" "" "postgres"++ -- Prepare session settings with a smart constructor,+ -- which checks the inputted values on correctness.+ -- Set the connections pool size to 6 and the timeout to 30.+ sessionSettings <- maybe (fail "Improper session settings") return $ + H.sessionSettings 6 30++ -- Run a database session,+ -- while automatically managing the resources and exceptions.+ H.session postgresSettings sessionSettings $ do++ -- Execute a group of statements without any locking and ACID guarantees:+ H.tx Nothing $ do+ H.unit [H.q|DROP TABLE IF EXISTS a|]+ H.unit [H.q|CREATE TABLE a (id SERIAL NOT NULL, balance INT8, PRIMARY KEY (id))|]+ -- Insert three rows:+ replicateM_ 3 $ do + H.unit [H.q|INSERT INTO a (balance) VALUES (0)|]++ -- Declare a list of transfer settings, which we'll later use.+ -- The tuple structure is: + -- @(withdrawalAccountID, arrivalAccountID, amount)@+ let transfers :: [(Int, Int, Int)] = + [(1, 2, 20), (2, 1, 30), (2, 3, 100)]++ forM_ transfers $ \(id1, id2, amount) -> do+ -- Run a transaction with ACID guarantees.+ -- Hasql will automatically roll it back and retry it in case of conflicts.+ H.tx (Just (H.Serializable, True)) $ do+ -- Use MaybeT to handle empty results:+ runMaybeT $ do+ do+ Identity balance1 <- MaybeT $ H.single $ [H.q|SELECT balance FROM a WHERE id=?|] id1+ Identity balance2 <- MaybeT $ H.single $ [H.q|SELECT balance FROM a WHERE id=?|] id2+ lift $ H.unit $ [H.q|UPDATE a SET balance=? WHERE id=?|] (balance1 - amount) id1+ lift $ H.unit $ [H.q|UPDATE a SET balance=? WHERE id=?|] (balance2 + amount) id2++ -- Output all the updated rows:+ do+ -- Unfortunately in this case there's no way to infer the type of the results,+ -- so we need to specify it explicitly:+ rows :: [(Int, Int)] <- H.tx Nothing $ H.list $ [H.q|SELECT * FROM a|]+ forM_ rows $ \(id, amount) -> do+ liftIO $ putStrLn $ "ID: " ++ show id ++ ", Amount: " ++ show amount++++
hasql.cabal view
@@ -1,31 +1,49 @@ name: hasql version:- 0.1.0+ 0.1.1 synopsis: A minimalistic general high level API for relational databases description: A robust and concise yet powerful API for communication with arbitrary- relational databases. Features:+ relational databases. .+ __Features__:+ . * Concise and crisp API. Just a few functions and two monads doing all the boilerplate job for you. .+ * A powerful transaction abstraction, which provides + an automated resolution of conflicts.+ The API ensures that you're only able to perform a specific+ set of actions in the transaction context,+ which allows Hasql to safely resolve conflicting transactions + by automatically retrying them.+ This is much inspired by STM and ST.+ .+ * Support for cursors. Allows to fetch virtually limitless result sets in a+ constant memory using streaming.+ .+ * Employment of prepared statements. + Every statement you emit gets prepared and cached. + This raises the performance of the backend.+ . * Automated management of resources related to connections, transactions and cursors. . * A built-in connections pool. .- * Employment of prepared statements. Every statement you emit gets prepared- and cached. This raises the performance of the backend.- .- * Support for cursors. Allows to fetch virtually limitless result sets in a- constant memory using streaming.- . * Type-level generation of templates. You just can't write a statement with an incorrect number of placeholders. . * Mapping to any types actually supported by the backend.+ .+ __Links__:+ .+ * <../src/demo/Main.hs A basic tutorial-demo>+ .+ * <http://hackage.haskell.org/package/hasql-postgres A PostgreSQL backend>+ . category: Database homepage:@@ -58,6 +76,12 @@ library hs-source-dirs: library+ ghc-options:+ -funbox-strict-fields+ default-extensions:+ Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor, DeriveGeneric, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, ImpredicativeTypes, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples+ default-language:+ Haskell2010 other-modules: Hasql.Prelude Hasql.QQ@@ -93,9 +117,29 @@ mtl-prelude == 2.*, base-prelude >= 0.1.3 && < 0.2, base >= 4.5 && < 4.8+++-- Well, it's not a benchmark actually, +-- but there's no better way to specify an executable, +-- which is not intended for distribution, in Cabal.+benchmark demo+ type: + exitcode-stdio-1.0+ hs-source-dirs:+ demo+ main-is:+ Main.hs ghc-options:+ -O2+ -threaded+ "-with-rtsopts=-N" -funbox-strict-fields- default-extensions:- Arrows, BangPatterns, ConstraintKinds, DataKinds, DefaultSignatures, DeriveDataTypeable, DeriveFunctor, DeriveGeneric, EmptyDataDecls, FlexibleContexts, FlexibleInstances, FunctionalDependencies, GADTs, GeneralizedNewtypeDeriving, ImpredicativeTypes, LambdaCase, LiberalTypeSynonyms, MagicHash, MultiParamTypeClasses, MultiWayIf, NoImplicitPrelude, NoMonomorphismRestriction, OverloadedStrings, PatternGuards, ParallelListComp, QuasiQuotes, RankNTypes, RecordWildCards, ScopedTypeVariables, StandaloneDeriving, TemplateHaskell, TupleSections, TypeFamilies, TypeOperators, UnboxedTuples default-language: Haskell2010+ build-depends:+ hasql-postgres == 0.1.*,+ hasql == 0.1.*,+ transformers,+ base >= 4.5 && < 4.8++
library/Hasql.hs view
@@ -1,3 +1,8 @@+-- |+-- This is the API of the \"hasql\" library.+-- For an introduction to the package +-- and links to more documentation please refer to +-- <index.html the package's index page>. module Hasql ( -- * Session@@ -8,9 +13,6 @@ SessionSettings, sessionSettings, - -- ** Error- Error(..),- -- * Transaction Tx, Mode,@@ -32,6 +34,9 @@ -- * Row parser RowParser.RowParser(..),+ + -- * Error+ Error(..), ) where