Unverified Commit 54a83488 authored by Tobias Kristensen's avatar Tobias Kristensen Committed by Luís Cobucci

Default value declaration for immutable types

parent 1595a431
......@@ -2277,17 +2277,19 @@ abstract class AbstractPlatform
$default = empty($field['notnull']) ? ' DEFAULT NULL' : '';
if (isset($field['default'])) {
$default = " DEFAULT '".$field['default']."'";
$default = " DEFAULT '" . $field['default'] . "'";
if (isset($field['type'])) {
if (in_array((string) $field['type'], array("Integer", "BigInt", "SmallInt"))) {
$default = " DEFAULT ".$field['default'];
} elseif (in_array((string) $field['type'], array('DateTime', 'DateTimeTz')) && $field['default'] == $this->getCurrentTimestampSQL()) {
$default = " DEFAULT ".$this->getCurrentTimestampSQL();
} elseif ((string) $field['type'] == 'Time' && $field['default'] == $this->getCurrentTimeSQL()) {
$default = " DEFAULT ".$this->getCurrentTimeSQL();
} elseif ((string) $field['type'] == 'Date' && $field['default'] == $this->getCurrentDateSQL()) {
$default = " DEFAULT ".$this->getCurrentDateSQL();
} elseif ((string) $field['type'] == 'Boolean') {
$type = (string) $field['type'];
if (in_array($type, ["Integer", "BigInt", "SmallInt"], true)) {
$default = " DEFAULT " . $field['default'];
} elseif (in_array($type, ['DateTime', 'DateTimeTz', 'DateTimeImmutable', 'DateTimeTzImmutable'], true) && $field['default'] === $this->getCurrentTimestampSQL()) {
$default = " DEFAULT " . $this->getCurrentTimestampSQL();
} elseif (in_array($type, ['Time', 'TimeImmutable'], true) && $field['default'] === $this->getCurrentTimeSQL()) {
$default = " DEFAULT " . $this->getCurrentTimeSQL();
} elseif (in_array($type, ['Date', 'DateImmutable'], true) && $field['default'] === $this->getCurrentDateSQL()) {
$default = " DEFAULT " . $this->getCurrentDateSQL();
} elseif ($type === 'Boolean') {
$default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
}
}
......
......@@ -1554,15 +1554,17 @@ class SQLServerPlatform extends AbstractPlatform
return " DEFAULT '" . $field['default'] . "'";
}
if (in_array((string) $field['type'], array('Integer', 'BigInt', 'SmallInt'))) {
$type = (string) $field['type'];
if (in_array($type, ['Integer', 'BigInt', 'SmallInt'], true)) {
return " DEFAULT " . $field['default'];
}
if (in_array((string) $field['type'], array('DateTime', 'DateTimeTz')) && $field['default'] == $this->getCurrentTimestampSQL()) {
if (in_array($type, ['DateTime', 'DateTimeTz', 'DateTimeImmutable', 'DateTimeTzImmutable'], true) && $field['default'] === $this->getCurrentTimestampSQL()) {
return " DEFAULT " . $this->getCurrentTimestampSQL();
}
if ((string) $field['type'] == 'Boolean') {
if ($type === 'Boolean') {
return " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
}
......
......@@ -533,18 +533,22 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->assertEquals(" DEFAULT 'non_timestamp'", $this->_platform->getDefaultValueDeclarationSQL($field));
}
public function testGetDefaultValueDeclarationSQLDateTime()
/**
* @group 2859
*/
public function testGetDefaultValueDeclarationSQLDateTime() : void
{
// timestamps on datetime types should not be quoted
foreach (array('datetime', 'datetimetz') as $type) {
$field = array(
foreach (['datetime', 'datetimetz', 'datetime_immutable', 'datetimetz_immutable'] as $type) {
$field = [
'type' => Type::getType($type),
'default' => $this->_platform->getCurrentTimestampSQL()
);
$this->assertEquals(' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(), $this->_platform->getDefaultValueDeclarationSQL($field));
'default' => $this->_platform->getCurrentTimestampSQL(),
];
self::assertSame(
' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(),
$this->_platform->getDefaultValueDeclarationSQL($field)
);
}
}
......@@ -563,6 +567,25 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
}
}
/**
* @group 2859
*/
public function testGetDefaultValueDeclarationSQLForDateType() : void
{
$currentDateSql = $this->_platform->getCurrentDateSQL();
foreach (['date', 'date_immutable'] as $type) {
$field = [
'type' => Type::getType($type),
'default' => $currentDateSql,
];
self::assertSame(
' DEFAULT ' . $currentDateSql,
$this->_platform->getDefaultValueDeclarationSQL($field)
);
}
}
/**
* @group DBAL-45
*/
......
......@@ -1422,4 +1422,23 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
true
);
}
/**
* @group 2859
*/
public function testGetDefaultValueDeclarationSQLForDateType() : void
{
$currentDateSql = $this->_platform->getCurrentDateSQL();
foreach (['date', 'date_immutable'] as $type) {
$field = [
'type' => Type::getType($type),
'default' => $currentDateSql,
];
self::assertSame(
" DEFAULT '" . $currentDateSql . "'",
$this->_platform->getDefaultValueDeclarationSQL($field)
);
}
}
}
......@@ -13,10 +13,6 @@ class SQLServer2008PlatformTest extends AbstractSQLServerPlatformTestCase
public function testGeneratesTypeDeclarationForDateTimeTz()
{
$this->assertEquals(
'DATETIMEOFFSET(6)',
$this->_platform->getDateTimeTzTypeDeclarationSQL(
array())
);
$this->assertEquals('DATETIMEOFFSET(6)', $this->_platform->getDateTimeTzTypeDeclarationSQL([]));
}
}
......@@ -57,5 +57,4 @@ class SQLServerPlatformTest extends AbstractSQLServerPlatformTestCase
array('SELECT id_0, MIN(sclr_2) AS dctrn_minrownum FROM (SELECT c0_.id AS id_0, c0_.title AS title_1, ROW_NUMBER() OVER(ORDER BY c0_.title ASC) AS sclr_2 FROM TestTable c0_) dctrn_result GROUP BY id_0 ORDER BY dctrn_minrownum ASC', 30, null, 'WITH dctrn_cte AS (SELECT TOP 30 id_0, MIN(sclr_2) AS dctrn_minrownum FROM (SELECT c0_.id AS id_0, c0_.title AS title_1, ROW_NUMBER() OVER(ORDER BY c0_.title ASC) AS sclr_2 FROM TestTable c0_) dctrn_result GROUP BY id_0 ORDER BY dctrn_minrownum ASC) SELECT * FROM (SELECT *, ROW_NUMBER() OVER (ORDER BY (SELECT 0)) AS doctrine_rownum FROM dctrn_cte) AS doctrine_tbl WHERE doctrine_rownum BETWEEN 1 AND 30 ORDER BY doctrine_rownum ASC'),
);
}
}
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