Commit ab2b3999 authored by jwage's avatar jwage

[2.0] Adding date and time types. Fixing CURRENT_DATE, CURRENT_TIMESTAMP and CURRENT_TIME functions

parent cc59161b
......@@ -1256,7 +1256,15 @@ abstract class AbstractPlatform
return 'CURRENT_TIME';
}
/**
* Gets the SQL specific for the platform to get the current timestamp
*
* @return string
*/
public function getCurrentTimestampSql()
{
return 'CURRENT_TIMESTAMP';
}
/**
* Get sql for transaction isolation level Connection constant
......@@ -1504,6 +1512,16 @@ abstract class AbstractPlatform
return 'Y-m-d H:i:s';
}
public function getDateFormatString()
{
return 'Y-m-d';
}
public function getTimeFormatString()
{
return 'H:i:s';
}
/**
* Gets the SQL snippet used to declare a VARCHAR column type.
*
......
<?php
namespace Doctrine\DBAL\Types;
/**
* Type that maps an SQL DATETIME to a PHP Date object.
*
* @since 2.0
*/
class DateType extends Type
{
public function getName()
{
return 'Date';
}
/**
* {@inheritdoc}
*/
public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
return $platform->getDateTypeDeclarationSql($fieldDeclaration);
}
/**
* {@inheritdoc}
*
* @override
*/
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
return $value->format($platform->getDateFormatString());
}
/**
* {@inheritdoc}
*
* @override
*/
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
return \DateTime::createFromFormat($platform->getDateFormatString(), $value);
}
}
\ No newline at end of file
<?php
namespace Doctrine\DBAL\Types;
/**
* Type that maps an SQL DATETIME to a PHP Date object.
*
* @since 2.0
*/
class TimeType extends Type
{
public function getName()
{
return 'Time';
}
/**
* {@inheritdoc}
*/
public function getSqlDeclaration(array $fieldDeclaration, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
return $platform->getTimeTypeDeclarationSql($fieldDeclaration);
}
/**
* {@inheritdoc}
*
* @override
*/
public function convertToDatabaseValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
return $value->format($platform->getTimeFormatString());
}
/**
* {@inheritdoc}
*
* @override
*/
public function convertToPHPValue($value, \Doctrine\DBAL\Platforms\AbstractPlatform $platform)
{
return \DateTime::createFromFormat($platform->getTimeFormatString(), $value);
}
}
\ No newline at end of file
......@@ -33,6 +33,8 @@ abstract class Type
'string' => 'Doctrine\DBAL\Types\StringType',
'text' => 'Doctrine\DBAL\Types\TextType',
'datetime' => 'Doctrine\DBAL\Types\DateTimeType',
'date' => 'Doctrine\DBAL\Types\DateType',
'time' => 'Doctrine\DBAL\Types\TimeType',
'decimal' => 'Doctrine\DBAL\Types\DecimalType',
'double' => 'Doctrine\DBAL\Types\DoubleType'
);
......
......@@ -28,6 +28,7 @@ class CurrentTimeFunction extends FunctionNode
{
$lexer = $parser->getLexer();
$parser->match($lexer->lookahead['value']);
$parser->match('(');
$parser->match(')');
}
}
}
\ No newline at end of file
......@@ -18,8 +18,7 @@ class CurrentTimestampFunction extends FunctionNode
*/
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
//TODO: Use platform to get SQL
return 'CURRENT_TIMESTAMP';
return $sqlWalker->getConnection()->getDatabasePlatform()->getCurrentTimestampSql();
}
/**
......@@ -29,6 +28,7 @@ class CurrentTimestampFunction extends FunctionNode
{
$lexer = $parser->getLexer();
$parser->match($lexer->lookahead['value']);
$parser->match('(');
$parser->match(')');
}
}
}
\ No newline at end of file
......@@ -23,12 +23,16 @@ class AllTests
{
$suite = new \Doctrine\Tests\DbalTestSuite('Doctrine DBAL');
// Platform tests
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\SqlitePlatformTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\MySqlPlatformTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\PostgreSqlPlatformTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\MsSqlPlatformTest');
// Type tests
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTimeTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\TimeTest');
$suite->addTest(Functional\AllTests::suite());
......
<?php
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks;
require_once __DIR__ . '/../../TestInit.php';
class DateTest extends \Doctrine\Tests\DbalTestCase
{
protected
$_platform,
$_type;
protected function setUp()
{
$this->_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
$this->_type = Type::getType('date');
}
public function testDateConvertsToDatabaseValue()
{
$this->assertTrue(
is_string($this->_type->convertToDatabaseValue(new \DateTime(), $this->_platform))
);
}
public function testDateConvertsToPHPValue()
{
// Birthday of jwage and also birthday of Doctrine. Send him a present ;)
$this->assertTrue(
$this->_type->convertToPHPValue('1985-09-01', $this->_platform)
instanceof \DateTime
);
}
}
\ No newline at end of file
<?php
namespace Doctrine\Tests\DBAL\Types;
use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks;
require_once __DIR__ . '/../../TestInit.php';
class TimeTest extends \Doctrine\Tests\DbalTestCase
{
protected
$_platform,
$_type;
protected function setUp()
{
$this->_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
$this->_type = Type::getType('time');
}
public function testTimeConvertsToDatabaseValue()
{
$this->assertTrue(
is_string($this->_type->convertToDatabaseValue(new \DateTime(), $this->_platform))
);
}
public function testTimeConvertsToPHPValue()
{
$this->assertTrue(
$this->_type->convertToPHPValue('5:30:55', $this->_platform)
instanceof \DateTime
);
}
}
\ No newline at end of file
......@@ -17,4 +17,12 @@ class DateTimeModel
* @Column(type="datetime")
*/
public $datetime;
/**
* @Column(type="date")
*/
public $date;
/**
* @Column(type="time")
*/
public $time;
}
\ No newline at end of file
......@@ -287,6 +287,18 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
$this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.datetime > CURRENT_DATE', $q->getSql());
}
public function testCurrentTimeFunction()
{
$q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.time > current_time()');
$this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.time > CURRENT_TIME', $q->getSql());
}
public function testCurrentTimestampFunction()
{
$q = $this->_em->createQuery('SELECT d.id FROM Doctrine\Tests\Models\Generic\DateTimeModel d WHERE d.datetime > current_timestamp()');
$this->assertEquals('SELECT d0_.id AS id0 FROM date_time_model d0_ WHERE d0_.datetime > CURRENT_TIMESTAMP', $q->getSql());
}
/*public function testExistsExpressionInWhereCorrelatedSubqueryAssocCondition()
{
$this->assertSqlGeneration(
......
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