on db148ff help sign in
rel8/src/Rel8/Expr/Serialize.hs
49 lines 1.4 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.Null ( Unnullify, Nullity( Null, NotNull ), Sql, nullable )
25import Rel8.Type ( DBType, typeInformation )
26import Rel8.Type.Information ( TypeInformation(..) )
29-- | Produce an expression from a literal.
30--
31-- Note that you can usually use 'Rel8.lit', but @litExpr@ can solve problems
32-- of inference in polymorphic code.
33litExpr :: Sql DBType a => a -> Expr a
34litExpr = slitExpr nullable typeInformation
37slitExpr :: Nullity a -> TypeInformation (Unnullify a) -> a -> Expr a
38slitExpr nullity info@TypeInformation {encode} =
39 scastExpr info . Expr . encoder
40 where
41 encoder = case nullity of
42 Null -> maybe (Opaleye.ConstExpr Opaleye.NullLit) encode
43 NotNull -> encode
46sparseValue :: Nullity a -> TypeInformation (Unnullify a) -> Hasql.Row a
47sparseValue nullity TypeInformation {decode} = case nullity of
48 Null -> Hasql.column $ Hasql.nullable decode
49 NotNull -> Hasql.column $ Hasql.nonNullable decode