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..c86ceee 100644 --- a/src/Database/Esqueleto/Internal/Language.hs +++ b/src/Database/Esqueleto/Internal/Language.hs @@ -352,6 +352,8 @@ 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 :: 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 16a24e9..aca54ed 100644 --- a/src/Database/Esqueleto/Internal/Sql.hs +++ b/src/Database/Esqueleto/Internal/Sql.hs @@ -533,6 +533,12 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where (/.) = unsafeSqlBinOp " / " (*.) = unsafeSqlBinOp " * " + between a@(ERaw _ _) (ERaw _ f) (ERaw _ g) = unsafeSqlBinOp " BETWEEN " a $ ERaw Never $ \x -> + let (_, fv) = f x + (_, gv) = g x + in (" ? AND ? ", fv ++ gv) + between _ _ _ = throw (CompositeKeyErr NotError) + random_ = unsafeSqlValue "RANDOM()" round_ = unsafeSqlFunction "ROUND" ceiling_ = unsafeSqlFunction "CEILING" diff --git a/test/Common/Test.hs b/test/Common/Test.hs index 34c9c87..08533e5 100644 --- a/test/Common/Test.hs +++ b/test/Common/Test.hs @@ -630,6 +630,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_ (between (p ^. PersonAge) (just $ val 20) (just $ val 40)) + return p + liftIO $ ret `shouldBe` [ p1e ] it "works with avg_" $ run $ do _ <- insert' p1