Commit ce8f11f6 authored by Steve Müller's avatar Steve Müller

add date arithmetic interval methods for seconds, minutes, weeks, quarters and years

parent a0777077
...@@ -69,6 +69,46 @@ abstract class AbstractPlatform ...@@ -69,6 +69,46 @@ abstract class AbstractPlatform
*/ */
const CREATE_FOREIGNKEYS = 2; const CREATE_FOREIGNKEYS = 2;
/**
* @var string
*/
const DATE_INTERVAL_UNIT_SECOND = 'SECOND';
/**
* @var string
*/
const DATE_INTERVAL_UNIT_MINUTE = 'MINUTE';
/**
* @var string
*/
const DATE_INTERVAL_UNIT_HOUR = 'HOUR';
/**
* @var string
*/
const DATE_INTERVAL_UNIT_DAY = 'DAY';
/**
* @var string
*/
const DATE_INTERVAL_UNIT_WEEK = 'WEEK';
/**
* @var string
*/
const DATE_INTERVAL_UNIT_MONTH = 'MONTH';
/**
* @var string
*/
const DATE_INTERVAL_UNIT_QUARTER = 'QUARTER';
/**
* @var string
*/
const DATE_INTERVAL_UNIT_YEAR = 'YEAR';
/** /**
* @var integer * @var integer
*/ */
...@@ -988,6 +1028,66 @@ abstract class AbstractPlatform ...@@ -988,6 +1028,66 @@ abstract class AbstractPlatform
throw DBALException::notSupported(__METHOD__); throw DBALException::notSupported(__METHOD__);
} }
/**
* Returns the SQL to add the number of given seconds to a date.
*
* @param string $date
* @param integer $seconds
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateAddSecondsExpression($date, $seconds)
{
return $this->getDateArithmeticIntervalExpression($date, '+', $seconds, self::DATE_INTERVAL_UNIT_SECOND);
}
/**
* Returns the SQL to subtract the number of given seconds from a date.
*
* @param string $date
* @param integer $seconds
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateSubSecondsExpression($date, $seconds)
{
return $this->getDateArithmeticIntervalExpression($date, '-', $seconds, self::DATE_INTERVAL_UNIT_SECOND);
}
/**
* Returns the SQL to add the number of given minutes to a date.
*
* @param string $date
* @param integer $minutes
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateAddMinutesExpression($date, $minutes)
{
return $this->getDateArithmeticIntervalExpression($date, '+', $minutes, self::DATE_INTERVAL_UNIT_MINUTE);
}
/**
* Returns the SQL to subtract the number of given minutes from a date.
*
* @param string $date
* @param integer $minutes
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateSubMinutesExpression($date, $minutes)
{
return $this->getDateArithmeticIntervalExpression($date, '-', $minutes, self::DATE_INTERVAL_UNIT_MINUTE);
}
/** /**
* Returns the SQL to add the number of given hours to a date. * Returns the SQL to add the number of given hours to a date.
* *
...@@ -1000,7 +1100,7 @@ abstract class AbstractPlatform ...@@ -1000,7 +1100,7 @@ abstract class AbstractPlatform
*/ */
public function getDateAddHourExpression($date, $hours) public function getDateAddHourExpression($date, $hours)
{ {
throw DBALException::notSupported(__METHOD__); return $this->getDateArithmeticIntervalExpression($date, '+', $hours, self::DATE_INTERVAL_UNIT_HOUR);
} }
/** /**
...@@ -1015,7 +1115,7 @@ abstract class AbstractPlatform ...@@ -1015,7 +1115,7 @@ abstract class AbstractPlatform
*/ */
public function getDateSubHourExpression($date, $hours) public function getDateSubHourExpression($date, $hours)
{ {
throw DBALException::notSupported(__METHOD__); return $this->getDateArithmeticIntervalExpression($date, '-', $hours, self::DATE_INTERVAL_UNIT_HOUR);
} }
/** /**
...@@ -1030,7 +1130,7 @@ abstract class AbstractPlatform ...@@ -1030,7 +1130,7 @@ abstract class AbstractPlatform
*/ */
public function getDateAddDaysExpression($date, $days) public function getDateAddDaysExpression($date, $days)
{ {
throw DBALException::notSupported(__METHOD__); return $this->getDateArithmeticIntervalExpression($date, '+', $days, self::DATE_INTERVAL_UNIT_DAY);
} }
/** /**
...@@ -1045,7 +1145,37 @@ abstract class AbstractPlatform ...@@ -1045,7 +1145,37 @@ abstract class AbstractPlatform
*/ */
public function getDateSubDaysExpression($date, $days) public function getDateSubDaysExpression($date, $days)
{ {
throw DBALException::notSupported(__METHOD__); return $this->getDateArithmeticIntervalExpression($date, '-', $days, self::DATE_INTERVAL_UNIT_DAY);
}
/**
* Returns the SQL to add the number of given weeks to a date.
*
* @param string $date
* @param integer $weeks
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateAddWeeksExpression($date, $weeks)
{
return $this->getDateArithmeticIntervalExpression($date, '+', $weeks, self::DATE_INTERVAL_UNIT_WEEK);
}
/**
* Returns the SQL to subtract the number of given weeks from a date.
*
* @param string $date
* @param integer $weeks
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateSubWeeksExpression($date, $weeks)
{
return $this->getDateArithmeticIntervalExpression($date, '-', $weeks, self::DATE_INTERVAL_UNIT_WEEK);
} }
/** /**
...@@ -1060,7 +1190,7 @@ abstract class AbstractPlatform ...@@ -1060,7 +1190,7 @@ abstract class AbstractPlatform
*/ */
public function getDateAddMonthExpression($date, $months) public function getDateAddMonthExpression($date, $months)
{ {
throw DBALException::notSupported(__METHOD__); return $this->getDateArithmeticIntervalExpression($date, '+', $months, self::DATE_INTERVAL_UNIT_MONTH);
} }
/** /**
...@@ -1074,6 +1204,84 @@ abstract class AbstractPlatform ...@@ -1074,6 +1204,84 @@ abstract class AbstractPlatform
* @throws \Doctrine\DBAL\DBALException If not supported on this platform. * @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/ */
public function getDateSubMonthExpression($date, $months) public function getDateSubMonthExpression($date, $months)
{
return $this->getDateArithmeticIntervalExpression($date, '-', $months, self::DATE_INTERVAL_UNIT_MONTH);
}
/**
* Returns the SQL to add the number of given quarters to a date.
*
* @param string $date
* @param integer $quarters
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateAddQuartersExpression($date, $quarters)
{
return $this->getDateArithmeticIntervalExpression($date, '+', $quarters, self::DATE_INTERVAL_UNIT_QUARTER);
}
/**
* Returns the SQL to subtract the number of given quarters from a date.
*
* @param string $date
* @param integer $quarters
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateSubQuartersExpression($date, $quarters)
{
return $this->getDateArithmeticIntervalExpression($date, '-', $quarters, self::DATE_INTERVAL_UNIT_QUARTER);
}
/**
* Returns the SQL to add the number of given years to a date.
*
* @param string $date
* @param integer $years
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateAddYearsExpression($date, $years)
{
return $this->getDateArithmeticIntervalExpression($date, '+', $years, self::DATE_INTERVAL_UNIT_YEAR);
}
/**
* Returns the SQL to subtract the number of given years from a date.
*
* @param string $date
* @param integer $years
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
public function getDateSubYearsExpression($date, $years)
{
return $this->getDateArithmeticIntervalExpression($date, '-', $years, self::DATE_INTERVAL_UNIT_YEAR);
}
/**
* Returns the SQL for a date arithmetic expression.
*
* @param string $date The column or literal representing a date to perform the arithmetic operation on.
* @param string $operator The arithmetic operator (+ or -).
* @param integer $interval The interval that shall be calculated into the date.
* @param string $unit The unit of the interval that shall be calculated into the date.
* One of the DATE_INTERVAL_UNIT_* constants.
*
* @return string
*
* @throws \Doctrine\DBAL\DBALException If not supported on this platform.
*/
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
throw DBALException::notSupported(__METHOD__); throw DBALException::notSupported(__METHOD__);
} }
......
...@@ -171,25 +171,21 @@ class DB2Platform extends AbstractPlatform ...@@ -171,25 +171,21 @@ class DB2Platform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDateAddDaysExpression($date, $days) protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
return $date . ' + ' . $days . ' days'; switch ($unit) {
} case self::DATE_INTERVAL_UNIT_WEEK:
$interval *= 7;
$unit = self::DATE_INTERVAL_UNIT_DAY;
break;
/** case self::DATE_INTERVAL_UNIT_QUARTER:
* {@inheritdoc} $interval *= 3;
*/ $unit = self::DATE_INTERVAL_UNIT_MONTH;
public function getDateAddHourExpression($date, $hours) break;
{
return $date . ' + ' . $hours . ' hours';
} }
/** return $date . ' ' . $operator . ' ' . $interval . ' ' . $unit;
* {@inheritdoc}
*/
public function getDateAddMonthExpression($date, $months)
{
return $date . ' + ' . $months . ' months';
} }
/** /**
...@@ -200,30 +196,6 @@ class DB2Platform extends AbstractPlatform ...@@ -200,30 +196,6 @@ class DB2Platform extends AbstractPlatform
return 'DAYS(' . $date1 . ') - DAYS(' . $date2 . ')'; return 'DAYS(' . $date1 . ') - DAYS(' . $date2 . ')';
} }
/**
* {@inheritdoc}
*/
public function getDateSubDaysExpression($date, $days)
{
return $date . ' - ' . $days . ' days';
}
/**
* {@inheritdoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return $date . ' - ' . $hours . ' hours';
}
/**
* {@inheritdoc}
*/
public function getDateSubMonthExpression($date, $months)
{
return $date . ' - ' . $months . ' months';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
......
...@@ -58,59 +58,21 @@ class DrizzlePlatform extends AbstractPlatform ...@@ -58,59 +58,21 @@ class DrizzlePlatform extends AbstractPlatform
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
*/
public function getDateDiffExpression($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}
*/
public function getDateAddDaysExpression($date, $days)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
}
/**
* {@inheritDoc}
*/ */
public function getDateSubDaysExpression($date, $days) protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)'; $function = '+' === $operator ? 'DATE_ADD' : 'DATE_SUB';
}
/** return $function . '(' . $date . ', INTERVAL ' . $interval . ' ' . $unit . ')';
* {@inheritDoc}
*/
public function getDateAddMonthExpression($date, $months)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateSubMonthExpression($date, $months) public function getDateDiffExpression($date1, $date2)
{ {
return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)'; return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
} }
/** /**
......
...@@ -109,59 +109,21 @@ class MySqlPlatform extends AbstractPlatform ...@@ -109,59 +109,21 @@ class MySqlPlatform extends AbstractPlatform
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
*/
public function getDateDiffExpression($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}
*/
public function getDateAddDaysExpression($date, $days)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
}
/**
* {@inheritDoc}
*/ */
public function getDateSubDaysExpression($date, $days) protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)'; $function = '+' === $operator ? 'DATE_ADD' : 'DATE_SUB';
}
/** return $function . '(' . $date . ', INTERVAL ' . $interval . ' ' . $unit . ')';
* {@inheritDoc}
*/
public function getDateAddMonthExpression($date, $months)
{
return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateSubMonthExpression($date, $months) public function getDateDiffExpression($date1, $date2)
{ {
return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)'; return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
} }
/** /**
......
...@@ -100,63 +100,61 @@ class OraclePlatform extends AbstractPlatform ...@@ -100,63 +100,61 @@ class OraclePlatform extends AbstractPlatform
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
*
* Note: Since Oracle timestamp differences are calculated down to the microsecond we have to truncate
* them to the difference in days. This is obviously a restriction of the original functionality, but we
* need to make this a portable function.
*/ */
public function getDateDiffExpression($date1, $date2) protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
return "TRUNC(TO_NUMBER(SUBSTR((" . $date1 . "-" . $date2 . "), 1, INSTR(" . $date1 . "-" . $date2 .", ' '))))"; switch ($unit) {
} case self::DATE_INTERVAL_UNIT_MONTH:
case self::DATE_INTERVAL_UNIT_QUARTER:
case self::DATE_INTERVAL_UNIT_YEAR:
switch ($unit) {
case self::DATE_INTERVAL_UNIT_QUARTER:
$interval *= 3;
break;
/** case self::DATE_INTERVAL_UNIT_YEAR:
* {@inheritDoc} $interval *= 12;
*/ break;
public function getDateAddHourExpression($date, $hours)
{
return '(' . $date . '+' . $hours . '/24)';
} }
/** return 'ADD_MONTHS(' . $date . ', ' . $operator . $interval . ')';
* {@inheritDoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return '(' . $date . '-' . $hours . '/24)';
}
/** default:
* {@inheritDoc} $calculationClause = '';
*/
public function getDateAddDaysExpression($date, $days)
{
return '(' . $date . '+' . $days . ')';
}
/** switch ($unit) {
* {@inheritDoc} case self::DATE_INTERVAL_UNIT_SECOND:
*/ $calculationClause = '/24/60/60';
public function getDateSubDaysExpression($date, $days) break;
{
return '(' . $date . '-' . $days . ')'; case self::DATE_INTERVAL_UNIT_MINUTE:
$calculationClause = '/24/60';
break;
case self::DATE_INTERVAL_UNIT_HOUR:
$calculationClause = '/24';
break;
case self::DATE_INTERVAL_UNIT_WEEK:
$calculationClause = '*7';
break;
} }
/** return '(' . $date . $operator . $interval . $calculationClause . ')';
* {@inheritDoc} }
*/
public function getDateAddMonthExpression($date, $months)
{
return "ADD_MONTHS(" . $date . ", " . $months . ")";
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*
* Note: Since Oracle timestamp differences are calculated down to the microsecond we have to truncate
* them to the difference in days. This is obviously a restriction of the original functionality, but we
* need to make this a portable function.
*/ */
public function getDateSubMonthExpression($date, $months) public function getDateDiffExpression($date1, $date2)
{ {
return "ADD_MONTHS(" . $date . ", -" . $months . ")"; return "TRUNC(TO_NUMBER(SUBSTR((" . $date1 . "-" . $date2 . "), 1, INSTR(" . $date1 . "-" . $date2 .", ' '))))";
} }
/** /**
......
...@@ -98,59 +98,24 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -98,59 +98,24 @@ class PostgreSqlPlatform extends AbstractPlatform
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
*/
public function getDateDiffExpression($date1, $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}
*/
public function getDateAddDaysExpression($date, $days)
{
return "(" . $date ." + (" . $days . " || ' day')::interval)";
}
/**
* {@inheritDoc}
*/ */
public function getDateSubDaysExpression($date, $days) protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
return "(" . $date ." - (" . $days . " || ' day')::interval)"; if (self::DATE_INTERVAL_UNIT_QUARTER === $unit) {
$interval *= 3;
$unit = self::DATE_INTERVAL_UNIT_MONTH;
} }
/** return "(" . $date ." " . $operator . " (" . $interval . " || ' " . $unit . "')::interval)";
* {@inheritDoc}
*/
public function getDateAddMonthExpression($date, $months)
{
return "(" . $date ." + (" . $months . " || ' month')::interval)";
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateSubMonthExpression($date, $months) public function getDateDiffExpression($date1, $date2)
{ {
return "(" . $date ." - (" . $months . " || ' month')::interval)"; return '(DATE(' . $date1 . ')-DATE(' . $date2 . '))';
} }
/** /**
......
...@@ -466,25 +466,15 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -466,25 +466,15 @@ class SQLAnywherePlatform extends AbstractPlatform
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getDateAddDaysExpression($date, $days) protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
return 'DATEADD(day, ' . $days . ', ' . $date . ')'; $factorClause = '';
}
/** if ('-' === $operator) {
* {@inheritdoc} $factorClause = '-1 * ';
*/
public function getDateAddHourExpression($date, $hours)
{
return 'DATEADD(hour, ' . $hours . ', ' . $date . ')';
} }
/** return 'DATEADD(' . $unit . ', ' . $factorClause . $interval . ', ' . $date . ')';
* {@inheritdoc}
*/
public function getDateAddMonthExpression($date, $months)
{
return 'DATEADD(month, ' . $months . ', ' . $date . ')';
} }
/** /**
...@@ -495,30 +485,6 @@ class SQLAnywherePlatform extends AbstractPlatform ...@@ -495,30 +485,6 @@ class SQLAnywherePlatform extends AbstractPlatform
return 'DATEDIFF(day, ' . $date2 . ', ' . $date1 . ')'; return 'DATEDIFF(day, ' . $date2 . ', ' . $date1 . ')';
} }
/**
* {@inheritdoc}
*/
public function getDateSubDaysExpression($date, $days)
{
return 'DATEADD(day, -1 * ' . $days . ', ' . $date . ')';
}
/**
* {@inheritdoc}
*/
public function getDateSubHourExpression($date, $hours)
{
return 'DATEADD(hour, -1 * ' . $hours . ', ' . $date . ')';
}
/**
* {@inheritdoc}
*/
public function getDateSubMonthExpression($date, $months)
{
return 'DATEADD(month, -1 * ' . $months . ', ' . $date . ')';
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -39,59 +39,25 @@ use Doctrine\DBAL\Schema\Table; ...@@ -39,59 +39,25 @@ use Doctrine\DBAL\Schema\Table;
class SQLServerPlatform extends AbstractPlatform class SQLServerPlatform extends AbstractPlatform
{ {
/** /**
* {@inheritDoc} * {@inheritdoc}
*/
public function getDateDiffExpression($date1, $date2)
{
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}
*/ */
public function getDateAddDaysExpression($date, $days) protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
return 'DATEADD(day, ' . $days . ', ' . $date . ')'; $factorClause = '';
}
/** if ('-' === $operator) {
* {@inheritDoc} $factorClause = '-1 * ';
*/
public function getDateSubDaysExpression($date, $days)
{
return 'DATEADD(day, -1 * ' . $days . ', ' . $date . ')';
} }
/** return 'DATEADD(' . $unit . ', ' . $factorClause . $interval . ', ' . $date . ')';
* {@inheritDoc}
*/
public function getDateAddMonthExpression($date, $months)
{
return 'DATEADD(month, ' . $months . ', ' . $date . ')';
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateSubMonthExpression($date, $months) public function getDateDiffExpression($date1, $date2)
{ {
return 'DATEADD(month, -1 * ' . $months . ', ' . $date . ')'; return 'DATEDIFF(day, ' . $date2 . ',' . $date1 . ')';
} }
/** /**
......
...@@ -124,59 +124,39 @@ class SqlitePlatform extends AbstractPlatform ...@@ -124,59 +124,39 @@ class SqlitePlatform extends AbstractPlatform
} }
/** /**
* {@inheritDoc} * {@inheritdoc}
*/
public function getDateDiffExpression($date1, $date2)
{
return 'ROUND(JULIANDAY('.$date1 . ')-JULIANDAY('.$date2.'))';
}
/**
* {@inheritDoc}
*/ */
public function getDateAddHourExpression($date, $hours) protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{ {
return "DATETIME(" . $date . ",'+". $hours . " hour')"; switch ($unit) {
} case self::DATE_INTERVAL_UNIT_SECOND:
case self::DATE_INTERVAL_UNIT_MINUTE:
case self::DATE_INTERVAL_UNIT_HOUR:
return "DATETIME(" . $date . ",'" . $operator . $interval . " " . $unit . "')";
/** default:
* {@inheritDoc} switch ($unit) {
*/ case self::DATE_INTERVAL_UNIT_WEEK:
public function getDateSubHourExpression($date, $hours) $interval *= 7;
{ $unit = self::DATE_INTERVAL_UNIT_DAY;
return "DATETIME(" . $date . ",'-". $hours . " hour')"; break;
}
/** case self::DATE_INTERVAL_UNIT_QUARTER:
* {@inheritDoc} $interval *= 3;
*/ $unit = self::DATE_INTERVAL_UNIT_MONTH;
public function getDateAddDaysExpression($date, $days) break;
{
return "DATE(" . $date . ",'+". $days . " day')";
} }
/** return "DATE(" . $date . ",'" . $operator . $interval . " " . $unit . "')";
* {@inheritDoc}
*/
public function getDateSubDaysExpression($date, $days)
{
return "DATE(" . $date . ",'-". $days . " day')";
} }
/**
* {@inheritDoc}
*/
public function getDateAddMonthExpression($date, $months)
{
return "DATE(" . $date . ",'+". $months . " month')";
} }
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function getDateSubMonthExpression($date, $months) public function getDateDiffExpression($date1, $date2)
{ {
return "DATE(" . $date . ",'-". $months . " month')"; return 'ROUND(JULIANDAY('.$date1 . ')-JULIANDAY('.$date2.'))';
} }
/** /**
......
...@@ -501,12 +501,22 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -501,12 +501,22 @@ 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->getDateAddSecondsExpression('test_datetime', 1) .' AS add_seconds, ';
$sql .= $p->getDateSubSecondsExpression('test_datetime', 1) .' AS sub_seconds, ';
$sql .= $p->getDateAddMinutesExpression('test_datetime', 5) .' AS add_minutes, ';
$sql .= $p->getDateSubMinutesExpression('test_datetime', 5) .' AS sub_minutes, ';
$sql .= $p->getDateAddHourExpression('test_datetime', 3) .' AS add_hour, '; $sql .= $p->getDateAddHourExpression('test_datetime', 3) .' AS add_hour, ';
$sql .= $p->getDateSubHourExpression('test_datetime', 3) .' AS sub_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->getDateAddWeeksExpression('test_datetime', 1) .' AS add_weeks, ';
$sql .= $p->getDateSubWeeksExpression('test_datetime', 1) .' AS sub_weeks, ';
$sql .= $p->getDateAddMonthExpression('test_datetime', 2) .' AS add_month, '; $sql .= $p->getDateAddMonthExpression('test_datetime', 2) .' AS add_month, ';
$sql .= $p->getDateSubMonthExpression('test_datetime', 2) .' AS sub_month '; $sql .= $p->getDateSubMonthExpression('test_datetime', 2) .' AS sub_month, ';
$sql .= $p->getDateAddQuartersExpression('test_datetime', 3) .' AS add_quarters, ';
$sql .= $p->getDateSubQuartersExpression('test_datetime', 3) .' AS sub_quarters, ';
$sql .= $p->getDateAddYearsExpression('test_datetime', 6) .' AS add_years, ';
$sql .= $p->getDateSubYearsExpression('test_datetime', 6) .' AS sub_years ';
$sql .= 'FROM fetch_table'; $sql .= 'FROM fetch_table';
$row = $this->_conn->fetchAssoc($sql); $row = $this->_conn->fetchAssoc($sql);
...@@ -514,12 +524,22 @@ class DataAccessTest extends \Doctrine\Tests\DbalFunctionalTestCase ...@@ -514,12 +524,22 @@ 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 10:10:11', date('Y-m-d H:i:s', strtotime($row['add_seconds'])), "Adding second should end up on 2010-01-01 10:10:11");
$this->assertEquals('2010-01-01 10:10:09', date('Y-m-d H:i:s', strtotime($row['sub_seconds'])), "Subtracting second should end up on 2010-01-01 10:10:09");
$this->assertEquals('2010-01-01 10:15:10', date('Y-m-d H:i:s', strtotime($row['add_minutes'])), "Adding minutes should end up on 2010-01-01 10:15:10");
$this->assertEquals('2010-01-01 10:05:10', date('Y-m-d H:i:s', strtotime($row['sub_minutes'])), "Subtracting minutes should end up on 2010-01-01 10:05:10");
$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 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-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-01-08', date('Y-m-d', strtotime($row['add_weeks'])), "Adding week should end up on 2010-01-08");
$this->assertEquals('2009-12-25', date('Y-m-d', strtotime($row['sub_weeks'])), "Subtracting week should end up on 2009-12-25");
$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");
$this->assertEquals('2009-11-01', date('Y-m-d', strtotime($row['sub_month'])), "Adding month should end up on 2009-11-01"); $this->assertEquals('2009-11-01', date('Y-m-d', strtotime($row['sub_month'])), "Substracting month should end up on 2009-11-01");
$this->assertEquals('2010-10-01', date('Y-m-d', strtotime($row['add_quarters'])), "Adding quarters should end up on 2010-04-01");
$this->assertEquals('2009-04-01', date('Y-m-d', strtotime($row['sub_quarters'])), "Substracting quarters should end up on 2009-10-01");
$this->assertEquals('2016-01-01', date('Y-m-d', strtotime($row['add_years'])), "Adding years should end up on 2016-01-01");
$this->assertEquals('2004-01-01', date('Y-m-d', strtotime($row['sub_years'])), "Substracting years should end up on 2004-01-01");
} }
public function testLocateExpression() public function testLocateExpression()
......
...@@ -290,13 +290,23 @@ class DB2PlatformTest extends AbstractPlatformTestCase ...@@ -290,13 +290,23 @@ class DB2PlatformTest extends AbstractPlatformTestCase
$this->assertEquals('CURRENT DATE', $this->_platform->getCurrentDateSQL()); $this->assertEquals('CURRENT DATE', $this->_platform->getCurrentDateSQL());
$this->assertEquals('CURRENT TIME', $this->_platform->getCurrentTimeSQL()); $this->assertEquals('CURRENT TIME', $this->_platform->getCurrentTimeSQL());
$this->assertEquals('CURRENT TIMESTAMP', $this->_platform->getCurrentTimestampSQL()); $this->assertEquals('CURRENT TIMESTAMP', $this->_platform->getCurrentTimestampSQL());
$this->assertEquals("'1987/05/02' + 4 days", $this->_platform->getDateAddDaysExpression("'1987/05/02'", 4)); $this->assertEquals("'1987/05/02' + 4 DAY", $this->_platform->getDateAddDaysExpression("'1987/05/02'", 4));
$this->assertEquals("'1987/05/02' + 12 hours", $this->_platform->getDateAddHourExpression("'1987/05/02'", 12)); $this->assertEquals("'1987/05/02' + 12 HOUR", $this->_platform->getDateAddHourExpression("'1987/05/02'", 12));
$this->assertEquals("'1987/05/02' + 102 months", $this->_platform->getDateAddMonthExpression("'1987/05/02'", 102)); $this->assertEquals("'1987/05/02' + 2 MINUTE", $this->_platform->getDateAddMinutesExpression("'1987/05/02'", 2));
$this->assertEquals("'1987/05/02' + 102 MONTH", $this->_platform->getDateAddMonthExpression("'1987/05/02'", 102));
$this->assertEquals("'1987/05/02' + 15 MONTH", $this->_platform->getDateAddQuartersExpression("'1987/05/02'", 5));
$this->assertEquals("'1987/05/02' + 1 SECOND", $this->_platform->getDateAddSecondsExpression("'1987/05/02'", 1));
$this->assertEquals("'1987/05/02' + 21 DAY", $this->_platform->getDateAddWeeksExpression("'1987/05/02'", 3));
$this->assertEquals("'1987/05/02' + 10 YEAR", $this->_platform->getDateAddYearsExpression("'1987/05/02'", 10));
$this->assertEquals("DAYS('1987/05/02') - DAYS('1987/04/01')", $this->_platform->getDateDiffExpression("'1987/05/02'", "'1987/04/01'")); $this->assertEquals("DAYS('1987/05/02') - DAYS('1987/04/01')", $this->_platform->getDateDiffExpression("'1987/05/02'", "'1987/04/01'"));
$this->assertEquals("'1987/05/02' - 4 days", $this->_platform->getDateSubDaysExpression("'1987/05/02'", 4)); $this->assertEquals("'1987/05/02' - 4 DAY", $this->_platform->getDateSubDaysExpression("'1987/05/02'", 4));
$this->assertEquals("'1987/05/02' - 12 hours", $this->_platform->getDateSubHourExpression("'1987/05/02'", 12)); $this->assertEquals("'1987/05/02' - 12 HOUR", $this->_platform->getDateSubHourExpression("'1987/05/02'", 12));
$this->assertEquals("'1987/05/02' - 102 months", $this->_platform->getDateSubMonthExpression("'1987/05/02'", 102)); $this->assertEquals("'1987/05/02' - 2 MINUTE", $this->_platform->getDateSubMinutesExpression("'1987/05/02'", 2));
$this->assertEquals("'1987/05/02' - 102 MONTH", $this->_platform->getDateSubMonthExpression("'1987/05/02'", 102));
$this->assertEquals("'1987/05/02' - 15 MONTH", $this->_platform->getDateSubQuartersExpression("'1987/05/02'", 5));
$this->assertEquals("'1987/05/02' - 1 SECOND", $this->_platform->getDateSubSecondsExpression("'1987/05/02'", 1));
$this->assertEquals("'1987/05/02' - 21 DAY", $this->_platform->getDateSubWeeksExpression("'1987/05/02'", 3));
$this->assertEquals("'1987/05/02' - 10 YEAR", $this->_platform->getDateSubYearsExpression("'1987/05/02'", 10));
$this->assertEquals(' WITH RR USE AND KEEP UPDATE LOCKS', $this->_platform->getForUpdateSQL()); $this->assertEquals(' WITH RR USE AND KEEP UPDATE LOCKS', $this->_platform->getForUpdateSQL());
$this->assertEquals('LOCATE(substring_column, string_column)', $this->_platform->getLocateExpression('string_column', 'substring_column')); $this->assertEquals('LOCATE(substring_column, string_column)', $this->_platform->getLocateExpression('string_column', 'substring_column'));
$this->assertEquals('LOCATE(substring_column, string_column)', $this->_platform->getLocateExpression('string_column', 'substring_column')); $this->assertEquals('LOCATE(substring_column, string_column)', $this->_platform->getLocateExpression('string_column', 'substring_column'));
......
...@@ -533,13 +533,23 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase ...@@ -533,13 +533,23 @@ class SQLAnywherePlatformTest extends AbstractPlatformTestCase
$this->assertEquals('CURRENT DATE', $this->_platform->getCurrentDateSQL()); $this->assertEquals('CURRENT DATE', $this->_platform->getCurrentDateSQL());
$this->assertEquals('CURRENT TIME', $this->_platform->getCurrentTimeSQL()); $this->assertEquals('CURRENT TIME', $this->_platform->getCurrentTimeSQL());
$this->assertEquals('CURRENT TIMESTAMP', $this->_platform->getCurrentTimestampSQL()); $this->assertEquals('CURRENT TIMESTAMP', $this->_platform->getCurrentTimestampSQL());
$this->assertEquals("DATEADD(day, 4, '1987/05/02')", $this->_platform->getDateAddDaysExpression("'1987/05/02'", 4)); $this->assertEquals("DATEADD(DAY, 4, '1987/05/02')", $this->_platform->getDateAddDaysExpression("'1987/05/02'", 4));
$this->assertEquals("DATEADD(hour, 12, '1987/05/02')", $this->_platform->getDateAddHourExpression("'1987/05/02'", 12)); $this->assertEquals("DATEADD(HOUR, 12, '1987/05/02')", $this->_platform->getDateAddHourExpression("'1987/05/02'", 12));
$this->assertEquals("DATEADD(month, 102, '1987/05/02')", $this->_platform->getDateAddMonthExpression("'1987/05/02'", 102)); $this->assertEquals("DATEADD(MINUTE, 2, '1987/05/02')", $this->_platform->getDateAddMinutesExpression("'1987/05/02'", 2));
$this->assertEquals("DATEADD(MONTH, 102, '1987/05/02')", $this->_platform->getDateAddMonthExpression("'1987/05/02'", 102));
$this->assertEquals("DATEADD(QUARTER, 5, '1987/05/02')", $this->_platform->getDateAddQuartersExpression("'1987/05/02'", 5));
$this->assertEquals("DATEADD(SECOND, 1, '1987/05/02')", $this->_platform->getDateAddSecondsExpression("'1987/05/02'", 1));
$this->assertEquals("DATEADD(WEEK, 3, '1987/05/02')", $this->_platform->getDateAddWeeksExpression("'1987/05/02'", 3));
$this->assertEquals("DATEADD(YEAR, 10, '1987/05/02')", $this->_platform->getDateAddYearsExpression("'1987/05/02'", 10));
$this->assertEquals("DATEDIFF(day, '1987/04/01', '1987/05/02')", $this->_platform->getDateDiffExpression("'1987/05/02'", "'1987/04/01'")); $this->assertEquals("DATEDIFF(day, '1987/04/01', '1987/05/02')", $this->_platform->getDateDiffExpression("'1987/05/02'", "'1987/04/01'"));
$this->assertEquals("DATEADD(day, -1 * 4, '1987/05/02')", $this->_platform->getDateSubDaysExpression("'1987/05/02'", 4)); $this->assertEquals("DATEADD(DAY, -1 * 4, '1987/05/02')", $this->_platform->getDateSubDaysExpression("'1987/05/02'", 4));
$this->assertEquals("DATEADD(hour, -1 * 12, '1987/05/02')", $this->_platform->getDateSubHourExpression("'1987/05/02'", 12)); $this->assertEquals("DATEADD(HOUR, -1 * 12, '1987/05/02')", $this->_platform->getDateSubHourExpression("'1987/05/02'", 12));
$this->assertEquals("DATEADD(month, -1 * 102, '1987/05/02')", $this->_platform->getDateSubMonthExpression("'1987/05/02'", 102)); $this->assertEquals("DATEADD(MINUTE, -1 * 2, '1987/05/02')", $this->_platform->getDateSubMinutesExpression("'1987/05/02'", 2));
$this->assertEquals("DATEADD(MONTH, -1 * 102, '1987/05/02')", $this->_platform->getDateSubMonthExpression("'1987/05/02'", 102));
$this->assertEquals("DATEADD(QUARTER, -1 * 5, '1987/05/02')", $this->_platform->getDateSubQuartersExpression("'1987/05/02'", 5));
$this->assertEquals("DATEADD(SECOND, -1 * 1, '1987/05/02')", $this->_platform->getDateSubSecondsExpression("'1987/05/02'", 1));
$this->assertEquals("DATEADD(WEEK, -1 * 3, '1987/05/02')", $this->_platform->getDateSubWeeksExpression("'1987/05/02'", 3));
$this->assertEquals("DATEADD(YEAR, -1 * 10, '1987/05/02')", $this->_platform->getDateSubYearsExpression("'1987/05/02'", 10));
$this->assertEquals("Y-m-d H:i:s.u", $this->_platform->getDateTimeFormatString()); $this->assertEquals("Y-m-d H:i:s.u", $this->_platform->getDateTimeFormatString());
$this->assertEquals("H:i:s.u", $this->_platform->getTimeFormatString()); $this->assertEquals("H:i:s.u", $this->_platform->getTimeFormatString());
$this->assertEquals('FOR UPDATE BY LOCK', $this->_platform->getForUpdateSQL()); $this->assertEquals('FOR UPDATE BY LOCK', $this->_platform->getForUpdateSQL());
......
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