module Hasql.Encoders
(
Params,
unit,
param,
nullableParam,
Value,
bool,
int2,
int4,
int8,
float4,
float8,
numeric,
char,
text,
bytea,
date,
timestamp,
timestamptz,
time,
timetz,
interval,
uuid,
inet,
json,
jsonBytes,
jsonb,
jsonbBytes,
array,
enum,
unknown,
Array,
element,
nullableElement,
dimension,
)
where
import Hasql.Private.Prelude hiding (bool)
import qualified PostgreSQL.Binary.Encoding as A
import qualified PostgreSQL.Binary.Data as B
import qualified Text.Builder as C
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
newtype Params a =
Params (Params.Params a)
deriving (Contravariant, Divisible, Decidable, Monoid, Semigroup)
{-# INLINABLE unit #-}
unit :: Params ()
unit =
Params mempty
{-# INLINABLE param #-}
param :: Value a -> Params a
param (Value x) =
Params (Params.value x)
{-# INLINABLE nullableParam #-}
nullableParam :: Value a -> Params (Maybe a)
nullableParam (Value x) =
Params (Params.nullableValue x)
instance Default (Params ()) where
{-# INLINE def #-}
def =
unit
instance Default (Value a) => Default (Params (Identity a)) where
{-# INLINE def #-}
def =
contramap runIdentity (param def)
instance (Default (Value a1), Default (Value a2)) => Default (Params (a1, a2)) where
{-# INLINE def #-}
def =
contrazip2 (param def) (param def)
instance (Default (Value a1), Default (Value a2), Default (Value a3)) => Default (Params (a1, a2, a3)) where
{-# INLINE def #-}
def =
contrazip3 (param def) (param def) (param def)
instance (Default (Value a1), Default (Value a2), Default (Value a3), Default (Value a4)) => Default (Params (a1, a2, a3, a4)) where
{-# INLINE def #-}
def =
contrazip4 (param def) (param def) (param def) (param 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
{-# INLINE def #-}
def =
contrazip5 (param def) (param def) (param def) (param def) (param def)
newtype Value a =
Value (Value.Value a)
deriving (Contravariant)
{-# INLINABLE bool #-}
bool :: Value Bool
bool =
Value (Value.unsafePTIWithShow PTI.bool (const A.bool))
{-# INLINABLE int2 #-}
int2 :: Value Int16
int2 =
Value (Value.unsafePTIWithShow PTI.int2 (const A.int2_int16))
{-# INLINABLE int4 #-}
int4 :: Value Int32
int4 =
Value (Value.unsafePTIWithShow PTI.int4 (const A.int4_int32))
{-# INLINABLE int8 #-}
int8 :: Value Int64
int8 =
Value (Value.unsafePTIWithShow PTI.int8 (const A.int8_int64))
{-# INLINABLE float4 #-}
float4 :: Value Float
float4 =
Value (Value.unsafePTIWithShow PTI.float4 (const A.float4))
{-# INLINABLE float8 #-}
float8 :: Value Double
float8 =
Value (Value.unsafePTIWithShow PTI.float8 (const A.float8))
{-# INLINABLE numeric #-}
numeric :: Value B.Scientific
numeric =
Value (Value.unsafePTIWithShow PTI.numeric (const A.numeric))
{-# INLINABLE char #-}
char :: Value Char
char =
Value (Value.unsafePTIWithShow PTI.text (const A.char_utf8))
{-# INLINABLE text #-}
text :: Value Text
text =
Value (Value.unsafePTIWithShow PTI.text (const A.text_strict))
{-# INLINABLE bytea #-}
bytea :: Value ByteString
bytea =
Value (Value.unsafePTIWithShow PTI.bytea (const A.bytea_strict))
{-# INLINABLE date #-}
date :: Value B.Day
date =
Value (Value.unsafePTIWithShow PTI.date (const A.date))
{-# INLINABLE timestamp #-}
timestamp :: Value B.LocalTime
timestamp =
Value (Value.unsafePTIWithShow PTI.timestamp (Prelude.bool A.timestamp_float A.timestamp_int))
{-# INLINABLE timestamptz #-}
timestamptz :: Value B.UTCTime
timestamptz =
Value (Value.unsafePTIWithShow PTI.timestamptz (Prelude.bool A.timestamptz_float A.timestamptz_int))
{-# INLINABLE time #-}
time :: Value B.TimeOfDay
time =
Value (Value.unsafePTIWithShow PTI.time (Prelude.bool A.time_float A.time_int))
{-# INLINABLE timetz #-}
timetz :: Value (B.TimeOfDay, B.TimeZone)
timetz =
Value (Value.unsafePTIWithShow PTI.timetz (Prelude.bool A.timetz_float A.timetz_int))
{-# INLINABLE interval #-}
interval :: Value B.DiffTime
interval =
Value (Value.unsafePTIWithShow PTI.interval (Prelude.bool A.interval_float A.interval_int))
{-# INLINABLE uuid #-}
uuid :: Value B.UUID
uuid =
Value (Value.unsafePTIWithShow PTI.uuid (const A.uuid))
{-# INLINABLE inet #-}
inet :: Value (B.NetAddr B.IP)
inet =
Value (Value.unsafePTIWithShow PTI.inet (const A.inet))
{-# INLINABLE json #-}
json :: Value B.Value
json =
Value (Value.unsafePTIWithShow PTI.json (const A.json_ast))
{-# INLINABLE jsonBytes #-}
jsonBytes :: Value ByteString
jsonBytes =
Value (Value.unsafePTIWithShow PTI.json (const A.json_bytes))
{-# INLINABLE jsonb #-}
jsonb :: Value B.Value
jsonb =
Value (Value.unsafePTIWithShow PTI.jsonb (const A.jsonb_ast))
{-# INLINABLE jsonbBytes #-}
jsonbBytes :: Value ByteString
jsonbBytes =
Value (Value.unsafePTIWithShow PTI.jsonb (const A.jsonb_bytes))
{-# INLINABLE array #-}
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)
{-# INLINABLE enum #-}
enum :: (a -> Text) -> Value a
enum mapping =
Value (Value.unsafePTI PTI.text (const (A.text_strict . mapping)) (C.text . mapping))
{-# INLINABLE unknown #-}
unknown :: Value ByteString
unknown =
Value (Value.unsafePTIWithShow PTI.unknown (const A.bytea_strict))
instance Default (Value Bool) where
{-# INLINE def #-}
def =
bool
instance Default (Value Int16) where
{-# INLINE def #-}
def =
int2
instance Default (Value Int32) where
{-# INLINE def #-}
def =
int4
instance Default (Value Int64) where
{-# INLINE def #-}
def =
int8
instance Default (Value Float) where
{-# INLINE def #-}
def =
float4
instance Default (Value Double) where
{-# INLINE def #-}
def =
float8
instance Default (Value B.Scientific) where
{-# INLINE def #-}
def =
numeric
instance Default (Value Char) where
{-# INLINE def #-}
def =
char
instance Default (Value Text) where
{-# INLINE def #-}
def =
text
instance Default (Value ByteString) where
{-# INLINE def #-}
def =
bytea
instance Default (Value B.Day) where
{-# INLINE def #-}
def =
date
instance Default (Value B.LocalTime) where
{-# INLINE def #-}
def =
timestamp
instance Default (Value B.UTCTime) where
{-# INLINE def #-}
def =
timestamptz
instance Default (Value B.TimeOfDay) where
{-# INLINE def #-}
def =
time
instance Default (Value (B.TimeOfDay, B.TimeZone)) where
{-# INLINE def #-}
def =
timetz
instance Default (Value B.DiffTime) where
{-# INLINE def #-}
def =
interval
instance Default (Value B.UUID) where
{-# INLINE def #-}
def =
uuid
instance Default (Value B.Value) where
{-# INLINE def #-}
def =
json
newtype Array a =
Array (Array.Array a)
{-# INLINABLE element #-}
element :: Value a -> Array a
element (Value (Value.Value elementOID arrayOID encoder renderer)) =
Array (Array.value elementOID arrayOID encoder renderer)
{-# INLINABLE nullableElement #-}
nullableElement :: Value a -> Array (Maybe a)
nullableElement (Value (Value.Value elementOID arrayOID encoder renderer)) =
Array (Array.nullableValue elementOID arrayOID encoder renderer)
{-# INLINABLE dimension #-}
dimension :: (forall a. (a -> b -> a) -> a -> c -> a) -> Array b -> Array c
dimension foldl (Array imp) =
Array (Array.dimension foldl imp)