Skip to content

Commit 008de26

Browse files
authored
Merge pull request #250 from simon-mundy/fix/quote-identifier-escape
Double a quote character inside an identifier instead of replacing it
2 parents faafc17 + b4503e9 commit 008de26

8 files changed

Lines changed: 47 additions & 16 deletions

File tree

‎src/Adapter/Platform/AbstractPlatform.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ abstract class AbstractPlatform implements PlatformInterface
2929
/** @var array{string, string} The opening and closing identifier quotes */
3030
protected array $quoteIdentifier = ['"', '"'];
3131

32-
protected string $quoteIdentifierTo = '\'';
32+
/** Replacement for a quote character inside an identifier; SQL-92 doubles it */
33+
protected string $quoteIdentifierTo = '""';
3334

3435
protected bool $quoteIdentifiers = true;
3536

‎test/unit/Adapter/Platform/Sql92Test.php‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PhpDbTest\Adapter\Platform\TestAsset\TestPlatform;
1313
use PhpDbTest\TestAsset\TestSql92Platform;
1414
use PHPUnit\Framework\Attributes\CoversMethod;
15+
use PHPUnit\Framework\Attributes\DataProvider;
1516
use PHPUnit\Framework\Attributes\Group;
1617
use PHPUnit\Framework\Attributes\Test;
1718
use PHPUnit\Framework\TestCase;
@@ -40,6 +41,28 @@ final class Sql92Test extends TestCase
4041
{
4142
protected Sql92 $platform;
4243

44+
/**
45+
* @return array<string, array{string, string}>
46+
*/
47+
public static function identifiersWithEmbeddedQuotes(): array
48+
{
49+
return [
50+
'quote inside a name' => ['my"col', '"my""col"'],
51+
'quote only' => ['"', '""""'],
52+
'adjacent quotes' => ['a""b', '"a""""b"'],
53+
'single quote unchanged' => ["it's", "\"it's\""],
54+
];
55+
}
56+
57+
#[DataProvider('identifiersWithEmbeddedQuotes')]
58+
#[Test]
59+
public function abstractPlatformQuoteIdentifierDoublesEmbeddedQuoteCharacters(
60+
string $identifier,
61+
string $expected,
62+
): void {
63+
static::assertSame($expected, (new TestPlatform())->quoteIdentifier($identifier));
64+
}
65+
4366
#[Test]
4467
public function abstractPlatformQuoteValueEscapesWithDriver(): void
4568
{
@@ -107,6 +130,13 @@ public function quoteIdentifierChainEscapesDoubleQuotes(): void
107130
static::assertSame('"a\\"b"."c"', $this->platform->quoteIdentifierChain(['a"b', 'c']));
108131
}
109132

133+
#[DataProvider('identifiersWithEmbeddedQuotes')]
134+
#[Test]
135+
public function quoteIdentifierDoublesEmbeddedQuoteCharacters(string $identifier, string $expected): void
136+
{
137+
static::assertSame($expected, $this->platform->quoteIdentifier($identifier));
138+
}
139+
110140
#[Test]
111141
public function quoteIdentifierInFragment(): void
112142
{
@@ -143,7 +173,7 @@ public function quoteIdentifierInFragmentReplacesTheOpeningQuoteInEachPart(): vo
143173
{
144174
$platform = new TestPlatform(quoteIdentifier: ['$', '#']);
145175

146-
static::assertSame("\$a'b#.\$c#", $platform->quoteIdentifierInFragment('a$b.c'));
176+
static::assertSame('$a""b#.$c#', $platform->quoteIdentifierInFragment('a$b.c'));
147177
}
148178

149179
#[Test]
@@ -167,7 +197,7 @@ public function quoteIdentifierWrapsInTheOpeningAndClosingQuotesAndReplacesTheOp
167197
{
168198
$platform = new TestPlatform(quoteIdentifier: ['[', ']']);
169199

170-
static::assertSame("[a'b]c]", $platform->quoteIdentifier('a[b]c'));
200+
static::assertSame('[a""b]c]', $platform->quoteIdentifier('a[b]c'));
171201
}
172202

173203
#[Test]

‎test/unit/Sql/IdentifierQuoterTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static function quotedNameProvider(): array
8383
'as after qualifier' => ['t.as', '"t"."as"'],
8484
'as alone' => ['as', '"as"'],
8585
'non-ascii letter' => ['café', '"café"'],
86-
'quote character' => ['a"b', '"a\'b"'],
86+
'quote character' => ['a"b', '"a""b"'],
8787
];
8888
}
8989

