Safe Haskell | None |
---|---|
Language | Haskell2010 |
Synopsis
- runSelect :: Default FromFields fields haskells => Connection -> Select fields -> IO [haskells]
- runSelectI :: Default (Inferrable FromFields) fields haskells => Connection -> Select fields -> IO [haskells]
- runSelectTF :: Default FromFields (rec O) (rec H) => Connection -> Select (rec O) -> IO [rec H]
- runSelectFold :: Default FromFields fields haskells => Connection -> Select fields -> b -> (b -> haskells -> IO b) -> IO b
- declareCursor :: Default FromFields fields haskells => Connection -> Select fields -> IO (Cursor haskells)
- closeCursor :: Cursor fields -> IO ()
- foldForward :: Cursor haskells -> Int -> (a -> haskells -> IO a) -> a -> IO (Either a a)
- unsafeFromField :: (b -> b') -> FromField sqlType b -> FromField sqlType' b'
- runSelectExplicit :: FromFields fields haskells -> Connection -> Select fields -> IO [haskells]
- runSelectFoldExplicit :: FromFields fields haskells -> Connection -> Select fields -> b -> (b -> haskells -> IO b) -> IO b
- declareCursorExplicit :: FromFields fields haskells -> Connection -> Select fields -> IO (Cursor haskells)
- data Cursor haskells
- data FromFields columns haskells
- data FromField pgType haskellType
- class DefaultFromField sqlType haskellType where
- defaultFromField :: FromField sqlType haskellType
- fromPGSFromField :: FromField haskell => FromField pgType haskell
- fromPGSFieldParser :: FieldParser haskell -> FromField pgType haskell
Running Select
s
:: Default FromFields fields haskells | |
=> Connection | |
-> Select fields | |
-> IO [haskells] |
runSelect
's use of the
typeclass means that the
compiler will have trouble inferring types. It is strongly
recommended that you provide full type signatures when using
Default
FromFields
runSelect
.
Example type specialization:
runSelect ::Select
(Field
SqlInt4
,Field
SqlText
) -> IO [(Int, String)]
Assuming the makeAdaptorAndInstance
splice has been run for the product type Foo
:
runSelect ::Select
(Foo (Field
SqlInt4
) (Field
SqlText
) (Field
SqlBool
) -> IO [Foo Int String Bool]
:: Default (Inferrable FromFields) fields haskells | |
=> Connection | |
-> Select fields | |
-> IO [haskells] |
Version of runSelect
with better type inference
:: Default FromFields (rec O) (rec H) | |
=> Connection | |
-> Select (rec O) | |
-> IO [rec H] |
runSelectTF
has better type inference than runSelect
but only
works with "higher-kinded data" types.
:: Default FromFields fields haskells | |
=> Connection | |
-> Select fields | |
-> b | |
-> (b -> haskells -> IO b) | |
-> IO b |
runSelectFold
streams the results of a query incrementally and consumes
the results with a left fold.
This fold is not strict. The stream consumer is responsible for forcing the evaluation of its result to avoid space leaks.
Cursor interface
:: Default FromFields fields haskells | |
=> Connection | |
-> Select fields | |
-> IO (Cursor haskells) |
Declare a temporary cursor. The cursor is given a unique name for the given connection.
closeCursor :: Cursor fields -> IO () Source #
Close the given cursor.
Creating new FromField
s
unsafeFromField :: (b -> b') -> FromField sqlType b -> FromField sqlType' b' Source #
Use unsafeFromField
to make an instance to allow you to run
queries on your own datatypes. For example:
newtype Foo = Foo Int instance DefaultFromField Foo Foo where defaultFromField = unsafeFromField Foo defaultFromField
It is "unsafe" because it does not check that the sqlType
correctly corresponds to the Haskell type.
Explicit versions
runSelectExplicit :: FromFields fields haskells -> Connection -> Select fields -> IO [haskells] Source #
runSelectFoldExplicit :: FromFields fields haskells -> Connection -> Select fields -> b -> (b -> haskells -> IO b) -> IO b Source #
declareCursorExplicit :: FromFields fields haskells -> Connection -> Select fields -> IO (Cursor haskells) Source #
Datatypes
data FromFields columns haskells Source #
A FromFields
specifies how to convert Postgres values (fields
)
into Haskell values (haskells
). Most likely you will never need
to create on of these or handle one directly. It will be provided
for you by the Default
FromFields
instance.
"FromFields
fields
haskells
" corresponds to
postgresql-simple's "RowParser
haskells
". "Default
FromFields
columns
haskells
" corresponds to
postgresql-simple's "FromRow
haskells
".
Instances
data FromField pgType haskellType Source #
A FromField
sqlType
haskellType
encodes how to turn
a value of Postgres type sqlType
into a value of Haskell type
haskellType
. For example a value of type FromField
SqlText
String
encodes how to turn a SqlText
result from the
database into a Haskell String
.
"FromField
sqlType
haskellType
" corresponds to
postgresql-simple's "FieldParser
haskellType
".
Instances
class DefaultFromField sqlType haskellType where Source #
A DefaultFromField
sqlType
haskellType
represents
the default way to turn a sqlType
result from the database into a
Haskell value of type haskellType
.
"DefaultFromField
sqlType
haskellType
" corresponds
to postgresql-simple's "FromField
haskellType
".
Creating an instance of DefaultFromField
for your own types is
necessary for retrieving those types from the database.
You should use one of the three methods below for writing a
DefaultFromField
instance.
- If you already have a postgresql-simple
FromField
instance for yourhaskellType
, usefromPGSFromField
. (This is how most of the built-in instances are defined.) - If you don't have a postgresql-simple
FromField
instance, but you do have an OpaleyeFromField
value for the type it wraps useunsafeFromField
if possible. See the documentation forunsafeFromField
for an example. - If you have a more complicated case, but not a
FromField
instance, write aFieldParser
for your type and usefromPGSFieldParser
. You can also add aFromField
instance using this.
defaultFromField :: FromField sqlType haskellType Source #
Instances
Helper functions
fromPGSFromField :: FromField haskell => FromField pgType haskell Source #
fromPGSFieldParser :: FieldParser haskell -> FromField pgType haskell Source #