/**
     * @param string $datasourceName
     * @return DataSourceHandler
     */
    protected function getDataSourceHandler($datasourceName) {
        $environment_metamodel = data_controller_get_environment_metamodel();

        $datasource = $environment_metamodel->getDataSource($datasourceName);

        return $this->lookupDataSourceHandler($datasource->type);
    }
function gd_datasource_page_admin_retrieve ( $datasourceName ) {

    $EnvironmentMetaModel = data_controller_get_environment_metamodel();

    $DataSource = $EnvironmentMetaModel->getDataSource($datasourceName);

    $output  = '<h2>'.$DataSource->publicName.'</h2>';
    $output .= '<pre>'.print_r($DataSource,true).'</pre>';

    $output .= '<h3>Datasets</h3>';
    $output .= '<ul>';
    foreach ( gd_dataset_findall_by_datasource($datasourceName) as $dataset ) {
        $output .= '<li>'.$dataset->publicName.'</li>';
    }
    $output .= '</ul>';

    $output .= '<h3>Reports</h3>';
    $output .= '<ul>';
    foreach ( gd_report_findall_by_datasource(LOAD_ENTITY,$datasourceName) as $report ) {
        $output .= '<li>'.$report->title.'</li>';
    }
    $output .= '</ul>';

    $output .= '<h3>Dashboards</h3>';
    $output .= '<ul>';
    foreach ( gd_dashboard_findall_by_datasource(LOAD_ENTITY,$datasourceName) as $dashboard ) {
        $output .= '<li>'.$dashboard->title.'</li>';
    }
    $output .= '</ul>';


    return array('datasource_info'=>array(
        '#markup' => $output
    ));
}
 public function dropDatasetStorage(DataControllerCallContext $callcontext, DropDatasetStorageRequest $request)
 {
     $environment_metamodel = data_controller_get_environment_metamodel();
     $dataset = DatasetTypeHelper::getTableDataset($request->datasetName);
     $datasource = $environment_metamodel->getDataSource($dataset->datasourceName);
     $sql = $this->getExtension('dropTable')->generate($this, $dataset);
     LogHelper::log_info(new StatementLogMessage('table.drop', $sql));
     $this->executeStatement($datasource, $sql);
 }
    protected function checkDataSourceStructurePermission($datasourceName) {
        $environment_metamodel = data_controller_get_environment_metamodel();

        $datasource = $environment_metamodel->getDataSource($datasourceName);

        if ($datasource->isReadOnly()) {
            throw new IllegalStateException(t(
                'Structure manipulation is not permitted for the data source: %datasourceName',
                array('%datasourceName' => $datasource->publicName)));
        }
    }
    protected function executeScriptFunction(DatasetMetaData $dataset, $functionName, array $parameters = NULL) {
        $environment_metamodel = data_controller_get_environment_metamodel();

        $datasource = $environment_metamodel->getDataSource($dataset->datasourceName);

        // preparing URI
        $uri = $datasource->protocol . '://' . $datasource->host . (isset($datasource->port) ? (':' . $datasource->port) : '') . $datasource->path;

        // preparing query parameters
        $queryParameters = NULL;
        $queryParameters[self::PARAMETER_NAME__OPERATION] = $functionName;
        $queryParameters[DataQueryControllerUIParameterNames::DATASET] = $dataset->name;
        // preparing server callback name
        $queryParameters[self::PARAMETER_NAME__CALLBACK_SERVER_NAME] = $this->callbackServerName;
        // preparing version
        $scriptFileName = data_controller_script_get_script_file_name($dataset);
        $selectedVersion = data_controller_script_prepare_version($dataset, $scriptFileName);
        if (isset($selectedVersion)) {
            $queryParameters[DataQueryControllerUIParameterNames::PARAMETER_NAME__DATASET_VERSION] = $selectedVersion;
        }
        ArrayHelper::merge($queryParameters, $parameters);

        // preparing CURL request
        $curlProxy = $this->initiateCURLProxy($uri);
        $handler = $curlProxy->initializeHandler('GET', '/dp_datasource_integration.py', $queryParameters);
        // executing the request in single thread environment
        $executor = new SingleCURLHandlerExecutor($handler);
        $output = $executor->execute();

        $records = NULL;
        if (isset($output)) {
            try {
                $records = json_decode($output, TRUE);
                if (isset($records)) {
                    if (count($records) == 0) {
                        $records = NULL;
                    }
                }
                else {
                    throw new IllegalStateException(t(
                        'Error occurred during execution of a script for the dataset: %datasetName',
                        array('%datasetName' => $dataset->publicName)));
                }
            }
            catch (Exception $e) {
                LogHelper::log_debug(new PreservedTextMessage($output));
                throw $e;
            }
        }

        return $records;
    }
    public function registerDataset(DataControllerCallContext $callcontext, DatasetMetaData $dataset, $stage) {
        parent::registerDataset($callcontext, $dataset, $stage);

        if ($stage == DatasetStorageObserver::STAGE__BEFORE) {
            // 'fixing' logical dataset
            if (!isset($this->newLogicalDataset->uuid)) {
                $this->newLogicalDataset->uuid = \GD\Utility\Uuid::generate();
            }

            // 'fixing' physical dataset
            $dataset->loaderName = 'GD_DatasetMetaModelLoader';

            global $user;

            $environment_metamodel = data_controller_get_environment_metamodel();
            $datasource = $environment_metamodel->getDataSource($dataset->datasourceName);

            $node = new stdClass();
            $node->type = NODE_TYPE_DATASET;
            $node->language = LANGUAGE_NONE;
            $node->uid = $user->uid;
            node_object_prepare($node);

            $node->title = $dataset->publicName;
            $node->field_dataset_uuid[$node->language][0]['value'] = $this->newLogicalDataset->uuid;
            $node->field_dataset_sysname[$node->language][0]['value'] = $this->newLogicalDataset->name;
            $node->field_dataset_datasource[$node->language][0]['value'] = $datasource->name;
            $node->field_dataset_desc[$node->language][0]['value'] = $dataset->description;
            $node->field_dataset_source_type[$node->language][0]['value'] = $this->newLogicalDataset->sourceType;
            $node->field_dataset_source[$node->language][0]['value'] = $this->newLogicalDataset->source;
            $node->field_dataset_records[$node->language][0]['value'] = 0;
            // updating aliases
            if (isset($this->newLogicalDataset->aliases)) {
                foreach ($this->newLogicalDataset->aliases as $index => $alias) {
                    $node->field_dataset_alias[$node->language][$index]['value'] = $alias;
                }
            }

            $node->status = $dataset->isUsed() ? NODE_PUBLISHED : NODE_NOT_PUBLISHED;

            node_save($node);

            $this->newLogicalDataset->nid = $node->nid;
            $this->newLogicalDataset->loaderName = $dataset->loaderName;
        }
    }
    public function dropColumnStorage(DataControllerCallContext $callcontext, DataSourceStructureHandler $datasourceStructureHandler, DatasetMetaData $dataset, $columnName, $stage) {
        if ($stage == DatasetStorageObserver::STAGE__AFTER) {
            $environment_metamodel = data_controller_get_environment_metamodel();

            $originalLogicalColumn = $this->originalLogicalDataset->getColumn($columnName);
            $originalColumn = $dataset->getColumn($columnName);

            $datasource = $environment_metamodel->getDataSource($dataset->datasourceName);
            $datasourceQueryHandler = DataSourceQueryFactory::getInstance()->getHandler($datasource->type);

            $generator = new FunctionDateDimensionMetaDataGenerator($datasourceQueryHandler);
            $generator->clean($originalColumn);
            $generator->clean($originalLogicalColumn);
        }

        parent::dropColumnStorage($callcontext, $datasourceStructureHandler, $dataset, $columnName, $stage);
    }
