Skip to content

Commit

Permalink
[9.x] InteractsWithDatabase::castAsJson($value) incorrectly handles…
Browse files Browse the repository at this point in the history
… SQLite Database (#44196)

* Implement compileJsonCast method in database query grammar

* Use compileJsonCast to retrieve queryable json value

* Add castAsJson tests

* CS fixes

* Fix tests

* Use single quotes with concatenation for consistency

* formatting

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
stevebauman and taylorotwell authored Sep 19, 2022
1 parent f40fe7d commit 98a9954
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,17 @@ protected function compileJsonLength($column, $operator, $value)
throw new RuntimeException('This database engine does not support JSON length operations.');
}

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

/**
* Compile a "where fulltext" clause.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,17 @@ protected function compileJsonLength($column, $operator, $value)
return 'json_length('.$field.$path.') '.$operator.' '.$value;
}

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

/**
* Compile the random statement into SQL.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ protected function compileJsonLength($column, $operator, $value)
return '(select count(*) from openjson('.$field.$path.')) '.$operator.' '.$value;
}

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

/**
* Compile a single having clause.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ public function castAsJson($value)

$value = DB::connection()->getPdo()->quote($value);

return DB::raw("CAST($value AS JSON)");
return DB::raw(
DB::connection()->getQueryGrammar()->compileJsonValueCast($value)
);
}

/**
Expand Down
145 changes: 145 additions & 0 deletions tests/Testing/Concerns/InteractsWithDatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Illuminate\Tests\Testing\Concerns;

use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Query\Grammars\MySqlGrammar;
use Illuminate\Database\Query\Grammars\PostgresGrammar;
use Illuminate\Database\Query\Grammars\SQLiteGrammar;
use Illuminate\Database\Query\Grammars\SqlServerGrammar;
use Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Facade;
use Mockery as m;
use PHPUnit\Framework\TestCase;

class InteractsWithDatabaseTest extends TestCase
{
protected function setUp(): void
{
Facade::clearResolvedInstances();
Facade::setFacadeApplication(null);
}

protected function tearDown(): void
{
m::close();
}

public function testCastToJsonSqlite()
{
$grammar = new SQLiteGrammar();

$this->assertEquals(<<<'TEXT'
'["foo","bar"]'
TEXT,
$this->castAsJson(['foo', 'bar'], $grammar)
);

$this->assertEquals(<<<'TEXT'
'["foo","bar"]'
TEXT,
$this->castAsJson(collect(['foo', 'bar']), $grammar)
);

$this->assertEquals(<<<'TEXT'
'{"foo":"bar"}'
TEXT,
$this->castAsJson((object) ['foo' => 'bar'], $grammar)
);
}

public function testCastToJsonPostgres()
{
$grammar = new PostgresGrammar();

$this->assertEquals(<<<'TEXT'
'["foo","bar"]'
TEXT,
$this->castAsJson(['foo', 'bar'], $grammar)
);

$this->assertEquals(<<<'TEXT'
'["foo","bar"]'
TEXT,
$this->castAsJson(collect(['foo', 'bar']), $grammar)
);

$this->assertEquals(<<<'TEXT'
'{"foo":"bar"}'
TEXT,
$this->castAsJson((object) ['foo' => 'bar'], $grammar)
);
}

public function testCastToJsonSqlServer()
{
$grammar = new SqlServerGrammar();

$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)
);
}

public function testCastToJsonMySql()
{
$grammar = new MySqlGrammar();

$this->assertEquals(<<<'TEXT'
cast('["foo","bar"]' as json)
TEXT,
$this->castAsJson(['foo', 'bar'], $grammar)
);

$this->assertEquals(<<<'TEXT'
cast('["foo","bar"]' as json)
TEXT,
$this->castAsJson(collect(['foo', 'bar']), $grammar)
);

$this->assertEquals(<<<'TEXT'
cast('{"foo":"bar"}' as json)
TEXT,
$this->castAsJson((object) ['foo' => 'bar'], $grammar)
);
}

protected function castAsJson($value, $grammar)
{
$connection = m::mock(ConnectionInterface::class);

$connection->shouldReceive('getQueryGrammar')->andReturn($grammar);

$connection->shouldReceive('getPdo->quote')->andReturnUsing(function ($value) {
return "'".$value."'";
});

DB::shouldReceive('connection')->andReturn($connection);

DB::shouldReceive('raw')->andReturnUsing(function ($value) {
return new Expression($value);
});

$instance = new class
{
use InteractsWithDatabase;
};

return $instance->castAsJson($value)->getValue();
}
}

0 comments on commit 98a9954

Please sign in to comment.