module Hasql.Generic.HasParams
( HasParams
, HasEField
, HasEValue
, mkParams
, mkEField
, mkEValue
, gParams
, gEEnumValue
) where
import qualified Data.Aeson.Types as JSON
import Data.ByteString (ByteString)
import Data.Foldable (foldl')
import Data.Functor.Contravariant
import Data.Int (Int16, Int32, Int64)
import qualified Data.Map as Map
import Data.Scientific (Scientific)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Time (Day, DiffTime, LocalTime,
TimeOfDay, TimeZone, UTCTime)
import Data.UUID (UUID)
import Data.Vector (Vector)
import qualified Data.Vector as Vector
import Data.Word (Word16, Word32, Word64)
import Generics.SOP
import qualified GHC.Generics as GHC
import Hasql.Encoders
class HasParams a where
mkParams :: Params a
default mkParams :: (Generic a, Code a ~ '[ xs ], All HasEField xs) => Params a
mkParams = gParams
class HasEField a where
mkEField :: Params a
class HasEValue a where
mkEValue :: Value a
gParams :: (Generic a, Code a ~ '[ xs ], All HasEField xs) => Params a
gParams =
contramap (unZ . unSOP . from)
(mconcat $ hcollapse
(hcmap (Proxy :: Proxy HasEField)
(\ (Fn p) -> K (contramap (unI . p . K) mkEField))
projections
)
)
class (a ~ b) => Equal a b
instance (a ~ b) => Equal a b
gEEnumValue :: (Generic a, All (Equal '[]) (Code a)) => NP (K Text) (Code a) -> Value a
gEEnumValue names =
enum $ hcollapse . hzipWith const names . unSOP . from
instance HasEValue Bool where
mkEValue = bool
instance HasEValue Int16 where
mkEValue = int2
instance HasEValue Int32 where
mkEValue = int4
instance HasEValue Int64 where
mkEValue = int8
instance HasEValue Float where
mkEValue = float4
instance HasEValue Double where
mkEValue = float8
instance HasEValue Scientific where
mkEValue = numeric
instance HasEValue Char where
mkEValue = char
instance HasEValue Text where
mkEValue = text
instance HasEValue ByteString where
mkEValue = bytea
instance HasEValue Day where
mkEValue = date
instance HasEValue LocalTime where
mkEValue = timestamp
instance HasEValue UTCTime where
mkEValue = timestamptz
instance HasEValue TimeOfDay where
mkEValue = time
instance HasEValue (TimeOfDay, TimeZone) where
mkEValue = timetz
instance HasEValue DiffTime where
mkEValue = interval
instance HasEValue UUID where
mkEValue = uuid
instance HasEValue JSON.Value where
mkEValue = jsonb
instance HasEValue a => HasEField [Maybe a] where
mkEField = value $ array (arrayDimension foldl' (arrayNullableValue mkEValue))
instance HasEValue a => HasEField [a] where
mkEField = value $ array (arrayDimension foldl' (arrayValue mkEValue))
instance HasEValue a => HasEField (Vector (Maybe a)) where
mkEField = value $ array (arrayDimension Vector.foldl' (arrayNullableValue mkEValue))
instance HasEValue a => HasEField (Vector a) where
mkEField = value $ array (arrayDimension Vector.foldl' (arrayValue mkEValue))
instance HasEValue a => HasEField (Maybe a) where
mkEField = nullableValue mkEValue
instance HasEValue a => HasEField a where
mkEField = value mkEValue
instance HasEField Int where
mkEField = contramap fromIntegral (value int8)
instance HasEField (Maybe Int) where
mkEField = contramap (fmap fromIntegral) (nullableValue int8)
instance HasEField Word16 where
mkEField = contramap fromIntegral (value int2)
instance HasEField Word32 where
mkEField = contramap fromIntegral (value int4)
instance HasEField Word64 where
mkEField = contramap fromIntegral (value int8)
instance HasEField (Maybe Word16) where
mkEField = contramap (fmap fromIntegral) (nullableValue int2)
instance HasEField (Maybe Word32) where
mkEField = contramap (fmap fromIntegral) (nullableValue int4)
instance HasEField (Maybe Word64) where
mkEField = contramap (fmap fromIntegral) (nullableValue int8)
instance All HasEField [a,b] => HasParams (a,b)
instance All HasEField [a,b,c] => HasParams (a,b,c)
instance All HasEField [a,b,c,d] => HasParams (a,b,c,d)
instance All HasEField [a,b,c,d,e] => HasParams (a,b,c,d,e)
instance All HasEField [a,b,c,d,e,f] => HasParams (a,b,c,d,e,f)
instance All HasEField [a,b,c,d,e,f,g] => HasParams (a,b,c,d,e,f,g)
instance All HasEField [a,b,c,d,e,f,g,h] => HasParams (a,b,c,d,e,f,g,h)
instance All HasEField [a,b,c,d,e,f,g,h,i] => HasParams (a,b,c,d,e,f,g,h,i)
instance All HasEField [a,b,c,d,e,f,g,h,i,j] => HasParams (a,b,c,d,e,f,g,h,i,j)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k] => HasParams (a,b,c,d,e,f,g,h,i,j,k)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y)
instance All HasEField [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z] => HasParams (a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)