on 00a36f5 help sign in
rel8/src/Rel8/Expr/Serialize.hs
48 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 ( Nullability( Nullable, NonNullable )
26 , Sql, nullabilization
27 )
28import Rel8.Type ( DBType, typeInformation )
29import Rel8.Type.Information ( TypeInformation(..) )
32litExpr :: Sql DBType a => a -> Expr a
33litExpr = slitExpr nullabilization typeInformation
36slitExpr :: Nullability db a -> TypeInformation db -> a -> Expr a
37slitExpr nullable info@TypeInformation {encode} =
38 scastExpr nullable info . Expr . encoder
39 where
40 encoder = case nullable of
41 Nullable -> maybe (Opaleye.ConstExpr Opaleye.NullLit) encode
42 NonNullable -> encode
45sparseValue :: Nullability db a -> TypeInformation db -> Hasql.Row a
46sparseValue nullability TypeInformation {decode, out} = case nullability of
47 Nullable -> Hasql.column $ Hasql.nullable $ out <$> decode
48 NonNullable -> Hasql.column $ Hasql.nonNullable $ out <$> decode