Пример #8
0
 protected function prepareSharedCacheHandler($cacheDataSourceName)
 {
     $environment_metamodel = data_controller_get_environment_metamodel();
     $handler = NULL;
     if (isset($cacheDataSourceName)) {
         $cacheDataSource = $environment_metamodel->findDataSource($cacheDataSourceName);
         $handler = $this->initializeSharedCacheHandler($cacheDataSource);
     } else {
         $cacheDataSources = $environment_metamodel->findDataSourceByNamespacelessName(self::$DATASOURCE_NAME__DEFAULT);
         if (isset($cacheDataSources)) {
             foreach ($cacheDataSources as $cacheDataSource) {
                 $handler = $this->initializeSharedCacheHandler($cacheDataSource);
                 // selecting first accessible cache
                 if (isset($handler)) {
                     break;
                 }
             }
         }
     }
     return $handler;
 }
    public function finalize(AbstractMetaModel $metamodel) {
        parent::finalize($metamodel);

        $environment_metamodel = data_controller_get_environment_metamodel();

        $generators = NULL;

        foreach ($metamodel->datasets as $dataset) {
            $datasource = $environment_metamodel->getDataSource($dataset->datasourceName);

            // preparing generator for the data source type
            if (!isset($generators[$datasource->type])) {
                $datasourceQueryHandler = DataSourceQueryFactory::getInstance()->getHandler($datasource->type);

                $generators[$datasource->type] = new FunctionDateDimensionMetaDataGenerator($datasourceQueryHandler);
            }
            $generator = $generators[$datasource->type];

            foreach ($dataset->columns as $column) {
                $generator->generate($column);
            }
        }
    }
    public function load(AbstractMetaModel $metamodel, array $filters = NULL) {
        $environment_metamodel = data_controller_get_environment_metamodel();

        LogHelper::log_notice(t('Loading Meta Model from GovDashboard Content Types ...'));

        $loaderName = $this->getName();

        $datasetQuery = new EntityFieldQuery();
        $datasetQuery->entityCondition('entity_type', 'node');
        $datasetQuery->propertyCondition('type', NODE_TYPE_DATASET);
        $datasetQuery->propertyCondition('status', NODE_PUBLISHED);
        $datasetQuery->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT');
        // applying filters. Note that we have custom mapping for filter properties
        $datasetFilters = isset($filters['DatasetMetaData']) ? $filters['DatasetMetaData'] : NULL;
        if (isset($datasetFilters)) {
            foreach ($datasetFilters as $propertyName => $filterValues) {
                switch ($propertyName) {
                    case 'datasourceName':
                        $selectedDataSourceNames = FALSE;
                        // checking if any of the data sources are actually data marts
                        foreach ($filterValues as $datasourceName) {
                            $datasource = $environment_metamodel->findDataSource($datasourceName);
                            if (isset($datasource->nid)) {
                                $selectedDataSourceNames[] = $datasourceName;
                            }
                        }
                        if (isset($selectedDataSourceNames)) {
                            $datasetQuery->fieldCondition('field_dataset_datasource', 'value', $selectedDataSourceNames);
                        }
                        else {
                            // there is no selected datamarts for this request
                            return;
                        }
                        break;
                    default:
                        throw new UnsupportedOperationException(t(
                            'Unsupported mapping for the property for filtering during dataset loading: %propertyName',
                            array('%propertyName' => $propertyName)));
                }
            }
        }
        $datasetEntities = $datasetQuery->execute();
        $dataset_nids = isset($datasetEntities['node']) ? array_keys($datasetEntities['node']) : NULL;
        if (!isset($dataset_nids)) {
            return;
        }

        $datasetNodes = node_load_multiple($dataset_nids);

        // loading columns for selected datasets
        $columnNodes = gd_column_get_columns_4_dataset($dataset_nids, LOAD_ENTITY, INCLUDE_UNPUBLISHED);
        // grouping nodes in context of dataset
        $datasetsColumnNodes = $this->groupNodesByDataset($columnNodes, 'field_column_dataset');

        // preparing dataset & cubes
        $processedDatasetCount = 0;
        foreach ($datasetNodes as $datasetNode) {
            $dataset_nid = $datasetNode->nid;

            $datasourceName = get_node_field_value($datasetNode, 'field_dataset_datasource');
            $datasource = isset($datasourceName) ? $environment_metamodel->findDataSource($datasourceName) : NULL;
            if (!isset($datasource)) {
                // the data mart could be unpublished or ...
                continue;
            }

            $datasetColumnNodes = isset($datasetsColumnNodes[$dataset_nid]) ? $datasetsColumnNodes[$dataset_nid] : NULL;

            // preparing dataset
            $dataset = GD_DatasetMetaModelLoaderHelper::prepareDataset($metamodel, $datasetNode, $datasetColumnNodes, $datasource);
            // assigning a loader which created the dataset
            $dataset->loaderName = $loaderName;

            $processedDatasetCount++;
        }

        LogHelper::log_info(t('Processed @datasetCount dataset node(s)', array('@datasetCount' => $processedDatasetCount)));
    }
    protected function assembleTableName() {
        $assembledTableName = $this->name;

        $index = strpos($assembledTableName, '.');
        if ($index === FALSE) {
            $environment_metamodel = data_controller_get_environment_metamodel();

            $datasourceName = $this->dataset->datasourceName;
            $datasource = $environment_metamodel->getDataSource($datasourceName);

            $datasourceQueryHandler = DataSourceQueryFactory::getInstance()->getHandler($datasource->type);

            $assembledTableName = assemble_database_entity_name($datasourceQueryHandler, $datasource->name, $assembledTableName);
        }

        return $assembledTableName;
    }
 public function load(AbstractMetaModelFactory $factory, AbstractMetaModel $metamodel, array $filters = NULL, $finalAttempt)
 {
     $selectedDataSourceType = $this->selectedDataSourceType();
     LogHelper::log_notice(t("Finalizing Dataset Meta Data for '@databaseType' database connections ...", array('@databaseType' => $selectedDataSourceType)));
     if ($finalAttempt === FALSE) {
         return self::LOAD_STATE__POSTPONED;
     }
     $finalizedDatasetCount = 0;
     $environment_metamodel = data_controller_get_environment_metamodel();
     // processing all database connections
     foreach ($environment_metamodel->datasources as $datasource) {
         if ($datasource->type !== $selectedDataSourceType) {
             continue;
         }
         // selecting datasets which could be processed for the selected connection
         $selectedSources = NULL;
         foreach ($metamodel->datasets as $dataset) {
             // the dataset should belong to the selected data source
             if ($dataset->datasourceName !== $datasource->name) {
                 continue;
             }
             // the dataset has to be of type table
             if (DatasetTypeHelper::detectDatasetSourceType($dataset) !== DatasetTypeHelper::DATASET_SOURCE_TYPE__TABLE) {
                 continue;
             }
             // whole dataset meta data was prepared using different method. There is nothing else can be done
             if ($dataset->isComplete()) {
                 continue;
             }
             $tableName = strtolower($dataset->source);
             // invalidating existing column indexes
             $dataset->invalidateColumnIndexes();
             // there could be several datasets for one table
             $selectedSources[$tableName][] = $dataset;
         }
         if (!isset($selectedSources)) {
             continue;
         }
         $datasourceHandler = DataSourceQueryFactory::getInstance()->getHandler($datasource->type);
         $metadataCallback = $datasourceHandler->prepareQueryStatementExecutionCallbackInstance();
         // processing meta data for selected datasets
         $columnsMetaDataProperties = $this->prepareColumnsMetaDataProperties($datasource, array_keys($selectedSources));
         if (isset($columnsMetaDataProperties)) {
             foreach ($columnsMetaDataProperties as $columnMetaDataProperties) {
                 $tableName = strtolower($columnMetaDataProperties[self::PROPERTY__TABLE_NAME]);
                 $datasets = $selectedSources[$tableName];
                 foreach ($datasets as $dataset) {
                     $column = new ColumnMetaData();
                     $column->name = strtolower($columnMetaDataProperties[self::PROPERTY__COLUMN_NAME]);
                     $column->columnIndex = $columnMetaDataProperties[self::PROPERTY__COLUMN_INDEX];
                     // preparing column type
                     $column->type->databaseType = $columnMetaDataProperties[self::PROPERTY__COLUMN_TYPE];
                     $column->type->applicationType = $metadataCallback->calculateApplicationDataType($column);
                     // checking if the column is a system column which should be invisible
                     if (substr_compare($column->name, DatasetSystemColumnNames::COLUMN_NAME_PREFIX, 0, strlen(DatasetSystemColumnNames::COLUMN_NAME_PREFIX)) === 0) {
                         $column->visible = FALSE;
                     }
                     $dataset->initializeColumnFrom($column);
                 }
             }
         }
         // marking all selected datasets as completed
         foreach ($selectedSources as $datasets) {
             foreach ($datasets as $dataset) {
                 $dataset->markAsComplete();
                 $finalizedDatasetCount++;
             }
         }
     }
     LogHelper::log_info(t('Finalized @datasetCount dataset meta data configurations', array('@datasetCount' => $finalizedDatasetCount)));
     return self::LOAD_STATE__SUCCESSFUL;
 }
    /**
     * @param $datasetName
     * @return DatasetReference[]|null
     */
    public function findReferencesByDatasetName($datasetName) {
        $references = NULL;
        foreach ($this->references as $reference) {
            foreach ($reference->points as $referencePoint) {
                foreach ($referencePoint->columns as $referencePointColumn) {
                    if ($referencePointColumn->datasetName == $datasetName) {
                        $references[] = $reference;
                        continue 3;
                    }
                }
            }
        }
        if (!isset($references)) {
            return NULL;
        }

        $environment_metamodel = NULL;

        $dataset = $this->getDataset($datasetName);
        $datasourceName = $dataset->datasourceName;

        // eliminating references from different data sources
        $selectedReferences = NULL;
        foreach ($references as $reference) {
            $selectedReference = new DatasetReference();
            foreach ($reference->points as $referencePoint) {
                $isPointSelected = TRUE;
                foreach ($referencePoint->columns as $referencePointColumn) {
                    // FIXME fixing references (to resolve the issue we need to post process configuration)
                    if (!isset($referencePointColumn->columnName)) {
                        $referencePointColumn->columnName = $this->getDataset($referencePointColumn->datasetName)->getKeyColumn()->name;
                    }

                    if ($referencePointColumn->datasetName == $dataset->name) {
                        continue;
                    }

                    $columnDataset = $this->getDataset($referencePointColumn->datasetName);
                    // this dataset is shared across different data sources
                    if ($columnDataset->isShared()) {
                        continue;
                    }

                    $columnDataSourceName = $columnDataset->datasourceName;
                    // this dataset is from the same data source
                    if ($columnDataSourceName == $datasourceName) {
                        continue;
                    }

                    if (!isset($environment_metamodel)) {
                        $environment_metamodel = data_controller_get_environment_metamodel();
                    }
                    $datasource = $environment_metamodel->getDataSource($columnDataSourceName);
                    if (!$datasource->isShared()) {
                        $isPointSelected = FALSE;
                        break;
                    }
                }
                if ($isPointSelected) {
                    $selectedReference->points[] = $referencePoint;
                }
            }

            if ($selectedReference->getPointCount() > 1) {
                // preparing properties of selected reference
                $selectedReference->initializeInstanceFrom($reference);
                $selectedReferences[] = $selectedReference;
            }
        }

        return $selectedReferences;
    }
    public function insertOrUpdateOrDeleteDatasetRecords(DataControllerCallContext $callcontext, AbstractDatasetManipulationRequest $request) {
        $environment_metamodel = data_controller_get_environment_metamodel();
        $metamodel = data_controller_get_metamodel();

        $datasourceQueryHandler = DataSourceQueryFactory::getInstance()->getHandler($this->getDataSourceType());

        $dataset = $metamodel->getDataset($request->datasetName);
        $datasource = $environment_metamodel->getDataSource($dataset->datasourceName);
        $recordMetaData = isset($request->recordsHolder->recordMetaData) ? $request->recordsHolder->recordMetaData : $dataset;

        $isRecordIndexed = $request->recordsHolder instanceof IndexedRecordsHolder;

        $keyColumnNames = $recordMetaData->getKeyColumnNames();
        $nonkeyColumnNames = $recordMetaData->findNonKeyColumnNames();

        // preparing a request to find existing records
        $queryRequest = new DatasetQueryRequest($request->datasetName);
        // loading only key columns from database
        $queryRequest->addColumns($keyColumnNames);
        // a request can be more efficient for single column key
        if (count($keyColumnNames) == 1) {
            list($keyColumnIndex, $keyColumnName) = each($keyColumnNames);
            $keyColumnIdentifier = $isRecordIndexed ? $keyColumnIndex : $keyColumnName;

            $keyValues = NULL;
            foreach ($request->recordsHolder->records as $record) {
                ArrayHelper::addUniqueValue($keyValues, $record->getColumnValue($keyColumnIdentifier, TRUE));
            }

            $queryRequest->addQueryValue(
                0,
                $keyColumnName, OperatorFactory::getInstance()->initiateHandler(EqualOperatorHandler::OPERATOR__NAME, array($keyValues)));
        }
        else {
            for ($i = 0, $count = count($request->recordsHolder->records); $i < $count; $i++) {
                $record = $request->recordsHolder->records[$i];
                foreach ($keyColumnNames as $keyColumnIndex => $keyColumnName) {
                    $keyColumnIdentifier = $isRecordIndexed ? $keyColumnIndex : $keyColumnName;

                    $keyColumnValue = $record->getColumnValue($keyColumnIdentifier, TRUE);
                    $queryRequest->addQueryValue(
                        $i,
                        $keyColumnName, OperatorFactory::getInstance()->initiateHandler(EqualOperatorHandler::OPERATOR__NAME, $keyColumnValue));
                }
            }
        }

        // loading existing records ... if any
        $existingRecordFormatter = new QueryKeyResultFormatter($keyColumnNames);
        $existingRecords = $existingRecordFormatter->formatRecords($datasourceQueryHandler->queryDataset($callcontext, $queryRequest));

        // sorting out records for insert, update and delete operations
        $keyedRecords = $insertedRecordKeys = $updatedRecordKeys = $deletedRecordKeys = NULL;
        foreach ($request->recordsHolder->records as $record) {
            $keyParts = NULL;
            foreach ($keyColumnNames as $keyColumnIndex => $keyColumnName) {
                $keyColumnIdentifier = $isRecordIndexed ? $keyColumnIndex : $keyColumnName;
                $keyParts[] = $record->getColumnValue($keyColumnIdentifier, TRUE);
            }
            $key = ArrayHelper::prepareCompositeKey($keyParts);
            $keyedRecords[$key] = $record;

            // checking if the record has to be deleted
            $isDeletable = TRUE;
            if (isset($nonkeyColumnNames)) {
                foreach ($nonkeyColumnNames as $columnIndex => $columnName) {
                    $columnIdentifier = $isRecordIndexed ? $columnIndex : $columnName;
                    if ($record->getColumnValue($columnIdentifier) != NULL) {
                        $isDeletable = FALSE;
                        break;
                    }
                }
            }
            else {
                // the dataset has NO non-key columns. We should not delete these records
                $isDeletable = FALSE;
            }

            if ($isDeletable) {
                unset($insertedRecordKeys[$key]);
                unset($updatedRecordKeys[$key]);
                // the record physically present in database and needs to be deleted
                if (isset($existingRecords[$key])) {
                    unset($existingRecords[$key]);
                    $deletedRecordKeys[$key] = TRUE;
                }
            }
            elseif (isset($insertedRecordKeys[$key])) {
                // the key has been already used to insert a record within this batch. This record needs to be part of update operation
                $updatedRecordKeys[$key] = TRUE;
            }
            elseif (isset($existingRecords[$key])) {
                $updatedRecordKeys[$key] = TRUE;
            }
            else {
                $insertedRecordKeys[$key] = TRUE;
            }
        }

        $sqls = NULL;

        // deleting existing records
        $deletedRecordCount = 0;
        if (isset($deletedRecordKeys)) {
            $deleteRecordHolder = $this->prepareRecordHolder($request, $keyedRecords, $deletedRecordKeys);
            // preparing request
            $deleteRequest = new DatasetDeleteRequest($request->datasetName, $deleteRecordHolder);
            // preparing statements to delete records from the database
            ArrayHelper::appendValue($sqls, $this->prepareDeleteDatasetRecordStatements($callcontext, $deleteRequest));

            $deletedRecordCount = count($deleteRecordHolder->records);
        }

        // inserting new records
        $insertedRecordCount = 0;
        if (isset($insertedRecordKeys)) {
            $insertRecordHolder = $this->prepareRecordHolder($request, $keyedRecords, $insertedRecordKeys);
            // preparing request
            $insertRequest = new DatasetInsertRequest($request->datasetName, $insertRecordHolder);
            // preparing statements to insert records into the database
            ArrayHelper::appendValue($sqls, $this->prepareInsertDatasetRecordStatements($callcontext, $insertRequest));

            $insertedRecordCount = count($insertRecordHolder->records);
        }

        // updating existing records
        $updatedRecordCount = 0;
        if (isset($updatedRecordKeys)) {
            $updateRecordHolder = $this->prepareRecordHolder($request, $keyedRecords, $updatedRecordKeys);
            // preparing request
            $updateRequest = new DatasetUpdateRequest($request->datasetName, $updateRecordHolder);
            // preparing statements to update records in the database
            ArrayHelper::appendValue($sqls, $this->prepareUpdateDatasetRecordStatements($callcontext, $updateRequest));

            $updatedRecordCount = count($updateRecordHolder->records);
        }

        $affectedRecordCount = isset($sqls) ? $this->executeManipulationStatementBatch($datasource, $sqls) : 0;
        if (($insertedRecordCount + $updatedRecordCount + $deletedRecordCount) < $affectedRecordCount) {
            throw new IllegalStateException(t('Number of affected records is greater than expected number of inserted, updated and deleted records'));
        }

        return array($insertedRecordCount, $updatedRecordCount, $deletedRecordCount);
    }
