From 692880c10e401f329e907ef085333583b2a2da00 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 3 Feb 2024 09:39:54 +0700 Subject: [PATCH] Refactor `DDLQueryBuilder::getColumnDefinition()` --- CHANGELOG.md | 1 + src/DDLQueryBuilder.php | 14 ++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 148a6d14..5eea5e6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Enh #312: Change property `Schema::$typeMap` to constant `Schema::TYPE_MAP` (@Tigrov) - Bug #314: Fix `Command::insertWithReturningPks()` method for empty values (@Tigrov) +- Enh #320: Refactor `DDLQueryBuilder::getColumnDefinition()` method (@Tigrov) ## 1.1.0 November 12, 2023 diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index f812754c..7b06e76b 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -10,6 +10,7 @@ use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder; use function preg_match; +use function preg_quote; use function preg_replace; use function trim; @@ -173,21 +174,18 @@ public function renameColumn(string $table, string $oldName, string $newName): s */ public function getColumnDefinition(string $table, string $column): string { - $result = ''; $sql = $this->schema->getTableSchema($table)?->getCreateSql(); if (empty($sql)) { return ''; } - if (preg_match_all('/^\s*([`"])(.*?)\\1\s+(.*?),?$/m', $sql, $matches)) { - foreach ($matches[2] as $i => $c) { - if ($c === $column) { - $result = $matches[3][$i]; - } - } + $quotedColumn = preg_quote($column, '/'); + + if (preg_match("/^\s*([`\"])$quotedColumn\\1\s+(.*?),?$/m", $sql, $matches) !== 1) { + return ''; } - return $result; + return $matches[2]; } }