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
*
* @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.
......
......@@ -48,19 +48,8 @@ class DB2Platform extends AbstractPlatform
*
* @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)')
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
}
......
......@@ -516,20 +516,9 @@ class MsSqlPlatform extends AbstractPlatform
}
/** @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 ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NTEXT');
return $fixed ? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NVARCHAR(255)');
}
/** @override */
......
......@@ -152,19 +152,8 @@ class MySqlPlatform extends AbstractPlatform
*
* @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)')
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
}
......@@ -599,4 +588,9 @@ class MySqlPlatform extends AbstractPlatform
'year' => 'date',
);
}
public function getVarcharMaxLength()
{
return 65535;
}
}
......@@ -234,19 +234,8 @@ class OraclePlatform extends AbstractPlatform
* @params array $field
* @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)')
: ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
}
......
......@@ -591,21 +591,10 @@ class PostgreSqlPlatform extends AbstractPlatform
* @params array $field
* @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)')
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
: ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
}
/** @override */
......@@ -707,4 +696,9 @@ class PostgreSqlPlatform extends AbstractPlatform
'year' => 'date',
);
}
public function getVarcharMaxLength()
{
return 65535;
}
}
......@@ -300,18 +300,8 @@ class SqlitePlatform extends AbstractPlatform
/**
* {@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)')
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
}
......
......@@ -34,4 +34,8 @@ class MockPlatform extends \Doctrine\DBAL\Platforms\AbstractPlatform
}
protected function initializeDoctrineTypeMappings() {
}
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
{
}
}
......@@ -105,7 +105,7 @@ class MsSqlPlatformTest extends AbstractPlatformTestCase
'Variable string declaration is not correct'
);
$this->assertEquals(
'NTEXT',
'NVARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct'
);
......
......@@ -127,7 +127,7 @@ class OraclePlatformTest extends AbstractPlatformTestCase
'Variable string declaration is not correct'
);
$this->assertEquals(
'VARCHAR2(4000)',
'VARCHAR2(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct'
);
......
......@@ -135,7 +135,7 @@ class PostgreSqlPlatformTest extends AbstractPlatformTestCase
'Variable string declaration is not correct'
);
$this->assertEquals(
'TEXT',
'VARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct'
);
......
......@@ -89,7 +89,7 @@ class SqlitePlatformTest extends AbstractPlatformTestCase
'Variable string declaration is not correct'
);
$this->assertEquals(
'TEXT',
'VARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct'
);
......
......@@ -84,4 +84,8 @@ class DatabasePlatformMock extends \Doctrine\DBAL\Platforms\AbstractPlatform
}
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