on 03b366b help sign in
rel8/src/Rel8/Expr/Serialize.hs
47 lines 1.3 kB history raw
1{-# language NamedFieldPuns #-}
2{-# language TypeFamilies #-}
4module Rel8.Expr.Serialize
5 ( litExpr
6 , slitExpr
7 , sparseValue
8 )
9where
11-- base
12import Prelude
14-- hasql
15import qualified Hasql.Decoders as Hasql
17-- opaleye
18import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
20-- rel8
21import {-# SOURCE #-} Rel8.Expr ( Expr( Expr ) )
22import Rel8.Expr.Opaleye ( scastExpr )
23import Rel8.Schema.Nullability
24 ( Nullability( Nullable, NonNullable )
25 , Nullabilizes, nullabilization
26 )
27import Rel8.Type ( DBType, typeInformation )
28import Rel8.Type.Information ( TypeInformation(..) )
31litExpr :: (DBType db, Nullabilizes db a) => a -> Expr a
32litExpr = slitExpr nullabilization typeInformation
35slitExpr :: Nullability db a -> TypeInformation db -> a -> Expr a
36slitExpr nullable info@TypeInformation {encode} =
37 scastExpr nullable info . Expr . encoder
38 where
39 encoder = case nullable of
40 Nullable -> maybe (Opaleye.ConstExpr Opaleye.NullLit) encode
41 NonNullable -> encode
44sparseValue :: Nullability db a -> TypeInformation db -> Hasql.Row a
45sparseValue nullability TypeInformation {decode, out} = case nullability of
46 Nullable -> Hasql.column $ Hasql.nullable $ out <$> decode
47 NonNullable -> Hasql.column $ Hasql.nonNullable $ out <$> decode