From aaf2b6312f32af913a68415150c2b799bf9d77bb Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Wed, 24 Oct 2018 06:22:59 +0300 Subject: [PATCH] Fixed quoting of string literals containing backslash --- .../DBAL/Platforms/OraclePlatform.php | 11 ------- .../DBAL/Platforms/PostgreSqlPlatform.php | 11 ------- .../DBAL/Functional/Platform/QuotingTest.php | 32 +++++++++++++++++++ .../AbstractPostgreSqlPlatformTestCase.php | 16 +++++----- .../DBAL/Platforms/OraclePlatformTest.php | 12 +++---- 5 files changed, 46 insertions(+), 36 deletions(-) create mode 100644 tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php diff --git a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php index bc05b241098..1d249e9aa10 100644 --- a/lib/Doctrine/DBAL/Platforms/OraclePlatform.php +++ b/lib/Doctrine/DBAL/Platforms/OraclePlatform.php @@ -21,7 +21,6 @@ use function implode; use function preg_match; use function sprintf; -use function str_replace; use function strlen; use function strpos; use function strtoupper; @@ -1175,14 +1174,4 @@ public function getBlobTypeDeclarationSQL(array $field) { return 'BLOB'; } - - /** - * {@inheritdoc} - */ - public function quoteStringLiteral($str) - { - $str = str_replace('\\', '\\\\', $str); // Oracle requires backslashes to be escaped aswell. - - return parent::quoteStringLiteral($str); - } } diff --git a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php index 242c0fd4f28..cb4603f5199 100644 --- a/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php +++ b/lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php @@ -28,7 +28,6 @@ use function is_numeric; use function is_string; use function sprintf; -use function str_replace; use function strpos; use function strtolower; use function trim; @@ -1199,16 +1198,6 @@ public function getBlobTypeDeclarationSQL(array $field) return 'BYTEA'; } - /** - * {@inheritdoc} - */ - public function quoteStringLiteral($str) - { - $str = str_replace('\\', '\\\\', $str); // PostgreSQL requires backslashes to be escaped aswell. - - return parent::quoteStringLiteral($str); - } - /** * {@inheritdoc} */ diff --git a/tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php b/tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php new file mode 100644 index 00000000000..3a1ae7c8440 --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php @@ -0,0 +1,32 @@ +connection->getDatabasePlatform(); + $query = $platform->getDummySelectSQL( + $platform->quoteStringLiteral($string) + ); + + self::assertSame($string, $this->connection->fetchColumn($query)); + } + + /** + * @return mixed[][] + */ + public static function stringLiteralProvider() : iterable + { + return [ + 'backslash' => ['\\'], + 'single-quote' => ["'"], + ]; + } +} diff --git a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php index b7312f376ad..2624de8f71c 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/AbstractPostgreSqlPlatformTestCase.php @@ -952,7 +952,7 @@ public function testReturnsCloseActiveDatabaseConnectionsSQL() */ public function testQuotesTableNameInListTableForeignKeysSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true); } /** @@ -961,7 +961,7 @@ public function testQuotesTableNameInListTableForeignKeysSQL() public function testQuotesSchemaNameInListTableForeignKeysSQL() { self::assertContains( - "'Foo''Bar\\\\'", + "'Foo''Bar\\'", $this->platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"), '', true @@ -973,7 +973,7 @@ public function testQuotesSchemaNameInListTableForeignKeysSQL() */ public function testQuotesTableNameInListTableConstraintsSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true); } /** @@ -981,7 +981,7 @@ public function testQuotesTableNameInListTableConstraintsSQL() */ public function testQuotesTableNameInListTableIndexesSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\"), '', true); } /** @@ -990,7 +990,7 @@ public function testQuotesTableNameInListTableIndexesSQL() public function testQuotesSchemaNameInListTableIndexesSQL() { self::assertContains( - "'Foo''Bar\\\\'", + "'Foo''Bar\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"), '', true @@ -1002,7 +1002,7 @@ public function testQuotesSchemaNameInListTableIndexesSQL() */ public function testQuotesTableNameInListTableColumnsSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableColumnsSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableColumnsSQL("Foo'Bar\\"), '', true); } /** @@ -1011,7 +1011,7 @@ public function testQuotesTableNameInListTableColumnsSQL() public function testQuotesSchemaNameInListTableColumnsSQL() { self::assertContains( - "'Foo''Bar\\\\'", + "'Foo''Bar\\'", $this->platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"), '', true @@ -1024,7 +1024,7 @@ public function testQuotesSchemaNameInListTableColumnsSQL() public function testQuotesDatabaseNameInCloseActiveDatabaseConnectionsSQL() { self::assertContains( - "'Foo''Bar\\\\'", + "'Foo''Bar\\'", $this->platform->getCloseActiveDatabaseConnectionsSQL("Foo'Bar\\"), '', true diff --git a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php index 051b3e68dd9..e8f8f0cdd7f 100644 --- a/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php +++ b/tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php @@ -875,7 +875,7 @@ protected function getGeneratesAlterTableRenameIndexUsedByForeignKeySQL() */ public function testQuotesDatabaseNameInListSequencesSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListSequencesSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListSequencesSQL("Foo'Bar\\"), '', true); } /** @@ -883,7 +883,7 @@ public function testQuotesDatabaseNameInListSequencesSQL() */ public function testQuotesTableNameInListTableIndexesSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\"), '', true); } /** @@ -891,7 +891,7 @@ public function testQuotesTableNameInListTableIndexesSQL() */ public function testQuotesTableNameInListTableForeignKeysSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true); } /** @@ -899,7 +899,7 @@ public function testQuotesTableNameInListTableForeignKeysSQL() */ public function testQuotesTableNameInListTableConstraintsSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true); } /** @@ -907,7 +907,7 @@ public function testQuotesTableNameInListTableConstraintsSQL() */ public function testQuotesTableNameInListTableColumnsSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableColumnsSQL("Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableColumnsSQL("Foo'Bar\\"), '', true); } /** @@ -915,6 +915,6 @@ public function testQuotesTableNameInListTableColumnsSQL() */ public function testQuotesDatabaseNameInListTableColumnsSQL() { - self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\"), '', true); + self::assertContains("'Foo''Bar\\'", $this->platform->getListTableColumnsSQL('foo_table', "Foo'Bar\\"), '', true); } }