Commit a172949b authored by Benjamin Eberlei's avatar Benjamin Eberlei

DBAL-41 - Add float support

parent 22ecf03f
......@@ -85,6 +85,74 @@ abstract class AbstractPlatform
*/
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.
*
......@@ -141,13 +209,6 @@ abstract class AbstractPlatform
return isset($this->doctrineTypeMapping[$dbType]);
}
/**
* Lazy load Doctrine Type Mappings
*
* @return void
*/
abstract protected function initializeDoctrineTypeMappings();
/**
* Gets the character used for identifier quoting.
*
......@@ -1090,46 +1151,6 @@ abstract class AbstractPlatform
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
* declaration to be used in statements like CREATE TABLE.
......@@ -1703,6 +1724,11 @@ abstract class AbstractPlatform
throw DBALException::notSupported(__METHOD__);
}
public function getFloatDeclarationSQL(array $fieldDeclaration)
{
return 'DOUBLE PRECISION';
}
/**
* Gets the default transaction isolation level of the platform.
*
......@@ -1905,27 +1931,6 @@ abstract class AbstractPlatform
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.
......
......@@ -653,32 +653,34 @@ DROP DATABASE ' . $name . ';';
protected function initializeDoctrineTypeMappings()
{
$this->doctrineTypeMapping = array(
'bigint' => 'bigint',
'numeric' => 'decimal',
'bit' => 'boolean',
'smallint' => 'smallint',
'decimal' => 'decimal',
'smallmoney' => 'integer',
'int' => 'integer',
'tinyint' => 'smallint',
'money' => 'integer',
'float' => 'decimal',
'real' => 'decimal',
'date' => 'date',
'datetimeoffset' => 'datetimetz',
'datetime2' => 'datetime',
'smalldatetime' => 'datetime',
'datetime' => 'datetime',
'time' => 'time',
'char' => 'string',
'varchar' => 'string',
'text' => 'text',
'nchar' => 'string',
'nvarchar' => 'string',
'ntext' => 'text',
'binary' => 'text',
'varbinary' => 'text',
'image' => 'text',
'bigint' => 'bigint',
'numeric' => 'decimal',
'bit' => 'boolean',
'smallint' => 'smallint',
'decimal' => 'decimal',
'smallmoney' => 'integer',
'int' => 'integer',
'tinyint' => 'smallint',
'money' => 'integer',
'float' => 'float',
'real' => 'float',
'double' => 'float',
'double precision' => 'float',
'date' => 'date',
'datetimeoffset' => 'datetimetz',
'datetime2' => 'datetime',
'smalldatetime' => 'datetime',
'datetime' => 'datetime',
'time' => 'time',
'char' => 'string',
'varchar' => 'string',
'text' => 'text',
'nchar' => 'string',
'nvarchar' => 'string',
'ntext' => 'text',
'binary' => 'text',
'varbinary' => 'text',
'image' => 'text',
);
}
......
......@@ -670,41 +670,41 @@ class PostgreSqlPlatform extends AbstractPlatform
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' => 'datetimetz',
'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',
'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' => 'datetimetz',
'time' => 'time',
'timetz' => 'time',
'float' => 'float',
'float4' => 'float',
'float8' => 'float',
'double' => 'float',
'double precision' => 'float',
'real' => 'float',
'decimal' => 'decimal',
'money' => 'decimal',
'numeric' => 'decimal',
'year' => 'date',
);
}
}
......@@ -464,9 +464,9 @@ class SqlitePlatform extends AbstractPlatform
'datetime' => 'datetime',
'timestamp' => 'datetime',
'time' => 'time',
'float' => 'decimal',
'double' => 'decimal',
'real' => 'decimal',
'float' => 'float',
'double' => 'float',
'real' => 'float',
'decimal' => 'decimal',
'numeric' => 'decimal',
);
......
<?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
......
<?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
const SMALLINT = 'smallint';
const STRING = 'string';
const TEXT = 'text';
const FLOAT = 'float';
/** Map of already instantiated type objects. One instance per type (flyweight). */
private static $_typeObjects = array();
......@@ -65,7 +66,8 @@ abstract class Type
self::DATETIMETZ => 'Doctrine\DBAL\Types\DateTimeTzType',
self::DATE => 'Doctrine\DBAL\Types\DateType',
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. */
......
......@@ -43,6 +43,7 @@ class AllTests
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\IntegerTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\SmallIntTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\StringTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\FloatTest');
// Schema tests
$suite->addTestSuite('Doctrine\Tests\DBAL\Schema\ColumnTest');
......
......@@ -32,6 +32,8 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase
$table->addColumn('test_text', 'text', array('notnull' => false));
$table->addColumn('test_array', 'array', 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'));
$sm->createTable($table);
......@@ -57,6 +59,8 @@ class TypeConversionTest extends \Doctrine\Tests\DbalFunctionalTestCase
array('text', str_repeat('foo ', 1000), 'string'),
array('array', array('foo' => 'bar'), 'array'),
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