hasql/library/Hasql/Encoders/Params.hs

104 lines
3.5 KiB
Haskell
Raw Normal View History

module Hasql.Encoders.Params where
2015-11-08 21:09:42 +03:00
2024-04-19 07:38:30 +03:00
import Database.PostgreSQL.LibPQ qualified as A
import Hasql.Encoders.Value qualified as C
import Hasql.PostgresTypeInfo qualified as D
import Hasql.Prelude
2024-04-19 07:38:30 +03:00
import PostgreSQL.Binary.Encoding qualified as B
import Text.Builder qualified as E
2015-11-08 21:09:42 +03:00
renderReadable :: Params a -> a -> [Text]
renderReadable (Params _ _ _ printer) params =
printer params
& toList
2024-04-21 08:14:25 +03:00
compilePreparedStatementData :: Params a -> Bool -> a -> ([A.Oid], [Maybe (ByteString, A.Format)])
compilePreparedStatementData (Params _ columnsMetadata serializer _) integerDatetimes input =
(oidList, valueAndFormatList)
where
(oidList, formatList) =
columnsMetadata & toList & unzip
valueAndFormatList =
serializer integerDatetimes input
& toList
& zipWith (\format encoding -> (,format) <$> encoding) formatList
compileUnpreparedStatementData :: Params a -> Bool -> a -> [Maybe (A.Oid, ByteString, A.Format)]
compileUnpreparedStatementData (Params _ columnsMetadata serializer printer) integerDatetimes input =
zipWith
( \(oid, format) encoding ->
(,,) <$> pure oid <*> encoding <*> pure format
)
(toList columnsMetadata)
(toList (serializer integerDatetimes input))
2015-11-08 21:09:42 +03:00
-- |
2015-11-21 13:36:01 +03:00
-- Encoder of some representation of a parameters product.
2024-04-21 06:25:01 +03:00
data Params a = Params
{ size :: !Int,
columnsMetadata :: !(DList (A.Oid, A.Format)),
serializer :: Bool -> a -> DList (Maybe ByteString),
printer :: a -> DList Text
}
instance Contravariant Params where
contramap fn (Params size columnsMetadata oldSerializer oldPrinter) = Params {..}
where
serializer idt = oldSerializer idt . fn
printer = oldPrinter . fn
instance Divisible Params where
divide
divisor
(Params leftSize leftColumnsMetadata leftSerializer leftPrinter)
(Params rightSize rightColumnsMetadata rightSerializer rightPrinter) =
Params
{ size = leftSize + rightSize,
columnsMetadata = leftColumnsMetadata <> rightColumnsMetadata,
serializer = \idt input -> case divisor input of
(leftInput, rightInput) -> leftSerializer idt leftInput <> rightSerializer idt rightInput,
printer = \input -> case divisor input of
(leftInput, rightInput) -> leftPrinter leftInput <> rightPrinter rightInput
}
conquer =
Params
{ size = 0,
columnsMetadata = mempty,
serializer = mempty,
printer = mempty
}
instance Semigroup (Params a) where
Params leftSize leftColumnsMetadata leftSerializer leftPrinter <> Params rightSize rightColumnsMetadata rightSerializer rightPrinter =
Params
{ size = leftSize + rightSize,
columnsMetadata = leftColumnsMetadata <> rightColumnsMetadata,
serializer = \idt input -> leftSerializer idt input <> rightSerializer idt input,
printer = \input -> leftPrinter input <> rightPrinter input
}
instance Monoid (Params a) where
mempty = conquer
2016-02-23 09:39:57 +03:00
2017-03-20 23:13:21 +03:00
value :: C.Value a -> Params a
2024-04-21 06:25:01 +03:00
value (C.Value valueOID _ serialize print) =
Params
{ size = 1,
columnsMetadata = pure (pqOid, format),
serializer = \idt -> pure . Just . B.encodingBytes . serialize idt,
printer = pure . E.run . print
}
where
D.OID _ pqOid format = valueOID
2015-11-08 21:09:42 +03:00
2017-03-20 23:13:21 +03:00
nullableValue :: C.Value a -> Params (Maybe a)
2024-04-21 06:25:01 +03:00
nullableValue (C.Value valueOID _ serialize print) =
2023-10-13 02:24:12 +03:00
Params
2024-04-21 06:25:01 +03:00
{ size = 1,
columnsMetadata = pure (pqOid, format),
serializer = \idt -> pure . fmap (B.encodingBytes . serialize idt),
printer = pure . maybe "null" (E.run . print)
}
where
D.OID _ pqOid format = valueOID