Skip to content

Commit

Permalink
[11.x] Adds support for using castAsJson with a MariaDb connection (#…
Browse files Browse the repository at this point in the history
…51963)

* Adds support for using castAsJson with a MariaDb connection

* formatting

---------

Co-authored-by: haniha <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Jul 1, 2024
1 parent 0d06b47 commit 0de031f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Query/Grammars/MariaDbGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ public function compileJoinLateral(JoinLateralClause $join, string $expression):
throw new RuntimeException('This database engine does not support lateral joins.');
}

/**
* Compile a "JSON value cast" statement into SQL.
*
* @param string $value
* @return string
*/
public function compileJsonValueCast($value)
{
return "json_query({$value}, '$')";
}

/**
* Determine whether to use a legacy group limit clause for MySQL < 8.0.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/Testing/Concerns/InteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0de031f

Please sign in to comment.