Exemplo n.º 1
0
 public function executeJob(StatementJob $statement, array $parameters = array())
 {
     /* @var $statement UseStatement */
     $executionContext = new ExecutionContext($this->schemaManager, $statement, $parameters);
     $databaseValue = $statement->getDatabase();
     $databaseName = $this->valueResolver->resolveValue($databaseValue, $executionContext);
     $this->schemaManager->setCurrentlyUsedDatabaseId($databaseName);
     $result = new TemporaryResult();
     $result->setIsSuccess($this->schemaManager->getCurrentlyUsedDatabaseId() === $databaseName);
     return $result;
 }
Exemplo n.º 2
0
 public function executeJob(StatementJob $statement, array $parameters = array())
 {
     /* @var $statement CreateDatabaseStatement */
     $executionContext = new ExecutionContext($this->schemaManager, $statement, $parameters);
     $name = $this->valueResolver->resolveValue($statement->getName(), $executionContext);
     $this->schemaManager->createSchema($name);
     foreach ($this->tableManager->getTableFactories() as $tableFactory) {
         /* @var $tableFactory TableFactoryInterface */
         if ($tableFactory instanceof InformationSchemaTableFactory) {
             $tableFactory->clearCache();
         }
     }
     ### CREATE RESULTSET
     $result = new TemporaryResult();
     $result->setIsSuccess($this->schemaManager->schemaExists($name));
     return $result;
 }
Exemplo n.º 3
0
 public function executeJob(StatementJob $statement, array $parameters = array())
 {
     /* @var $statement CreateTableStatement */
     $context = new ExecutionContext($this->schemaManager, $statement, $parameters);
     /* @var $databaseSchema Schema */
     $databaseSchema = $this->schemaManager->getSchema();
     $schemaId = $this->schemaManager->getCurrentlyUsedDatabaseId();
     $tableName = $this->valueResolver->resolveValue($statement->getName(), $context);
     $schemaPage = new DatabaseSchemaPage();
     $schemaPage->setName($tableName);
     $schemaPage->setType(Type::TABLE());
     $schemaPage->setEngine(Engine::factory($statement->getEngine()->getName()));
     $schemaPage->setCollation(strtoupper($statement->getCollate()));
     $schemaPage->setUseChecksum($statement->getUseChecksum());
     $schemaPage->setMaxRows($statement->getMaximumRows());
     $schemaPage->setMinRows($statement->getMinimumRows());
     $schemaPage->setPackKeys($statement->getPackKeys());
     $schemaPage->setDelayKeyWrite($statement->getDelayKeyWrite());
     $schemaPage->setRowFormat(RowFormat::factory($statement->getRowFormat()));
     $schemaPage->setInsertMethod(InsertMethod::factory($statement->getInsertMethod()));
     $databaseSchema->registerTableSchema($schemaPage);
     /* @var $tableSchema TableSchema */
     $tableSchema = $this->schemaManager->getTableSchema($tableName);
     assert($tableSchema instanceof TableSchema);
     ### WRITE COLUMNS
     switch (true) {
         case is_array($statement->getColumnDefinition()):
             foreach ($statement->getColumnDefinition() as $name => $column) {
                 /* @var $column ColumnDefinition */
                 $columnPage = new ColumnSchema();
                 $columnPage->setName($name);
                 $columnPage->setDataType(DataType::factory($column->getDataType()->getName()));
                 /* @var $dataType DataType */
                 $dataType = $columnPage->getDataType();
                 $flags = 0;
                 if ($column->getIsPrimaryKey()) {
                     $flags = $flags ^ ColumnSchema::EXTRA_PRIMARY_KEY;
                 }
                 if ($column->getIsUnique()) {
                     $flags = $flags ^ ColumnSchema::EXTRA_UNIQUE_KEY;
                 }
                 if (!$column->getIsNullable()) {
                     $flags = $flags ^ ColumnSchema::EXTRA_NOT_NULL;
                 }
                 if ($column->getIsAutoIncrement()) {
                     $flags = $flags ^ ColumnSchema::EXTRA_AUTO_INCREMENT;
                 }
                 $columnPage->setExtraFlags($flags);
                 /* @var $dataType DataType */
                 $dataType = $column->getDataType();
                 $columnPage->setLength($dataType->getByteLength());
                 $columnPage->setSecondLength($dataType->getSecondByteLength());
                 if (!is_null($column->getDataTypeLength())) {
                     $columnPage->setLength($column->getDataTypeLength());
                 }
                 if (!is_null($column->getDataTypeSecondLength())) {
                     $columnPage->setSecondLength($column->getDataTypeSecondLength());
                 }
                 if (!is_null($column->getDefaultValue()) && !$columnPage->isDefaultValueInFile()) {
                     $columnPage->setDefaultValue($column->getDefaultValue());
                 }
                 $columnIndex = $tableSchema->addColumnSchema($columnPage);
                 if (!is_null($column->getDefaultValue()) && $columnPage->isDefaultValueInFile()) {
                     $defaultValueFilepath = sprintf(FilePathes::FILEPATH_DEFAULT_VALUE, $schemaId, $tableName, $columnIndex);
                     $this->tableManager->getFilesystem()->putFileContents($defaultValueFilepath, $column->getDefaultValue());
                 }
             }
             break;
         case $statement->getColumnDefinition() instanceof Select:
             break;
         case $statement->getColumnDefinition() instanceof TableInterface:
             break;
     }
     foreach ($statement->getIndexes() as $indexName => $index) {
         /* @var $index Index */
         $indexSchemaPage = new IndexSchema();
         $indexSchemaPage->setName($indexName);
         $indexSchemaPage->setEngine(IndexEngine::BTREE());
         switch (true) {
             case $index->getIsPrimary():
                 $indexSchemaPage->setType(IndexType::PRIMARY());
                 break;
             case $index->getIsUnique():
                 $indexSchemaPage->setType(IndexType::UNIQUE());
                 break;
             default:
                 $indexSchemaPage->setType(IndexType::INDEX());
                 break;
         }
         $method = ForeignKeyMethod::factory($index->getForeignKeyOnDeleteReferenceOption()->getName());
         $indexSchemaPage->setForeignKeyOnDeleteMethod($method);
         $method = ForeignKeyMethod::factory($index->getForeignKeyOnUpdateReferenceOption()->getName());
         $indexSchemaPage->setForeignKeyOnUpdateMethod($method);
         $keyLength = 0;
         $columns = array();
         foreach ($index->getColumns() as $indexColumnName) {
             $indexColumnId = $tableSchema->getColumnIndex((string) $indexColumnName);
             if (strlen($indexColumnId) <= 0) {
                 throw new ErrorException("Could not find index column '{$indexColumnName}' in table-schema!");
             }
             $columns[] = $indexColumnId;
             $keyLength += $statement->getColumnDefinition()[(string) $indexColumnName]->getDataSize();
         }
         $indexSchemaPage->setColumns($columns);
         $indexSchemaPage->setKeyLength($keyLength);
         $tableSchema->addIndexSchema($indexSchemaPage);
     }
     foreach ($this->tableManager->getTableFactories() as $tableFactory) {
         /* @var $tableFactory TableFactoryInterface */
         if ($tableFactory instanceof InformationSchemaTableFactory) {
             $tableFactory->clearCache();
         }
     }
     ### RESULT
     $result = new TemporaryResult();
     $result->setIsSuccess(true);
     return $result;
 }
