/** * For queries like http://localhost/NorthWind.svc/Customers */ public function getResourceSet(QueryType $queryType, ResourceSet $resourceSet, $filterInfo = null, $orderBy = null, $top = null, $skip = null) { $result = new QueryResult(); $entityClassName = $resourceSet->getResourceType()->getInstanceType()->name; $entityName = $this->getEntityName($entityClassName); $tableName = $this->getTableName($entityName); $option = null; if ($queryType == QueryType::ENTITIES_WITH_COUNT()) { //tell mysql we want to know the count prior to the LIMIT //$option = 'SQL_CALC_FOUND_ROWS'; } $where = $filterInfo ? ' WHERE ' . $filterInfo->getExpressionAsString() : ''; $order = $orderBy ? ' ORDER BY ' . $this->getOrderByExpressionAsString($orderBy) : ''; $sqlCount = 'SELECT COUNT(*) FROM ' . $tableName . $where; if ($queryType == QueryType::ENTITIES() || $queryType == QueryType::ENTITIES_WITH_COUNT()) { $sql = 'SELECT ' . $option . ' * FROM ' . $tableName . $where . $order . ($top ? ' LIMIT ' . $top : '') . ($skip ? ' OFFSET ' . $skip : ''); $data = $this->queryAll($sql); if ($queryType == QueryType::ENTITIES_WITH_COUNT()) { //get those found rows //$result->count = $this->queryScalar('SELECT FOUND_ROWS()'); $result->count = $this->queryScalar($sqlCount); } $result->results = array_map($entityClassName . '::fromRecord', $data); } elseif ($queryType == QueryType::COUNT()) { $result->count = QueryResult::adjustCountForPaging($this->queryScalar($sqlCount), $top, $skip); } return $result; }
/** * @dataProvider provider */ public function testAdjustCountForPaging($id, $count, $top, $skip, $expected) { $actual = QueryResult::adjustCountForPaging($count, $top, $skip); $this->assertEquals($expected, $actual, $id); }