Skip to content

Commit

Permalink
Adjusted the date arithmetic functions in ClickhousePlatform
Browse files Browse the repository at this point in the history
  • Loading branch information
pschombara committed Sep 26, 2024
1 parent 192d79d commit 0f5ae1f
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions src/ClickHousePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@

class ClickHousePlatform extends AbstractPlatform
{
protected const TIME_MINUTE = 60;
protected const TIME_HOUR = self::TIME_MINUTE * 60;
protected const TIME_DAY = self::TIME_HOUR * 24;
protected const TIME_WEEK = self::TIME_DAY * 7;

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -348,79 +343,79 @@ public function getDateDiffExpression(string $date1, string $date2): string
*/
public function getDateAddSecondsExpression(string $date, string $seconds): string
{
return $date . ' + ' . $seconds;
return $this->getDateArithmeticIntervalExpression($date, '+', $seconds, DateIntervalUnit::SECOND);
}

/**
* {@inheritDoc}
*/
public function getDateSubSecondsExpression(string $date, string $seconds): string
{
return $date . ' - ' . $seconds;
return $this->getDateArithmeticIntervalExpression($date, '-', $seconds, DateIntervalUnit::SECOND);
}

/**
* {@inheritDoc}
*/
public function getDateAddMinutesExpression(string $date, string $minutes): string
{
return $date . ' + ' . ((int) $minutes * self::TIME_MINUTE);
return $this->getDateArithmeticIntervalExpression($date, '+', $minutes, DateIntervalUnit::MINUTE);
}

/**
* {@inheritDoc}
*/
public function getDateSubMinutesExpression(string $date, string $minutes): string
{
return $date . ' - ' . ((int) $minutes * self::TIME_MINUTE);
return $this->getDateArithmeticIntervalExpression($date, '-', $minutes, DateIntervalUnit::MINUTE);
}

/**
* {@inheritDoc}
*/
public function getDateAddHourExpression(string $date, string $hours): string
{
return $date . ' + ' . ((int) $hours * self::TIME_HOUR);
return $this->getDateArithmeticIntervalExpression($date, '+', $hours, DateIntervalUnit::HOUR);
}

/**
* {@inheritDoc}
*/
public function getDateSubHourExpression(string $date, string $hours): string
{
return $date . ' - ' . ((int) $hours * self::TIME_HOUR);
return $this->getDateArithmeticIntervalExpression($date, '-', $hours, DateIntervalUnit::HOUR);
}

/**
* {@inheritDoc}
*/
public function getDateAddDaysExpression(string $date, string $days): string
{
return $date . ' + ' . ((int) $days * self::TIME_DAY);
return $this->getDateArithmeticIntervalExpression($date, '+', $days, DateIntervalUnit::DAY);
}

/**
* {@inheritDoc}
*/
public function getDateSubDaysExpression(string $date, string $days): string
{
return $date . ' - ' . ((int) $days * self::TIME_DAY);
return $this->getDateArithmeticIntervalExpression($date, '-', $days, DateIntervalUnit::DAY);
}

/**
* {@inheritDoc}
*/
public function getDateAddWeeksExpression(string $date, string $weeks): string
{
return $date . ' + ' . ((int) $weeks * self::TIME_WEEK);
return $this->getDateArithmeticIntervalExpression($date, '+', $weeks, DateIntervalUnit::WEEK);
}

/**
* {@inheritDoc}
*/
public function getDateSubWeeksExpression(string $date, string $weeks): string
{
return $date . ' - ' . ((int) $weeks * self::TIME_WEEK);
return $this->getDateArithmeticIntervalExpression($date, '-', $weeks, DateIntervalUnit::WEEK);
}

/**
Expand Down Expand Up @@ -1105,24 +1100,10 @@ public function getCurrentDatabaseExpression(): string

protected function getDateArithmeticIntervalExpression(string $date, string $operator, string $interval, DateIntervalUnit $unit): string
{
$func = match($operator) {
'+' => 'plus',
'-' => 'minus',
default => throw new \InvalidArgumentException(\sprintf('Unsupported operator "%s".', $operator)),
};

$seconds = ((int) $interval) * match ($unit) {
DateIntervalUnit::SECOND => 1,
DateIntervalUnit::MINUTE => 60,
DateIntervalUnit::HOUR => 3600,
DateIntervalUnit::DAY => 86400,
DateIntervalUnit::WEEK => 604800,
DateIntervalUnit::MONTH => 2592000,
DateIntervalUnit::QUARTER => 7884000,
DateIntervalUnit::YEAR => 31536000,
};

return $this->$func(\sprintf('(%s, %s)', $date, $seconds));
$operation = '+' === $operator ? 'date_add' : 'date_sub';
$toDateFunction = strlen($date) > 10 ? 'toDateTime' : 'toDate';

return \sprintf('%s(%s, %d, %s(\'%s\'))', $operation, $unit->value, (int) $interval, $toDateFunction, $date);
}

public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string
Expand Down

0 comments on commit 0f5ae1f

Please sign in to comment.