preql-0.5: safe PostgreSQL queries using Quasiquoters
Safe HaskellNone
LanguageHaskell2010

Preql.Effect

Description

Effect class, expressing that a database connection is available or can be acquired, and transactions run.

Synopsis

Documentation

class Monad m => SqlQuery (m :: * -> *) where Source #

SqlQuery is separate from SQL so that nested Transactions are statically prevented. query can be used directly within any SQL monad (running a single-statement transaction), or within a Transaction.

Users should not need to define instances, as every SQL instance implies a SqlQuery instance.

Methods

query :: (ToSql p, FromSql r, KnownNat (Width r)) => (Query (Width r), p) -> m (Vector r) Source #

Run a parameterized query that returns data. The tuple argument is typically provided by one of the Quasiquoters: sql or select

query_ :: ToSql p => (Query 0, p) -> m () Source #

Run a parameterized query that does not return data.

Instances

Instances details
(Monad m, SQL m) => SqlQuery m Source # 
Instance details

Defined in Preql.Effect

Methods

query :: (ToSql p, FromSql r, KnownNat (Width r)) => (Query (Width r), p) -> m (Vector r) Source #

query_ :: ToSql p => (Query 0, p) -> m () Source #

SqlQuery Transaction Source # 
Instance details

Defined in Preql.Effect

Methods

query :: (ToSql p, FromSql r, KnownNat (Width r)) => (Query (Width r), p) -> Transaction (Vector r) Source #

query_ :: ToSql p => (Query 0, p) -> Transaction () Source #

class SqlQuery m => SQL (m :: * -> *) where Source #

An Effect class for running SQL queries. You can think of this as a context specifying a particular Postgres connection (or connection pool). A minimal instance defines withConnection.

Override the remaining methods to log errors before rethrowing, or not to rethrow.

Minimal complete definition

withConnection

Methods

runTransaction' :: IsolationLevel -> Transaction a -> m a Source #

Run multiple queries in a transaction.

withConnection :: (Connection -> m a) -> m a Source #

runTransaction covers the most common patterns of mult-statement transactions. withConnection is useful when you want more control, or want to override the defaults that your instance defines. For example: - change the number of retries - interleave calls to other services with the Postgres transaction - ensure a prepared statement is shared among successive transactions

queryOn :: (ToSql p, FromSql r, KnownNat (Width r)) => Connection -> (Query (Width r), p) -> m (Vector r) Source #

Run a query on the specified Connection

default queryOn :: (ToSql p, FromSql r, KnownNat (Width r), MonadIO m) => Connection -> (Query (Width r), p) -> m (Vector r) Source #

queryOn_ :: ToSql p => Connection -> (Query 0, p) -> m () Source #

default queryOn_ :: (ToSql p, MonadIO m) => Connection -> (Query 0, p) -> m () Source #

Instances

Instances details
SQL m => SQL (MaybeT m) Source # 
Instance details

Defined in Preql.Effect

SQL m => SQL (StateT s m) Source # 
Instance details

Defined in Preql.Effect

Methods

runTransaction' :: IsolationLevel -> Transaction a -> StateT s m a Source #

withConnection :: (Connection -> StateT s m a) -> StateT s m a Source #

queryOn :: (ToSql p, FromSql r, KnownNat (Width r)) => Connection -> (Query (Width r), p) -> StateT s m (Vector r) Source #

queryOn_ :: ToSql p => Connection -> (Query 0, p) -> StateT s m () Source #

SQL m => SQL (ReaderT r m) Source # 
Instance details

Defined in Preql.Effect

Methods

runTransaction' :: IsolationLevel -> Transaction a -> ReaderT r m a Source #

withConnection :: (Connection -> ReaderT r m a) -> ReaderT r m a Source #

queryOn :: (ToSql p, FromSql r0, KnownNat (Width r0)) => Connection -> (Query (Width r0), p) -> ReaderT r m (Vector r0) Source #

queryOn_ :: ToSql p => Connection -> (Query 0, p) -> ReaderT r m () Source #

