Unverified Commit 40a87b7f authored by Tobias Kristensen's avatar Tobias Kristensen Committed by Luís Cobucci

Default value declaration for immutable types

parent 39cb21be
......@@ -2277,18 +2277,17 @@ 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'])) {
$type = $field['type'];
if ($type instanceof Types\PhpIntegerMappingType) {
$default = " DEFAULT ".$field['default'];
} elseif ($type instanceof Types\PhpDateTimeMappingType
&& $field['default'] == $this->getCurrentTimestampSQL()) {
$default = " DEFAULT ".$this->getCurrentTimestampSQL();
} elseif ($type instanceof Types\TimeType && $field['default'] == $this->getCurrentTimeSQL()) {
$default = " DEFAULT ".$this->getCurrentTimeSQL();
} elseif ($type instanceof Types\DateType && $field['default'] == $this->getCurrentDateSQL()) {
$default = " DEFAULT ".$this->getCurrentDateSQL();
$default = " DEFAULT " . $field['default'];
} elseif ($type instanceof Types\PhpDateTimeMappingType && $field['default'] === $this->getCurrentTimestampSQL()) {
$default = " DEFAULT " . $this->getCurrentTimestampSQL();
} elseif ($type instanceof Types\TimeType && $field['default'] === $this->getCurrentTimeSQL()) {
$default = " DEFAULT " . $this->getCurrentTimeSQL();
} elseif ($type instanceof Types\DateType && $field['default'] === $this->getCurrentDateSQL()) {
$default = " DEFAULT " . $this->getCurrentDateSQL();
} elseif ($type instanceof Types\BooleanType) {
$default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
}
......
......@@ -1561,8 +1561,7 @@ class SQLServerPlatform extends AbstractPlatform
return " DEFAULT " . $field['default'];
}
if ($type instanceof Types\PhpDateTimeMappingType
&& $field['default'] == $this->getCurrentTimestampSQL()) {
if ($type instanceof Types\PhpDateTimeMappingType && $field['default'] === $this->getCurrentTimestampSQL()) {
return " DEFAULT " . $this->getCurrentTimestampSQL();
}
......
......@@ -533,18 +533,22 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
self::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) {
foreach (['datetime', 'datetimetz', 'datetime_immutable', 'datetimetz_immutable'] as $type) {
$field = [
'type' => Type::getType($type),
'default' => $this->_platform->getCurrentTimestampSQL(),
];
$field = array(
'type' => Type::getType($type),
'default' => $this->_platform->getCurrentTimestampSQL()
self::assertSame(
' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(),
$this->_platform->getDefaultValueDeclarationSQL($field)
);
self::assertEquals(' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(), $this->_platform->getDefaultValueDeclarationSQL($field));
}
}
......@@ -563,18 +567,23 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
}
}
/**
* @group 2859
*/
public function testGetDefaultValueDeclarationSQLForDateType() : void
{
$currentDateSql = $this->_platform->getCurrentDateSQL();
$field = [
'type' => Type::getType('date'),
'default' => $currentDateSql,
];
foreach (['date', 'date_immutable'] as $type) {
$field = [
'type' => Type::getType($type),
'default' => $currentDateSql,
];
self::assertSame(
' DEFAULT ' . $currentDateSql,
$this->_platform->getDefaultValueDeclarationSQL($field)
);
self::assertSame(
' DEFAULT ' . $currentDateSql,
$this->_platform->getDefaultValueDeclarationSQL($field)
);
}
}
/**
......
......@@ -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)
);
}
}
}
......@@ -3,7 +3,6 @@
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\SQLServer2008Platform;
use Doctrine\DBAL\Types\Type;
class SQLServer2008PlatformTest extends AbstractSQLServerPlatformTestCase
{
......@@ -14,24 +13,6 @@ class SQLServer2008PlatformTest extends AbstractSQLServerPlatformTestCase
public function testGeneratesTypeDeclarationForDateTimeTz()
{
self::assertEquals(
'DATETIMEOFFSET(6)',
$this->_platform->getDateTimeTzTypeDeclarationSQL(
array())
);
}
public function testGetDefaultValueDeclarationSQLForDateType() : void
{
$currentDateSql = $this->_platform->getCurrentDateSQL();
$field = [
'type' => Type::getType('date'),
'default' => $currentDateSql,
];
self::assertSame(
" DEFAULT '" . $currentDateSql . "'",
$this->_platform->getDefaultValueDeclarationSQL($field)
);
self::assertEquals('DATETIMEOFFSET(6)', $this->_platform->getDateTimeTzTypeDeclarationSQL([]));
}
}
......@@ -4,7 +4,6 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Types\Type;
class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase
{
......@@ -372,18 +371,4 @@ class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase
$sql = $this->_platform->modifyLimitQuery($querySql, 10);
self::assertEquals($expectedSql, $sql);
}
public function testGetDefaultValueDeclarationSQLForDateType() : void
{
$currentDateSql = $this->_platform->getCurrentDateSQL();
$field = [
'type' => Type::getType('date'),
'default' => $currentDateSql,
];
self::assertSame(
" DEFAULT '" . $currentDateSql . "'",
$this->_platform->getDefaultValueDeclarationSQL($field)
);
}
}
......@@ -4,7 +4,6 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Types\Type;
class SQLServerPlatformTest extends AbstractSQLServerPlatformTestCase
{
......@@ -58,18 +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'),
);
}
public function testGetDefaultValueDeclarationSQLForDateType() : void
{
$currentDateSql = $this->_platform->getCurrentDateSQL();
$field = [
'type' => Type::getType('date'),
'default' => $currentDateSql,
];
self::assertSame(
" DEFAULT '" . $currentDateSql . "'",
$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