‎test/unit/Sql/InsertColumnNameTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static function insertColumnPreparedProvider(): array
5252
],
5353
'quote character' => [
5454
['c"d' => 1],
55-
'INSERT INTO "t" ("c\'d") VALUES (:c_d)',
55+
'INSERT INTO "t" ("c""d") VALUES (:c_d)',
5656
['c_d' => 1],
5757
],
5858
'sanitised names collide' => [
@@ -78,7 +78,7 @@ public static function insertColumnSqlProvider(): array
7878
'dotted' => [['film.title' => 1], 'INSERT INTO "t" ("film.title") VALUES (\'1\')'],
7979
'hyphen' => [['x-y' => 1], 'INSERT INTO "t" ("x-y") VALUES (\'1\')'],
8080
'space' => [['a b' => 1], 'INSERT INTO "t" ("a b") VALUES (\'1\')'],
81-
'quote character' => [['c"d' => 1], 'INSERT INTO "t" ("c\'d") VALUES (\'1\')'],
81+
'quote character' => [['c"d' => 1], 'INSERT INTO "t" ("c""d") VALUES (\'1\')'],
8282
];
8383
}
8484

‎test/unit/Sql/PredicateIdentifierNameTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function brokenNameProvider(): array
4040
return [
4141
'hyphen' => ['x-y', '"x-y"'],
4242
'space' => ['my col', '"my col"'],
43-
'quote character' => ['a"b', '"a\'b"'],
43+
'quote character' => ['a"b', '"a""b"'],
4444
'non-ascii' => ['nâme', '"nâme"'],
4545
'tab' => ["a\tb", "\"a\tb\""],
4646
'dotted hyphen' => ['t-1.x-y', '"t-1"."x-y"'],

‎test/unit/Sql/SelectColumnNameTest.php‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static function groupColumnProvider(): array
3131
'hyphen' => ['x-y', '"x-y"'],
3232
'dotted hyphen' => ['t-1.x-y', '"t-1"."x-y"'],
3333
'space' => ['my col', '"my col"'],
34-
'quote character' => ['a"b', '"a\'b"'],
34+
'quote character' => ['a"b', '"a""b"'],
3535
'arithmetic' => ['a * b', '"a" * "b"'],
3636
'arithmetic without spaces' => ['a*b', '"a"*"b"'],
3737
'expression' => [new Expression('YEAR(created)'), 'YEAR(created)'],
@@ -53,8 +53,8 @@ public static function joinColumnProvider(): array
5353
'hyphen aliased' => [['a' => 'x-y'], '"zac"."x-y" AS "a"'],
5454
'space' => [['my col'], '"zac"."my col" AS "my col"'],
5555
'space aliased' => [['a' => 'my col'], '"zac"."my col" AS "a"'],
56-
'quote character' => [['a"b'], '"zac"."a\'b" AS "a\'b"'],
57-
'quote character aliased' => [['a' => 'a"b'], '"zac"."a\'b" AS "a"'],
56+
'quote character' => [['a"b'], '"zac"."a""b" AS "a""b"'],
57+
'quote character aliased' => [['a' => 'a"b'], '"zac"."a""b" AS "a"'],
5858
'arithmetic' => [['price * qty'], '"zac"."price" * "qty" AS "price * qty"'],
5959
'arithmetic without spaces' => [['a*b'], '"zac"."a"*"b" AS "a*b"'],
6060
'inline alias' => [['bar AS baz'], '"zac"."bar" AS "baz" AS "bar AS baz"'],
@@ -82,8 +82,8 @@ public static function selectColumnProvider(): array
8282
'hyphen without prefix' => [['x-y'], false, 'SELECT "x-y" AS "x-y" FROM "foo"'],
8383
'space' => [['my col'], true, 'SELECT "foo"."my col" AS "my col" FROM "foo"'],
8484
'space aliased' => [['a' => 'my col'], true, 'SELECT "foo"."my col" AS "a" FROM "foo"'],
85-
'quote character' => [['a"b'], true, 'SELECT "foo"."a\'b" AS "a\'b" FROM "foo"'],
86-
'quote character aliased' => [['a' => 'a"b'], true, 'SELECT "foo"."a\'b" AS "a" FROM "foo"'],
85+
'quote character' => [['a"b'], true, 'SELECT "foo"."a""b" AS "a""b" FROM "foo"'],
86+
'quote character aliased' => [['a' => 'a"b'], true, 'SELECT "foo"."a""b" AS "a" FROM "foo"'],
8787
'dotted hyphen' => [['t-1.x-y'], false, 'SELECT "t-1"."x-y" AS "t-1.x-y" FROM "foo"'],
8888
'dotted space aliased' => [
8989
['a' => 't 1.my col'],

‎test/unit/Sql/SelectOrderNameTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public static function brokenOrderProvider(): array
3232
'hyphen' => ['x-y', '"x-y" ASC'],
3333
'hyphen descending' => ['x-y DESC', '"x-y" DESC'],
3434
'dotted hyphen' => ['t-1.x-y DESC', '"t-1"."x-y" DESC'],
35-
'quote character' => ['a"b DESC', '"a\'b" DESC'],
35+
'quote character' => ['a"b DESC', '"a""b" DESC'],
3636
'non-ascii' => ['café DESC', '"café" DESC'],
3737
'hyphen in comma list' => ['x-y DESC, b', '"x-y" DESC, "b" ASC'],
3838
'hyphen array key' => [['x-y' => 'DESC'], '"x-y" DESC'],
3939
'space array key' => [['my col' => 'DESC'], '"my col" DESC'],
4040
'dotted space array key' => [['t.my col' => 'ASC'], '"t"."my col" ASC'],
41-
'quote character array key' => [['a"b' => 'DESC'], '"a\'b" DESC'],
41+
'quote character array key' => [['a"b' => 'DESC'], '"a""b" DESC'],
4242
'non-ascii array key' => [['café' => 'DESC'], '"café" DESC'],
4343
'hyphen array list' => [['x-y DESC', 'b'], '"x-y" DESC, "b" ASC'],
4444
'hyphen qualified star' => ['x-y.* DESC', '"x-y".* DESC'],

‎test/unit/Sql/UpdateColumnNameTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function setColumnPreparedProvider(): array
5353
],
5454
'quote character' => [
5555
['c"d' => 1],
56-
'UPDATE "t" SET "c\'d" = :c_d',
56+
'UPDATE "t" SET "c""d" = :c_d',
5757
['c_d' => 1],
5858
],
5959
'sanitised names collide' => [
@@ -84,7 +84,7 @@ public static function setColumnSqlProvider(): array
8484
'dotted' => [['film.title' => 1], 'UPDATE "t" SET "film"."title" = \'1\''],
8585
'hyphen' => [['x-y' => 1], 'UPDATE "t" SET "x-y" = \'1\''],
8686
'space' => [['a b' => 1], 'UPDATE "t" SET "a b" = \'1\''],
87-
'quote character' => [['c"d' => 1], 'UPDATE "t" SET "c\'d" = \'1\''],
87+
'quote character' => [['c"d' => 1], 'UPDATE "t" SET "c""d" = \'1\''],
8888
'dotted hyphen' => [['f-1.x-y' => 1], 'UPDATE "t" SET "f-1"."x-y" = \'1\''],
8989
];
9090
}

0 commit comments

Comments
 (0)