From 4d8ca38dddcab022a2a509da8b12a5f4236a4d13 Mon Sep 17 00:00:00 2001 From: eliseekn Date: Sun, 7 Apr 2024 20:56:32 +0000 Subject: [PATCH] Fix #12 Fix PostgreSQL and SQLite support --- src/DatesFunctions.php | 6 +++--- src/LaravelMetrics.php | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/DatesFunctions.php b/src/DatesFunctions.php index b64a6b6..416f4cc 100644 --- a/src/DatesFunctions.php +++ b/src/DatesFunctions.php @@ -60,7 +60,7 @@ protected function formatPeriod(string $period): string if ($driver === 'mysql') { return match ($period) { - Period::TODAY->value, Period::DAY->value => "weekday($this->dateColumn)", + Period::TODAY->value, Period::DAY->value => "day($this->dateColumn)", Period::WEEK->value => "week($this->dateColumn)", Period::MONTH->value => "month($this->dateColumn)", default => "year($this->dateColumn)", @@ -69,7 +69,7 @@ protected function formatPeriod(string $period): string if ($driver === 'pgsql') { return match ($period) { - Period::TODAY->value, Period::DAY->value => "EXTRACT(DOW FROM $this->dateColumn)", + Period::TODAY->value, Period::DAY->value => "EXTRACT(DAY FROM $this->dateColumn)", Period::WEEK->value => "EXTRACT(WEEK FROM $this->dateColumn)", Period::MONTH->value => "EXTRACT(MONTH FROM $this->dateColumn)", default => "EXTRACT(YEAR FROM $this->dateColumn)", @@ -77,7 +77,7 @@ protected function formatPeriod(string $period): string } return match ($period) { - Period::TODAY->value, Period::DAY->value => "strftime('%w', $this->dateColumn)", + Period::TODAY->value, Period::DAY->value => "strftime('%d', $this->dateColumn)", Period::WEEK->value => "strftime('%W', $this->dateColumn)", Period::MONTH->value => "strftime('%m', $this->dateColumn)", default => "strftime('%Y', $this->dateColumn)", diff --git a/src/LaravelMetrics.php b/src/LaravelMetrics.php index feaab9a..9f37f1d 100644 --- a/src/LaravelMetrics.php +++ b/src/LaravelMetrics.php @@ -455,7 +455,9 @@ protected function withVariations(int $count = 1): int throw new InvalidPeriodException(); } - $laravelMetrics = (new self($this->builder)); + $laravelMetrics = (new self(DB::table($this->table))) + ->by($this->period, $count) + ->aggregate($this->aggregate, str_replace($this->table . '.', '', $this->column)); $result = match ($this->period) { Period::DAY->value => $laravelMetrics