Skip to content

Commit

Permalink
Support PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
skollro committed Jul 24, 2022
1 parent e2d546a commit a32b5dd
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/StatsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ public static function getPeriodDateFormat(string $period): string
{
$dbDriver = Config::get('database.connections.'.Config::get('database.default', 'mysql').'.driver', 'mysql');

if ($dbDriver === 'pgsql') {
return match ($period) {
'year' => "to_char(created_at, 'YYYY')",
'month' => "to_char(created_at, 'YYYY-MM')",
'week' => "to_char(created_at, 'IYYYIW')",
'day' => "to_char(created_at, 'YYYY-MM-DD')",
'hour' => "to_char(created_at, 'YYYY-MM-DD HH24')",
'minute' => "to_char(created_at, 'YYYY-MM-DD HH24:MI')",
};
}

return match ($period) {
'year' => "date_format(created_at,'%Y')",
'month' => "date_format(created_at,'%Y-%m')",
Expand Down Expand Up @@ -245,11 +256,13 @@ protected function getLatestSetPerPeriod(): EloquentCollection
{
$periodDateFormat = static::getPeriodDateFormat($this->period);

$statsTable = $this->getStatsTableName();
$statsKey = $this->getStatsKey();
$query = $this->queryStats();

$statsTable = $query->getGrammar()->wrap($this->getStatsTableName());
$statsKey = $query->getGrammar()->wrap($this->getStatsKey());

$rankedSets = $this->queryStats()
->selectRaw("ROW_NUMBER() OVER (PARTITION BY {$periodDateFormat} ORDER BY `{$statsKey}` DESC) AS rn, `{$statsTable}`.*, {$periodDateFormat} as period")
$rankedSets = $query
->selectRaw("ROW_NUMBER() OVER (PARTITION BY {$periodDateFormat} ORDER BY {$statsKey} DESC) AS rn, {$statsTable}.*, {$periodDateFormat} as period")
->where('type', DataPoint::TYPE_SET)
->where('created_at', '>=', $this->start)
->where('created_at', '<', $this->end)
Expand Down

0 comments on commit a32b5dd

Please sign in to comment.