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
c32ada27
Commit
c32ada27
authored
Mar 27, 2011
by
Benjamin Eberlei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DDC-1014] Add Date Diff, Add and Sub Support for MySQL, SQLite, PostgreSQL and Oracle.
parent
14e9bf62
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
123 additions
and
0 deletions
+123
-0
MySqlPlatform.php
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
+25
-0
OraclePlatform.php
lib/Doctrine/DBAL/Platforms/OraclePlatform.php
+25
-0
PostgreSqlPlatform.php
lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
+25
-0
SqlitePlatform.php
lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
+25
-0
DataAccessTest.php
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
+23
-0
No files found.
lib/Doctrine/DBAL/Platforms/MySqlPlatform.php
View file @
c32ada27
...
...
@@ -99,6 +99,31 @@ class MySqlPlatform extends AbstractPlatform
return
'CONCAT('
.
join
(
', '
,
(
array
)
$args
)
.
')'
;
}
public
function
getDateDiffExpression
(
$date1
,
$date2
)
{
return
'DATEDIFF('
.
$date1
.
', '
.
$date2
.
')'
;
}
public
function
getDateAddDaysExpression
(
$date
,
$days
)
{
return
'DATE_ADD('
.
$date
.
', INTERVAL '
.
(
int
)
$days
.
' DAY)'
;
}
public
function
getDateSubDaysExpression
(
$date
,
$days
)
{
return
'DATE_SUB('
.
$date
.
', INTERVAL '
.
(
int
)
$days
.
' DAY)'
;
}
public
function
getDateAddMonthExpression
(
$date
,
$months
)
{
return
'DATE_ADD('
.
$date
.
', INTERVAL '
.
(
int
)
$months
.
' MONTH)'
;
}
public
function
getDateSubMonthExpression
(
$date
,
$months
)
{
return
'DATE_SUB('
.
$date
.
', INTERVAL '
.
(
int
)
$months
.
' MONTH)'
;
}
public
function
getListDatabasesSQL
()
{
return
'SHOW DATABASES'
;
...
...
lib/Doctrine/DBAL/Platforms/OraclePlatform.php
View file @
c32ada27
...
...
@@ -99,6 +99,31 @@ class OraclePlatform extends AbstractPlatform
{
return
'SYS_GUID()'
;
}
public
function
getDateDiffExpression
(
$date1
,
$date2
)
{
return
'('
.
$date1
.
'-'
.
$date2
.
')'
;
}
public
function
getDateAddDaysExpression
(
$date
,
$days
)
{
return
'('
.
$date
.
'+'
.
(
int
)
$days
.
')'
;
}
public
function
getDateSubDaysExpression
(
$date
,
$days
)
{
return
'('
.
$date
.
'-'
.
(
int
)
$days
.
')'
;
}
public
function
getDateAddMonthExpression
(
$date
,
$months
)
{
return
"ADD_MONTHS("
.
$date
.
", "
.
(
int
)
$months
.
")"
;
}
public
function
getDateSubMonthExpression
(
$date
,
$months
)
{
return
"ADD_MONTHS("
.
$date
.
", -"
.
(
int
)
$months
.
")"
;
}
/**
* Gets the SQL used to create a sequence that starts with a given value
...
...
lib/Doctrine/DBAL/Platforms/PostgreSqlPlatform.php
View file @
c32ada27
...
...
@@ -91,6 +91,31 @@ class PostgreSqlPlatform extends AbstractPlatform
return
'POSITION('
.
$substr
.
' IN '
.
$str
.
')'
;
}
}
public
function
getDateDiffExpression
(
$date1
,
$date2
)
{
return
'('
.
$date1
.
'-'
.
$date2
.
')'
;
}
public
function
getDateAddDaysExpression
(
$date
,
$days
)
{
return
"("
.
$date
.
"+ interval '"
.
(
int
)
$days
.
" day')"
;
}
public
function
getDateSubDaysExpression
(
$date
,
$days
)
{
return
"("
.
$date
.
"- interval '"
.
(
int
)
$days
.
" day')"
;
}
public
function
getDateAddMonthExpression
(
$date
,
$months
)
{
return
"("
.
$date
.
"+ interval '"
.
(
int
)
$months
.
" month')"
;
}
public
function
getDateSubMonthExpression
(
$date
,
$months
)
{
return
"("
.
$date
.
"- interval '"
.
(
int
)
$months
.
" month')"
;
}
/**
* parses a literal boolean value and returns
...
...
lib/Doctrine/DBAL/Platforms/SqlitePlatform.php
View file @
c32ada27
...
...
@@ -125,6 +125,31 @@ class SqlitePlatform extends AbstractPlatform
}
}
public
function
getDateDiffExpression
(
$date1
,
$date2
)
{
return
'ROUND(JULIANDAY('
.
$date1
.
')-JULIANDAY('
.
$date2
.
'))'
;
}
public
function
getDateAddDaysExpression
(
$date
,
$days
)
{
return
"DATE("
.
$date
.
",'+"
.
(
int
)
$days
.
" day')"
;
}
public
function
getDateSubDaysExpression
(
$date
,
$days
)
{
return
"DATE("
.
$date
.
",'-"
.
(
int
)
$days
.
" day')"
;
}
public
function
getDateAddMonthExpression
(
$date
,
$months
)
{
return
"DATE("
.
$date
.
",'+"
.
(
int
)
$months
.
" month')"
;
}
public
function
getDateSubMonthExpression
(
$date
,
$months
)
{
return
"DATE("
.
$date
.
",'-"
.
(
int
)
$months
.
" month')"
;
}
protected
function
_getTransactionIsolationLevelSQL
(
$level
)
{
switch
(
$level
)
{
...
...
tests/Doctrine/Tests/DBAL/Functional/DataAccessTest.php
View file @
c32ada27
...
...
@@ -244,4 +244,27 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$this
->
assertEquals
(
5
,
count
(
$data
));
$this
->
assertEquals
(
array
(
array
(
100
),
array
(
101
),
array
(
102
),
array
(
103
),
array
(
104
)),
$data
);
}
/**
* @group DDC-1014
*/
public
function
testDateArithmetics
()
{
$p
=
$this
->
_conn
->
getDatabasePlatform
();
$sql
=
'SELECT '
;
$sql
.=
$p
->
getDateDiffExpression
(
'test_datetime'
,
"'2010-12-24 12:00:00'"
)
.
' AS diff, '
;
$sql
.=
$p
->
getDateAddDaysExpression
(
'test_datetime'
,
10
)
.
' AS add_days, '
;
$sql
.=
$p
->
getDateSubDaysExpression
(
'test_datetime'
,
10
)
.
' AS sub_days, '
;
$sql
.=
$p
->
getDateAddMonthExpression
(
'test_datetime'
,
2
)
.
' AS add_month, '
;
$sql
.=
$p
->
getDateSubMonthExpression
(
'test_datetime'
,
2
)
.
' AS sub_month '
;
$sql
.=
'FROM fetch_table'
;
$row
=
$this
->
_conn
->
fetchAssoc
(
$sql
);
$this
->
assertEquals
(
-
357
,
(
int
)
$row
[
'diff'
],
"Date difference should be -356 days."
);
$this
->
assertEquals
(
'2010-01-11'
,
date
(
'Y-m-d'
,
strtotime
(
$row
[
'add_days'
])),
"Adding date should end up on 2010-01-11"
);
$this
->
assertEquals
(
'2009-12-22'
,
date
(
'Y-m-d'
,
strtotime
(
$row
[
'sub_days'
])),
"Subtracting date should end up on 2009-12-22"
);
$this
->
assertEquals
(
'2010-03-01'
,
date
(
'Y-m-d'
,
strtotime
(
$row
[
'add_month'
])),
"Adding month should end up on 2010-03-01"
);
$this
->
assertEquals
(
'2009-11-01'
,
date
(
'Y-m-d'
,
strtotime
(
$row
[
'sub_month'
])),
"Adding month should end up on 2009-11-01"
);
}
}
\ No newline at end of file
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