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 testAggregateCount() { $pivotDescription = new PivotDescription($this->dataTable, array('country')); $pivotDescription->addDataField('income', AggregationType::COUNT); $pivotAction = new PivotAction($pivotDescription); $data =& $pivotAction->execute(); $this->assertSame(2, $data->getNumberOfColumns()); $this->assertSame(3, $data->getNumberOfRows()); $col1 = $data->getColumnDescription(1); $this->assertSame('count US Income', $col1->getLabel()); $this->assertSame(3, $data->getRow(0)->getCell(1)->getValue()->getRawValue(), "Expect count(income) = 3 for US on day 1"); $this->assertSame(2, $data->getRow(1)->getCell(1)->getValue()->getRawValue(), "Expect count(income) = 2 for US on day 2"); $this->assertSame(2, $data->getRow(2)->getCell(1)->getValue()->getRawValue(), "Expect count(income) = 2 for US on day 3"); }