Exemplo n.º 4
0
 protected function executeDropTable(DropStatement $statement, ExecutionContext $context)
 {
     foreach ($statement->getSubjects() as $tableNameValue) {
         $tableName = $this->valueResolver->resolveValue($tableNameValue, $context);
         $this->schemaManager->dropTable($tableName);
     }
     $databaseSchema = $this->schemaManager->getSchema();
     $result = new TemporaryResult();
     $result->setIsSuccess(!$databaseSchema->tableExists($tableName));
     return $result;
 }
Exemplo n.º 5
0
 public function executeJob(StatementJob $statement, array $parameters = array())
 {
     /* @var $statement InsertStatement */
     $result = new TemporaryResult();
     $context = new ExecutionContext($this->schemaManager, $statement, $parameters);
     $tableName = (string) $statement->getTable();
     /* @var $table TableInterface */
     $table = $this->tableManager->getTable($tableName);
     /* @var $tableSchema TableSchema */
     $tableSchema = $table->getTableSchema();
     ### BUILD COLUMN MAP
     $columnNameToIdMap = array();
     foreach ($tableSchema->getColumnIterator() as $columnId => $columnPage) {
         /* @var $columnPage Column */
         $columnName = $columnPage->getName();
         $columnNameToIdMap[$columnName] = $columnId;
     }
     ### GET INDICES
     $indices = array();
     foreach ($tableSchema->getIndexIterator() as $indexId => $indexPage) {
         /* @var $indexPage Index */
         /* @var $index Index */
         $index = $table->getIndex($indexId);
         $indices[$indexId] = $index;
     }
     ### BUILD ROW DATA
     $rowDatas = array();
     switch (true) {
         case $statement->getDataSource() instanceof Select:
             $subResult = $this->selectExecutor->executeJob($statement->getDataSource(), $parameters);
             foreach ($subResult as $subResultRow) {
                 $rowData = array();
                 foreach ($subResultRow as $columnName => $value) {
                     $columnId = $columnNameToIdMap[$columnName];
                     $rowData[$columnId] = $value;
                 }
                 $rowDatas[] = $rowData;
             }
             break;
         case is_array($statement->getDataSource()):
             foreach ($statement->getDataSource() as $row) {
                 $sourceRow = array();
                 foreach ($statement->getColumns() as $sourceColumnId => $sourceColumn) {
                     /* @var $sourceColumn Column */
                     $sourceColumnName = (string) $sourceColumn;
                     $sourceRow[$sourceColumnName] = $row[$sourceColumnId];
                 }
                 $rowData = array();
                 foreach ($statement->getColumns() as $sourceColumnId => $sourceColumn) {
                     /* @var $sourceColumn Column */
                     $columnName = (string) $sourceColumn;
                     if (!isset($columnNameToIdMap[$columnName])) {
                         throw new ErrorException("Unknown column '{$columnName}' in statement!");
                     }
                     $columnId = $columnNameToIdMap[$columnName];
                     if (isset($sourceRow[$columnName])) {
                         $value = $sourceRow[$columnName];
                         $value = $this->valueResolver->resolveValue($value, $context);
                     } else {
                         $value = null;
                     }
                     /* @var $columnPage Column */
                     $columnPage = $tableSchema->getColumn($columnId);
                     if (is_null($value) && $columnPage->isAutoIncrement()) {
                         $value = $table->getAutoIncrementId();
                         $table->incrementAutoIncrementId();
                     }
                     if ($columnPage->isNotNull() && is_null($value)) {
                         $columnName = $tableSchema->getColumn($columnId)->getName();
                         throw new ErrorException("Column '{$columnName}' cannot be NULL!");
                     }
                     $rowData[$columnId] = $value;
                 }
                 $primaryKey = array();
                 // fill up missing columns
                 foreach ($columnNameToIdMap as $columnName => $columnId) {
                     /* @var $columnPage Column */
                     $columnPage = $tableSchema->getColumn($columnId);
                     if (!isset($rowData[$columnId])) {
                         if ($columnPage->isNotNull()) {
                             if ($columnPage->isAutoIncrement()) {
                                 $rowData[$columnId] = $table->getAutoIncrementId();
                                 $table->incrementAutoIncrementId();
                             } elseif ($columnPage->hasDefaultValue()) {
                                 if ($columnPage->isDefaultValueInFile()) {
                                     $defaultValueReference = $columnPage->getDefaultValue();
                                     $defaultValueFilePath = sprintf(FilePathes::FILEPATH_DEFAULT_VALUE, $schemaId, $tableName, $columnId);
                                 } else {
                                     $rowData[$columnId] = $columnPage->getDefaultValue();
                                 }
                             } else {
                                 throw new ErrorException("Column '{$columnName}' cannot be NULL!");
                             }
                         }
                     }
                     if ($columnPage->isPrimaryKey()) {
                         $primaryKey[$columnName] = $rowData[$columnId];
                     }
                 }
                 $result->setLastInsertId($primaryKey);
                 $rowData = $table->convertStringRowToDataRow($rowData);
                 foreach ($indices as $indexId => $index) {
                     /* @var $index Index */
                     if (!$index->getIndexSchema()->isUnique()) {
                         continue;
                     }
                     if (count($index->searchRow($rowData)) > 0) {
                         $rowDataString = implode(", ", $rowData);
                         throw new ErrorException("Cannot insert because row '{$rowDataString}' collides " . "with unique key '{$index->getIndexSchema()->getName()}'!");
                     }
                 }
                 $rowDatas[] = $rowData;
             }
             break;
     }
     ### INSERT DATA
     $insertedRowIds = array();
     $success = false;
     try {
         foreach ($rowDatas as $rowData) {
             // check unique keys
             foreach ($indices as $indexId => $index) {
                 if (!$index->getIndexSchema()->isUnique()) {
                     continue;
                 }
                 if (count($index->searchRow($rowData)) > 0) {
                     throw new ErrorException("Cannot insert because of unique key '{$index->getIndexSchema()->getName()}'!");
                 }
             }
             $rowId = $table->addRowData($rowData);
             $insertedRowIds[] = $rowId;
             // insert into indicies
             foreach ($indices as $indexId => $index) {
                 $index->insertRow($rowData, $this->decstr($rowId));
             }
         }
         $success = true;
     } catch (Exception $exception) {
         ### ROLLBACK
         foreach ($insertedRowIds as $rowId) {
             $table->removeRow($rowId);
             // remove from indicies
             foreach ($indices as $indexId => $index) {
                 $index->removeRow($row, $this->decstr($rowId));
             }
         }
         throw new ErrorException("Exception in INSERT statement, rollback executed.", null, null, null, 0, $exception);
     }
     ### RESULT
     $result->setIsSuccess((bool) $success);
     return $result;
 }