Commit c32ada27 authored by Benjamin Eberlei's avatar Benjamin Eberlei

[DDC-1014] Add Date Diff, Add and Sub Support for MySQL, SQLite, PostgreSQL and Oracle.

parent 14e9bf62
......@@ -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';
......
......@@ -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
......
......@@ -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
......
......@@ -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) {
......
......@@ -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
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