Skip to content

Commit

Permalink
Merge pull request #2 from tombenevides/feature/support-postgresql
Browse files Browse the repository at this point in the history
Support PostgreSQL
  • Loading branch information
Larsklopstra authored Nov 1, 2021
2 parents 6fda6e4 + 5f732c9 commit 9074e23
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/drivers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ We currently support two drivers:

- MySQL
- Sqlite
- PostgreSQL
22 changes: 22 additions & 0 deletions src/Adapters/PgsqlAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Flowframe\Trend\Adapters;

use Error;

class PgsqlAdapter extends AbstractAdapter
{
public function format(string $column, string $interval): string
{
$format = match ($interval) {
'minute' => 'YYYY-MM-DD HH24:MI:00',
'hour' => 'YYYY-MM-DD HH24:00:00',
'day' => 'YYYY-MM-DD',
'month' => 'YYYY-MM',
'year' => 'YYYY',
default => throw new Error('Invalid interval.'),
};

return "to_char({$column}, '{$format}')";
}
}
2 changes: 2 additions & 0 deletions src/Trend.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\CarbonPeriod;
use Error;
use Flowframe\Trend\Adapters\MySqlAdapter;
use Flowframe\Trend\Adapters\PgsqlAdapter;
use Flowframe\Trend\Adapters\SqliteAdapter;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
Expand Down Expand Up @@ -157,6 +158,7 @@ protected function getSqlDate(): string
$adapter = match ($this->builder->getConnection()->getDriverName()) {
'mysql' => new MySqlAdapter(),
'sqlite' => new SqliteAdapter(),
'pgsql' => new PgsqlAdapter(),
default => throw new Error('Unsupported database driver.'),
};

Expand Down

0 comments on commit 9074e23

Please sign in to comment.