SQL (ReaderT Connection IO) Source #

Most larger applications will define an instance; this one is suitable to test out the library. A safer version would use MVar Connection to ensure only one thread using it.

Instance details

Defined in Preql.Effect

SQL m => SQL (ExceptT e m) Source # 
Instance details

Defined in Preql.Effect

Methods

runTransaction' :: IsolationLevel -> Transaction a -> ExceptT e m a Source #

withConnection :: (Connection -> ExceptT e m a) -> ExceptT e m a Source #

queryOn :: (ToSql p, FromSql r, KnownNat (Width r)) => Connection -> (Query (Width r), p) -> ExceptT e m (Vector r) Source #

queryOn_ :: ToSql p => Connection -> (Query 0, p) -> ExceptT e m () Source #

SQL m => SQL (StateT s m) Source # 
Instance details

Defined in Preql.Effect

Methods

runTransaction' :: IsolationLevel -> Transaction a -> StateT s m a Source #

withConnection :: (Connection -> StateT s m a) -> StateT s m a Source #

queryOn :: (ToSql p, FromSql r, KnownNat (Width r)) => Connection -> (Query (Width r), p) -> StateT s m (Vector r) Source #

queryOn_ :: ToSql p => Connection -> (Query 0, p) -> StateT s m () Source #

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

Defined in Preql.Effect

Methods

runTransaction' :: IsolationLevel -> Transaction a -> RWST r w s m a Source #

withConnection :: (Connection -> RWST r w s m a) -> RWST r w s m a Source #

queryOn :: (ToSql p, FromSql r0, KnownNat (Width r0)) => Connection -> (Query (Width r0), p) -> RWST r w s m (Vector r0) Source #

queryOn_ :: ToSql p => Connection -> (Query 0, p) -> RWST r w s m () Source #

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

Defined in Preql.Effect

Methods

runTransaction' :: IsolationLevel -> Transaction a -> RWST r w s m a Source #

withConnection :: (Connection -> RWST r w s m a) -> RWST r w s m a Source #

queryOn :: (ToSql p, FromSql r0, KnownNat (Width r0)) => Connection -> (Query (Width r0), p) -> RWST r w s m (Vector r0) Source #

queryOn_ :: ToSql p => Connection -> (Query 0, p) -> RWST r w s m () Source #

runTransaction :: SQL m => Transaction a -> m a Source #

Run a Transaction with full Serializable isolation.

runTransactionIO :: IsolationLevel -> Transaction a -> Connection -> IO (Either QueryError a) Source #

Run the provided Transaction. If it fails with a QueryError, roll back.

data Transaction a Source #

A Transaction can only contain SQL queries (and pure functions).

Instances

Instances details
Monad Transaction Source # 
Instance details

Defined in Preql.Effect.Internal

Methods

(>>=) :: Transaction a -> (a -> Transaction b) -> Transaction b #

(>>) :: Transaction a -> Transaction b -> Transaction b #

return :: a -> Transaction a #

Functor Transaction Source # 
Instance details

Defined in Preql.Effect.Internal

Methods

fmap :: (a -> b) -> Transaction a -> Transaction b #

(<$) :: a -> Transaction b -> Transaction a #

Applicative Transaction Source # 
Instance details

Defined in Preql.Effect.Internal

Methods

pure :: a -> Transaction a #

(<*>) :: Transaction (a -> b) -> Transaction a -> Transaction b #

liftA2 :: (a -> b -> c) -> Transaction a -> Transaction b -> Transaction c #

(*>) :: Transaction a -> Transaction b -> Transaction b #

(<*) :: Transaction a -> Transaction b -> Transaction a #

SqlQuery Transaction Source # 
Instance details

Defined in Preql.Effect

Methods

query :: (ToSql p, FromSql r, KnownNat (Width r)) => (Query (Width r), p) -> Transaction (Vector r) Source #

query_ :: ToSql p => (Query 0, p) -> Transaction () Source #