Skip to content

Commit

Permalink
Fixes for Quoter (#144)
Browse files Browse the repository at this point in the history
* Fixes for Quoter.
  • Loading branch information
darkdef committed Sep 2, 2022
1 parent 5f608f5 commit 56e58f0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/ConnectionPDO.php
Expand Up @@ -10,6 +10,7 @@
use Yiisoft\Db\Exception\Exception;
use Yiisoft\Db\Exception\InvalidConfigException;
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface;
use Yiisoft\Db\Schema\Quoter;
use Yiisoft\Db\Schema\QuoterInterface;
use Yiisoft\Db\Schema\SchemaInterface;
use Yiisoft\Db\Transaction\TransactionInterface;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function getQueryBuilder(): QueryBuilderInterface
public function getQuoter(): QuoterInterface
{
if ($this->quoter === null) {
$this->quoter = new Quoter('`', '`', $this->getTablePrefix());
$this->quoter = new Quoter('`', '`', $this->getTablePrefix(), $this->getActivePDO());
}

return $this->quoter;
Expand Down
19 changes: 14 additions & 5 deletions src/Quoter.php
Expand Up @@ -4,10 +4,14 @@

namespace Yiisoft\Db\Mysql;

use PDO;
use Yiisoft\Db\Schema\Quoter as BaseQuoter;
use Yiisoft\Db\Schema\QuoterInterface;

final class Quoter extends BaseQuoter implements QuoterInterface
/**
* @todo Remove or use? Where is question
* Temporary not used. Need add more tests for many charset
*/
final class Quoter extends BaseQuoter
{
/**
* @psalm-param string[]|string $columnQuoteCharacter
Expand All @@ -16,9 +20,10 @@ final class Quoter extends BaseQuoter implements QuoterInterface
public function __construct(
array|string $columnQuoteCharacter,
array|string $tableQuoteCharacter,
string $tablePrefix = ''
string $tablePrefix = '',
protected PDO|null $pdo = null
) {
parent::__construct($columnQuoteCharacter, $tableQuoteCharacter, $tablePrefix);
parent::__construct($columnQuoteCharacter, $tableQuoteCharacter, $tablePrefix, $pdo);
}

public function quoteValue(mixed $value): mixed
Expand All @@ -27,6 +32,10 @@ public function quoteValue(mixed $value): mixed
return $value;
}

return "'" . preg_replace('~[\x00\x0A\x0D\x1A\x22\x25\x27\x5C]~u', '\\\$0', $value) . "'";
return "'" . str_replace(
['\\', "\x00", "\n", "\r", "'", '"', "\x1a"],
['\\\\', '\\0', '\\n', '\\r', "\'", '\"', '\\Z'],
$value
) . "'";
}
}
8 changes: 8 additions & 0 deletions tests/Fixture/mysql.sql
Expand Up @@ -12,6 +12,7 @@ DROP TABLE IF EXISTS `order_with_null_fk` CASCADE;
DROP TABLE IF EXISTS `category` CASCADE;
DROP TABLE IF EXISTS `customer` CASCADE;
DROP TABLE IF EXISTS `profile` CASCADE;
DROP TABLE IF EXISTS `quoter` CASCADE;
DROP TABLE IF EXISTS `null_values` CASCADE;
DROP TABLE IF EXISTS `negative_default_values` CASCADE;
DROP TABLE IF EXISTS `type` CASCADE;
Expand Down Expand Up @@ -47,6 +48,13 @@ CREATE TABLE `profile` (
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `quoter` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(16) NOT NULL,
`description` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `customer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`email` varchar(128) NOT NULL,
Expand Down

0 comments on commit 56e58f0

Please sign in to comment.