Unverified Commit e828bca2 authored by Luís Cobucci's avatar Luís Cobucci Committed by GitHub

Merge pull request #2861 from amsiq/2.6

Default value declaration for immutable types

Fixes: https://github.com/doctrine/dbal/issues/2859
parents 39cb21be 9bbf3d0f
...@@ -2274,28 +2274,39 @@ abstract class AbstractPlatform ...@@ -2274,28 +2274,39 @@ abstract class AbstractPlatform
*/ */
public function getDefaultValueDeclarationSQL($field) public function getDefaultValueDeclarationSQL($field)
{ {
$default = empty($field['notnull']) ? ' DEFAULT NULL' : ''; if ( ! isset($field['default'])) {
return empty($field['notnull']) ? ' DEFAULT NULL' : '';
}
$default = $field['default'];
if ( ! isset($field['type'])) {
return " DEFAULT '" . $default . "'";
}
if (isset($field['default'])) {
$default = " DEFAULT '".$field['default']."'";
if (isset($field['type'])) {
$type = $field['type']; $type = $field['type'];
if ($type instanceof Types\PhpIntegerMappingType) { if ($type instanceof Types\PhpIntegerMappingType) {
$default = " DEFAULT ".$field['default']; return " DEFAULT " . $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']) . "'";
} }
if ($type instanceof Types\PhpDateTimeMappingType && $default === $this->getCurrentTimestampSQL()) {
return " DEFAULT " . $this->getCurrentTimestampSQL();
} }
if ($type instanceof Types\TimeType && $default === $this->getCurrentTimeSQL()) {
return " DEFAULT " . $this->getCurrentTimeSQL();
}
if ($type instanceof Types\DateType && $default === $this->getCurrentDateSQL()) {
return " DEFAULT " . $this->getCurrentDateSQL();
}
if ($type instanceof Types\BooleanType) {
return " DEFAULT '" . $this->convertBooleans($default) . "'";
} }
return $default; return " DEFAULT '" . $default . "'";
} }
/** /**
......
...@@ -1561,8 +1561,7 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -1561,8 +1561,7 @@ class SQLServerPlatform extends AbstractPlatform
return " DEFAULT " . $field['default']; return " DEFAULT " . $field['default'];
} }
if ($type instanceof Types\PhpDateTimeMappingType if ($type instanceof Types\PhpDateTimeMappingType && $field['default'] === $this->getCurrentTimestampSQL()) {
&& $field['default'] == $this->getCurrentTimestampSQL()) {
return " DEFAULT " . $this->getCurrentTimestampSQL(); return " DEFAULT " . $this->getCurrentTimestampSQL();
} }
......
...@@ -533,18 +533,22 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -533,18 +533,22 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
self::assertEquals(" DEFAULT 'non_timestamp'", $this->_platform->getDefaultValueDeclarationSQL($field)); 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 // timestamps on datetime types should not be quoted
foreach (array('datetime', 'datetimetz') as $type) { foreach (['datetime', 'datetimetz', 'datetime_immutable', 'datetimetz_immutable'] as $type) {
$field = [
$field = array(
'type' => Type::getType($type), 'type' => Type::getType($type),
'default' => $this->_platform->getCurrentTimestampSQL() 'default' => $this->_platform->getCurrentTimestampSQL(),
); ];
self::assertEquals(' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(), $this->_platform->getDefaultValueDeclarationSQL($field));
self::assertSame(
' DEFAULT ' . $this->_platform->getCurrentTimestampSQL(),
$this->_platform->getDefaultValueDeclarationSQL($field)
);
} }
} }
...@@ -563,11 +567,15 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -563,11 +567,15 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
} }
} }
/**
* @group 2859
*/
public function testGetDefaultValueDeclarationSQLForDateType() : void public function testGetDefaultValueDeclarationSQLForDateType() : void
{ {
$currentDateSql = $this->_platform->getCurrentDateSQL(); $currentDateSql = $this->_platform->getCurrentDateSQL();
foreach (['date', 'date_immutable'] as $type) {
$field = [ $field = [
'type' => Type::getType('date'), 'type' => Type::getType($type),
'default' => $currentDateSql, 'default' => $currentDateSql,
]; ];
...@@ -576,6 +584,7 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase ...@@ -576,6 +584,7 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->_platform->getDefaultValueDeclarationSQL($field) $this->_platform->getDefaultValueDeclarationSQL($field)
); );
} }
}
/** /**
* @group DBAL-45 * @group DBAL-45
......
...@@ -1422,4 +1422,23 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas ...@@ -1422,4 +1422,23 @@ abstract class AbstractSQLServerPlatformTestCase extends AbstractPlatformTestCas
true 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 @@ ...@@ -3,7 +3,6 @@
namespace Doctrine\Tests\DBAL\Platforms; namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\SQLServer2008Platform; use Doctrine\DBAL\Platforms\SQLServer2008Platform;
use Doctrine\DBAL\Types\Type;
class SQLServer2008PlatformTest extends AbstractSQLServerPlatformTestCase class SQLServer2008PlatformTest extends AbstractSQLServerPlatformTestCase
{ {
...@@ -14,24 +13,6 @@ class SQLServer2008PlatformTest extends AbstractSQLServerPlatformTestCase ...@@ -14,24 +13,6 @@ class SQLServer2008PlatformTest extends AbstractSQLServerPlatformTestCase
public function testGeneratesTypeDeclarationForDateTimeTz() public function testGeneratesTypeDeclarationForDateTimeTz()
{ {
self::assertEquals( self::assertEquals('DATETIMEOFFSET(6)', $this->_platform->getDateTimeTzTypeDeclarationSQL([]));
'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)
);
} }
} }
...@@ -4,7 +4,6 @@ namespace Doctrine\Tests\DBAL\Platforms; ...@@ -4,7 +4,6 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Types\Type;
class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase
{ {
...@@ -372,18 +371,4 @@ class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase ...@@ -372,18 +371,4 @@ class SQLServer2012PlatformTest extends AbstractSQLServerPlatformTestCase
$sql = $this->_platform->modifyLimitQuery($querySql, 10); $sql = $this->_platform->modifyLimitQuery($querySql, 10);
self::assertEquals($expectedSql, $sql); 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; ...@@ -4,7 +4,6 @@ namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\LockMode; use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\SQLServerPlatform; use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Types\Type;
class SQLServerPlatformTest extends AbstractSQLServerPlatformTestCase class SQLServerPlatformTest extends AbstractSQLServerPlatformTestCase
{ {
...@@ -58,18 +57,4 @@ 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'), 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