From 2c4cf87242a6e2cdbc6bde3760d5aff915f6510c Mon Sep 17 00:00:00 2001 From: haniha Date: Sun, 30 Jun 2024 12:08:45 +0330 Subject: [PATCH 1/2] Adds support for using castAsJson with a MariaDb connection --- .../Query/Grammars/MariaDbGrammar.php | 11 +++++++++ .../Concerns/InteractsWithDatabaseTest.php | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php b/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php index 74c732c5b990..1ca05c7d1c3c 100755 --- a/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php @@ -32,4 +32,15 @@ public function useLegacyGroupLimit(Builder $query) { return false; } + + /** + * Compile a "JSON value cast" statement into SQL. + * + * @param string $value + * @return string + */ + public function compileJsonValueCast($value) + { + return "json_query($value, '$')"; + } } diff --git a/tests/Testing/Concerns/InteractsWithDatabaseTest.php b/tests/Testing/Concerns/InteractsWithDatabaseTest.php index 927dd317d7eb..497653645292 100644 --- a/tests/Testing/Concerns/InteractsWithDatabaseTest.php +++ b/tests/Testing/Concerns/InteractsWithDatabaseTest.php @@ -4,6 +4,7 @@ use Illuminate\Database\ConnectionInterface; use Illuminate\Database\Query\Expression; +use Illuminate\Database\Query\Grammars\MariaDbGrammar; use Illuminate\Database\Query\Grammars\MySqlGrammar; use Illuminate\Database\Query\Grammars\PostgresGrammar; use Illuminate\Database\Query\Grammars\SQLiteGrammar; @@ -119,6 +120,29 @@ public function testCastToJsonMySql() ); } + public function testCastToJsonMariaDb() + { + $grammar = new MariaDbGrammar(); + + $this->assertEquals(<<<'TEXT' + json_query('["foo","bar"]', '$') + TEXT, + $this->castAsJson(['foo', 'bar'], $grammar) + ); + + $this->assertEquals(<<<'TEXT' + json_query('["foo","bar"]', '$') + TEXT, + $this->castAsJson(collect(['foo', 'bar']), $grammar) + ); + + $this->assertEquals(<<<'TEXT' + json_query('{"foo":"bar"}', '$') + TEXT, + $this->castAsJson((object) ['foo' => 'bar'], $grammar) + ); + } + protected function castAsJson($value, $grammar) { $connection = m::mock(ConnectionInterface::class); From 59aa8f2ba2dc2c817d6c35213b3c3be2004ac27c Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 1 Jul 2024 16:00:13 -0500 Subject: [PATCH 2/2] formatting --- .../Query/Grammars/MariaDbGrammar.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php b/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php index 1ca05c7d1c3c..ca17399fe0dd 100755 --- a/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php @@ -23,24 +23,24 @@ public function compileJoinLateral(JoinLateralClause $join, string $expression): } /** - * Determine whether to use a legacy group limit clause for MySQL < 8.0. + * Compile a "JSON value cast" statement into SQL. * - * @param \Illuminate\Database\Query\Builder $query - * @return bool + * @param string $value + * @return string */ - public function useLegacyGroupLimit(Builder $query) + public function compileJsonValueCast($value) { - return false; + return "json_query({$value}, '$')"; } /** - * Compile a "JSON value cast" statement into SQL. + * Determine whether to use a legacy group limit clause for MySQL < 8.0. * - * @param string $value - * @return string + * @param \Illuminate\Database\Query\Builder $query + * @return bool */ - public function compileJsonValueCast($value) + public function useLegacyGroupLimit(Builder $query) { - return "json_query($value, '$')"; + return false; } }