1{-# language DataKinds #-}
2{-# language FlexibleContexts #-}
4{-# language ScopedTypeVariables #-}
5{-# language TypeApplications #-}
7{-# options_ghc -fno-warn-redundant-constraints #-}
10 ( (<.), (<=.), (>.), (>=.)
11 , (<?), (<=?), (>?), (>=?)
12 , leastExpr, greatestExpr
20import qualified Opaleye.Internal.HaskellDB.PrimQuery as Opaleye
23import Rel8.Expr ( Expr( Expr ) )
24import Rel8.Expr.Bool ( (&&.), (||.), coalesce )
25import Rel8.Expr.Null ( isNull, isNonNull, nullableExpr, unsafeLiftOpNull )
26import Rel8.Expr.Opaleye ( toPrimExpr, zipPrimExprsWith )
27import Rel8.Schema.Null ( Nullity( Null, NotNull ), Sql, nullable )
28import Rel8.Type.Ord ( DBOrd )
31lt :: DBOrd a => Expr a -> Expr a -> Expr Bool
32lt = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:<))
35le :: DBOrd a => Expr a -> Expr a -> Expr Bool
36le = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:<=))
39gt :: DBOrd a => Expr a -> Expr a -> Expr Bool
40gt = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:>))
43ge :: DBOrd a => Expr a -> Expr a -> Expr Bool
44ge = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:>=))
47-- | Corresponds to the SQL @<@ operator. Note that this differs from SQL @<@
48-- as @null@ will sort below any other value. For a version of @<@ that exactly
49-- matches SQL, see '(<?)'.
50(<.) :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
51(<.) = case nullable @a of
52 Null -> \ma mb -> isNull ma &&. isNonNull mb ||. ma <? mb
57-- | Corresponds to the SQL @<=@ operator. Note that this differs from SQL @<=@
58-- as @null@ will sort below any other value. For a version of @<=@ that exactly
59-- matches SQL, see '(<=?)'.
60(<=.) :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
61(<=.) = case nullable @a of
62 Null -> \ma mb -> isNull ma ||. ma <=? mb
67-- | Corresponds to the SQL @>@ operator. Note that this differs from SQL @>@
68-- as @null@ will sort below any other value. For a version of @>@ that exactly
69-- matches SQL, see '(>?)'.
70(>.) :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
71(>.) = case nullable @a of
72 Null -> \ma mb -> isNonNull ma &&. isNull mb ||. ma >? mb
77-- | Corresponds to the SQL @>=@ operator. Note that this differs from SQL @>@
78-- as @null@ will sort below any other value. For a version of @>=@ that
79-- exactly matches SQL, see '(>=?)'.
80(>=.) :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
81(>=.) = case nullable @a of
82 Null -> \ma mb -> isNull mb ||. ma >=? mb
87-- | Corresponds to the SQL @<@ operator. Returns @null@ if either arguments
89(<?) :: DBOrd a => Expr (Maybe a) -> Expr (Maybe a) -> Expr Bool
90a <? b = coalesce $ unsafeLiftOpNull lt a b
94-- | Corresponds to the SQL @<=@ operator. Returns @null@ if either arguments
96(<=?) :: DBOrd a => Expr (Maybe a) -> Expr (Maybe a) -> Expr Bool
97a <=? b = coalesce $ unsafeLiftOpNull le a b
101-- | Corresponds to the SQL @>@ operator. Returns @null@ if either arguments
103(>?) :: DBOrd a => Expr (Maybe a) -> Expr (Maybe a) -> Expr Bool
104a >? b = coalesce $ unsafeLiftOpNull gt a b
108-- | Corresponds to the SQL @>=@ operator. Returns @null@ if either arguments
110(>=?) :: DBOrd a => Expr (Maybe a) -> Expr (Maybe a) -> Expr Bool
111a >=? b = coalesce $ unsafeLiftOpNull ge a b
115-- | Given two expressions, return the expression that sorts less than the
118-- Corresponds to the SQL @least()@ function.
119leastExpr :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr a
120leastExpr ma mb = case nullable @a of
121 Null -> nullableExpr ma (\a -> nullableExpr mb (least_ a) mb) ma
122 NotNull -> least_ ma mb
124 least_ a b = Expr (Opaleye.FunExpr "LEAST" [toPrimExpr a, toPrimExpr b])
127-- | Given two expressions, return the expression that sorts greater than the
130-- Corresponds to the SQL @greatest()@ function.
131greatestExpr :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr a
132greatestExpr ma mb = case nullable @a of
133 Null -> nullableExpr mb (\a -> nullableExpr ma (greatest_ a) mb) ma
134 NotNull -> greatest_ ma mb
137 Expr (Opaleye.FunExpr "GREATEST" [toPrimExpr a, toPrimExpr b])
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list