module Hasql.Encoding
(
Params,
unit,
value,
nullableValue,
Value,
bool,
int2,
int4,
int8,
float4,
float8,
numeric,
char,
text,
bytea,
date,
timestamp,
timestamptz,
time,
timetz,
interval,
uuid,
json,
array,
enum,
Array,
arrayValue,
arrayNullableValue,
arrayDimension,
)
where
import Hasql.Prelude hiding (bool)
import qualified PostgreSQL.Binary.Encoder as Encoder
import qualified Data.Aeson as Aeson
import qualified Hasql.Encoding.Params as Params
import qualified Hasql.Encoding.Value as Value
import qualified Hasql.Encoding.Array as Array
import qualified Hasql.PTI as PTI
import qualified Hasql.Prelude as Prelude
newtype Params a =
Params (Params.Params a)
deriving (Contravariant, Divisible, Monoid)
unit :: Params ()
unit =
Params mempty
value :: Value a -> Params a
value (Value x) =
Params (Params.value x)
nullableValue :: Value a -> Params (Maybe a)
nullableValue (Value x) =
Params (Params.nullableValue x)
instance Default (Params ()) where
def =
unit
instance Default (Value a) => Default (Params (Identity a)) where
def =
contramap runIdentity (value def)
instance (Default (Value a1), Default (Value a2)) => Default (Params (a1, a2)) where
def =
contrazip2 (value def) (value def)
instance (Default (Value a1), Default (Value a2), Default (Value a3)) => Default (Params (a1, a2, a3)) where
def =
contrazip3 (value def) (value def) (value def)
instance (Default (Value a1), Default (Value a2), Default (Value a3), Default (Value a4)) => Default (Params (a1, a2, a3, a4)) where
def =
contrazip4 (value def) (value def) (value def) (value def)
instance (Default (Value a1), Default (Value a2), Default (Value a3), Default (Value a4), Default (Value a5)) => Default (Params (a1, a2, a3, a4, a5)) where
def =
contrazip5 (value def) (value def) (value def) (value def) (value def)
newtype Value a =
Value (Value.Value a)
deriving (Contravariant)
bool :: Value Bool
bool =
Value (Value.unsafePTI PTI.bool (const Encoder.bool))
int2 :: Value Int16
int2 =
Value (Value.unsafePTI PTI.int2 (const Encoder.int2_int16))
int4 :: Value Int32
int4 =
Value (Value.unsafePTI PTI.int4 (const Encoder.int4_int32))
int8 :: Value Int64
int8 =
Value (Value.unsafePTI PTI.int8 (const Encoder.int8_int64))
float4 :: Value Float
float4 =
Value (Value.unsafePTI PTI.float4 (const Encoder.float4))
float8 :: Value Double
float8 =
Value (Value.unsafePTI PTI.float8 (const Encoder.float8))
numeric :: Value Scientific
numeric =
Value (Value.unsafePTI PTI.numeric (const Encoder.numeric))
char :: Value Char
char =
Value (Value.unsafePTI PTI.text (const Encoder.char))
text :: Value Text
text =
Value (Value.unsafePTI PTI.text (const Encoder.text_strict))
bytea :: Value ByteString
bytea =
Value (Value.unsafePTI PTI.bytea (const Encoder.bytea_strict))
date :: Value Day
date =
Value (Value.unsafePTI PTI.date (const Encoder.date))
timestamp :: Value LocalTime
timestamp =
Value (Value.unsafePTI PTI.timestamp (Prelude.bool Encoder.timestamp_int Encoder.timestamp_float))
timestamptz :: Value UTCTime
timestamptz =
Value (Value.unsafePTI PTI.timestamptz (Prelude.bool Encoder.timestamptz_int Encoder.timestamptz_float))
time :: Value TimeOfDay
time =
Value (Value.unsafePTI PTI.time (Prelude.bool Encoder.time_int Encoder.time_float))
timetz :: Value (TimeOfDay, TimeZone)
timetz =
Value (Value.unsafePTI PTI.timetz (Prelude.bool Encoder.timetz_int Encoder.timetz_float))
interval :: Value DiffTime
interval =
Value (Value.unsafePTI PTI.interval (Prelude.bool Encoder.interval_int Encoder.interval_float))
uuid :: Value UUID
uuid =
Value (Value.unsafePTI PTI.uuid (const Encoder.uuid))
json :: Value Aeson.Value
json =
Value (Value.unsafePTI PTI.json (const Encoder.json))
array :: Array a -> Value a
array (Array imp) =
Array.run imp & \(arrayOID, encoder') ->
Value (Value.Value arrayOID arrayOID encoder')
enum :: (a -> Text) -> Value a
enum mapping =
Value (Value.unsafePTI PTI.text (const (Encoder.enum mapping)))
instance Default (Value Bool) where
def =
bool
instance Default (Value Int16) where
def =
int2
instance Default (Value Int32) where
def =
int4
instance Default (Value Int64) where
def =
int8
instance Default (Value Float) where
def =
float4
instance Default (Value Double) where
def =
float8
instance Default (Value Scientific) where
def =
numeric
instance Default (Value Char) where
def =
char
instance Default (Value Text) where
def =
text
instance Default (Value ByteString) where
def =
bytea
instance Default (Value Day) where
def =
date
instance Default (Value LocalTime) where
def =
timestamp
instance Default (Value UTCTime) where
def =
timestamptz
instance Default (Value TimeOfDay) where
def =
time
instance Default (Value (TimeOfDay, TimeZone)) where
def =
timetz
instance Default (Value DiffTime) where
def =
interval
instance Default (Value UUID) where
def =
uuid
instance Default (Value Aeson.Value) where
def =
json
newtype Array a =
Array (Array.Array a)
arrayValue :: Value a -> Array a
arrayValue (Value (Value.Value elementOID arrayOID encoder')) =
Array (Array.value elementOID arrayOID encoder')
arrayNullableValue :: Value a -> Array (Maybe a)
arrayNullableValue (Value (Value.Value elementOID arrayOID encoder')) =
Array (Array.nullableValue elementOID arrayOID encoder')
arrayDimension :: (forall a. (a -> b -> a) -> a -> c -> a) -> Array b -> Array c
arrayDimension foldl (Array imp) =
Array (Array.dimension foldl imp)