Skip to content

Commit

Permalink
Refactor DDLQueryBuilder::getColumnDefinition()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Feb 3, 2024
1 parent fa3c327 commit 692880c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 6 additions & 8 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 @@ -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)) {

Check failure on line 179 in src/DDLQueryBuilder.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

RiskyTruthyFalsyComparison

src/DDLQueryBuilder.php:179:13: RiskyTruthyFalsyComparison: Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

Check failure on line 179 in src/DDLQueryBuilder.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

RiskyTruthyFalsyComparison

src/DDLQueryBuilder.php:179:13: RiskyTruthyFalsyComparison: Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
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];
}
}

0 comments on commit 692880c

Please sign in to comment.