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

[6.x] SQLite JSON update support with json_patch #31492

Merged
merged 2 commits into from
Feb 18, 2020
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
47 changes: 44 additions & 3 deletions src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,37 @@ public function compileInsertOrIgnore(Builder $query, array $values)
return Str::replaceFirst('insert', 'insert or ignore', $this->compileInsert($query, $values));
}

/**
* Group the nested JSON columns.
*
* @param array $values
* @return array
*/
protected function groupJsonColumnsForUpdate(array $values)
{
$groups = [];

foreach ($values as $key => $value) {
if ($this->isJsonSelector($key)) {
Arr::set($groups, str_replace('->', '.', Str::after($key, '.')), $value);
}
}

return $groups;
}

/**
* Compile a "JSON" patch statement into SQL.
*
* @param string $column
* @param mixed $value
* @return string
*/
protected function compileJsonPatch($column, $value)
{
return "json_patch(ifnull({$this->wrap($column)}, json('{}')), json({$this->parameter($value)}))";
}

/**
* Compile the columns for an update statement.
*
Expand All @@ -169,10 +200,16 @@ public function compileInsertOrIgnore(Builder $query, array $values)
*/
protected function compileUpdateColumns(Builder $query, array $values)
{
return collect($values)->map(function ($value, $key) {
$groups = $this->groupJsonColumnsForUpdate($values);

return collect($values)->reject(function ($value, $key) {
return $this->isJsonSelector($key);
})->merge($groups)->map(function ($value, $key) use ($groups) {
$column = last(explode('.', $key));

return $this->wrap($column).' = '.$this->parameter($value);
$updateSql = isset($groups[$key]) ? $this->compileJsonPatch($column, $value) : $this->parameter($value);

return $this->wrap($column).' = '.$updateSql;
})->implode(', ');
}

Expand Down Expand Up @@ -205,7 +242,11 @@ protected function compileUpdateWithJoinsOrLimit(Builder $query, array $values)
*/
public function prepareBindingsForUpdate(array $bindings, array $values)
{
$values = collect($values)->map(function ($value) {
$groups = $this->groupJsonColumnsForUpdate($values);

$values = collect($values)->reject(function ($value, $key) {
return $this->isJsonSelector($key);
})->merge($groups)->map(function ($value) {
return is_array($value) ? json_encode($value) : $value;
})->all();

Expand Down
18 changes: 18 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,24 @@ public function testSQLiteUpdateWrappingJsonArray()
]);
}

public function testSQLiteUpdateWrappingNestedJsonArray()
{
$builder = $this->getSQLiteBuilder();
$builder->getConnection()->shouldReceive('update')
->with('update "users" set "group_id" = 45, "created_at" = ?, "options" = json_patch(ifnull("options", json(\'{}\')), json(?))', [
new DateTime('2019-08-06'),
json_encode(['name' => 'Taylor', 'security' => ['2fa' => false, 'presets' => ['laravel', 'vue']], 'sharing' => ['twitter' => 'username']]),
]);

$builder->from('users')->update([
'options->name' => 'Taylor',
'group_id' => new Raw('45'),
'options->security' => ['2fa' => false, 'presets' => ['laravel', 'vue']],
'options->sharing->twitter' => 'username',
'created_at' => new DateTime('2019-08-06'),
]);
}

public function testMySqlWrappingJsonWithString()
{
$builder = $this->getMySqlBuilder();
Expand Down