Safe Haskell | None |
---|
Database.PostgreSQL.Simple.FromField
- class FromField a where
- fromField :: FieldParser a
- type FieldParser a = Field -> Maybe ByteString -> Conversion a
- data Conversion a
- runConversion :: Conversion a -> Connection -> IO (Ok a)
- conversionMap :: (Ok a -> Ok b) -> Conversion a -> Conversion b
- conversionError :: Exception err => err -> Conversion a
- data ResultError
- = Incompatible { }
- | UnexpectedNull { }
- | ConversionFailed { }
- returnError :: forall a err. (Typeable a, Exception err) => (String -> Maybe Oid -> String -> String -> String -> err) -> Field -> String -> Conversion a
- data Field
- typename :: Field -> Conversion ByteString
- data TypeInfo
- = Basic {
- typoid :: !Oid
- typcategory :: !Char
- typdelim :: !Char
- typname :: !ByteString
- | Array {
- typoid :: !Oid
- typcategory :: !Char
- typdelim :: !Char
- typname :: !ByteString
- typelem :: !TypeInfo
- = Basic {
- typeInfo :: Field -> Conversion TypeInfo
- typeInfoByOid :: Oid -> Conversion TypeInfo
- name :: Field -> Maybe ByteString
- tableOid :: Field -> Maybe Oid
- tableColumn :: Field -> Int
- format :: Field -> Format
- typeOid :: Field -> Oid
- newtype Oid = Oid CUInt
- data Format
Documentation
A type that may be converted from a SQL type.
Methods
fromField :: FieldParser aSource
Convert a SQL value to a Haskell value.
Returns a list of exceptions if the conversion fails. In the case of
library instances, this will usually be a single ResultError
, but
may be a UnicodeException
.
Implementations of fromField
should not retain any references to
the Field
nor the ByteString
arguments after the result has
been evaluated to WHNF. Such a reference causes the entire
LibPQ.
to be retained.
Result
For example, the instance for ByteString
uses copy
to avoid
such a reference, and that using bytestring functions such as drop
and takeWhile
alone will also trigger this memory leak.
Instances
type FieldParser a = Field -> Maybe ByteString -> Conversion aSource
data Conversion a Source
runConversion :: Conversion a -> Connection -> IO (Ok a)Source
conversionMap :: (Ok a -> Ok b) -> Conversion a -> Conversion bSource
conversionError :: Exception err => err -> Conversion aSource
data ResultError Source
Exception thrown if conversion from a SQL value to a Haskell value fails.
Constructors
Incompatible | The SQL and Haskell types are not compatible. |
Fields
| |
UnexpectedNull | A SQL |
Fields
| |
ConversionFailed | The SQL value could not be parsed, or could not be represented as a valid Haskell value, or an unexpected low-level error occurred (e.g. mismatch between metadata and actual data in a row). |
Fields
|
returnError :: forall a err. (Typeable a, Exception err) => (String -> Maybe Oid -> String -> String -> String -> err) -> Field -> String -> Conversion aSource
Given one of the constructors from ResultError
, the field,
and an errMessage
, this fills in the other fields in the
exception value and returns it in a 'Left . SomeException'
constructor.
A Field represents metadata about a particular field
You don't particularly want to retain these structures for a long period of time, as they will retain the entire query result, not just the field metadata
typename :: Field -> Conversion ByteStringSource
Returns the data type name. This is the preferred way of identifying types that do not have a stable type oid, such as types provided by extensions to PostgreSQL.
More concretely, it returns the typname
column associated with the
type oid in the pg_type
table. First, postgresql-simple will check
built-in, static table. If the type oid is not there, postgresql-simple
will check a per-connection cache, and then finally query the database's
meta-schema.
Constructors
Basic | |
Fields
| |
Array | |
Fields
|
name :: Field -> Maybe ByteStringSource
Returns the name of the column. This is often determined by a table
definition, but it can be set using an as
clause.
tableOid :: Field -> Maybe OidSource
Returns the name of the object id of the table
associated with the
column, if any. Returns Nothing
when there is no such table;
for example a computed column does not have a table associated with it.
Analogous to libpq's PQftable
.
tableColumn :: Field -> IntSource
If the column has a table associated with it, this returns the number
off the associated table column. Numbering starts from 0. Analogous
to libpq's PQftablecol
.
format :: Field -> FormatSource
This returns whether the data was returned in a binary or textual format.
Analogous to libpq's PQfformat
.
This returns the type oid associated with the column. Analogous
to libpq's PQftype
.
newtype Oid