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 7 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 @@ -611,6 +611,17 @@ protected function compileJsonContains($column, $value)
throw new RuntimeException('This database engine does not support JSON contains operations.');
}

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

/**
* Prepare the binding for a "JSON contains" statement.
*
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 @@ -113,6 +113,17 @@ protected function compileJsonContainsKey($column)
return 'ifnull(json_contains_path('.$field.', \'one\''.$path.'), 0)';
}

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

/**
* Compile a "JSON length" 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 @@ -154,6 +154,17 @@ protected function compileJsonContains($column, $value)
return $value.' in (select [value] from openjson('.$field.$path.'))';
}

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

/**
* Prepare the binding for a "JSON contains" statement.
*
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()->compileJsonCast($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();
}
}