Skip to content

Commit

Permalink
create filterWherePast and filterWhereFuture functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyj0 committed Oct 7, 2021
1 parent d9c443c commit 41e8bdd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
45 changes: 45 additions & 0 deletions IHP/QueryBuilder.hs
Expand Up @@ -37,6 +37,8 @@ module IHP.QueryBuilder
, filterWhereILikeJoinedTable
, filterWhereMatchesJoinedTable
, filterWhereIMatchesJoinedTable
, filterWherePast
, filterWhereFuture
, labelResults
, EqOrIsOperator
, filterWhereSql
Expand Down Expand Up @@ -747,6 +749,49 @@ filterWhereIMatchesJoinedTable (name, value) queryBuilderProvider = injectQueryB
{-# INLINE filterWhereIMatchesJoinedTable #-}


-- | Filter all rows by whether a field is in the past, determined by comparing 'NOW()' to the field's value.
--
-- Opposite of 'filterWhereFuture'
--
-- __Example:__ Fetch all posts scheduled for the past.
--
-- > publicPosts <- query @Post
-- > |> filterWherePast #scheduledAt
-- > |> fetch
-- > -- SELECT * FROM posts WHERE scheduled_at <= NOW()
filterWherePast
:: ( KnownSymbol table
, KnownSymbol name
, ToField value
, HasField name (GetModelByTableName table) value
, HasQueryBuilder queryBuilderProvider joinRegister
)
=> Proxy name -> queryBuilderProvider table -> queryBuilderProvider table
filterWherePast name = filterWhereSql (name, "<= NOW()")
{-# INLINE filterWherePast #-}

-- | Filter all rows by whether a field is in the future, determined by comparing 'NOW()' to the field's value.
--
-- Opposite of 'filterWherePast'
--
-- __Example:__ Fetch all posts scheduled for the future.
--
-- > hiddenPosts <- query @Post
-- > |> filterWhereFuture #scheduledAt
-- > |> fetch
-- > -- SELECT * FROM posts WHERE scheduled_at > NOW()
filterWhereFuture
:: ( KnownSymbol table
, KnownSymbol name
, ToField value
, HasField name (GetModelByTableName table) value
, HasQueryBuilder queryBuilderProvider joinRegister
)
=> Proxy name -> queryBuilderProvider table -> queryBuilderProvider table
filterWhereFuture name = filterWhereSql (name, "> NOW()")
{-# INLINE filterWhereFuture #-}


-- | Allows to add a custom raw sql where condition
--
-- If your query cannot be represented with 'filterWhereSql', take a look at 'IHP.ModelSupport.sqlQuery'.
Expand Down
14 changes: 14 additions & 0 deletions Test/QueryBuilderSpec.hs
Expand Up @@ -219,6 +219,20 @@ tests = do
|> filterWhereILikeJoinedTable @User (#name, "%" <> searchTerm <> "%")
(toSQL theQuery `shouldBe` ("SELECT posts.id, posts.title, posts.external_url, posts.created_at, posts.public, posts.created_by, posts.category_id FROM posts INNER JOIN users ON posts.created_by = users.id WHERE users.name ILIKE ?", [Escape "%louis%"]))

describe "filterWherePast" do
it "should produce a SQL with the correct WHERE condition" do
let theQuery = query @Post
|> filterWherePast #createdAt

(toSQL theQuery) `shouldBe` ("SELECT posts.id, posts.title, posts.external_url, posts.created_at, posts.public, posts.created_by, posts.category_id FROM posts WHERE posts.created_at ?", [Plain "<= NOW()"])

describe "filterWhereFuture" do
it "should produce a SQL with the correct WHERE condition" do
let theQuery = query @Post
|> filterWhereFuture #createdAt

(toSQL theQuery) `shouldBe` ("SELECT posts.id, posts.title, posts.external_url, posts.created_at, posts.public, posts.created_by, posts.category_id FROM posts WHERE posts.created_at ?", [Plain "> NOW()"])

describe "filterWhereSql" do
it "should produce a SQL with a WHERE condition" do
let theValues :: [Text] = ["first", "second"]
Expand Down

0 comments on commit 41e8bdd

Please sign in to comment.