2 declare(strict_types = 1);
19 use Doctrine\DBAL\Platforms\AbstractPlatform;
20 use Doctrine\DBAL\Platforms\MySqlPlatform;
21 use Doctrine\DBAL\Schema\Column;
22 use Doctrine\DBAL\Schema\Index;
23 use Doctrine\DBAL\Schema\Table;
24 use Doctrine\DBAL\Types\Type;
25 use Doctrine\DBAL\Types\Types;
66 $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
68 foreach ($connectionPool->getCustomDoctrineTypes() as $type => $className) {
69 if (!Type::hasType($type)) {
70 Type::addType($type, $className);
73 $this->platform =
$platform ?: GeneralUtility::makeInstance(MySqlPlatform::class);
87 $this->table = GeneralUtility::makeInstance(
89 $tableStatement->tableName->getQuotedName(),
94 $this->buildTableOptions($tableStatement->tableOptions)
97 foreach ($tableStatement->createDefinition->items as $item) {
98 switch (get_class($item)) {
99 case CreateColumnDefinitionItem::class:
102 case CreateIndexDefinitionItem::class:
105 case CreateForeignKeyDefinitionItem::class:
109 throw new \RuntimeException(
110 'Unknown item definition of type "' . get_class($item) .
'" encountered.',
127 $column = $this->table->addColumn(
128 $item->columnName->getQuotedName(),
129 $this->getDoctrineColumnTypeName($item->dataType)
132 $column->setNotnull(!$item->allowNull);
133 $column->setAutoincrement((
bool)$item->autoIncrement);
134 $column->setComment($item->comment);
137 if ($item->hasDefaultValue && !$column->getAutoincrement()) {
138 $column->setDefault($item->defaultValue);
141 if ($item->dataType->getLength()) {
142 $column->setLength($item->dataType->getLength());
145 if ($item->dataType->getPrecision() >= 0) {
146 $column->setPrecision($item->dataType->getPrecision());
149 if ($item->dataType->getScale() >= 0) {
150 $column->setScale($item->dataType->getScale());
153 if ($item->dataType->isUnsigned()) {
154 $column->setUnsigned(
true);
158 if ($item->dataType->isFixed()) {
159 $column->setFixed(
true);
165 $column->setPlatformOption(
'unquotedValues', $item->dataType->getValues());
169 $this->table->addIndex([$item->columnName->getQuotedName()]);
173 $this->table->addUniqueIndex([$item->columnName->getQuotedName()]);
176 if ($item->primary) {
177 $this->table->setPrimaryKey([$item->columnName->getQuotedName()]);
180 if ($item->reference !==
null) {
182 [$item->columnName->getQuotedName()],
198 $indexName = $item->indexName->getQuotedName();
200 $columnNames = array_map(
202 if ($columnName->length) {
203 return $columnName->columnName->getQuotedName() .
'(' . $columnName->length .
')';
205 return $columnName->columnName->getQuotedName();
210 if ($item->isPrimary) {
211 $this->table->setPrimaryKey($columnNames);
212 $index = $this->table->getPrimaryKey();
214 $index = GeneralUtility::makeInstance(
222 if ($item->isFulltext) {
223 $index->addFlag(
'fulltext');
224 } elseif ($item->isSpatial) {
225 $index->addFlag(
'spatial');
228 $this->table = GeneralUtility::makeInstance(
230 $this->table->getQuotedName($this->platform),
231 $this->table->getColumns(),
232 array_merge($this->table->getIndexes(), [strtolower($indexName) => $index]),
233 $this->table->getForeignKeys(),
235 $this->table->getOptions()
249 $indexName = $item->indexName->getQuotedName() ?:
null;
250 $localColumnNames = array_map(
252 return $columnName->columnName->getQuotedName();
267 array $localColumnNames,
269 string $indexName =
null
271 $foreignTableName = $referenceDefinition->tableName->getQuotedName();
272 $foreignColumNames = array_map(
274 return $columnName->columnName->getQuotedName();
276 $referenceDefinition->columnNames
280 'onDelete' => $referenceDefinition->onDelete,
281 'onUpdate' => $referenceDefinition->onUpdate,
284 $this->table->addForeignKeyConstraint(
300 $doctrineType =
null;
301 switch (get_class($dataType)) {
302 case DataType\TinyIntDataType::class:
304 case DataType\SmallIntDataType::class:
305 $doctrineType = Types::SMALLINT;
307 case DataType\MediumIntDataType::class:
309 case DataType\IntegerDataType::class:
310 $doctrineType = Types::INTEGER;
312 case DataType\BigIntDataType::class:
313 $doctrineType = Types::BIGINT;
315 case DataType\BinaryDataType::class:
316 case DataType\VarBinaryDataType::class:
318 $doctrineType = Types::BINARY;
320 case DataType\TinyBlobDataType::class:
321 case DataType\MediumBlobDataType::class:
322 case DataType\BlobDataType::class:
323 case DataType\LongBlobDataType::class:
325 $doctrineType = Types::BLOB;
327 case DataType\DateDataType::class:
328 $doctrineType = Types::DATE_MUTABLE;
330 case DataType\TimestampDataType::class:
331 case DataType\DateTimeDataType::class:
333 $doctrineType = Types::DATETIME_MUTABLE;
335 case DataType\NumericDataType::class:
336 case DataType\DecimalDataType::class:
337 $doctrineType = Types::DECIMAL;
339 case DataType\RealDataType::class:
340 case DataType\FloatDataType::class:
341 case DataType\DoubleDataType::class:
342 $doctrineType = Types::FLOAT;
344 case DataType\TimeDataType::class:
345 $doctrineType = Types::TIME_MUTABLE;
347 case DataType\TinyTextDataType::class:
348 case DataType\MediumTextDataType::class:
349 case DataType\TextDataType::class:
350 case DataType\LongTextDataType::class:
351 $doctrineType = Types::TEXT;
353 case DataType\CharDataType::class:
354 case DataType\VarCharDataType::class:
355 $doctrineType = Types::STRING;
357 case DataType\EnumDataType::class:
360 case DataType\SetDataType::class:
363 case DataType\JsonDataType::class:
365 $doctrineType = Types::TEXT;
367 case DataType\YearDataType::class:
374 $doctrineType = Types::SMALLINT;
377 throw new \RuntimeException(
378 'Unsupported data type: ' . get_class($dataType) .
'!',
383 return $doctrineType;
396 if (!empty($tableOptions[
'engine'])) {
397 $options[
'engine'] = (string)$tableOptions[
'engine'];
399 if (!empty($tableOptions[
'character_set'])) {
400 $options[
'charset'] = (string)$tableOptions[
'character_set'];
402 if (!empty($tableOptions[
'collation'])) {
403 $options[
'collate'] = (string)$tableOptions[
'collation'];
405 if (!empty($tableOptions[
'auto_increment'])) {
406 $options[
'auto_increment'] = (string)$tableOptions[
'auto_increment'];
408 if (!empty($tableOptions[
'comment'])) {
409 $options[
'comment'] = (string)$tableOptions[
'comment'];
411 if (!empty($tableOptions[
'row_format'])) {
412 $options[
'row_format'] = (string)$tableOptions[
'row_format'];