hasql/library/Hasql/Encoders/Params.hs

32 lines
969 B
Haskell
Raw Normal View History

module Hasql.Encoders.Params where
2015-11-08 21:09:42 +03:00
2017-03-20 23:13:21 +03:00
import qualified Database.PostgreSQL.LibPQ as A
import qualified Hasql.Encoders.Value as C
2024-04-19 07:34:33 +03:00
import qualified Hasql.PostgresTypeInfo as D
import Hasql.Prelude
2022-06-20 13:54:54 +03:00
import qualified PostgreSQL.Binary.Encoding as B
import qualified Text.Builder as E
2015-11-08 21:09:42 +03:00
-- |
2015-11-21 13:36:01 +03:00
-- Encoder of some representation of a parameters product.
2022-06-20 13:54:54 +03:00
newtype Params a
= Params (Op (DList (A.Oid, A.Format, Bool -> Maybe ByteString, Text)) a)
2018-06-06 12:23:18 +03:00
deriving (Contravariant, Divisible, Decidable, Semigroup, Monoid)
2016-02-23 09:39:57 +03:00
2017-03-20 23:13:21 +03:00
value :: C.Value a -> Params a
2015-11-08 21:09:42 +03:00
value =
contramap Just . nullableValue
2017-03-20 23:13:21 +03:00
nullableValue :: C.Value a -> Params (Maybe a)
nullableValue (C.Value valueOID arrayOID encode render) =
2023-10-13 02:24:12 +03:00
Params
$ Op
$ \input ->
2022-06-20 13:54:54 +03:00
let D.OID _ pqOid format =
valueOID
encoder env =
fmap (B.encodingBytes . encode env) input
rendering =
maybe "null" (E.run . render) input
in pure (pqOid, format, encoder, rendering)