Functional test of escaped default values via INSERT and SELECT

parent 7e65c84c
...@@ -2306,7 +2306,7 @@ abstract class AbstractPlatform ...@@ -2306,7 +2306,7 @@ abstract class AbstractPlatform
return " DEFAULT '" . $this->convertBooleans($default) . "'"; return " DEFAULT '" . $this->convertBooleans($default) . "'";
} }
return " DEFAULT " . $this->quoteStringLiteral($default); return " DEFAULT " . $this->quoteDefaultStringLiteral($default);
} }
/** /**
...@@ -3570,4 +3570,16 @@ abstract class AbstractPlatform ...@@ -3570,4 +3570,16 @@ abstract class AbstractPlatform
{ {
return "'"; return "'";
} }
/**
* Quote a literal string for usage as DEFAULT value.
*
* @param string $str
*
* @return string
*/
protected function quoteDefaultStringLiteral(string $str) : string
{
return $this->quoteStringLiteral($str);
}
} }
...@@ -1236,4 +1236,12 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -1236,4 +1236,12 @@ class PostgreSqlPlatform extends AbstractPlatform
{ {
return $type instanceof IntegerType || $type instanceof BigIntType; return $type instanceof IntegerType || $type instanceof BigIntType;
} }
/**
* {@inheritDoc}
*/
protected function quoteDefaultStringLiteral(string $str) : string
{
return parent::quoteStringLiteral($str);
}
} }
...@@ -454,21 +454,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -454,21 +454,7 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$platform = $this->_sm->getDatabasePlatform(); $platform = $this->_sm->getDatabasePlatform();
$this->_conn->query('DROP TABLE IF EXISTS test_column_defaults_with_create'); $this->_conn->query('DROP TABLE IF EXISTS test_column_defaults_with_create');
$escapeSequences = [ $default = implode('+', $this->getEscapedLiterals());
"\\0", // An ASCII NUL (X'00') character
"\\'", "''", // Single quote
'\\"', '""', // Double quote
'\\b', // A backspace character
'\\n', // A new-line character
'\\r', // A carriage return character
'\\t', // A tab character
'\\Z', // ASCII 26 (Control+Z)
'\\\\', // A backslash (\) character
'\\%', // A percent (%) character
'\\_', // An underscore (_) character
];
$default = implode('+', $escapeSequences);
$sql = "CREATE TABLE test_column_defaults_with_create( $sql = "CREATE TABLE test_column_defaults_with_create(
col1 VARCHAR(255) NULL DEFAULT {$platform->quoteStringLiteral($default)} col1 VARCHAR(255) NULL DEFAULT {$platform->quoteStringLiteral($default)}
...@@ -477,4 +463,23 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase ...@@ -477,4 +463,23 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$onlineTable = $this->_sm->listTableDetails("test_column_defaults_with_create"); $onlineTable = $this->_sm->listTableDetails("test_column_defaults_with_create");
self::assertSame($default, $onlineTable->getColumn('col1')->getDefault()); self::assertSame($default, $onlineTable->getColumn('col1')->getDefault());
} }
protected function getEscapedLiterals() : array
{
return [
"\\0", // An ASCII NUL (X'00') character
"\\'", // Single quote
"''", // Single quote
'\\"', // Double quote
'""', // Double quote
'\\b', // A backspace character
'\\n', // A new-line character
'\\r', // A carriage return character
'\\t', // A tab character
'\\Z', // ASCII 26 (Control+Z)
'\\\\', // A backslash (\) character
'\\%', // A percent (%) character
'\\_', // An underscore (_) character
];
}
} }
...@@ -1398,18 +1398,38 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest ...@@ -1398,18 +1398,38 @@ class SchemaManagerFunctionalTestCase extends \Doctrine\Tests\DbalFunctionalTest
self::assertEquals($sequence2InitialValue, $actualSequence2->getInitialValue()); self::assertEquals($sequence2InitialValue, $actualSequence2->getInitialValue());
} }
public function testEscapedDefaultValueMustBePreserved() /**
* Returns literals that are platform specific escaped.
*
* @return array
*/
protected function getEscapedLiterals() : array
{ {
$value = "a\\0b\\'c\"d\te\\Zf\\\\g''h"; return [
"\\'", // A single quote
'\\"', // A double quote
'\\n', // A new-line character
'\\r', // A carriage return character
];
}
public function testEscapedDefaultValueMustBePreserved() : void
{
$value = implode('+', $this->getEscapedLiterals());
$table = new Table('string_escaped_default_value'); $table = new Table('string_escaped_default_value');
$table->addColumn('def_string', 'string', array('default' => $value)); $table->addColumn('def_string', 'string', array('default' => $value));
$table->addColumn('def_foo', 'string');
$this->_sm->dropAndCreateTable($table); $this->_sm->dropAndCreateTable($table);
$onlineTable = $this->_sm->listTableDetails('string_escaped_default_value'); $onlineTable = $this->_sm->listTableDetails('string_escaped_default_value');
$this->assertSame($value, $onlineTable->getColumn('def_string')->getDefault()); self::assertSame($value, $onlineTable->getColumn('def_string')->getDefault(), 'should be able introspect the value of default');
$comparator = new Comparator(); $comparator = new Comparator();
$this->assertFalse($comparator->diffTable($table, $onlineTable)); self::assertFalse($comparator->diffTable($table, $onlineTable), 'introspected schema should have no differences');
$this->_conn->insert('string_escaped_default_value', array('def_foo' => 'foo'));
$row = $this->_conn->fetchAssoc('SELECT def_string FROM string_escaped_default_value');
self::assertSame($value, $row['def_string'], 'inserted default value should be the configured default value');
} }
} }
...@@ -977,7 +977,7 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -977,7 +977,7 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
); );
} }
public function testGetDefaultValueDeclarationSQLEscaped() public function testGetDefaultValueDeclarationSQLEscaped() : void
{ {
// string must be escaped // string must be escaped
$field = [ $field = [
...@@ -985,6 +985,6 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -985,6 +985,6 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
'default' => 'Foo\\Bar' 'default' => 'Foo\\Bar'
]; ];
self::assertSame(" DEFAULT 'Foo\\\\Bar'", $this->_platform->getDefaultValueDeclarationSQL($field)); self::assertSame(" DEFAULT 'Foo\\Bar'", $this->_platform->getDefaultValueDeclarationSQL($field));
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment