Skip to content

Commit

Permalink
Fixed handling special characters in default values on Oracle
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jan 3, 2018
1 parent e3ab127 commit b256ad6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
10 changes: 0 additions & 10 deletions lib/Doctrine/DBAL/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1166,14 +1166,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);
}
}
8 changes: 6 additions & 2 deletions lib/Doctrine/DBAL/Schema/OracleSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,12 @@ protected function _getPortableTableColumnDefinition($tableColumn)
}

if (null !== $tableColumn['data_default']) {
// Default values returned from database are enclosed in single quotes.
$tableColumn['data_default'] = trim($tableColumn['data_default'], "'");
// Default values returned from database are represented as literal expressions
$tableColumn['data_default'] = str_replace(
"''",
"'",
preg_replace('/^\'(.*)\'$/s', '$1', $tableColumn['data_default'])
);
}

$precision = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1410,19 +1410,20 @@ public function testCreateAndListSequences() : void
private function getEscapedLiterals() : array
{
return [
['An ASCII NUL (X\'00\')', "foo\\0bar"],
['Single quote, C-style', "foo\\'bar"],
['An ASCII NUL (X\'00\')', "foo\0bar"],
['Single quote, C-style', "foo'bar"],
['Single quote, doubled-style', "foo''bar"],
['Double quote, C-style', 'foo\\"bar'],
['Double quote, C-style', 'foo"bar'],
['Double quote, double-style', 'foo""bar'],
['Backspace', 'foo\\bbar'],
['New-line', 'foo\\nbar'],
['Carriage return', 'foo\\rbar'],
['Tab', 'foo\\tbar'],
['ASCII 26 (Control+Z)', 'foo\\Zbar'],
['Backslash (\)', 'foo\\\\bar'],
['Percent (%)', 'foo\\%bar'],
['Underscore (_)', 'foo\\_bar'],
['Backspace', "foo\x08bar"],
['New-line', "foo\nbar"],
['Carriage return', "foo\rbar"],
['Tab', "foo\tbar"],
['ASCII 26 (Control+Z)', "foo\x1abar"],
['Backslash (\)', 'foo\\bar'],
['Double backslash', 'foo\\\\bar'],
['Percent (%)', 'foo%bar'],
['Underscore (_)', 'foo_bar'],
];
}

Expand Down Expand Up @@ -1452,9 +1453,10 @@ public function testEscapedDefaultValueCanBeInserted() : void
$this->createTableForDefaultValues();

$this->_conn->insert('string_escaped_default_value', ['def_foo' => 'foo']);
$row = $this->_conn->fetchAssoc('SELECT * FROM string_escaped_default_value');

foreach ($this->getEscapedLiterals() as $i => $literal) {
self::assertSame($literal[1], $row['field' . $i], 'inserted default value should be the configured default value for: ' . $literal[0]);
$value = $this->_conn->fetchColumn('SELECT field' . $i . ' FROM string_escaped_default_value');
self::assertSame($literal[1], $value, 'inserted default value should be the configured default value for: ' . $literal[0]);
}
}
}
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 @@ -832,46 +832,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 b256ad6

Please sign in to comment.