mirror of
https://github.com/nikita-volkov/hasql.git
synced 2024-12-28 04:57:14 +03:00
Rename "SEx" to "Ex", since we're not teenagers here
This commit is contained in:
parent
9607cf8637
commit
23cf4b9420
16
demo/Main.hs
16
demo/Main.hs
@ -36,11 +36,11 @@ main = do
|
||||
|
||||
-- Execute a group of statements without any locking and ACID guarantees:
|
||||
H.tx Nothing $ do
|
||||
H.unitSEx [H.stmt|DROP TABLE IF EXISTS a|]
|
||||
H.unitSEx [H.stmt|CREATE TABLE a (id SERIAL NOT NULL, balance INT8, PRIMARY KEY (id))|]
|
||||
H.unitEx [H.stmt|DROP TABLE IF EXISTS a|]
|
||||
H.unitEx [H.stmt|CREATE TABLE a (id SERIAL NOT NULL, balance INT8, PRIMARY KEY (id))|]
|
||||
-- Insert three rows:
|
||||
replicateM_ 3 $ do
|
||||
H.unitSEx [H.stmt|INSERT INTO a (balance) VALUES (0)|]
|
||||
H.unitEx [H.stmt|INSERT INTO a (balance) VALUES (0)|]
|
||||
|
||||
-- Declare a list of transfer settings, which we'll later use.
|
||||
-- The tuple structure is:
|
||||
@ -56,16 +56,16 @@ main = do
|
||||
runMaybeT $ do
|
||||
-- To distinguish results rows containing just one column,
|
||||
-- we use 'Identity' as a sort of a single element tuple.
|
||||
Identity balance1 <- MaybeT $ H.maybeSEx $ [H.stmt|SELECT balance FROM a WHERE id=?|] id1
|
||||
Identity balance2 <- MaybeT $ H.maybeSEx $ [H.stmt|SELECT balance FROM a WHERE id=?|] id2
|
||||
lift $ H.unitSEx $ [H.stmt|UPDATE a SET balance=? WHERE id=?|] (balance1 - amount) id1
|
||||
lift $ H.unitSEx $ [H.stmt|UPDATE a SET balance=? WHERE id=?|] (balance2 + amount) id2
|
||||
Identity balance1 <- MaybeT $ H.maybeEx $ [H.stmt|SELECT balance FROM a WHERE id=?|] id1
|
||||
Identity balance2 <- MaybeT $ H.maybeEx $ [H.stmt|SELECT balance FROM a WHERE id=?|] id2
|
||||
lift $ H.unitEx $ [H.stmt|UPDATE a SET balance=? WHERE id=?|] (balance1 - amount) id1
|
||||
lift $ H.unitEx $ [H.stmt|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 <- H.tx Nothing $ H.vectorSEx $ [H.stmt|SELECT * FROM a|]
|
||||
rows <- H.tx Nothing $ H.vectorEx $ [H.stmt|SELECT * FROM a|]
|
||||
forM_ rows $ \(id :: Int, amount :: Int) -> do
|
||||
liftIO $ putStrLn $ "ID: " ++ show id ++ ", Amount: " ++ show amount
|
||||
|
||||
|
@ -18,18 +18,18 @@ main =
|
||||
flip shouldBe (Right (Nothing :: Maybe (Identity Int))) =<< do
|
||||
session $ do
|
||||
H.tx Nothing $ do
|
||||
H.unitSEx $ [H.stmt|DROP TABLE IF EXISTS a|]
|
||||
H.unitSEx $ [H.stmt|CREATE TABLE a (x INT8 NOT NULL, PRIMARY KEY (x))|]
|
||||
H.unitEx $ [H.stmt|DROP TABLE IF EXISTS a|]
|
||||
H.unitEx $ [H.stmt|CREATE TABLE a (x INT8 NOT NULL, PRIMARY KEY (x))|]
|
||||
H.tx (Just (H.Serializable, Just False)) $ do
|
||||
H.unitSEx $ [H.stmt|INSERT INTO a (x) VALUES (2)|]
|
||||
H.unitEx $ [H.stmt|INSERT INTO a (x) VALUES (2)|]
|
||||
H.tx Nothing $ do
|
||||
H.maybeSEx $ [H.stmt|SELECT x FROM a WHERE x = 2|]
|
||||
H.maybeEx $ [H.stmt|SELECT x FROM a WHERE x = 2|]
|
||||
|
||||
context "UTF-8 templates" $ do
|
||||
|
||||
it "encode properly" $ do
|
||||
flip shouldBe (Right (Just (Identity ("Ёжик" :: Text)))) =<< do
|
||||
session $ H.tx Nothing $ H.maybeSEx $ [H.stmt| SELECT 'Ёжик' |]
|
||||
session $ H.tx Nothing $ H.maybeEx $ [H.stmt| SELECT 'Ёжик' |]
|
||||
|
||||
context "Bug" $ do
|
||||
|
||||
@ -37,16 +37,16 @@ main =
|
||||
|
||||
it "should not be" $ do
|
||||
session $ H.tx Nothing $ do
|
||||
H.unitSEx [H.stmt|DROP TABLE IF EXISTS artist|]
|
||||
H.unitSEx [H.stmt|DROP TABLE IF EXISTS artist_union|]
|
||||
H.unitSEx $
|
||||
H.unitEx [H.stmt|DROP TABLE IF EXISTS artist|]
|
||||
H.unitEx [H.stmt|DROP TABLE IF EXISTS artist_union|]
|
||||
H.unitEx $
|
||||
[H.stmt|
|
||||
CREATE TABLE "artist_union" (
|
||||
"id" BIGSERIAL,
|
||||
PRIMARY KEY ("id")
|
||||
)
|
||||
|]
|
||||
H.unitSEx $
|
||||
H.unitEx $
|
||||
[H.stmt|
|
||||
CREATE TABLE "artist" (
|
||||
"id" BIGSERIAL,
|
||||
@ -60,13 +60,13 @@ main =
|
||||
let
|
||||
insertArtistUnion :: H.Tx HP.Postgres s Int64
|
||||
insertArtistUnion =
|
||||
fmap (runIdentity . fromJust) $ H.maybeSEx $
|
||||
fmap (runIdentity . fromJust) $ H.maybeEx $
|
||||
[H.stmt|
|
||||
INSERT INTO artist_union DEFAULT VALUES RETURNING id
|
||||
|]
|
||||
insertArtist :: Int64 -> [Text] -> H.Tx HP.Postgres s Int64
|
||||
insertArtist unionID artistNames =
|
||||
fmap (runIdentity . fromJust) $ H.maybeSEx $
|
||||
fmap (runIdentity . fromJust) $ H.maybeEx $
|
||||
[H.stmt|
|
||||
INSERT INTO artist
|
||||
(artist_union_id,
|
||||
@ -92,16 +92,16 @@ main =
|
||||
flip shouldSatisfy (\case Left (H.ResultError _) -> True; _ -> False) =<< do
|
||||
session $ do
|
||||
H.tx Nothing $ do
|
||||
H.unitSEx [H.stmt|DROP TABLE IF EXISTS data|]
|
||||
H.unitSEx [H.stmt|CREATE TABLE data (
|
||||
H.unitEx [H.stmt|DROP TABLE IF EXISTS data|]
|
||||
H.unitEx [H.stmt|CREATE TABLE data (
|
||||
field1 DECIMAL NOT NULL,
|
||||
field2 BIGINT NOT NULL,
|
||||
PRIMARY KEY (field1)
|
||||
)|]
|
||||
H.unitSEx [H.stmt|INSERT INTO data (field1, field2) VALUES (0, 0)|]
|
||||
H.unitEx [H.stmt|INSERT INTO data (field1, field2) VALUES (0, 0)|]
|
||||
mrow :: Maybe (Double, Int64, String) <-
|
||||
H.tx Nothing $
|
||||
H.maybeSEx $ [H.stmt|SELECT * FROM data|]
|
||||
H.maybeEx $ [H.stmt|SELECT * FROM data|]
|
||||
return ()
|
||||
|
||||
|
||||
|
@ -31,14 +31,14 @@ module Hasql
|
||||
stmt,
|
||||
|
||||
-- * Statement Execution
|
||||
SEx,
|
||||
unitSEx,
|
||||
countSEx,
|
||||
singleSEx,
|
||||
maybeSEx,
|
||||
listSEx,
|
||||
vectorSEx,
|
||||
streamSEx,
|
||||
Ex,
|
||||
unitEx,
|
||||
countEx,
|
||||
singleEx,
|
||||
maybeEx,
|
||||
listEx,
|
||||
vectorEx,
|
||||
streamEx,
|
||||
|
||||
-- * Transaction
|
||||
Tx,
|
||||
@ -255,20 +255,20 @@ tx mode (Tx m) =
|
||||
-- Statement executor.
|
||||
--
|
||||
-- Just an alias to a function, which executes a statement in 'Tx'.
|
||||
type SEx c s r =
|
||||
type Ex c s r =
|
||||
Bknd.Stmt c -> Tx c s r
|
||||
|
||||
-- |
|
||||
-- Execute a statement without processing the result.
|
||||
unitSEx :: SEx c s ()
|
||||
unitSEx =
|
||||
unitEx :: Ex c s ()
|
||||
unitEx =
|
||||
Tx . lift . Bknd.unitTx
|
||||
|
||||
-- |
|
||||
-- Execute a statement and count the amount of affected rows.
|
||||
-- Useful for resolving how many rows were updated or deleted.
|
||||
countSEx :: Bknd.CxValue c Word64 => SEx c s Word64
|
||||
countSEx =
|
||||
countEx :: Bknd.CxValue c Word64 => Ex c s Word64
|
||||
countEx =
|
||||
Tx . lift . Bknd.countTx
|
||||
|
||||
-- |
|
||||
@ -279,33 +279,33 @@ countSEx =
|
||||
--
|
||||
-- Please note that using this executor for selecting rows is conceptually wrong,
|
||||
-- since in that case the results are always optional.
|
||||
-- Use 'maybeSEx', 'listSEx' or 'vectorSEx' instead.
|
||||
-- Use 'maybeEx', 'listEx' or 'vectorEx' instead.
|
||||
--
|
||||
-- If the result is empty this executor will raise 'ResultError'.
|
||||
singleSEx :: CxRow.CxRow c r => SEx c s r
|
||||
singleSEx =
|
||||
join . fmap (maybe (Tx $ left $ ResultError "No rows on 'singleSEx'") return) .
|
||||
maybeSEx
|
||||
singleEx :: CxRow.CxRow c r => Ex c s r
|
||||
singleEx =
|
||||
join . fmap (maybe (Tx $ left $ ResultError "No rows on 'singleEx'") return) .
|
||||
maybeEx
|
||||
|
||||
-- |
|
||||
-- Execute a statement,
|
||||
-- which optionally produces a single result row.
|
||||
maybeSEx :: CxRow.CxRow c r => SEx c s (Maybe r)
|
||||
maybeSEx =
|
||||
fmap (fmap Vector.unsafeHead . mfilter (not . Vector.null) . Just) . vectorSEx
|
||||
maybeEx :: CxRow.CxRow c r => Ex c s (Maybe r)
|
||||
maybeEx =
|
||||
fmap (fmap Vector.unsafeHead . mfilter (not . Vector.null) . Just) . vectorEx
|
||||
|
||||
-- |
|
||||
-- Execute a statement,
|
||||
-- and produce a list of results.
|
||||
listSEx :: CxRow.CxRow c r => SEx c s [r]
|
||||
listSEx =
|
||||
fmap toList . vectorSEx
|
||||
listEx :: CxRow.CxRow c r => Ex c s [r]
|
||||
listEx =
|
||||
fmap toList . vectorEx
|
||||
|
||||
-- |
|
||||
-- Execute a statement,
|
||||
-- and produce a vector of results.
|
||||
vectorSEx :: CxRow.CxRow c r => SEx c s (Vector r)
|
||||
vectorSEx s =
|
||||
vectorEx :: CxRow.CxRow c r => Ex c s (Vector r)
|
||||
vectorEx s =
|
||||
Tx $ do
|
||||
r <- lift $ Bknd.vectorTx s
|
||||
EitherT $ return $ traverse ((mapLeft ResultError) . CxRow.parseRow) $ r
|
||||
@ -324,8 +324,8 @@ vectorSEx s =
|
||||
-- Note that in most databases cursors require establishing a database transaction,
|
||||
-- so depending on a backend the transaction may result in an error,
|
||||
-- if you run it improperly.
|
||||
streamSEx :: CxRow.CxRow c r => Int -> SEx c s (TxListT s (Tx c s) r)
|
||||
streamSEx n s =
|
||||
streamEx :: CxRow.CxRow c r => Int -> Ex c s (TxListT s (Tx c s) r)
|
||||
streamEx n s =
|
||||
Tx $ do
|
||||
r <- lift $ Bknd.streamTx n s
|
||||
return $ TxListT $ do
|
||||
|
Loading…
Reference in New Issue
Block a user