Commit ba421d1d authored by Marco Pivetta's avatar Marco Pivetta Committed by GitHub

Merge pull request #2835 from greg0ire/avoid_type_to_string

Stop relying on Type::__toString
parents 6e3afd5f 9e75bb0c
...@@ -19,29 +19,29 @@ ...@@ -19,29 +19,29 @@
namespace Doctrine\DBAL\Platforms; namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException; use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Identifier; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Types; use Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Schema\Constraint; use Doctrine\DBAL\Schema\Constraint;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\Sequence;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Types;
use Doctrine\DBAL\Schema\ColumnDiff;
use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Events;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableAddColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRemoveColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;
use Doctrine\DBAL\Event\SchemaAlterTableRenameColumnEventArgs;
/** /**
* Base class for all DatabasePlatforms. The DatabasePlatforms are the central * Base class for all DatabasePlatforms. The DatabasePlatforms are the central
...@@ -1556,7 +1556,7 @@ abstract class AbstractPlatform ...@@ -1556,7 +1556,7 @@ abstract class AbstractPlatform
$columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption('version') : false; $columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption('version') : false;
$columnData['comment'] = $this->getColumnComment($column); $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; $columnData['length'] = 255;
} }
...@@ -2279,15 +2279,17 @@ abstract class AbstractPlatform ...@@ -2279,15 +2279,17 @@ abstract class AbstractPlatform
if (isset($field['default'])) { if (isset($field['default'])) {
$default = " DEFAULT '".$field['default']."'"; $default = " DEFAULT '".$field['default']."'";
if (isset($field['type'])) { 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']; $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(); $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(); $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(); $default = " DEFAULT ".$this->getCurrentDateSQL();
} elseif ((string) $field['type'] == 'Boolean') { } elseif ($type instanceof Types\BooleanType) {
$default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'"; $default = " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
} }
} }
......
...@@ -27,6 +27,7 @@ use Doctrine\DBAL\Schema\Identifier; ...@@ -27,6 +27,7 @@ use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types;
/** /**
* The SQLServerPlatform provides the behavior, features and SQL dialect of the * The SQLServerPlatform provides the behavior, features and SQL dialect of the
...@@ -1554,15 +1555,18 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -1554,15 +1555,18 @@ class SQLServerPlatform extends AbstractPlatform
return " DEFAULT '" . $field['default'] . "'"; 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']; 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(); return " DEFAULT " . $this->getCurrentTimestampSQL();
} }
if ((string) $field['type'] == 'Boolean') { if ($type instanceof Types\BooleanType) {
return " DEFAULT '" . $this->convertBooleans($field['default']) . "'"; return " DEFAULT '" . $this->convertBooleans($field['default']) . "'";
} }
......
...@@ -27,6 +27,7 @@ use Doctrine\DBAL\Schema\Identifier; ...@@ -27,6 +27,7 @@ use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table; use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Types;
/** /**
* The SqlitePlatform class describes the specifics and dialects of the SQLite * The SqlitePlatform class describes the specifics and dialects of the SQLite
...@@ -904,7 +905,7 @@ class SqlitePlatform extends AbstractPlatform ...@@ -904,7 +905,7 @@ class SqlitePlatform extends AbstractPlatform
if ( ! $columnDiff->fromColumn instanceof Column || if ( ! $columnDiff->fromColumn instanceof Column ||
! $columnDiff->column instanceof Column || ! $columnDiff->column instanceof Column ||
! $columnDiff->column->getAutoincrement() || ! $columnDiff->column->getAutoincrement() ||
! (string) $columnDiff->column->getType() === 'Integer' ! $columnDiff->column->getType() instanceof Types\IntegerType
) { ) {
continue; continue;
} }
...@@ -915,9 +916,9 @@ class SqlitePlatform extends AbstractPlatform ...@@ -915,9 +916,9 @@ class SqlitePlatform extends AbstractPlatform
continue; 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]); unset($diff->changedColumns[$oldColumnName]);
} }
} }
...@@ -942,17 +943,17 @@ class SqlitePlatform extends AbstractPlatform ...@@ -942,17 +943,17 @@ class SqlitePlatform extends AbstractPlatform
} }
$field = array_merge(['unique' => null, 'autoincrement' => null, 'default' => null], $column->toArray()); $field = array_merge(['unique' => null, 'autoincrement' => null, 'default' => null], $column->toArray());
$type = (string) $field['type']; $type = $field['type'];
switch (true) { switch (true) {
case isset($field['columnDefinition']) || $field['autoincrement'] || $field['unique']: case isset($field['columnDefinition']) || $field['autoincrement'] || $field['unique']:
case $type == 'DateTime' && $field['default'] == $this->getCurrentTimestampSQL(): case $type instanceof Types\DateTimeType && $field['default'] == $this->getCurrentTimestampSQL():
case $type == 'Date' && $field['default'] == $this->getCurrentDateSQL(): case $type instanceof Types\DateType && $field['default'] == $this->getCurrentDateSQL():
case $type == 'Time' && $field['default'] == $this->getCurrentTimeSQL(): case $type instanceof Types\TimeType && $field['default'] == $this->getCurrentTimeSQL():
return false; return false;
} }
$field['name'] = $column->getQuotedName($this); $field['name'] = $column->getQuotedName($this);
if (strtolower($field['type']) == 'string' && $field['length'] === null) { if ($type instanceof Types\StringType && $field['length'] === null) {
$field['length'] = 255; $field['length'] = 255;
} }
......
...@@ -27,7 +27,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -27,7 +27,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* @author robo * @author robo
* @since 2.0 * @since 2.0
*/ */
class BigIntType extends Type class BigIntType extends Type implements PhpIntegerMappingType
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -26,7 +26,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -26,7 +26,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* *
* @since 2.0 * @since 2.0
*/ */
class DateTimeType extends Type class DateTimeType extends Type implements PhpDateTimeMappingType
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -44,7 +44,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -44,7 +44,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* @author Jonathan Wage <jonwage@gmail.com> * @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
*/ */
class DateTimeTzType extends Type class DateTimeTzType extends Type implements PhpDateTimeMappingType
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -27,7 +27,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; ...@@ -27,7 +27,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* @author Roman Borschel <roman@code-factory.org> * @author Roman Borschel <roman@code-factory.org>
* @since 2.0 * @since 2.0
*/ */
class IntegerType extends Type class IntegerType extends Type implements PhpIntegerMappingType
{ {
/** /**
* {@inheritdoc} * {@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; ...@@ -26,7 +26,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
* *
* @author robo * @author robo
*/ */
class SmallIntType extends Type class SmallIntType extends Type implements PhpIntegerMappingType
{ {
/** /**
* {@inheritdoc} * {@inheritdoc}
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL\Functional\Ticket; namespace Doctrine\Tests\DBAL\Functional\Ticket;
use Doctrine\DBAL\Schema\SQLServerSchemaManager; use Doctrine\DBAL\Schema\SQLServerSchemaManager;
use Doctrine\DBAL\Types\DecimalType;
/** /**
* @group DBAL-461 * @group DBAL-461
...@@ -31,6 +32,6 @@ class DBAL461Test extends \PHPUnit\Framework\TestCase ...@@ -31,6 +32,6 @@ class DBAL461Test extends \PHPUnit\Framework\TestCase
'comment' => null, '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