Unverified Commit be458b5d authored by Sergei Morozov's avatar Sergei Morozov

Merge pull request #3586 from morozov/issues/3263

Get rid of hard-coded default values of maximum field lengths
parents 75bf53d7 9c4ce0b0
# Upgrade to 3.0
## BC BREAK: Changes in handling string and binary columns
- When generating schema DDL, DBAL no longer provides the default length for string and binary columns. The application may need to provide the column length if required by the target platform.
- The `\DBAL\Platforms\AbstractPlatform::getVarcharTypeDeclarationSQL()` method has been renamed to `::getStringTypeDeclarationSQL()`.
- The following `AbstractPlatform` methods have been removed as no longer relevant: `::getCharMaxLength()`, `::getVarcharMaxLength()`, `::getVarcharDefaultLength()`, `::getBinaryMaxLength()`, `::getBinaryDefaultLength()`.
## BC BREAK: Changes in `Doctrine\DBAL\Event\SchemaCreateTableEventArgs`
Table columns are no longer indexed by column name. Use the `name` attribute of the column instead.
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Exception;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use function sprintf;
final class ColumnLengthRequired extends DBALException
{
/**
* @param AbstractPlatform $platform The target platform
* @param string $type The SQL column type
*/
public static function new(AbstractPlatform $platform, string $type) : self
{
return new self(
sprintf(
'The "%s" platform requires the length of a %s column to be specified',
$platform->getName(),
$type
)
);
}
}
......@@ -28,7 +28,8 @@ class TableGeneratorSchemaVisitor implements Visitor
public function acceptSchema(Schema $schema) : void
{
$table = $schema->createTable($this->generatorTableName);
$table->addColumn('sequence_name', 'string');
$table->addColumn('sequence_name', 'string', ['length' => 255]);
$table->addColumn('sequence_value', 'integer', ['default' => 1]);
$table->addColumn('sequence_increment_by', 'integer', ['default' => 1]);
}
......
......@@ -15,6 +15,7 @@ use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
use Doctrine\DBAL\Event\SchemaDropTableEventArgs;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Exception\ColumnLengthRequired;
use Doctrine\DBAL\Platforms\Exception\NoColumnsSpecifiedForTable;
use Doctrine\DBAL\Platforms\Exception\NotSupported;
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
......@@ -168,43 +169,39 @@ abstract class AbstractPlatform
}
/**
* Returns the SQL snippet used to declare a VARCHAR column type.
* Returns the SQL snippet used to declare a string column type.
*
* @param mixed[] $field
* @param array<string, mixed> $column The column definition.
*
* @throws ColumnLengthRequired
*/
public function getVarcharTypeDeclarationSQL(array $field) : string
public function getStringTypeDeclarationSQL(array $column) : string
{
if (! isset($field['length'])) {
$field['length'] = $this->getVarcharDefaultLength();
}
$fixed = $field['fixed'] ?? false;
$maxLength = $fixed
? $this->getCharMaxLength()
: $this->getVarcharMaxLength();
$length = $column['length'] ?? null;
if ($field['length'] > $maxLength) {
return $this->getClobTypeDeclarationSQL($field);
if (empty($column['fixed'])) {
return $this->getVarcharTypeDeclarationSQLSnippet($length);
}
return $this->getVarcharTypeDeclarationSQLSnippet($field['length'], $fixed);
return $this->getCharTypeDeclarationSQLSnippet($length);
}
/**
* Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
* Returns the SQL snippet used to declare a binary string column type.
*
* @param mixed[] $field The column definition.
* @param array<string, mixed> $column The column definition.
*
* @throws ColumnLengthRequired
*/
public function getBinaryTypeDeclarationSQL(array $field) : string
public function getBinaryTypeDeclarationSQL(array $column) : string
{
if (! isset($field['length'])) {
$field['length'] = $this->getBinaryDefaultLength();
}
$length = $column['length'] ?? null;
$fixed = $field['fixed'] ?? false;
if (empty($column['fixed'])) {
return $this->getVarbinaryTypeDeclarationSQLSnippet($length);
}
return $this->getBinaryTypeDeclarationSQLSnippet($field['length'], $fixed);
return $this->getBinaryTypeDeclarationSQLSnippet($length);
}
/**
......@@ -213,14 +210,16 @@ abstract class AbstractPlatform
* By default this maps directly to a CHAR(36) and only maps to more
* special datatypes when the underlying databases support this datatype.
*
* @param mixed[] $field
* @param array<string, mixed> $column The column definition.
*
* @throws DBALException
*/
public function getGuidTypeDeclarationSQL(array $field) : string
public function getGuidTypeDeclarationSQL(array $column) : string
{
$field['length'] = 36;
$field['fixed'] = true;
$column['length'] = 36;
$column['fixed'] = true;
return $this->getVarcharTypeDeclarationSQL($field);
return $this->getStringTypeDeclarationSQL($column);
}
/**
......@@ -237,27 +236,71 @@ abstract class AbstractPlatform
}
/**
* @param int $length The length of the column.
* @param bool $fixed Whether the column length is fixed.
* @param int|null $length The length of the column in characters
* or NULL if the length should be omitted.
*
* @throws DBALException If not supported on this platform.
* @throws ColumnLengthRequired
*/
protected function getCharTypeDeclarationSQLSnippet(?int $length) : string
{
$sql = 'CHAR';
if ($length !== null) {
$sql .= sprintf('(%d)', $length);
}
return $sql;
}
/**
* @param int|null $length The length of the column in characters
* or NULL if the length should be omitted.
*
* @throws ColumnLengthRequired
*/
protected function getVarcharTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getVarcharTypeDeclarationSQLSnippet(?int $length) : string
{
throw NotSupported::new('VARCHARs not supported by Platform.');
if ($length === null) {
throw ColumnLengthRequired::new($this, 'VARCHAR');
}
return sprintf('VARCHAR(%d)', $length);
}
/**
* Returns the SQL snippet used to declare a BINARY/VARBINARY column type.
* Returns the SQL snippet used to declare a fixed length binary column type.
*
* @param int $length The length of the column.
* @param bool $fixed Whether the column length is fixed.
* @param int|null $length The length of the column in bytes
* or NULL if the length should be omitted.
*
* @throws DBALException If not supported on this platform.
* @throws ColumnLengthRequired
*/
protected function getBinaryTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getBinaryTypeDeclarationSQLSnippet(?int $length) : string
{
throw NotSupported::new('BINARY/VARBINARY column types are not supported by this platform.');
$sql = 'BINARY';
if ($length !== null) {
$sql .= sprintf('(%d)', $length);
}
return $sql;
}
/**
* Returns the SQL snippet used to declare a variable length binary column type.
*
* @param int|null $length The length of the column in bytes
* or NULL if the length should be omitted.
*
* @throws ColumnLengthRequired
*/
protected function getVarbinaryTypeDeclarationSQLSnippet(?int $length) : string
{
if ($length === null) {
throw ColumnLengthRequired::new($this, 'VARBINARY');
}
return sprintf('VARBINARY(%d)', $length);
}
/**
......@@ -438,46 +481,6 @@ abstract class AbstractPlatform
return "\n";
}
/**
* Gets the maximum length of a char field.
*/
public function getCharMaxLength() : int
{
return $this->getVarcharMaxLength();
}
/**
* Gets the maximum length of a varchar field.
*/
public function getVarcharMaxLength() : int
{
return 4000;
}
/**
* Gets the default length of a varchar field.
*/
public function getVarcharDefaultLength() : int
{
return 255;
}
/**
* Gets the maximum length of a binary field.
*/
public function getBinaryMaxLength() : int
{
return 4000;
}
/**
* Gets the default length of a binary field.
*/
public function getBinaryDefaultLength() : int
{
return 255;
}
/**
* Gets all SQL wildcard characters of the platform.
*
......@@ -1337,10 +1340,6 @@ abstract class AbstractPlatform
$columnData['version'] = $column->hasPlatformOption('version') ? $column->getPlatformOption('version') : false;
$columnData['comment'] = $this->getColumnComment($column);
if ($columnData['type'] instanceof Types\StringType && $columnData['length'] === null) {
$columnData['length'] = 255;
}
if (in_array($column->getName(), $options['primary'])) {
$columnData['primary'] = true;
}
......
......@@ -22,43 +22,6 @@ use function strtoupper;
class DB2Platform extends AbstractPlatform
{
/**
* {@inheritdoc}
*/
public function getCharMaxLength() : int
{
return 254;
}
/**
* {@inheritdoc}
*/
public function getBinaryMaxLength() : int
{
return 32704;
}
/**
* {@inheritdoc}
*/
public function getBinaryDefaultLength() : int
{
return 1;
}
/**
* {@inheritDoc}
*/
public function getVarcharTypeDeclarationSQL(array $field) : string
{
// for IBM DB2, the CHAR max length is less than VARCHAR default length
if (! isset($field['length']) && ! empty($field['fixed'])) {
$field['length'] = $this->getCharMaxLength();
}
return parent::getVarcharTypeDeclarationSQL($field);
}
/**
* {@inheritDoc}
*/
......@@ -109,18 +72,17 @@ class DB2Platform extends AbstractPlatform
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getBinaryTypeDeclarationSQLSnippet(?int $length) : string
{
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(254)')
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
return $this->getCharTypeDeclarationSQLSnippet($length) . ' FOR BIT DATA';
}
/**
* {@inheritdoc}
* {@inheritDoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getVarbinaryTypeDeclarationSQLSnippet(?int $length) : string
{
return $this->getVarcharTypeDeclarationSQLSnippet($length, $fixed) . ' FOR BIT DATA';
return $this->getVarcharTypeDeclarationSQLSnippet($length) . ' FOR BIT DATA';
}
/**
......
......@@ -201,23 +201,6 @@ class MySqlPlatform extends AbstractPlatform
return 'DROP VIEW ' . $name;
}
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
{
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
}
/**
* {@inheritdoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
{
return $fixed ? 'BINARY(' . ($length ?: 255) . ')' : 'VARBINARY(' . ($length ?: 255) . ')';
}
/**
* Gets the SQL snippet used to declare a CLOB column type.
* TINYTEXT : 2 ^ 8 - 1 = 255
......@@ -1038,22 +1021,6 @@ SQL
];
}
/**
* {@inheritDoc}
*/
public function getVarcharMaxLength() : int
{
return 65535;
}
/**
* {@inheritdoc}
*/
public function getBinaryMaxLength() : int
{
return 65535;
}
/**
* {@inheritDoc}
*/
......
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\ColumnLengthRequired;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\DBAL\Schema\Index;
......@@ -309,26 +310,33 @@ class OraclePlatform extends AbstractPlatform
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getVarcharTypeDeclarationSQLSnippet(?int $length) : string
{
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
: ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
if ($length === null) {
throw ColumnLengthRequired::new($this, 'VARCHAR2');
}
return sprintf('VARCHAR2(%d)', $length);
}
/**
* {@inheritdoc}
* {@inheritDoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getBinaryTypeDeclarationSQLSnippet(?int $length) : string
{
return 'RAW(' . ($length ?: $this->getBinaryMaxLength()) . ')';
if ($length === null) {
throw ColumnLengthRequired::new($this, 'RAW');
}
return sprintf('RAW(%d)', $length);
}
/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function getBinaryMaxLength() : int
protected function getVarbinaryTypeDeclarationSQLSnippet(?int $length) : string
{
return 2000;
return $this->getBinaryTypeDeclarationSQLSnippet($length);
}
/**
......
......@@ -959,7 +959,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getGuidTypeDeclarationSQL(array $field) : string
public function getGuidTypeDeclarationSQL(array $column) : string
{
return 'UUID';
}
......@@ -1007,16 +1007,29 @@ SQL
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getVarcharTypeDeclarationSQLSnippet(?int $length) : string
{
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
$sql = 'VARCHAR';
if ($length !== null) {
$sql .= sprintf('(%d)', $length);
}
return $sql;
}
/**
* {@inheritdoc}
* {@inheritDoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet(?int $length) : string
{
return 'BYTEA';
}
/**
* {@inheritDoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getVarbinaryTypeDeclarationSQLSnippet(?int $length) : string
{
return 'BYTEA';
}
......@@ -1135,30 +1148,6 @@ SQL
];
}
/**
* {@inheritDoc}
*/
public function getVarcharMaxLength() : int
{
return 65535;
}
/**
* {@inheritdoc}
*/
public function getBinaryMaxLength() : int
{
return 0;
}
/**
* {@inheritdoc}
*/
public function getBinaryDefaultLength() : int
{
return 0;
}
/**
* {@inheritdoc}
*/
......
......@@ -295,22 +295,6 @@ class SQLAnywherePlatform extends AbstractPlatform
return $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
}
/**
* {@inheritdoc}
*/
public function getBinaryDefaultLength() : int
{
return 1;
}
/**
* {@inheritdoc}
*/
public function getBinaryMaxLength() : int
{
return 32767;
}
/**
* {@inheritdoc}
*/
......@@ -658,7 +642,7 @@ class SQLAnywherePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getGuidTypeDeclarationSQL(array $field) : string
public function getGuidTypeDeclarationSQL(array $column) : string
{
return 'UNIQUEIDENTIFIER';
}
......@@ -1214,22 +1198,6 @@ SQL
return 'TIMESTAMP WITH TIME ZONE';
}
/**
* {@inheritdoc}
*/
public function getVarcharDefaultLength() : int
{
return 1;
}
/**
* {@inheritdoc}
*/
public function getVarcharMaxLength() : int
{
return 32767;
}
/**
* {@inheritdoc}
*/
......@@ -1399,16 +1367,6 @@ SQL
return $sql;
}
/**
* {@inheritdoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
{
return $fixed
? 'BINARY(' . ($length ?: $this->getBinaryDefaultLength()) . ')'
: 'VARBINARY(' . ($length ?: $this->getBinaryDefaultLength()) . ')';
}
/**
* Returns the SQL snippet for creating a table constraint.
*
......@@ -1496,16 +1454,6 @@ SQL
return Keywords\SQLAnywhereKeywords::class;
}
/**
* {@inheritdoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
{
return $fixed
? ($length ? 'CHAR(' . $length . ')' : 'CHAR(' . $this->getVarcharDefaultLength() . ')')
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(' . $this->getVarcharDefaultLength() . ')');
}
/**
* {@inheritdoc}
*/
......
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Doctrine\DBAL\Platforms;
use Doctrine\DBAL\Exception\ColumnLengthRequired;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ColumnDiff;
......@@ -1136,7 +1137,7 @@ SQL
/**
* {@inheritDoc}
*/
public function getGuidTypeDeclarationSQL(array $field) : string
public function getGuidTypeDeclarationSQL(array $column) : string
{
return 'UNIQUEIDENTIFIER';
}
......@@ -1152,25 +1153,27 @@ SQL
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getCharTypeDeclarationSQLSnippet(?int $length) : string
{
return $fixed ? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NVARCHAR(255)');
}
$sql = 'NCHAR';
/**
* {@inheritdoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
{
return $fixed ? 'BINARY(' . ($length ?: 255) . ')' : 'VARBINARY(' . ($length ?: 255) . ')';
if ($length !== null) {
$sql .= sprintf('(%d)', $length);
}
return $sql;
}
/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function getBinaryMaxLength() : int
protected function getVarcharTypeDeclarationSQLSnippet(?int $length) : string
{
return 8000;
if ($length === null) {
throw ColumnLengthRequired::new($this, 'NVARCHAR');
}
return sprintf('NVARCHAR(%d)', $length);
}
/**
......
......@@ -378,20 +378,10 @@ class SqlitePlatform extends AbstractPlatform
return ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')';
}
/**
* {@inheritDoc}
*/
protected function getVarcharTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
{
return $fixed
? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
}
/**
* {@inheritdoc}
*/
protected function getBinaryTypeDeclarationSQLSnippet(int $length, bool $fixed) : string
protected function getBinaryTypeDeclarationSQLSnippet(?int $length) : string
{
return 'BLOB';
}
......@@ -399,17 +389,23 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@inheritdoc}
*/
public function getBinaryMaxLength() : int
protected function getVarcharTypeDeclarationSQLSnippet(?int $length) : string
{
return 0;
$sql = 'VARCHAR';
if ($length !== null) {
$sql .= sprintf('(%d)', $length);
}
return $sql;
}
/**
* {@inheritdoc}
* {@inheritDoc}
*/
public function getBinaryDefaultLength() : int
protected function getVarbinaryTypeDeclarationSQLSnippet(?int $length) : string
{
return 0;
return 'BLOB';
}
/**
......
......@@ -422,10 +422,10 @@ class Comparator
if (($properties1['type'] instanceof Types\StringType && ! $properties1['type'] instanceof Types\GuidType) ||
$properties1['type'] instanceof Types\BinaryType
) {
// check if value of length is set at all, default value assumed otherwise.
$length1 = $properties1['length'] ?: 255;
$length2 = $properties2['length'] ?: 255;
if ($length1 !== $length2) {
if ((isset($properties1['length']) !== isset($properties2['length']))
|| (isset($properties1['length']) && isset($properties2['length'])
&& $properties1['length'] !== $properties2['length'])
) {
$changedProperties[] = 'length';
}
......
......@@ -33,7 +33,7 @@ class DateIntervalType extends Type
{
$fieldDeclaration['length'] = 255;
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
return $platform->getStringTypeDeclarationSQL($fieldDeclaration);
}
/**
......
......@@ -16,7 +16,7 @@ class StringType extends Type
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) : string
{
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration);
return $platform->getStringTypeDeclarationSQL($fieldDeclaration);
}
/**
......
......@@ -162,6 +162,8 @@ abstract class Type
*
* @param array<string, mixed> $fieldDeclaration The field declaration.
* @param AbstractPlatform $platform The currently used database platform.
*
* @throws DBALException
*/
abstract public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) : string;
......
......@@ -52,7 +52,7 @@ class DataAccessTest extends DbalFunctionalTestCase
$table = new Table('fetch_table');
$table->addColumn('test_int', 'integer');
$table->addColumn('test_string', 'string');
$table->addColumn('test_string', 'string', ['length' => 32]);
$table->addColumn('test_datetime', 'datetime', ['notnull' => false]);
$table->setPrimaryKey(['test_int']);
......
......@@ -163,8 +163,8 @@ class NamedParametersTest extends DbalFunctionalTestCase
try {
$table = new Table('ddc1372_foobar');
$table->addColumn('id', 'integer');
$table->addColumn('foo', 'string');
$table->addColumn('bar', 'string');
$table->addColumn('foo', 'string', ['length' => 1]);
$table->addColumn('bar', 'string', ['length' => 1]);
$table->setPrimaryKey(['id']);
$sm = $this->connection->getSchemaManager();
......
......@@ -24,7 +24,7 @@ class PDOStatementTest extends DbalFunctionalTestCase
$table = new Table('stmt_test');
$table->addColumn('id', 'integer');
$table->addColumn('name', 'string');
$table->addColumn('name', 'string', ['length' => 8]);
$this->connection->getSchemaManager()->dropAndCreateTable($table);
}
......
<?php
declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Functional\Platform;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\Tests\DbalFunctionalTestCase;
use function sprintf;
abstract class ColumnTest extends DbalFunctionalTestCase
{
public function testVariableLengthStringNoLength() : void
{
$this->assertColumn(Types::STRING, [], 'Test', ParameterType::STRING);
}
public function testVariableLengthStringWithLength() : void
{
$this->assertColumn(Types::STRING, ['length' => 8], 'Doctrine', ParameterType::STRING);
}
public function testFixedLengthStringNoLength() : void
{
$this->assertColumn(Types::STRING, ['fixed' => true], 'Z', ParameterType::STRING);
}
public function testFixedLengthStringWithLength() : void
{
$this->assertColumn(Types::STRING, [
'fixed' => true,
'length' => 8,
], 'Doctrine', ParameterType::STRING);
}
public function testVariableLengthBinaryNoLength() : void
{
$this->assertColumn(Types::BINARY, [], "\x00\x01\x02\x03", ParameterType::BINARY);
}
public function testVariableLengthBinaryWithLength() : void
{
$this->assertColumn(Types::BINARY, ['length' => 8], "\xCE\xC6\x6B\xDD\x9F\xD8\x07\xB4", ParameterType::BINARY);
}
public function testFixedLengthBinaryNoLength() : void
{
$this->assertColumn(Types::BINARY, ['fixed' => true], "\xFF", ParameterType::BINARY);
}
public function testFixedLengthBinaryWithLength() : void
{
$this->assertColumn(Types::BINARY, [
'fixed' => true,
'length' => 8,
], "\xA0\x0A\x7B\x0E\xA4\x60\x78\xD8", ParameterType::BINARY);
}
protected function requirePlatform(string $class) : void
{
if ($this->connection->getDatabasePlatform() instanceof $class) {
return;
}
self::markTestSkipped(sprintf('The test requires %s', $class));
}
/**
* @param array<string, mixed> $column
*/
protected function assertColumn(string $type, array $column, string $value, int $bindType) : void
{
$table = new Table('column_test');
$table->addColumn('val', $type, $column);
$sm = $this->connection->getSchemaManager();
$sm->dropAndCreateTable($table);
self::assertSame(1, $this->connection->insert('column_test', ['val' => $value], [$bindType]));
self::assertSame($value, Type::getType($type)->convertToPHPValue(
$this->connection->fetchColumn('SELECT val FROM column_test'),
$this->connection->getDatabasePlatform()
));
}
}
<?php
declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
final class IBMDB2 extends ColumnTest
{
protected function setUp() : void
{
parent::setUp();
$this->requirePlatform(DB2Platform::class);
}
public function testVariableLengthStringNoLength() : void
{
self::markTestSkipped();
}
public function testVariableLengthBinaryNoLength() : void
{
self::markTestSkipped();
}
}
<?php
declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
final class MySQL extends ColumnTest
{
protected function setUp() : void
{
parent::setUp();
$this->requirePlatform(MySqlPlatform::class);
}
public function testVariableLengthStringNoLength() : void
{
self::markTestSkipped();
}
public function testVariableLengthBinaryNoLength() : void
{
self::markTestSkipped();
}
}
<?php
declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
final class Oracle extends ColumnTest
{
protected function setUp() : void
{
parent::setUp();
$this->requirePlatform(OraclePlatform::class);
}
public function testVariableLengthStringNoLength() : void
{
self::markTestSkipped();
}
public function testVariableLengthBinaryNoLength() : void
{
self::markTestSkipped();
}
public function testFixedLengthBinaryNoLength() : void
{
self::markTestSkipped();
}
}
<?php
declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
final class PostgreSQL extends ColumnTest
{
protected function setUp() : void
{
parent::setUp();
$this->requirePlatform(PostgreSqlPlatform::class);
}
}
<?php
declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
final class SQLServer extends ColumnTest
{
protected function setUp() : void
{
parent::setUp();
$this->requirePlatform(SQLServerPlatform::class);
}
public function testVariableLengthStringNoLength() : void
{
self::markTestSkipped();
}
public function testVariableLengthBinaryNoLength() : void
{
self::markTestSkipped();
}
}
<?php
declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\Tests\DBAL\Functional\Platform\ColumnTest;
final class SQLite extends ColumnTest
{
protected function setUp() : void
{
parent::setUp();
$this->requirePlatform(SqlitePlatform::class);
}
}
......@@ -11,7 +11,6 @@ use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\Portability\Connection as ConnectionPortability;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalFunctionalTestCase;
use Throwable;
use function strlen;
/**
......@@ -44,20 +43,31 @@ class PortabilityTest extends DbalFunctionalTestCase
$this->portableConnection = DriverManager::getConnection($params, $this->connection->getConfiguration(), $this->connection->getEventManager());
try {
$table = new Table('portability_table');
$table->addColumn('Test_Int', 'integer');
$table->addColumn('Test_String', 'string', ['fixed' => true, 'length' => 32]);
$table->addColumn('Test_Null', 'string', ['notnull' => false]);
$table->setPrimaryKey(['Test_Int']);
$sm = $this->portableConnection->getSchemaManager();
$sm->createTable($table);
$this->portableConnection->insert('portability_table', ['Test_Int' => 1, 'Test_String' => 'foo', 'Test_Null' => '']);
$this->portableConnection->insert('portability_table', ['Test_Int' => 2, 'Test_String' => 'foo ', 'Test_Null' => null]);
} catch (Throwable $e) {
}
$table = new Table('portability_table');
$table->addColumn('Test_Int', 'integer');
$table->addColumn('Test_String', 'string', [
'length' => 8,
'fixed' => true,
]);
$table->addColumn('Test_Null', 'string', [
'length' => 1,
'notnull' => false,
]);
$table->setPrimaryKey(['Test_Int']);
$sm = $this->portableConnection->getSchemaManager();
$sm->dropAndCreateTable($table);
$this->portableConnection->insert('portability_table', [
'Test_Int' => 1,
'Test_String' => 'foo',
'Test_Null' => '',
]);
$this->portableConnection->insert('portability_table', [
'Test_Int' => 2,
'Test_String' => 'foo ',
'Test_Null' => null,
]);
}
return $this->portableConnection;
......
......@@ -35,11 +35,14 @@ class ResultCacheTest extends DbalFunctionalTestCase
$table = new Table('caching');
$table->addColumn('test_int', 'integer');
$table->addColumn('test_string', 'string', ['notnull' => false]);
$table->addColumn('test_string', 'string', [
'length' => 8,
'notnull' => false,
]);
$table->setPrimaryKey(['test_int']);
$sm = $this->connection->getSchemaManager();
$sm->createTable($table);
$sm->dropAndCreateTable($table);
foreach ($this->expectedResult as $row) {
$this->connection->insert('caching', $row);
......@@ -52,13 +55,6 @@ class ResultCacheTest extends DbalFunctionalTestCase
$config->setResultCacheImpl($cache);
}
protected function tearDown() : void
{
$this->connection->getSchemaManager()->dropTable('caching');
parent::tearDown();
}
public function testCacheFetchAssoc() : void
{
$this->assertCacheNonCacheSelectSameFetchModeAreEqual(
......
......@@ -28,6 +28,7 @@ class DefaultValueTest extends DbalFunctionalTestCase
foreach (self::columnProvider() as [$name, $default]) {
$table->addColumn($name, 'string', [
'length' => 32,
'default' => $default,
'notnull' => false,
]);
......
......@@ -55,29 +55,6 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
self::assertContains('foo_id', $primaryKey);
}
public function testDiffTableBug() : void
{
$schema = new Schema();
$table = $schema->createTable('diffbug_routing_translations');
$table->addColumn('id', 'integer');
$table->addColumn('route', 'string');
$table->addColumn('locale', 'string');
$table->addColumn('attribute', 'string');
$table->addColumn('localized_value', 'string');
$table->addColumn('original_value', 'string');
$table->setPrimaryKey(['id']);
$table->addUniqueIndex(['route', 'locale', 'attribute']);
$table->addIndex(['localized_value']); // this is much more selective than the unique index
$this->schemaManager->createTable($table);
$tableFetched = $this->schemaManager->listTableDetails('diffbug_routing_translations');
$comparator = new Comparator();
$diff = $comparator->diffTable($tableFetched, $table);
self::assertNull($diff, 'no changes expected.');
}
public function testFulltextIndex() : void
{
$table = new Table('fulltext_index');
......@@ -457,7 +434,10 @@ class MySqlSchemaManagerTest extends SchemaManagerFunctionalTestCase
$table->addColumn('col_datetime_null', 'datetime', ['notnull' => false, 'default' => null]);
$table->addColumn('col_int', 'integer', ['default' => 1]);
$table->addColumn('col_neg_int', 'integer', ['default' => -1]);
$table->addColumn('col_string', 'string', ['default' => 'A']);
$table->addColumn('col_string', 'string', [
'length' => 1,
'default' => 'A',
]);
$table->addColumn('col_decimal', 'decimal', ['scale' => 3, 'precision' => 6, 'default' => -2.3]);
$table->addColumn('col_date', 'date', ['default' => '2012-12-12']);
......
......@@ -53,8 +53,11 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$table = new Table($tableName);
$table->addColumn('id', 'integer');
$table->addColumn('column_varbinary', 'binary', []);
$table->addColumn('column_binary', 'binary', ['fixed' => true]);
$table->addColumn('column_varbinary', 'binary', ['length' => 32]);
$table->addColumn('column_binary', 'binary', [
'length' => 32,
'fixed' => true,
]);
$table->setPrimaryKey(['id']);
$this->schemaManager->createTable($table);
......@@ -80,7 +83,7 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$table->addColumn('id', 'integer');
$table->addColumn('foo', 'integer');
$table->addColumn('bar', 'string');
$table->addColumn('bar', 'string', ['length' => 32]);
$table->setPrimaryKey(['id']);
$this->schemaManager->dropAndCreateTable($table);
......@@ -234,7 +237,7 @@ class OracleSchemaManagerTest extends SchemaManagerFunctionalTestCase
$this->schemaManager->dropAndCreateTable($table);
$otherTable = new Table($table->getName());
$otherTable->addColumn('id', Types::STRING);
$otherTable->addColumn('id', Types::STRING, ['length' => 32]);
TestUtil::getTempConnection()->getSchemaManager()->dropAndCreateTable($otherTable);
$columns = $this->schemaManager->listTableColumns($table->getName(), $this->connection->getDatabase());
......
......@@ -39,30 +39,42 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
public function testColumnCollation() : void
{
$table = new Table($tableName = 'test_collation');
$column = $table->addColumn($columnName = 'test', 'string');
$column = $table->addColumn('test', 'string', ['length' => 32]);
$this->schemaManager->dropAndCreateTable($table);
$columns = $this->schemaManager->listTableColumns($tableName);
self::assertTrue($columns[$columnName]->hasPlatformOption('collation')); // SQL Server should report a default collation on the column
self::assertTrue($columns['test']->hasPlatformOption('collation')); // SQL Server should report a default collation on the column
$column->setPlatformOption('collation', $collation = 'Icelandic_CS_AS');
$this->schemaManager->dropAndCreateTable($table);
$columns = $this->schemaManager->listTableColumns($tableName);
self::assertEquals($collation, $columns[$columnName]->getPlatformOption('collation'));
self::assertEquals($collation, $columns['test']->getPlatformOption('collation'));
}
public function testDefaultConstraints() : void
{
$table = new Table('sqlsrv_default_constraints');
$table->addColumn('no_default', 'string');
$table->addColumn('no_default', 'string', ['length' => 32]);
$table->addColumn('df_integer', 'integer', ['default' => 666]);
$table->addColumn('df_string_1', 'string', ['default' => 'foobar']);
$table->addColumn('df_string_2', 'string', ['default' => 'Doctrine rocks!!!']);
$table->addColumn('df_string_3', 'string', ['default' => 'another default value']);
$table->addColumn('df_string_4', 'string', ['default' => 'column to rename']);
$table->addColumn('df_string_1', 'string', [
'length' => 32,
'default' => 'foobar',
]);
$table->addColumn('df_string_2', 'string', [
'length' => 32,
'default' => 'Doctrine rocks!!!',
]);
$table->addColumn('df_string_3', 'string', [
'length' => 32,
'default' => 'another default value',
]);
$table->addColumn('df_string_4', 'string', [
'length' => 32,
'default' => 'column to rename',
]);
$table->addColumn('df_boolean', 'boolean', ['default' => true]);
$this->schemaManager->createTable($table);
......@@ -87,9 +99,12 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
),
'df_string_2' => new ColumnDiff(
'df_string_2',
new Column('df_string_2', Type::getType('string')),
new Column('df_string_2', Type::getType('string'), ['length' => 32]),
['default'],
new Column('df_string_2', Type::getType('string'), ['default' => 'Doctrine rocks!!!'])
new Column('df_string_2', Type::getType('string'), [
'length' => 32,
'default' => 'Doctrine rocks!!!',
])
),
'df_string_3' => new ColumnDiff(
'df_string_3',
......@@ -105,7 +120,7 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
),
],
[
'df_string_1' => new Column('df_string_1', Type::getType('string')),
'df_string_1' => new Column('df_string_1', Type::getType('string'), ['length' => 32]),
],
[],
[],
......@@ -210,9 +225,12 @@ class SQLServerSchemaManagerTest extends SchemaManagerFunctionalTestCase
// Remove comment from null-commented column.
$tableDiff->changedColumns['comment_null'] = new ColumnDiff(
'comment_null',
new Column('comment_null', Type::getType('string')),
new Column('comment_null', Type::getType('string'), ['length' => 255]),
['type'],
new Column('comment_null', Type::getType('integer'), ['comment' => null])
new Column('comment_null', Type::getType('integer'), [
'length' => 255,
'comment' => null,
])
);
// Change type to custom type from empty string commented column.
......
......@@ -916,7 +916,10 @@ abstract class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase
$table = new Table($tableName);
$table->addColumn('col_int', 'smallint', ['default' => 666]);
$table->addColumn('col_string', 'string', ['default' => 'foo']);
$table->addColumn('col_string', 'string', [
'length' => 3,
'default' => 'foo',
]);
$this->schemaManager->dropAndCreateTable($table);
......@@ -931,9 +934,16 @@ abstract class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase
$tableDiff->changedColumns['col_string'] = new ColumnDiff(
'col_string',
new Column('col_string', Type::getType('string'), ['default' => 'foo', 'fixed' => true]),
new Column('col_string', Type::getType('string'), [
'length' => 3,
'fixed' => true,
'default' => 'foo',
]),
['fixed'],
new Column('col_string', Type::getType('string'), ['default' => 'foo'])
new Column('col_string', Type::getType('string'), [
'length' => 3,
'default' => 'foo',
])
);
$this->schemaManager->alterTable($tableDiff);
......@@ -1061,10 +1071,22 @@ abstract class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase
{
$table = new Table('col_def_lifecycle');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('column1', 'string', ['default' => null]);
$table->addColumn('column2', 'string', ['default' => '']);
$table->addColumn('column3', 'string', ['default' => 'default1']);
$table->addColumn('column4', 'integer', ['default' => 0]);
$table->addColumn('column1', 'string', [
'length' => 1,
'default' => null,
]);
$table->addColumn('column2', 'string', [
'length' => 1,
'default' => '',
]);
$table->addColumn('column3', 'string', [
'length' => 8,
'default' => 'default1',
]);
$table->addColumn('column4', 'integer', [
'length' => 1,
'default' => 0,
]);
$table->setPrimaryKey(['id']);
$this->schemaManager->dropAndCreateTable($table);
......@@ -1102,7 +1124,7 @@ abstract class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase
$table = new Table($tableName);
$table->addColumn('id', 'integer');
$table->addColumn('column_varbinary', 'binary', []);
$table->addColumn('column_varbinary', 'binary', ['length' => 16]);
$table->addColumn('column_binary', 'binary', ['fixed' => true]);
$table->setPrimaryKey(['id']);
......@@ -1136,7 +1158,7 @@ abstract class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase
$table = new Table($primaryTableName);
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('foo', 'integer');
$table->addColumn('bar', 'string');
$table->addColumn('bar', 'string', ['length' => 32]);
$table->addForeignKeyConstraint($foreignTableName, ['foo'], ['id']);
$table->addIndex(['bar']);
$table->setPrimaryKey(['id']);
......@@ -1423,7 +1445,7 @@ abstract class SchemaManagerFunctionalTestCase extends DbalFunctionalTestCase
{
$table = new Table('test_pk_auto_increment');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('text', 'string');
$table->addColumn('text', 'string', ['length' => 1]);
$table->setPrimaryKey(['id']);
$this->schemaManager->dropAndCreateTable($table);
......
......@@ -67,7 +67,7 @@ class StatementTest extends DbalFunctionalTestCase
$sm = $this->connection->getSchemaManager();
$table = new Table('stmt_longer_results');
$table->addColumn('param', 'string');
$table->addColumn('param', 'string', ['length' => 24]);
$table->addColumn('val', 'text');
$sm->createTable($table);
......
......@@ -23,7 +23,10 @@ class TypeConversionTest extends DbalFunctionalTestCase
$table = new Table('type_conversion');
$table->addColumn('id', 'integer', ['notnull' => false]);
$table->addColumn('test_string', 'string', ['notnull' => false]);
$table->addColumn('test_string', 'string', [
'length' => 16,
'notnull' => false,
]);
$table->addColumn('test_boolean', 'boolean', ['notnull' => false]);
$table->addColumn('test_bigint', 'bigint', ['notnull' => false]);
$table->addColumn('test_smallint', 'bigint', ['notnull' => false]);
......
......@@ -21,16 +21,17 @@ class WriteTest extends DbalFunctionalTestCase
{
parent::setUp();
try {
$table = new Table('write_table');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('test_int', 'integer');
$table->addColumn('test_string', 'string', ['notnull' => false]);
$table->setPrimaryKey(['id']);
$table = new Table('write_table');
$table->addColumn('id', 'integer', ['autoincrement' => true]);
$table->addColumn('test_int', 'integer');
$table->addColumn('test_string', 'string', [
'length' => 32,
'notnull' => false,
]);
$table->setPrimaryKey(['id']);
$this->connection->getSchemaManager()->dropAndCreateTable($table);
$this->connection->getSchemaManager()->createTable($table);
} catch (Throwable $e) {
}
$this->connection->executeUpdate('DELETE FROM write_table');
}
......
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Exception\ColumnLengthRequired;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
......@@ -108,26 +109,6 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
);
}
public function testGeneratesTypeDeclarationForStrings() : void
{
self::assertEquals(
'CHAR(10)',
$this->platform->getVarcharTypeDeclarationSQL(
['length' => 10, 'fixed' => true]
)
);
self::assertEquals(
'VARCHAR(50)',
$this->platform->getVarcharTypeDeclarationSQL(['length' => 50]),
'Variable string declaration is not correct'
);
self::assertEquals(
'VARCHAR(255)',
$this->platform->getVarcharTypeDeclarationSQL([]),
'Long string declaration is not correct'
);
}
public function testPrefersIdentityColumns() : void
{
self::assertTrue($this->platform->prefersIdentityColumns());
......@@ -530,31 +511,18 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
self::assertSame('binary', $this->platform->getDoctrineTypeMapping('varbinary'));
}
protected function getBinaryMaxLength() : int
public function testGetVariableLengthStringTypeDeclarationSQLNoLength() : void
{
return 65535;
$this->expectException(ColumnLengthRequired::class);
parent::testGetVariableLengthStringTypeDeclarationSQLNoLength();
}
public function testReturnsBinaryTypeDeclarationSQL() : void
public function testGetVariableLengthBinaryTypeDeclarationSQLNoLength() : void
{
self::assertSame('VARBINARY(255)', $this->platform->getBinaryTypeDeclarationSQL([]));
self::assertSame('VARBINARY(255)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 0]));
self::assertSame('VARBINARY(65535)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 65535]));
self::assertSame('VARBINARY(65536)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 65536]));
$this->expectException(ColumnLengthRequired::class);
self::assertSame('BINARY(255)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
self::assertSame('BINARY(255)', $this->platform->getBinaryTypeDeclarationSQL([
'fixed' => true,
'length' => 0,
]));
self::assertSame('BINARY(65535)', $this->platform->getBinaryTypeDeclarationSQL([
'fixed' => true,
'length' => 65535,
]));
self::assertSame('BINARY(65536)', $this->platform->getBinaryTypeDeclarationSQL([
'fixed' => true,
'length' => 65536,
]));
parent::testGetVariableLengthBinaryTypeDeclarationSQLNoLength();
}
public function testDoesNotPropagateForeignKeyCreationForNonSupportingEngines() : void
......@@ -983,8 +951,8 @@ abstract class AbstractMySQLPlatformTestCase extends AbstractPlatformTestCase
public function testGetCreateTableSQLWithColumnCollation() : void
{
$table = new Table('foo');
$table->addColumn('no_collation', 'string');
$table->addColumn('column_collation', 'string')->setPlatformOption('collation', 'ascii_general_ci');
$table->addColumn('no_collation', 'string', ['length' => 255]);
$table->addColumn('column_collation', 'string', ['length' => 255])->setPlatformOption('collation', 'ascii_general_ci');
self::assertSame(
['CREATE TABLE foo (no_collation VARCHAR(255) NOT NULL, column_collation VARCHAR(255) NOT NULL COLLATE `ascii_general_ci`) DEFAULT CHARACTER SET utf8 COLLATE `utf8_unicode_ci` ENGINE = InnoDB'],
......
......@@ -345,20 +345,24 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
$table->addColumn('bloo', 'boolean');
$table->setPrimaryKey(['id']);
$tableDiff = new TableDiff('mytable');
$tableDiff->fromTable = $table;
$tableDiff->newName = 'userlist';
$tableDiff->addedColumns['quota'] = new Column('quota', Type::getType('integer'), ['notnull' => false]);
$tableDiff->removedColumns['foo'] = new Column('foo', Type::getType('integer'));
$tableDiff->changedColumns['bar'] = new ColumnDiff(
$tableDiff = new TableDiff('mytable');
$tableDiff->fromTable = $table;
$tableDiff->newName = 'userlist';
$tableDiff->addedColumns['quota'] = new Column('quota', Type::getType('integer'), ['notnull' => false]);
$tableDiff->removedColumns['foo'] = new Column('foo', Type::getType('integer'));
$tableDiff->changedColumns['bar'] = new ColumnDiff(
'bar',
new Column(
'baz',
Type::getType('string'),
['default' => 'def']
[
'length' => 255,
'default' => 'def',
]
),
['type', 'notnull', 'default']
);
$tableDiff->changedColumns['bloo'] = new ColumnDiff(
'bloo',
new Column(
......@@ -473,14 +477,9 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
$tableDiff->removedColumns['removed'] = new Column('removed', Type::getType('integer'), []);
$tableDiff->changedColumns['changed'] = new ColumnDiff(
'changed',
new Column(
'changed2',
Type::getType('string'),
[]
),
[]
new Column('changed2', Type::getType('string'), ['length' => 255])
);
$tableDiff->renamedColumns['renamed'] = new Column('renamed2', Type::getType('integer'), []);
$tableDiff->renamedColumns['renamed'] = new Column('renamed2', Type::getType('integer'));
$this->platform->getAlterTableSQL($tableDiff);
}
......@@ -506,19 +505,15 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
$tableDiff->addedColumns['quota'] = new Column('quota', Type::getType('integer'), ['comment' => 'A comment']);
$tableDiff->changedColumns['foo'] = new ColumnDiff(
'foo',
new Column(
'foo',
Type::getType('string')
),
new Column('foo', Type::getType('string'), ['length' => 255]),
['comment']
);
$tableDiff->changedColumns['bar'] = new ColumnDiff(
'bar',
new Column(
'baz',
Type::getType('string'),
['comment' => 'B comment']
),
new Column('baz', Type::getType('string'), [
'length' => 255,
'comment' => 'B comment',
]),
['comment']
);
......@@ -640,7 +635,7 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
public function testQuotedColumnInPrimaryKeyPropagation() : void
{
$table = new Table('`quoted`');
$table->addColumn('create', 'string');
$table->addColumn('create', 'string', ['length' => 255]);
$table->setPrimaryKey(['create']);
$sql = $this->platform->getCreateTableSQL($table);
......@@ -673,7 +668,7 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
public function testQuotedColumnInIndexPropagation() : void
{
$table = new Table('`quoted`');
$table->addColumn('create', 'string');
$table->addColumn('create', 'string', ['length' => 255]);
$table->addIndex(['create']);
$sql = $this->platform->getCreateTableSQL($table);
......@@ -683,7 +678,7 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
public function testQuotedNameInIndexSQL() : void
{
$table = new Table('test');
$table->addColumn('column1', 'string');
$table->addColumn('column1', 'string', ['length' => 255]);
$table->addIndex(['column1'], '`key`');
$sql = $this->platform->getCreateTableSQL($table);
......@@ -696,9 +691,9 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
public function testQuotedColumnInForeignKeyPropagation() : void
{
$table = new Table('`quoted`');
$table->addColumn('create', 'string');
$table->addColumn('foo', 'string');
$table->addColumn('`bar`', 'string');
$table->addColumn('create', 'string', ['length' => 255]);
$table->addColumn('foo', 'string', ['length' => 255]);
$table->addColumn('`bar`', 'string', ['length' => 255]);
// Foreign table with reserved keyword as name (needs quotation).
$foreignTable = new Table('foreign');
......@@ -808,7 +803,8 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
'select',
new Column(
'select',
Type::getType('string')
Type::getType('string'),
['length' => 255]
),
['type']
);
......@@ -837,31 +833,114 @@ abstract class AbstractPlatformTestCase extends DbalTestCase
$this->platform->getIdentitySequenceName('mytable', 'mycolumn');
}
public function testReturnsBinaryDefaultLength() : void
public function testGetFixedLengthStringTypeDeclarationSQLNoLength() : void
{
self::assertSame(
$this->getExpectedFixedLengthStringTypeDeclarationSQLNoLength(),
$this->platform->getStringTypeDeclarationSQL(['fixed' => true])
);
}
protected function getExpectedFixedLengthStringTypeDeclarationSQLNoLength() : string
{
self::assertSame($this->getBinaryDefaultLength(), $this->platform->getBinaryDefaultLength());
return 'CHAR';
}
protected function getBinaryDefaultLength() : int
public function testGetFixedLengthStringTypeDeclarationSQLWithLength() : void
{
return 255;
self::assertSame(
$this->getExpectedFixedLengthStringTypeDeclarationSQLWithLength(),
$this->platform->getStringTypeDeclarationSQL([
'fixed' => true,
'length' => 16,
])
);
}
public function testReturnsBinaryMaxLength() : void
protected function getExpectedFixedLengthStringTypeDeclarationSQLWithLength() : string
{
self::assertSame($this->getBinaryMaxLength(), $this->platform->getBinaryMaxLength());
return 'CHAR(16)';
}
protected function getBinaryMaxLength() : int
public function testGetVariableLengthStringTypeDeclarationSQLNoLength() : void
{
return 4000;
self::assertSame(
$this->getExpectedVariableLengthStringTypeDeclarationSQLNoLength(),
$this->platform->getStringTypeDeclarationSQL([])
);
}
public function testReturnsBinaryTypeDeclarationSQL() : void
protected function getExpectedVariableLengthStringTypeDeclarationSQLNoLength() : string
{
$this->expectException(DBALException::class);
return 'VARCHAR';
}
public function testGetVariableLengthStringTypeDeclarationSQLWithLength() : void
{
self::assertSame(
$this->getExpectedVariableLengthStringTypeDeclarationSQLWithLength(),
$this->platform->getStringTypeDeclarationSQL(['length' => 16])
);
}
$this->platform->getBinaryTypeDeclarationSQL([]);
protected function getExpectedVariableLengthStringTypeDeclarationSQLWithLength() : string
{
return 'VARCHAR(16)';
}
public function testGetFixedLengthBinaryTypeDeclarationSQLNoLength() : void
{
self::assertSame(
$this->getExpectedFixedLengthBinaryTypeDeclarationSQLNoLength(),
$this->platform->getBinaryTypeDeclarationSQL(['fixed' => true])
);
}
public function getExpectedFixedLengthBinaryTypeDeclarationSQLNoLength() : string
{
return 'BINARY';
}
public function testGetFixedLengthBinaryTypeDeclarationSQLWithLength() : void
{
self::assertSame(
$this->getExpectedFixedLengthBinaryTypeDeclarationSQLWithLength(),
$this->platform->getBinaryTypeDeclarationSQL([
'fixed' => true,
'length' => 16,
])
);
}
public function getExpectedFixedLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 'BINARY(16)';
}
public function testGetVariableLengthBinaryTypeDeclarationSQLNoLength() : void
{
self::assertSame(
$this->getExpectedVariableLengthBinaryTypeDeclarationSQLNoLength(),
$this->platform->getBinaryTypeDeclarationSQL([])
);
}
public function getExpectedVariableLengthBinaryTypeDeclarationSQLNoLength() : string
{
return 'VARBINARY';
}
public function testGetVariableLengthBinaryTypeDeclarationSQLWithLength() : void
{
self::assertSame(
$this->getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength(),
$this->platform->getBinaryTypeDeclarationSQL(['length' => 16])
);
}
public function getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 'VARBINARY(16)';
}
/**
......
......@@ -260,26 +260,6 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
);
}
public function testGeneratesTypeDeclarationForStrings() : void
{
self::assertEquals(
'CHAR(10)',
$this->platform->getVarcharTypeDeclarationSQL(
['length' => 10, 'fixed' => true]
)
);
self::assertEquals(
'VARCHAR(50)',
$this->platform->getVarcharTypeDeclarationSQL(['length' => 50]),
'Variable string declaration is not correct'
);
self::assertEquals(
'VARCHAR(255)',
$this->platform->getVarcharTypeDeclarationSQL([]),
'Long string declaration is not correct'
);
}
public function getGenerateUniqueIndexSql() : string
{
return 'CREATE UNIQUE INDEX index_name ON test (test, test2)';
......@@ -631,25 +611,24 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
];
}
protected function getBinaryDefaultLength() : int
public function getExpectedFixedLengthBinaryTypeDeclarationSQLNoLength() : string
{
return 0;
return 'BYTEA';
}
protected function getBinaryMaxLength() : int
public function getExpectedFixedLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 0;
return 'BYTEA';
}
public function testReturnsBinaryTypeDeclarationSQL() : void
public function getExpectedVariableLengthBinaryTypeDeclarationSQLNoLength() : string
{
self::assertSame('BYTEA', $this->platform->getBinaryTypeDeclarationSQL([]));
self::assertSame('BYTEA', $this->platform->getBinaryTypeDeclarationSQL(['length' => 0]));
self::assertSame('BYTEA', $this->platform->getBinaryTypeDeclarationSQL(['length' => 9999999]));
return 'BYTEA';
}
self::assertSame('BYTEA', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
self::assertSame('BYTEA', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 0]));
self::assertSame('BYTEA', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 9999999]));
public function getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 'BYTEA';
}
public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType() : void
......
......@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\Exception\ColumnLengthRequired;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Schema\Column;
......@@ -238,11 +239,6 @@ class DB2PlatformTest extends AbstractPlatformTestCase
'autoincrement' => true,
];
self::assertEquals('VARCHAR(255)', $this->platform->getVarcharTypeDeclarationSQL([]));
self::assertEquals('VARCHAR(10)', $this->platform->getVarcharTypeDeclarationSQL(['length' => 10]));
self::assertEquals('CHAR(254)', $this->platform->getVarcharTypeDeclarationSQL(['fixed' => true]));
self::assertEquals('CHAR(10)', $this->platform->getVarcharTypeDeclarationSQL($fullColumnDef));
self::assertEquals('SMALLINT', $this->platform->getSmallIntTypeDeclarationSQL([]));
self::assertEquals('SMALLINT', $this->platform->getSmallIntTypeDeclarationSQL(['unsigned' => true]));
self::assertEquals('SMALLINT GENERATED BY DEFAULT AS IDENTITY', $this->platform->getSmallIntTypeDeclarationSQL($fullColumnDef));
......@@ -426,24 +422,38 @@ class DB2PlatformTest extends AbstractPlatformTestCase
self::assertSame('COL', $this->platform->getSQLResultCasing('cOl'));
}
protected function getBinaryDefaultLength() : int
public function testGetVariableLengthStringTypeDeclarationSQLNoLength() : void
{
$this->expectException(ColumnLengthRequired::class);
parent::testGetVariableLengthStringTypeDeclarationSQLNoLength();
}
public function getExpectedFixedLengthBinaryTypeDeclarationSQLNoLength() : string
{
return 1;
return 'CHAR FOR BIT DATA';
}
protected function getBinaryMaxLength() : int
public function getExpectedFixedLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 32704;
return 'CHAR(16) FOR BIT DATA';
}
public function testReturnsBinaryTypeDeclarationSQL() : void
public function getExpectedVariableLengthBinaryTypeDeclarationSQLNoLength() : string
{
self::assertSame('VARCHAR(1) FOR BIT DATA', $this->platform->getBinaryTypeDeclarationSQL([]));
self::assertSame('VARCHAR(255) FOR BIT DATA', $this->platform->getBinaryTypeDeclarationSQL(['length' => 0]));
self::assertSame('VARCHAR(32704) FOR BIT DATA', $this->platform->getBinaryTypeDeclarationSQL(['length' => 32704]));
return 'CHAR(16) FOR BIT DATA';
}
self::assertSame('CHAR(1) FOR BIT DATA', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
self::assertSame('CHAR(254) FOR BIT DATA', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 0]));
public function testGetVariableLengthBinaryTypeDeclarationSQLNoLength() : void
{
$this->expectException(ColumnLengthRequired::class);
parent::testGetVariableLengthBinaryTypeDeclarationSQLNoLength();
}
public function getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 'VARCHAR(16) FOR BIT DATA';
}
/**
......
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\ColumnLengthRequired;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Column;
......@@ -180,26 +181,6 @@ class OraclePlatformTest extends AbstractPlatformTestCase
);
}
public function testGeneratesTypeDeclarationsForStrings() : void
{
self::assertEquals(
'CHAR(10)',
$this->platform->getVarcharTypeDeclarationSQL(
['length' => 10, 'fixed' => true]
)
);
self::assertEquals(
'VARCHAR2(50)',
$this->platform->getVarcharTypeDeclarationSQL(['length' => 50]),
'Variable string declaration is not correct'
);
self::assertEquals(
'VARCHAR2(255)',
$this->platform->getVarcharTypeDeclarationSQL([]),
'Long string declaration is not correct'
);
}
public function testPrefersIdentityColumns() : void
{
self::assertFalse($this->platform->prefersIdentityColumns());
......@@ -321,7 +302,8 @@ class OraclePlatformTest extends AbstractPlatformTestCase
$columnName = strtoupper('id' . uniqid());
$tableName = strtoupper('table' . uniqid());
$table = new Table($tableName);
$column = $table->addColumn($columnName, 'integer');
$column = $table->addColumn($columnName, 'integer');
$column->setAutoincrement(true);
$targets = [
sprintf('CREATE TABLE %s (%s NUMBER(10) NOT NULL)', $tableName, $columnName),
......@@ -455,31 +437,45 @@ class OraclePlatformTest extends AbstractPlatformTestCase
*/
public function testAlterTableNotNULL() : void
{
$tableDiff = new TableDiff('mytable');
$tableDiff->changedColumns['foo'] = new ColumnDiff(
$tableDiff = new TableDiff('mytable');
$tableDiff->changedColumns['foo'] = new ColumnDiff(
'foo',
new Column(
'foo',
Type::getType('string'),
['default' => 'bla', 'notnull' => true]
[
'length' => 255,
'default' => 'bla',
'notnull' => true,
]
),
['type']
);
$tableDiff->changedColumns['bar'] = new ColumnDiff(
$tableDiff->changedColumns['bar'] = new ColumnDiff(
'bar',
new Column(
'baz',
Type::getType('string'),
['default' => 'bla', 'notnull' => true]
[
'length' => 255,
'default' => 'bla',
'notnull' => true,
]
),
['type', 'notnull']
);
$tableDiff->changedColumns['metar'] = new ColumnDiff(
'metar',
new Column(
'metar',
Type::getType('string'),
['length' => 2000, 'notnull' => false]
[
'length' => 2000,
'notnull' => false,
]
),
['notnull']
);
......@@ -503,26 +499,40 @@ class OraclePlatformTest extends AbstractPlatformTestCase
self::assertSame('date', $this->platform->getDoctrineTypeMapping('date'));
}
protected function getBinaryMaxLength() : int
public function testGetVariableLengthStringTypeDeclarationSQLNoLength() : void
{
$this->expectException(ColumnLengthRequired::class);
parent::testGetVariableLengthStringTypeDeclarationSQLNoLength();
}
protected function getExpectedVariableLengthStringTypeDeclarationSQLWithLength() : string
{
return 2000;
return 'VARCHAR2(16)';
}
public function testReturnsBinaryTypeDeclarationSQL() : void
public function testGetFixedLengthBinaryTypeDeclarationSQLNoLength() : void
{
self::assertSame('RAW(255)', $this->platform->getBinaryTypeDeclarationSQL([]));
self::assertSame('RAW(2000)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 0]));
self::assertSame('RAW(2000)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 2000]));
$this->expectException(ColumnLengthRequired::class);
parent::testGetFixedLengthBinaryTypeDeclarationSQLNoLength();
}
self::assertSame('RAW(255)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
self::assertSame('RAW(2000)', $this->platform->getBinaryTypeDeclarationSQL([
'fixed' => true,
'length' => 0,
]));
self::assertSame('RAW(2000)', $this->platform->getBinaryTypeDeclarationSQL([
'fixed' => true,
'length' => 2000,
]));
public function getExpectedFixedLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 'RAW(16)';
}
public function testGetVariableLengthBinaryTypeDeclarationSQLNoLength() : void
{
$this->expectException(ColumnLengthRequired::class);
parent::testGetVariableLengthBinaryTypeDeclarationSQLNoLength();
}
public function getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 'RAW(16)';
}
public function testDoesNotPropagateUnnecessaryTableAlterationOnBinaryType() : void
......
......@@ -28,7 +28,7 @@ class PostgreSqlPlatformTest extends AbstractPostgreSqlPlatformTestCase
$table->addOption('comment', 'foo');
self::assertSame(
[
'CREATE TABLE foo (id VARCHAR(255) NOT NULL)',
'CREATE TABLE foo (id VARCHAR NOT NULL)',
"COMMENT ON TABLE foo IS 'foo'",
],
$this->platform->getCreateTableSQL($table),
......
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Doctrine\Tests\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\ColumnLengthRequired;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
......@@ -42,7 +43,7 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
public function getGenerateAlterTableSql() : array
{
return [
"ALTER TABLE mytable ADD quota INT DEFAULT NULL, DROP foo, ALTER baz VARCHAR(1) DEFAULT 'def' NOT NULL, ALTER bloo BIT DEFAULT '0' NOT NULL",
"ALTER TABLE mytable ADD quota INT DEFAULT NULL, DROP foo, ALTER baz VARCHAR(255) DEFAULT 'def' NOT NULL, ALTER bloo BIT DEFAULT '0' NOT NULL",
'ALTER TABLE mytable RENAME userlist',
];
}
......@@ -310,9 +311,6 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
self::assertEquals('DATETIME', $this->platform->getDateTimeTypeDeclarationSQL($fullColumnDef));
self::assertEquals('TIME', $this->platform->getTimeTypeDeclarationSQL($fullColumnDef));
self::assertEquals('UNIQUEIDENTIFIER', $this->platform->getGuidTypeDeclarationSQL($fullColumnDef));
self::assertEquals(1, $this->platform->getVarcharDefaultLength());
self::assertEquals(32767, $this->platform->getVarcharMaxLength());
}
public function testHasNativeGuidType() : void
......@@ -929,31 +927,18 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
self::assertSame('binary', $this->platform->getDoctrineTypeMapping('varbinary'));
}
protected function getBinaryDefaultLength() : int
public function testGetVariableLengthStringTypeDeclarationSQLNoLength() : void
{
return 1;
}
$this->expectException(ColumnLengthRequired::class);
protected function getBinaryMaxLength() : int
{
return 32767;
parent::testGetVariableLengthStringTypeDeclarationSQLNoLength();
}
public function testReturnsBinaryTypeDeclarationSQL() : void
public function testGetVariableLengthBinaryTypeDeclarationSQLNoLength() : void
{
self::assertSame('VARBINARY(1)', $this->platform->getBinaryTypeDeclarationSQL([]));
self::assertSame('VARBINARY(1)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 0]));
self::assertSame('VARBINARY(32767)', $this->platform->getBinaryTypeDeclarationSQL(['length' => 32767]));
$this->expectException(ColumnLengthRequired::class);
self::assertSame('BINARY(1)', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
self::assertSame('BINARY(1)', $this->platform->getBinaryTypeDeclarationSQL([
'fixed' => true,
'length' => 0,
]));
self::assertSame('BINARY(32767)', $this->platform->getBinaryTypeDeclarationSQL([
'fixed' => true,
'length' => 32767,
]));
parent::testGetVariableLengthBinaryTypeDeclarationSQLNoLength();
}
/**
......
......@@ -238,26 +238,6 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
);
}
public function testGeneratesTypeDeclarationForStrings() : void
{
self::assertEquals(
'CHAR(10)',
$this->platform->getVarcharTypeDeclarationSQL(
['length' => 10, 'fixed' => true]
)
);
self::assertEquals(
'VARCHAR(50)',
$this->platform->getVarcharTypeDeclarationSQL(['length' => 50]),
'Variable string declaration is not correct'
);
self::assertEquals(
'VARCHAR(255)',
$this->platform->getVarcharTypeDeclarationSQL([]),
'Long string declaration is not correct'
);
}
public function getGenerateIndexSql() : string
{
return 'CREATE INDEX my_idx ON mytable (user_name, last_login)';
......@@ -493,25 +473,24 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
];
}
protected function getBinaryDefaultLength() : int
public function getExpectedFixedLengthBinaryTypeDeclarationSQLNoLength() : string
{
return 0;
return 'BLOB';
}
protected function getBinaryMaxLength() : int
public function getExpectedFixedLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 0;
return 'BLOB';
}
public function testReturnsBinaryTypeDeclarationSQL() : void
public function getExpectedVariableLengthBinaryTypeDeclarationSQLNoLength() : string
{
self::assertSame('BLOB', $this->platform->getBinaryTypeDeclarationSQL([]));
self::assertSame('BLOB', $this->platform->getBinaryTypeDeclarationSQL(['length' => 0]));
self::assertSame('BLOB', $this->platform->getBinaryTypeDeclarationSQL(['length' => 9999999]));
return 'BLOB';
}
self::assertSame('BLOB', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true]));
self::assertSame('BLOB', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 0]));
self::assertSame('BLOB', $this->platform->getBinaryTypeDeclarationSQL(['fixed' => true, 'length' => 9999999]));
public function getExpectedVariableLengthBinaryTypeDeclarationSQLWithLength() : string
{
return 'BLOB';
}
/**
......@@ -817,8 +796,8 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
public function testGetCreateTableSQLWithColumnCollation() : void
{
$table = new Table('foo');
$table->addColumn('no_collation', 'string');
$table->addColumn('column_collation', 'string')->setPlatformOption('collation', 'NOCASE');
$table->addColumn('no_collation', 'string', ['length' => 255]);
$table->addColumn('column_collation', 'string', ['length' => 255])->setPlatformOption('collation', 'NOCASE');
self::assertSame(
['CREATE TABLE foo (no_collation VARCHAR(255) NOT NULL, column_collation VARCHAR(255) NOT NULL COLLATE NOCASE)'],
......
......@@ -45,13 +45,39 @@ class BinaryTest extends DbalTestCase
self::assertSame(Types::BINARY, $this->type->getName());
}
public function testReturnsSQLDeclaration() : void
/**
* @param mixed[][] $definition
*
* @dataProvider definitionProvider()
*/
public function testReturnsSQLDeclaration(array $definition, string $expectedDeclaration) : void
{
$this->platform->expects($this->once())
->method('getBinaryTypeDeclarationSQL')
->willReturn('TEST_BINARY');
$platform = $this->getMockForAbstractClass(AbstractPlatform::class);
self::assertSame($expectedDeclaration, $this->type->getSQLDeclaration($definition, $platform));
}
self::assertSame('TEST_BINARY', $this->type->getSQLDeclaration([], $this->platform));
/**
* @return mixed[][]
*/
public static function definitionProvider() : iterable
{
return [
'fixed-unspecified-length' => [
['fixed' => true],
'BINARY',
],
'fixed-specified-length' => [
[
'fixed' => true,
'length' => 20,
],
'BINARY(20)',
],
'variable-length' => [
['length' => 20],
'VARBINARY(20)',
],
];
}
public function testBinaryNullConvertsToPHPValue() : void
......
......@@ -24,10 +24,10 @@ class StringTest extends DbalTestCase
$this->type = Type::getType('string');
}
public function testReturnsSqlDeclarationFromPlatformVarchar() : void
public function testReturnsSQLDeclaration() : void
{
$this->platform->expects($this->once())
->method('getVarcharTypeDeclarationSQL')
->method('getStringTypeDeclarationSQL')
->willReturn('TEST_VARCHAR');
self::assertEquals('TEST_VARCHAR', $this->type->getSqlDeclaration([], $this->platform));
......
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