Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs on boolean operators #411

Merged
merged 3 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -60,7 +60,7 @@
import Data.Kind (Type)
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import qualified Data.Monoid as Monoid

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.10.4)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.10.4)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.6.5)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.2.2)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.2.2)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.8.4)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.8.4)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.0.2)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.0.2)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6.2)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6.2)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.8.1)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.8.1)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4.5)

The qualified import of ‘Data.Monoid’ is redundant

Check warning on line 63 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4.5)

The qualified import of ‘Data.Monoid’ is redundant
import Data.Proxy (Proxy(..))
import Data.Set (Set)
import qualified Data.Set as Set
Expand All @@ -70,7 +70,7 @@
import Data.Typeable (Typeable)
import Database.Esqueleto.Internal.ExprParser (TableAccess(..), parseOnExpr)
import Database.Esqueleto.Internal.PersistentImport
import Database.Persist (EntityNameDB(..), FieldNameDB(..), SymbolToField(..))

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.10.4)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.10.4)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.2.2)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.2.2)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.8.4)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 8.8.4)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.0.2)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.0.2)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6.2)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.6.2)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.8.1)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.8.1)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4.5)

The import of ‘Database.Persist’ is redundant

Check warning on line 73 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.4.5)

The import of ‘Database.Persist’ is redundant
import qualified Database.Persist
import Database.Persist.Sql.Util
( entityColumnCount
Expand Down Expand Up @@ -727,42 +727,157 @@
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 Expand Up @@ -1318,7 +1433,7 @@
unique = finalR uniqueConstructor
-- there must be a better way to get the constrain name from a unique, make this not a list search
filterF = (==) (persistUniqueToFieldNames unique) . uniqueFields
uniqueDef = head . filter filterF . getEntityUniques . entityDef $ proxy

Check warning on line 1436 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.8.1)

In the use of ‘head’

Check warning on line 1436 in src/Database/Esqueleto/Internal/Internal.hs

View workflow job for this annotation

GitHub Actions / build (3.10.2.1, 9.8.1)

In the use of ‘head’

-- | Render updates to be use in a SET clause for a given sql backend.
--
Expand Down
Loading