squeal-postgresql-0.9.0.0: Squeal PostgreSQL Library
Copyright(c) Eitan Chatav 2019
Maintainereitan@morphism.tech
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Squeal.PostgreSQL.Session.Monad

Description

Run Statements in the mtl-style typeclass MonadPQ.

Synopsis

MonadPQ

class Monad pq => MonadPQ db pq | pq -> db where Source #

MonadPQ is an mtl style constraint, similar to MonadState, for using LibPQ to run Statements.

Minimal complete definition

Nothing

Methods

executeParams Source #

Arguments

:: Statement db x y

query or manipulation

-> x

parameters

-> pq (Result y) 

executeParams runs a Statement which takes out-of-line parameters.

>>> import Data.Int (Int32, Int64)
>>> import Data.Monoid (Sum(Sum))
>>> :{
let
  sumOf :: Statement db (Int32, Int32) (Sum Int32)
  sumOf = query $ values_ $
    ( param @1 @('NotNull 'PGint4) +
      param @2 @('NotNull 'PGint4)
    ) `as` #getSum
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $ do
    result <- executeParams sumOf (2,2)
    firstRow result
:}
Just (Sum {getSum = 4})

default executeParams :: (MonadTrans t, MonadPQ db m, pq ~ t m) => Statement db x y -> x -> pq (Result y) Source #

executeParams_ Source #

Arguments

:: Statement db x ()

query or manipulation

-> x

parameters

-> pq () 

executeParams_ runs a returning-free Statement.

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Data.Int(Int32)
>>> :{
let
  insertion :: Statement DB (Int32, Int32) ()
  insertion = manipulation $ insertInto_ #tab $ Values_ $
    Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
    Set (param @2 @('NotNull 'PGint4)) `as` #col2
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen (executeParams_ insertion (2,2))
    & pqThen (define teardown)
:}

execute Source #

Arguments

:: Statement db () y

query or manipulation

-> pq (Result y) 

execute runs a parameter-free Statement.

>>> import Data.Int(Int32)
>>> :{
let
  two :: Expr ('NotNull 'PGint4)
  two = 2
  twoPlusTwo :: Statement db () (Only Int32)
  twoPlusTwo = query $ values_ $ (two + two) `as` #fromOnly
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $ do
    result <- execute twoPlusTwo
    firstRow result
:}
Just (Only {fromOnly = 4})

execute_ Source #

Arguments

:: Statement db () ()

query or manipulation

-> pq () 

execute_ runs a parameter-free, returning-free Statement.

>>> :{
let
  silence :: Statement db () ()
  silence = manipulation $
    UnsafeManipulation "Set client_min_messages TO WARNING"
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $ execute_ silence
:}

prepare Source #

Arguments

:: Statement db x y

query or manipulation

-> pq (Prepared pq x (Result y)) 

prepare creates a Prepared statement. When prepare is executed, the specified Statement is parsed, analyzed, and rewritten.

>>> import Data.Int (Int32, Int64)
>>> import Data.Monoid (Sum(Sum))
>>> :{
let
  sumOf :: Statement db (Int32, Int32) (Sum Int32)
  sumOf = query $ values_ $
    ( param @1 @('NotNull 'PGint4) +
      param @2 @('NotNull 'PGint4)
    ) `as` #getSum
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $ do
    prepared <- prepare sumOf
    result <- runPrepared prepared (2,2)
    deallocate prepared
    firstRow result
:}
Just (Sum {getSum = 4})

default prepare :: (MonadTrans t, MonadPQ db m, pq ~ t m) => Statement db x y -> pq (Prepared pq x (Result y)) Source #

prepare_ Source #

Arguments

:: Statement db x ()

query or manipulation

-> pq (Prepared pq x ()) 

prepare_ creates a Prepared statement. When prepare_ is executed, the specified Statement is parsed, analyzed, and rewritten.

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Data.Int(Int32)
>>> :{
let
  insertion :: Statement DB (Int32, Int32) ()
  insertion = manipulation $ insertInto_ #tab $ Values_ $
    Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
    Set (param @2 @('NotNull 'PGint4)) `as` #col2
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  session :: PQ DB DB IO ()
  session = do
    prepared <- prepare_ insertion
    runPrepared prepared (2,2)
    deallocate prepared
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen session
    & pqThen (define teardown)
:}

Instances

