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

Support subqueries to Builder->update #6789

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 17 additions & 2 deletions src/database/src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2523,9 +2523,24 @@ public function insertOrIgnore(array $values): int
*/
public function update(array $values)
{
$sql = $this->grammar->compileUpdate($this, $values);
$values = collect($values)->map(function ($value) {
if (! $value instanceof Builder) {
return ['value' => $value, 'bindings' => $value];
}

[$query, $bindings] = $this->parseSub($value);

return ['value' => new Expression("({$query})"), 'bindings' => fn () => $bindings];
});

$sql = $this->grammar->compileUpdate($this, $values->map(fn ($value) => $value['value'])->all());

return $this->connection->update($sql, $this->cleanBindings($this->grammar->prepareBindingsForUpdate($this->bindings, $values)));
return $this->connection->update($sql, $this->cleanBindings(
$this->grammar->prepareBindingsForUpdate(
$this->bindings,
$values->map(fn ($value) => $value['bindings'])->all()
)
));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/database/src/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use function Hyperf\Collection\collect;
use function Hyperf\Collection\head;
use function Hyperf\Collection\last;
use function Hyperf\Collection\value;

class Grammar extends BaseGrammar
{
Expand Down Expand Up @@ -228,7 +229,7 @@ public function compileUpsert(Builder $query, array $values, array $uniqueBy, ar
public function prepareBindingsForUpdate(array $bindings, array $values): array
{
$cleanBindings = Arr::except($bindings, ['join', 'select']);

$values = Arr::flatten(array_map(fn ($value) => value($value), $values));
return array_values(
array_merge($bindings['join'], $values, Arr::flatten($cleanBindings))
);
Expand Down
9 changes: 9 additions & 0 deletions src/database/tests/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,15 @@ public function testLeftJoinLateral()
$this->assertSame('select * from `users` left join lateral (select * from `contacts` where `contracts`.`user_id` = `users`.`id`) as `sub` on true', $builder->toSql());
}

public function testUpdateMethodWorksWithQueryAsValue()
{
$builder = $this->getBuilder();
$builder->getConnection()->shouldReceive('update')->once()->with('update "users" set "credits" = (select sum(credits) from "transactions" where "transactions"."user_id" = "users"."id" and "type" = ?) where "id" = ?', ['foo', 1])->andReturn(1);
$result = $builder->from('users')->where('id', '=', 1)->update(['credits' => $this->getBuilder()->from('transactions')->selectRaw('sum(credits)')->whereColumn('transactions.user_id', 'users.id')->where('type', 'foo')]);

$this->assertEquals(1, $result);
}

protected function getBuilder(): Builder
{
$grammar = new Grammar();
Expand Down
Loading