From dd16400d64ad4e03ccd971b1d2ad38e38bf1d122 Mon Sep 17 00:00:00 2001 From: Maximilian Tagher Date: Wed, 26 Aug 2020 14:36:41 -0400 Subject: [PATCH] Document isNothing (#203) * Document isNothing I have two goals with this documentation: 1. Surface how to do `IS NOT NULL`. This PR makes a search for that string turn up a result, and directs you to combine `isNothing` with `not_`. 2. Documents a major gotcha where behavior between Persistent and Esqueleto is different. I haven't tested this in awhile, but we run into this gotcha occassionally, so I'm pretty confident it's still an issue. * .. * .. Co-authored-by: Matt Parsons --- changelog.md | 2 ++ src/Database/Esqueleto/Internal/Internal.hs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/changelog.md b/changelog.md index 364ffc1..ae43f48 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,7 @@ 3.3.3.3 ======== +- @maxgabriel + - [#203](https://github.com/bitemyapp/esqueleto/pull/203) Document `isNothing` - @sestrella - [#198](https://github.com/bitemyapp/esqueleto/pull/198) - Allow PostgreSQL aggregate functions to take a filter clause diff --git a/src/Database/Esqueleto/Internal/Internal.hs b/src/Database/Esqueleto/Internal/Internal.hs index b65fe30..5e89f68 100644 --- a/src/Database/Esqueleto/Internal/Internal.hs +++ b/src/Database/Esqueleto/Internal/Internal.hs @@ -579,6 +579,22 @@ val :: PersistField typ => typ -> SqlExpr (Value typ) val v = ERaw Never $ const ("?", [toPersistValue v]) -- | @IS NULL@ comparison. +-- +-- For @IS NOT NULL@, you can negate this with 'not_', as in @not_ (isNothing (person ^. PersonAge))@ +-- +-- Warning: Persistent and Esqueleto have different behavior for @!= Nothing@: +-- +-- +----------------+----------------------------------+---------------+ +-- | | Haskell | SQL | +-- +================+==================================+===============+ +-- | __Persistent__ | @'Database.Persist.!=.' Nothing@ | @IS NOT NULL@ | +-- +----------------+----------------------------------+---------------+ +-- | __Esqueleto__ | @'!=.' Nothing@ | @!= NULL@ | +-- +----------------+----------------------------------+---------------+ +-- +-- In SQL, @= NULL@ and @!= NULL@ return NULL instead of true or false. For this reason, you very likely do not want to use @'!=.' Nothing@ in Esqueleto. + + isNothing :: PersistField typ => SqlExpr (Value (Maybe typ)) -> SqlExpr (Value Bool) isNothing v = case v of