Пример #15
0
 protected function errorTransactionNotSupported($datasourceName)
 {
     $environment_metamodel = data_controller_get_environment_metamodel();
     $datasource = $environment_metamodel->getDataSource($datasourceName);
     throw new UnsupportedOperationException(t('Transaction support is not available for the data source: @datasourceName', array('@datasourceName' => $datasource->publicName)));
 }
    public function load(AbstractMetaModel $metamodel, array $filters = NULL) {
        $loaderClassName = get_class($this);

        $environment_metamodel = data_controller_get_environment_metamodel();
        foreach ($environment_metamodel->datasources as $datasource) {
            if (!$this->isMetaDataAcceptable($datasource, $filters)) {
                continue;
            }

            if (!$this->isDataSourceAcceptable($datasource, $filters)) {
                continue;
            }

            $environment = new SystemTableMetaModelLoaderEnvironment();
            $this->prepareLoaderEnvironment($datasource, $environment);
            if ($environment->classname != $loaderClassName) {
                continue;
            }

            $callcontext = $this->initiateCallContext();
            $callcontext->environment = $environment;

            $this->loadFromDataSource($callcontext, $metamodel, $datasource, $filters);
        }
    }
    public function flush($subsetName = NULL) {
        $environment_metamodel = data_controller_get_environment_metamodel();

        foreach ($environment_metamodel->datasources as $datasource) {
            if (!$this->isCacheDataSource($datasource)) {
                continue;
            }

            $handler = $this->initializeSharedCacheHandler($datasource);
            if (!isset($handler)) {
                continue;
            }

            $handler = $this->proxySharedCacheHandler($handler, $subsetName, FALSE);
            $handler->flush();
        }

        $this->handlers = NULL;
    }
 public function countCubeRecords(DataControllerCallContext $callcontext, CubeQueryRequest $request, ResultFormatter $resultFormatter)
 {
     $cubeName = $request->getCubeName();
     LogHelper::log_notice(t('Counting SQL-based cube records: @cubeName', array('@cubeName' => $cubeName)));
     $environment_metamodel = data_controller_get_environment_metamodel();
     $metamodel = data_controller_get_metamodel();
     $cube = $metamodel->getCube($cubeName);
     $cubeDatasetName = $cube->sourceDatasetName;
     $cubeDataset = $metamodel->getDataset($cubeDatasetName);
     $datasource = $environment_metamodel->getDataSource($cubeDataset->datasourceName);
     $statement = $this->prepareCubeQueryStatement($callcontext, $request);
     list($isSubqueryRequired, $assembledSections) = $statement->prepareSections(NULL);
     $statement = new Statement();
     $statement->tables[] = new SubquerySection(Statement::assemble($isSubqueryRequired, NULL, $assembledSections));
     return $this->countRecords($callcontext, $datasource, array($statement));
 }
