diff --git a/src/Database/Esqueleto.hs b/src/Database/Esqueleto.hs index f33c882..9a546f0 100644 --- a/src/Database/Esqueleto.hs +++ b/src/Database/Esqueleto.hs @@ -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 diff --git a/src/Database/Esqueleto/Internal/Language.hs b/src/Database/Esqueleto/Internal/Language.hs index ac992e1..a1efb70 100644 --- a/src/Database/Esqueleto/Internal/Language.hs +++ b/src/Database/Esqueleto/Internal/Language.hs @@ -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) diff --git a/src/Database/Esqueleto/Internal/Sql.hs b/src/Database/Esqueleto/Internal/Sql.hs index 7f2e0c9..43ad016 100644 --- a/src/Database/Esqueleto/Internal/Sql.hs +++ b/src/Database/Esqueleto/Internal/Sql.hs @@ -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" diff --git a/test/Common/Test.hs b/test/Common/Test.hs index d5cac2c..1ecb17b 100644 --- a/test/Common/Test.hs +++ b/test/Common/Test.hs @@ -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