postgresql-simple-0.7.0.1: Mid-Level PostgreSQL client library

Copyright | (c) 2011 MailRank Inc. (c) 2011-2013 Leon P Smith
License | BSD3
Maintainer | Leon P Smith <leon@melding-monads.com>
Stability | experimental
Safe Haskell | None
Language | Haskell2010

Database.PostgreSQL.Simple.FromField

Description

The `FromField` typeclass, for converting a single value in a row returned by a SQL query into a more useful Haskell representation. Note that each instance of `FromField` is documented by a list of compatible postgresql types.

A Haskell numeric type is considered to be compatible with all PostgreSQL numeric types that are less accurate than it. For instance, the Haskell `Double` type is compatible with the PostgreSQL's 32-bit `int` type because it can represent a `int` exactly. On the other hand, since a `Double` might lose precision if representing PostgreSQL's 64-bit `bigint`, the two are not considered compatible.

Note that the `Float` and `Double` instances use attoparsec's `double` conversion routine, which sacrifices some accuracy for speed. If you need accuracy, consider first converting data to a `Scientific` or `Rational` type, and then converting to a floating-point type. If you are defining your own `FromRow` instances, this can be achieved simply by ``fromRational` `<$>` `field``, although this idiom is additionally compatible with PostgreSQL's `int8` and `numeric` types. If this is unacceptable, you may find `fieldWith` useful.

Also note that while converting to a `Double` through the `Scientific` type is likely somewhat faster than converting through the `Rational` type, the `Scientific` type has no way to represent `NaN` and `±Infinity` values. Thus, if you need precision conversion of regular floating point values and the possibility of receiving these special values from the backend, stick with `Rational`.

Because `FromField` is a typeclass, one may provide conversions to additional Haskell types without modifying postgresql-simple. This is particularly useful for supporting PostgreSQL types that postgresql-simple does not support out-of-box. Here's an example of what such an instance might look like for a UUID type that implements the `Read` class:

    import Data.UUID ( UUID )
    import Database.PostgreSQL.Simple.FromField
           ( FromField (fromField) , typeOid, returnError, ResultError (..) )
    import Database.PostgreSQL.Simple.TypeInfo.Static (typoid, uuid)
    import qualified Data.ByteString.Char8 as B

    instance FromField UUID where
       fromField f mdata =
          if typeOid f /= typoid uuid
            then returnError Incompatible f ""
            else case B.unpack `fmap` mdata of
                   Nothing  -> returnError UnexpectedNull f ""
                   Just dat ->
                      case [ x | (x,t) <- reads dat, ("","") <- lex t ] of
                        [x] -> return x
                        _   -> returnError ConversionFailed f dat

Note that because PostgreSQL's `uuid` type is built into postgres and is not provided by an extension, the `typeOid` of `uuid` does not change and thus we can examine it directly. One could hard-code the type oid, or obtain it by other means, but in this case we simply pull it out of the static table provided by postgresql-simple.

On the other hand if the type is provided by an extension, such as `PostGIS` or `hstore`, then the `typeOid` is not stable and can vary from database to database. In this case it is recommended that FromField instances use `typename` instead.

Synopsis

- 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 {
    - errSQLType :: String
    - errSQLTableOid :: Maybe Oid
    - errSQLField :: String
    - errHaskellType :: String
    - errMessage :: String
    }
  - | UnexpectedNull {
    - errSQLType :: String
    - errSQLTableOid :: Maybe Oid
    - errSQLField :: String
    - errHaskellType :: String
    - errMessage :: String
    }
  - | ConversionFailed {
    - errSQLType :: String
    - errSQLTableOid :: Maybe Oid
    - errSQLField :: String
    - errHaskellType :: String
    - errMessage :: String
    }
- returnError :: (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
    }
  - | Range {
    - typoid :: !Oid
    - typcategory :: !Char
    - typdelim :: !Char
    - typname :: !ByteString
    - rngsubtype :: !TypeInfo
    }
  - | Composite {
    - typoid :: !Oid
    - typcategory :: !Char
    - typdelim :: !Char
    - typname :: !ByteString
    - typrelid :: !Oid
    - attributes :: !(Vector Attribute)
    }
- data Attribute = Attribute {
  - attname :: !ByteString
  - atttype :: !TypeInfo
  }
- 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
  - = Text
  - | Binary
