Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbstractPlatform: allow any string in interval for date interval expression #6302

Merged
merged 2 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
83 changes: 47 additions & 36 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1342,8 +1342,8 @@ public function getDateDiffExpression($date1, $date2)
/**
* Returns the SQL to add the number of given seconds to a date.
*
* @param string $date
* @param int|numeric-string $seconds
* @param string $date
* @param int|string $seconds
*
* @return string
*
Expand All @@ -1365,8 +1365,8 @@ public function getDateAddSecondsExpression($date, $seconds)
/**
* Returns the SQL to subtract the number of given seconds from a date.
*
* @param string $date
* @param int|numeric-string $seconds
* @param string $date
* @param int|string $seconds
*
* @return string
*
Expand All @@ -1388,8 +1388,8 @@ public function getDateSubSecondsExpression($date, $seconds)
/**
* Returns the SQL to add the number of given minutes to a date.
*
* @param string $date
* @param int|numeric-string $minutes
* @param string $date
* @param int|string $minutes
*
* @return string
*
Expand All @@ -1411,8 +1411,8 @@ public function getDateAddMinutesExpression($date, $minutes)
/**
* Returns the SQL to subtract the number of given minutes from a date.
*
* @param string $date
* @param int|numeric-string $minutes
* @param string $date
* @param int|string $minutes
*
* @return string
*
Expand All @@ -1434,8 +1434,8 @@ public function getDateSubMinutesExpression($date, $minutes)
/**
* Returns the SQL to add the number of given hours to a date.
*
* @param string $date
* @param int|numeric-string $hours
* @param string $date
* @param int|string $hours
*
* @return string
*
Expand All @@ -1457,8 +1457,8 @@ public function getDateAddHourExpression($date, $hours)
/**
* Returns the SQL to subtract the number of given hours to a date.
*
* @param string $date
* @param int|numeric-string $hours
* @param string $date
* @param int|string $hours
*
* @return string
*
Expand All @@ -1480,8 +1480,8 @@ public function getDateSubHourExpression($date, $hours)
/**
* Returns the SQL to add the number of given days to a date.
*
* @param string $date
* @param int|numeric-string $days
* @param string $date
* @param int|string $days
*
* @return string
*
Expand All @@ -1503,8 +1503,8 @@ public function getDateAddDaysExpression($date, $days)
/**
* Returns the SQL to subtract the number of given days to a date.
*
* @param string $date
* @param int|numeric-string $days
* @param string $date
* @param int|string $days
*
* @return string
*
Expand All @@ -1526,8 +1526,8 @@ public function getDateSubDaysExpression($date, $days)
/**
* Returns the SQL to add the number of given weeks to a date.
*
* @param string $date
* @param int|numeric-string $weeks
* @param string $date
* @param int|string $weeks
*
* @return string
*
Expand All @@ -1549,8 +1549,8 @@ public function getDateAddWeeksExpression($date, $weeks)
/**
* Returns the SQL to subtract the number of given weeks from a date.
*
* @param string $date
* @param int|numeric-string $weeks
* @param string $date
* @param int|string $weeks
*
* @return string
*
Expand All @@ -1572,8 +1572,8 @@ public function getDateSubWeeksExpression($date, $weeks)
/**
* Returns the SQL to add the number of given months to a date.
*
* @param string $date
* @param int|numeric-string $months
* @param string $date
* @param int|string $months
*
* @return string
*
Expand All @@ -1595,8 +1595,8 @@ public function getDateAddMonthExpression($date, $months)
/**
* Returns the SQL to subtract the number of given months to a date.
*
* @param string $date
* @param int|numeric-string $months
* @param string $date
* @param int|string $months
*
* @return string
*
Expand All @@ -1618,8 +1618,8 @@ public function getDateSubMonthExpression($date, $months)
/**
* Returns the SQL to add the number of given quarters to a date.
*
* @param string $date
* @param int|numeric-string $quarters
* @param string $date
* @param int|string $quarters
*
* @return string
*
Expand All @@ -1641,8 +1641,8 @@ public function getDateAddQuartersExpression($date, $quarters)
/**
* Returns the SQL to subtract the number of given quarters from a date.
*
* @param string $date
* @param int|numeric-string $quarters
* @param string $date
* @param int|string $quarters
*
* @return string
*
Expand All @@ -1664,8 +1664,8 @@ public function getDateSubQuartersExpression($date, $quarters)
/**
* Returns the SQL to add the number of given years to a date.
*
* @param string $date
* @param int|numeric-string $years
* @param string $date
* @param int|string $years
*
* @return string
*
Expand All @@ -1687,8 +1687,8 @@ public function getDateAddYearsExpression($date, $years)
/**
* Returns the SQL to subtract the number of given years from a date.
*
* @param string $date
* @param int|numeric-string $years
* @param string $date
* @param int|string $years
*
* @return string
*
Expand All @@ -1710,11 +1710,11 @@ public function getDateSubYearsExpression($date, $years)
/**
* Returns the SQL for a date arithmetic expression.
*
* @param string $date The column or literal representing a date
* @param string $date The column or literal representing a date
* to perform the arithmetic operation on.
* @param string $operator The arithmetic operator (+ or -).
* @param int|numeric-string $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.
* @param string $operator The arithmetic operator (+ or -).
* @param int|string $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 {@see DateIntervalUnit} constants.
*
* @return string
Expand All @@ -1726,6 +1726,17 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv
throw Exception::notSupported(__METHOD__);
}

/**
* Generates the SQL expression which represents the given date interval multiplied by a number
*
* @param string $interval SQL expression describing the interval value
* @param int $multiplier Interval multiplier
*/
protected function multiplyInterval(string $interval, int $multiplier): string
{
return sprintf('(%s * %d)', $interval, $multiplier);
}

