module Hasql.Codecs.Encoders.Value where

import ByteString.StrictBuilder qualified
import Data.Aeson qualified as Aeson
import Data.ByteString.Lazy qualified as LazyByteString
import Data.HashMap.Strict qualified as HashMap
import Data.HashSet qualified as HashSet
import Data.IP qualified as Iproute
import Hasql.Codecs.TypeInfo qualified as TypeInfo
import Hasql.Platform.Prelude
import PostgreSQL.Binary.Encoding qualified as Binary
import PostgreSQL.Binary.Range qualified as Range
import TextBuilder qualified as TextBuilder

-- |
-- Value encoder.
data Value a
  = Value
      -- | Schema name, if any.
      (Maybe Text)
      -- | Type name.
      Text
      -- | Statically known OID for the type.
      -- When unspecified, the OID may be determined at runtime by looking up by name.
      (Maybe Word32)
      -- | Statically known OID for the array-type with this type as the element.
      -- When unspecified, the OID may be determined at runtime by looking up by name.
      -- It may also mean that there may be no array type containing this type, which is the case in attempts to double-nest arrays.
      (Maybe Word32)
      -- | Dimensionality. If 0 then it is not an array, but a scalar value.
      Word
      -- | Text format?
      Bool
      -- | Names of types that are not known statically and must be looked up at runtime collected from the nested composite and array encoders.
      (HashSet (Maybe Text, Text))
      -- | Serialization function on the resolved OIDs.
      (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Binary.Encoding)
      -- | Render function for error messages.
      (a -> TextBuilder.TextBuilder)

instance Contravariant Value where
  {-# INLINE contramap #-}
  contramap :: forall a' a. (a' -> a) -> Value a -> Value a'
contramap a' -> a
f (Value Maybe Text
schemaName Text
typeName Maybe Word32
valueOid Maybe Word32
arrayOid Word
dimensionality Bool
textFormat HashSet (Maybe Text, Text)
unknownTypes HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding
encode a -> TextBuilder
render) =
    Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a' -> Encoding)
