Commit b4d80abd authored by Guilherme Blanco's avatar Guilherme Blanco

Merge pull request #354 from makhov/master

Add hour to DATE_ADD and DATE_SUB
parents 92b61c75 7dcb6f93
...@@ -939,6 +939,36 @@ abstract class AbstractPlatform ...@@ -939,6 +939,36 @@ abstract class AbstractPlatform
throw DBALException::notSupported(__METHOD__); throw DBALException::notSupported(__METHOD__);
} }
/**
* Returns the SQL to add the number of given hours to a date.
*
* @param string $date
* @param integer $hours
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateAddHourExpression($date, $hours)
{
throw DBALException::notSupported(__METHOD__);
}
/**
* Returns the SQL to subtract the number of given hours to a date.
*
* @param string $date
* @param integer $hours
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateSubHourExpression($date, $hours)
{
throw DBALException::notSupported(__METHOD__);
}
/** /**
* Returns the SQL to add the number of given days to a date. * Returns the SQL to add the number of given days to a date.
* *
......
...@@ -65,6 +65,22 @@ class DrizzlePlatform extends AbstractPlatform ...@@ -65,6 +65,22 @@ class DrizzlePlatform extends AbstractPlatform
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')'; return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
} }
/**
* {@inheritDoc}
*/
public function getDateAddHourExpression($date, $hours)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
}
/**
* {@inheritDoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return 'DATE_SUB(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -115,6 +115,22 @@ class MySqlPlatform extends AbstractPlatform ...@@ -115,6 +115,22 @@ class MySqlPlatform extends AbstractPlatform
return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')'; return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
} }
/**
* {@inheritDoc}
*/
public function getDateAddHourExpression($date, $hours)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
}
/**
* {@inheritDoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return 'DATE_SUB(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -110,6 +110,22 @@ class OraclePlatform extends AbstractPlatform ...@@ -110,6 +110,22 @@ class OraclePlatform extends AbstractPlatform
return "TRUNC(TO_NUMBER(SUBSTR((" . $date1 . "-" . $date2 . "), 1, INSTR(" . $date1 . "-" . $date2 .", ' '))))"; return "TRUNC(TO_NUMBER(SUBSTR((" . $date1 . "-" . $date2 . "), 1, INSTR(" . $date1 . "-" . $date2 .", ' '))))";
} }
/**
* {@inheritDoc}
*/
public function getDateAddHourExpression($date, $hours)
{
return '(' . $date . '+' . $hours . '/24)';
}
/**
* {@inheritDoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return '(' . $date . '-' . $hours . '/24)';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -100,6 +100,22 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -100,6 +100,22 @@ class PostgreSqlPlatform extends AbstractPlatform
return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))'; return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))';
} }
/**
* {@inheritDoc}
*/
public function getDateAddHourExpression($date, $hours)
{
return "(" . $date ." + (" . $hours . " || ' hour')::interval)";
}
/**
* {@inheritDoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return "(" . $date ." - (" . $hours . " || ' hour')::interval)";
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -46,6 +46,22 @@ class SQLServerPlatform extends AbstractPlatform ...@@ -46,6 +46,22 @@ class SQLServerPlatform extends AbstractPlatform
return 'DATEDIFF(day, ' . $date2 . ',' . $date1 . ')'; return 'DATEDIFF(day, ' . $date2 . ',' . $date1 . ')';
} }
/**
* {@inheritDoc}
*/
public function getDateAddHourExpression($date, $hours)
{
return 'DATEADD(hour, ' . $hours . ', ' . $date . ')';
}
/**
* {@inheritDoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return 'DATEADD(hour, -1 * ' . $hours . ', ' . $date . ')';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -130,6 +130,22 @@ class SqlitePlatform extends AbstractPlatform ...@@ -130,6 +130,22 @@ class SqlitePlatform extends AbstractPlatform
return 'ROUND(JULIANDAY('.$date1 . ')-JULIANDAY('.$date2.'))'; return 'ROUND(JULIANDAY('.$date1 . ')-JULIANDAY('.$date2.'))';
} }
/**
* {@inheritDoc}
*/
public function getDateAddHourExpression($date, $hours)
{
return "DATETIME(" . $date . ",'+". $hours . " hour')";
}
/**
* {@inheritDoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return "DATETIME(" . $date . ",'-". $hours . " hour')";
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -348,6 +348,8 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -348,6 +348,8 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$p = $this->_conn->getDatabasePlatform(); $p = $this->_conn->getDatabasePlatform();
$sql = 'SELECT '; $sql = 'SELECT ';
$sql .= $p->getDateDiffExpression('test_datetime', $p->getCurrentTimestampSQL()) .' AS diff, '; $sql .= $p->getDateDiffExpression('test_datetime', $p->getCurrentTimestampSQL()) .' AS diff, ';
$sql .= $p->getDateAddHourExpression('test_datetime', 3) .' AS add_hour, ';
$sql .= $p->getDateSubHourExpression('test_datetime', 3) .' AS sub_hour, ';
$sql .= $p->getDateAddDaysExpression('test_datetime', 10) .' AS add_days, '; $sql .= $p->getDateAddDaysExpression('test_datetime', 10) .' AS add_days, ';
$sql .= $p->getDateSubDaysExpression('test_datetime', 10) .' AS sub_days, '; $sql .= $p->getDateSubDaysExpression('test_datetime', 10) .' AS sub_days, ';
$sql .= $p->getDateAddMonthExpression('test_datetime', 2) .' AS add_month, '; $sql .= $p->getDateAddMonthExpression('test_datetime', 2) .' AS add_month, ';
...@@ -359,6 +361,8 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -359,6 +361,8 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase
$diff = floor( (strtotime('2010-01-01')-time()) / 3600 / 24); $diff = floor( (strtotime('2010-01-01')-time()) / 3600 / 24);
$this->assertEquals($diff, (int)$row['diff'], "Date difference should be approx. ".$diff." days.", 1); $this->assertEquals($diff, (int)$row['diff'], "Date difference should be approx. ".$diff." days.", 1);
$this->assertEquals('2010-01-01 13:10', date('Y-m-d H:i', strtotime($row['add_hour'])), "Adding date should end up on 2010-01-01 13:10");
$this->assertEquals('2010-01-01 07:10', date('Y-m-d H:i', strtotime($row['sub_hour'])), "Subtracting date should end up on 2010-01-01 07:10");
$this->assertEquals('2010-01-11', date('Y-m-d', strtotime($row['add_days'])), "Adding date should end up on 2010-01-11"); $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('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('2010-03-01', date('Y-m-d', strtotime($row['add_month'])), "Adding month should end up on 2010-03-01");
......
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