- pgArrayFieldParser :: Typeable a => FieldParser a -> FieldParser (PGArray a)
- attoFieldParser :: Typeable a => (Oid -> Bool) -> Parser a -> FieldParser a
- optionalField :: FieldParser a -> FieldParser (Maybe a)
- fromJSONField :: (FromJSON a, Typeable a) => FieldParser a
- fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString

# Documentation

class FromField a where Source #

A type that may be converted from a SQL type.

Methods

fromField :: FieldParser a Source #

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`.

Note that retaining any reference to the `Field` argument causes the entire `LibPQ.`Result`` to be retained. Thus, implementations of `fromField` should return results that do not refer to this value after the result have been evaluated to WHNF.

Note that as of `postgresql-simple-0.4.0.0`, the `ByteString` value has already been copied out of the `LibPQ.`Result`` before it has been passed to `fromField`. This is because for short strings, it's cheaper to copy the string than to set up a finalizer.

#### Instances

Instances details

FromField Value Source # | json, jsonb
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Value Source #
FromField ByteString Source # | bytea, name, text, "char", bpchar, varchar, unknown
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser ByteString Source #
FromField ByteString Source # | bytea, name, text, "char", bpchar, varchar, unknown
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser ByteString Source #
FromField Int16 Source # | int2
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Int16 Source #
FromField Int32 Source # | int2, int4
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Int32 Source #
FromField Int64 Source # | int2, int4, int8
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Int64 Source #
FromField Oid Source # | oid
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Oid Source #
FromField HStoreList Source # | hstore
Instance details Defined in Database.PostgreSQL.Simple.HStore.Implementation Methods fromField :: FieldParser HStoreList Source #
FromField HStoreMap Source # |
Instance details Defined in Database.PostgreSQL.Simple.HStore.Implementation Methods fromField :: FieldParser HStoreMap Source #
FromField Date Source # | date
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Date Source #
FromField LocalTimestamp Source # | timestamp
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser LocalTimestamp Source #
FromField UTCTimestamp Source # | timestamptz
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser UTCTimestamp Source #
FromField ZonedTimestamp Source # | timestamptz
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser ZonedTimestamp Source #
FromField Null Source # | compatible with any data type, but the value must be null
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Null Source #
FromField Scientific Source # | int2, int4, int8, float4, float8, numeric
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Scientific Source #
FromField Text Source # | name, text, "char", bpchar, varchar
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Text Source #
FromField Text Source # | name, text, "char", bpchar, varchar
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Text Source #
FromField Day Source # | date
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Day Source #
FromField UTCTime Source # | timestamptz
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser UTCTime Source #
FromField CalendarDiffTime Source # | interval. Requires you to configure intervalstyle as `iso_8601`. You can configure intervalstyle on every connection with a `SET` command, but for better performance you may want to configure it permanently in the file found with `SHOW config_file;` .
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser CalendarDiffTime Source #
FromField LocalTime Source # | timestamp
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser LocalTime Source #
FromField TimeOfDay Source # | time
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser TimeOfDay Source #
FromField ZonedTime Source # | timestamptz
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser ZonedTime Source #
FromField UUID Source # | uuid
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser UUID Source #
FromField Integer Source # | int2, int4, int8
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Integer Source #
FromField () Source # | void
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser () Source #
FromField Bool Source # | bool
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Bool Source #
FromField Char Source # | "char", bpchar
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Char Source #
FromField Double Source # | int2, int4, float4, float8 (Uses attoparsec's `double` routine, for better accuracy convert to `Scientific` or `Rational` first)
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Double Source #
FromField Float Source # | int2, float4 (Uses attoparsec's `double` routine, for better accuracy convert to `Scientific` or `Rational` first)
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Float Source #
FromField Int Source # | int2, int4, and if compiled as 64-bit code, int8 as well. This library was compiled as 64-bit code.
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Int Source #
FromField (CI Text) Source # | citext
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (CI Text) Source #
FromField (CI Text) Source # | citext
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (CI Text) Source #
FromField a => FromField (Identity a) Source # |
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (Identity a) Source #
FromField a => FromField (IORef a) Source # | Compatible with the same set of types as `a`. Note that modifying the `IORef` does not have any effects outside the local process on the local machine.
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (IORef a) Source #
FromField a => FromField (MVar a) Source # | Compatible with the same set of types as `a`. Note that modifying the `MVar` does not have any effects outside the local process on the local machine.
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (MVar a) Source #
FromField (Ratio Integer) Source # | int2, int4, int8, float4, float8, numeric
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (Ratio Integer) Source #
(FromJSON a, Typeable a) => FromField (Aeson a) Source # |
Instance details Defined in Database.PostgreSQL.Simple.Newtypes Methods fromField :: FieldParser (Aeson a) Source #
(FromField a, Typeable a) => FromField (PGRange a) Source # |
Instance details Defined in Database.PostgreSQL.Simple.Range Methods fromField :: FieldParser (PGRange a) Source #
FromField (Binary ByteString) Source # | bytea
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (Binary ByteString) Source #
FromField (Binary ByteString) Source # | bytea
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (Binary ByteString) Source #
(FromField a, Typeable a) => FromField (PGArray a) Source # | any postgresql array whose elements are compatible with type `a`
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (PGArray a) Source #
(FromField a, Typeable a) => FromField (Vector a) Source # |
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (Vector a) Source #
(FromField a, Typeable a) => FromField (IOVector a) Source # |
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (IOVector a) Source #
FromField a => FromField (Maybe a) Source # | For dealing with null values. Compatible with any postgresql type compatible with type `a`. Note that the type is not checked if the value is null, although it is inadvisable to rely on this behavior.
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (Maybe a) Source #
FromField [Char] Source # | name, text, "char", bpchar, varchar
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser [Char] Source #
(FromField a, FromField b) => FromField (Either a b) Source # | Compatible with both types. Conversions to type `b` are preferred, the conversion to type `a` will be tried after the `Right` conversion fails.
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (Either a b) Source #
FromField a => FromField (Const a b) Source # |
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser (Const a b) Source #

type FieldParser a = Field -> Maybe ByteString -> Conversion a Source #

data Conversion a Source #

#### Instances

Instances details

Alternative Conversion Source # |
Instance details Defined in Database.PostgreSQL.Simple.Internal Methods empty :: Conversion a Source # (<|>) :: Conversion a -> Conversion a -> Conversion a Source # some :: Conversion a -> Conversion [a] Source # many :: Conversion a -> Conversion [a] Source #
Applicative Conversion Source # |
Instance details Defined in Database.PostgreSQL.Simple.Internal Methods pure :: a -> Conversion a Source # (<*>) :: Conversion (a -> b) -> Conversion a -> Conversion b Source # liftA2 :: (a -> b -> c) -> Conversion a -> Conversion b -> Conversion c Source # (*>) :: Conversion a -> Conversion b -> Conversion b Source # (<*) :: Conversion a -> Conversion b -> Conversion a Source #
Functor Conversion Source # |
Instance details Defined in Database.PostgreSQL.Simple.Internal Methods fmap :: (a -> b) -> Conversion a -> Conversion b Source # (<$) :: a -> Conversion b -> Conversion a Source #
Monad Conversion Source # |
Instance details Defined in Database.PostgreSQL.Simple.Internal Methods (>>=) :: Conversion a -> (a -> Conversion b) -> Conversion b Source # (>>) :: Conversion a -> Conversion b -> Conversion b Source # return :: a -> Conversion a Source #
MonadPlus Conversion Source # |
Instance details Defined in Database.PostgreSQL.Simple.Internal Methods mzero :: Conversion a Source # mplus :: Conversion a -> Conversion a -> Conversion a Source #

runConversion :: Conversion a -> Connection -> IO (Ok a) Source #

conversionMap :: (Ok a -> Ok b) -> Conversion a -> Conversion b Source #

conversionError :: Exception err => err -> Conversion a Source #

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

- errSQLType :: String
- errSQLTableOid :: Maybe Oid
- errSQLField :: String
- errHaskellType :: String
- errMessage :: String

UnexpectedNull | A SQL `NULL` was encountered when the Haskell type did not permit it.

Fields

- errSQLType :: String
- errSQLTableOid :: Maybe Oid
- errSQLField :: String
- errHaskellType :: String
- errMessage :: String

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

- errSQLType :: String
- errSQLTableOid :: Maybe Oid
- errSQLField :: String
- errHaskellType :: String
- errMessage :: String

#### Instances

Instances details

Exception ResultError Source # |
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods toException :: ResultError -> SomeException Source # fromException :: SomeException -> Maybe ResultError Source # displayException :: ResultError -> String Source # backtraceDesired :: ResultError -> Bool Source #
Show ResultError Source # |
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods showsPrec :: Int -> ResultError -> ShowS Source # show :: ResultError -> String Source # showList :: [ResultError] -> ShowS Source #
Eq ResultError Source # |
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods (==) :: ResultError -> ResultError -> Bool Source # (/=) :: ResultError -> ResultError -> Bool Source #

returnError :: (Typeable a, Exception err) => (String -> Maybe Oid -> String -> String -> String -> err) -> Field -> String -> Conversion a Source #

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.

data Field Source #

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 ByteString Source #

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 the 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.

data TypeInfo Source #

A structure representing some of the metadata regarding a PostgreSQL type, mostly taken from the `pg_type` table.

Constructors

Basic |

Fields

- typoid :: !Oid
- typcategory :: !Char
- typdelim :: !Char
- typname :: !ByteString

Array |

Fields

- typoid :: !Oid
- typcategory :: !Char
- typdelim :: !Char
- typname :: !ByteString
- typelem :: !TypeInfo

Range |

Fields

- typoid :: !Oid
- typcategory :: !Char
- typdelim :: !Char
- typname :: !ByteString
- rngsubtype :: !TypeInfo

Composite |

Fields

- typoid :: !Oid
- typcategory :: !Char
- typdelim :: !Char
- typname :: !ByteString
- typrelid :: !Oid
- attributes :: !(Vector Attribute)

#### Instances

Instances details

Show TypeInfo Source # |
Instance details Defined in Database.PostgreSQL.Simple.TypeInfo.Types Methods showsPrec :: Int -> TypeInfo -> ShowS Source # show :: TypeInfo -> String Source # showList :: [TypeInfo] -> ShowS Source #

data Attribute Source #

Constructors

Attribute |

Fields

- attname :: !ByteString
- atttype :: !TypeInfo

#### Instances

Instances details

Show Attribute Source # |
Instance details Defined in Database.PostgreSQL.Simple.TypeInfo.Types Methods showsPrec :: Int -> Attribute -> ShowS Source # show :: Attribute -> String Source # showList :: [Attribute] -> ShowS Source #

typeInfo :: Field -> Conversion TypeInfo Source #

typeInfoByOid :: Oid -> Conversion TypeInfo Source #

name :: Field -> Maybe ByteString Source #

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 Oid Source #

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 -> Int Source #

If the column has a table associated with it, this returns the number of the associated table column. Table columns have nonzero numbers. Zero is returned if the specified column is not a simple reference to a table column, or when using pre-3.0 protocol. Analogous to libpq's `PQftablecol`.

format :: Field -> Format Source #

This returns whether the data was returned in a binary or textual format. Analogous to libpq's `PQfformat`.

typeOid :: Field -> Oid Source #

This returns the type oid associated with the column. Analogous to libpq's `PQftype`.

newtype Oid Source #

Constructors

Oid CUInt |

#### Instances

Instances details

Storable Oid |

Instance details

Defined in Database.PostgreSQL.LibPQ.Oid

Methods

sizeOf :: Oid -> Int Source #

alignment :: Oid -> Int Source #

peekElemOff :: Ptr Oid -> Int -> IO Oid Source #

pokeElemOff :: Ptr Oid -> Int -> Oid -> IO () Source #

peekByteOff :: Ptr b -> Int -> IO Oid Source #

pokeByteOff :: Ptr b -> Int -> Oid -> IO () Source #

peek :: Ptr Oid -> IO Oid Source #

poke :: Ptr Oid -> Oid -> IO () Source #

Read Oid |
Instance details Defined in Database.PostgreSQL.LibPQ.Oid Methods readsPrec :: Int -> ReadS Oid Source # readList :: ReadS [Oid] Source # readPrec :: ReadPrec Oid Source # readListPrec :: ReadPrec [Oid] Source #
Show Oid |
Instance details Defined in Database.PostgreSQL.LibPQ.Oid Methods showsPrec :: Int -> Oid -> ShowS Source # show :: Oid -> String Source # showList :: [Oid] -> ShowS Source #
Eq Oid |
Instance details Defined in Database.PostgreSQL.LibPQ.Oid Methods (==) :: Oid -> Oid -> Bool Source # (/=) :: Oid -> Oid -> Bool Source #
Ord Oid |
Instance details Defined in Database.PostgreSQL.LibPQ.Oid Methods compare :: Oid -> Oid -> Ordering Source # (<) :: Oid -> Oid -> Bool Source # (<=) :: Oid -> Oid -> Bool Source # (>) :: Oid -> Oid -> Bool Source # (>=) :: Oid -> Oid -> Bool Source # max :: Oid -> Oid -> Oid Source # min :: Oid -> Oid -> Oid Source #
FromField Oid Source # | oid
Instance details Defined in Database.PostgreSQL.Simple.FromField Methods fromField :: FieldParser Oid Source #
ToField Oid Source # |
Instance details Defined in Database.PostgreSQL.Simple.ToField Methods toField :: Oid -> Action Source #

data Format Source #

Constructors

Text |
Binary |

#### Instances

Instances details

Enum Format |

Instance details

Defined in Database.PostgreSQL.LibPQ.Enums

Methods

succ :: Format -> Format Source #

pred :: Format -> Format Source #

toEnum :: Int -> Format Source #

fromEnum :: Format -> Int Source #

enumFrom :: Format -> [Format] Source #

enumFromThen :: Format -> Format -> [Format] Source #

enumFromTo :: Format -> Format -> [Format] Source #

enumFromThenTo :: Format -> Format -> Format -> [Format] Source #

Show Format |
Instance details Defined in Database.PostgreSQL.LibPQ.Enums Methods showsPrec :: Int -> Format -> ShowS Source # show :: Format -> String Source # showList :: [Format] -> ShowS Source #
Eq Format |
Instance details Defined in Database.PostgreSQL.LibPQ.Enums Methods (==) :: Format -> Format -> Bool Source # (/=) :: Format -> Format -> Bool Source #
Ord Format |
Instance details Defined in Database.PostgreSQL.LibPQ.Enums Methods compare :: Format -> Format -> Ordering Source # (<) :: Format -> Format -> Bool Source # (<=) :: Format -> Format -> Bool Source # (>) :: Format -> Format -> Bool Source # (>=) :: Format -> Format -> Bool Source # max :: Format -> Format -> Format Source # min :: Format -> Format -> Format Source #
FromCInt Format |
Instance details Defined in Database.PostgreSQL.LibPQ.Enums Methods fromCInt :: CInt -> Maybe Format
ToCInt Format |
Instance details Defined in Database.PostgreSQL.LibPQ.Enums Methods toCInt :: Format -> CInt

pgArrayFieldParser :: Typeable a => FieldParser a -> FieldParser (PGArray a) Source #

attoFieldParser Source #

Arguments

:: Typeable a |
=> (Oid -> Bool) | Predicate for whether the postgresql type oid is compatible with this parser
-> Parser a | An attoparsec parser.
-> FieldParser a |

Construct a field parser from an attoparsec parser. An `Incompatible` error is thrown if the PostgreSQL oid does not match the specified predicate.

    instance FromField Int16 where
      fromField = attoFieldParser ok16 (signed decimal)

Since: 0.6.3

optionalField :: FieldParser a -> FieldParser (Maybe a) Source #

For dealing with SQL `null` values outside of the `FromField` class. Alternatively, one could use `optional`, but that also turns type and conversion errors into `Nothing`, whereas this is more specific and turns only `null` values into `Nothing`.

fromJSONField :: (FromJSON a, Typeable a) => FieldParser a Source #

Parse a field to a JSON `Value` and convert that into a Haskell value using the `FromJSON` instance.

This can be used as the default implementation for the `fromField` method for Haskell types that have a JSON representation in PostgreSQL.

The `Typeable` constraint is required to show more informative error messages when parsing fails.

Note that `fromJSONField :: FieldParser (`Maybe` Foo)` will return ``Nothing`` on the json `null` value, and return an exception on SQL `null` value. Alternatively, one could write ``optionalField` fromJSONField` that will return `Nothing` on SQL `null`, and otherwise will call `fromJSONField :: FieldParser Foo` and then return ``Just`` the result value, or return its exception. If one would like to return `Nothing` on both the SQL `null` and json `null` values, one way to do it would be to write `\f mv -> `join` `<$>` optionalField fromJSONField f mv`

fromFieldJSONByteString :: Field -> Maybe ByteString -> Conversion ByteString Source #

Return the JSON ByteString directly

Since: 0.6.3