remove misleadingly named emptyOnFalse and similar (#1803)
This commit is contained in:
@@ -33,9 +33,6 @@ param = HE.param . HE.nonNullable
|
|||||||
arrayParam :: HE.Value a -> HE.Params [a]
|
arrayParam :: HE.Value a -> HE.Params [a]
|
||||||
arrayParam = param . HE.foldableArray . HE.nonNullable
|
arrayParam = param . HE.foldableArray . HE.nonNullable
|
||||||
|
|
||||||
emptySnippetOnFalse :: H.Snippet -> Bool -> H.Snippet
|
|
||||||
emptySnippetOnFalse val cond = if cond then mempty else val
|
|
||||||
|
|
||||||
intercalateSnippet :: ByteString -> [H.Snippet] -> H.Snippet
|
intercalateSnippet :: ByteString -> [H.Snippet] -> H.Snippet
|
||||||
intercalateSnippet _ [] = mempty
|
intercalateSnippet _ [] = mempty
|
||||||
intercalateSnippet frag snippets = foldr1 (\a b -> a <> H.sql frag <> b) snippets
|
intercalateSnippet frag snippets = foldr1 (\a b -> a <> H.sql frag <> b) snippets
|
||||||
|
|||||||
@@ -120,9 +120,6 @@ fromQi t = (if T.null s then mempty else pgFmtIdent s <> ".") <> pgFmtIdent n
|
|||||||
n = qiName t
|
n = qiName t
|
||||||
s = qiSchema t
|
s = qiSchema t
|
||||||
|
|
||||||
emptyOnFalse :: SqlFragment -> Bool -> SqlFragment
|
|
||||||
emptyOnFalse val cond = if cond then mempty else val
|
|
||||||
|
|
||||||
pgFmtColumn :: QualifiedIdentifier -> Text -> SqlFragment
|
pgFmtColumn :: QualifiedIdentifier -> Text -> SqlFragment
|
||||||
pgFmtColumn table "*" = fromQi table <> ".*"
|
pgFmtColumn table "*" = fromQi table <> ".*"
|
||||||
pgFmtColumn table c = fromQi table <> "." <> pgFmtIdent c
|
pgFmtColumn table c = fromQi table <> "." <> pgFmtIdent c
|
||||||
@@ -226,7 +223,7 @@ returningF qi returnings =
|
|||||||
|
|
||||||
limitOffsetF :: NonnegRange -> H.Snippet
|
limitOffsetF :: NonnegRange -> H.Snippet
|
||||||
limitOffsetF range =
|
limitOffsetF range =
|
||||||
("LIMIT " <> limit <> " OFFSET " <> offset) `emptySnippetOnFalse` (range == allRange)
|
if range == allRange then mempty else "LIMIT " <> limit <> " OFFSET " <> offset
|
||||||
where
|
where
|
||||||
limit = maybe "ALL" (\l -> unknownEncoder (BS.pack $ show l)) $ rangeLimit range
|
limit = maybe "ALL" (\l -> unknownEncoder (BS.pack $ show l)) $ rangeLimit range
|
||||||
offset = unknownEncoder (BS.pack . show $ rangeOffset range)
|
offset = unknownEncoder (BS.pack . show $ rangeOffset range)
|
||||||
|
|||||||
@@ -38,9 +38,9 @@ readRequestToQuery (Node (Select colSelects mainQi tblAlias implJoins logicFores
|
|||||||
intercalateSnippet ", " ((pgFmtSelectItem qi <$> colSelects) ++ selects) <>
|
intercalateSnippet ", " ((pgFmtSelectItem qi <$> colSelects) ++ selects) <>
|
||||||
"FROM " <> H.sql (BS.intercalate ", " (tabl : implJs)) <> " " <>
|
"FROM " <> H.sql (BS.intercalate ", " (tabl : implJs)) <> " " <>
|
||||||
intercalateSnippet " " joins <> " " <>
|
intercalateSnippet " " joins <> " " <>
|
||||||
("WHERE " <> intercalateSnippet " AND " (map (pgFmtLogicTree qi) logicForest ++ map pgFmtJoinCondition joinConditions_))
|
(if null logicForest && null joinConditions_ then mempty else "WHERE " <> intercalateSnippet " AND " (map (pgFmtLogicTree qi) logicForest ++ map pgFmtJoinCondition joinConditions_))
|
||||||
`emptySnippetOnFalse` (null logicForest && null joinConditions_) <> " " <>
|
<> " " <>
|
||||||
(("ORDER BY " <> intercalateSnippet ", " (map (pgFmtOrderTerm qi) ordts)) `emptySnippetOnFalse` null ordts) <> " " <>
|
(if null ordts then mempty else "ORDER BY " <> intercalateSnippet ", " (map (pgFmtOrderTerm qi) ordts)) <> " " <>
|
||||||
limitOffsetF range
|
limitOffsetF range
|
||||||
where
|
where
|
||||||
implJs = fromQi <$> implJoins
|
implJs = fromQi <$> implJoins
|
||||||
@@ -73,17 +73,20 @@ mutateRequestToQuery (Insert mainQi iCols body onConflct putConditions returning
|
|||||||
"SELECT " <> H.sql cols <> " " <>
|
"SELECT " <> H.sql cols <> " " <>
|
||||||
H.sql ("FROM json_populate_recordset (null::" <> fromQi mainQi <> ", " <> selectBody <> ") _ ") <>
|
H.sql ("FROM json_populate_recordset (null::" <> fromQi mainQi <> ", " <> selectBody <> ") _ ") <>
|
||||||
-- Only used for PUT
|
-- Only used for PUT
|
||||||
("WHERE " <> intercalateSnippet " AND " (pgFmtLogicTree (QualifiedIdentifier mempty "_") <$> putConditions)) `emptySnippetOnFalse` null putConditions <>
|
(if null putConditions then mempty else "WHERE " <> intercalateSnippet " AND " (pgFmtLogicTree (QualifiedIdentifier mempty "_") <$> putConditions)) <>
|
||||||
H.sql (BS.unwords [
|
H.sql (BS.unwords [
|
||||||
maybe "" (\(oncDo, oncCols) -> (
|
maybe "" (\(oncDo, oncCols) ->
|
||||||
"ON CONFLICT(" <> BS.intercalate ", " (pgFmtIdent <$> oncCols) <> ") " <> case oncDo of
|
if null oncCols then
|
||||||
IgnoreDuplicates ->
|
mempty
|
||||||
"DO NOTHING"
|
else
|
||||||
MergeDuplicates ->
|
"ON CONFLICT(" <> BS.intercalate ", " (pgFmtIdent <$> oncCols) <> ") " <> case oncDo of
|
||||||
if S.null iCols
|
IgnoreDuplicates ->
|
||||||
then "DO NOTHING"
|
"DO NOTHING"
|
||||||
else "DO UPDATE SET " <> BS.intercalate ", " (pgFmtIdent <> const " = EXCLUDED." <> pgFmtIdent <$> S.toList iCols)
|
MergeDuplicates ->
|
||||||
) `emptyOnFalse` null oncCols) onConflct,
|
if S.null iCols
|
||||||
|
then "DO NOTHING"
|
||||||
|
else "DO UPDATE SET " <> BS.intercalate ", " (pgFmtIdent <> const " = EXCLUDED." <> pgFmtIdent <$> S.toList iCols)
|
||||||
|
) onConflct,
|
||||||
returningF mainQi returnings
|
returningF mainQi returnings
|
||||||
])
|
])
|
||||||
where
|
where
|
||||||
@@ -98,7 +101,7 @@ mutateRequestToQuery (Update mainQi uCols body logicForest returnings) =
|
|||||||
"WITH " <> normalizedBody body <> " " <>
|
"WITH " <> normalizedBody body <> " " <>
|
||||||
"UPDATE " <> H.sql (fromQi mainQi) <> " SET " <> H.sql cols <> " " <>
|
"UPDATE " <> H.sql (fromQi mainQi) <> " SET " <> H.sql cols <> " " <>
|
||||||
"FROM (SELECT * FROM json_populate_recordset (null::" <> H.sql (fromQi mainQi) <> " , " <> H.sql selectBody <> " )) _ " <>
|
"FROM (SELECT * FROM json_populate_recordset (null::" <> H.sql (fromQi mainQi) <> " , " <> H.sql selectBody <> " )) _ " <>
|
||||||
("WHERE " <> intercalateSnippet " AND " (pgFmtLogicTree mainQi <$> logicForest)) `emptySnippetOnFalse` null logicForest <> " " <>
|
(if null logicForest then mempty else "WHERE " <> intercalateSnippet " AND " (pgFmtLogicTree mainQi <$> logicForest)) <> " " <>
|
||||||
H.sql (returningF mainQi returnings)
|
H.sql (returningF mainQi returnings)
|
||||||
where
|
where
|
||||||
cols = BS.intercalate ", " (pgFmtIdent <> const " = _." <> pgFmtIdent <$> S.toList uCols)
|
cols = BS.intercalate ", " (pgFmtIdent <> const " = _." <> pgFmtIdent <$> S.toList uCols)
|
||||||
@@ -108,7 +111,7 @@ mutateRequestToQuery (Update mainQi uCols body logicForest returnings) =
|
|||||||
| otherwise = BS.intercalate ", " (pgFmtColumn (QualifiedIdentifier mempty $ qiName mainQi) <$> returnings)
|
| otherwise = BS.intercalate ", " (pgFmtColumn (QualifiedIdentifier mempty $ qiName mainQi) <$> returnings)
|
||||||
mutateRequestToQuery (Delete mainQi logicForest returnings) =
|
mutateRequestToQuery (Delete mainQi logicForest returnings) =
|
||||||
"DELETE FROM " <> H.sql (fromQi mainQi) <> " " <>
|
"DELETE FROM " <> H.sql (fromQi mainQi) <> " " <>
|
||||||
("WHERE " <> intercalateSnippet " AND " (map (pgFmtLogicTree mainQi) logicForest)) `emptySnippetOnFalse` null logicForest <> " " <>
|
(if null logicForest then mempty else "WHERE " <> intercalateSnippet " AND " (map (pgFmtLogicTree mainQi) logicForest)) <> " " <>
|
||||||
H.sql (returningF mainQi returnings)
|
H.sql (returningF mainQi returnings)
|
||||||
|
|
||||||
requestToCallProcQuery :: QualifiedIdentifier -> [PgArg] -> Maybe PayloadJSON -> Bool -> Maybe PreferParameters -> [FieldName] -> H.Snippet
|
requestToCallProcQuery :: QualifiedIdentifier -> [PgArg] -> Maybe PayloadJSON -> Bool -> Maybe PreferParameters -> [FieldName] -> H.Snippet
|
||||||
@@ -168,7 +171,7 @@ requestToCallProcQuery qi pgArgs pj returnsScalar preferParams returnings =
|
|||||||
readRequestToCountQuery :: ReadRequest -> H.Snippet
|
readRequestToCountQuery :: ReadRequest -> H.Snippet
|
||||||
readRequestToCountQuery (Node (Select{from=qi, where_=logicForest}, _) _) =
|
readRequestToCountQuery (Node (Select{from=qi, where_=logicForest}, _) _) =
|
||||||
"SELECT 1 " <> "FROM " <> H.sql (fromQi qi) <> " " <>
|
"SELECT 1 " <> "FROM " <> H.sql (fromQi qi) <> " " <>
|
||||||
("WHERE " <> intercalateSnippet " AND " (map (pgFmtLogicTree qi) logicForest)) `emptySnippetOnFalse` null logicForest
|
if null logicForest then mempty else "WHERE " <> intercalateSnippet " AND " (map (pgFmtLogicTree qi) logicForest)
|
||||||
|
|
||||||
limitedQuery :: H.Snippet -> Maybe Integer -> H.Snippet
|
limitedQuery :: H.Snippet -> Maybe Integer -> H.Snippet
|
||||||
limitedQuery query maxRows = query <> H.sql (maybe mempty (\x -> " LIMIT " <> BS.pack (show x)) maxRows)
|
limitedQuery query maxRows = query <> H.sql (maybe mempty (\x -> " LIMIT " <> BS.pack (show x)) maxRows)
|
||||||
|
|||||||
Reference in New Issue
Block a user