public static function prepareTableSection(AbstractSQLDataSourceQueryHandler $datasourceHandler, DataControllerCallContext $callcontext, array &$requestedColumnNames = NULL) { $metamodel = data_controller_get_metamodel(); $tableQuarters = new DatasetSection($metamodel->getDataset(DateDimensionDatasetNames::QUARTERS), self::$TABLE_ALIAS_SUFFIX__QUARTERS); self::selectColumn($tableQuarters, $requestedColumnNames, FALSE, 'quarter_def_id', FALSE); self::selectColumn($tableQuarters, $requestedColumnNames, FALSE, 'year_id', FALSE); // calculated field: first day of quarter $expressionFirstDayOfQuarter = $datasourceHandler->concatenateValues(array('(' . ColumnStatementCompositeEntityParser::assembleColumnName(self::$TABLE_ALIAS_SUFFIX__QUARTER_DEF . '.series') . ' - 1) * 3 + 1', "'/1/'", ColumnStatementCompositeEntityParser::assembleColumnName(DateDimensionYearDatasetAssembler::$TABLE_ALIAS_SUFFIX__YEARS . '.entry_year'))); $expressionFirstDayOfQuarter = $datasourceHandler->formatDateValue($expressionFirstDayOfQuarter, 'm/d/Y'); $columnFirstDayOfQuarter = self::selectColumn($tableQuarters, $requestedColumnNames, FALSE, $expressionFirstDayOfQuarter, TRUE, 'quarter_first_date'); if (isset($columnFirstDayOfQuarter)) { // registering required column for this functionality to work. This will indirectly force joining with required tables self::registerDependentColumnName($requestedColumnNames, 'quarter_series'); self::registerDependentColumnName($requestedColumnNames, 'entry_year'); } $columnQuarterId = self::selectColumn($tableQuarters, $requestedColumnNames, isset($tableQuarters->columns), 'quarter_id', FALSE); if (isset($columnQuarterId)) { $columnQuarterId->key = TRUE; } return $tableQuarters; }
public function assemble(AbstractSQLDataSourceQueryHandler $datasourceHandler, AbstractQueryRequest $request, DatasetMetaData $dataset, array $columnNames = NULL) { $statement = new Statement(); $TABLE_ALIAS__FACTS = 'dsf'; $TABLE_ALIAS__COLUMN_JOIN = 'cj'; $metamodel = data_controller_get_metamodel(); $cubeName = $dataset->name; $cube = $metamodel->getCube($cubeName); $factsDataset = $metamodel->getDataset($cube->factsDatasetName); self::$TABLE_ALIAS_SUFFIX__SEQUENCE++; $factsTable = new DatasetSection($factsDataset, $TABLE_ALIAS__FACTS . self::$TABLE_ALIAS_SUFFIX__SEQUENCE); $statement->tables[] = $factsTable; foreach ($dataset->getColumns() as $column) { $columnName = $column->name; $columnDefaultAlias = NULL; if (isset($columnNames)) { $columnDefaultAlias = array_search($columnName, $columnNames); if ($columnDefaultAlias === FALSE) { continue; } // fixing the alias if the list of columns is not associative array if (is_int($columnDefaultAlias)) { $columnDefaultAlias = NULL; } } // FIXME the following code does not work in the following situation: // 1) we need to query a dataset which is defined in GovDashboard using file upload // 2) column names are not provided // 3) for columns which are references to lookup, value from the primary key from lookup table is returned // even if in definition of the dataset we have columns with the following application type: name@lookup, code @lookup and etc $isReferenceUsed = FALSE; $handler = DimensionLookupFactory::getInstance()->findHandler($column->type->getLogicalApplicationType()); if (isset($handler)) { // FIXME remove this implementation when we implement 'Executable Tree' functionality list($connectedDatasetName, $connectedColumnName) = $handler->adjustReferencePointColumn($metamodel, $factsDataset->name, $columnName); if (($connectedDatasetName != $factsDataset->name) || ($connectedColumnName != $columnName)) { self::$TABLE_ALIAS_SUFFIX__SEQUENCE++; $columnJoinAliasPrefix = $TABLE_ALIAS__COLUMN_JOIN . self::$TABLE_ALIAS_SUFFIX__SEQUENCE; $connectedDataset = $metamodel->getDataset($connectedDatasetName); $connectedDatasetKeyColumnName = $connectedDataset->getKeyColumn()->name; // preparing list of columns we want to get from connected dataset $connectedDatasetColumnNames = NULL; ArrayHelper::addUniqueValue($connectedDatasetColumnNames, $connectedDatasetKeyColumnName); ArrayHelper::addUniqueValue($connectedDatasetColumnNames, $connectedColumnName); $connectedStatement = $datasourceHandler->assembleDatasetSourceStatement($request, $connectedDataset, $connectedDatasetColumnNames); $connectedStatement->addTableAliasPrefix($columnJoinAliasPrefix); $connectedTable = $connectedStatement->getColumnTable($connectedColumnName); // registering the column for facts table $factsTableColumn = new ColumnSection($columnName, 'fact_' . $columnName); $factsTableColumn->visible = FALSE; $factsTable->columns[] = $factsTableColumn; // adjusting key column from lookup $connectedDatasetKeyColumn = $connectedTable->getColumn($connectedDatasetKeyColumnName); $connectedDatasetKeyColumn->alias = 'lookup_' . $connectedDatasetKeyColumnName; $connectedDatasetKeyColumn->visible = FALSE; // adjusting value column from lookup $connectedDatasetValueColumn = $connectedTable->getColumn($connectedColumnName); $connectedDatasetValueColumn->alias = $columnName; // the key column could be the same as value column $connectedDatasetValueColumn->visible = TRUE; // new value column which uses composite name as column alias if (isset($columnDefaultAlias)) { $connectedDatasetValueColumn2 = new ColumnSection( $connectedColumnName, DataSourceColumnNameHelper::generateFromParameterElements($datasourceHandler->getMaximumEntityNameLength(), $columnDefaultAlias)); $connectedDatasetValueColumn2->visible = FALSE; $connectedTable->columns[] = $connectedDatasetValueColumn2; } // linking the lookup table with the facts table $connectedTable->conditions[] = new JoinConditionSection( $connectedDatasetKeyColumn->alias, new TableColumnConditionSectionValue($factsTable->alias, $columnName)); $statement->merge($connectedStatement); $isReferenceUsed = TRUE; } } if (!$isReferenceUsed) { $factsTable->columns[] = new ColumnSection($columnName); } } return $statement; }
protected function prepareReferencedCubeQueryStatement( AbstractSQLDataSourceQueryHandler $datasourceHandler, DataControllerCallContext $callcontext, Statement $combinedStatement, array $datasetMappedCubeRequests, ReferenceLink $link) { if (!isset($link->nestedLinks)) { return; } $metamodel = data_controller_get_metamodel(); foreach ($link->nestedLinks as $referencedLink) { $referencedRequest = NULL; if (isset($datasetMappedCubeRequests[$referencedLink->dataset->name])) { $referencedRequest = clone $datasetMappedCubeRequests[$referencedLink->dataset->name]; } else { // checking if there is corresponding cube for the referenced dataset $possibleReferencedCube = $metamodel->findCubeByDatasetName($referencedLink->dataset->name); if (isset($possibleReferencedCube)) { $referencedRequest = new CubeQueryRequest($possibleReferencedCube->name); $referencedRequest->referenced = TRUE; $datasetMappedCubeRequests[$possibleReferencedCube->factsDatasetName] = $referencedRequest; } } if (isset($referencedRequest)) { // preparing parent cube $parentRequest = $datasetMappedCubeRequests[$link->dataset->name]; $parentCubeName = $parentRequest->getCubeName(); $parentCube = $metamodel->getCube($parentCubeName); // preparing referenced cube $referencedCubeName = $referencedRequest->getCubeName(); $referencedCube = $metamodel->getCube($referencedCubeName); // adding required dimensions $joinConditions = NULL; foreach ($referencedLink->parentColumnNames as $columnIndex => $parentColumnName) { // looking for a dimension in parent cube $parentDimension = $parentCube->getDimensionByAttributeColumnName($parentColumnName); // looking for a dimension in referenced cube $referencedColumnName = $referencedLink->columnNames[$columnIndex]; $referencedDimension = $referencedCube->getDimensionByAttributeColumnName($referencedColumnName); // checking if this dimension is part of query portion of parent request $parentRequestDimensionQuery = $parentRequest->findDimensionQuery($parentDimension->name); if (isset($parentRequestDimensionQuery)) { // copying the query request to referenced cube $referencedRequestDimensionQuery = new __AbstractCubeQueryRequest_DimensionQuery($referencedDimension->name); $referencedRequestDimensionQuery->columns = $parentRequestDimensionQuery->columns; $referencedRequest->importDimensionQueryFrom($referencedRequestDimensionQuery); } // checking if there is a related query for parent column name $parentRequestFactsDatasetColumnQuery = $parentRequest->findFactsDatasetColumnQuery($parentColumnName); if (isset($parentRequestFactsDatasetColumnQuery)) { // copying the query request to referenced cube $referencedRequest->addFactsDatasetColumnQueryValues($referencedColumnName, $parentRequestFactsDatasetColumnQuery->values); } // checking if this dimension is part of parent request $parentRequestDimension = $parentRequest->findDimension($parentDimension->name); if (!isset($parentRequestDimension)) { // because this dimension is not in list of returned columns we should not use it to link with referenced cube continue; } $referencedRequestDimension = $referencedRequest->addDimension( NULL, // TODO support requestColumnIndex here $referencedDimension->name); $columnNames = $parentRequestDimension->getColumnNames(); if (isset($columnNames)) { foreach ($columnNames as $columnName) { $referencedRequestDimension->registerColumnName( NULL, // TODO support requestColumnIndex here $columnName); } } else { // to help the following loop to link cubes by dimension $columnNames = array(NULL); } foreach ($columnNames as $columnName) { $parentDatabaseColumnName = DataSourceColumnNameHelper::generateFromParameterElements( $datasourceHandler->getMaximumEntityNameLength(), ($parentRequest->referenced ? ReferencePathHelper::assembleReference($parentCube->factsDatasetName, $parentDimension->name) : $parentDimension->name), $columnName); $referencedDatabaseColumnName = DataSourceColumnNameHelper::generateFromParameterElements( $datasourceHandler->getMaximumEntityNameLength(), ReferencePathHelper::assembleReference($referencedCube->factsDatasetName, $referencedDimension->name), $columnName); $joinConditions[] = new JoinConditionSection( $referencedDatabaseColumnName, new TableColumnConditionSectionValue(self::$TABLE_ALIAS__REFERENCED . $link->linkId, $parentDatabaseColumnName)); } } if (!isset($joinConditions)) { throw new IllegalArgumentException(t( 'There is no common columns to join %datasetNameA and %datasetNameB datasets', array('%datasetNameA' => $parentCube->publicName, '%datasetNameB' => $referencedCube->publicName))); } // preparing aggregation statement for referenced cube $referencedAggregationStatement = $this->prepareSelectedCubeQueryStatement($datasourceHandler, $callcontext, $referencedRequest); list($isSubqueryRequired, $assembledReferencedCubeSections) = $referencedAggregationStatement->prepareSections(NULL); $referencedCubeSubquerySection = new SubquerySection( Statement::assemble($isSubqueryRequired, NULL, $assembledReferencedCubeSections, SelectStatementPrint::INDENT__LEFT_OUTER_JOIN__SUBQUERY, FALSE), self::$TABLE_ALIAS__REFERENCED . $referencedLink->linkId); // preparing columns which are returned by referenced aggregation foreach ($referencedAggregationStatement->tables as $table) { if (!isset($table->columns)) { continue; } foreach ($table->columns as $column) { if (!$column->visible) { continue; } $referencedCubeSubquerySection->columns[] = new ColumnSection($column->alias); } } // linking with parent cube foreach ($joinConditions as $joinCondition) { // we do not need to return columns which are used to join with parent cube $referencedCubeSubquerySection->getColumn($joinCondition->subjectColumnName)->visible = FALSE; $referencedCubeSubquerySection->conditions[] = $joinCondition; } // adding to resulting statement $combinedStatement->tables[] = $referencedCubeSubquerySection; // applying referenced cubes measure conditions on resulting statement as well $measureQueries = $referencedRequest->findMeasureQueries(); if (isset($measureQueries)) { foreach ($measureQueries as $measureQuery) { $measureName = ReferencePathHelper::assembleReference($referencedCube->factsDatasetName, $measureQuery->name); $measureDatabaseColumnName = DataSourceColumnNameHelper::generateFromParameterElements( $datasourceHandler->getMaximumEntityNameLength(), $measureName); foreach ($measureQuery->values as $value) { $combinedStatement->conditions[] = new WhereConditionSection( self::$TABLE_ALIAS__REFERENCED . $referencedLink->linkId, $measureDatabaseColumnName, new ExactConditionSectionValue( $datasourceHandler->formatOperatorValue($callcontext, $referencedRequest, $referencedCube->factsDatasetName, NULL, $value))); } } } } else { throw new UnsupportedOperationException(t('Cube joins using intermediate dataset is not supported yet')); // preparing statement for intermediate dataset $requiredColumnNames = $referencedLink->columnNames; $referencedIntermediateDatasetStatement = $datasourceHandler->prepareDatasetSourceStatement($callcontext, NULL, $referencedLink->dataset, $requiredColumnNames); // adding condition to join with parent statement $referencedIntermediateDatasetTableSection = $referencedIntermediateDatasetStatement->tables[0]; foreach ($referencedLink->columnNames as $columnIndex => $referencedColumnName) { $referencedDatabaseColumnName = $referencedColumnName; $parentColumnName = $referencedLink->parentColumnNames[$columnIndex]; $parentDatabaseColumnName = $parentColumnName; $referencedIntermediateDatasetTableSection->conditions[] = new JoinConditionSection( $referencedDatabaseColumnName, new TableColumnConditionSectionValue(self::$TABLE_ALIAS__REFERENCED . $link->linkId, $parentDatabaseColumnName)); } $combinedStatement->merge($referencedIntermediateDatasetStatement); } // recursively check nested levels $this->prepareReferencedCubeQueryStatement($datasourceHandler, $callcontext, $combinedStatement, $datasetMappedCubeRequests, $referencedLink); } }