From 329bef8cc8756010d6f47aa67956458983d8584a Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 14:37:06 +1100 Subject: [PATCH 1/4] Double a quote character inside an identifier instead of replacing it AbstractPlatform defaulted $quoteIdentifierTo to a single quote, so quoteIdentifier('my"col') rendered "my'col" and named a different column. SQL-92 escapes an embedded identifier quote by doubling it, so the default is now '""' and the same name renders "my""col". The Insert and Update column name tests had the old output as their expectation for a column named c"d; they now expect "c""d". --- src/Adapter/Platform/AbstractPlatform.php | 3 ++- test/unit/Adapter/Platform/Sql92Test.php | 30 +++++++++++++++++++++++ test/unit/Sql/InsertColumnNameTest.php | 4 +-- test/unit/Sql/UpdateColumnNameTest.php | 4 +-- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/Adapter/Platform/AbstractPlatform.php b/src/Adapter/Platform/AbstractPlatform.php index dd09a77b5..f8edb87d7 100644 --- a/src/Adapter/Platform/AbstractPlatform.php +++ b/src/Adapter/Platform/AbstractPlatform.php @@ -27,7 +27,8 @@ abstract class AbstractPlatform implements PlatformInterface /** @var string[] */ protected array $quoteIdentifier = ['"', '"']; - protected string $quoteIdentifierTo = '\''; + /** Replacement for a quote character inside an identifier; SQL-92 doubles it */ + protected string $quoteIdentifierTo = '""'; protected bool $quoteIdentifiers = true; diff --git a/test/unit/Adapter/Platform/Sql92Test.php b/test/unit/Adapter/Platform/Sql92Test.php index 3be65869e..22c8ba02c 100644 --- a/test/unit/Adapter/Platform/Sql92Test.php +++ b/test/unit/Adapter/Platform/Sql92Test.php @@ -12,6 +12,7 @@ use PhpDbTest\Adapter\Platform\TestAsset\TestPlatform; use PhpDbTest\TestAsset\TestSql92Platform; use PHPUnit\Framework\Attributes\CoversMethod; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; @@ -40,6 +41,28 @@ final class Sql92Test extends TestCase { protected Sql92 $platform; + /** + * @return array + */ + public static function identifiersWithEmbeddedQuotes(): array + { + return [ + 'quote inside a name' => ['my"col', '"my""col"'], + 'quote only' => ['"', '""""'], + 'adjacent quotes' => ['a""b', '"a""""b"'], + 'single quote unchanged' => ["it's", "\"it's\""], + ]; + } + + #[DataProvider('identifiersWithEmbeddedQuotes')] + #[Test] + public function abstractPlatformQuoteIdentifierDoublesEmbeddedQuoteCharacters( + string $identifier, + string $expected, + ): void { + static::assertSame($expected, (new TestPlatform())->quoteIdentifier($identifier)); + } + #[Test] public function abstractPlatformQuoteValueEscapesWithDriver(): void { @@ -95,6 +118,13 @@ public function quoteIdentifierChain(): void static::assertSame('"schema"."identifier"', $this->platform->quoteIdentifierChain(['schema', 'identifier'])); } + #[DataProvider('identifiersWithEmbeddedQuotes')] + #[Test] + public function quoteIdentifierDoublesEmbeddedQuoteCharacters(string $identifier, string $expected): void + { + static::assertSame($expected, $this->platform->quoteIdentifier($identifier)); + } + #[Test] public function quoteIdentifierInFragment(): void { diff --git a/test/unit/Sql/InsertColumnNameTest.php b/test/unit/Sql/InsertColumnNameTest.php index dec805995..76571cf87 100644 --- a/test/unit/Sql/InsertColumnNameTest.php +++ b/test/unit/Sql/InsertColumnNameTest.php @@ -52,7 +52,7 @@ public static function insertColumnPreparedProvider(): array ], 'quote character' => [ ['c"d' => 1], - 'INSERT INTO "t" ("c\'d") VALUES (:c_d)', + 'INSERT INTO "t" ("c""d") VALUES (:c_d)', ['c_d' => 1], ], 'sanitised names collide' => [ @@ -78,7 +78,7 @@ public static function insertColumnSqlProvider(): array 'dotted' => [['film.title' => 1], 'INSERT INTO "t" ("film.title") VALUES (\'1\')'], 'hyphen' => [['x-y' => 1], 'INSERT INTO "t" ("x-y") VALUES (\'1\')'], 'space' => [['a b' => 1], 'INSERT INTO "t" ("a b") VALUES (\'1\')'], - 'quote character' => [['c"d' => 1], 'INSERT INTO "t" ("c\'d") VALUES (\'1\')'], + 'quote character' => [['c"d' => 1], 'INSERT INTO "t" ("c""d") VALUES (\'1\')'], ]; } diff --git a/test/unit/Sql/UpdateColumnNameTest.php b/test/unit/Sql/UpdateColumnNameTest.php index 1f8cc8a60..fa87ec167 100644 --- a/test/unit/Sql/UpdateColumnNameTest.php +++ b/test/unit/Sql/UpdateColumnNameTest.php @@ -53,7 +53,7 @@ public static function setColumnPreparedProvider(): array ], 'quote character' => [ ['c"d' => 1], - 'UPDATE "t" SET "c\'d" = :c_d', + 'UPDATE "t" SET "c""d" = :c_d', ['c_d' => 1], ], 'sanitised names collide' => [ @@ -84,7 +84,7 @@ public static function setColumnSqlProvider(): array 'dotted' => [['film.title' => 1], 'UPDATE "t" SET "film"."title" = \'1\''], 'hyphen' => [['x-y' => 1], 'UPDATE "t" SET "x-y" = \'1\''], 'space' => [['a b' => 1], 'UPDATE "t" SET "a b" = \'1\''], - 'quote character' => [['c"d' => 1], 'UPDATE "t" SET "c\'d" = \'1\''], + 'quote character' => [['c"d' => 1], 'UPDATE "t" SET "c""d" = \'1\''], 'dotted hyphen' => [['f-1.x-y' => 1], 'UPDATE "t" SET "f-1"."x-y" = \'1\''], ]; } From d7971e2fbb8e0e966e1e0a689141947d161a4a53 Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 16:36:35 +1100 Subject: [PATCH 2/4] Expect doubled quotes in the identifier tests merged from 0.6.x The IdentifierQuoter, column-name and ORDER BY tests from #241 and #243 pinned the old SQL-92 escaping, which turned an embedded " into '. With this PR's fix the quote is doubled, so a"b now renders "a""b". --- test/unit/Sql/IdentifierQuoterTest.php | 2 +- test/unit/Sql/SelectColumnNameTest.php | 10 +++++----- test/unit/Sql/SelectOrderNameTest.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/unit/Sql/IdentifierQuoterTest.php b/test/unit/Sql/IdentifierQuoterTest.php index b5ed8f071..d37b67bbc 100644 --- a/test/unit/Sql/IdentifierQuoterTest.php +++ b/test/unit/Sql/IdentifierQuoterTest.php @@ -83,7 +83,7 @@ public static function quotedNameProvider(): array 'as after qualifier' => ['t.as', '"t"."as"'], 'as alone' => ['as', '"as"'], 'non-ascii letter' => ['café', '"café"'], - 'quote character' => ['a"b', '"a\'b"'], + 'quote character' => ['a"b', '"a""b"'], ]; } diff --git a/test/unit/Sql/SelectColumnNameTest.php b/test/unit/Sql/SelectColumnNameTest.php index f7f00b759..bb2fd1e01 100644 --- a/test/unit/Sql/SelectColumnNameTest.php +++ b/test/unit/Sql/SelectColumnNameTest.php @@ -31,7 +31,7 @@ public static function groupColumnProvider(): array 'hyphen' => ['x-y', '"x-y"'], 'dotted hyphen' => ['t-1.x-y', '"t-1"."x-y"'], 'space' => ['my col', '"my col"'], - 'quote character' => ['a"b', '"a\'b"'], + 'quote character' => ['a"b', '"a""b"'], 'arithmetic' => ['a * b', '"a" * "b"'], 'arithmetic without spaces' => ['a*b', '"a"*"b"'], 'expression' => [new Expression('YEAR(created)'), 'YEAR(created)'], @@ -53,8 +53,8 @@ public static function joinColumnProvider(): array 'hyphen aliased' => [['a' => 'x-y'], '"zac"."x-y" AS "a"'], 'space' => [['my col'], '"zac"."my col" AS "my col"'], 'space aliased' => [['a' => 'my col'], '"zac"."my col" AS "a"'], - 'quote character' => [['a"b'], '"zac"."a\'b" AS "a\'b"'], - 'quote character aliased' => [['a' => 'a"b'], '"zac"."a\'b" AS "a"'], + 'quote character' => [['a"b'], '"zac"."a""b" AS "a""b"'], + 'quote character aliased' => [['a' => 'a"b'], '"zac"."a""b" AS "a"'], 'arithmetic' => [['price * qty'], '"zac"."price" * "qty" AS "price * qty"'], 'arithmetic without spaces' => [['a*b'], '"zac"."a"*"b" AS "a*b"'], 'inline alias' => [['bar AS baz'], '"zac"."bar" AS "baz" AS "bar AS baz"'], @@ -82,8 +82,8 @@ public static function selectColumnProvider(): array 'hyphen without prefix' => [['x-y'], false, 'SELECT "x-y" AS "x-y" FROM "foo"'], 'space' => [['my col'], true, 'SELECT "foo"."my col" AS "my col" FROM "foo"'], 'space aliased' => [['a' => 'my col'], true, 'SELECT "foo"."my col" AS "a" FROM "foo"'], - 'quote character' => [['a"b'], true, 'SELECT "foo"."a\'b" AS "a\'b" FROM "foo"'], - 'quote character aliased' => [['a' => 'a"b'], true, 'SELECT "foo"."a\'b" AS "a" FROM "foo"'], + 'quote character' => [['a"b'], true, 'SELECT "foo"."a""b" AS "a""b" FROM "foo"'], + 'quote character aliased' => [['a' => 'a"b'], true, 'SELECT "foo"."a""b" AS "a" FROM "foo"'], 'dotted hyphen' => [['t-1.x-y'], false, 'SELECT "t-1"."x-y" AS "t-1.x-y" FROM "foo"'], 'dotted space aliased' => [ ['a' => 't 1.my col'], diff --git a/test/unit/Sql/SelectOrderNameTest.php b/test/unit/Sql/SelectOrderNameTest.php index 1a9200eaa..2ebb8b3c8 100644 --- a/test/unit/Sql/SelectOrderNameTest.php +++ b/test/unit/Sql/SelectOrderNameTest.php @@ -32,13 +32,13 @@ public static function brokenOrderProvider(): array 'hyphen' => ['x-y', '"x-y" ASC'], 'hyphen descending' => ['x-y DESC', '"x-y" DESC'], 'dotted hyphen' => ['t-1.x-y DESC', '"t-1"."x-y" DESC'], - 'quote character' => ['a"b DESC', '"a\'b" DESC'], + 'quote character' => ['a"b DESC', '"a""b" DESC'], 'non-ascii' => ['café DESC', '"café" DESC'], 'hyphen in comma list' => ['x-y DESC, b', '"x-y" DESC, "b" ASC'], 'hyphen array key' => [['x-y' => 'DESC'], '"x-y" DESC'], 'space array key' => [['my col' => 'DESC'], '"my col" DESC'], 'dotted space array key' => [['t.my col' => 'ASC'], '"t"."my col" ASC'], - 'quote character array key' => [['a"b' => 'DESC'], '"a\'b" DESC'], + 'quote character array key' => [['a"b' => 'DESC'], '"a""b" DESC'], 'non-ascii array key' => [['café' => 'DESC'], '"café" DESC'], 'hyphen array list' => [['x-y DESC', 'b'], '"x-y" DESC, "b" ASC'], 'hyphen qualified star' => ['x-y.* DESC', '"x-y".* DESC'], From d3c1c563b94091bbf0ec51e2cf7615ff36abd0f4 Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 16:55:05 +1100 Subject: [PATCH 3/4] Expect the doubled quote in the platform tests merged from 0.6.x Two Sql92Test cases from #248 pinned the old default replacement, which turned an opening quote inside an identifier into '. With this PR the default replacement doubles it, so the expectations become "". The merge also left a test method out of sorted order; mago format restores it. --- test/unit/Adapter/Platform/Sql92Test.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/unit/Adapter/Platform/Sql92Test.php b/test/unit/Adapter/Platform/Sql92Test.php index 7b0e70c90..cb3c92762 100644 --- a/test/unit/Adapter/Platform/Sql92Test.php +++ b/test/unit/Adapter/Platform/Sql92Test.php @@ -124,17 +124,17 @@ public function quoteIdentifierChain(): void static::assertSame('"schema"."identifier"', $this->platform->quoteIdentifierChain(['schema', 'identifier'])); } - #[DataProvider('identifiersWithEmbeddedQuotes')] #[Test] - public function quoteIdentifierDoublesEmbeddedQuoteCharacters(string $identifier, string $expected): void + public function quoteIdentifierChainEscapesDoubleQuotes(): void { - static::assertSame($expected, $this->platform->quoteIdentifier($identifier)); + static::assertSame('"a\\"b"."c"', $this->platform->quoteIdentifierChain(['a"b', 'c'])); } + #[DataProvider('identifiersWithEmbeddedQuotes')] #[Test] - public function quoteIdentifierChainEscapesDoubleQuotes(): void + public function quoteIdentifierDoublesEmbeddedQuoteCharacters(string $identifier, string $expected): void { - static::assertSame('"a\\"b"."c"', $this->platform->quoteIdentifierChain(['a"b', 'c'])); + static::assertSame($expected, $this->platform->quoteIdentifier($identifier)); } #[Test] @@ -173,7 +173,7 @@ public function quoteIdentifierInFragmentReplacesTheOpeningQuoteInEachPart(): vo { $platform = new TestPlatform(quoteIdentifier: ['$', '#']); - static::assertSame("\$a'b#.\$c#", $platform->quoteIdentifierInFragment('a$b.c')); + static::assertSame('$a""b#.$c#', $platform->quoteIdentifierInFragment('a$b.c')); } #[Test] @@ -197,7 +197,7 @@ public function quoteIdentifierWrapsInTheOpeningAndClosingQuotesAndReplacesTheOp { $platform = new TestPlatform(quoteIdentifier: ['[', ']']); - static::assertSame("[a'b]c]", $platform->quoteIdentifier('a[b]c')); + static::assertSame('[a""b]c]', $platform->quoteIdentifier('a[b]c')); } #[Test] From b4503e9792582ce2f2c6695ecabf7dd098f981af Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 17:13:17 +1100 Subject: [PATCH 4/4] Expect doubled quotes in the predicate name tests merged from 0.6.x PredicateIdentifierNameTest from #244 pinned the old SQL-92 escaping of an embedded " as '. With this PR the quote is doubled, so a"b renders "a""b" in every predicate slot. --- test/unit/Sql/PredicateIdentifierNameTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/Sql/PredicateIdentifierNameTest.php b/test/unit/Sql/PredicateIdentifierNameTest.php index 4516bf7cb..b9c7073f2 100644 --- a/test/unit/Sql/PredicateIdentifierNameTest.php +++ b/test/unit/Sql/PredicateIdentifierNameTest.php @@ -40,7 +40,7 @@ public static function brokenNameProvider(): array return [ 'hyphen' => ['x-y', '"x-y"'], 'space' => ['my col', '"my col"'], - 'quote character' => ['a"b', '"a\'b"'], + 'quote character' => ['a"b', '"a""b"'], 'non-ascii' => ['nâme', '"nâme"'], 'tab' => ["a\tb", "\"a\tb\""], 'dotted hyphen' => ['t-1.x-y', '"t-1"."x-y"'],