refactor(sql): minor esqueleto refactoring

This commit is contained in:
Steffen Jost 2022-10-28 14:49:03 +02:00
parent fb82dcbb33
commit 173468f246

View File

@ -30,7 +30,7 @@ module Database.Esqueleto.Utils
, maybe, maybe2, maybeEq, guardMaybe, unsafeCoalesce
, bool
, max, min
, greatest
, greatest, least
, abs
, SqlProject(..)
, (->.), (#>>.)
@ -114,8 +114,10 @@ infixl 4 ?=.
isJust :: PersistField typ => E.SqlExpr (E.Value (Maybe typ)) -> E.SqlExpr (E.Value Bool)
isJust = E.not_ . E.isNothing
-- | Deprecated, use coalesce directly
alt :: PersistField typ => E.SqlExpr (E.Value (Maybe typ)) -> E.SqlExpr (E.Value (Maybe typ)) -> E.SqlExpr (E.Value (Maybe typ))
alt a b = E.case_ [(isJust a, a), (isJust b, b)] b
-- alt a b = E.case_ [(isJust a, a), (isJust b, b)] b
alt a b = E.coalesce [a,b]
infix 4 `isInfixOf`, `hasInfix`
@ -435,11 +437,12 @@ max, min :: PersistField a
max a b = bool a b $ b E.>. a
min a b = bool a b $ b E.<. a
-- these alternatives for max/min ought to be more efficient
greatest :: PersistField a => E.SqlExpr (E.Value a) -> E.SqlExpr (E.Value a) -> E.SqlExpr (E.Value a)
greatest a b = E.unsafeSqlFunction "GREATEST" $ E.toArgList (a,b)
greatest :: PersistField a => (E.SqlExpr (E.Value a), E.SqlExpr (E.Value a)) -> E.SqlExpr (E.Value a)
greatest = E.unsafeSqlFunction "GREATEST" . E.toArgList
least :: PersistField a => E.SqlExpr (E.Value a) -> E.SqlExpr (E.Value a) -> E.SqlExpr (E.Value a)
least a b = E.unsafeSqlFunction "LEAST" $ E.toArgList (a,b)
abs :: (PersistField a, Num a)
=> E.SqlExpr (E.Value a)