Skip to content

Commit

Permalink
Refactor Quoter::quoteValue() method (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Nov 23, 2024
1 parent e3fad7b commit 47ded78
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Enh #359: Refactor `Dsn` class (@Tigrov)
- Enh #361, #362: Refactor `Schema::findColumns()` method (@Tigrov)
- Enh #363: Refactor `Schema::normalizeDefaultValue()` method and move it to `ColumnFactory` class (@Tigrov)
- Enh #366: Refactor `Quoter::quoteValue()` method (@Tigrov)

## 1.2.0 March 21, 2024

Expand Down
2 changes: 1 addition & 1 deletion src/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function buildCommentString(): string
return '';
}

return ' COMMENT ' . (string) (new Quoter('`', '`'))->quoteValue($this->getComment());
return ' COMMENT ' . (new Quoter('`', '`'))->quoteValue($this->getComment() ?? '');
}

public function asString(): string
Expand Down
2 changes: 1 addition & 1 deletion src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected function buildComment(ColumnSchemaInterface $column): string
{
$comment = $column->getComment();

return $comment === null ? '' : ' COMMENT ' . (string) $this->queryBuilder->quoter()->quoteValue($comment);
return $comment === null ? '' : ' COMMENT ' . $this->queryBuilder->quoter()->quoteValue($comment);
}

protected function getDbType(ColumnSchemaInterface $column): string
Expand Down
4 changes: 2 additions & 2 deletions src/DDLQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function addCommentOnColumn(string $table, string $column, string $commen
. $this->quoter->quoteColumnName($column)
. (empty($definition) ? '' : ' ' . $definition)
. ' COMMENT '
. (string) $this->quoter->quoteValue($comment);
. $this->quoter->quoteValue($comment);

if ($check === 1) {
$alterSql .= ' ' . $checkMatches[0];
Expand All @@ -70,7 +70,7 @@ public function addCommentOnTable(string $table, string $comment): string
return 'ALTER TABLE '
. $this->quoter->quoteTableName($table)
. ' COMMENT '
. (string) $this->quoter->quoteValue($comment);
. $this->quoter->quoteValue($comment);
}

public function addDefaultValue(string $table, string $name, string $column, mixed $value): string
Expand Down
23 changes: 11 additions & 12 deletions src/Quoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@

use Yiisoft\Db\Schema\Quoter as BaseQuoter;

use function is_string;
use function str_replace;
use function strtr;

/**
* Implements MySQL, MariaDB quoting and unquoting methods.
*/
final class Quoter extends BaseQuoter
{
public function quoteValue(mixed $value): mixed
public function quoteValue(string $value): string
{
if (!is_string($value)) {
return $value;
}

return "'" . str_replace(
['\\', "\x00", "\n", "\r", "'", '"', "\x1a"],
['\\\\', '\\0', '\\n', '\\r', "\'", '\"', '\\Z'],
$value
) . "'";
return "'" . strtr($value, [
'\\' => '\\\\',
"\x00" => '\\0',
"\n" => '\\n',
"\r" => '\\r',
"'" => "\'",
'"' => '\"',
"\x1a" => '\\Z',
]) . "'";
}
}
1 change: 1 addition & 0 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ public static function buildColumnDefinition(): array
$values["check('')"][0] = 'int';
$values['check(null)'][0] = 'int';
$values['defaultValue($expression)'][0] = 'int DEFAULT (1 + 2)';
$values['defaultValue($emptyExpression)'][0] = 'int';
$values["comment('comment')"][0] = "varchar(255) COMMENT 'comment'";
$values["comment('')"][0] = "varchar(255) COMMENT ''";
$values['integer()->primaryKey()'][0] = 'int PRIMARY KEY';
Expand Down
5 changes: 0 additions & 5 deletions tests/QuoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,9 @@ public function testQuoteValue(): void

$quoter = $db->getQuoter();

$this->assertFalse($quoter->quoteValue(false));
$this->assertTrue($quoter->quoteValue(true));
$this->assertNull($quoter->quoteValue(null));
$this->assertSame("'1.1'", $quoter->quoteValue('1.1'));
$this->assertSame("'1.1e0'", $quoter->quoteValue('1.1e0'));
$this->assertSame("'test'", $quoter->quoteValue('test'));
$this->assertSame("'test\'test'", $quoter->quoteValue("test'test"));
$this->assertSame(1, $quoter->quoteValue(1));
$this->assertSame(1.1, $quoter->quoteValue(1.1));
}
}

0 comments on commit 47ded78

Please sign in to comment.