123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- <?php
- namespace Doctrine\DBAL\Schema;
- use Doctrine\DBAL\Platforms\MariaDb1027Platform;
- use Doctrine\DBAL\Platforms\MySqlPlatform;
- use Doctrine\DBAL\Types\Type;
- use function array_change_key_case;
- use function array_shift;
- use function array_values;
- use function assert;
- use function explode;
- use function is_string;
- use function preg_match;
- use function strpos;
- use function strtok;
- use function strtolower;
- use function strtr;
- use const CASE_LOWER;
- /**
- * Schema manager for the MySql RDBMS.
- */
- class MySqlSchemaManager extends AbstractSchemaManager
- {
- /**
- * @see https://mariadb.com/kb/en/library/string-literals/#escape-sequences
- */
- private const MARIADB_ESCAPE_SEQUENCES = [
- '\\0' => "\0",
- "\\'" => "'",
- '\\"' => '"',
- '\\b' => "\b",
- '\\n' => "\n",
- '\\r' => "\r",
- '\\t' => "\t",
- '\\Z' => "\x1a",
- '\\\\' => '\\',
- '\\%' => '%',
- '\\_' => '_',
- // Internally, MariaDB escapes single quotes using the standard syntax
- "''" => "'",
- ];
- /**
- * {@inheritdoc}
- */
- protected function _getPortableViewDefinition($view)
- {
- return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableTableDefinition($table)
- {
- return array_shift($table);
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableUserDefinition($user)
- {
- return [
- 'user' => $user['User'],
- 'password' => $user['Password'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableTableIndexesList($tableIndexes, $tableName = null)
- {
- foreach ($tableIndexes as $k => $v) {
- $v = array_change_key_case($v, CASE_LOWER);
- if ($v['key_name'] === 'PRIMARY') {
- $v['primary'] = true;
- } else {
- $v['primary'] = false;
- }
- if (strpos($v['index_type'], 'FULLTEXT') !== false) {
- $v['flags'] = ['FULLTEXT'];
- } elseif (strpos($v['index_type'], 'SPATIAL') !== false) {
- $v['flags'] = ['SPATIAL'];
- }
- // Ignore prohibited prefix `length` for spatial index
- if (strpos($v['index_type'], 'SPATIAL') === false) {
- $v['length'] = isset($v['sub_part']) ? (int) $v['sub_part'] : null;
- }
- $tableIndexes[$k] = $v;
- }
- return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableDatabaseDefinition($database)
- {
- return $database['Database'];
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableTableColumnDefinition($tableColumn)
- {
- $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
- $dbType = strtolower($tableColumn['type']);
- $dbType = strtok($dbType, '(), ');
- assert(is_string($dbType));
- $length = $tableColumn['length'] ?? strtok('(), ');
- $fixed = null;
- if (! isset($tableColumn['name'])) {
- $tableColumn['name'] = '';
- }
- $scale = null;
- $precision = null;
- $type = $this->_platform->getDoctrineTypeMapping($dbType);
- // In cases where not connected to a database DESCRIBE $table does not return 'Comment'
- if (isset($tableColumn['comment'])) {
- $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
- $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
- }
- switch ($dbType) {
- case 'char':
- case 'binary':
- $fixed = true;
- break;
- case 'float':
- case 'double':
- case 'real':
- case 'numeric':
- case 'decimal':
- if (preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
- $precision = $match[1];
- $scale = $match[2];
- $length = null;
- }
- break;
- case 'tinytext':
- $length = MySqlPlatform::LENGTH_LIMIT_TINYTEXT;
- break;
- case 'text':
- $length = MySqlPlatform::LENGTH_LIMIT_TEXT;
- break;
- case 'mediumtext':
- $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMTEXT;
- break;
- case 'tinyblob':
- $length = MySqlPlatform::LENGTH_LIMIT_TINYBLOB;
- break;
- case 'blob':
- $length = MySqlPlatform::LENGTH_LIMIT_BLOB;
- break;
- case 'mediumblob':
- $length = MySqlPlatform::LENGTH_LIMIT_MEDIUMBLOB;
- break;
- case 'tinyint':
- case 'smallint':
- case 'mediumint':
- case 'int':
- case 'integer':
- case 'bigint':
- case 'year':
- $length = null;
- break;
- }
- if ($this->_platform instanceof MariaDb1027Platform) {
- $columnDefault = $this->getMariaDb1027ColumnDefault($this->_platform, $tableColumn['default']);
- } else {
- $columnDefault = $tableColumn['default'];
- }
- $options = [
- 'length' => $length !== null ? (int) $length : null,
- 'unsigned' => strpos($tableColumn['type'], 'unsigned') !== false,
- 'fixed' => (bool) $fixed,
- 'default' => $columnDefault,
- 'notnull' => $tableColumn['null'] !== 'YES',
- 'scale' => null,
- 'precision' => null,
- 'autoincrement' => strpos($tableColumn['extra'], 'auto_increment') !== false,
- 'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== ''
- ? $tableColumn['comment']
- : null,
- ];
- if ($scale !== null && $precision !== null) {
- $options['scale'] = (int) $scale;
- $options['precision'] = (int) $precision;
- }
- $column = new Column($tableColumn['field'], Type::getType($type), $options);
- if (isset($tableColumn['characterset'])) {
- $column->setPlatformOption('charset', $tableColumn['characterset']);
- }
- if (isset($tableColumn['collation'])) {
- $column->setPlatformOption('collation', $tableColumn['collation']);
- }
- return $column;
- }
- /**
- * Return Doctrine/Mysql-compatible column default values for MariaDB 10.2.7+ servers.
- *
- * - Since MariaDb 10.2.7 column defaults stored in information_schema are now quoted
- * to distinguish them from expressions (see MDEV-10134).
- * - CURRENT_TIMESTAMP, CURRENT_TIME, CURRENT_DATE are stored in information_schema
- * as current_timestamp(), currdate(), currtime()
- * - Quoted 'NULL' is not enforced by Maria, it is technically possible to have
- * null in some circumstances (see https://jira.mariadb.org/browse/MDEV-14053)
- * - \' is always stored as '' in information_schema (normalized)
- *
- * @link https://mariadb.com/kb/en/library/information-schema-columns-table/
- * @link https://jira.mariadb.org/browse/MDEV-13132
- *
- * @param string|null $columnDefault default value as stored in information_schema for MariaDB >= 10.2.7
- */
- private function getMariaDb1027ColumnDefault(MariaDb1027Platform $platform, ?string $columnDefault): ?string
- {
- if ($columnDefault === 'NULL' || $columnDefault === null) {
- return null;
- }
- if (preg_match('/^\'(.*)\'$/', $columnDefault, $matches)) {
- return strtr($matches[1], self::MARIADB_ESCAPE_SEQUENCES);
- }
- switch ($columnDefault) {
- case 'current_timestamp()':
- return $platform->getCurrentTimestampSQL();
- case 'curdate()':
- return $platform->getCurrentDateSQL();
- case 'curtime()':
- return $platform->getCurrentTimeSQL();
- }
- return $columnDefault;
- }
- /**
- * {@inheritdoc}
- */
- protected function _getPortableTableForeignKeysList($tableForeignKeys)
- {
- $list = [];
- foreach ($tableForeignKeys as $value) {
- $value = array_change_key_case($value, CASE_LOWER);
- if (! isset($list[$value['constraint_name']])) {
- if (! isset($value['delete_rule']) || $value['delete_rule'] === 'RESTRICT') {
- $value['delete_rule'] = null;
- }
- if (! isset($value['update_rule']) || $value['update_rule'] === 'RESTRICT') {
- $value['update_rule'] = null;
- }
- $list[$value['constraint_name']] = [
- 'name' => $value['constraint_name'],
- 'local' => [],
- 'foreign' => [],
- 'foreignTable' => $value['referenced_table_name'],
- 'onDelete' => $value['delete_rule'],
- 'onUpdate' => $value['update_rule'],
- ];
- }
- $list[$value['constraint_name']]['local'][] = $value['column_name'];
- $list[$value['constraint_name']]['foreign'][] = $value['referenced_column_name'];
- }
- $result = [];
- foreach ($list as $constraint) {
- $result[] = new ForeignKeyConstraint(
- array_values($constraint['local']),
- $constraint['foreignTable'],
- array_values($constraint['foreign']),
- $constraint['name'],
- [
- 'onDelete' => $constraint['onDelete'],
- 'onUpdate' => $constraint['onUpdate'],
- ]
- );
- }
- return $result;
- }
- /**
- * {@inheritdoc}
- */
- public function listTableDetails($name)
- {
- $table = parent::listTableDetails($name);
- $platform = $this->_platform;
- assert($platform instanceof MySqlPlatform);
- $sql = $platform->getListTableMetadataSQL($name);
- $tableOptions = $this->_conn->fetchAssociative($sql);
- if ($tableOptions === false) {
- return $table;
- }
- $table->addOption('engine', $tableOptions['ENGINE']);
- if ($tableOptions['TABLE_COLLATION'] !== null) {
- $table->addOption('collation', $tableOptions['TABLE_COLLATION']);
- }
- if ($tableOptions['AUTO_INCREMENT'] !== null) {
- $table->addOption('autoincrement', $tableOptions['AUTO_INCREMENT']);
- }
- $table->addOption('comment', $tableOptions['TABLE_COMMENT']);
- $table->addOption('create_options', $this->parseCreateOptions($tableOptions['CREATE_OPTIONS']));
- return $table;
- }
- /**
- * @return string[]|true[]
- */
- private function parseCreateOptions(?string $string): array
- {
- $options = [];
- if ($string === null || $string === '') {
- return $options;
- }
- foreach (explode(' ', $string) as $pair) {
- $parts = explode('=', $pair, 2);
- $options[$parts[0]] = $parts[1] ?? true;
- }
- return $options;
- }
- }
|