-
-
Notifications
You must be signed in to change notification settings - Fork 90
Allow JSON columns to be used as the dateColumn in MySQL #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to use MySqlGrammar
you need to provide the connection information. You also need to add the Trend
class.
Lines 171 to 181 in 5ace11d
protected function getSqlDate(): string | |
{ | |
$adapter = match ($this->builder->getConnection()->getDriverName()) { | |
'mysql', 'mariadb' => new MySqlAdapter(), | |
'sqlite' => new SqliteAdapter(), | |
'pgsql' => new PgsqlAdapter(), | |
default => throw new Error('Unsupported database driver.'), | |
}; | |
return $adapter->format($this->dateColumn, $this->interval); | |
} |
protected function getSqlDate(): string
{
$connection = $this->builder->getConnection();
$adapter = match ($connection->getDriverName()) {
'mysql', 'mariadb' => new MySqlAdapter($connection),
'sqlite' => new SqliteAdapter(),
'pgsql' => new PgsqlAdapter(),
default => throw new Error('Unsupported database driver.'),
};
return $adapter->format($this->dateColumn, $this->interval);
}
src/Adapters/MySqlAdapter.php
Outdated
@@ -3,6 +3,7 @@ | |||
namespace Flowframe\Trend\Adapters; | |||
|
|||
use Error; | |||
use Illuminate\Database\Query\Grammars\MySqlGrammar; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use Illuminate\Database\Query\Grammars\MySqlGrammar; | |
use Illuminate\Database\ConnectionInterface; | |
use Illuminate\Database\Query\Grammars\MySqlGrammar; |
src/Adapters/MySqlAdapter.php
Outdated
|
||
$wrappedColumn = (new MySqlGrammar)->wrap($column); | ||
return "date_format({$wrappedColumn}, '{$format}')"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$wrappedColumn = (new MySqlGrammar)->wrap($column); | |
return "date_format({$wrappedColumn}, '{$format}')"; | |
$wrappedColumn = (new MySqlGrammar($this->connection))->wrap($column); | |
return "date_format({$wrappedColumn}, '{$format}')"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or basically you can do:
Lines 171 to 181 in 5ace11d
protected function getSqlDate(): string | |
{ | |
$adapter = match ($this->builder->getConnection()->getDriverName()) { | |
'mysql', 'mariadb' => new MySqlAdapter(), | |
'sqlite' => new SqliteAdapter(), | |
'pgsql' => new PgsqlAdapter(), | |
default => throw new Error('Unsupported database driver.'), | |
}; | |
return $adapter->format($this->dateColumn, $this->interval); | |
} |
protected function getSqlDate(): string
{
$adapter = match ($this->builder->getConnection()->getDriverName()) {
'mysql', 'mariadb' => new MySqlAdapter(),
'sqlite' => new SqliteAdapter(),
'pgsql' => new PgsqlAdapter(),
default => throw new Error('Unsupported database driver.'),
};
return $adapter->format(
column: $this->builder->getGrammar()->wrap($this->dateColumn), // <--- HERE
interval: $this->interval
);
}
I think that the only place the connection gets used (in the mysql grammar anyways) is escaping raw sql and doing upserts. The code as written works in our environment. But your suggestion of grabbing the connection in the trend instead and pulling the builders grammar is cleaner. Thanks @sarpavci |
@@ -17,7 +17,7 @@ public function format(string $column, string $interval): string | |||
'year' => '%Y', | |||
default => throw new Error('Invalid interval.'), | |||
}; | |||
|
|||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can checkout this file :)
@Larsklopstra Please take a look at this PR. |
What
This PR allows passes the dateColumn through the Illuminate MySQL grammar wrapper so that you can use JSON columns. Fixes #71
Why
We have dates in JSON columns that we would like to use to trend. The "where_between" call already allows this but the "date_format" does not.
How
Simply call the MySqlGrammar->wrap on the column name instead of injecting the string directly into the date format call.