From f159233de8040e7191f6e1ec431351cb958e7839 Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Tue, 24 Oct 2017 10:51:52 -0500 Subject: [PATCH] Change UPDATE query to a parametrized query --- src/PostgREST/QueryBuilder.hs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs index 157e1c7c3..0f5b1ca33 100644 --- a/src/PostgREST/QueryBuilder.hs +++ b/src/PostgREST/QueryBuilder.hs @@ -302,11 +302,10 @@ requestToQuery schema _ (DbMutate (Insert mainTbl (PayloadJSON rows) returnings) requestToQuery schema _ (DbMutate (Update mainTbl (PayloadJSON rows) logicForest returnings)) = case rows V.!? 0 of Just obj -> - let assignments = map - (\(k,v) -> pgFmtIdent k <> "=" <> insertableValue v) $ HM.toList obj in + let cols = intercalate ", " (pgFmtIdent <> const " = _." <> pgFmtIdent <$> HM.keys obj) in unwords [ "UPDATE ", fromQi qi, - " SET " <> intercalate "," assignments <> " ", + " SET " <> cols <> " FROM (SELECT * FROM json_populate_recordset(null::" <> fromQi qi <> ", $1)) _ ", ("WHERE " <> intercalate " AND " (map (pgFmtLogicTree qi) logicForest)) `emptyOnFalse` null logicForest, ("RETURNING " <> intercalate ", " (map (pgFmtColumn qi) returnings)) `emptyOnFalse` null returnings ] @@ -389,10 +388,6 @@ unicodeStatement = H.statement . T.encodeUtf8 emptyOnFalse :: Text -> Bool -> Text emptyOnFalse val cond = if cond then "" else val -insertableValue :: JSON.Value -> SqlFragment -insertableValue JSON.Null = "null" -insertableValue v = (<> "::unknown") . pgFmtLit $ unquoted v - pgFmtColumn :: QualifiedIdentifier -> Text -> SqlFragment pgFmtColumn table "*" = fromQi table <> ".*" pgFmtColumn table c = fromQi table <> "." <> pgFmtIdent c