diff --git a/CHANGELOG.md b/CHANGELOG.md index 816a1bb38..3b566bd72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added ### Fixed +- Do not apply limit to parent items - @ruslantalpa ## [0.3.2.0] - 2016-06-10 diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 3b9f2f7b7..0a3c4722a 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -229,9 +229,9 @@ app dbStructure conf apiRequest = topLevelRange = fromMaybe allRange $ M.lookup "limit" $ iRange apiRequest readDbRequest = DbRead <$> buildReadRequest (configMaxRows conf) (dbRelations dbStructure) apiRequest mutateDbRequest = DbMutate <$> buildMutateRequest apiRequest - selectQuery = requestToQuery schema <$> readDbRequest + selectQuery = requestToQuery schema False <$> readDbRequest countQuery = requestToCountQuery schema <$> readDbRequest - mutateQuery = requestToQuery schema <$> mutateDbRequest + mutateQuery = requestToQuery schema False <$> mutateDbRequest readSqlParts = (,) <$> selectQuery <*> countQuery mutateSqlParts = (,) <$> selectQuery <*> mutateQuery respondToRange response = if topLevelRange == emptyRange diff --git a/src/PostgREST/QueryBuilder.hs b/src/PostgREST/QueryBuilder.hs index 246f993bc..621b5476f 100644 --- a/src/PostgREST/QueryBuilder.hs +++ b/src/PostgREST/QueryBuilder.hs @@ -271,10 +271,10 @@ requestToCountQuery schema (DbRead (Node (Select _ _ conditions _ _, (mainTbl, _ fn Filter{value=VForeignKey _ _} = False localConditions = filter fn conditions -requestToQuery :: Schema -> DbRequest -> SqlQuery -requestToQuery _ (DbMutate (Insert _ (PayloadParseError _))) = undefined -requestToQuery _ (DbMutate (Update _ (PayloadParseError _) _)) = undefined -requestToQuery schema (DbRead (Node (Select colSelects tbls conditions ord range, (nodeName, maybeRelation, _)) forest)) = +requestToQuery :: Schema -> Bool -> DbRequest -> SqlQuery +requestToQuery _ _ (DbMutate (Insert _ (PayloadParseError _))) = undefined +requestToQuery _ _ (DbMutate (Update _ (PayloadParseError _) _)) = undefined +requestToQuery schema isParent (DbRead (Node (Select colSelects tbls conditions ord range, (nodeName, maybeRelation, _)) forest)) = query where -- TODO! the folloing helper functions are just to remove the "schema" part when the table is "source" which is the name @@ -289,7 +289,7 @@ requestToQuery schema (DbRead (Node (Select colSelects tbls conditions ord range unwords joins, ("WHERE " <> intercalate " AND " ( map (pgFmtCondition qi ) conditions )) `emptyOnNull` conditions, orderF (fromMaybe [] ord), - limitF range + if isParent then "" else limitF range ] orderF ts = if null ts @@ -311,7 +311,7 @@ requestToQuery schema (DbRead (Node (Select colSelects tbls conditions ord range <> "SELECT array_to_json(array_agg(row_to_json("<>pgFmtIdent table<>"))) " <> "FROM (" <> subquery <> ") " <> pgFmtIdent table <> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) - where subquery = requestToQuery schema (DbRead (Node n forst)) + where subquery = requestToQuery schema False (DbRead (Node n forst)) getQueryParts (Node n@(_, (name, Just r@Relation{relType=Parent,relTable=Table{tableName=table}}, alias)) forst) (j,s) = (joi:j,sel:s) where node_name = fromMaybe name alias @@ -321,19 +321,19 @@ requestToQuery schema (DbRead (Node (Select colSelects tbls conditions ord range sel = "row_to_json(" <> pgFmtIdent local_table_name <> ".*) AS " <> pgFmtIdent node_name joi = " LEFT OUTER JOIN ( " <> subquery <> " ) AS " <> pgFmtIdent local_table_name <> " ON " <> intercalate " AND " ( map (pgFmtCondition qi . replaceTableName local_table_name) (getJoinConditions r) ) - where subquery = requestToQuery schema (DbRead (Node n forst)) + where subquery = requestToQuery schema True (DbRead (Node n forst)) getQueryParts (Node n@(_, (name, Just Relation{relType=Many,relTable=Table{tableName=table}}, alias)) forst) (j,s) = (j,sel:s) where sel = "COALESCE ((" <> "SELECT array_to_json(array_agg(row_to_json("<>pgFmtIdent table<>"))) " <> "FROM (" <> subquery <> ") " <> pgFmtIdent table <> "), '[]') AS " <> pgFmtIdent (fromMaybe name alias) - where subquery = requestToQuery schema (DbRead (Node n forst)) + where subquery = requestToQuery schema False (DbRead (Node n forst)) --the following is just to remove the warning --getQueryParts is not total but requestToQuery is called only after addJoinConditions which ensures the only --posible relations are Child Parent Many getQueryParts (Node (_,(_,Nothing,_)) _) _ = undefined -requestToQuery schema (DbMutate (Insert mainTbl (PayloadJSON (UniformObjects rows)))) = +requestToQuery schema _ (DbMutate (Insert mainTbl (PayloadJSON (UniformObjects rows)))) = let qi = QualifiedIdentifier schema mainTbl cols = map pgFmtIdent $ fromMaybe [] (HM.keys <$> (rows V.!? 0)) colsString = intercalate ", " cols @@ -345,7 +345,7 @@ requestToQuery schema (DbMutate (Insert mainTbl (PayloadJSON (UniformObjects row else ["SELECT", colsString, "FROM json_populate_recordset(null::" , fromQi qi, ", $1)"] in insInto <> vals -requestToQuery schema (DbMutate (Update mainTbl (PayloadJSON (UniformObjects rows)) conditions)) = +requestToQuery schema _ (DbMutate (Update mainTbl (PayloadJSON (UniformObjects rows)) conditions)) = case rows V.!? 0 of Just obj -> let assignments = map @@ -358,7 +358,7 @@ requestToQuery schema (DbMutate (Update mainTbl (PayloadJSON (UniformObjects row Nothing -> undefined where qi = QualifiedIdentifier schema mainTbl -requestToQuery schema (DbMutate (Delete mainTbl conditions)) = +requestToQuery schema _ (DbMutate (Delete mainTbl conditions)) = query where qi = QualifiedIdentifier schema mainTbl diff --git a/test/Feature/QueryLimitedSpec.hs b/test/Feature/QueryLimitedSpec.hs index 80d78006d..311fb83c0 100644 --- a/test/Feature/QueryLimitedSpec.hs +++ b/test/Feature/QueryLimitedSpec.hs @@ -35,3 +35,12 @@ spec = , matchStatus = 206 , matchHeaders = ["Content-Range" <:> "0-1/3"] } + + it "limit is not applied to parent embeds" $ + get "/tasks?select=id,project{id}&id=gt.5" + `shouldRespondWith` ResponseMatcher { + matchBody = Just [str|[{"id":6,"project":{"id":3}},{"id":7,"project":{"id":4}}]|] + , matchStatus = 206 + , matchHeaders = ["Content-Range" <:> "0-1/3"] + } +