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 551ba19
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 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
4 changes: 2 additions & 2 deletions src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ protected function getDateArithmeticIntervalExpression($date, $operator, $interv
{
switch ($unit) {
case DateIntervalUnit::WEEK:
$interval *= 7;
$interval = $this->multiplyInterval((string) $interval, 7);
$unit = DateIntervalUnit::DAY;
break;

case DateIntervalUnit::QUARTER:
$interval *= 3;
$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

0 comments on commit 551ba19

Please sign in to comment.