/**
* Returns the SQL bit AND comparison expression.
*
Expand Down
8 changes: 4 additions & 4 deletions src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,13 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv
{
switch ($unit) {
case DateIntervalUnit::WEEK:
$interval *= 7;
$unit = DateIntervalUnit::DAY;
$interval = $this->multiplyInterval((string) $interval, 7);
$unit = DateIntervalUnit::DAY;
break;

case DateIntervalUnit::QUARTER:
$interval *= 3;
$unit = DateIntervalUnit::MONTH;
$interval = $this->multiplyInterval((string) $interval, 3);
$unit = DateIntervalUnit::MONTH;
break;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv
case DateIntervalUnit::YEAR:
switch ($unit) {
case DateIntervalUnit::QUARTER:
$interval *= 3;
$interval = $this->multiplyInterval((string) $interval, 3);
break;

case DateIntervalUnit::YEAR:
$interval *= 12;
$interval = $this->multiplyInterval((string) $interval, 12);
break;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public function getLocateExpression($str, $substr, $startPos = false)
protected function getDateArithmeticIntervalExpression($date, $operator, $interval, $unit)
{
if ($unit === DateIntervalUnit::QUARTER) {
$interval *= 3;
$unit = DateIntervalUnit::MONTH;
$interval = $this->multiplyInterval((string) $interval, 3);
$unit = DateIntervalUnit::MONTH;
}

return '(' . $date . ' ' . $operator . ' (' . $interval . " || ' " . $unit . "')::interval)";
Expand Down
8 changes: 4 additions & 4 deletions src/Platforms/SqlitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv

switch ($unit) {
case DateIntervalUnit::WEEK:
$interval *= 7;
$unit = DateIntervalUnit::DAY;
$interval = $this->multiplyInterval((string) $interval, 7);
$unit = DateIntervalUnit::DAY;
break;

case DateIntervalUnit::QUARTER:
$interval *= 3;
$unit = DateIntervalUnit::MONTH;
$interval = $this->multiplyInterval((string) $interval, 3);
$unit = DateIntervalUnit::MONTH;
break;
}

Expand Down
14 changes: 10 additions & 4 deletions tests/Platforms/DB2PlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,12 @@ public function testGeneratesSQLSnippets(): void
self::assertEquals("'1987/05/02' + 12 HOUR", $this->platform->getDateAddHourExpression("'1987/05/02'", 12));
self::assertEquals("'1987/05/02' + 2 MINUTE", $this->platform->getDateAddMinutesExpression("'1987/05/02'", 2));
self::assertEquals("'1987/05/02' + 102 MONTH", $this->platform->getDateAddMonthExpression("'1987/05/02'", 102));
self::assertEquals("'1987/05/02' + 15 MONTH", $this->platform->getDateAddQuartersExpression("'1987/05/02'", 5));
self::assertEquals(
"'1987/05/02' + (5 * 3) MONTH",
$this->platform->getDateAddQuartersExpression("'1987/05/02'", 5),
);
self::assertEquals("'1987/05/02' + 1 SECOND", $this->platform->getDateAddSecondsExpression("'1987/05/02'", 1));
self::assertEquals("'1987/05/02' + 21 DAY", $this->platform->getDateAddWeeksExpression("'1987/05/02'", 3));
self::assertEquals("'1987/05/02' + (3 * 7) DAY", $this->platform->getDateAddWeeksExpression("'1987/05/02'", 3));
self::assertEquals("'1987/05/02' + 10 YEAR", $this->platform->getDateAddYearsExpression("'1987/05/02'", 10));

self::assertEquals(
Expand All @@ -335,9 +338,12 @@ public function testGeneratesSQLSnippets(): void
self::assertEquals("'1987/05/02' - 12 HOUR", $this->platform->getDateSubHourExpression("'1987/05/02'", 12));
self::assertEquals("'1987/05/02' - 2 MINUTE", $this->platform->getDateSubMinutesExpression("'1987/05/02'", 2));
self::assertEquals("'1987/05/02' - 102 MONTH", $this->platform->getDateSubMonthExpression("'1987/05/02'", 102));
self::assertEquals("'1987/05/02' - 15 MONTH", $this->platform->getDateSubQuartersExpression("'1987/05/02'", 5));
self::assertEquals(
"'1987/05/02' - (5 * 3) MONTH",
$this->platform->getDateSubQuartersExpression("'1987/05/02'", 5),
);
self::assertEquals("'1987/05/02' - 1 SECOND", $this->platform->getDateSubSecondsExpression("'1987/05/02'", 1));
self::assertEquals("'1987/05/02' - 21 DAY", $this->platform->getDateSubWeeksExpression("'1987/05/02'", 3));
self::assertEquals("'1987/05/02' - (3 * 7) DAY", $this->platform->getDateSubWeeksExpression("'1987/05/02'", 3));
self::assertEquals("'1987/05/02' - 10 YEAR", $this->platform->getDateSubYearsExpression("'1987/05/02'", 10));
self::assertEquals(' WITH RR USE AND KEEP UPDATE LOCKS', $this->platform->getForUpdateSQL());

Expand Down