Fix coalesce/coalesceDefault for sub-queries
If COALESCE has a sub-query as an argument, then this sub-query needs to remain wrapped in an extra pair of parentheses.
This commit is contained in:
parent
abf91a1352
commit
f22a11d989
@ -376,8 +376,8 @@ instance Esqueleto SqlQuery SqlExpr SqlBackend where
|
|||||||
min_ = unsafeSqlFunction "MIN"
|
min_ = unsafeSqlFunction "MIN"
|
||||||
max_ = unsafeSqlFunction "MAX"
|
max_ = unsafeSqlFunction "MAX"
|
||||||
|
|
||||||
coalesce = unsafeSqlFunction "COALESCE"
|
coalesce = unsafeSqlFunctionParens "COALESCE"
|
||||||
coalesceDefault exprs = unsafeSqlFunction "COALESCE" . (exprs ++) . return . just
|
coalesceDefault exprs = unsafeSqlFunctionParens "COALESCE" . (exprs ++) . return . just
|
||||||
|
|
||||||
like = unsafeSqlBinOp " LIKE "
|
like = unsafeSqlBinOp " LIKE "
|
||||||
(%) = unsafeSqlValue "'%'"
|
(%) = unsafeSqlValue "'%'"
|
||||||
@ -524,6 +524,16 @@ unsafeSqlExtractSubField subField arg =
|
|||||||
uncommas' $ map (\(ERaw _ f) -> f info) $ toArgList arg
|
uncommas' $ map (\(ERaw _ f) -> f info) $ toArgList arg
|
||||||
in ("EXTRACT" <> parens (subField <> " FROM " <> argsTLB), argsVals)
|
in ("EXTRACT" <> parens (subField <> " FROM " <> argsTLB), argsVals)
|
||||||
|
|
||||||
|
-- | (Internal) A raw SQL function. Preserves parentheses around arguments.
|
||||||
|
-- See 'unsafeSqlBinOp' for warnings.
|
||||||
|
unsafeSqlFunctionParens :: UnsafeSqlFunctionArgument a =>
|
||||||
|
TLB.Builder -> a -> SqlExpr (Value b)
|
||||||
|
unsafeSqlFunctionParens name arg =
|
||||||
|
ERaw Never $ \info ->
|
||||||
|
let (argsTLB, argsVals) =
|
||||||
|
uncommas' $ map (\(ERaw p f) -> first (parensM p) (f info)) $ toArgList arg
|
||||||
|
in (name <> parens argsTLB, argsVals)
|
||||||
|
|
||||||
|
|
||||||
class UnsafeSqlFunctionArgument a where
|
class UnsafeSqlFunctionArgument a where
|
||||||
toArgList :: a -> [SqlExpr (Value ())]
|
toArgList :: a -> [SqlExpr (Value ())]
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user