Skip to content

Commit

Permalink
Merge branch 'DBAL-33'
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Jul 24, 2010
2 parents 6794793 + a1e4c86 commit 835c9a0
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public function getDateTypeDeclarationSQL(array $fieldDeclaration)
*/
public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
{
return 'TIME';
return 'TIME(0) WITHOUT TIME ZONE';
}

/**
Expand Down
60 changes: 60 additions & 0 deletions lib/Doctrine/DBAL/Types/VarDateTimeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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;

/**
* Variable DateTime Type using date_create() instead of DateTime::createFromFormat()
*
* This type has performance implications as it runs twice as long as the regular
* {@see DateTimeType}, however in certain PostgreSQL configurations with
* TIMESTAMP(n) columns where n > 0 it is necessary to use this type.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.com
* @since 2.0
* @author Benjamin Eberlei <kontakt@beberlei.de>
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
*/
class VarDateTimeType extends DateTimeType
{
/**
* @throws ConversionException
* @param string $value
* @param AbstractPlatform $platform
* @return DateTime
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null) {
return null;
}

$val = date_create($value);
if (!$val) {
throw ConversionException::conversionFailed($value, $this->getName());
}
return $val;
}
}
1 change: 1 addition & 0 deletions tests/Doctrine/Tests/DBAL/AllTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public static function suite()
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\ObjectTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTimeTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTimeTzTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\VarDateTimeTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\DateTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\TimeTest');
$suite->addTestSuite('Doctrine\Tests\DBAL\Types\BooleanTest');
Expand Down
62 changes: 62 additions & 0 deletions tests/Doctrine/Tests/DBAL/Types/VarDateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Doctrine\Tests\DBAL\Types;

use Doctrine\DBAL\Types\Type;
use Doctrine\Tests\DBAL\Mocks;

require_once __DIR__ . '/../../TestInit.php';

class VarDateTimeTest extends \Doctrine\Tests\DbalTestCase
{
protected
$_platform,
$_type;

protected function setUp()
{
$this->_platform = new \Doctrine\Tests\DBAL\Mocks\MockPlatform();
if (!Type::hasType('vardatetime')) {
Type::addType('vardatetime', 'Doctrine\DBAL\Types\VarDateTimeType');
}
$this->_type = Type::getType('vardatetime');
}

public function testDateTimeConvertsToDatabaseValue()
{
$date = new \DateTime('1985-09-01 10:10:10');

$expected = $date->format($this->_platform->getDateTimeTzFormatString());
$actual = is_string($this->_type->convertToDatabaseValue($date, $this->_platform));

$this->assertEquals($expected, $actual);
}

public function testDateTimeConvertsToPHPValue()
{
// Birthday of jwage and also birthday of Doctrine. Send him a present ;)
$date = $this->_type->convertToPHPValue('1985-09-01 00:00:00', $this->_platform);
$this->assertType('DateTime', $date);
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
$this->assertEquals('000000', $date->format('u'));
}

public function testInvalidDateTimeFormatConversion()
{
$this->setExpectedException('Doctrine\DBAL\Types\ConversionException');
$this->_type->convertToPHPValue('abcdefg', $this->_platform);
}

public function testConversionWithMicroseconds()
{
$date = $this->_type->convertToPHPValue('1985-09-01 00:00:00.123456', $this->_platform);
$this->assertType('DateTime', $date);
$this->assertEquals('1985-09-01 00:00:00', $date->format('Y-m-d H:i:s'));
$this->assertEquals('123456', $date->format('u'));
}

public function testNullConversion()
{
$this->assertNull($this->_type->convertToPHPValue(null, $this->_platform));
}
}

0 comments on commit 835c9a0

Please sign in to comment.