Skip to content

Commit

Permalink
Merge pull request #3330 from morozov/issues/3328
Browse files Browse the repository at this point in the history
Fixed quoting of string literals containing backslash
  • Loading branch information
morozov authored Nov 12, 2018
2 parents 822d970 + aaf2b63 commit ff6cbdb
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
11 changes: 0 additions & 11 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
11 changes: 0 additions & 11 deletions lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Platform/QuotingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Doctrine\Tests\DBAL\Functional\Platform;

use Doctrine\Tests\DbalFunctionalTestCase;

class QuotingTest extends DbalFunctionalTestCase
{
/**
* @dataProvider stringLiteralProvider
*/
public function testQuoteStringLiteral(string $string) : void
{
$platform = $this->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' => ["'"],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -961,7 +961,7 @@ public function testQuotesTableNameInListTableForeignKeysSQL()
public function testQuotesSchemaNameInListTableForeignKeysSQL()
{
self::assertContains(
"'Foo''Bar\\\\'",
"'Foo''Bar\\'",
$this->platform->getListTableForeignKeysSQL("Foo'Bar\\.baz_table"),
'',
true
Expand All @@ -973,15 +973,15 @@ 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);
}

/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
self::assertContains("'Foo''Bar\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
}

/**
Expand All @@ -990,7 +990,7 @@ public function testQuotesTableNameInListTableIndexesSQL()
public function testQuotesSchemaNameInListTableIndexesSQL()
{
self::assertContains(
"'Foo''Bar\\\\'",
"'Foo''Bar\\'",
$this->platform->getListTableIndexesSQL("Foo'Bar\\.baz_table"),
'',
true
Expand All @@ -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);
}

/**
Expand All @@ -1011,7 +1011,7 @@ public function testQuotesTableNameInListTableColumnsSQL()
public function testQuotesSchemaNameInListTableColumnsSQL()
{
self::assertContains(
"'Foo''Bar\\\\'",
"'Foo''Bar\\'",
$this->platform->getListTableColumnsSQL("Foo'Bar\\.baz_table"),
'',
true
Expand All @@ -1024,7 +1024,7 @@ public function testQuotesSchemaNameInListTableColumnsSQL()
public function testQuotesDatabaseNameInCloseActiveDatabaseConnectionsSQL()
{
self::assertContains(
"'Foo''Bar\\\\'",
"'Foo''Bar\\'",
$this->platform->getCloseActiveDatabaseConnectionsSQL("Foo'Bar\\"),
'',
true
Expand Down
12 changes: 6 additions & 6 deletions tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,46 +875,46 @@ 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);
}

/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableIndexesSQL()
{
self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
self::assertContains("'Foo''Bar\\'", $this->platform->getListTableIndexesSQL("Foo'Bar\\"), '', true);
}

/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableForeignKeysSQL()
{
self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
self::assertContains("'Foo''Bar\\'", $this->platform->getListTableForeignKeysSQL("Foo'Bar\\"), '', true);
}

/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableConstraintsSQL()
{
self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true);
self::assertContains("'Foo''Bar\\'", $this->platform->getListTableConstraintsSQL("Foo'Bar\\"), '', true);
}

/**
* @group DBAL-2436
*/
public function testQuotesTableNameInListTableColumnsSQL()
{
self::assertContains("'Foo''Bar\\\\'", $this->platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
self::assertContains("'Foo''Bar\\'", $this->platform->getListTableColumnsSQL("Foo'Bar\\"), '', true);
}

/**
* @group DBAL-2436
*/
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);
}
}

0 comments on commit ff6cbdb

Please sign in to comment.