Skip to content

Commit 0de031f

Browse files
hanihahanihataylorotwell
authored
[11.x] Adds support for using castAsJson with a MariaDb connection (#51963)
* Adds support for using castAsJson with a MariaDb connection * formatting --------- Co-authored-by: haniha <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0d06b47 commit 0de031f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ public function compileJoinLateral(JoinLateralClause $join, string $expression):
2222
throw new RuntimeException('This database engine does not support lateral joins.');
2323
}
2424

25+
/**
26+
* Compile a "JSON value cast" statement into SQL.
27+
*
28+
* @param string $value
29+
* @return string
30+
*/
31+
public function compileJsonValueCast($value)
32+
{
33+
return "json_query({$value}, '$')";
34+
}
35+
2536
/**
2637
* Determine whether to use a legacy group limit clause for MySQL < 8.0.
2738
*

tests/Testing/Concerns/InteractsWithDatabaseTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\ConnectionInterface;
66
use Illuminate\Database\Query\Expression;
7+
use Illuminate\Database\Query\Grammars\MariaDbGrammar;
78
use Illuminate\Database\Query\Grammars\MySqlGrammar;
89
use Illuminate\Database\Query\Grammars\PostgresGrammar;
910
use Illuminate\Database\Query\Grammars\SQLiteGrammar;
@@ -119,6 +120,29 @@ public function testCastToJsonMySql()
119120
);
120121
}
121122

123+
public function testCastToJsonMariaDb()
124+
{
125+
$grammar = new MariaDbGrammar();
126+
127+
$this->assertEquals(<<<'TEXT'
128+
json_query('["foo","bar"]', '$')
129+
TEXT,
130+
$this->castAsJson(['foo', 'bar'], $grammar)
131+
);
132+
133+
$this->assertEquals(<<<'TEXT'
134+
json_query('["foo","bar"]', '$')
135+
TEXT,
136+
$this->castAsJson(collect(['foo', 'bar']), $grammar)
137+
);
138+
139+
$this->assertEquals(<<<'TEXT'
140+
json_query('{"foo":"bar"}', '$')
141+
TEXT,
142+
$this->castAsJson((object) ['foo' => 'bar'], $grammar)
143+
);
144+
}
145+
122146
protected function castAsJson($value, $grammar)
123147
{
124148
$connection = m::mock(ConnectionInterface::class);

0 commit comments

Comments
 (0)