Пример #19
0
 protected function assembleTableName()
 {
     $environment_metamodel = data_controller_get_environment_metamodel();
     $datasourceName = $this->dataset->datasourceName;
     $datasource = $environment_metamodel->getDataSource($datasourceName);
     $datasourceQueryHandler = DataSourceQueryFactory::getInstance()->getHandler($datasource->type);
     $owner = $datasourceQueryHandler->getDataSourceOwner($datasourceName);
     return (isset($owner) ? $owner . '.' : '') . $this->name;
 }
    protected function allowPrimaryKeyCreation(DataSourceHandler $handler, DataControllerCallContext $callcontext, DatasetMetaData $dataset) {
        $primaryKeyColumnNames = $dataset->findKeyColumnNames();
        if (isset($primaryKeyColumnNames)) {
            $environment_metamodel = data_controller_get_environment_metamodel();

            $datasource = $environment_metamodel->getDataSource($dataset->datasourceName);

            $assembledTableName = assemble_database_entity_name($handler, $datasource->name, $dataset->source);

            $datasourceQueryHandler = DataSourceQueryFactory::getInstance()->getHandler($datasource->type);

            // checking if there are any records with NULL values
            $nullValueConditions = array();
            foreach ($primaryKeyColumnNames as $columnName) {
                $nullValueConditions[] = "$columnName IS NULL";
            }
            $nullValueSQL = 'SELECT ' . implode(', ', $primaryKeyColumnNames) . " FROM $assembledTableName WHERE " . implode(' OR ', $nullValueConditions);
            $datasourceQueryHandler->getExtension('applyPagination')->apply($datasourceQueryHandler, $nullValueSQL, 0, 1);
            LogHelper::log_info(new StatementLogMessage('dataset.update.primaryKey.data.check.null', $nullValueSQL));
            $nullColumnRecords = $datasourceQueryHandler->executeQuery($callcontext, $datasource, $nullValueSQL);
            $isTruncateRequired = isset($nullColumnRecords);

            // checking for non-unique values
            if (!$isTruncateRequired) {
                $nonUniqueValueSQL = 'SELECT ' . implode(', ', $primaryKeyColumnNames)
                    . " FROM $assembledTableName GROUP BY " . implode(', ', $primaryKeyColumnNames) . ' HAVING COUNT(*) > 1';
                $datasourceQueryHandler->getExtension('applyPagination')->apply($datasourceQueryHandler, $nonUniqueValueSQL, 0, 1);
                LogHelper::log_info(new StatementLogMessage('dataset.update.primaryKey.data.check', $nonUniqueValueSQL));
                $nonUniqueColumnRecords = $datasourceQueryHandler->executeQuery($callcontext, $datasource, $nonUniqueValueSQL);
                $isTruncateRequired = isset($nonUniqueColumnRecords);
            }

            if ($isTruncateRequired) {
                $truncateDatasetSQL = $handler->getExtension('truncateTable')->generate($handler, $dataset);
                LogHelper::log_info(new StatementLogMessage('dataset.update.primaryKey.data.truncate', $truncateDatasetSQL));
                $handler->executeStatement($datasource, $truncateDatasetSQL);
            }
        }
    }
 protected function getDataSourceHandler()
 {
     $environment_metamodel = data_controller_get_environment_metamodel();
     $datasource = $environment_metamodel->getDataSource($this->datasourceName);
     return DataSourceQueryFactory::getInstance()->getHandler($datasource->type);
 }
    protected function executeCubeCountRequest(DataQueryControllerCubeRequest $request) {
        $environment_metamodel = data_controller_get_environment_metamodel();
        $metamodel = data_controller_get_metamodel();

        $callcontext = $this->prepareCallContext();

        $requestPreparer = new DataSourceCubeQueryRequestPreparer();
        $cubeCountRequest = $requestPreparer->prepareCountRequest($request);

        $this->prepareCubeRequestMetaData($cubeCountRequest);

        if (isset($request->resultFormatter)) {
            $request->resultFormatter->adjustCubeCountRequest($callcontext, $cubeCountRequest);
        }

        LogHelper::log_debug($cubeCountRequest);

        $dataset = $metamodel->getDataset($request->datasetName);
        $datasource = $environment_metamodel->getDataSource($dataset->datasourceName);

        $isCacheSupported = $this->isCacheSupported($datasource);
        $cache = $isCacheSupported ? new DataQueryControllerCacheProxy($cubeCountRequest) : NULL;

        list($data, $cacheHit) = isset($cache) ? $cache->getCachedResult() : array(NULL, FALSE);
        if (!$cacheHit) {
            $data = RequestChainFactory::getInstance()->initializeChain()->countCubeRecords(
                $this->lookupDataSourceHandler($datasource->type), $callcontext, $cubeCountRequest);
            if ($isCacheSupported) {
                $cache->cacheResult($data);
            }
        }

        return $data;
    }
    protected function update(Import\ImportStream $stream, Import\ImportContext $context) {
        $datasets = $stream->get('datasets');
        if (empty($datasets)) {
            return;
        }

        gd_datasource_set_active($context->get('datasourceName'));
        $metamodel = data_controller_get_metamodel();

        $environment_metamodel = data_controller_get_environment_metamodel();
        $datasource = $environment_metamodel->getDataSource($context->get('datasourceName'));

        foreach ( $datasets as $dataset ) {
            $existingDataset = GD_DatasetMetaModelLoaderHelper::findDatasetByUUID($metamodel->datasets, $dataset->uuid);
            if ( !$existingDataset ) {

                // map for re-linking dependent objects
                $dataset->originalName = $dataset->name;

                // create new name
                $newDatasetName = GD_NamingConvention::generateDatasetName();
                $dataset->name = $newDatasetName;
                $dataset->source = $newDatasetName;
                $dataset->datasourceName = $datasource->name;
            } else {
                // map for re-linking dependent objects
                $dataset->originalName = $dataset->name;

                $dataset->name = $existingDataset->name;
                $dataset->source = $existingDataset->source;
                $dataset->datasourceName = $existingDataset->datasourceName;
            }
        }

        // prepare dataset columns for import
        $this->prepareColumns($datasets);

        // prepares the metadata object, has to be of type RecordMetaData
        foreach ( $datasets as $key => $dataset ) {
            $metadata = new DatasetMetaData();
            $metadata->initializeFrom($dataset);
            $datasets[$key] = $metadata;
        }

        // ensure datasets are created in order to satisfy dependencies
        usort($datasets, array(new ReferencedDatasetComparator(), 'compare'));

        foreach ( $datasets as $dataset ) {
            $existingDataset = GD_DatasetMetaModelLoaderHelper::findDatasetByUUID($metamodel->datasets, $dataset->uuid);
            if ($existingDataset) {
                gd_data_controller_ddl_modify_dataset($dataset);
            } else {
                MetaModelFactory::getInstance()->startGlobalModification();
                try {
                    $transaction = db_transaction();
                    try {
                        gd_data_controller_ddl_create_dataset($dataset);
                    } catch (Exception $e) {
                        $transaction->rollback();
                        throw $e;
                    }
                } catch (Exception $e) {
                    MetaModelFactory::getInstance()->finishGlobalModification(false);
                    throw $e;
                }
                MetaModelFactory::getInstance()->finishGlobalModification(true);
            }
        }

        $stream->set('datasets',$datasets);
    }
 protected function dropTestDatabase() {
     // unregistering test data source and dropping corresponding database
     $environment_metamodel = data_controller_get_environment_metamodel();
     $datasource = $environment_metamodel->findDataSource(DATASOURCE_NAME__TEST);
     if (isset($datasource)) {
         gd_datasource_drop($datasource->name);
     }
 }
    public function queryCube(DataControllerCallContext $callcontext, CubeQueryRequest $request, ResultFormatter $resultFormatter) {
        $records = NULL;

        $environment_metamodel = data_controller_get_environment_metamodel();
        $metamodel = data_controller_get_metamodel();

        $cubeName = $request->getCubeName();
        $cube = $metamodel->getCube($cubeName);

        $cubeDatasetName = $cube->sourceDatasetName;
        $cubeDataset = $metamodel->getDataset($cubeDatasetName);
        $datasource = $environment_metamodel->getDataSource($cubeDataset->datasourceName);

        $designDocumentName = NameSpaceHelper::removeNameSpace($cubeName);
        $viewName = NameSpaceHelper::removeNameSpace($cubeName);
        $url = '/' . $cubeDataset->source->database . "/_design/$designDocumentName/_view/$viewName";

        $dimensionCount = $cube->getDimensionCount();

        $queryKeys = NULL;
        // TODO list of dimensions could be empty
        foreach ($cube->dimensions as $dimension) {
            $queryDimension = $request->findDimensionQuery($dimension->name);

            if (isset($queryDimension)) {
                $queryKeys[] = $queryDimension->values;
            }
            else {
                $queryKeys[] = NULL;
            }
        }

        // TODO develop more comprehensive validation or mapping
        if ($cube->getMeasureCount() != 1) {
            throw new UnsupportedOperationException(t('Only one measure is supported'));
        }
        $cubeMeasurePropertyName = NULL;
        foreach ($cube->measures as $measureName => $measure) {
            $cubeMeasurePropertyName = $measureName;
        }

        // preparing set of keys to access data
        $requestKeys = NULL;
        $this->prepareCubeRequestKeys($requestKeys, $queryKeys, 0, NULL);

        // preparing server requests
        if (isset($requestKeys)) {
            foreach ($requestKeys as $requestKey) {
                $parameterKey = '';
                foreach ($requestKey as $dimensionKey) {
                    $parameterKey .= self::prepareSingleValue($dimensionKey);
                }
                $parameterKey = '[' . substr($parameterKey, 0, strlen($parameterKey) - 1) . ']';

                $serverRequest = NULL;
                $serverRequest->url = $url . "?key=$parameterKey";

                // executing the server request
                $serverResponse = $this->communicateWithServer($datasource, $serverRequest);
                $this->checkDocumentExistence($serverResponse, TRUE);

                if (isset($serverResponse->rows[0])) {
                    $record = NULL;
                    // adding dimension-related properties
                    for ($i = 0; $i < $dimensionCount; $i++) {
                        // we should have data for a dimension to report related property
                        if (!isset($requestKey[$i])) {
                            continue;
                        }

                        $dimension = $cube->dimensions[$i];
                        $dimensionKey = $requestKey[$i];

                        // FIXME there is no support for targetKey any more
                        $record[$dimension->targetKey] = $dimensionKey;
                    }
                    // adding measure value
                    $record[$cubeMeasurePropertyName] = $serverResponse->rows[0]->value;

                    $records[] = $record;
                }
            }
        }

        return $records;
    }