2016-08-06 12:22:11 +03:00
|
|
|
module Hasql.Private.Encoders.Params where
|
2015-11-08 21:09:42 +03:00
|
|
|
|
2016-08-06 12:22:11 +03:00
|
|
|
import Hasql.Private.Prelude
|
2017-03-20 23:13:21 +03:00
|
|
|
import qualified Database.PostgreSQL.LibPQ as A
|
|
|
|
import qualified PostgreSQL.Binary.Encoding as B
|
|
|
|
import qualified Hasql.Private.Encoders.Value as C
|
|
|
|
import qualified Hasql.Private.PTI as D
|
2021-10-22 04:13:52 +03:00
|
|
|
import qualified TextBuilder 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.
|
2015-11-08 21:09:42 +03:00
|
|
|
newtype Params a =
|
2018-05-23 12:15:34 +03:00
|
|
|
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)
|
2018-05-23 12:15:34 +03:00
|
|
|
nullableValue (C.Value valueOID arrayOID encode render) =
|
|
|
|
Params $ Op $ \ input ->
|
|
|
|
let
|
|
|
|
D.OID _ pqOid format =
|
|
|
|
valueOID
|
|
|
|
encoder env =
|
|
|
|
fmap (B.encodingBytes . encode env) input
|
|
|
|
rendering =
|
2021-10-22 04:13:52 +03:00
|
|
|
maybe "null" (E.buildText . render) input
|
2018-05-23 12:15:34 +03:00
|
|
|
in pure (pqOid, format, encoder, rendering)
|