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, nullable, unsafeLiftOpNull )
26import Rel8.Expr.Opaleye ( toPrimExpr, zipPrimExprsWith )
27import Rel8.Schema.Null ( Nullity( Null, NotNull ), Sql )
28import qualified Rel8.Schema.Null as Schema ( nullable )
29import Rel8.Type.Ord ( DBOrd )
32lt :: DBOrd a => Expr a -> Expr a -> Expr Bool
33lt = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:<))
36le :: DBOrd a => Expr a -> Expr a -> Expr Bool
37le = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:<=))
40gt :: DBOrd a => Expr a -> Expr a -> Expr Bool
41gt = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:>))
44ge :: DBOrd a => Expr a -> Expr a -> Expr Bool
45ge = zipPrimExprsWith (Opaleye.BinExpr (Opaleye.:>=))
48-- | Corresponds to the SQL @<@ operator. Note that this differs from SQL @<@
49-- as @null@ will sort below any other value. For a version of @<@ that exactly
50-- matches SQL, see '(<?)'.
51(<.) :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
52(<.) = case Schema.nullable @a of
53 Null -> \ma mb -> isNull ma &&. isNonNull mb ||. ma <? mb
58-- | Corresponds to the SQL @<=@ operator. Note that this differs from SQL @<=@
59-- as @null@ will sort below any other value. For a version of @<=@ that exactly
60-- matches SQL, see '(<=?)'.
61(<=.) :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
62(<=.) = case Schema.nullable @a of
63 Null -> \ma mb -> isNull ma ||. ma <=? mb
68-- | Corresponds to the SQL @>@ operator. Note that this differs from SQL @>@
69-- as @null@ will sort below any other value. For a version of @>@ that exactly
70-- matches SQL, see '(>?)'.
71(>.) :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
72(>.) = case Schema.nullable @a of
73 Null -> \ma mb -> isNonNull ma &&. isNull mb ||. ma >? mb
78-- | Corresponds to the SQL @>=@ operator. Note that this differs from SQL @>@
79-- as @null@ will sort below any other value. For a version of @>=@ that
80-- exactly matches SQL, see '(>=?)'.
81(>=.) :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr Bool
82(>=.) = case Schema.nullable @a of
83 Null -> \ma mb -> isNull mb ||. ma >=? mb
88-- | Corresponds to the SQL @<@ operator. Returns @null@ if either arguments
90(<?) :: DBOrd a => Expr (Maybe a) -> Expr (Maybe a) -> Expr Bool
91a <? b = coalesce $ unsafeLiftOpNull lt a b
95-- | Corresponds to the SQL @<=@ operator. Returns @null@ if either arguments
97(<=?) :: DBOrd a => Expr (Maybe a) -> Expr (Maybe a) -> Expr Bool
98a <=? b = coalesce $ unsafeLiftOpNull le a b
102-- | Corresponds to the SQL @>@ operator. Returns @null@ if either arguments
104(>?) :: DBOrd a => Expr (Maybe a) -> Expr (Maybe a) -> Expr Bool
105a >? b = coalesce $ unsafeLiftOpNull gt a b
109-- | Corresponds to the SQL @>=@ operator. Returns @null@ if either arguments
111(>=?) :: DBOrd a => Expr (Maybe a) -> Expr (Maybe a) -> Expr Bool
112a >=? b = coalesce $ unsafeLiftOpNull ge a b
116-- | Given two expressions, return the expression that sorts less than the
119-- Corresponds to the SQL @least()@ function.
120leastExpr :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr a
121leastExpr ma mb = case Schema.nullable @a of
122 Null -> nullable ma (\a -> nullable mb (least_ a) mb) ma
123 NotNull -> least_ ma mb
125 least_ a b = Expr (Opaleye.FunExpr "LEAST" [toPrimExpr a, toPrimExpr b])
128-- | Given two expressions, return the expression that sorts greater than the
131-- Corresponds to the SQL @greatest()@ function.
132greatestExpr :: forall a. Sql DBOrd a => Expr a -> Expr a -> Expr a
133greatestExpr ma mb = case Schema.nullable @a of
134 Null -> nullable mb (\a -> nullable ma (greatest_ a) mb) ma
135 NotNull -> greatest_ ma mb
138 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