From 692880c10e401f329e907ef085333583b2a2da00 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 3 Feb 2024 09:39:54 +0700 Subject: [PATCH 1/6] 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]; } } From 472fbd40199d6886aa36ee7bcad619db4b66a394 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 3 Feb 2024 09:57:09 +0700 Subject: [PATCH 2/6] Add tests --- tests/QueryBuilderTest.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 20c5e64b..a798d202 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -516,7 +516,6 @@ public function testInsertInteger() public function testRenameColumn(): void { $db = $this->getConnection(); - $qb = $db->getQueryBuilder(); $this->assertSame( @@ -525,6 +524,20 @@ public function testRenameColumn(): void SQL, $qb->renameColumn('alpha', 'string_identifier', 'string_identifier_test'), ); + + $this->assertSame( + <<renameColumn('alpha', 'non_exist_column', 'new_column'), + ); + + $this->assertSame( + <<renameColumn('non_exist_table', 'non_exist_column', 'new_column'), + ); } /** From 2590ec4466e5e391506bf2480b38149ce0d711af Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 3 Feb 2024 09:58:26 +0700 Subject: [PATCH 3/6] Change `public` to `private` --- src/DDLQueryBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index 7b06e76b..458fea91 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -172,7 +172,7 @@ public function renameColumn(string $table, string $oldName, string $newName): s * * @return string The column definition. */ - public function getColumnDefinition(string $table, string $column): string + private function getColumnDefinition(string $table, string $column): string { $sql = $this->schema->getTableSchema($table)?->getCreateSql(); From fcaf6ccf3f0e2a3b0e05905b7f82e114aceb0bab Mon Sep 17 00:00:00 2001 From: Tigrov Date: Sat, 3 Feb 2024 10:09:37 +0700 Subject: [PATCH 4/6] Update CHANGELOG.md [skip ci] --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5eea5e6e..2cfe410c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ - 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) +- Enh #320: Minor refactoring of `DDLQueryBuilder::getColumnDefinition()` method (@Tigrov) +- Bug #320: Change visibility of `DDLQueryBuilder::getColumnDefinition()` method to `private` (@Tigrov) ## 1.1.0 November 12, 2023 From bfa929aec7124fa83144250e4961ebed0d27cde9 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Wed, 3 Apr 2024 13:26:08 +0700 Subject: [PATCH 5/6] Update comments Update comments --- src/DDLQueryBuilder.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/DDLQueryBuilder.php b/src/DDLQueryBuilder.php index 43ef39d5..3dd1687f 100644 --- a/src/DDLQueryBuilder.php +++ b/src/DDLQueryBuilder.php @@ -168,9 +168,8 @@ public function renameColumn(string $table, string $oldName, string $newName): s * @param string $table The table name. * @param string $column The column name. * - * @throws Throwable In case when table doesn't contain a column. - * - * @return string The column definition. + * @return string The column definition or empty string in case when schema does not contain the table + * or the table doesn't contain the column. */ private function getColumnDefinition(string $table, string $column): string { From abf02895dfca101ed0374819ced79c73aeb858ad Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 4 Apr 2024 10:32:02 +0700 Subject: [PATCH 6/6] Update CHANGELOG.md [skip ci] --- CHANGELOG.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 974415f3..85434d58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,15 +2,14 @@ ## 1.2.1 under development -- no changes in this release. +- Enh #320: Minor refactoring of `DDLQueryBuilder::getColumnDefinition()` method (@Tigrov) +- Bug #320: Change visibility of `DDLQueryBuilder::getColumnDefinition()` method to `private` (@Tigrov) ## 1.2.0 March 21, 2024 - Enh #312: Change property `Schema::$typeMap` to constant `Schema::TYPE_MAP` (@Tigrov) - Enh #318: Resolve deprecated methods (@Tigrov) - Enh #319: Minor refactoring of `DDLQueryBuilder` and `Schema` (@Tigrov) -- Enh #320: Minor refactoring of `DDLQueryBuilder::getColumnDefinition()` method (@Tigrov) -- Bug #320: Change visibility of `DDLQueryBuilder::getColumnDefinition()` method to `private` (@Tigrov) - Bug #314: Fix `Command::insertWithReturningPks()` method for empty values (@Tigrov) ## 1.1.0 November 12, 2023