Skip to content

Commit

Permalink
Introduce AbstractPlatform::multiplyInterval()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 10, 2024
1 parent 5fdb268 commit ade652c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 16 deletions.
11 changes: 11 additions & 0 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions tests/Platforms/DB2PlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,9 @@ 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));

Check warning on line 324 in tests/Platforms/DB2PlatformTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Line exceeds 120 characters; contains 125 characters
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 +335,9 @@ 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));

Check warning on line 338 in tests/Platforms/DB2PlatformTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Line exceeds 120 characters; contains 125 characters
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

0 comments on commit ade652c

Please sign in to comment.