Skip to content
Merged
3 changes: 2 additions & 1 deletion src/Adapter/Platform/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
34 changes: 32 additions & 2 deletions test/unit/Adapter/Platform/Sql92Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +41,28 @@ final class Sql92Test extends TestCase
{
protected Sql92 $platform;

/**
* @return array<string, array{string, string}>
*/
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
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/IdentifierQuoterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"'],
];
}

Expand Down
4 changes: 2 additions & 2 deletions test/unit/Sql/InsertColumnNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -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\')'],
];
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/Sql/PredicateIdentifierNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"'],
Expand Down
10 changes: 5 additions & 5 deletions test/unit/Sql/SelectColumnNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)'],
Expand All @@ -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"'],
Expand Down Expand Up @@ -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'],
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Sql/SelectOrderNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down
4 changes: 2 additions & 2 deletions test/unit/Sql/UpdateColumnNameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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\''],
];
}
Expand Down
Loading