on 11bcbbb help sign in
rel8/src/Rel8/Expr/Serialize.hs
49 lines 1.3 kB history raw
1{-# language FlexibleContexts #-}
2{-# language NamedFieldPuns #-}
3{-# language TypeFamilies #-}
5module Rel8.Expr.Serialize
6 ( litExpr
7 , slitExpr
8 , sparseValue
9 )
10where
12-- base
13import Prelude
15-- hasql
16import qualified Hasql.Decoders as Hasql
18-- opaleye
19import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
21-- rel8
22import {-# SOURCE #-} Rel8.Expr ( Expr( Expr ) )
23import Rel8.Expr.Opaleye ( scastExpr )
24import Rel8.Schema.Nullability
25 ( Unnullify
26 , Nullability( Nullable, NonNullable )
27 , Sql, nullabilization
28 )
29import Rel8.Type ( DBType, typeInformation )
30import Rel8.Type.Information ( TypeInformation(..) )
33litExpr :: Sql DBType a => a -> Expr a
34litExpr = slitExpr nullabilization typeInformation
37slitExpr :: Nullability a -> TypeInformation (Unnullify a) -> a -> Expr a
38slitExpr nullability info@TypeInformation {encode} =
39 scastExpr info . Expr . encoder
40 where
41 encoder = case nullability of
42 Nullable -> maybe (Opaleye.ConstExpr Opaleye.NullLit) encode
43 NonNullable -> encode
46sparseValue :: Nullability a -> TypeInformation (Unnullify a) -> Hasql.Row a
47sparseValue nullability TypeInformation {decode, out} = case nullability of
48 Nullable -> Hasql.column $ Hasql.nullable $ out <$> decode
49 NonNullable -> Hasql.column $ Hasql.nonNullable $ out <$> decode