Commit a172949b authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-41 - Add float support

parent 22ecf03f
...@@ -85,6 +85,74 @@ abstract class AbstractPlatform ...@@ -85,6 +85,74 @@ abstract class AbstractPlatform
*/ */
public function __construct() {} public function __construct() {}
/**
* Gets the SQL snippet that declares a boolean column.
*
* @param array $columnDef
* @return string
*/
abstract public function getBooleanTypeDeclarationSQL(array $columnDef);
/**
* Gets the SQL snippet that declares a 4 byte integer column.
*
* @param array $columnDef
* @return string
*/
abstract public function getIntegerTypeDeclarationSQL(array $columnDef);
/**
* Gets the SQL snippet that declares an 8 byte integer column.
*
* @param array $columnDef
* @return string
*/
abstract public function getBigIntTypeDeclarationSQL(array $columnDef);
/**
* Gets the SQL snippet that declares a 2 byte integer column.
*
* @param array $columnDef
* @return string
*/
abstract public function getSmallIntTypeDeclarationSQL(array $columnDef);
/**
* Gets the SQL snippet that declares common properties of an integer column.
*
* @param array $columnDef
* @return string
*/
abstract protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef);
/**
* Lazy load Doctrine Type Mappings
*
* @return void
*/
abstract protected function initializeDoctrineTypeMappings();
/**
* Gets the SQL snippet used to declare a VARCHAR column type.
*
* @param array $field
*/
abstract public function getVarcharTypeDeclarationSQL(array $field);
/**
* Gets the SQL snippet used to declare a CLOB column type.
*
* @param array $field
*/
abstract public function getClobTypeDeclarationSQL(array $field);
/**
* Gets the name of the platform.
*
* @return string
*/
abstract public function getName();
/** /**
* Register a doctrine type to be used in conjunction with a column type of this platform. * Register a doctrine type to be used in conjunction with a column type of this platform.
* *
...@@ -141,13 +209,6 @@ abstract class AbstractPlatform ...@@ -141,13 +209,6 @@ abstract class AbstractPlatform
return isset($this->doctrineTypeMapping[$dbType]); return isset($this->doctrineTypeMapping[$dbType]);
} }
/**
* Lazy load Doctrine Type Mappings
*
* @return void
*/
abstract protected function initializeDoctrineTypeMappings();
/** /**
* Gets the character used for identifier quoting. * Gets the character used for identifier quoting.
* *
...@@ -1090,46 +1151,6 @@ abstract class AbstractPlatform ...@@ -1090,46 +1151,6 @@ abstract class AbstractPlatform
return 'NUMERIC(' . $columnDef['precision'] . ', ' . $columnDef['scale'] . ')'; return 'NUMERIC(' . $columnDef['precision'] . ', ' . $columnDef['scale'] . ')';
} }
/**
* Gets the SQL snippet that declares a boolean column.
*
* @param array $columnDef
* @return string
*/
abstract public function getBooleanTypeDeclarationSQL(array $columnDef);
/**
* Gets the SQL snippet that declares a 4 byte integer column.
*
* @param array $columnDef
* @return string
*/
abstract public function getIntegerTypeDeclarationSQL(array $columnDef);
/**
* Gets the SQL snippet that declares an 8 byte integer column.
*
* @param array $columnDef
* @return string
*/
abstract public function getBigIntTypeDeclarationSQL(array $columnDef);
/**
* Gets the SQL snippet that declares a 2 byte integer column.
*
* @param array $columnDef
* @return string
*/
abstract public function getSmallIntTypeDeclarationSQL(array $columnDef);
/**
* Gets the SQL snippet that declares common properties of an integer column.
*
* @param array $columnDef
* @return string
*/
abstract protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef);
/** /**
* Obtain DBMS specific SQL code portion needed to set a default value * Obtain DBMS specific SQL code portion needed to set a default value
* declaration to be used in statements like CREATE TABLE. * declaration to be used in statements like CREATE TABLE.
...@@ -1703,6 +1724,11 @@ abstract class AbstractPlatform ...@@ -1703,6 +1724,11 @@ abstract class AbstractPlatform
throw DBALException::notSupported(__METHOD__); throw DBALException::notSupported(__METHOD__);
} }
public function getFloatDeclarationSQL(array $fieldDeclaration)
{
return 'DOUBLE PRECISION';
}
/** /**
* Gets the default transaction isolation level of the platform. * Gets the default transaction isolation level of the platform.
* *
...@@ -1905,27 +1931,6 @@ abstract class AbstractPlatform ...@@ -1905,27 +1931,6 @@ abstract class AbstractPlatform
return $query; return $query;
} }
/**
* Gets the SQL snippet used to declare a VARCHAR column type.
*
* @param array $field
*/
abstract public function getVarcharTypeDeclarationSQL(array $field);
/**
* Gets the SQL snippet used to declare a CLOB column type.
*
* @param array $field
*/
abstract public function getClobTypeDeclarationSQL(array $field);
/**
* Gets the name of the platform.
*
* @return string
*/
abstract public function getName();
/** /**
* Gets the character casing of a column in an SQL result set of this platform. * Gets the character casing of a column in an SQL result set of this platform.
......
...@@ -653,32 +653,34 @@ DROP DATABASE ' . $name . ';'; ...@@ -653,32 +653,34 @@ DROP DATABASE ' . $name . ';';
protected function initializeDoctrineTypeMappings() protected function initializeDoctrineTypeMappings()
{ {
$this->doctrineTypeMapping = array( $this->doctrineTypeMapping = array(
'bigint' => 'bigint', 'bigint' => 'bigint',
'numeric' => 'decimal', 'numeric' => 'decimal',
'bit' => 'boolean', 'bit' => 'boolean',
'smallint' => 'smallint', 'smallint' => 'smallint',
'decimal' => 'decimal', 'decimal' => 'decimal',
'smallmoney' => 'integer', 'smallmoney' => 'integer',
'int' => 'integer', 'int' => 'integer',
'tinyint' => 'smallint', 'tinyint' => 'smallint',
'money' => 'integer', 'money' => 'integer',
'float' => 'decimal', 'float' => 'float',
'real' => 'decimal', 'real' => 'float',
'date' => 'date', 'double' => 'float',
'datetimeoffset' => 'datetimetz', 'double precision' => 'float',
'datetime2' => 'datetime', 'date' => 'date',
'smalldatetime' => 'datetime', 'datetimeoffset' => 'datetimetz',
'datetime' => 'datetime', 'datetime2' => 'datetime',
'time' => 'time', 'smalldatetime' => 'datetime',
'char' => 'string', 'datetime' => 'datetime',
'varchar' => 'string', 'time' => 'time',
'text' => 'text', 'char' => 'string',
'nchar' => 'string', 'varchar' => 'string',
'nvarchar' => 'string', 'text' => 'text',
'ntext' => 'text', 'nchar' => 'string',
'binary' => 'text', 'nvarchar' => 'string',
'varbinary' => 'text', 'ntext' => 'text',
'image' => 'text', 'binary' => 'text',
'varbinary' => 'text',
'image' => 'text',
); );
} }
......
...@@ -670,41 +670,41 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -670,41 +670,41 @@ class PostgreSqlPlatform extends AbstractPlatform
protected function initializeDoctrineTypeMappings() protected function initializeDoctrineTypeMappings()
{ {
$this->doctrineTypeMapping = array( $this->doctrineTypeMapping = array(
'smallint' => 'smallint', 'smallint' => 'smallint',
'int2' => 'smallint', 'int2' => 'smallint',
'serial' => 'integer', 'serial' => 'integer',
'serial4' => 'integer', 'serial4' => 'integer',
'int' => 'integer', 'int' => 'integer',
'int4' => 'integer', 'int4' => 'integer',
'integer' => 'integer', 'integer' => 'integer',
'bigserial' => 'bigint', 'bigserial' => 'bigint',
'serial8' => 'bigint', 'serial8' => 'bigint',
'bigint' => 'bigint', 'bigint' => 'bigint',
'int8' => 'bigint', 'int8' => 'bigint',
'bool' => 'boolean', 'bool' => 'boolean',
'boolean' => 'boolean', 'boolean' => 'boolean',
'text' => 'text', 'text' => 'text',
'varchar' => 'string', 'varchar' => 'string',
'interval' => 'string', 'interval' => 'string',
'_varchar' => 'string', '_varchar' => 'string',
'char' => 'string', 'char' => 'string',
'bpchar' => 'string', 'bpchar' => 'string',
'date' => 'date', 'date' => 'date',
'datetime' => 'datetime', 'datetime' => 'datetime',
'timestamp' => 'datetime', 'timestamp' => 'datetime',
'timestamptz' => 'datetimetz', 'timestamptz' => 'datetimetz',
'time' => 'time', 'time' => 'time',
'timetz' => 'time', 'timetz' => 'time',
'float' => 'decimal', 'float' => 'float',
'float4' => 'decimal', 'float4' => 'float',
'float8' => 'decimal', 'float8' => 'float',
'double' => 'decimal', 'double' => 'float',
'double precision' => 'decimal', 'double precision' => 'float',
'real' => 'decimal', 'real' => 'float',
'decimal' => 'decimal', 'decimal' => 'decimal',
'money' => 'decimal', 'money' => 'decimal',
'numeric' => 'decimal', 'numeric' => 'decimal',
'year' => 'date', 'year' => 'date',
); );
} }
} }
...@@ -464,9 +464,9 @@ class SqlitePlatform extends AbstractPlatform ...@@ -464,9 +464,9 @@ class SqlitePlatform extends AbstractPlatform
'datetime' => 'datetime', 'datetime' => 'datetime',
'timestamp' => 'datetime', 'timestamp' => 'datetime',
'time' => 'time', 'time' => 'time',
'float' => 'decimal', 'float' => 'float',
'double' => 'decimal', 'double' => 'float',
'real' => 'decimal', 'real' => 'float',
'decimal' => 'decimal', 'decimal' => 'decimal',
'numeric' => 'decimal', 'numeric' => 'decimal',
); );
......
<?php <?php
/* /*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
......
<?php
/*
* 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
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
class FloatType extends Type
{
public function getName()
{
return Type::FLOAT;
}
/**
* @param array $fieldDeclaration
* @param AbstractPlatform $platform
* @return string
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getFloatDeclarationSQL($fieldDeclaration);
}
/**
* Converts a value from its database representation to its PHP representation
* of this type.
*
* @param mixed $value The value to convert.
* @param AbstractPlatform $platform The currently used database platform.
* @return mixed The PHP representation of the value.
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return (null === $value) ? null : (double) $value;
}
}
...@@ -47,6 +47,7 @@ abstract class Type ...@@ -47,6 +47,7 @@ abstract class Type
const SMALLINT = 'smallint'; const SMALLINT = 'smallint';
const STRING = 'string'; const STRING = 'string';
const TEXT = 'text'; const TEXT = 'text';
const FLOAT = 'float';
/** Map of already instantiated type objects. One instance per type (flyweight). */ /** Map of already instantiated type objects. One instance per type (flyweight). */
private static $_typeObjects = array(); private static $_typeObjects = array();
...@@ -65,7 +66,8 @@ abstract class Type ...@@ -65,7 +66,8 @@ abstract class Type
self::DATETIMETZ => 'Doctrine\DBAL\Types\DateTimeTzType', self::DATETIMETZ => 'Doctrine\DBAL\Types\DateTimeTzType',
self::DATE => 'Doctrine\DBAL\Types\DateType', self::DATE => 'Doctrine\DBAL\Types\DateType',
self::TIME => 'Doctrine\DBAL\Types\TimeType', self::TIME => 'Doctrine\DBAL\Types\TimeType',
self::DECIMAL => 'Doctrine\DBAL\Types\DecimalType' self::DECIMAL => 'Doctrine\DBAL\Types\DecimalType',
self::FLOAT => 'Doctrine\DBAL\Types\FloatType',
); );
/* Prevent instantiation and force use of the factory method. */ /* Prevent instantiation and force use of the factory method. */
......
...@@ -43,6 +43,7 @@ class AllTests ...@@ -43,6 +43,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\IntegerTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Types\IntegerTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\SmallIntTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Types\SmallIntTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\StringTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Types\StringTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\FloatTest');
// Schema tests // Schema tests
$suite->addTestSuite('Doctrine\Tests\DBAL\Schema\ColumnTest'); $suite->addTestSuite('Doctrine\Tests\DBAL\Schema\ColumnTest');
......
...@@ -32,6 +32,8 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -32,6 +32,8 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$table->addColumn('test_text', 'text', array('notnull' => false)); $table->addColumn('test_text', 'text', array('notnull' => false));
$table->addColumn('test_array', 'array', array('notnull' => false)); $table->addColumn('test_array', 'array', array('notnull' => false));
$table->addColumn('test_object', 'object', array('notnull' => false)); $table->addColumn('test_object', 'object', array('notnull' => false));
$table->addColumn('test_float', 'float', array('notnull' => false));
$table->addColumn('test_decimal', 'decimal', array('notnull' => false, 'scale' => 2, 'precision' => 10));
$table->setPrimaryKey(array('id')); $table->setPrimaryKey(array('id'));
$sm->createTable($table); $sm->createTable($table);
...@@ -57,6 +59,8 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -57,6 +59,8 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase
array('text', str_repeat('foo ', 1000), 'string'), array('text', str_repeat('foo ', 1000), 'string'),
array('array', array('foo' => 'bar'), 'array'), array('array', array('foo' => 'bar'), 'array'),
array('object', $obj, 'object'), array('object', $obj, 'object'),
array('float', 1.5, 'float'),
array('decimal', 1.55, 'float'),
); );
} }
......
<?php
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks;
require_once __DIR__ . '/../../TestInit.php';
class FloatTest extends \Doctrine\Tests\DbalTestCase
{
protected $_platform, $_type;
protected function setUp()
{
$this->_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
$this->_type = Type::getType('float');
}
public function testFloatConvertsToPHPValue()
{
$this->assertType('float', $this->_type->convertToPHPValue('5.5', $this->_platform));
}
public function testFloatNullConvertsToPHPValue()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
public function testFloatConvertToDatabaseValue()
{
$this->assertType('float', $this->_type->convertToDatabaseValue(5.5, $this->_platform));
}
public function testFloatNullConvertToDatabaseValue()
{
$this->assertNull($this->_type->convertToDatabaseValue(null, $this->_platform));
}
}
\ 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