2019-05-20 19:24:27 +03:00
|
|
|
{-|
|
|
|
|
A DSL for declaration of query parameter encoders.
|
|
|
|
-}
|
|
|
|
module Hasql.Private.Encoders
|
|
|
|
where
|
|
|
|
|
|
|
|
import Hasql.Private.Prelude hiding (bool)
|
|
|
|
import qualified PostgreSQL.Binary.Encoding as A
|
|
|
|
import qualified PostgreSQL.Binary.Data as B
|
2021-11-02 10:26:35 +03:00
|
|
|
import qualified Text.Builder as C
|
2019-05-20 19:24:27 +03:00
|
|
|
import qualified Hasql.Private.Encoders.Params as Params
|
|
|
|
import qualified Hasql.Private.Encoders.Value as Value
|
|
|
|
import qualified Hasql.Private.Encoders.Array as Array
|
|
|
|
import qualified Hasql.Private.PTI as PTI
|
|
|
|
import qualified Hasql.Private.Prelude as Prelude
|
|
|
|
|
|
|
|
|
|
|
|
-- * Parameters Product Encoder
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
{-|
|
|
|
|
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 =
|
2019-05-21 13:18:20 +03:00
|
|
|
('fst' '>$<' 'param' ('nonNullable' 'int8')) '<>'
|
|
|
|
('snd' '>$<' 'param' ('nullable' 'text'))
|
2019-05-20 19:24:27 +03:00
|
|
|
@
|
|
|
|
|
|
|
|
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 =
|
2019-05-21 13:18:20 +03:00
|
|
|
'contrazip2' ('param' ('nonNullable' 'int8')) ('param' ('nullable' 'text'))
|
2019-05-20 19:24:27 +03:00
|
|
|
@
|
|
|
|
|
|
|
|
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 =
|
2019-05-21 13:18:20 +03:00
|
|
|
(name '>$<' 'param' ('nonNullable' 'text')) '<>'
|
|
|
|
(gender '>$<' 'param' ('nonNullable' genderValue)) '<>'
|
|
|
|
('fromIntegral' . age '>$<' 'param' ('nonNullable' 'int8'))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
2019-05-21 12:59:50 +03:00
|
|
|
genderValue :: 'Value' Gender
|
|
|
|
genderValue = 'enum' genderText 'text' where
|
2019-05-20 19:24:27 +03:00
|
|
|
genderText gender = case gender of
|
|
|
|
Male -> "male"
|
|
|
|
Female -> "female"
|
|
|
|
@
|
|
|
|
-}
|
|
|
|
newtype Params a = Params (Params.Params a)
|
|
|
|
deriving (Contravariant, Divisible, Decidable, Monoid, Semigroup)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
No parameters. Same as `mempty` and `conquered`.
|
|
|
|
-}
|
|
|
|
noParams :: Params ()
|
|
|
|
noParams = mempty
|
|
|
|
|
|
|
|
{-|
|
2019-05-21 12:59:50 +03:00
|
|
|
Lift a single parameter encoder, with its nullability specified,
|
|
|
|
associating it with a single placeholder.
|
2019-05-20 19:24:27 +03:00
|
|
|
-}
|
2019-05-21 12:59:50 +03:00
|
|
|
param :: NullableOrNot Value a -> Params a
|
2019-05-20 19:24:27 +03:00
|
|
|
param = \ case
|
2019-05-21 12:59:50 +03:00
|
|
|
NonNullable (Value valueEnc) -> Params (Params.value valueEnc)
|
|
|
|
Nullable (Value valueEnc) -> Params (Params.nullableValue valueEnc)
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- * Nullability
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
{-|
|
|
|
|
Extensional specification of nullability over a generic encoder.
|
|
|
|
-}
|
|
|
|
data NullableOrNot encoder a where
|
|
|
|
NonNullable :: encoder a -> NullableOrNot encoder a
|
|
|
|
Nullable :: encoder a -> NullableOrNot encoder (Maybe a)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
Specify that an encoder produces a non-nullable value.
|
|
|
|
-}
|
|
|
|
nonNullable :: encoder a -> NullableOrNot encoder a
|
|
|
|
nonNullable = NonNullable
|
|
|
|
|
|
|
|
{-|
|
|
|
|
Specify that an encoder produces a nullable value.
|
|
|
|
-}
|
|
|
|
nullable :: encoder a -> NullableOrNot encoder (Maybe a)
|
|
|
|
nullable = Nullable
|
|
|
|
|
|
|
|
|
2019-05-21 12:59:50 +03:00
|
|
|
-- * Value
|
2019-05-20 19:24:27 +03:00
|
|
|
-------------------------
|
|
|
|
|
|
|
|
{-|
|
2019-05-21 12:59:50 +03:00
|
|
|
Value encoder.
|
2019-05-20 19:24:27 +03:00
|
|
|
-}
|
2019-05-21 12:59:50 +03:00
|
|
|
newtype Value a = Value (Value.Value a)
|
2019-05-20 19:24:27 +03:00
|
|
|
deriving (Contravariant)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @BOOL@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE bool #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
bool :: Value Bool
|
|
|
|
bool = Value (Value.unsafePTIWithShow PTI.bool (const A.bool))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @INT2@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE int2 #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
int2 :: Value Int16
|
|
|
|
int2 = Value (Value.unsafePTIWithShow PTI.int2 (const A.int2_int16))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @INT4@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE int4 #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
int4 :: Value Int32
|
|
|
|
int4 = Value (Value.unsafePTIWithShow PTI.int4 (const A.int4_int32))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @INT8@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE int8 #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
int8 :: Value Int64
|
|
|
|
int8 = Value (Value.unsafePTIWithShow PTI.int8 (const A.int8_int64))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @FLOAT4@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE float4 #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
float4 :: Value Float
|
|
|
|
float4 = Value (Value.unsafePTIWithShow PTI.float4 (const A.float4))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @FLOAT8@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE float8 #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
float8 :: Value Double
|
|
|
|
float8 = Value (Value.unsafePTIWithShow PTI.float8 (const A.float8))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @NUMERIC@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE numeric #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
numeric :: Value B.Scientific
|
|
|
|
numeric = Value (Value.unsafePTIWithShow PTI.numeric (const A.numeric))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @CHAR@ values.
|
2019-05-22 21:50:59 +03:00
|
|
|
|
2019-05-26 18:00:26 +03:00
|
|
|
Note that it supports Unicode values and
|
2019-05-22 21:50:59 +03:00
|
|
|
identifies itself under the @TEXT@ OID because of that.
|
2019-05-20 19:24:27 +03:00
|
|
|
-}
|
|
|
|
{-# INLINABLE char #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
char :: Value Char
|
|
|
|
char = Value (Value.unsafePTIWithShow PTI.text (const A.char_utf8))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @TEXT@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE text #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
text :: Value Text
|
|
|
|
text = Value (Value.unsafePTIWithShow PTI.text (const A.text_strict))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @BYTEA@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE bytea #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
bytea :: Value ByteString
|
|
|
|
bytea = Value (Value.unsafePTIWithShow PTI.bytea (const A.bytea_strict))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @DATE@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE date #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
date :: Value B.Day
|
|
|
|
date = Value (Value.unsafePTIWithShow PTI.date (const A.date))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @TIMESTAMP@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE timestamp #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
timestamp :: Value B.LocalTime
|
|
|
|
timestamp = Value (Value.unsafePTIWithShow PTI.timestamp (Prelude.bool A.timestamp_float A.timestamp_int))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @TIMESTAMPTZ@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE timestamptz #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
timestamptz :: Value B.UTCTime
|
|
|
|
timestamptz = Value (Value.unsafePTIWithShow PTI.timestamptz (Prelude.bool A.timestamptz_float A.timestamptz_int))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @TIME@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE time #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
time :: Value B.TimeOfDay
|
|
|
|
time = Value (Value.unsafePTIWithShow PTI.time (Prelude.bool A.time_float A.time_int))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @TIMETZ@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE timetz #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
timetz :: Value (B.TimeOfDay, B.TimeZone)
|
|
|
|
timetz = Value (Value.unsafePTIWithShow PTI.timetz (Prelude.bool A.timetz_float A.timetz_int))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @INTERVAL@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE interval #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
interval :: Value B.DiffTime
|
|
|
|
interval = Value (Value.unsafePTIWithShow PTI.interval (Prelude.bool A.interval_float A.interval_int))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @UUID@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE uuid #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
uuid :: Value B.UUID
|
|
|
|
uuid = Value (Value.unsafePTIWithShow PTI.uuid (const A.uuid))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @INET@ values.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE inet #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
inet :: Value (B.NetAddr B.IP)
|
|
|
|
inet = Value (Value.unsafePTIWithShow PTI.inet (const A.inet))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @JSON@ values from JSON AST.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE json #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
json :: Value B.Value
|
|
|
|
json = Value (Value.unsafePTIWithShow PTI.json (const A.json_ast))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @JSON@ values from raw JSON.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE jsonBytes #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
jsonBytes :: Value ByteString
|
|
|
|
jsonBytes = Value (Value.unsafePTIWithShow PTI.json (const A.json_bytes))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @JSONB@ values from JSON AST.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE jsonb #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
jsonb :: Value B.Value
|
|
|
|
jsonb = Value (Value.unsafePTIWithShow PTI.jsonb (const A.jsonb_ast))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of @JSONB@ values from raw JSON.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE jsonbBytes #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
jsonbBytes :: Value ByteString
|
|
|
|
jsonbBytes = Value (Value.unsafePTIWithShow PTI.jsonb (const A.jsonb_bytes))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
Given a function,
|
|
|
|
which maps a value into a textual enum label used on the DB side,
|
|
|
|
produces an encoder of that value.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE enum #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
enum :: (a -> Text) -> Value a
|
|
|
|
enum mapping = Value (Value.unsafePTI PTI.text (const (A.text_strict . mapping)) (C.text . mapping))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
|
|
|
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
|
|
|
|
<https://www.postgresql.org/docs/10/static/protocol-overview.html#protocol-format-codes Formats and Format Codes>
|
|
|
|
section of the Postgres' documentation.
|
|
|
|
-}
|
|
|
|
{-# INLINABLE unknown #-}
|
2019-05-21 12:59:50 +03:00
|
|
|
unknown :: Value ByteString
|
|
|
|
unknown = Value (Value.unsafePTIWithShow PTI.unknown (const A.bytea_strict))
|
2019-05-20 19:24:27 +03:00
|
|
|
|
2019-05-21 21:20:31 +03:00
|
|
|
{-|
|
|
|
|
Lift an array encoder into a parameter encoder.
|
|
|
|
-}
|
|
|
|
array :: Array a -> Value a
|
|
|
|
array (Array (Array.Array valueOID arrayOID arrayEncoder renderer)) = let
|
|
|
|
encoder env input = A.array (PTI.oidWord32 valueOID) (arrayEncoder env input)
|
|
|
|
in Value (Value.Value arrayOID arrayOID encoder renderer)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
Lift a value encoder of element into a unidimensional array encoder of a foldable value.
|
|
|
|
|
2019-05-28 12:10:12 +03:00
|
|
|
This function is merely a shortcut to the following expression:
|
2019-05-21 21:20:31 +03:00
|
|
|
|
|
|
|
@
|
2019-05-28 12:10:12 +03:00
|
|
|
('array' . 'dimension' 'foldl'' . 'element')
|
2019-05-21 21:20:31 +03:00
|
|
|
@
|
|
|
|
|
2019-05-28 12:10:12 +03:00
|
|
|
You can use it like this:
|
2019-05-21 21:20:31 +03:00
|
|
|
|
|
|
|
@
|
2019-05-28 12:10:12 +03:00
|
|
|
vectorOfInts :: Value (Vector Int64)
|
|
|
|
vectorOfInts = 'foldableArray' ('nonNullable' 'int8')
|
2019-05-21 21:20:31 +03:00
|
|
|
@
|
2019-05-27 20:43:46 +03:00
|
|
|
|
|
|
|
Please notice that in case of multidimensional arrays nesting 'foldableArray' encoder
|
|
|
|
won't work. You have to explicitly construct the array encoder using 'array'.
|
2019-05-21 21:20:31 +03:00
|
|
|
-}
|
|
|
|
{-# INLINE foldableArray #-}
|
2019-05-21 21:22:42 +03:00
|
|
|
foldableArray :: Foldable foldable => NullableOrNot Value element -> Value (foldable element)
|
2019-05-21 21:20:31 +03:00
|
|
|
foldableArray = array . dimension foldl' . element
|
|
|
|
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
-- * Array
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
{-|
|
|
|
|
Generic array encoder.
|
|
|
|
|
|
|
|
Here's an example of its usage:
|
|
|
|
|
|
|
|
@
|
|
|
|
someParamsEncoder :: 'Params' [[Int64]]
|
2019-05-21 13:29:00 +03:00
|
|
|
someParamsEncoder = 'param' ('nonNullable' ('array' ('dimension' 'foldl'' ('dimension' 'foldl'' ('element' ('nonNullable' 'int8'))))))
|
2019-05-20 19:24:27 +03:00
|
|
|
@
|
|
|
|
|
2019-05-21 18:51:53 +03:00
|
|
|
Please note that the PostgreSQL @IN@ keyword does not accept an array, but rather a syntactical list of
|
2019-05-20 19:24:27 +03:00
|
|
|
values, thus this encoder is not suited for that. Use a @value = ANY($1)@ condition instead.
|
|
|
|
-}
|
|
|
|
newtype Array a = Array (Array.Array a)
|
2019-05-26 18:05:48 +03:00
|
|
|
deriving (Contravariant)
|
2019-05-20 19:24:27 +03:00
|
|
|
|
|
|
|
{-|
|
2019-05-21 12:59:50 +03:00
|
|
|
Lifts a 'Value' encoder into an 'Array' encoder.
|
2019-05-20 19:24:27 +03:00
|
|
|
-}
|
2019-05-21 12:59:50 +03:00
|
|
|
element :: NullableOrNot Value a -> Array a
|
2019-05-20 19:24:27 +03:00
|
|
|
element = \ case
|
2019-05-21 12:59:50 +03:00
|
|
|
NonNullable (Value (Value.Value elementOID arrayOID encoder renderer)) ->
|
2019-05-20 19:24:27 +03:00
|
|
|
Array (Array.value elementOID arrayOID encoder renderer)
|
2019-05-21 12:59:50 +03:00
|
|
|
Nullable (Value (Value.Value elementOID arrayOID encoder renderer)) ->
|
2019-05-20 19:24:27 +03:00
|
|
|
Array (Array.nullableValue elementOID arrayOID encoder renderer)
|
|
|
|
|
|
|
|
{-|
|
|
|
|
Encoder of an array dimension,
|
|
|
|
which thus provides support for multidimensional arrays.
|
|
|
|
|
|
|
|
Accepts:
|
|
|
|
|
|
|
|
* An implementation of the left-fold operation,
|
|
|
|
such as @Data.Foldable.'foldl''@,
|
|
|
|
which determines the input value.
|
|
|
|
|
2019-05-21 21:36:29 +03:00
|
|
|
* A component encoder, which can be either another 'dimension' or 'element'.
|
2019-05-20 19:24:27 +03:00
|
|
|
-}
|
|
|
|
{-# INLINABLE dimension #-}
|
|
|
|
dimension :: (forall a. (a -> b -> a) -> a -> c -> a) -> Array b -> Array c
|
|
|
|
dimension foldl (Array imp) = Array (Array.dimension foldl imp)
|