diff --git a/src/Adapter/Platform/AbstractPlatform.php b/src/Adapter/Platform/AbstractPlatform.php index 77c5e5da..d6ac5dbb 100644 --- a/src/Adapter/Platform/AbstractPlatform.php +++ b/src/Adapter/Platform/AbstractPlatform.php @@ -29,7 +29,8 @@ abstract class AbstractPlatform implements PlatformInterface /** @var array{string, string} The opening and closing identifier quotes */ 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 a124e928..cb3c9276 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 { @@ -107,6 +130,13 @@ public function quoteIdentifierChainEscapesDoubleQuotes(): void static::assertSame('"a\\"b"."c"', $this->platform->quoteIdentifierChain(['a"b', 'c'])); } + #[DataProvider('identifiersWithEmbeddedQuotes')] + #[Test] + public function quoteIdentifierDoublesEmbeddedQuoteCharacters(string $identifier, string $expected): void + { + static::assertSame($expected, $this->platform->quoteIdentifier($identifier)); + } + #[Test] public function quoteIdentifierInFragment(): void { @@ -143,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] @@ -167,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] diff --git a/test/unit/Sql/IdentifierQuoterTest.php b/test/unit/Sql/IdentifierQuoterTest.php index b5ed8f07..d37b67bb 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/InsertColumnNameTest.php b/test/unit/Sql/InsertColumnNameTest.php index dec80599..76571cf8 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/PredicateIdentifierNameTest.php b/test/unit/Sql/PredicateIdentifierNameTest.php index 4516bf7c..b9c7073f 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"'], diff --git a/test/unit/Sql/SelectColumnNameTest.php b/test/unit/Sql/SelectColumnNameTest.php index f7f00b75..bb2fd1e0 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 1a9200ea..2ebb8b3c 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'], diff --git a/test/unit/Sql/UpdateColumnNameTest.php b/test/unit/Sql/UpdateColumnNameTest.php index 1f8cc8a6..fa87ec16 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\''], ]; }