Copyright | (c) Eitan Chatav 2017 |
---|---|
Maintainer | eitan@morphism.tech |
Stability | experimental |
Safe Haskell | None |
Language | Haskell2010 |
PQ
is where Squeal statements come to actually get run by
LibPQ
. It contains a PQ
indexed monad transformer to run Definition
s and
a MonadPQ
constraint for running a Manipulation
or Query
.
- newtype Connection (schema :: TablesType) = Connection {}
- connectdb :: forall schema io. MonadBase IO io => ByteString -> io (Connection schema)
- finish :: MonadBase IO io => Connection schema -> io ()
- withConnection :: forall schema0 schema1 io x. MonadBaseControl IO io => ByteString -> (Connection schema0 -> io (x, Connection schema1)) -> io x
- newtype PQ (schema0 :: TablesType) (schema1 :: TablesType) (m :: Type -> Type) (x :: Type) = PQ {
- runPQ :: Connection schema0 -> m (x, Connection schema1)
- execPQ :: Functor m => PQ schema0 schema1 m x -> Connection schema0 -> m (Connection schema1)
- pqAp :: Monad m => PQ schema0 schema1 m (x -> y) -> PQ schema1 schema2 m x -> PQ schema0 schema2 m y
- pqBind :: Monad m => (x -> PQ schema1 schema2 m y) -> PQ schema0 schema1 m x -> PQ schema0 schema2 m y
- pqThen :: Monad m => PQ schema1 schema2 m y -> PQ schema0 schema1 m x -> PQ schema0 schema2 m y
- define :: MonadBase IO io => Definition schema0 schema1 -> PQ schema0 schema1 io (Result '[])
- thenDefine :: MonadBase IO io => Definition schema1 schema2 -> PQ schema0 schema1 io x -> PQ schema0 schema2 io (Result '[])
- class Monad pq => MonadPQ schema pq | pq -> schema where
- type PQRun schema = forall m x. Monad m => PQ schema schema m x -> m (x, Connection schema)
- pqliftWith :: Functor m => (PQRun schema -> m a) -> PQ schema schema m a
- newtype Result (columns :: ColumnsType) = Result {}
- newtype RowNumber = RowNumber {
- unRowNumber :: Row
- newtype ColumnNumber (n :: Nat) (cs :: [k]) (c :: k) = UnsafeColumnNumber {}
- class KnownNat n => HasColumnNumber n columns column | n columns -> column where
- getValue :: (FromColumnValue colty y, MonadBase IO io) => RowNumber -> ColumnNumber n columns colty -> Result columns -> io y
- getRow :: (FromRow columns y, MonadBase IO io) => RowNumber -> Result columns -> io y
- getRows :: (FromRow columns y, MonadBase IO io) => Result columns -> io [y]
- ntuples :: MonadBase IO io => Result columns -> io RowNumber
- nextRow :: (FromRow columns y, MonadBase IO io) => RowNumber -> Result columns -> RowNumber -> io (Maybe (RowNumber, y))
- firstRow :: (FromRow columns y, MonadBase IO io) => Result columns -> io (Maybe y)
- liftResult :: MonadBase IO io => (Result -> IO x) -> Result results -> io x
Connection
newtype Connection (schema :: TablesType) Source #
A Connection
consists of a LibPQ
Connection
and a phantom TablesType
:: MonadBase IO io | |
=> ByteString | conninfo |
-> io (Connection schema) |
Makes a new connection to the database server.
This function opens a new database connection using the parameters taken from the string conninfo.
The passed string can be empty to use all default parameters, or it can contain one or more parameter settings separated by whitespace. Each parameter setting is in the form keyword = value. Spaces around the equal sign are optional. To write an empty value or a value containing spaces, surround it with single quotes, e.g., keyword = 'a value'. Single quotes and backslashes within the value must be escaped with a backslash, i.e., ' and .
To specify the schema you wish to connect with, use type application.
>>>
:set -XDataKinds
>>>
:set -XTypeOperators
>>>
type Schema = '["tab" ::: '["col" ::: 'Required ('Null 'PGint2)]]
>>>
:set -XTypeApplications
>>>
:set -XOverloadedStrings
>>>
conn <- connectdb @Schema "host=localhost port=5432 dbname=exampledb"
Note that, for now, squeal doesn't offer any protection from connecting with the wrong schema!
finish :: MonadBase IO io => Connection schema -> io () Source #
Closes the connection to the server.
withConnection :: forall schema0 schema1 io x. MonadBaseControl IO io => ByteString -> (Connection schema0 -> io (x, Connection schema1)) -> io x Source #
PQ
newtype PQ (schema0 :: TablesType) (schema1 :: TablesType) (m :: Type -> Type) (x :: Type) Source #
We keep track of the schema via an Atkey indexed state monad transformer,
PQ
.
PQ | |
|
MonadBase b m => MonadBase b (PQ schema schema m) Source # | |
MonadBaseControl b m => MonadBaseControl b (PQ schema schema m) Source # | |
MonadBase IO io => MonadPQ schema (PQ schema schema io) Source # | |
MonadTrans (PQ schema schema) Source # | |
Monad m => Monad (PQ schema schema m) Source # | |
Functor m => Functor (PQ schema0 schema1 m) Source # | |
Monad m => Applicative (PQ schema schema m) Source # | |
type StM (PQ schema schema m) x Source # | |
execPQ :: Functor m => PQ schema0 schema1 m x -> Connection schema0 -> m (Connection schema1) Source #
Run a PQ
and discard the result but keep the Connection
.
pqAp :: Monad m => PQ schema0 schema1 m (x -> y) -> PQ schema1 schema2 m x -> PQ schema0 schema2 m y Source #
indexed analog of <*>
pqBind :: Monad m => (x -> PQ schema1 schema2 m y) -> PQ schema0 schema1 m x -> PQ schema0 schema2 m y Source #
indexed analog of =<<
pqThen :: Monad m => PQ schema1 schema2 m y -> PQ schema0 schema1 m x -> PQ schema0 schema2 m y Source #
indexed analog of flipped >>
define :: MonadBase IO io => Definition schema0 schema1 -> PQ schema0 schema1 io (Result '[]) Source #
Run a Definition
with exec
, we expect that libpq obeys the law
define statement1 & thenDefine statement2 = define (statement1 >>> statement2)
thenDefine :: MonadBase IO io => Definition schema1 schema2 -> PQ schema0 schema1 io x -> PQ schema0 schema2 io (Result '[]) Source #
Chain together define
actions.
MonadPQ
class Monad pq => MonadPQ schema pq | pq -> schema where Source #
MonadPQ
is an mtl
style constraint, similar to
MonadState
, for using LibPQ
to
manipulateParams
runs aManipulation
with params from a type with aToParams
constraint. It callsexecParams
and doesn't afraid of anything.manipulate
is likemanipulateParams
for a parameter-free statement.runQueryParams
is likemanipulateParams
for query statements.traversePrepared
has the same type signature as a composition oftraverse
andmanipulateParams
but provides an optimization by preparing the statement withprepare
and then traversing aTraversable
container withexecPrepared
. The temporary prepared statement is then deallocated.forPrepared
is a flippedtraversePrepared
traversePrepared_
is liketraversePrepared
but works onFoldable
containers and returns unit.forPrepared_
is a flippedtraversePrepared_
.liftPQ
lets you lift actions fromLibPQ
that require a connection into your monad.
To define an instance, you can minimally define only manipulateParams
,
traversePrepared
, traversePrepared_
and liftPQ
. Monad transformers get
a default instance.
:: ToParams x params | |
=> Manipulation schema params ys | |
-> x | |
-> pq (Result ys) |
:: (MonadTrans t, MonadPQ schema pq1, pq ~ t pq1) | |
=> ToParams x params | |
=> Manipulation schema params ys | |
-> x | |
-> pq (Result ys) |
manipulate :: Manipulation schema '[] ys -> pq (Result ys) Source #
:: (ToParams x params, Traversable list) | |
=> Manipulation schema params ys | |
-> list x | |
-> pq (list (Result ys)) |
:: (MonadTrans t, MonadPQ schema pq1, pq ~ t pq1) | |
=> (ToParams x params, Traversable list) | |
=> Manipulation schema params ys | |
-> list x | |
-> pq (list (Result ys)) |
:: (ToParams x params, Traversable list) | |
=> list x | |
-> Manipulation schema params ys | |
-> pq (list (Result ys)) |
:: (ToParams x params, Foldable list) | |
=> Manipulation schema params '[] | |
-> list x | |
-> pq () |
:: (MonadTrans t, MonadPQ schema pq1, pq ~ t pq1) | |
=> (ToParams x params, Foldable list) | |
=> Manipulation schema params '[] | |
-> list x | |
-> pq () |
:: (ToParams x params, Foldable list) | |
=> list x | |
-> Manipulation schema params '[] | |
-> pq () |
liftPQ :: (Connection -> IO a) -> pq a Source #
liftPQ :: (MonadTrans t, MonadPQ schema pq1, pq ~ t pq1) => (Connection -> IO a) -> pq a Source #
MonadPQ schema m => MonadPQ schema (ListT m) Source # | |
MonadPQ schema m => MonadPQ schema (MaybeT m) Source # | |
MonadPQ schema m => MonadPQ schema (ExceptT e m) Source # | |
(Monoid w, MonadPQ schema m) => MonadPQ schema (WriterT w m) Source # | |
(Monoid w, MonadPQ schema m) => MonadPQ schema (WriterT w m) Source # | |
MonadPQ schema m => MonadPQ schema (StateT s m) Source # | |
MonadPQ schema m => MonadPQ schema (StateT s m) Source # | |
MonadPQ schema m => MonadPQ schema (IdentityT * m) Source # | |
MonadPQ schema m => MonadPQ schema (ContT * r m) Source # | |
MonadPQ schema m => MonadPQ schema (ReaderT * r m) Source # | |
MonadBase IO io => MonadPQ schema (PQ schema schema io) Source # | |
(Monoid w, MonadPQ schema m) => MonadPQ schema (RWST r w s m) Source # | |
(Monoid w, MonadPQ schema m) => MonadPQ schema (RWST r w s m) Source # | |
type PQRun schema = forall m x. Monad m => PQ schema schema m x -> m (x, Connection schema) Source #
A snapshot of the state of a PQ
computation.
pqliftWith :: Functor m => (PQRun schema -> m a) -> PQ schema schema m a Source #
Helper function in defining MonadBaseControl
instance for PQ
.
Result
newtype Result (columns :: ColumnsType) Source #
Encapsulates the result of a squeal command run by LibPQ
.
Result
s are parameterized by a ColumnsType
describing the column names
and their types.
newtype ColumnNumber (n :: Nat) (cs :: [k]) (c :: k) Source #
In addition to being newtypes around a CInt
, a ColumnNumber
is
parameterized by a Nat
ural number and acts as an index into a row.
class KnownNat n => HasColumnNumber n columns column | n columns -> column where Source #
>>>
getColumnNumber (columnNumber @5 @'[_,_,_,_,_,_])
Col 5
columnNumber :: ColumnNumber n columns column Source #
(KnownNat n, HasColumnNumber k ((-) n 1) columns column) => HasColumnNumber k n ((:) k column' columns) column Source # | |
HasColumnNumber k 0 ((:) k column1 columns) column1 Source # | |
:: (FromColumnValue colty y, MonadBase IO io) | |
=> RowNumber | row |
-> ColumnNumber n columns colty | col |
-> Result columns | result |
-> io y |
Get a single value corresponding to a given row and column number
from a Result
.
Get a row corresponding to a given row number from a Result
.
Get all rows from a Result
.
ntuples :: MonadBase IO io => Result columns -> io RowNumber Source #
Returns the number of rows (tuples) in the query result.
Get the first row if possible from a Result
.