on fb4e1a5 help sign in
rel8/src/Rel8/Expr/Serialize.hs
53 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.Nullability
25 ( Unnullify
26 , Nullability( Nullable, NonNullable )
27 , Sql, nullabilization
28 )
29import Rel8.Type ( DBType, typeInformation )
30import Rel8.Type.Information ( TypeInformation(..) )
33-- | Produce an expression from a literal.
34--
35-- Note that you can usually use 'Rel8.lit', but @litExpr@ can solve problems
36-- of inference in polymorphic code.
37litExpr :: Sql DBType a => a -> Expr a
38litExpr = slitExpr nullabilization typeInformation
41slitExpr :: Nullability a -> TypeInformation (Unnullify a) -> a -> Expr a
42slitExpr nullability info@TypeInformation {encode} =
43 scastExpr info . Expr . encoder
44 where
45 encoder = case nullability of
46 Nullable -> maybe (Opaleye.ConstExpr Opaleye.NullLit) encode
47 NonNullable -> encode
50sparseValue :: Nullability a -> TypeInformation (Unnullify a) -> Hasql.Row a
51sparseValue nullability TypeInformation {decode} = case nullability of
52 Nullable -> Hasql.column $ Hasql.nullable decode
53 NonNullable -> Hasql.column $ Hasql.nonNullable decode