| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Hasql.Decoders
Description
A DSL for declaration of result decoders.
Synopsis
- data Result a
- noResult :: Result ()
- rowsAffected :: Result Int64
- singleRow :: Row a -> Result a
- rowMaybe :: Row a -> Result (Maybe a)
- rowVector :: Row a -> Result (Vector a)
- rowList :: Row a -> Result [a]
- foldlRows :: (a -> b -> a) -> a -> Row b -> Result a
- foldrRows :: (b -> a -> a) -> a -> Row b -> Result a
- data Row a
- column :: NullableOrNot Value a -> Row a
- data NullableOrNot (decoder :: Type -> Type) a
- nonNullable :: decoder a -> NullableOrNot decoder a
- nullable :: decoder a -> NullableOrNot decoder (Maybe a)
- data Value a
- bool :: Value Bool
- int2 :: Value Int16
- int4 :: Value Int32
- int8 :: Value Int64
- float4 :: Value Float
- float8 :: Value Double
- numeric :: Value Scientific
- char :: Value Char
- text :: Value Text
- varchar :: Value Text
- bpchar :: Value Text
- bytea :: Value ByteString
- date :: Value Day
- timestamp :: Value LocalTime
- timestamptz :: Value UTCTime
- time :: Value TimeOfDay
- timetz :: Value (TimeOfDay, TimeZone)
- interval :: Value DiffTime
- uuid :: Value UUID
- inet :: Value IPRange
- macaddr :: Value (Word8, Word8, Word8, Word8, Word8, Word8)
- json :: Value Value
- jsonBytes :: (ByteString -> Either Text a) -> Value a
- jsonb :: Value Value
- jsonbBytes :: (ByteString -> Either Text a) -> Value a
- int4range :: Value (Range Int32)
- int8range :: Value (Range Int64)
- numrange :: Value (Range Scientific)
- tsrange :: Value (Range LocalTime)
- tstzrange :: Value (Range UTCTime)
- daterange :: Value (Range Day)
- int4multirange :: Value (Multirange Int32)
- int8multirange :: Value (Multirange Int64)
- nummultirange :: Value (Multirange Scientific)
- tsmultirange :: Value (Multirange LocalTime)
- tstzmultirange :: Value (Multirange UTCTime)
- datemultirange :: Value (Multirange Day)
- citext :: Value Text
- array :: Array a -> Value a
- listArray :: NullableOrNot Value element -> Value [element]
- vectorArray :: Vector vector element => NullableOrNot Value element -> Value (vector element)
- composite :: Maybe Text -> Text -> Composite a -> Value a
- record :: Composite a -> Value a
- hstore :: (forall (m :: Type -> Type). Monad m => Int -> m (Text, Maybe Text) -> m a) -> Value a
- enum :: Maybe Text -> Text -> (Text -> Maybe a) -> Value a
- custom :: Maybe Text -> Text -> Maybe (Word32, Word32) -> [(Maybe Text, Text)] -> (((Maybe Text, Text) -> (Word32, Word32)) -> ByteString -> Either Text a) -> Value a
- refine :: (a -> Either Text b) -> Value a -> Value b
- data Array a
- dimension :: (forall (m :: Type -> Type). Monad m => Int -> m a -> m b) -> Array a -> Array b
- element :: NullableOrNot Value a -> Array a
- data Composite a
- field :: NullableOrNot Value a -> Composite a
Result
Decoder of a query result.
noResult :: Result () Source #
Decode no value from the result.
Useful for statements like INSERT or CREATE.
rowsAffected :: Result Int64 Source #
Get the amount of rows affected by such statements as
UPDATE or DELETE.
singleRow :: Row a -> Result a Source #
Exactly one row.
Will raise the UnexpectedRowCountStatementError error if it's any other.
Specialized multi-row results
rowVector :: Row a -> Result (Vector a) Source #
Zero or more rows packed into the vector.
It's recommended to prefer this function to rowList,
since it performs notably better.
Multi-row traversers
Row
Decoder of an individual row, which gets composed of column value decoders. E.g.:
x ::Row(Maybe Int64, Text, TimeOfDay) x = (,,)<$>(column.nullable)int8<*>(column.nonNullable)text<*>(column.nonNullable)time
column :: NullableOrNot Value a -> Row a Source #
Lift an individual value decoder to a composable row decoder.
Nullability
data NullableOrNot (decoder :: Type -> Type) a Source #
Extensional specification of nullability over a generic decoder.
nonNullable :: decoder a -> NullableOrNot decoder a Source #
Specify that a decoder produces a non-nullable value.
nullable :: decoder a -> NullableOrNot decoder (Maybe a) Source #
Specify that a decoder produces a nullable value.
Value
numeric :: Value Scientific Source #
Decoder of the NUMERIC values.
bytea :: Value ByteString Source #
Decoder of the BYTEA values.
timestamptz :: Value UTCTime Source #
Decoder of the TIMESTAMPTZ values.
NOTICE
Postgres does not store the timezone information of TIMESTAMPTZ.
Instead it stores a UTC value and performs silent conversions
to the currently set timezone, when dealt with in the text format.
However this library bypasses the silent conversions
and communicates with Postgres using the UTC values directly.
macaddr :: Value (Word8, Word8, Word8, Word8, Word8, Word8) Source #
Decoder of the MACADDR values.
Represented as a 6-tuple of Word8 values in big endian order. If
you use ip library consider using it with fromOctets.
(\(a,b,c,d,e,f) -> fromOctets a b c d e f) <$> macaddr
jsonBytes :: (ByteString -> Either Text a) -> Value a Source #
Decoder of the JSON values into a raw JSON ByteString.
jsonbBytes :: (ByteString -> Either Text a) -> Value a Source #
Decoder of the JSONB values into a raw JSON ByteString.
int4multirange :: Value (Multirange Int32) Source #
Decoder of the INT4MULTIRANGE values.
int8multirange :: Value (Multirange Int64) Source #
Decoder of the INT8MULTIRANGE values.
nummultirange :: Value (Multirange Scientific) Source #
Decoder of the NUMMULTIRANGE values.
tsmultirange :: Value (Multirange LocalTime) Source #
Decoder of the TSMULTIRANGE values.
tstzmultirange :: Value (Multirange UTCTime) Source #
Decoder of the TSTZMULTIRANGE values.
datemultirange :: Value (Multirange Day) Source #
Decoder of the DATEMULTIRANGE values.
Decoder of the CITEXT values.
Requires the citext extension to be installed in the database.
listArray :: NullableOrNot Value element -> Value [element] Source #
Lift a value decoder of element into a unidimensional array decoder producing a list.
This function is merely a shortcut to the following expression:
(array.dimensionControl.Monad.replicateM.element)
Please notice that in case of multidimensional arrays nesting listArray decoder
won't work. You have to explicitly construct the array decoder using array.
vectorArray :: Vector vector element => NullableOrNot Value element -> Value (vector element) Source #
Lift a value decoder of element into a unidimensional array decoder producing a generic vector.
This function is merely a shortcut to the following expression:
(array.dimensionData.Vector.Generic.replicateM.element)
Please notice that in case of multidimensional arrays nesting vectorArray decoder
won't work. You have to explicitly construct the array decoder using array.
hstore :: (forall (m :: Type -> Type). Monad m => Int -> m (Text, Maybe Text) -> m a) -> Value a Source #
Binary generic decoder of HSTORE values.
Here's how you can use it to construct a specific value:
x :: Value [(Text, Maybe Text)]
x = hstore replicateM
Arguments
| :: Maybe Text | Schema name. |
| -> Text | Type name. |
| -> (Text -> Maybe a) | Mapping from text to value. |
| -> Value a |
Given a partial mapping from text to value, produces a decoder of that value for a named enum type.
Arguments
| :: Maybe Text | Schema name. |
| -> Text | Type name. |
| -> Maybe (Word32, Word32) | Possible static OIDs for the type. The first is for scalar values the second is for arrays. When unspecified, the OIDs will be automatically determined at runtime by looking up by name. |
| -> [(Maybe Text, Text)] | Other named types whose OIDs are needed for deserializing. E.g., when decoding composite types you can check the OIDs of its fields against the ones specified by Postgres. When any of the requested types is missing in the database an error will be raised upon the statement execution. |
| -> (((Maybe Text, Text) -> (Word32, Word32)) -> ByteString -> Either Text a) | Deserialization function in the context of resolved OIDs of the types requested in the previous parameter. It's safe to assume that all of the requested types will be present. In case you run the provided lookup function with unmentioned type names it will produce OID of 0 for them, standing for unknown type in Postgres. |
| -> Value a |
Low level API for defining custom value decoders.
refine :: (a -> Either Text b) -> Value a -> Value b Source #
Refine a value decoder, lifting the possible error to the session level.
Array
Binary generic array decoder.
Here's how you can use it to produce a specific array value decoder:
x ::Value[[Text]] x =array(dimensionreplicateM(dimensionreplicateM(element(nonNullabletext))))
dimension :: (forall (m :: Type -> Type). Monad m => Int -> m a -> m b) -> Array a -> Array b Source #
Binary function for parsing a dimension of an array. Provides support for multi-dimensional arrays.
Accepts:
- An implementation of the
replicateMfunction (Control.Monad.,replicateMData.Vector.), which determines the output value.replicateM - Binary decoder of its components, which can be either another
dimensionorelement.
Composite
Composable decoder of composite values (rows, records).