Commit 626a334e authored by beberlei's avatar beberlei Committed by Benjamin Eberlei

Added registerDoctrineMappingType() and getDoctrineMappingType() methods,...

Added registerDoctrineMappingType() and getDoctrineMappingType() methods, converted DB2 platform and added some tests, made all platforms work for now

Support Sqlite

Migrated MySQL to Doctrine Type Map

Fixed nasty bug in testcase

Migrated Postgres to support Doctrine Mapping Types

Refactored Oracle Platform to support Doctrine Mapping Types

Refactored DB2 support for Doctrine Mapping Types a little bit
parent 8b5f4ccd
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -42,6 +40,7 @@ use Doctrine\DBAL\DBALException,
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @todo Remove any unnecessary methods.
*/
abstract class AbstractPlatform
......@@ -76,11 +75,63 @@ abstract class AbstractPlatform
*/
const TRIM_BOTH = 3;
/**
* @var array
*/
protected $doctrineTypeMapping = null;
/**
* Constructor.
*/
public function __construct() {}
/**
* Register a doctrine type to be used in conjunction with a column type of this platform.
*
* @param string $dbType
* @param string $doctrineType
*/
public function registerDoctrineTypeMapping($dbType, $doctrineType)
{
if ($this->doctrineTypeMapping === null) {
$this->initializeDoctrineTypeMappings();
}
if (!Types\Type::hasType($doctrineType)) {
throw DBALException::typeNotFound($doctrineType);
}
$dbType = strtolower($dbType);
$this->doctrineTypeMapping[$dbType] = $doctrineType;
}
/**
* Get the Doctrine type that is mapped for the given database column type.
*
* @param string $dbType
* @return string
*/
public function getDoctrineTypeMapping($dbType)
{
if ($this->doctrineTypeMapping === null) {
$this->initializeDoctrineTypeMappings();
}
$dbType = strtolower($dbType);
if (isset($this->doctrineTypeMapping[$dbType])) {
return $this->doctrineTypeMapping[$dbType];
} else {
throw new \Doctrine\DBAL\DBALException("Unknown database type ".$dbType." requested, " . get_class($this) . " may not support it.");
}
}
/**
* Lazy load Doctrine Type Mappings
*
* @return void
*/
abstract protected function initializeDoctrineTypeMappings();
/**
* Gets the character used for identifier quoting.
*
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -27,6 +25,24 @@ use Doctrine\DBAL\Schema\TableDiff;
class DB2Platform extends AbstractPlatform
{
public function initializeDoctrineTypeMappings()
{
$this->doctrineTypeMapping = array(
'smallint' => 'smallint',
'bigint' => 'bigint',
'integer' => 'integer',
'time' => 'time',
'date' => 'date',
'varchar' => 'string',
'character' => 'string',
'clob' => 'text',
'decimal' => 'decimal',
'double' => 'decimal',
'real' => 'decimal',
'timestamp' => 'datetime',
);
}
/**
* Gets the SQL snippet used to declare a VARCHAR column type.
*
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -511,4 +509,9 @@ class MsSqlPlatform extends AbstractPlatform
return $fromClause;
}
}
protected function initializeDoctrineTypeMappings()
{
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -588,4 +586,33 @@ class MySqlPlatform extends AbstractPlatform
{
return 'LOCK IN SHARE MODE';
}
protected function initializeDoctrineTypeMappings()
{
$this->doctrineTypeMapping = array(
'tinyint' => 'boolean',
'smallint' => 'smallint',
'mediumint' => 'integer',
'int' => 'integer',
'integer' => 'integer',
'bigint' => 'bigint',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'text' => 'text',
'varchar' => 'string',
'string' => 'string',
'char' => 'string',
'date' => 'date',
'datetime' => 'datetime',
'timestamp' => 'datetime',
'time' => 'time',
'float' => 'decimal',
'double' => 'decimal',
'real' => 'decimal',
'decimal' => 'decimal',
'numeric' => 'decimal',
'year' => 'date',
);
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -650,4 +648,27 @@ LEFT JOIN all_cons_columns r_cols
{
return 'TRUNCATE TABLE '.$tableName;
}
protected function initializeDoctrineTypeMappings()
{
$this->doctrineTypeMapping = array(
'integer' => 'integer',
'number' => 'integer',
'pls_integer' => 'boolean',
'binary_integer' => 'boolean',
'varchar' => 'string',
'varchar2' => 'string',
'nvarchar2' => 'string',
'char' => 'string',
'nchar' => 'string',
'date' => 'datetime',
'timestamp' => 'datetime',
'float' => 'decimal',
'long' => 'string',
'clob' => 'text',
'nclob' => 'text',
'rowid' => 'string',
'urowid' => 'string'
);
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -642,4 +640,45 @@ class PostgreSqlPlatform extends AbstractPlatform
{
return 'FOR SHARE';
}
protected function initializeDoctrineTypeMappings()
{
$this->doctrineTypeMapping = array(
'smallint' => 'smallint',
'int2' => 'smallint',
'serial' => 'integer',
'serial4' => 'integer',
'int' => 'integer',
'int4' => 'integer',
'integer' => 'integer',
'bigserial' => 'bigint',
'serial8' => 'bigint',
'bigint' => 'bigint',
'int8' => 'bigint',
'bool' => 'boolean',
'boolean' => 'boolean',
'text' => 'text',
'varchar' => 'string',
'interval' => 'string',
'_varchar' => 'string',
'char' => 'string',
'bpchar' => 'string',
'date' => 'date',
'datetime' => 'datetime',
'timestamp' => 'datetime',
'timestamptz' => 'datetime',
'time' => 'time',
'timetz' => 'time',
'float' => 'decimal',
'float4' => 'decimal',
'float8' => 'decimal',
'double' => 'decimal',
'double precision' => 'decimal',
'real' => 'decimal',
'decimal' => 'decimal',
'money' => 'decimal',
'numeric' => 'decimal',
'year' => 'date',
);
}
}
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -433,4 +431,39 @@ class SqlitePlatform extends AbstractPlatform
{
return '';
}
protected function initializeDoctrineTypeMappings()
{
$this->doctrineTypeMapping = array(
'boolean' => 'boolean',
'tinyint' => 'boolean',
'smallint' => 'smallint',
'mediumint' => 'integer',
'int' => 'integer',
'integer' => 'integer',
'serial' => 'integer',
'bigint' => 'bigint',
'bigserial' => 'bigint',
'clob' => 'text',
'tinytext' => 'text',
'mediumtext' => 'text',
'longtext' => 'text',
'text' => 'text',
'varchar' => 'string',
'varchar2' => 'string',
'nvarchar' => 'string',
'image' => 'string',
'ntext' => 'string',
'char' => 'string',
'date' => 'date',
'datetime' => 'datetime',
'timestamp' => 'datetime',
'time' => 'time',
'float' => 'decimal',
'double' => 'decimal',
'real' => 'decimal',
'decimal' => 'decimal',
'numeric' => 'decimal',
);
}
}
......@@ -66,41 +66,27 @@ class DB2SchemaManager extends AbstractSchemaManager
$unsigned = false;
$scale = false;
$precision = false;
$type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']);
switch (strtolower($tableColumn['typename'])) {
case 'smallint':
case 'bigint':
case 'integer':
case 'time':
case 'date':
$type = strtolower($tableColumn['typename']);
break;
case 'varchar':
$type = 'string';
$length = $tableColumn['length'];
$fixed = false;
break;
case 'character':
$type = 'string';
$length = $tableColumn['length'];
$fixed = true;
break;
case 'clob':
$type = 'text';
$length = $tableColumn['length'];
break;
case 'decimal':
case 'double':
case 'real':
$type = 'decimal';
$scale = $tableColumn['scale'];
$precision = $tableColumn['length'];
break;
case 'timestamp':
$type = 'datetime';
break;
default:
throw new \Doctrine\DBAL\DBALException("Unknown Type: ".$tableColumn['typename']);
}
$options = array(
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -89,9 +87,6 @@ class MySqlSchemaManager extends AbstractSchemaManager
{
$dbType = strtolower($tableColumn['Type']);
$dbType = strtok($dbType, '(), ');
if ($dbType == 'national') {
$dbType = strtok('(), ');
}
if (isset($tableColumn['length'])) {
$length = $tableColumn['length'];
$decimal = '';
......@@ -109,70 +104,10 @@ class MySqlSchemaManager extends AbstractSchemaManager
$scale = null;
$precision = null;
// Map db type to Doctrine mapping type
$type = $this->_platform->getDoctrineTypeMapping($dbType);
switch ($dbType) {
case 'tinyint':
$type = 'boolean';
$length = null;
break;
case 'smallint':
$type = 'smallint';
$length = null;
break;
case 'mediumint':
$type = 'integer';
$length = null;
break;
case 'int':
case 'integer':
$type = 'integer';
$length = null;
break;
case 'bigint':
$type = 'bigint';
$length = null;
break;
case 'tinytext':
case 'mediumtext':
case 'longtext':
case 'text':
$type = 'text';
$fixed = false;
break;
case 'varchar':
$fixed = false;
case 'string':
case 'char':
$type = 'string';
if ($length == '1') {
$type = 'boolean';
if (preg_match('/^(is|has)/', $tableColumn['name'])) {
$type = array_reverse($type);
}
} else if (strstr($dbType, 'text')) {
$type = 'text';
if ($decimal == 'binary') {
$type = 'blob';
}
}
if ($fixed !== false) {
$fixed = true;
}
break;
case 'set':
$fixed = false;
$type = 'text';
$type = 'integer'; //FIXME:???
break;
case 'date':
$type = 'date';
break;
case 'datetime':
case 'timestamp':
$type = 'datetime';
break;
case 'time':
$type = 'time';
$fixed = true;
break;
case 'float':
case 'double':
......@@ -184,36 +119,22 @@ class MySqlSchemaManager extends AbstractSchemaManager
$scale = $match[2];
$length = null;
}
$type = 'decimal';
break;
case 'tinyint':
case 'smallint':
case 'mediumint':
case 'int':
case 'integer':
case 'bigint':
case 'tinyblob':
case 'mediumblob':
case 'longblob':
case 'blob':
case 'binary':
case 'varbinary':
$type = 'blob';
$length = null;
break;
case 'year':
$type = 'integer';
$type = 'date';
$length = null;
break;
case 'geometry':
case 'geometrycollection':
case 'point':
case 'multipoint':
case 'linestring':
case 'multilinestring':
case 'polygon':
case 'multipolygon':
$type = 'blob';
$length = null;
break;
default:
$type = 'string';
$length = null;
}
$length = ((int) $length == 0) ? null : (int) $length;
......
......@@ -111,68 +111,57 @@ class OracleSchemaManager extends AbstractSchemaManager
$precision = null;
$scale = null;
$type = $this->_platform->getDoctrineTypeMapping($dbType);
switch ($dbType) {
case 'integer':
case 'number':
// @todo this sucks for the mapping stuff, is this deterministic maybe?
if($tableColumn['data_scale'] > 0) {
$type = 'decimal';
$precision = $tableColumn['data_precision'];
$scale = $tableColumn['data_scale'];
} else {
$type = 'integer';
if ($precision == 0 && $scale == 1) {
$type = 'boolean';
} else {
$type = 'decimal';
}
}
$length = null;
break;
case 'pls_integer':
case 'binary_integer':
$type = 'boolean';
$length = null;
break;
case 'varchar':
case 'varchar2':
case 'nvarchar2':
$fixed = false;
break;
case 'char':
case 'nchar':
if ($length == '1' && preg_match('/^(is|has)/', $tableColumn['column_name'])) {
$type = 'boolean';
} else {
$type = 'string';
}
if ($fixed !== false) {
$fixed = true;
}
$fixed = true;
break;
case 'date':
case 'timestamp':
$type = 'datetime';
$length = null;
break;
case 'float':
$precision = $tableColumn['data_precision'];
$scale = $tableColumn['data_scale'];
$type = 'decimal';
$length = null;
break;
case 'long':
$type = 'string';
case 'clob':
case 'nclob':
$length = null;
$type = 'text';
break;
case 'blob':
case 'raw':
case 'long raw':
case 'bfile':
$type = 'blob';
$length = null;
break;
case 'rowid':
case 'urowid':
default:
$type = 'string';
$length = null;
}
......
......@@ -199,11 +199,11 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
$dbType = strtolower($tableColumn['type']);
$type = $this->_platform->getDoctrineTypeMapping($dbType);
$autoincrement = false;
switch ($dbType) {
case 'smallint':
case 'int2':
$type = 'smallint';
$length = null;
break;
case 'serial':
......@@ -213,7 +213,6 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
case 'int':
case 'int4':
case 'integer':
$type = 'integer';
$length = null;
break;
case 'bigserial':
......@@ -222,52 +221,23 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
// break missing intentionally
case 'bigint':
case 'int8':
$type = 'bigint';
$length = null;
break;
case 'bool':
case 'boolean':
$type = 'boolean';
$length = null;
break;
case 'text':
$fixed = false;
$type = 'text';
break;
case 'varchar':
case 'interval':
case '_varchar':
$fixed = false;
case 'tsvector':
case 'unknown':
break;
case 'char':
case 'bpchar':
$type = 'string';
if ($length == '1') {
if (preg_match('/^(is|has)/', $tableColumn['name'])) {
$type = 'boolean';
}
} elseif (strstr($dbType, 'text')) {
$type = 'text';
}
if ($fixed !== false) {
$fixed = true;
}
break;
case 'date':
$type = 'date';
$length = null;
break;
case 'datetime':
case 'timestamp':
case 'timetz':
case 'timestamptz':
$type = 'datetime';
$length = null;
break;
case 'time':
$type = 'time';
$length = null;
$fixed = true;
break;
case 'float':
case 'float4':
......@@ -283,34 +253,10 @@ class PostgreSqlSchemaManager extends AbstractSchemaManager
$scale = $match[2];
$length = null;
}
$type = 'decimal';
break;
case 'tinyblob':
case 'mediumblob':
case 'longblob':
case 'blob':
case 'bytea':
case 'geometry':
case 'geometrycollection':
case 'point':
case 'multipoint':
case 'linestring':
case 'multilinestring':
case 'polygon':
case 'multipolygon':
$type = 'blob';
$length = null;
break;
case 'oid':
$type = 'blob';
$length = null;
break;
case 'year':
$type = 'date';
$length = null;
break;
default:
$type = 'string';
}
$options = array(
......
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......@@ -131,11 +129,10 @@ class SqliteSchemaManager extends AbstractSchemaManager
}
$dbType = strtolower($tableColumn['type']);
$length = isset($tableColumn['length']) ? $tableColumn['length'] : null;
$unsigned = (boolean) isset($tableColumn['unsigned']) ? $tableColumn['unsigned'] : false;
$fixed = false;
$type = null;
$type = $this->_platform->getDoctrineTypeMapping($dbType);
$default = $tableColumn['dflt_value'];
if ($default == 'NULL') {
$default = null;
......@@ -150,72 +147,8 @@ class SqliteSchemaManager extends AbstractSchemaManager
$scale = null;
switch ($dbType) {
case 'boolean':
$type = 'boolean';
break;
case 'tinyint':
$type = 'boolean';
$length = null;
break;
case 'smallint':
$type = 'smallint';
$length = null;
break;
case 'mediumint':
case 'int':
case 'integer':
case 'serial':
$type = 'integer';
$length = null;
break;
case 'bigint':
case 'bigserial':
$type = 'bigint';
$length = null;
break;
case 'clob':
$fixed = false;
$type = 'text';
break;
case 'tinytext':
case 'mediumtext':
case 'longtext':
case 'text':
$type = 'text';
break;
case 'varchar':
case 'varchar2':
case 'nvarchar':
case 'ntext':
case 'image':
case 'nchar':
$fixed = false;
case 'char':
$type = 'string';
if ($length == '1') {
$type = 'boolean';
if (preg_match('/^(is|has)/', $tableColumn['name'])) {
$type = array_reverse($type);
}
} elseif (strstr($dbType, 'text')) {
$type = 'clob';
}
if ($fixed !== false) {
$fixed = true;
}
break;
case 'date':
$type = 'date';
$length = null;
break;
case 'datetime':
case 'timestamp':
$type = 'datetime';
$length = null;
break;
case 'time':
$type = 'time';
$length = null;
$fixed = true;
break;
case 'float':
case 'double':
......@@ -223,23 +156,8 @@ class SqliteSchemaManager extends AbstractSchemaManager
case 'decimal':
case 'numeric':
list($precision, $scale) = array_map('trim', explode(', ', $tableColumn['length']));
$type = 'decimal';
$length = null;
break;
case 'tinyblob':
case 'mediumblob':
case 'longblob':
case 'blob':
$type = 'blob';
$length = null;
break;
case 'year':
$type = 'date';
$length = null;
break;
default:
$type = 'string';
$length = null;
}
$options = array(
......
......@@ -32,4 +32,6 @@ class MockPlatform extends \Doctrine\DBAL\Platforms\AbstractPlatform
{
return 'mock';
}
protected function initializeDoctrineTypeMappings() {
}
}
......@@ -16,6 +16,24 @@ abstract class AbstractPlatformTestCase extends \Doctrine\Tests\DbalTestCase
$this->_platform = $this->createPlatform();
}
public function testGetUnknownDoctrineMappingType()
{
$this->setExpectedException('Doctrine\DBAL\DBALException');
$this->_platform->getDoctrineTypeMapping('foobar');
}
public function testRegisterDoctrineMappingType()
{
$this->_platform->registerDoctrineTypeMapping('foo', 'integer');
$this->assertEquals('integer', $this->_platform->getDoctrineTypeMapping('foo'));
}
public function testRegisterUnknownDoctrineMappingType()
{
$this->setExpectedException('Doctrine\DBAL\DBALException');
$this->_platform->registerDoctrineTypeMapping('foo', 'bar');
}
public function testCreateWithNoColumns()
{
$table = new \Doctrine\DBAL\Schema\Table('test');
......
......@@ -4,7 +4,11 @@ namespace Doctrine\Tests;
class DbalFunctionalTestCase extends DbalTestCase
{
/* Shared connection when a TestCase is run alone (outside of it's functional suite) */
/**
* Shared connection when a TestCase is run alone (outside of it's functional suite)
*
* @var Doctrine\DBAL\Connection
*/
private static $_sharedConn;
/**
......@@ -15,6 +19,9 @@ class DbalFunctionalTestCase extends DbalTestCase
protected function resetSharedConn()
{
$this->sharedFixture['conn'] = null;
if (self::$_sharedConn) {
self::$_sharedConn->close();
}
self::$_sharedConn = null;
}
......
......@@ -82,4 +82,6 @@ class DatabasePlatformMock extends \Doctrine\DBAL\Platforms\AbstractPlatform
{
return 'mock';
}
protected function initializeDoctrineTypeMappings() {
}
}
\ 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