[2.0] Adding date and time types. Fixing CURRENT_DATE, CURRENT_TIMESTAMP and CURRENT_TIME functions
This commit is contained in:
parent
cc59161bea
commit
ab2b3999c0
@ -1256,7 +1256,15 @@ abstract class AbstractPlatform
|
|||||||
return 'CURRENT_TIME';
|
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
|
* Get sql for transaction isolation level Connection constant
|
||||||
@ -1504,6 +1512,16 @@ abstract class AbstractPlatform
|
|||||||
return 'Y-m-d H:i:s';
|
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.
|
* Gets the SQL snippet used to declare a VARCHAR column type.
|
||||||
*
|
*
|
||||||
|
44
lib/Doctrine/DBAL/Types/DateType.php
Normal file
44
lib/Doctrine/DBAL/Types/DateType.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
44
lib/Doctrine/DBAL/Types/TimeType.php
Normal file
44
lib/Doctrine/DBAL/Types/TimeType.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?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);
|
||||||
|
}
|
||||||
|
}
|
@ -33,6 +33,8 @@ abstract class Type
|
|||||||
'string' => 'Doctrine\DBAL\Types\StringType',
|
'string' => 'Doctrine\DBAL\Types\StringType',
|
||||||
'text' => 'Doctrine\DBAL\Types\TextType',
|
'text' => 'Doctrine\DBAL\Types\TextType',
|
||||||
'datetime' => 'Doctrine\DBAL\Types\DateTimeType',
|
'datetime' => 'Doctrine\DBAL\Types\DateTimeType',
|
||||||
|
'date' => 'Doctrine\DBAL\Types\DateType',
|
||||||
|
'time' => 'Doctrine\DBAL\Types\TimeType',
|
||||||
'decimal' => 'Doctrine\DBAL\Types\DecimalType',
|
'decimal' => 'Doctrine\DBAL\Types\DecimalType',
|
||||||
'double' => 'Doctrine\DBAL\Types\DoubleType'
|
'double' => 'Doctrine\DBAL\Types\DoubleType'
|
||||||
);
|
);
|
||||||
|
@ -28,6 +28,7 @@ class CurrentTimeFunction extends FunctionNode
|
|||||||
{
|
{
|
||||||
$lexer = $parser->getLexer();
|
$lexer = $parser->getLexer();
|
||||||
$parser->match($lexer->lookahead['value']);
|
$parser->match($lexer->lookahead['value']);
|
||||||
|
$parser->match('(');
|
||||||
|
$parser->match(')');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,8 +18,7 @@ class CurrentTimestampFunction extends FunctionNode
|
|||||||
*/
|
*/
|
||||||
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
|
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
|
||||||
{
|
{
|
||||||
//TODO: Use platform to get SQL
|
return $sqlWalker->getConnection()->getDatabasePlatform()->getCurrentTimestampSql();
|
||||||
return 'CURRENT_TIMESTAMP';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,6 +28,7 @@ class CurrentTimestampFunction extends FunctionNode
|
|||||||
{
|
{
|
||||||
$lexer = $parser->getLexer();
|
$lexer = $parser->getLexer();
|
||||||
$parser->match($lexer->lookahead['value']);
|
$parser->match($lexer->lookahead['value']);
|
||||||
|
$parser->match('(');
|
||||||
|
$parser->match(')');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,12 +23,16 @@ class AllTests
|
|||||||
{
|
{
|
||||||
$suite = new \Doctrine\Tests\DbalTestSuite('Doctrine DBAL');
|
$suite = new \Doctrine\Tests\DbalTestSuite('Doctrine DBAL');
|
||||||
|
|
||||||
|
// Platform tests
|
||||||
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\SqlitePlatformTest');
|
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\SqlitePlatformTest');
|
||||||
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\MySqlPlatformTest');
|
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\MySqlPlatformTest');
|
||||||
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\PostgreSqlPlatformTest');
|
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\PostgreSqlPlatformTest');
|
||||||
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\MsSqlPlatformTest');
|
$suite->addTestSuite('Doctrine\Tests\DBAL\Platforms\MsSqlPlatformTest');
|
||||||
|
|
||||||
|
// Type tests
|
||||||
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTimeTest');
|
$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());
|
$suite->addTest(Functional\AllTests::suite());
|
||||||
|
|
||||||
|
37
tests/Doctrine/Tests/DBAL/Types/DateTest.php
Normal file
37
tests/Doctrine/Tests/DBAL/Types/DateTest.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
36
tests/Doctrine/Tests/DBAL/Types/TimeTest.php
Normal file
36
tests/Doctrine/Tests/DBAL/Types/TimeTest.php
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<?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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -17,4 +17,12 @@ class DateTimeModel
|
|||||||
* @Column(type="datetime")
|
* @Column(type="datetime")
|
||||||
*/
|
*/
|
||||||
public $datetime;
|
public $datetime;
|
||||||
|
/**
|
||||||
|
* @Column(type="date")
|
||||||
|
*/
|
||||||
|
public $date;
|
||||||
|
/**
|
||||||
|
* @Column(type="time")
|
||||||
|
*/
|
||||||
|
public $time;
|
||||||
}
|
}
|
@ -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());
|
$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()
|
/*public function testExistsExpressionInWhereCorrelatedSubqueryAssocCondition()
|
||||||
{
|
{
|
||||||
$this->assertSqlGeneration(
|
$this->assertSqlGeneration(
|
||||||
|
Loading…
Reference in New Issue
Block a user