Skip to content

Commit

Permalink
better nullability
Browse files Browse the repository at this point in the history
  • Loading branch information
parsonsmatt committed Dec 30, 2024
1 parent 13fcf89 commit 1abf4ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
4 changes: 0 additions & 4 deletions src/Database/Esqueleto/Experimental/ToMaybe.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ module Database.Esqueleto.Experimental.ToMaybe
import Database.Esqueleto.Internal.Internal hiding (From(..), from, on)
import Database.Esqueleto.Internal.PersistentImport (Entity(..))

type family Nullable a where
Nullable (Maybe a) = a
Nullable a = a

class ToMaybe a where
type ToMaybeT a
toMaybe :: a -> ToMaybeT a
Expand Down
27 changes: 15 additions & 12 deletions src/Database/Esqueleto/Internal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ sub_select = sub SELECT
subSelect
:: PersistField a
=> SqlQuery (SqlExpr (Value a))
-> SqlExpr (Value (Maybe a))
-> SqlExpr (Value (Maybe (Nullable a)))
subSelect query = just (subSelectUnsafe (query <* limit 1))

-- | Execute a subquery @SELECT@ in a 'SqlExpr'. This function is a shorthand
Expand All @@ -486,7 +486,7 @@ subSelect query = just (subSelectUnsafe (query <* limit 1))
subSelectMaybe
:: PersistField a
=> SqlQuery (SqlExpr (Value (Maybe a)))
-> SqlExpr (Value (Maybe a))
-> SqlExpr (Value (Maybe (Nullable a)))
subSelectMaybe = joinV . subSelect

-- | Performs a @COUNT@ of the given query in a @subSelect@ manner. This is
Expand Down Expand Up @@ -630,7 +630,7 @@ withNonNull field f = do
=> SqlExpr (Maybe (Entity val))
-> EntityField val typ
-> SqlExpr (Value (Maybe typ'))
ERaw m f ?. field = just (ERaw m f ^. field)
ERaw m f ?. field = veryUnsafeCoerceSqlExprValue (ERaw m f ^. field)

-- | Lift a constant value from Haskell-land to the query.
val :: PersistField typ => typ -> SqlExpr (Value typ)
Expand Down Expand Up @@ -686,9 +686,8 @@ isNothing_ = isNothing
-- This function will not produce a nested 'Maybe'. This is in accord with
-- how SQL represents @NULL@. That means that @'just' . 'just' = 'just'@.
just
:: (NullableFieldProjection typ typ')
=> SqlExpr (Value typ)
-> SqlExpr (Value (Maybe typ'))
:: SqlExpr (Value typ)
-> SqlExpr (Value (Maybe (Nullable typ)))
just = veryUnsafeCoerceSqlExprValue

-- | @NULL@ value.
Expand All @@ -698,9 +697,8 @@ nothing = unsafeSqlValue "NULL"
-- | Join nested 'Maybe's in a 'Value' into one. This is useful when
-- calling aggregate functions on nullable fields.
joinV
:: NullableFieldProjection typ typ'
=> SqlExpr (Value (Maybe typ))
-> SqlExpr (Value (Maybe typ'))
:: SqlExpr (Value (Maybe typ))
-> SqlExpr (Value (Maybe (Nullable typ)))
joinV = veryUnsafeCoerceSqlExprValue


Expand Down Expand Up @@ -911,9 +909,10 @@ floor_ = unsafeSqlFunction "FLOOR"

sum_ :: (PersistField a, PersistField b) => SqlExpr (Value a) -> SqlExpr (Value (Maybe b))
sum_ = unsafeSqlFunction "SUM"
min_ :: (PersistField a) => SqlExpr (Value a) -> SqlExpr (Value (Maybe a))

min_ :: (PersistField a) => SqlExpr (Value a) -> SqlExpr (Value (Maybe (Nullable a)))
min_ = unsafeSqlFunction "MIN"
max_ :: (PersistField a) => SqlExpr (Value a) -> SqlExpr (Value (Maybe a))
max_ :: (PersistField a) => SqlExpr (Value a) -> SqlExpr (Value (Maybe (Nullable a)))
max_ = unsafeSqlFunction "MAX"
avg_ :: (PersistField a, PersistField b) => SqlExpr (Value a) -> SqlExpr (Value (Maybe b))
avg_ = unsafeSqlFunction "AVG"
Expand Down Expand Up @@ -959,7 +958,7 @@ coalesce = unsafeSqlFunctionParens "COALESCE"
-- a non-NULL result.
--
-- @since 1.4.3
coalesceDefault :: PersistField a => [SqlExpr (Value (Maybe a))] -> SqlExpr (Value a) -> SqlExpr (Value a)
coalesceDefault :: PersistField a => [SqlExpr (Value (Maybe (Nullable a)))] -> SqlExpr (Value a) -> SqlExpr (Value a)
coalesceDefault exprs = unsafeSqlFunctionParens "COALESCE" . (exprs ++) . return . just

-- | @LOWER@ function.
Expand Down Expand Up @@ -2519,6 +2518,10 @@ class NullableFieldProjection typ typ'
instance {-# incoherent #-} (typ ~ typ') => NullableFieldProjection (Maybe typ) typ'
instance {-# overlappable #-} (typ ~ typ') => NullableFieldProjection typ typ'

type family Nullable a where
Nullable (Maybe a) = a
Nullable a = a

-- | Data type to support from hack
data PreprocessedFrom a = PreprocessedFrom a FromClause

Expand Down

0 comments on commit 1abf4ce

Please sign in to comment.