Permalink
Browse files

DBAL-84 - Fix VARCHARs being silently set to 255 chars when larger th…

…an Maxlength. Now they are converted to Text/CLOB fields automatically. Refactored the VARCHAR code considerably.
  • Loading branch information...
1 parent 168fc6b commit e57e92e1c0ee7e7949c5e82236a5bb96973ab044 @beberlei beberlei committed Feb 20, 2011
View
20 lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
@@ -147,7 +147,25 @@ public function __construct() {}
*
* @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.
View
13 lib/Doctrine/DBAL/Platforms/DB2Platform.php
@@ -48,19 +48,8 @@ public function initializeDoctrineTypeMappings()
*
* @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)');
}
View
15 lib/Doctrine/DBAL/Platforms/MsSqlPlatform.php
@@ -516,20 +516,9 @@ public function getSmallIntTypeDeclarationSQL(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 ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NTEXT');
+ return $fixed ? ($length ? 'NCHAR(' . $length . ')' : 'CHAR(255)') : ($length ? 'NVARCHAR(' . $length . ')' : 'NVARCHAR(255)');
}
/** @override */
View
18 lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
@@ -152,19 +152,8 @@ public function getDropViewSQL($name)
*
* @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)');
}
@@ -617,4 +606,9 @@ protected function initializeDoctrineTypeMappings()
'year' => 'date',
);
}
+
+ public function getVarcharMaxLength()
+ {
+ return 65535;
+ }
}
View
13 lib/Doctrine/DBAL/Platforms/OraclePlatform.php
@@ -234,19 +234,8 @@ protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
* @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)');
}
View
20 lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
@@ -604,21 +604,10 @@ protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
* @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 */
@@ -720,4 +709,9 @@ protected function initializeDoctrineTypeMappings()
'year' => 'date',
);
}
+
+ public function getVarcharMaxLength()
+ {
+ return 65535;
+ }
}
View
12 lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
@@ -300,18 +300,8 @@ protected function _getCreateTableSQL($name, array $columns, array $options = ar
/**
* {@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');
}
View
4 tests/Doctrine/Tests/DBAL/Mocks/MockPlatform.php
@@ -34,4 +34,8 @@ public function getName()
}
protected function initializeDoctrineTypeMappings() {
}
+ protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
+ {
+
+ }
}
View
2 tests/Doctrine/Tests/DBAL/Platforms/MsSqlPlatformTest.php
@@ -105,7 +105,7 @@ public function testGeneratesTypeDeclarationsForStrings()
'Variable string declaration is not correct'
);
$this->assertEquals(
- 'NTEXT',
+ 'NVARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct'
);
View
2 tests/Doctrine/Tests/DBAL/Platforms/OraclePlatformTest.php
@@ -127,7 +127,7 @@ public function testGeneratesTypeDeclarationsForStrings()
'Variable string declaration is not correct'
);
$this->assertEquals(
- 'VARCHAR2(4000)',
+ 'VARCHAR2(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct'
);
View
2 tests/Doctrine/Tests/DBAL/Platforms/PostgreSqlPlatformTest.php
@@ -135,7 +135,7 @@ public function testGeneratesTypeDeclarationForStrings()
'Variable string declaration is not correct'
);
$this->assertEquals(
- 'TEXT',
+ 'VARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct'
);
View
2 tests/Doctrine/Tests/DBAL/Platforms/SqlitePlatformTest.php
@@ -89,7 +89,7 @@ public function testGeneratesTypeDeclarationForStrings()
'Variable string declaration is not correct'
);
$this->assertEquals(
- 'TEXT',
+ 'VARCHAR(255)',
$this->_platform->getVarcharTypeDeclarationSQL(array()),
'Long string declaration is not correct'
);
View
4 tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php
@@ -84,4 +84,8 @@ public function getName()
}
protected function initializeDoctrineTypeMappings() {
}
+ protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
+ {
+
+ }
}

0 comments on commit e57e92e

Please sign in to comment.