Stop relying on Type::__toString

It is a somewhat fragile thing to do.
parent b66d7047
......@@ -1556,7 +1556,7 @@ abstract class AbstractPlatform
$columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption('version') : false;
$columnData['comment'] = $this->getColumnComment($column);
if (strtolower($columnData['type']) == "string" && $columnData['length'] === null) {
if ($columnData['type'] instanceof Types\StringType && $columnData['length'] === null) {
$columnData['length'] = 255;
}
......@@ -2279,15 +2279,17 @@ abstract class AbstractPlatform
if (isset($field['default'])) {
$default = " DEFAULT '".$field['default']."'";
if (isset($field['type'])) {
if (in_array((string) $field['type'], ["Integer", "BigInt", "SmallInt"])) {
$type = $field['type'];
if ($type instanceof Types\PhpIntegerMappingType) {
$default = " DEFAULT ".$field['default'];
} elseif (in_array((string) $field['type'], ['DateTime', 'DateTimeTz']) && $field['default'] == $this->getCurrentTimestampSQL()) {
} elseif ($type instanceof Types\PhpDateTimeMappingType
&& $field['default'] == $this->getCurrentTimestampSQL()) {
$default = " DEFAULT ".$this->getCurrentTimestampSQL();
} elseif ((string) $field['type'] == 'Time' && $field['default'] == $this->getCurrentTimeSQL()) {
} elseif ($type instanceof Types\TimeType && $field['default'] == $this->getCurrentTimeSQL()) {
$default = " DEFAULT ".$this->getCurrentTimeSQL();
} elseif ((string) $field['type'] == 'Date' && $field['default'] == $this->getCurrentDateSQL()) {
} elseif ($type instanceof Types\DateType && $field['default'] == $this->getCurrentDateSQL()) {
$default = " DEFAULT ".$this->getCurrentDateSQL();
} elseif ((string) $field['type'] == 'Boolean') {
} elseif ($type instanceof Types\BooleanType) {
$default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
}
}
......
......@@ -27,6 +27,7 @@ use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types;
/**
* The SQLServerPlatform provides the behavior, features and SQL dialect of the
......@@ -1554,15 +1555,18 @@ class SQLServerPlatform extends AbstractPlatform
return " DEFAULT '" . $field['default'] . "'";
}
if (in_array((string) $field['type'], ['Integer', 'BigInt', 'SmallInt'])) {
$type = $field['type'];
if ($type instanceof Types\PhpIntegerMappingType) {
return " DEFAULT " . $field['default'];
}
if (in_array((string) $field['type'], ['DateTime', 'DateTimeTz']) && $field['default'] == $this->getCurrentTimestampSQL()) {
if ($type instanceof Types\PhpDateTimeMappingType
&& $field['default'] == $this->getCurrentTimestampSQL()) {
return " DEFAULT " . $this->getCurrentTimestampSQL();
}
if ((string) $field['type'] == 'Boolean') {
if ($type instanceof Types\BooleanType) {
return " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
}
......
......@@ -27,6 +27,7 @@ use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types;
/**
* The SqlitePlatform class describes the specifics and dialects of the SQLite
......@@ -904,7 +905,7 @@ class SqlitePlatform extends AbstractPlatform
if ( ! $columnDiff->fromColumn instanceof Column ||
! $columnDiff->column instanceof Column ||
! $columnDiff->column->getAutoincrement() ||
! (string) $columnDiff->column->getType() === 'Integer'
! $columnDiff->column->getType() instanceof Types\IntegerType
) {
continue;
}
......@@ -915,9 +916,9 @@ class SqlitePlatform extends AbstractPlatform
continue;
}
$fromColumnType = (string) $columnDiff->fromColumn->getType();
$fromColumnType = $columnDiff->fromColumn->getType();
if ($fromColumnType === 'SmallInt' || $fromColumnType === 'BigInt') {
if ($fromColumnType instanceof Types\SmallIntType || $fromColumnType instanceof Types\BigIntType) {
unset($diff->changedColumns[$oldColumnName]);
}
}
......@@ -942,17 +943,17 @@ class SqlitePlatform extends AbstractPlatform
}
$field = array_merge(['unique' => null, 'autoincrement' => null, 'default' => null], $column->toArray());
$type = (string) $field['type'];
$type = $field['type'];
switch (true) {
case isset($field['columnDefinition']) || $field['autoincrement'] || $field['unique']:
case $type == 'DateTime' && $field['default'] == $this->getCurrentTimestampSQL():
case $type == 'Date' && $field['default'] == $this->getCurrentDateSQL():
case $type == 'Time' && $field['default'] == $this->getCurrentTimeSQL():
case $type instanceof Types\DateTimeType && $field['default'] == $this->getCurrentTimestampSQL():
case $type instanceof Types\DateType && $field['default'] == $this->getCurrentDateSQL():
case $type instanceof Types\TimeType && $field['default'] == $this->getCurrentTimeSQL():
return false;
}
$field['name'] = $column->getQuotedName($this);
if (strtolower($field['type']) == 'string' && $field['length'] === null) {
if ($type instanceof Types\StringType && $field['length'] === null) {
$field['length'] = 255;
}
......
......@@ -27,7 +27,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* @author robo
* @since 2.0
*/
class BigIntType extends Type
class BigIntType extends Type implements PhpIntegerMappingType
{
/**
* {@inheritdoc}
......
......@@ -26,7 +26,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*
* @since 2.0
*/
class DateTimeType extends Type
class DateTimeType extends Type implements PhpDateTimeMappingType
{
/**
* {@inheritdoc}
......
......@@ -44,7 +44,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class DateTimeTzType extends Type
class DateTimeTzType extends Type implements PhpDateTimeMappingType
{
/**
* {@inheritdoc}
......
......@@ -27,7 +27,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* @author Roman Borschel <roman@code-factory.org>
* @since 2.0
*/
class IntegerType extends Type
class IntegerType extends Type implements PhpIntegerMappingType
{
/**
* {@inheritdoc}
......
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Implementations should map a database type to a PHP DateTimeInterface instance.
* @internal
*/
interface PhpDateTimeMappingType
{
}
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* Implementations should map a database type to a PHP integer.
* @internal
*/
interface PhpIntegerMappingType
{
}
......@@ -26,7 +26,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
*
* @author robo
*/
class SmallIntType extends Type
class SmallIntType extends Type implements PhpIntegerMappingType
{
/**
* {@inheritdoc}
......
......@@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL\Functional\Ticket;
use Doctrine\DBAL\Schema\SQLServerSchemaManager;
use Doctrine\DBAL\Types\DecimalType;
/**
* @group DBAL-461
......@@ -31,6 +32,6 @@ class DBAL461Test extends \PHPUnit\Framework\TestCase
'comment' => null,
));
self::assertEquals('Decimal', (string)$column->getType());
$this->assertInstanceOf(DecimalType::class, $column->getType());
}
}
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