Skip to content
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

[9.x] InteractsWithDatabase::castAsJson($value) incorrectly handles SQLite Database #44196

Merged
merged 8 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
stevebauman marked this conversation as resolved.
Show resolved Hide resolved
'["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();
}
}