Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
doctrine-dbal
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tomáš Trávníček
doctrine-dbal
Commits
ab2b3999
Commit
ab2b3999
authored
Jun 20, 2009
by
jwage
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[2.0] Adding date and time types. Fixing CURRENT_DATE, CURRENT_TIMESTAMP and CURRENT_TIME functions
parent
cc59161b
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
213 additions
and
7 deletions
+213
-7
AbstractPlatform.php
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
+19
-1
DateType.php
lib/Doctrine/DBAL/Types/DateType.php
+44
-0
TimeType.php
lib/Doctrine/DBAL/Types/TimeType.php
+44
-0
Type.php
lib/Doctrine/DBAL/Types/Type.php
+2
-0
CurrentTimeFunction.php
lib/Doctrine/ORM/Query/AST/Functions/CurrentTimeFunction.php
+3
-2
CurrentTimestampFunction.php
...rine/ORM/Query/AST/Functions/CurrentTimestampFunction.php
+4
-4
AllTests.php
tests/Doctrine/Tests/DBAL/AllTests.php
+4
-0
DateTest.php
tests/Doctrine/Tests/DBAL/Types/DateTest.php
+37
-0
TimeTest.php
tests/Doctrine/Tests/DBAL/Types/TimeTest.php
+36
-0
DateTimeModel.php
tests/Doctrine/Tests/Models/Generic/DateTimeModel.php
+8
-0
SelectSqlGenerationTest.php
tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
+12
-0
No files found.
lib/Doctrine/DBAL/Platforms/AbstractPlatform.php
View file @
ab2b3999
...
...
@@ -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.
*
...
...
lib/Doctrine/DBAL/Types/DateType.php
0 → 100644
View file @
ab2b3999
<?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
lib/Doctrine/DBAL/Types/TimeType.php
0 → 100644
View file @
ab2b3999
<?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
lib/Doctrine/DBAL/Types/Type.php
View file @
ab2b3999
...
...
@@ -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'
);
...
...
lib/Doctrine/ORM/Query/AST/Functions/CurrentTimeFunction.php
View file @
ab2b3999
...
...
@@ -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
lib/Doctrine/ORM/Query/AST/Functions/CurrentTimestampFunction.php
View file @
ab2b3999
...
...
@@ -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
tests/Doctrine/Tests/DBAL/AllTests.php
View file @
ab2b3999
...
...
@@ -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
());
...
...
tests/Doctrine/Tests/DBAL/Types/DateTest.php
0 → 100644
View file @
ab2b3999
<?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
tests/Doctrine/Tests/DBAL/Types/TimeTest.php
0 → 100644
View file @
ab2b3999
<?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
tests/Doctrine/Tests/Models/Generic/DateTimeModel.php
View file @
ab2b3999
...
...
@@ -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
tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php
View file @
ab2b3999
...
...
@@ -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(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment