Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DDLQueryBuilder::getColumnDefinition() #320

Merged
merged 9 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- 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)
Tigrov marked this conversation as resolved.
Show resolved Hide resolved
- Bug #314: Fix `Command::insertWithReturningPks()` method for empty values (@Tigrov)

## 1.1.0 November 12, 2023
Expand Down
21 changes: 9 additions & 12 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Db\QueryBuilder\AbstractDDLQueryBuilder;

use function preg_match;
use function preg_quote;
use function preg_replace;
use function trim;

Expand Down Expand Up @@ -167,27 +168,23 @@ 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.
*/
public function getColumnDefinition(string $table, string $column): string
private function getColumnDefinition(string $table, string $column): string
vjik marked this conversation as resolved.
Show resolved Hide resolved
{
$result = '';
$sql = $this->schema->getTableSchema($table)?->getCreateSql();

if (empty($sql)) {
return '';
}

if (preg_match_all('/^\s*([`"])(.*?)\\1\s+(.*?),?$/m', $sql, $matches) > 0) {
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 '';
samdark marked this conversation as resolved.
Show resolved Hide resolved
}

return $result;
return $matches[2];
}
}
15 changes: 14 additions & 1 deletion tests/QueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,6 @@ public function testInsertInteger()
public function testRenameColumn(): void
{
$db = $this->getConnection();

$qb = $db->getQueryBuilder();

$this->assertSame(
Expand All @@ -525,6 +524,20 @@ public function testRenameColumn(): void
SQL,
$qb->renameColumn('alpha', 'string_identifier', 'string_identifier_test'),
);

$this->assertSame(
<<<SQL
ALTER TABLE `alpha` CHANGE `non_exist_column` `new_column`
SQL,
$qb->renameColumn('alpha', 'non_exist_column', 'new_column'),
);

$this->assertSame(
<<<SQL
ALTER TABLE `non_exist_table` CHANGE `non_exist_column` `new_column`
SQL,
$qb->renameColumn('non_exist_table', 'non_exist_column', 'new_column'),
);
}

/**
Expand Down
Loading