From 8641273bfc1fe0b6e0aadabdf6a8403219f6bb29 Mon Sep 17 00:00:00 2001 From: Simon Mundy Date: Tue, 17 Nov 2026 14:37:31 +1100 Subject: [PATCH] Double a quote character inside an identifier instead of replacing it AdapterPlatform set $quoteIdentifierTo to a single quote, so quoteIdentifier('my"col') rendered "my'col" and named a different column. SQLite escapes an embedded identifier quote by doubling it, so the value is now '""' and the same name renders "my""col". --- src/AdapterPlatform.php | 6 ++---- test/unit/AdapterPlatformTest.php | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/AdapterPlatform.php b/src/AdapterPlatform.php index 10eaf9d..011edf7 100644 --- a/src/AdapterPlatform.php +++ b/src/AdapterPlatform.php @@ -21,10 +21,8 @@ class AdapterPlatform extends AbstractPlatform protected PdoDriverInterface|PDO|null $resource = null; - /** - * {@inheritDoc} - */ - protected string $quoteIdentifierTo = '\''; + /** Replacement for a quote character inside an identifier; SQLite doubles it */ + protected string $quoteIdentifierTo = '""'; public function __construct( protected readonly PdoDriverInterface|PDO|null $driver = null, diff --git a/test/unit/AdapterPlatformTest.php b/test/unit/AdapterPlatformTest.php index d23469d..eb3d4cb 100644 --- a/test/unit/AdapterPlatformTest.php +++ b/test/unit/AdapterPlatformTest.php @@ -10,6 +10,8 @@ use PhpDb\Sqlite\AdapterPlatform; use PhpDb\Sqlite\Sql\Platform as SqlPlatformDecorator; use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; #[CoversClass(AdapterPlatform::class)] @@ -17,6 +19,26 @@ final class AdapterPlatformTest extends TestCase { private AdapterPlatform $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 quoteIdentifierDoublesEmbeddedQuoteCharacters(string $identifier, string $expected): void + { + static::assertSame($expected, $this->platform->quoteIdentifier($identifier)); + } + public function testConstructWithPdo(): void { $pdoMock = $this->createMock(PDO::class);