public static function &doPivot(Query $query, DataTable &$data) { // If there is no pivot clause specified in the query, // return the source data $pivot = $query->getPivot(); if ($pivot === null) { return $data; } // Generate a pivot description so we know what fields are // used for what. $pivotDescription = new PivotDescription($data); $pivotDescription->setColumnFields($pivot->getAllColumnIds()); $select = $query->getSelect(); if ($select !== null) { $numSelectedFields = $select->getNumberOfColumns(); for ($i = 0; $i < $numSelectedFields; $i++) { $fieldId = $select->getColumnText($i); $func = $select->getColumnFunction($i); // adds a Row field if $func is null, else adds a data field $pivotDescription->addField($fieldId, $func); } } $pivotAction = new PivotAction($pivotDescription); $result =& $pivotAction->execute(); return $result; }
public function testExecuteWithOnlyPivot() { $text = 'pivot country, region'; $query = Query::constructFromString($text); $result =& QueryEngine::execute($query, $this->dataTable); $expected = 7; $actual = $result->getNumberOfColumns(); $this->assertSame($expected, $actual, "Expect day column plus 1 for each country+region pair == 7"); $expected = 3; $actual = $result->getNumberOfRows(); $this->assertSame($expected, $actual, "Expect 3 rows (1 for each day)"); }
/** * Parse the query * @throws \Exception any number of exceptions may be thrown on errors * @return Query */ public function parse() { if ($this->query->isEmpty()) { return $this->query; } $text = $this->query->getText(); $this->query->setSelect($this->readSelect($text)); $this->readWhere($text); $this->readGroupBy($text); $this->query->setPivot($this->readPivot($text)); $this->readOrderBy($text); $this->readSkipping($text); $this->readLimit($text); $this->readOffset($text); $this->readLabel($text); $this->readFormat($text); $this->readOptions($text); if ($text !== '') { throw new InvalidQueryException("Syntax error near: " . $text); } return $this->query; }
/** * @param Response &$response [required] [IN] [OUT] */ protected function executeTrue(Response &$response) { $request = $response->getRequest(); // Verify that the user is granted access to the data if ($this->isRestrictedAccessMode()) { $this->verifyAccessAllowed($request->getOutputType()); } // Populate the data $data =& $this->getDataTable($request); // Apply query, if any $query = Query::constructFromString($request->getQuery()); if (!$query->isEmpty()) { $data =& QueryEngine::execute($query, $data); } $response->setDataTable($data); }