-> (a' -> TextBuilder)
-> Value a'
forall a.
Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
Value Maybe Text
schemaName Text
typeName Maybe Word32
valueOid Maybe Word32
arrayOid Word
dimensionality Bool
textFormat HashSet (Maybe Text, Text)
unknownTypes (\HashMap (Maybe Text, Text) (Word32, Word32)
hashMap -> HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding
encode HashMap (Maybe Text, Text) (Word32, Word32)
hashMap (a -> Encoding) -> (a' -> a) -> a' -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a' -> a
f) (a -> TextBuilder
render (a -> TextBuilder) -> (a' -> a) -> a' -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a' -> a
f)

{-# INLINE primitive #-}
primitive :: Text -> Bool -> TypeInfo.TypeInfo -> (a -> Binary.Encoding) -> (a -> TextBuilder.TextBuilder) -> Value a
primitive :: forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
typeName Bool
isText TypeInfo
typeInfo a -> Encoding
encode a -> TextBuilder
render =
  Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
forall a.
Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
Value
    Maybe Text
forall a. Maybe a
Nothing
    Text
typeName
    (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just (TypeInfo -> Word32
TypeInfo.toBaseOid TypeInfo
typeInfo))
    (Word32 -> Maybe Word32
forall a. a -> Maybe a
Just (TypeInfo -> Word32
TypeInfo.toArrayOid TypeInfo
typeInfo))
    Word
0
    Bool
isText
    HashSet (Maybe Text, Text)
forall a. HashSet a
HashSet.empty
    ((a -> Encoding)
-> HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding
forall a b. a -> b -> a
const a -> Encoding
encode)
    a -> TextBuilder
render

-- |
-- Encoder of @BOOL@ values.
{-# INLINEABLE bool #-}
bool :: Value Bool
bool :: Value Bool
bool = Text
-> Bool
-> TypeInfo
-> (Bool -> Encoding)
-> (Bool -> TextBuilder)
-> Value Bool
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"bool" Bool
False TypeInfo
TypeInfo.bool Bool -> Encoding
Binary.bool (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (Bool -> String) -> Bool -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Bool -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INT2@ values.
{-# INLINEABLE int2 #-}
int2 :: Value Int16
int2 :: Value Int16
int2 = Text
-> Bool
-> TypeInfo
-> (Int16 -> Encoding)
-> (Int16 -> TextBuilder)
-> Value Int16
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"int2" Bool
False TypeInfo
TypeInfo.int2 Int16 -> Encoding
Binary.int2_int16 (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Int16 -> String) -> Int16 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int16 -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INT4@ values.
{-# INLINEABLE int4 #-}
int4 :: Value Int32
int4 :: Value Int32
int4 = Text
-> Bool
-> TypeInfo
-> (Int32 -> Encoding)
-> (Int32 -> TextBuilder)
-> Value Int32
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"int4" Bool
False TypeInfo
TypeInfo.int4 Int32 -> Encoding
Binary.int4_int32 (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Int32 -> String) -> Int32 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int32 -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INT8@ values.
{-# INLINEABLE int8 #-}
int8 :: Value Int64
int8 :: Value Int64
int8 = Text
-> Bool
-> TypeInfo
-> (Int64 -> Encoding)
-> (Int64 -> TextBuilder)
-> Value Int64
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"int8" Bool
False TypeInfo
TypeInfo.int8 Int64 -> Encoding
Binary.int8_int64 (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Int64 -> String) -> Int64 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int64 -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @FLOAT4@ values.
{-# INLINEABLE float4 #-}
float4 :: Value Float
float4 :: Value Float
float4 = Text
-> Bool
-> TypeInfo
-> (Float -> Encoding)
-> (Float -> TextBuilder)
-> Value Float
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"float4" Bool
False TypeInfo
TypeInfo.float4 Float -> Encoding
Binary.float4 (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Float -> String) -> Float -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Float -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @FLOAT8@ values.
{-# INLINEABLE float8 #-}
float8 :: Value Double
float8 :: Value Double
float8 = Text
-> Bool
-> TypeInfo
-> (Double -> Encoding)
-> (Double -> TextBuilder)
-> Value Double
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"float8" Bool
False TypeInfo
TypeInfo.float8 Double -> Encoding
Binary.float8 (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Double -> String) -> Double -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Double -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @NUMERIC@ values.
{-# INLINEABLE numeric #-}
numeric :: Value Scientific
numeric :: Value Scientific
numeric = Text
-> Bool
-> TypeInfo
-> (Scientific -> Encoding)
-> (Scientific -> TextBuilder)
-> Value Scientific
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"numeric" Bool
False TypeInfo
TypeInfo.numeric Scientific -> Encoding
Binary.numeric (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Scientific -> String) -> Scientific -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Scientific -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @CHAR@ values.
--
-- Note that it supports Unicode values and
-- identifies itself under the @TEXT@ OID because of that.
{-# INLINEABLE char #-}
char :: Value Char
char :: Value Char
char = Text
-> Bool
-> TypeInfo
-> (Char -> Encoding)
-> (Char -> TextBuilder)
-> Value Char
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"char" Bool
False TypeInfo
TypeInfo.text Char -> Encoding
Binary.char_utf8 (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (Char -> String) -> Char -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Char -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TEXT@ values.
{-# INLINEABLE text #-}
text :: Value Text
text :: Value Text
text = Text
-> Bool
-> TypeInfo
-> (Text -> Encoding)
-> (Text -> TextBuilder)
-> Value Text
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"text" Bool
False TypeInfo
TypeInfo.text Text -> Encoding
Binary.text_strict (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (Text -> String) -> Text -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @VARCHAR@ values.
{-# INLINEABLE varchar #-}
varchar :: Value Text
varchar :: Value Text
varchar = Text
-> Bool
-> TypeInfo
-> (Text -> Encoding)
-> (Text -> TextBuilder)
-> Value Text
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"varchar" Bool
False TypeInfo
TypeInfo.varchar Text -> Encoding
Binary.text_strict (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (Text -> String) -> Text -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @BPCHAR@ or @CHAR(n)@, @CHARACTER(n)@ values.
{-# INLINEABLE bpchar #-}
bpchar :: Value Text
bpchar :: Value Text
bpchar = Text
-> Bool
-> TypeInfo
-> (Text -> Encoding)
-> (Text -> TextBuilder)
-> Value Text
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"bpchar" Bool
False TypeInfo
TypeInfo.bpchar Text -> Encoding
Binary.text_strict (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (Text -> String) -> Text -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @BYTEA@ values.
{-# INLINEABLE bytea #-}
bytea :: Value ByteString
bytea :: Value ByteString
bytea = Text
-> Bool
-> TypeInfo
-> (ByteString -> Encoding)
-> (ByteString -> TextBuilder)
-> Value ByteString
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"bytea" Bool
False TypeInfo
TypeInfo.bytea ByteString -> Encoding
Binary.bytea_strict (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (ByteString -> String) -> ByteString -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @DATE@ values.
{-# INLINEABLE date #-}
date :: Value Day
date :: Value Day
date = Text
-> Bool
-> TypeInfo
-> (Day -> Encoding)
-> (Day -> TextBuilder)
-> Value Day
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"date" Bool
False TypeInfo
TypeInfo.date Day -> Encoding
Binary.date (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (Day -> String) -> Day -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Day -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TIMESTAMP@ values.
{-# INLINEABLE timestamp #-}
timestamp :: Value LocalTime
timestamp :: Value LocalTime
timestamp = Text
-> Bool
-> TypeInfo
-> (LocalTime -> Encoding)
-> (LocalTime -> TextBuilder)
-> Value LocalTime
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"timestamp" Bool
False TypeInfo
TypeInfo.timestamp LocalTime -> Encoding
Binary.timestamp_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (LocalTime -> String) -> LocalTime -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. LocalTime -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TIMESTAMPTZ@ values.
{-# INLINEABLE timestamptz #-}
timestamptz :: Value UTCTime
timestamptz :: Value UTCTime
timestamptz = Text
-> Bool
-> TypeInfo
-> (UTCTime -> Encoding)
-> (UTCTime -> TextBuilder)
-> Value UTCTime
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"timestamptz" Bool
False TypeInfo
TypeInfo.timestamptz UTCTime -> Encoding
Binary.timestamptz_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (UTCTime -> String) -> UTCTime -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. UTCTime -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TIME@ values.
{-# INLINEABLE time #-}
time :: Value TimeOfDay
time :: Value TimeOfDay
time = Text
-> Bool
-> TypeInfo
-> (TimeOfDay -> Encoding)
-> (TimeOfDay -> TextBuilder)
-> Value TimeOfDay
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"time" Bool
False TypeInfo
TypeInfo.time TimeOfDay -> Encoding
Binary.time_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (TimeOfDay -> String) -> TimeOfDay -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. TimeOfDay -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TIMETZ@ values.
{-# INLINEABLE timetz #-}
timetz :: Value (TimeOfDay, TimeZone)
timetz :: Value (TimeOfDay, TimeZone)
timetz = Text
-> Bool
-> TypeInfo
-> ((TimeOfDay, TimeZone) -> Encoding)
-> ((TimeOfDay, TimeZone) -> TextBuilder)
-> Value (TimeOfDay, TimeZone)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"timetz" Bool
False TypeInfo
TypeInfo.timetz (TimeOfDay, TimeZone) -> Encoding
Binary.timetz_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> ((TimeOfDay, TimeZone) -> String)
-> (TimeOfDay, TimeZone)
-> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (TimeOfDay, TimeZone) -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INTERVAL@ values.
{-# INLINEABLE interval #-}
interval :: Value DiffTime
interval :: Value DiffTime
interval = Text
-> Bool
-> TypeInfo
-> (DiffTime -> Encoding)
-> (DiffTime -> TextBuilder)
-> Value DiffTime
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"interval" Bool
False TypeInfo
TypeInfo.interval DiffTime -> Encoding
Binary.interval_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (DiffTime -> String) -> DiffTime -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. DiffTime -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @UUID@ values.
{-# INLINEABLE uuid #-}
uuid :: Value UUID
uuid :: Value UUID
uuid = Text
-> Bool
-> TypeInfo
-> (UUID -> Encoding)
-> (UUID -> TextBuilder)
-> Value UUID
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"uuid" Bool
False TypeInfo
TypeInfo.uuid UUID -> Encoding
Binary.uuid (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (UUID -> String) -> UUID -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. UUID -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INET@ values.
{-# INLINEABLE inet #-}
inet :: Value Iproute.IPRange
inet :: Value IPRange
inet = Text
-> Bool
-> TypeInfo
-> (IPRange -> Encoding)
-> (IPRange -> TextBuilder)
-> Value IPRange
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"inet" Bool
False TypeInfo
TypeInfo.inet IPRange -> Encoding
Binary.inet (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (IPRange -> String) -> IPRange -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IPRange -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @MACADDR@ values.
--
-- Represented as a 6-tuple of Word8 values in big endian order. If
-- you use `ip` library you can convert using its `toOctets`.
--
-- > toOctets >$< macaddr
{-# INLINEABLE macaddr #-}
macaddr :: Value (Word8, Word8, Word8, Word8, Word8, Word8)
macaddr :: Value (Word8, Word8, Word8, Word8, Word8, Word8)
macaddr = Text
-> Bool
-> TypeInfo
-> ((Word8, Word8, Word8, Word8, Word8, Word8) -> Encoding)
-> ((Word8, Word8, Word8, Word8, Word8, Word8) -> TextBuilder)
-> Value (Word8, Word8, Word8, Word8, Word8, Word8)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"macaddr" Bool
False TypeInfo
TypeInfo.macaddr (Word8, Word8, Word8, Word8, Word8, Word8) -> Encoding
Binary.macaddr (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> ((Word8, Word8, Word8, Word8, Word8, Word8) -> String)
-> (Word8, Word8, Word8, Word8, Word8, Word8)
-> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. (Word8, Word8, Word8, Word8, Word8, Word8) -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @JSON@ values from JSON AST.
{-# INLINEABLE json #-}
json :: Value Aeson.Value
json :: Value Value
json = Text
-> Bool
-> TypeInfo
-> (Value -> Encoding)
-> (Value -> TextBuilder)
-> Value Value
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"json" Bool
False TypeInfo
TypeInfo.json Value -> Encoding
Binary.json_ast (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Value -> String) -> Value -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Value -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @JSON@ values from raw JSON.
{-# INLINEABLE jsonBytes #-}
jsonBytes :: Value ByteString
jsonBytes :: Value ByteString
jsonBytes = Text
-> Bool
-> TypeInfo
-> (ByteString -> Encoding)
-> (ByteString -> TextBuilder)
-> Value ByteString
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"json" Bool
False TypeInfo
TypeInfo.json ByteString -> Encoding
Binary.json_bytes (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (ByteString -> String) -> ByteString -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @JSON@ values from raw JSON as lazy ByteString.
{-# INLINEABLE jsonLazyBytes #-}
jsonLazyBytes :: Value LazyByteString.ByteString
jsonLazyBytes :: Value ByteString
jsonLazyBytes = Text
-> Bool
-> TypeInfo
-> (ByteString -> Encoding)
-> (ByteString -> TextBuilder)
-> Value ByteString
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"json" Bool
False TypeInfo
TypeInfo.json ByteString -> Encoding
Binary.json_bytes_lazy (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (ByteString -> String) -> ByteString -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @JSONB@ values from JSON AST.
{-# INLINEABLE jsonb #-}
jsonb :: Value Aeson.Value
jsonb :: Value Value
jsonb = Text
-> Bool
-> TypeInfo
-> (Value -> Encoding)
-> (Value -> TextBuilder)
-> Value Value
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"jsonb" Bool
False TypeInfo
TypeInfo.jsonb Value -> Encoding
Binary.jsonb_ast (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Value -> String) -> Value -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Value -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @JSONB@ values from raw JSON.
{-# INLINEABLE jsonbBytes #-}
jsonbBytes :: Value ByteString
jsonbBytes :: Value ByteString
jsonbBytes = Text
-> Bool
-> TypeInfo
-> (ByteString -> Encoding)
-> (ByteString -> TextBuilder)
-> Value ByteString
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"jsonb" Bool
False TypeInfo
TypeInfo.jsonb ByteString -> Encoding
Binary.jsonb_bytes (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (ByteString -> String) -> ByteString -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @JSONB@ values from raw JSON as lazy ByteString.
{-# INLINEABLE jsonbLazyBytes #-}
jsonbLazyBytes :: Value LazyByteString.ByteString
jsonbLazyBytes :: Value ByteString
jsonbLazyBytes = Text
-> Bool
-> TypeInfo
-> (ByteString -> Encoding)
-> (ByteString -> TextBuilder)
-> Value ByteString
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"jsonb" Bool
False TypeInfo
TypeInfo.jsonb ByteString -> Encoding
Binary.jsonb_bytes_lazy (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (ByteString -> String) -> ByteString -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @OID@ values.
{-# INLINEABLE oid #-}
oid :: Value Int32
oid :: Value Int32
oid = Text
-> Bool
-> TypeInfo
-> (Int32 -> Encoding)
-> (Int32 -> TextBuilder)
-> Value Int32
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"oid" Bool
False TypeInfo
TypeInfo.oid Int32 -> Encoding
Binary.int4_int32 (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Int32 -> String) -> Int32 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int32 -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @NAME@ values.
{-# INLINEABLE name #-}
name :: Value Text
name :: Value Text
name = Text
-> Bool
-> TypeInfo
-> (Text -> Encoding)
-> (Text -> TextBuilder)
-> Value Text
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"name" Bool
False TypeInfo
TypeInfo.name Text -> Encoding
Binary.text_strict (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (Text -> String) -> Text -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INT4RANGE@ values.
{-# INLINEABLE int4range #-}
int4range :: Value (Range.Range Int32)
int4range :: Value (Range Int32)
int4range = Text
-> Bool
-> TypeInfo
-> (Range Int32 -> Encoding)
-> (Range Int32 -> TextBuilder)
-> Value (Range Int32)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"int4range" Bool
False TypeInfo
TypeInfo.int4range Range Int32 -> Encoding
Binary.int4range (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Range Int32 -> String) -> Range Int32 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Range Int32 -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INT8RANGE@ values.
{-# INLINEABLE int8range #-}
int8range :: Value (Range.Range Int64)
int8range :: Value (Range Int64)
int8range = Text
-> Bool
-> TypeInfo
-> (Range Int64 -> Encoding)
-> (Range Int64 -> TextBuilder)
-> Value (Range Int64)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"int8range" Bool
False TypeInfo
TypeInfo.int8range Range Int64 -> Encoding
Binary.int8range (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Range Int64 -> String) -> Range Int64 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Range Int64 -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @NUMRANGE@ values.
{-# INLINEABLE numrange #-}
numrange :: Value (Range.Range Scientific)
numrange :: Value (Range Scientific)
numrange = Text
-> Bool
-> TypeInfo
-> (Range Scientific -> Encoding)
-> (Range Scientific -> TextBuilder)
-> Value (Range Scientific)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"numrange" Bool
False TypeInfo
TypeInfo.numrange Range Scientific -> Encoding
Binary.numrange (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Range Scientific -> String) -> Range Scientific -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Range Scientific -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TSRANGE@ values.
{-# INLINEABLE tsrange #-}
tsrange :: Value (Range.Range LocalTime)
tsrange :: Value (Range LocalTime)
tsrange = Text
-> Bool
-> TypeInfo
-> (Range LocalTime -> Encoding)
-> (Range LocalTime -> TextBuilder)
-> Value (Range LocalTime)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"tsrange" Bool
False TypeInfo
TypeInfo.tsrange Range LocalTime -> Encoding
Binary.tsrange_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Range LocalTime -> String) -> Range LocalTime -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Range LocalTime -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TSTZRANGE@ values.
{-# INLINEABLE tstzrange #-}
tstzrange :: Value (Range.Range UTCTime)
tstzrange :: Value (Range UTCTime)
tstzrange = Text
-> Bool
-> TypeInfo
-> (Range UTCTime -> Encoding)
-> (Range UTCTime -> TextBuilder)
-> Value (Range UTCTime)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"tstzrange" Bool
False TypeInfo
TypeInfo.tstzrange Range UTCTime -> Encoding
Binary.tstzrange_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Range UTCTime -> String) -> Range UTCTime -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Range UTCTime -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @DATERANGE@ values.
{-# INLINEABLE daterange #-}
daterange :: Value (Range.Range Day)
daterange :: Value (Range Day)
daterange = Text
-> Bool
-> TypeInfo
-> (Range Day -> Encoding)
-> (Range Day -> TextBuilder)
-> Value (Range Day)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"daterange" Bool
False TypeInfo
TypeInfo.daterange Range Day -> Encoding
Binary.daterange (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Range Day -> String) -> Range Day -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Range Day -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INT4MULTIRANGE@ values.
{-# INLINEABLE int4multirange #-}
int4multirange :: Value (Range.Multirange Int32)
int4multirange :: Value (Multirange Int32)
int4multirange = Text
-> Bool
-> TypeInfo
-> (Multirange Int32 -> Encoding)
-> (Multirange Int32 -> TextBuilder)
-> Value (Multirange Int32)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"int4multirange" Bool
False TypeInfo
TypeInfo.int4multirange Multirange Int32 -> Encoding
Binary.int4multirange (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Multirange Int32 -> String) -> Multirange Int32 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Multirange Int32 -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @INT8MULTIRANGE@ values.
{-# INLINEABLE int8multirange #-}
int8multirange :: Value (Range.Multirange Int64)
int8multirange :: Value (Multirange Int64)
int8multirange = Text
-> Bool
-> TypeInfo
-> (Multirange Int64 -> Encoding)
-> (Multirange Int64 -> TextBuilder)
-> Value (Multirange Int64)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"int8multirange" Bool
False TypeInfo
TypeInfo.int8multirange Multirange Int64 -> Encoding
Binary.int8multirange (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Multirange Int64 -> String) -> Multirange Int64 -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Multirange Int64 -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @NUMMULTIRANGE@ values.
{-# INLINEABLE nummultirange #-}
nummultirange :: Value (Range.Multirange Scientific)
nummultirange :: Value (Multirange Scientific)
nummultirange = Text
-> Bool
-> TypeInfo
-> (Multirange Scientific -> Encoding)
-> (Multirange Scientific -> TextBuilder)
-> Value (Multirange Scientific)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"nummultirange" Bool
False TypeInfo
TypeInfo.nummultirange Multirange Scientific -> Encoding
Binary.nummultirange (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Multirange Scientific -> String)
-> Multirange Scientific
-> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Multirange Scientific -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TSMULTIRANGE@ values.
{-# INLINEABLE tsmultirange #-}
tsmultirange :: Value (Range.Multirange LocalTime)
tsmultirange :: Value (Multirange LocalTime)
tsmultirange = Text
-> Bool
-> TypeInfo
-> (Multirange LocalTime -> Encoding)
-> (Multirange LocalTime -> TextBuilder)
-> Value (Multirange LocalTime)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"tsmultirange" Bool
False TypeInfo
TypeInfo.tsmultirange Multirange LocalTime -> Encoding
Binary.tsmultirange_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Multirange LocalTime -> String)
-> Multirange LocalTime
-> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Multirange LocalTime -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @TSTZMULTIRANGE@ values.
{-# INLINEABLE tstzmultirange #-}
tstzmultirange :: Value (Range.Multirange UTCTime)
tstzmultirange :: Value (Multirange UTCTime)
tstzmultirange = Text
-> Bool
-> TypeInfo
-> (Multirange UTCTime -> Encoding)
-> (Multirange UTCTime -> TextBuilder)
-> Value (Multirange UTCTime)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"tstzmultirange" Bool
False TypeInfo
TypeInfo.tstzmultirange Multirange UTCTime -> Encoding
Binary.tstzmultirange_int (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Multirange UTCTime -> String)
-> Multirange UTCTime
-> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Multirange UTCTime -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @DATEMULTIRANGE@ values.
{-# INLINEABLE datemultirange #-}
datemultirange :: Value (Range.Multirange Day)
datemultirange :: Value (Multirange Day)
datemultirange = Text
-> Bool
-> TypeInfo
-> (Multirange Day -> Encoding)
-> (Multirange Day -> TextBuilder)
-> Value (Multirange Day)
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"datemultirange" Bool
False TypeInfo
TypeInfo.datemultirange Multirange Day -> Encoding
Binary.datemultirange (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (Multirange Day -> String) -> Multirange Day -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Multirange Day -> String
forall a. Show a => a -> String
show)

-- |
-- Encoder of @CITEXT@ values.
--
-- Requires the @citext@ extension to be installed in the database.
{-# INLINEABLE citext #-}
citext :: Value Text
citext :: Value Text
citext =
  Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32)
    -> Text -> Encoding)
-> (Text -> TextBuilder)
-> Value Text
forall a.
Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
Value
    Maybe Text
forall a. Maybe a
Nothing
    Text
"citext"
    Maybe Word32
forall a. Maybe a
Nothing
    Maybe Word32
forall a. Maybe a
Nothing
    Word
0
    Bool
False
    HashSet (Maybe Text, Text)
forall a. HashSet a
HashSet.empty
    ((Text -> Encoding)
-> HashMap (Maybe Text, Text) (Word32, Word32) -> Text -> Encoding
forall a b. a -> b -> a
const Text -> Encoding
Binary.text_strict)
    (String -> TextBuilder
TextBuilder.string (String -> TextBuilder) -> (Text -> String) -> Text -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Text -> String
forall a. Show a => a -> String
show)

-- |
-- Given a function which maps a value into a textual enum label used on the DB side,
-- produces an encoder of that value for a named enum type.
{-# INLINEABLE enum #-}
enum ::
  -- | Schema name where the enum type is defined.
  Maybe Text ->
  -- | Enum type name.
  Text ->
  -- | Mapping function from value to enum label.
  (a -> Text) ->
  Value a
enum :: forall a. Maybe Text -> Text -> (a -> Text) -> Value a
enum Maybe Text
schemaName Text
typeName a -> Text
mapping =
  Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
forall a.
Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
Value
    Maybe Text
schemaName
    Text
typeName
    Maybe Word32
forall a. Maybe a
Nothing
    Maybe Word32
forall a. Maybe a
Nothing
    Word
0
    Bool
False
    ((Maybe Text, Text) -> HashSet (Maybe Text, Text)
forall a. Hashable a => a -> HashSet a
HashSet.singleton (Maybe Text
schemaName, Text
typeName))
    ((a -> Encoding)
-> HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding
forall a b. a -> b -> a
const (Text -> Encoding
Binary.text_strict (Text -> Encoding) -> (a -> Text) -> a -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Text
mapping))
    (Text -> TextBuilder
TextBuilder.text (Text -> TextBuilder) -> (a -> Text) -> a -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Text
mapping)

-- |
-- 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 transmitted 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.
--
-- __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.
{-# DEPRECATED unknown "Use 'custom' instead." #-}
{-# INLINEABLE unknown #-}
unknown :: Value ByteString
unknown :: Value ByteString
unknown = Text
-> Bool
-> TypeInfo
-> (ByteString -> Encoding)
-> (ByteString -> TextBuilder)
-> Value ByteString
forall a.
Text
-> Bool
-> TypeInfo
-> (a -> Encoding)
-> (a -> TextBuilder)
-> Value a
primitive Text
"unknown" Bool
True TypeInfo
TypeInfo.unknown ByteString -> Encoding
Binary.bytea_strict (String -> TextBuilder
TextBuilder.string (String -> TextBuilder)
-> (ByteString -> String) -> ByteString -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> String
forall a. Show a => a -> String
show)

-- |
-- Low level API for defining custom value encoders.
{-# INLINEABLE custom #-}
custom ::
  -- | Schema name where the type is defined.
  Maybe Text ->
  -- | Type name.
  Text ->
  -- | Possible static OIDs for the type. The first is for scalar values the second is for arrays.
  --
  -- When unspecified, the OIDs will be automatically determined at runtime by looking up by name.
  Maybe (Word32, Word32) ->
  -- | Other named types whose OIDs are needed for serializing.
  --
  -- E.g., when encoding composite types Postgres requires specifying OIDs of all of its fields.
  --
  -- When any of the requested types is missing in the database an error will be raised upon the statement execution.
  [(Maybe Text, Text)] ->
  -- | Serialization function in the context of resolved OIDs of the types requested in the previous parameter.
  --
  -- It's safe to assume that all of the requested types will be present.
  -- In case you run the provided lookup function with unmentioned type names it will produce OID of 0 for them, standing for unknown type in Postgres.
  ( ((Maybe Text, Text) -> (Word32, Word32)) ->
    a ->
    ByteString
  ) ->
  -- | Render function for error messages.
  (a -> Text) ->
  Value a
custom :: forall a.
Maybe Text
-> Text
-> Maybe (Word32, Word32)
-> [(Maybe Text, Text)]
-> (((Maybe Text, Text) -> (Word32, Word32)) -> a -> ByteString)
-> (a -> Text)
-> Value a
custom Maybe Text
schemaName Text
typeName Maybe (Word32, Word32)
staticOids [(Maybe Text, Text)]
requiredTypes ((Maybe Text, Text) -> (Word32, Word32)) -> a -> ByteString
encode a -> Text
render =
  Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
forall a.
Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
Value
    Maybe Text
schemaName
    Text
typeName
    ((Word32, Word32) -> Word32
forall a b. (a, b) -> a
fst ((Word32, Word32) -> Word32)
-> Maybe (Word32, Word32) -> Maybe Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Word32, Word32)
staticOids)
    ((Word32, Word32) -> Word32
forall a b. (a, b) -> b
snd ((Word32, Word32) -> Word32)
-> Maybe (Word32, Word32) -> Maybe Word32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Word32, Word32)
staticOids)
    Word
0
    Bool
False
    ([(Maybe Text, Text)] -> HashSet (Maybe Text, Text)
forall a. (Eq a, Hashable a) => [a] -> HashSet a
HashSet.fromList [(Maybe Text, Text)]
requiredTypes)
    ( \HashMap (Maybe Text, Text) (Word32, Word32)
hashMap ->
        ByteString -> Encoding
ByteString.StrictBuilder.bytes (ByteString -> Encoding) -> (a -> ByteString) -> a -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((Maybe Text, Text) -> (Word32, Word32)) -> a -> ByteString
encode ((Word32, Word32) -> Maybe (Word32, Word32) -> (Word32, Word32)
forall a. a -> Maybe a -> a
fromMaybe (Word32
0, Word32
0) (Maybe (Word32, Word32) -> (Word32, Word32))
-> ((Maybe Text, Text) -> Maybe (Word32, Word32))
-> (Maybe Text, Text)
-> (Word32, Word32)
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ((Maybe Text, Text)
 -> HashMap (Maybe Text, Text) (Word32, Word32)
 -> Maybe (Word32, Word32))
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> (Maybe Text, Text)
-> Maybe (Word32, Word32)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (Maybe Text, Text)
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> Maybe (Word32, Word32)
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
HashMap.lookup HashMap (Maybe Text, Text) (Word32, Word32)
hashMap)
    )
    (Text -> TextBuilder
TextBuilder.text (Text -> TextBuilder) -> (a -> Text) -> a -> TextBuilder
forall b c a. (b -> c) -> (a -> b) -> a -> c
forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> Text
render)

-- |
-- Encoder of @HSTORE@ values from a foldable container of key-value pairs.
--
-- The value part can be @Nothing@ to represent a NULL value in the hstore.
{-# INLINEABLE hstore #-}
hstore :: (Foldable foldable) => Value (foldable (Text, Maybe Text))
hstore :: forall (foldable :: * -> *).
Foldable foldable =>
Value (foldable (Text, Maybe Text))
hstore =
  Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32)
    -> foldable (Text, Maybe Text) -> Encoding)
-> (foldable (Text, Maybe Text) -> TextBuilder)
-> Value (foldable (Text, Maybe Text))
forall a.
Maybe Text
-> Text
-> Maybe Word32
-> Maybe Word32
-> Word
-> Bool
-> HashSet (Maybe Text, Text)
-> (HashMap (Maybe Text, Text) (Word32, Word32) -> a -> Encoding)
-> (a -> TextBuilder)
-> Value a
Value
    Maybe Text
forall a. Maybe a
Nothing
    Text
"hstore"
    Maybe Word32
forall a. Maybe a
Nothing
    Maybe Word32
forall a. Maybe a
Nothing
    Word
0
    Bool
False
    HashSet (Maybe Text, Text)
forall a. HashSet a
HashSet.empty
    ((foldable (Text, Maybe Text) -> Encoding)
-> HashMap (Maybe Text, Text) (Word32, Word32)
-> foldable (Text, Maybe Text)
-> Encoding
forall a b. a -> b -> a
const foldable (Text, Maybe Text) -> Encoding
forall (foldable :: * -> *).
Foldable foldable =>
foldable (Text, Maybe Text) -> Encoding
Binary.hStore_foldable)
    foldable (Text, Maybe Text) -> TextBuilder
forall {t :: * -> *} {a} {a}.
(Foldable t, Show a, Show a) =>
t (a, Maybe a) -> TextBuilder
renderHstore
  where
    renderHstore :: t (a, Maybe a) -> TextBuilder
renderHstore t (a, Maybe a)
items =
      [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat
        [ Text -> TextBuilder
TextBuilder.text Text
"hstore(",
          TextBuilder -> [TextBuilder] -> TextBuilder
forall (f :: * -> *).
Foldable f =>
TextBuilder -> f TextBuilder -> TextBuilder
TextBuilder.intercalate TextBuilder
", " (((a, Maybe a) -> [TextBuilder]) -> t (a, Maybe a) -> [TextBuilder]
forall m a. Monoid m => (a -> m) -> t a -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(a, Maybe a)
pair -> [(a, Maybe a) -> TextBuilder
forall {a} {a}. (Show a, Show a) => (a, Maybe a) -> TextBuilder
renderPair (a, Maybe a)
pair]) t (a, Maybe a)
items),
          Text -> TextBuilder
TextBuilder.text Text
")"
        ]
    renderPair :: (a, Maybe a) -> TextBuilder
renderPair (a
key, Maybe a
maybeValue) =
      [TextBuilder] -> TextBuilder
forall a. Monoid a => [a] -> a
mconcat
        [ Text -> TextBuilder
TextBuilder.text Text
"(",
          String -> TextBuilder
TextBuilder.string (a -> String
forall a. Show a => a -> String
show a
key),
          Text -> TextBuilder
TextBuilder.text Text
", ",
          case Maybe a
maybeValue of
            Maybe a
Nothing -> Text -> TextBuilder
TextBuilder.text Text
"NULL"
            Just a
value -> String -> TextBuilder
TextBuilder.string (a -> String
forall a. Show a => a -> String
show a
value),
          Text -> TextBuilder
TextBuilder.text Text
")"
        ]