1<?php
2
3namespace Doctrine\DBAL\Schema;
4
5use Doctrine\DBAL\Types\Type;
6use function explode;
7use function strtolower;
8use function trim;
9
10/**
11 * Schema manager for the Drizzle RDBMS.
12 */
13class DrizzleSchemaManager extends AbstractSchemaManager
14{
15    /**
16     * {@inheritdoc}
17     */
18    protected function _getPortableTableColumnDefinition($tableColumn)
19    {
20        $dbType = strtolower($tableColumn['DATA_TYPE']);
21
22        $type                          = $this->_platform->getDoctrineTypeMapping($dbType);
23        $type                          = $this->extractDoctrineTypeFromComment($tableColumn['COLUMN_COMMENT'], $type);
24        $tableColumn['COLUMN_COMMENT'] = $this->removeDoctrineTypeFromComment($tableColumn['COLUMN_COMMENT'], $type);
25
26        $options = [
27            'notnull' => ! (bool) $tableColumn['IS_NULLABLE'],
28            'length' => (int) $tableColumn['CHARACTER_MAXIMUM_LENGTH'],
29            'default' => $tableColumn['COLUMN_DEFAULT'] ?? null,
30            'autoincrement' => (bool) $tableColumn['IS_AUTO_INCREMENT'],
31            'scale' => (int) $tableColumn['NUMERIC_SCALE'],
32            'precision' => (int) $tableColumn['NUMERIC_PRECISION'],
33            'comment' => isset($tableColumn['COLUMN_COMMENT']) && $tableColumn['COLUMN_COMMENT'] !== ''
34                ? $tableColumn['COLUMN_COMMENT']
35                : null,
36        ];
37
38        $column = new Column($tableColumn['COLUMN_NAME'], Type::getType($type), $options);
39
40        if (! empty($tableColumn['COLLATION_NAME'])) {
41            $column->setPlatformOption('collation', $tableColumn['COLLATION_NAME']);
42        }
43
44        return $column;
45    }
46
47    /**
48     * {@inheritdoc}
49     */
50    protected function _getPortableDatabaseDefinition($database)
51    {
52        return $database['SCHEMA_NAME'];
53    }
54
55    /**
56     * {@inheritdoc}
57     */
58    protected function _getPortableTableDefinition($table)
59    {
60        return $table['TABLE_NAME'];
61    }
62
63    /**
64     * {@inheritdoc}
65     */
66    public function _getPortableTableForeignKeyDefinition($tableForeignKey)
67    {
68        $columns = [];
69        foreach (explode(',', $tableForeignKey['CONSTRAINT_COLUMNS']) as $value) {
70            $columns[] = trim($value, ' `');
71        }
72
73        $refColumns = [];
74        foreach (explode(',', $tableForeignKey['REFERENCED_TABLE_COLUMNS']) as $value) {
75            $refColumns[] = trim($value, ' `');
76        }
77
78        return new ForeignKeyConstraint(
79            $columns,
80            $tableForeignKey['REFERENCED_TABLE_NAME'],
81            $refColumns,
82            $tableForeignKey['CONSTRAINT_NAME'],
83            [
84                'onUpdate' => $tableForeignKey['UPDATE_RULE'],
85                'onDelete' => $tableForeignKey['DELETE_RULE'],
86            ]
87        );
88    }
89
90    /**
91     * {@inheritdoc}
92     */
93    protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
94    {
95        $indexes = [];
96        foreach ($tableIndexes as $k) {
97            $k['primary'] = (bool) $k['primary'];
98            $indexes[]    = $k;
99        }
100
101        return parent::_getPortableTableIndexesList($indexes, $tableName);
102    }
103}
104