module Hasql.Private.Encoders.Value where

import Hasql.Private.Prelude
import qualified PostgreSQL.Binary.Encoding as B
import qualified Hasql.Private.PTI as PTI
import qualified Text.Builder as C


data Value a =
  Value PTI.OID PTI.OID (Bool -> a -> B.Encoding) (a -> C.Builder)

instance Contravariant Value where
  {-# INLINE contramap #-}
  contramap :: (a -> b) -> Value b -> Value a
contramap a -> b
f (Value OID
valueOID OID
arrayOID Bool -> b -> Encoding
encode b -> Builder
render) =
    OID -> OID -> (Bool -> a -> Encoding) -> (a -> Builder) -> Value a
forall a.
OID -> OID -> (Bool -> a -> Encoding) -> (a -> Builder) -> Value a
Value OID
valueOID OID
arrayOID (\Bool
integerDatetimes a
input -> Bool -> b -> Encoding
encode Bool
integerDatetimes (a -> b
f a
input)) (b -> Builder
render (b -> Builder) -> (a -> b) -> a -> Builder
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> b
f)

{-# INLINE unsafePTI #-}
unsafePTI :: PTI.PTI -> (Bool -> a -> B.Encoding) -> (a -> C.Builder) -> Value a
unsafePTI :: PTI -> (Bool -> a -> Encoding) -> (a -> Builder) -> Value a
unsafePTI PTI
pti =
  OID -> OID -> (Bool -> a -> Encoding) -> (a -> Builder) -> Value a
forall a.
OID -> OID -> (Bool -> a -> Encoding) -> (a -> Builder) -> Value a
Value (PTI -> OID
PTI.ptiOID PTI
pti) (OID -> Maybe OID -> OID
forall a. a -> Maybe a -> a
fromMaybe ([Char] -> OID
forall a. HasCallStack => [Char] -> a
error [Char]
"No array OID") (PTI -> Maybe OID
PTI.ptiArrayOID PTI
pti))

{-# INLINE unsafePTIWithShow #-}
unsafePTIWithShow :: Show a => PTI.PTI -> (Bool -> a -> B.Encoding) -> Value a
unsafePTIWithShow :: PTI -> (Bool -> a -> Encoding) -> Value a
unsafePTIWithShow PTI
pti Bool -> a -> Encoding
encode =
  PTI -> (Bool -> a -> Encoding) -> (a -> Builder) -> Value a
forall a.
PTI -> (Bool -> a -> Encoding) -> (a -> Builder) -> Value a
unsafePTI PTI
pti Bool -> a -> Encoding
encode ([Char] -> Builder
C.string ([Char] -> Builder) -> (a -> [Char]) -> a -> Builder
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. a -> [Char]
forall a. Show a => a -> [Char]
show)