Skip to content

Commit

Permalink
Optimized the code of `Hyperf\Database\Query\Grammars\Grammar::compil…
Browse files Browse the repository at this point in the history
…eUpdate`. (#7082)
  • Loading branch information
albertcht authored Sep 25, 2024
1 parent eb98c75 commit fe8d71d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
18 changes: 12 additions & 6 deletions src/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,15 @@ public function compileInsertOrIgnoreUsing(Builder $query, array $columns, strin

/**
* Compile an update statement into SQL.
*
* @param array $values
*/
public function compileUpdate(Builder $query, $values): string
public function compileUpdate(Builder $query, array $values): string
{
$table = $this->wrapTable($query->from);

// Each one of the columns in the update statements needs to be wrapped in the
// keyword identifiers, also a place-holder needs to be created for each of
// the values in the list of bindings so we can make the sets statements.
$columns = collect($values)->map(function ($value, $key) {
return $this->wrap($key) . ' = ' . $this->parameter($value);
})->implode(', ');
$columns = $this->compileUpdateColumns($query, $values);

// If the query has any "join" clauses, we will setup the joins on the builder
// and compile them so we can attach them to this update, as update queries
Expand Down Expand Up @@ -371,6 +367,16 @@ public function compileJoinLateral(JoinLateralClause $join, string $expression):
throw new RuntimeException('This database engine does not support lateral joins.');
}

/**
* Compile the columns for an update statement.
*/
protected function compileUpdateColumns(Builder $query, array $values): string
{
return collect($values)->map(function ($value, $key) {
return $this->wrap($key) . ' = ' . $this->parameter($value);
})->implode(', ');
}

/**
* Compile a "where JSON overlaps" clause.
*/
Expand Down
10 changes: 3 additions & 7 deletions src/Query/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,15 @@ public function compileRandom($seed): string

/**
* Compile an update statement into SQL.
*
* @param array $values
*/
public function compileUpdate(Builder $query, $values): string
public function compileUpdate(Builder $query, array $values): string
{
$table = $this->wrapTable($query->from);

// Each one of the columns in the update statements needs to be wrapped in the
// keyword identifiers, also a place-holder needs to be created for each of
// the values in the list of bindings so we can make the sets statements.
$columns = $this->compileUpdateColumns($values);
$columns = $this->compileUpdateColumns($query, $values);

// If the query has any "join" clauses, we will setup the joins on the builder
// and compile them so we can attach them to this update, as update queries
Expand Down Expand Up @@ -288,10 +286,8 @@ protected function compileLock(Builder $query, $value): string

/**
* Compile all of the columns for an update statement.
*
* @param array $values
*/
protected function compileUpdateColumns($values): string
protected function compileUpdateColumns(Builder $query, array $values): string
{
return collect($values)->map(function ($value, $key) {
if ($this->isJsonSelector($key)) {
Expand Down

0 comments on commit fe8d71d

Please sign in to comment.