Commit f1c282e7 authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-84 - Fix VARCHARs being silently set to 255 chars when larger than...

DBAL-84 - Fix VARCHARs being silently set to 255 chars when larger than Maxlength. Now they are converted to Text/CLOB fields automatically. Refactored the VARCHAR code considerably.
parent 8e5a76eb
...@@ -137,7 +137,25 @@ abstract class AbstractPlatform ...@@ -137,7 +137,25 @@ abstract class AbstractPlatform
* *
* @param array $field * @param array $field
*/ */
abstract public function getVarcharTypeDeclarationSQL(array $field); public function getVarcharTypeDeclarationSQL(array $field)
{
if ( !isset($field['length'])) {
$field['length'] = $this->getVarcharDefaultLength();
}
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
if ($field['length'] > $this->getVarcharMaxLength()) {
return $this->getClobTypeDeclarationSQL($field);
} else {
return $this->getVarcharTypeDeclarationSQLSnippet($field['length'], $fixed);
}
}
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{
throw DBALException::notSupported('VARCHARs not supported by Platform.');
}
/** /**
* Gets the SQL snippet used to declare a CLOB column type. * Gets the SQL snippet used to declare a CLOB column type.
......
...@@ -48,19 +48,8 @@ class DB2Platform extends AbstractPlatform ...@@ -48,19 +48,8 @@ class DB2Platform extends AbstractPlatform
* *
* @param array $field * @param array $field
*/ */
public function getVarcharTypeDeclarationSQL(array $field) protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{ {
if ( ! isset($field['length'])) {
if (array_key_exists('default', $field)) {
$field['length'] = $this->getVarcharDefaultLength();
} else {
$field['length'] = false;
}
}
$length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)'); : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
} }
......
...@@ -516,20 +516,9 @@ class MsSqlPlatform extends AbstractPlatform ...@@ -516,20 +516,9 @@ class MsSqlPlatform extends AbstractPlatform
} }
/** @override */ /** @override */
public function getVarcharTypeDeclarationSQL(array $field) protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{ {
if (!isset($field['length'])) { return $fixed ? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NVARCHAR(255)');
if (array_key_exists('default', $field)) {
$field['length'] = $this->getVarcharDefaultLength();
} else {
$field['length'] = false;
}
}
$length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
return $fixed ? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NTEXT');
} }
/** @override */ /** @override */
......
...@@ -152,19 +152,8 @@ class MySqlPlatform extends AbstractPlatform ...@@ -152,19 +152,8 @@ class MySqlPlatform extends AbstractPlatform
* *
* @params array $field * @params array $field
*/ */
public function getVarcharTypeDeclarationSQL(array $field) protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{ {
if ( ! isset($field['length'])) {
if (array_key_exists('default', $field)) {
$field['length'] = $this->getVarcharDefaultLength();
} else {
$field['length'] = false;
}
}
$length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)'); : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
} }
...@@ -599,4 +588,9 @@ class MySqlPlatform extends AbstractPlatform ...@@ -599,4 +588,9 @@ class MySqlPlatform extends AbstractPlatform
'year' => 'date', 'year' => 'date',
); );
} }
public function getVarcharMaxLength()
{
return 65535;
}
} }
...@@ -234,19 +234,8 @@ class OraclePlatform extends AbstractPlatform ...@@ -234,19 +234,8 @@ class OraclePlatform extends AbstractPlatform
* @params array $field * @params array $field
* @override * @override
*/ */
public function getVarcharTypeDeclarationSQL(array $field) protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{ {
if ( ! isset($field['length'])) {
if (array_key_exists('default', $field)) {
$field['length'] = $this->getVarcharDefaultLength();
} else {
$field['length'] = false;
}
}
$length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)') return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
: ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)'); : ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
} }
......
...@@ -591,21 +591,10 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -591,21 +591,10 @@ class PostgreSqlPlatform extends AbstractPlatform
* @params array $field * @params array $field
* @override * @override
*/ */
public function getVarcharTypeDeclarationSQL(array $field) protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{ {
if ( ! isset($field['length'])) {
if (array_key_exists('default', $field)) {
$field['length'] = $this->getVarcharDefaultLength();
} else {
$field['length'] = false;
}
}
$length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT'); : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
} }
/** @override */ /** @override */
...@@ -707,4 +696,9 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -707,4 +696,9 @@ class PostgreSqlPlatform extends AbstractPlatform
'year' => 'date', 'year' => 'date',
); );
} }
public function getVarcharMaxLength()
{
return 65535;
}
} }
...@@ -300,18 +300,8 @@ class SqlitePlatform extends AbstractPlatform ...@@ -300,18 +300,8 @@ class SqlitePlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getVarcharTypeDeclarationSQL(array $field) protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{ {
if ( ! isset($field['length'])) {
if (array_key_exists('default', $field)) {
$field['length'] = $this->getVarcharDefaultLength();
} else {
$field['length'] = false;
}
}
$length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)') return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT'); : ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
} }
......
...@@ -34,4 +34,8 @@ class MockPlatform extends \Doctrine\DBAL\Platforms\AbstractPlatform ...@@ -34,4 +34,8 @@ class MockPlatform extends \Doctrine\DBAL\Platforms\AbstractPlatform
} }
protected function initializeDoctrineTypeMappings() { protected function initializeDoctrineTypeMappings() {
} }
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{
}
} }
...@@ -105,7 +105,7 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase ...@@ -105,7 +105,7 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase
'Variable string declaration is not correct' 'Variable string declaration is not correct'
); );
$this->assertEquals( $this->assertEquals(
'NTEXT', 'NVARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()), $this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct' 'Long string declaration is not correct'
); );
......
...@@ -127,7 +127,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase ...@@ -127,7 +127,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase
'Variable string declaration is not correct' 'Variable string declaration is not correct'
); );
$this->assertEquals( $this->assertEquals(
'VARCHAR2(4000)', 'VARCHAR2(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()), $this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct' 'Long string declaration is not correct'
); );
......
...@@ -135,7 +135,7 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase ...@@ -135,7 +135,7 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
'Variable string declaration is not correct' 'Variable string declaration is not correct'
); );
$this->assertEquals( $this->assertEquals(
'TEXT', 'VARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()), $this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct' 'Long string declaration is not correct'
); );
......
...@@ -89,7 +89,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase ...@@ -89,7 +89,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'Variable string declaration is not correct' 'Variable string declaration is not correct'
); );
$this->assertEquals( $this->assertEquals(
'TEXT', 'VARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()), $this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct' 'Long string declaration is not correct'
); );
......
...@@ -84,4 +84,8 @@ class DatabasePlatformMock extends \Doctrine\DBAL\Platforms\AbstractPlatform ...@@ -84,4 +84,8 @@ class DatabasePlatformMock extends \Doctrine\DBAL\Platforms\AbstractPlatform
} }
protected function initializeDoctrineTypeMappings() { protected function initializeDoctrineTypeMappings() {
} }
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{
}
} }
\ No newline at end of file
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