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; }
protected function convertColumnDefinitionToColumnSchema(ColumnDefinition $columnDefinition, ExecutionContext $executionContext) { $columnPage = new ColumnSchema(); $columnPage->setName($columnDefinition->getName()); /* @var $dataType DataType */ $dataType = $columnDefinition->getDataType(); $columnPage->setDataType($dataType); if (!is_null($columnDefinition->getDataTypeLength())) { $columnPage->setLength($columnDefinition->getDataTypeLength()); } if (!is_null($columnDefinition->getDataTypeSecondLength())) { $columnPage->setSecondLength($columnDefinition->getDataTypeSecondLength()); } $flags = 0; if ($columnDefinition->getIsAutoIncrement()) { $flags = $flags ^ ColumnSchema::EXTRA_AUTO_INCREMENT; } if (!$columnDefinition->getIsNullable()) { $flags = $flags ^ ColumnSchema::EXTRA_NOT_NULL; } if ($columnDefinition->getIsPrimaryKey()) { $flags = $flags ^ ColumnSchema::EXTRA_PRIMARY_KEY; } if ($columnDefinition->getIsUnique()) { $flags = $flags ^ ColumnSchema::EXTRA_UNIQUE_KEY; } if ($columnDefinition->getIsUnsigned()) { $flags = $flags ^ ColumnSchema::EXTRA_UNSIGNED; } if (false) { $flags = $flags ^ ColumnSchema::EXTRA_ZEROFILL; } $columnPage->setExtraFlags($flags); #$columnPage->setFKColumnIndex($index); #$columnPage->setFKTableIndex($index); /* @var $defaultValue Value */ $defaultValue = $columnDefinition->getDefaultValue(); if (!is_null($defaultValue)) { if (!$dataType->mustResolveDefaultValue()) { # default value must be resolved at insertion-time => save unresolved $defaultValueData = $this->valueResolver->resolveValue($defaultValue, $executionContext); $defaultValueData = $this->dataConverter->convertStringToBinary($defaultValueData, $columnPage->getDataType()); } else { $defaultValueData = (string) $defaultValue; } } else { $defaultValueData = null; } $columnPage->setDefaultValue($defaultValueData); $comment = $columnDefinition->getComment(); # TODO: save column comment return $columnPage; }