Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
A DSL for declaration of statement parameter encoders.
For compactness of names all the types defined here imply being an encoder.
E.g., the Array
type is an encoder of arrays, not the data-structure itself.
Synopsis
- data Params a
- noParams :: Params ()
- param :: NullableOrNot Value a -> Params a
- data NullableOrNot encoder a
- nonNullable :: encoder a -> NullableOrNot encoder a
- nullable :: encoder a -> NullableOrNot encoder (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
- inet :: Value (NetAddr IP)
- json :: Value Value
- jsonBytes :: Value ByteString
- jsonLazyBytes :: Value ByteString
- jsonb :: Value Value
- jsonbBytes :: Value ByteString
- jsonbLazyBytes :: Value ByteString
- enum :: (a -> Text) -> Value a
- unknown :: Value ByteString
- array :: Array a -> Value a
- foldableArray :: Foldable foldable => NullableOrNot Value element -> Value (foldable element)
- composite :: Composite a -> Value a
- data Array a
- element :: NullableOrNot Value a -> Array a
- dimension :: (forall a. (a -> b -> a) -> a -> c -> a) -> Array b -> Array c
- data Composite a
- field :: NullableOrNot Value a -> Composite a
Parameters product
Encoder of some representation of a 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 = (fst
>$<
param
(nonNullable
int8
))<>
(snd
>$<
param
(nullable
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
(param
(nonNullable
int8
)) (param
(nullable
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 = (name>$<
param
(nonNullable
text
))<>
(gender>$<
param
(nonNullable
genderValue))<>
(fromIntegral
. age>$<
param
(nonNullable
int8
)) genderValue ::Value
Gender genderValue =enum
genderTexttext
where genderText gender = case gender of Male -> "male" Female -> "female"
param :: NullableOrNot Value a -> Params a Source #
Lift a single parameter encoder, with its nullability specified, associating it with a single placeholder.
Nullability
data NullableOrNot encoder a Source #
Extensional specification of nullability over a generic encoder.
nonNullable :: encoder a -> NullableOrNot encoder a Source #
Specify that an encoder produces a non-nullable value.
nullable :: encoder a -> NullableOrNot encoder (Maybe a) Source #
Specify that an encoder produces a nullable value.
Value
Value encoder.
numeric :: Value Scientific Source #
Encoder of NUMERIC
values.
Encoder of CHAR
values.
Note that it supports Unicode 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.
jsonBytes :: Value ByteString Source #
Encoder of JSON
values from raw JSON.
jsonLazyBytes :: Value ByteString Source #
Encoder of JSON
values from raw JSON as lazy ByteString.
jsonbBytes :: Value ByteString Source #
Encoder of JSONB
values from raw JSON.
jsonbLazyBytes :: Value ByteString Source #
Encoder of JSONB
values from raw JSON as lazy ByteString.
enum :: (a -> Text) -> Value a Source #
Given a function, which maps a value into a textual enum label used on the DB side, produces an encoder of that value.
unknown :: Value ByteString Source #
Identifies the value with the PostgreSQL's "unknown" type, thus leaving it up to Postgres to infer the actual type of the value.
The value transimitted is any value encoded in the Postgres' Text data format. For reference, see the Formats and Format Codes section of the Postgres' documentation.
Warning: Do not use this as part of composite encoders like array
since
it is the only encoder that doesn't use the binary format.
foldableArray :: Foldable foldable => NullableOrNot Value element -> Value (foldable element) Source #
Lift a value encoder of element into a unidimensional array encoder of a foldable value.
This function is merely a shortcut to the following expression:
(array
.dimension
foldl'
.element
)
You can use it like this:
vectorOfInts :: Value (Vector Int64) vectorOfInts =foldableArray
(nonNullable
int8
)
Please notice that in case of multidimensional arrays nesting foldableArray
encoder
won't work. You have to explicitly construct the array encoder using array
.
Array
Generic array encoder.
Here's an example of its usage:
someParamsEncoder ::Params
[[Int64]] someParamsEncoder =param
(nonNullable
(array
(dimension
foldl'
(dimension
foldl'
(element
(nonNullable
int8
))))))
Please note that the PostgreSQL IN
keyword does not accept an array, but rather a syntactical list of
values, thus this encoder is not suited for that. Use a value = ANY($1)
condition instead.