Something went wrong. Try again.
Compile-time SQL datatype generation from PostgreSQL for Haskell
Something went wrong. Try again.
4.8 kB · 158 lines
Haskell
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159module Hasql.Generate.Codec ( PgCodec (..) ) where
----------------------------------------------------------------------------------------------------
import qualified Data.Aeson as Aesonimport Data.Bool ( Bool )import Data.ByteString ( ByteString )import Data.Char ( Char )import Data.Functor ( fmap )import qualified Data.Functor.Contravariant as Contravariantimport Data.Int ( Int16, Int32, Int64 )import Data.Scientific ( Scientific )import Data.String ( String )import Data.Text ( Text )import qualified Data.Textimport Data.Time ( Day , DiffTime , LocalTime , TimeOfDay , TimeZone , UTCTime )import Data.UUID ( UUID )
import qualified Hasql.Decoders as Decodersimport qualified Hasql.Encoders as Encoders
import Prelude ( Double, Float )
----------------------------------------------------------------------------------------------------
{- Typeclass mapping a Haskell type to its hasql binary-protocol encoder and decoder. Each instance pairs a type with the corresponding hasql 'Value' encoder and decoder, enabling the TH code generator to resolve codecs by type inference alone.-}class PgCodec a where pgDecode :: Decoders.Value a pgEncode :: Encoders.Value a
----------------------------------------------------------------------------------------------------
instance PgCodec Bool where pgDecode :: Decoders.Value Bool pgDecode = Decoders.bool pgEncode :: Encoders.Value Bool pgEncode = Encoders.bool
instance PgCodec Int16 where pgDecode :: Decoders.Value Int16 pgDecode = Decoders.int2 pgEncode :: Encoders.Value Int16 pgEncode = Encoders.int2
instance PgCodec Int32 where pgDecode :: Decoders.Value Int32 pgDecode = Decoders.int4 pgEncode :: Encoders.Value Int32 pgEncode = Encoders.int4
instance PgCodec Int64 where pgDecode :: Decoders.Value Int64 pgDecode = Decoders.int8 pgEncode :: Encoders.Value Int64 pgEncode = Encoders.int8
instance PgCodec Float where pgDecode :: Decoders.Value Float pgDecode = Decoders.float4 pgEncode :: Encoders.Value Float pgEncode = Encoders.float4
instance PgCodec Double where pgDecode :: Decoders.Value Double pgDecode = Decoders.float8 pgEncode :: Encoders.Value Double pgEncode = Encoders.float8
instance PgCodec Scientific where pgDecode :: Decoders.Value Scientific pgDecode = Decoders.numeric pgEncode :: Encoders.Value Scientific pgEncode = Encoders.numeric
instance PgCodec Char where pgDecode :: Decoders.Value Char pgDecode = Decoders.char pgEncode :: Encoders.Value Char pgEncode = Encoders.char
instance PgCodec Text where pgDecode :: Decoders.Value Text pgDecode = Decoders.text pgEncode :: Encoders.Value Text pgEncode = Encoders.text
instance PgCodec String where pgDecode :: Decoders.Value String pgDecode = fmap Data.Text.unpack Decoders.text pgEncode :: Encoders.Value String pgEncode = Contravariant.contramap Data.Text.pack Encoders.text
instance PgCodec ByteString where pgDecode :: Decoders.Value ByteString pgDecode = Decoders.bytea pgEncode :: Encoders.Value ByteString pgEncode = Encoders.bytea
instance PgCodec Day where pgDecode :: Decoders.Value Day pgDecode = Decoders.date pgEncode :: Encoders.Value Day pgEncode = Encoders.date
instance PgCodec LocalTime where pgDecode :: Decoders.Value LocalTime pgDecode = Decoders.timestamp pgEncode :: Encoders.Value LocalTime pgEncode = Encoders.timestamp
instance PgCodec UTCTime where pgDecode :: Decoders.Value UTCTime pgDecode = Decoders.timestamptz pgEncode :: Encoders.Value UTCTime pgEncode = Encoders.timestamptz
instance PgCodec TimeOfDay where pgDecode :: Decoders.Value TimeOfDay pgDecode = Decoders.time pgEncode :: Encoders.Value TimeOfDay pgEncode = Encoders.time
instance PgCodec (TimeOfDay, TimeZone) where pgDecode :: Decoders.Value (TimeOfDay, TimeZone) pgDecode = Decoders.timetz pgEncode :: Encoders.Value (TimeOfDay, TimeZone) pgEncode = Encoders.timetz
instance PgCodec DiffTime where pgDecode :: Decoders.Value DiffTime pgDecode = Decoders.interval pgEncode :: Encoders.Value DiffTime pgEncode = Encoders.interval
instance PgCodec UUID where pgDecode :: Decoders.Value UUID pgDecode = Decoders.uuid pgEncode :: Encoders.Value UUID pgEncode = Encoders.uuid
instance PgCodec Aeson.Value where pgDecode :: Decoders.Value Aeson.Value pgDecode = Decoders.jsonb pgEncode :: Encoders.Value Aeson.Value pgEncode = Encoders.jsonb