Skip to content

Commit 57007d7

Browse files
authored
Fixes for Quoter (#355)
* Fixes for Quoter.
1 parent 2d37423 commit 57007d7

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

src/Driver/PDO/ConnectionPDO.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function getEmulatePrepare(): ?bool
119119
return $this->emulatePrepare;
120120
}
121121

122-
public function getPdo(): ?PDO
122+
public function getPDO(): ?PDO
123123
{
124124
return $this->pdo;
125125
}

src/Schema/Quoter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Yiisoft\Db\Schema;
66

7+
use PDO;
78
use function addcslashes;
89
use function explode;
910
use function implode;
@@ -24,7 +25,8 @@ public function __construct(
2425
private array|string $columnQuoteCharacter,
2526
/** @psalm-var string[]|string */
2627
private array|string $tableQuoteCharacter,
27-
private string $tablePrefix = ''
28+
private string $tablePrefix = '',
29+
protected PDO|null $pdo = null
2830
) {
2931
}
3032

@@ -134,6 +136,10 @@ public function quoteValue(mixed $value): mixed
134136
return $value;
135137
}
136138

139+
if ($this->pdo && ($value = $this->pdo->quote($value)) !== false) {
140+
return $value;
141+
}
142+
137143
return '\'' . str_replace('\'', '\'\'', addcslashes($value, "\000\032")) . '\'';
138144
}
139145

src/TestSupport/TestSchemaTrait.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,69 @@ public function testGetColumnNoExist(): void
346346
$this->assertNull($table->getColumn('no_exist'));
347347
}
348348

349+
public function testQuoterEscapingValue()
350+
{
351+
$db = $this->getConnection(true);
352+
$quoter = $db->getQuoter();
353+
354+
$db->createCommand('delete from {{quoter}}')->execute();
355+
$data = $this->generateQuoterEscapingValues();
356+
357+
foreach ($data as $index => $value) {
358+
$quotedName = $quoter->quoteValue('testValue_' . $index);
359+
$quoteValue = $quoter->quoteValue($value);
360+
361+
$db->createCommand('insert into {{quoter}}([[name]], [[description]]) values(' . $quotedName . ', ' . $quoteValue . ')')->execute();
362+
$result = $db->createCommand('select * from {{quoter}} where [[name]]=' . $quotedName)->queryOne();
363+
$this->assertEquals($value, $result['description']);
364+
}
365+
}
366+
367+
public function generateQuoterEscapingValues()
368+
{
369+
$result = [];
370+
$stringLength = 16;
371+
for ($i = 1; $i < 128 - $stringLength; $i += $stringLength) {
372+
$str = '';
373+
for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) {
374+
$str .= mb_chr($symbol, 'UTF-8');
375+
}
376+
$result[] = $str;
377+
378+
$str = '';
379+
for ($symbol = $i; $symbol < $i + $stringLength; $symbol++) {
380+
$str .= mb_chr($symbol, 'UTF-8') . mb_chr($symbol, 'UTF-8');
381+
}
382+
$result[] = $str;
383+
}
384+
385+
return $result;
386+
}
387+
388+
public function testQuoterEscapingValueFull()
389+
{
390+
$this->markTestSkipped('Very long test - only for check quoteValue');
391+
$template = 'aaaaa{1}aaa{1}aaaabbbbb{2}bbbb{2}bbbb';
392+
393+
$db = $this->getConnection(true);
394+
$quoter = $db->getQuoter();
395+
396+
$db->createCommand('delete from {{quoter}}')->execute();
397+
398+
for ($symbol1 = 1; $symbol1 <= 127; $symbol1++) {
399+
for ($symbol2 = 1; $symbol2 <= 127; $symbol2++) {
400+
$quotedName = $quoter->quoteValue('test_' . $symbol1 . '_' . $symbol2);
401+
$testString = str_replace(['{1}', '{2}',], [chr($symbol1), chr($symbol2)], $template);
402+
403+
$quoteValue = $quoter->quoteValue($testString);
404+
405+
$db->createCommand('insert into {{quoter}}([[name]], [[description]]) values(' . $quotedName . ', ' . $quoteValue . ')')->execute();
406+
$result = $db->createCommand('select * from {{quoter}} where [[name]]=' . $quotedName)->queryOne();
407+
$this->assertEquals($testString, $result['description']);
408+
}
409+
}
410+
}
411+
349412
private function assertMetadataEquals($expected, $actual): void
350413
{
351414
switch (strtolower(gettype($expected))) {

0 commit comments

Comments
 (0)