Safe Haskell | None |
---|---|
Language | Haskell2010 |
A DSL for declaration of query parameter encoders.
- data Params a
- unit :: Params ()
- value :: Value a -> Params a
- nullableValue :: Value a -> Params (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
- 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
- json :: Value Value
- array :: Array a -> Value a
- enum :: (a -> Text) -> Value a
- data Array a
- arrayValue :: Value a -> Array a
- arrayNullableValue :: Value a -> Array (Maybe a)
- arrayDimension :: (forall a. (a -> b -> a) -> a -> c -> a) -> Array b -> Array c
Params
Encoder of some representation of the parameters product.
Has instances of Contravariant
, Divisible
and Monoid
,
which you can use to compose multiple parameters together.
E.g.,
someParamsEncoder ::Params
(Int64, Maybe Text) someParamsEncoder =contramap
fst
(value
int8
)<>
contramap
snd
(nullableValue
text
)
As a general solution for tuples of any arity, instead of fst
and snd
,
consider the functions of the contrazip
family
from the "contravariant-extras" package.
E.g., here's how you can achieve the same as the above:
someParamsEncoder ::Params
(Int64, Maybe Text) someParamsEncoder =contrazip2
(value
int8
) (nullableValue
text
)
Here's how you can implement encoders for custom composite types:
data Person = Person { name :: Text, gender :: Gender, age :: Int } data Gender = Male | Female personParams ::Params
Person personParams =contramap
name (value
text
)<>
contramap
gender (value
genderValue)<>
contramap
(fromIntegral . age) (value
int8
) genderValue ::Value
Gender genderValue =contramap
genderTexttext
where genderText gender = case gender of Male -> "male" Female -> "female"
Divisible Params Source | |
Contravariant Params Source | |
Monoid (Params a) Source | |
Default (Params ()) Source | Maps to |
(Default (Value a1), Default (Value a2)) => Default (Params (a1, a2)) Source | |
(Default (Value a1), Default (Value a2), Default (Value a3)) => Default (Params (a1, a2, a3)) Source | |
(Default (Value a1), Default (Value a2), Default (Value a3), Default (Value a4)) => Default (Params (a1, a2, a3, a4)) Source | |
(Default (Value a1), Default (Value a2), Default (Value a3), Default (Value a4), Default (Value a5)) => Default (Params (a1, a2, a3, a4, a5)) Source | |
Default (Value a) => Default (Params (Identity a)) Source |
nullableValue :: Value a -> Params (Maybe a) Source
Lift an individual nullable value encoder to a parameters encoder.
Value
An individual value encoder. Will be mapped to a single placeholder in the query.
Contravariant Value Source | |
Default (Value Bool) Source | Maps to |
Default (Value Char) Source | Maps to |
Default (Value Double) Source | Maps to |
Default (Value Float) Source | Maps to |
Default (Value Int16) Source | Maps to |
Default (Value Int32) Source | Maps to |
Default (Value Int64) Source | Maps to |
Default (Value (TimeOfDay, TimeZone)) Source | Maps to |
Default (Value ByteString) Source | Maps to |
Default (Value Scientific) Source | Maps to |
Default (Value Text) Source | Maps to |
Default (Value UTCTime) Source | Maps to |
Default (Value Value) Source | Maps to |
Default (Value UUID) Source | Maps to |
Default (Value DiffTime) Source | Maps to |
Default (Value Day) Source | Maps to |
Default (Value TimeOfDay) Source | Maps to |
Default (Value LocalTime) Source | Maps to |
numeric :: Value Scientific Source
Encoder of NUMERIC
values.
Encoder of CHAR
values.
Note that it supports UTF-8 values and
identifies itself under the TEXT
OID because of that.
bytea :: Value ByteString Source
Encoder of BYTEA
values.
timestamptz :: Value UTCTime Source
Encoder of TIMESTAMPTZ
values.
enum :: (a -> Text) -> Value a Source
Given a function, which maps the value into the textual enum label from the DB side, produces a encoder of that value.
Array
A generic array encoder.
Here's an example of its usage:
x :: Value [[Int64]] x = array (arrayDimension foldl' (arrayDimension foldl' (arrayValue int8)))
arrayValue :: Value a -> Array a Source
arrayNullableValue :: Value a -> Array (Maybe a) Source
arrayDimension :: (forall a. (a -> b -> a) -> a -> c -> a) -> Array b -> Array c Source
An encoder of an array dimension, which thus provides support for multidimensional arrays.
Accepts:
- An implementation of the left-fold operation,
such as
Data.Foldable.
, which determines the input value.foldl'
- A component encoder, which can be either another
arrayDimension
,arrayValue
orarrayNullableValue
.