/** * Applies the query options to the resource(s) retrieved from the data source. * * @param SegmentDescriptor $segment The descriptor which holds resource(s) on which query options to be applied. * */ private function applyQueryOptions(SegmentDescriptor $segment) { //TODO: I'm not really happy with this..i think i'd rather keep the result the QueryResult //not even bother with the setCountValue stuff (shouldn't counts be on segments?) //and just work with the QueryResult in the object model serializer $result = $segment->getResult(); if (!$result instanceof QueryResult) { //If the segment isn't a query result, then there's no paging or counting to be done return; } // Note $inlinecount=allpages means include the total count regardless of paging..so we set the counts first // regardless if POData does the paging or not. if ($this->request->queryType == QueryType::ENTITIES_WITH_COUNT()) { if ($this->providers->handlesOrderedPaging()) { $this->request->setCountValue($result->count); } else { $this->request->setCountValue(count($result->results)); } } //Have POData perform paging if necessary if (!$this->providers->handlesOrderedPaging() && !empty($result->results)) { $result->results = $this->performPaging($result->results); } //a bit surprising, but $skip and $top affects $count so update it here, not above //IE data.svc/Collection/$count?$top=10 returns 10 even if Collection has 11+ entries if ($this->request->queryType == QueryType::COUNT()) { if ($this->providers->handlesOrderedPaging()) { $this->request->setCountValue($result->count); } else { $this->request->setCountValue(count($result->results)); } } $segment->setResult($result->results); }
public function testProcessRequestForCollectionWithInlineCountProviderHandlesPaging() { $requestURI = new Url('http://host.com/data.svc/Collection/?$inlinecount=allpages'); Phockito::when($this->mockServiceHost->getAbsoluteRequestUri())->return($requestURI); //mock inline count as all pages Phockito::when($this->mockServiceHost->getQueryStringItem(ODataConstants::HTTPQUERY_STRING_INLINECOUNT))->return("allpages"); $this->fakeServiceConfig->setAcceptCountRequests(true); $this->fakeServiceConfig->setMaxDataServiceVersion(ProtocolVersion::V2()); $uriProcessor = UriProcessor::process($this->mockService); $fakeQueryResult = new QueryResult(); $fakeQueryResult->results = array(1, 2, 3); $fakeQueryResult->count = 10; Phockito::when($this->mockProvidersWrapper->getResourceSet(QueryType::ENTITIES_WITH_COUNT(), $this->mockCollectionResourceSetWrapper, null, null, null, null))->return($fakeQueryResult); //indicate that the Provider performs the paging (thus it will use the count in the QueryResult) Phockito::when($this->mockProvidersWrapper->handlesOrderedPaging())->return(true); $uriProcessor->execute(); $request = $uriProcessor->getRequest(); $actual = $request->getTargetResult(); $this->assertEquals(array(1, 2, 3), $actual); $this->assertEquals(10, $request->getCountValue()); }