Instances details
MonadPQ db m => MonadPQ db (MaybeT m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> MaybeT m (Result y) Source #

executeParams_ :: Statement db x () -> x -> MaybeT m () Source #

execute :: Statement db () y -> MaybeT m (Result y) Source #

execute_ :: Statement db () () -> MaybeT m () Source #

prepare :: Statement db x y -> MaybeT m (Prepared (MaybeT m) x (Result y)) Source #

prepare_ :: Statement db x () -> MaybeT m (Prepared (MaybeT m) x ()) Source #

MonadPQ db m => MonadPQ db (ExceptT e m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> ExceptT e m (Result y) Source #

executeParams_ :: Statement db x () -> x -> ExceptT e m () Source #

execute :: Statement db () y -> ExceptT e m (Result y) Source #

execute_ :: Statement db () () -> ExceptT e m () Source #

prepare :: Statement db x y -> ExceptT e m (Prepared (ExceptT e m) x (Result y)) Source #

prepare_ :: Statement db x () -> ExceptT e m (Prepared (ExceptT e m) x ()) Source #

(Monoid w, MonadPQ db m) => MonadPQ db (WriterT w m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> WriterT w m (Result y) Source #

executeParams_ :: Statement db x () -> x -> WriterT w m () Source #

execute :: Statement db () y -> WriterT w m (Result y) Source #

execute_ :: Statement db () () -> WriterT w m () Source #

prepare :: Statement db x y -> WriterT w m (Prepared (WriterT w m) x (Result y)) Source #

prepare_ :: Statement db x () -> WriterT w m (Prepared (WriterT w m) x ()) Source #

(Monoid w, MonadPQ db m) => MonadPQ db (WriterT w m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> WriterT w m (Result y) Source #

executeParams_ :: Statement db x () -> x -> WriterT w m () Source #

execute :: Statement db () y -> WriterT w m (Result y) Source #

execute_ :: Statement db () () -> WriterT w m () Source #

prepare :: Statement db x y -> WriterT w m (Prepared (WriterT w m) x (Result y)) Source #

prepare_ :: Statement db x () -> WriterT w m (Prepared (WriterT w m) x ()) Source #

MonadPQ db m => MonadPQ db (StateT s m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> StateT s m (Result y) Source #

executeParams_ :: Statement db x () -> x -> StateT s m () Source #

execute :: Statement db () y -> StateT s m (Result y) Source #

execute_ :: Statement db () () -> StateT s m () Source #

prepare :: Statement db x y -> StateT s m (Prepared (StateT s m) x (Result y)) Source #

prepare_ :: Statement db x () -> StateT s m (Prepared (StateT s m) x ()) Source #

MonadPQ db m => MonadPQ db (StateT s m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> StateT s m (Result y) Source #

executeParams_ :: Statement db x () -> x -> StateT s m () Source #

execute :: Statement db () y -> StateT s m (Result y) Source #

execute_ :: Statement db () () -> StateT s m () Source #

prepare :: Statement db x y -> StateT s m (Prepared (StateT s m) x (Result y)) Source #

prepare_ :: Statement db x () -> StateT s m (Prepared (StateT s m) x ()) Source #

MonadPQ db m => MonadPQ db (ReaderT r m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> ReaderT r m (Result y) Source #

executeParams_ :: Statement db x () -> x -> ReaderT r m () Source #

execute :: Statement db () y -> ReaderT r m (Result y) Source #

execute_ :: Statement db () () -> ReaderT r m () Source #

prepare :: Statement db x y -> ReaderT r m (Prepared (ReaderT r m) x (Result y)) Source #

prepare_ :: Statement db x () -> ReaderT r m (Prepared (ReaderT r m) x ()) Source #

MonadPQ db m => MonadPQ db (IdentityT m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> IdentityT m (Result y) Source #

executeParams_ :: Statement db x () -> x -> IdentityT m () Source #

execute :: Statement db () y -> IdentityT m (Result y) Source #

execute_ :: Statement db () () -> IdentityT m () Source #

prepare :: Statement db x y -> IdentityT m (Prepared (IdentityT m) x (Result y)) Source #

prepare_ :: Statement db x () -> IdentityT m (Prepared (IdentityT m) x ()) Source #

MonadPQ db m => MonadPQ db (ContT r m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> ContT r m (Result y) Source #

executeParams_ :: Statement db x () -> x -> ContT r m () Source #

execute :: Statement db () y -> ContT r m (Result y) Source #

execute_ :: Statement db () () -> ContT r m () Source #

prepare :: Statement db x y -> ContT r m (Prepared (ContT r m) x (Result y)) Source #

prepare_ :: Statement db x () -> ContT r m (Prepared (ContT r m) x ()) Source #

(MonadIO io, db0 ~ db, db1 ~ db) => MonadPQ db (PQ db0 db1 io) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session

Methods

executeParams :: Statement db x y -> x -> PQ db0 db1 io (Result y) Source #

executeParams_ :: Statement db x () -> x -> PQ db0 db1 io () Source #

execute :: Statement db () y -> PQ db0 db1 io (Result y) Source #

execute_ :: Statement db () () -> PQ db0 db1 io () Source #

prepare :: Statement db x y -> PQ db0 db1 io (Prepared (PQ db0 db1 io) x (Result y)) Source #

prepare_ :: Statement db x () -> PQ db0 db1 io (Prepared (PQ db0 db1 io) x ()) Source #

(Monoid w, MonadPQ db m) => MonadPQ db (RWST r w s m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> RWST r w s m (Result y) Source #

executeParams_ :: Statement db x () -> x -> RWST r w s m () Source #

execute :: Statement db () y -> RWST r w s m (Result y) Source #

execute_ :: Statement db () () -> RWST r w s m () Source #

prepare :: Statement db x y -> RWST r w s m (Prepared (RWST r w s m) x (Result y)) Source #

prepare_ :: Statement db x () -> RWST r w s m (Prepared (RWST r w s m) x ()) Source #

(Monoid w, MonadPQ db m) => MonadPQ db (RWST r w s m) Source # 
Instance details

Defined in Squeal.PostgreSQL.Session.Monad

Methods

executeParams :: Statement db x y -> x -> RWST r w s m (Result y) Source #

executeParams_ :: Statement db x () -> x -> RWST r w s m () Source #

execute :: Statement db () y -> RWST r w s m (Result y) Source #

execute_ :: Statement db () () -> RWST r w s m () Source #

prepare :: Statement db x y -> RWST r w s m (Prepared (RWST r w s m) x (Result y)) Source #

prepare_ :: Statement db x () -> RWST r w s m (Prepared (RWST r w s m) x ()) Source #

Manipulate

manipulateParams Source #

Arguments

:: (MonadPQ db pq, GenericParams db params x xs, GenericRow row y ys) 
=> Manipulation '[] db params row

insertInto, update, or deleteFrom, and friends

-> x 
-> pq (Result y) 

manipulateParams runs a Manipulation.

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Control.Monad.IO.Class
>>> import Data.Int(Int32)
>>> :{
let
  insertAdd :: Manipulation_ DB (Int32, Int32) (Only Int32)
  insertAdd = insertInto #tab 
    ( Values_ $
        Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
        Set (param @2 @('NotNull 'PGint4)) `as` #col2
    ) OnConflictDoRaise
    ( Returning_ ((#col1 + #col2) `as` #fromOnly) )
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen
      ( do
          result <- manipulateParams insertAdd (2::Int32,2::Int32)
          Just (Only answer) <- firstRow result
          liftIO $ print (answer :: Int32)
      )
    & pqThen (define teardown)
:}
4

manipulateParams_ Source #

Arguments

:: (MonadPQ db pq, GenericParams db params x xs) 
=> Manipulation '[] db params '[]

insertInto_, update_, or deleteFrom_, and friends

-> x 
-> pq () 

manipulateParams_ runs a Manipulation, for a returning-free statement.

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Data.Int(Int32)
>>> :{
let
  insertion :: Manipulation_ DB (Int32, Int32) ()
  insertion = insertInto_ #tab $ Values_ $
    Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
    Set (param @2 @('NotNull 'PGint4)) `as` #col2
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen (manipulateParams_ insertion (2::Int32,2::Int32))
    & pqThen (define teardown)
:}

manipulate :: (MonadPQ db pq, GenericRow row y ys) => Manipulation '[] db '[] row -> pq (Result y) Source #

manipulate runs a Manipulation, for a parameter-free statement.

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Control.Monad.IO.Class
>>> import Data.Int(Int32)
>>> :{
let
  insertTwoPlusTwo :: Manipulation_ DB () (Only Int32)
  insertTwoPlusTwo = insertInto #tab 
    (Values_ $ Set 2 `as` #col1 :* Set 2 `as` #col2)
    OnConflictDoRaise
    (Returning_ ((#col1 + #col2) `as` #fromOnly))
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen
      ( do
          result <- manipulate insertTwoPlusTwo
          Just (Only answer) <- firstRow result
          liftIO $ print (answer :: Int32)
      )
    & pqThen (define teardown)
:}
4

manipulate_ :: MonadPQ db pq => Manipulation '[] db '[] '[] -> pq () Source #

manipulate_ runs a Manipulation, for a returning-free, parameter-free statement.

>>> :{
let
  silence :: Manipulation_ db () ()
  silence = UnsafeManipulation "Set client_min_messages TO WARNING"
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $ manipulate_ silence
:}

Run Query

runQueryParams Source #

Arguments

:: (MonadPQ db pq, GenericParams db params x xs, GenericRow row y ys) 
=> Query '[] '[] db params row

select and friends

-> x 
-> pq (Result y) 

runQueryParams runs a Query.

>>> import Data.Int (Int32, Int64)
>>> import Control.Monad.IO.Class
>>> import Data.Monoid (Sum(Sum))
>>> :{
let
  sumOf :: Query_ db (Int32, Int32) (Sum Int32)
  sumOf = values_ $
    ( param @1 @('NotNull 'PGint4) +
      param @2 @('NotNull 'PGint4)
    ) `as` #getSum
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $ do
    result <- runQueryParams sumOf (2::Int32,2::Int32)
    Just (Sum four) <- firstRow result
    liftIO $ print (four :: Int32)
:}
4

runQuery Source #

Arguments

:: (MonadPQ db pq, GenericRow row y ys) 
=> Query '[] '[] db '[] row

select and friends

-> pq (Result y) 

runQuery runs a Query, for a parameter-free statement.

>>> import Data.Int (Int32, Int64)
>>> import Control.Monad.IO.Class
>>> import Data.Monoid (Sum(Sum))
>>> :{
let
  twoPlusTwo :: Query_ db () (Sum Int32)
  twoPlusTwo = values_ $ (2 + 2) `as` #getSum
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $ do
    result <- runQuery twoPlusTwo
    Just (Sum four) <- firstRow result
    liftIO $ print (four :: Int32)
:}
4

Prepared

executePrepared Source #

Arguments

:: (MonadPQ db pq, Traversable list) 
=> Statement db x y

query or manipulation

-> list x

list of parameters

-> pq (list (Result y)) 

executePrepared runs a Statement on a Traversable container by first preparing the statement, then running the prepared statement on each element.

>>> import Data.Int (Int32, Int64)
>>> import Data.Monoid (Sum(Sum))
>>> :{
let
  sumOf :: Statement db (Int32, Int32) (Sum Int32)
  sumOf = query $ values_ $
    ( param @1 @('NotNull 'PGint4) +
      param @2 @('NotNull 'PGint4)
    ) `as` #getSum
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $ do
    results <- executePrepared sumOf [(2,2),(3,3),(4,4)]
    traverse firstRow results
:}
[Just (Sum {getSum = 4}),Just (Sum {getSum = 6}),Just (Sum {getSum = 8})]

executePrepared_ Source #

Arguments

:: (MonadPQ db pq, Foldable list) 
=> Statement db x ()

query or manipulation

-> list x

list of parameters

-> pq () 

executePrepared_ runs a returning-free Statement on a Foldable container by first preparing the statement, then running the prepared statement on each element.

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Data.Int(Int32)
>>> :{
let
  insertion :: Statement DB (Int32, Int32) ()
  insertion = manipulation $ insertInto_ #tab $ Values_ $
    Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
    Set (param @2 @('NotNull 'PGint4)) `as` #col2
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen (executePrepared_ insertion [(2,2),(3,3),(4,4)])
    & pqThen (define teardown)
:}

traversePrepared Source #

Arguments

:: (MonadPQ db pq, GenericParams db params x xs, GenericRow row y ys, Traversable list) 
=> Manipulation '[] db params row

insertInto, update, or deleteFrom, and friends

-> list x 
-> pq (list (Result y)) 

traversePrepared runs a Manipulation on a Traversable container by first preparing the statement, then running the prepared statement on each element.

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Control.Monad.IO.Class
>>> import Data.Int(Int32)
>>> :{
let
  insertAdd :: Manipulation_ DB (Int32, Int32) (Only Int32)
  insertAdd = insertInto #tab 
    ( Values_ $
        Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
        Set (param @2 @('NotNull 'PGint4)) `as` #col2
    ) OnConflictDoRaise
    ( Returning_ ((#col1 + #col2) `as` #fromOnly) )
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen
      ( do
          results <- traversePrepared insertAdd [(2::Int32,2::Int32),(3,3),(4,4)]
          answers <- traverse firstRow results
          liftIO $ print [answer :: Int32 | Just (Only answer) <- answers]
      )
    & pqThen (define teardown)
:}
[4,6,8]

forPrepared Source #

Arguments

:: (MonadPQ db pq, GenericParams db params x xs, GenericRow row y ys, Traversable list) 
=> list x 
-> Manipulation '[] db params row

insertInto, update, or deleteFrom, and friends

-> pq (list (Result y)) 

forPrepared is a flipped traversePrepared

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Control.Monad.IO.Class
>>> import Data.Int(Int32)
>>> :{
let
  insertAdd :: Manipulation_ DB (Int32, Int32) (Only Int32)
  insertAdd = insertInto #tab 
    ( Values_ $
        Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
        Set (param @2 @('NotNull 'PGint4)) `as` #col2
    ) OnConflictDoRaise
    ( Returning_ ((#col1 + #col2) `as` #fromOnly) )
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen
      ( do
          results <- forPrepared [(2::Int32,2::Int32),(3,3),(4,4)] insertAdd
          answers <- traverse firstRow results
          liftIO $ print [answer :: Int32 | Just (Only answer) <- answers]
      )
    & pqThen (define teardown)
:}
[4,6,8]

traversePrepared_ Source #

Arguments

:: (MonadPQ db pq, GenericParams db params x xs, Foldable list) 
=> Manipulation '[] db params '[]

insertInto_, update_, or deleteFrom_, and friends

-> list x 
-> pq () 

traversePrepared_ runs a returning-free Manipulation on a Foldable container by first preparing the statement, then running the prepared statement on each element.

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Data.Int(Int32)
>>> :{
let
  insertion :: Manipulation_ DB (Int32, Int32) ()
  insertion = insertInto_ #tab $ Values_ $
    Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
    Set (param @2 @('NotNull 'PGint4)) `as` #col2
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen (traversePrepared_ insertion [(2::Int32,2::Int32),(3,3),(4,4)])
    & pqThen (define teardown)
:}

forPrepared_ Source #

Arguments

:: (MonadPQ db pq, GenericParams db params x xs, Foldable list) 
=> list x 
-> Manipulation '[] db params '[]

insertInto_, update_, or deleteFrom_, and friends

-> pq () 

forPrepared_ is a flipped traversePrepared_

>>> type Column = 'NoDef :=> 'NotNull 'PGint4
>>> type Columns = '["col1" ::: Column, "col2" ::: Column]
>>> type Schema = '["tab" ::: 'Table ('[] :=> Columns)]
>>> type DB = Public Schema
>>> import Data.Int(Int32)
>>> :{
let
  insertion :: Manipulation_ DB (Int32, Int32) ()
  insertion = insertInto_ #tab $ Values_ $
    Set (param @1 @('NotNull 'PGint4)) `as` #col1 :*
    Set (param @2 @('NotNull 'PGint4)) `as` #col2
  setup :: Definition (Public '[]) DB
  setup = createTable #tab
    ( notNullable int4 `as` #col1 :*
      notNullable int4 `as` #col2
    ) Nil
  teardown :: Definition DB (Public '[])
  teardown = dropTable #tab
in
  withConnection "host=localhost port=5432 dbname=exampledb user=postgres password=postgres" $
    define setup
    & pqThen (forPrepared_ [(2::Int32,2::Int32),(3,3),(4,4)] insertion)
    & pqThen (define teardown)
:}

preparedFor Source #

Arguments

:: MonadPQ db pq 
=> (Prepared pq a (Result b) -> Prepared pq s t)

transform the input and output

-> Statement db a b

query or manipulation

-> s 
-> pq t 
  • prepare a statement
  • transforming its inputs and outputs with an optic, run the Prepared statement
  • deallocate the Prepared statement
>>> :type preparedFor traverse'
preparedFor traverse'
  :: (MonadPQ db pq, Traversable f) =>
     Statement db a b -> f a -> pq (f (Result b))