From 16059ad470a3ed2ce86627015bb2461a2b6b5dfa Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Sun, 10 Feb 2019 17:49:20 -0500 Subject: [PATCH] Remove pjType from ActionCreate * Remove isSingle from ActionCreate * Remove nRows from ActionCreate --- src/PostgREST/App.hs | 58 +++++++++++++++++------------------ src/PostgREST/QueryBuilder.hs | 13 +++++--- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 99ecbfc97..6dec3da19 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -146,41 +146,39 @@ app dbStructure proc conf apiRequest = ) ] (toS body) - (ActionCreate, TargetIdent (QualifiedIdentifier tSchema tName), Just PayloadJSON{pjRaw, pjType}) -> + (ActionCreate, TargetIdent (QualifiedIdentifier tSchema tName), Just PayloadJSON{pjRaw}) -> case mutateSqlParts tSchema tName of Left errorResponse -> return errorResponse Right (sq, mq) -> do - let (isSingle, nRows) = case pjType of - PJArray len -> (len == 1, len) - PJObject -> (True, 1) + let pkCols = tablePKCols dbStructure tSchema tName + stm = createWriteStatement sq mq + (contentType == CTSingularJSON) True + (contentType == CTTextCSV) (iPreferRepresentation apiRequest) pkCols + row <- H.statement (toS pjRaw) stm + let (_, queryTotal, fs, body) = extractQueryResult row + headers = catMaybes [ + if null fs + then Nothing + else Just (hLocation, "/" <> toS tName <> renderLocationFields fs) + , if iPreferRepresentation apiRequest == Full + then Just $ toHeader contentType + else Nothing + , Just . contentRangeH 1 0 $ + toInteger <$> if shouldCount then Just queryTotal else Nothing + , if null pkCols + then Nothing + else (\x -> ("Preference-Applied", show x)) <$> iPreferResolution apiRequest + ] if contentType == CTSingularJSON - && not isSingle + && queryTotal /= 1 && iPreferRepresentation apiRequest == Full - then return $ singularityError (toInteger nRows) - else do - let pkCols = tablePKCols dbStructure tSchema tName - stm = createWriteStatement sq mq - (contentType == CTSingularJSON) isSingle - (contentType == CTTextCSV) (iPreferRepresentation apiRequest) pkCols - row <- H.statement (toS pjRaw) stm - let (_, _, fs, body) = extractQueryResult row - headers = catMaybes [ - if null fs - then Nothing - else Just (hLocation, "/" <> toS tName <> renderLocationFields fs) - , if iPreferRepresentation apiRequest == Full - then Just $ toHeader contentType - else Nothing - , Just . contentRangeH 1 0 $ - toInteger <$> if shouldCount then Just nRows else Nothing - , if null pkCols - then Nothing - else (\x -> ("Preference-Applied", show x)) <$> iPreferResolution apiRequest - ] - - return . responseLBS status201 headers $ - if iPreferRepresentation apiRequest == Full - then toS body else "" + then do + HT.condemn + return $ singularityError (toInteger queryTotal) + else + return . responseLBS status201 headers $ + if iPreferRepresentation apiRequest == Full + then toS body else "" (ActionUpdate, TargetIdent (QualifiedIdentifier tSchema tName), Just p@PayloadJSON{pjRaw}) -> case (mutateSqlParts tSchema tName, pjIsEmpty p, iPreferRepresentation apiRequest == Full) of diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs index 1406faede..e679c6587 100644 --- a/src/PostgREST/QueryBuilder.hs +++ b/src/PostgREST/QueryBuilder.hs @@ -103,7 +103,7 @@ createReadStatement selectQuery countQuery isSingle countTotal asCsv binaryField createWriteStatement :: SqlQuery -> SqlQuery -> Bool -> Bool -> Bool -> PreferRepresentation -> [Text] -> H.Statement ByteString (Maybe ResultsWithCount) -createWriteStatement selectQuery mutateQuery wantSingle wantHdrs asCsv rep pKeys = +createWriteStatement selectQuery mutateQuery wantSingle isInsert asCsv rep pKeys = unicodeStatement sql (HE.param HE.unknown) decodeStandardMay True where @@ -123,9 +123,14 @@ createWriteStatement selectQuery mutateQuery wantSingle wantHdrs asCsv rep pKeys cols = intercalate ", " [ "'' AS total_result_set", -- when updateing it does not make sense "pg_catalog.count(_postgrest_t) AS page_total", - if wantHdrs - then "coalesce(" <> locationF pKeys <> ", " <> noLocationF <> ")" - else noLocationF <> " AS header", + if isInsert + then unwords [ + "CASE", + "WHEN pg_catalog.count(_postgrest_t) = 1 THEN", + "coalesce(" <> locationF pKeys <> ", " <> noLocationF <> ")", + "ELSE " <> noLocationF, + "END AS header"] + else noLocationF <> "AS header", if rep == Full then bodyF <> " AS body" else "''"