Merge pull request #111 from stackbuilders/add-between

Add between
This commit is contained in:
Matt Parsons 2019-04-19 18:00:10 -06:00 committed by GitHub
commit dfc20d5ae8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 1 deletions

View File

@ -44,7 +44,7 @@ module Database.Esqueleto
, val, isNothing, just, nothing, joinV, withNonNull
, countRows, count, countDistinct
, not_, (==.), (>=.), (>.), (<=.), (<.), (!=.), (&&.), (||.)
, (+.), (-.), (/.), (*.)
, between, (+.), (-.), (/.), (*.)
, random_, round_, ceiling_, floor_
, min_, max_, sum_, avg_, castNum, castNumM
, coalesce, coalesceDefault

View File

@ -352,6 +352,11 @@ class (Functor query, Applicative query, Monad query) =>
(/.) :: PersistField a => expr (Value a) -> expr (Value a) -> expr (Value a)
(*.) :: PersistField a => expr (Value a) -> expr (Value a) -> expr (Value a)
-- | @BETWEEN@ operator
--
-- /Since: 2.8.0/
between :: PersistField typ => expr (Value typ) -> (expr (Value typ), expr (Value typ)) -> expr (Value Bool)
random_ :: (PersistField a, Num a) => expr (Value a)
round_ :: (PersistField a, Num a, PersistField b, Num b) => expr (Value a) -> expr (Value b)

View File

@ -97,6 +97,7 @@ instance Exception EsqueletoError
data CompositeKeyError =
NotError
| BetweenError
| ToInsertionError
| CombineInsertionError
| FoldHelpError
@ -533,6 +534,12 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
(/.) = unsafeSqlBinOp " / "
(*.) = unsafeSqlBinOp " * "
a `between` (ERaw _ f, ERaw _ g) = unsafeSqlBinOp " BETWEEN " a $ ERaw Never $ \x ->
let (_, fv) = f x
(_, gv) = g x
in (" ? AND ? ", fv ++ gv)
_ `between` _ = throw $ CompositeKeyErr BetweenError
random_ = unsafeSqlValue "RANDOM()"
round_ = unsafeSqlFunction "ROUND"
ceiling_ = unsafeSqlFunction "CEILING"

View File

@ -629,6 +629,16 @@ testSelectWhere run = do
return p
liftIO $ ret `shouldBe` [ p3e ]
it "works for a simple example with between and [uses just . val]" $
run $ do
p1e <- insert' p1
_ <- insert' p2
_ <- insert' p3
ret <- select $
from $ \p -> do
where_ ((p ^. PersonAge) `between` (just $ val 20, just $ val 40))
return p
liftIO $ ret `shouldBe` [ p1e ]
it "works with avg_" $
run $ do
_ <- insert' p1