Skip to content

Commit

Permalink
Docs on boolean operators (#411)
Browse files Browse the repository at this point in the history
* Docs

* changelog

* changelog
  • Loading branch information
parsonsmatt authored Dec 26, 2024
1 parent 9631275 commit 7c5cb0e
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 6 deletions.
11 changes: 6 additions & 5 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
========
- @blujupiter32
- [#379](https://github.com/bitemyapp/esqueleto/pull/379)
- Fix a bug where `not_ (a &&. b)` would be interpeted as `(not_ a) &&.
b`
- Fix a bug where `not_ (a &&. b)` would be interpeted as `(not_ a) &&. b`
- @TeofilC
- [#394](https://github.com/bitemyapp/esqueleto/pull/394)
- Use TH quotes to eliminate some CPP.
- [#394](https://github.com/bitemyapp/esqueleto/pull/394)
- Use TH quotes to eliminate some CPP.
- @parsonsmatt, @jappeace
- [#346](#https://github.com/bitemyapp/esqueleto/pull/346), [#411](https://github.com/bitemyapp/esqueleto/pull/411)
- Add docs for more SQL operators

3.5.13.1
========

- @csamak
- [#405](https://github.com/bitemyapp/esqueleto/pull/405)
- Fix a bug introduced in 3.5.12.0 where deriveEsqueletoRecord incorrectly errors
Expand Down
117 changes: 116 additions & 1 deletion src/Database/Esqueleto/Internal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -727,42 +727,157 @@ not_ v = ERaw noMeta (const $ first ("NOT " <>) . x)
else
f Parens info

-- | This operator produces the SQL operator @=@, which is used to compare
-- values for equality.
--
-- Example:
--
-- @
-- query :: UserId -> SqlPersistT IO [Entity User]
-- query userId = select $ do
-- user <- from $ table \@User
-- where_ (user ^. UserId ==. val userId)
-- pure user
-- @
--
-- This would generate the following SQL:
--
-- @
-- SELECT user.*
-- FROM user
-- WHERE user.id = ?
-- @
(==.) :: PersistField typ => SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
(==.) = unsafeSqlBinOpComposite " = " " AND "

-- | This operator translates to the SQL operator @>=@.
--
-- Example:
--
-- @
-- where_ $ user ^. UserAge >=. val 21
-- @
(>=.) :: PersistField typ => SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
(>=.) = unsafeSqlBinOp " >= "

-- | This operator translates to the SQL operator @>@.
--
-- Example:
--
-- @
-- where_ $ user ^. UserAge >. val 20
-- @
(>.) :: PersistField typ => SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
(>.) = unsafeSqlBinOp " > "

-- | This operator translates to the SQL operator @<=@.
--
-- Example:
--
-- @
-- where_ $ val 21 <=. user ^. UserAge
-- @
(<=.) :: PersistField typ => SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
(<=.) = unsafeSqlBinOp " <= "

-- | This operator translates to the SQL operator @<@.
--
-- Example:
--
-- @
-- where_ $ val 20 <. user ^. UserAge
-- @
(<.) :: PersistField typ => SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
(<.) = unsafeSqlBinOp " < "

-- | This operator translates to the SQL operator @!=@.
--
-- Example:
--
-- @
-- where_ $ user ^. UserName !=. val "Bob"
-- @
(!=.) :: PersistField typ => SqlExpr (Value typ) -> SqlExpr (Value typ) -> SqlExpr (Value Bool)
(!=.) = unsafeSqlBinOpComposite " != " " OR "

-- | This operator translates to the SQL operator @AND@.
--
-- Example:
--
-- @
-- where_ $
-- user ^. UserName ==. val "Matt"
-- &&. user ^. UserAge >=. val 21
-- @
(&&.) :: SqlExpr (Value Bool) -> SqlExpr (Value Bool) -> SqlExpr (Value Bool)
(&&.) = unsafeSqlBinOp " AND "

-- | This operator translates to the SQL operator @AND@.
--
-- Example:
--
-- @
-- where_ $
-- user ^. UserName ==. val "Matt"
-- ||. user ^. UserName ==. val "John"
-- @
(||.) :: SqlExpr (Value Bool) -> SqlExpr (Value Bool) -> SqlExpr (Value Bool)
(||.) = unsafeSqlBinOp " OR "

-- | This operator translates to the SQL operator @+@.
--
-- This does not require or assume anything about the SQL values. Interpreting
-- what @+.@ means for a given type is left to the database engine.
--
-- Example:
--
-- @
-- user ^. UserAge +. val 10
-- @
(+.) :: PersistField a => SqlExpr (Value a) -> SqlExpr (Value a) -> SqlExpr (Value a)
(+.) = unsafeSqlBinOp " + "

-- | This operator translates to the SQL operator @-@.
--
-- This does not require or assume anything about the SQL values. Interpreting
-- what @-.@ means for a given type is left to the database engine.
--
-- Example:
--
-- @
-- user ^. UserAge -. val 10
-- @
(-.) :: PersistField a => SqlExpr (Value a) -> SqlExpr (Value a) -> SqlExpr (Value a)
(-.) = unsafeSqlBinOp " - "

-- | This operator translates to the SQL operator @/@.
--
-- This does not require or assume anything about the SQL values. Interpreting
-- what @/.@ means for a given type is left to the database engine.
--
-- Example:
--
-- @
-- user ^. UserAge /. val 10
-- @
(/.) :: PersistField a => SqlExpr (Value a) -> SqlExpr (Value a) -> SqlExpr (Value a)
(/.) = unsafeSqlBinOp " / "

-- | This operator translates to the SQL operator @*@.
--
-- This does not require or assume anything about the SQL values. Interpreting
-- what @*.@ means for a given type is left to the database engine.
--
-- Example:
--
-- @
-- user ^. UserAge *. val 10
-- @
(*.) :: PersistField a => SqlExpr (Value a) -> SqlExpr (Value a) -> SqlExpr (Value a)
(*.) = unsafeSqlBinOp " * "

-- | @BETWEEN@.
-- | @a `between` (b, c)@ translates to the SQL expression @a >= b AND a <= c@.
-- It does not use a SQL @BETWEEN@ operator.
--
-- @since: 3.1.0
between :: PersistField a => SqlExpr (Value a) -> (SqlExpr (Value a), SqlExpr (Value a)) -> SqlExpr (Value Bool)
Expand Down

0 comments on commit 7c5cb0e

Please sign in to comment.