From 94e93d632a84aa9defd0bba0964d67bc0e430542 Mon Sep 17 00:00:00 2001 From: eliseekn Date: Fri, 29 Dec 2023 11:22:43 +0000 Subject: [PATCH] Add PostgreSQL support Update CHANGELOG.md Update README.md --- .idea/php.xml | 5 +++++ CHANGELOG.md | 4 ++++ README.md | 22 +++++++++++----------- src/DatesFunctions.php | 29 ++++++++++++++++++++++++----- src/LaravelMetrics.php | 1 + 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/.idea/php.xml b/.idea/php.xml index 6f43e66..9867cfb 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -1,5 +1,10 @@ + + + + + diff --git a/CHANGELOG.md b/CHANGELOG.md index 9290a47..09652a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-metrics` will be documented in this file +## 2.8.0 + +- Add PostgreSQL support + ## 2.7.4 - Add "from" period to set custom startDate end use the current date as endDate for between period diff --git a/README.md b/README.md index a5f4156..e472225 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ composer require eliseekn/laravel-metrics ``` ## Features -- MySQL support +- MySQL and PostgreSQL support - Verbose query builder - Custom columns and table definition - Days and months translation with Carbon @@ -120,11 +120,11 @@ Order::metrics() ### Types of periods ```php LaravelMetrics::query(...) - ->byDay(int $count = 0) // or - ->byWeek(int $count = 0) // or - ->byMonth(int $count = 0) // or - ->byYear(int $count = 0) // or - ->between(string $startDate, string $endDate, string $dateIsoFormat) + ->byDay(int $count = 0) //or + ->byWeek(int $count = 0) //or + ->byMonth(int $count = 0) //or + ->byYear(int $count = 0) //or + ->between(string $startDate, string $endDate, string $dateIsoFormat) //or ->from(string $date, string $dateIsoFormat) ``` @@ -158,17 +158,17 @@ LaravelMetrics::query(...) ### Types of aggregates ```php LaravelMetrics::query(...) - ->count(string $column = 'id') // or - ->average(string $column) // or - ->sum(string $column) // or - ->max(string $column) // or + ->count(string $column = 'id') //or + ->average(string $column) //or + ->sum(string $column) //or + ->max(string $column) //or ->min(string $column) ``` ### Types of data ```php LaravelMetrics::query(...) - ->trends() // or + ->trends() //or ->metrics() ``` diff --git a/src/DatesFunctions.php b/src/DatesFunctions.php index c209bae..78270ae 100644 --- a/src/DatesFunctions.php +++ b/src/DatesFunctions.php @@ -55,12 +55,31 @@ protected function getMonthPeriod(): array protected function formatPeriod(string $period): string { + $driver = $this->builder->getConnection()->getDriverName(); + + if ($driver === 'mysql') { + return match ($period) { + Period::DAY->value => "weekday($this->dateColumn)", + Period::WEEK->value => "week($this->dateColumn)", + Period::MONTH->value => "month($this->dateColumn)", + default => "year($this->dateColumn)", + }; + } + + if ($driver === 'pgsql') { + return match ($period) { + Period::DAY->value => "EXTRACT(DOW 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)", + }; + } + return match ($period) { - Period::DAY->value => "weekday($this->dateColumn)", - Period::WEEK->value => "week($this->dateColumn)", - Period::MONTH->value => "month($this->dateColumn)", - Period::YEAR->value => "year($this->dateColumn)", - default => '', + Period::DAY->value => "strftime('%w', $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 b465c62..d40ec0b 100644 --- a/src/LaravelMetrics.php +++ b/src/LaravelMetrics.php @@ -114,6 +114,7 @@ public function byYear(int $count = 0): self { return $this->by(Period::YEAR->value, $count); } + public function between(string $start, string $end, string $dateIsoFormat = 'YYYY-MM-DD'): self { $this->checkDateFormat([$start, $end]);