dep: package update
This commit is contained in:
+27
-5
@@ -20,6 +20,7 @@ use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Traits\ForwardsCalls;
|
||||
use Illuminate\Support\Traits\Macroable;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
use RuntimeException;
|
||||
|
||||
class Builder
|
||||
@@ -1960,7 +1961,7 @@ class Builder
|
||||
/**
|
||||
* Add an "order by" clause to the query.
|
||||
*
|
||||
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column
|
||||
* @param \Closure|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column
|
||||
* @param string $direction
|
||||
* @return $this
|
||||
*
|
||||
@@ -1993,7 +1994,7 @@ class Builder
|
||||
/**
|
||||
* Add a descending "order by" clause to the query.
|
||||
*
|
||||
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column
|
||||
* @param \Closure|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column
|
||||
* @return $this
|
||||
*/
|
||||
public function orderByDesc($column)
|
||||
@@ -2004,7 +2005,7 @@ class Builder
|
||||
/**
|
||||
* Add an "order by" clause for a timestamp to the query.
|
||||
*
|
||||
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column
|
||||
* @param \Closure|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column
|
||||
* @return $this
|
||||
*/
|
||||
public function latest($column = 'created_at')
|
||||
@@ -2015,7 +2016,7 @@ class Builder
|
||||
/**
|
||||
* Add an "order by" clause for a timestamp to the query.
|
||||
*
|
||||
* @param \Closure|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column
|
||||
* @param \Closure|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder|\Illuminate\Database\Query\Expression|string $column
|
||||
* @return $this
|
||||
*/
|
||||
public function oldest($column = 'created_at')
|
||||
@@ -3000,6 +3001,27 @@ class Builder
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update records in a PostgreSQL database using the update from syntax.
|
||||
*
|
||||
* @param array $values
|
||||
* @return int
|
||||
*/
|
||||
public function updateFrom(array $values)
|
||||
{
|
||||
if (! method_exists($this->grammar, 'compileUpdateFrom')) {
|
||||
throw new LogicException('This database engine does not support the updateFrom method.');
|
||||
}
|
||||
|
||||
$this->applyBeforeQueryCallbacks();
|
||||
|
||||
$sql = $this->grammar->compileUpdateFrom($this, $values);
|
||||
|
||||
return $this->connection->update($sql, $this->cleanBindings(
|
||||
$this->grammar->prepareBindingsForUpdateFrom($this->bindings, $values)
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert or update a record matching the attributes, and fill it with values.
|
||||
*
|
||||
@@ -3402,7 +3424,7 @@ class Builder
|
||||
/**
|
||||
* Die and dump the current SQL and bindings.
|
||||
*
|
||||
* @return void
|
||||
* @return never
|
||||
*/
|
||||
public function dd()
|
||||
{
|
||||
|
||||
@@ -260,6 +260,114 @@ class PostgresGrammar extends Grammar
|
||||
return "{$field} = jsonb_set({$field}::jsonb, {$path}, {$this->parameter($value)})";
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile an update from statement into SQL.
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $query
|
||||
* @param array $values
|
||||
* @return string
|
||||
*/
|
||||
public function compileUpdateFrom(Builder $query, $values)
|
||||
{
|
||||
$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($query, $values);
|
||||
|
||||
$from = '';
|
||||
|
||||
if (isset($query->joins)) {
|
||||
// When using Postgres, updates with joins list the joined tables in the from
|
||||
// clause, which is different than other systems like MySQL. Here, we will
|
||||
// compile out the tables that are joined and add them to a from clause.
|
||||
$froms = collect($query->joins)->map(function ($join) {
|
||||
return $this->wrapTable($join->table);
|
||||
})->all();
|
||||
|
||||
if (count($froms) > 0) {
|
||||
$from = ' from '.implode(', ', $froms);
|
||||
}
|
||||
}
|
||||
|
||||
$where = $this->compileUpdateWheres($query);
|
||||
|
||||
return trim("update {$table} set {$columns}{$from} {$where}");
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the additional where clauses for updates with joins.
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $query
|
||||
* @return string
|
||||
*/
|
||||
protected function compileUpdateWheres(Builder $query)
|
||||
{
|
||||
$baseWheres = $this->compileWheres($query);
|
||||
|
||||
if (! isset($query->joins)) {
|
||||
return $baseWheres;
|
||||
}
|
||||
|
||||
// Once we compile the join constraints, we will either use them as the where
|
||||
// clause or append them to the existing base where clauses. If we need to
|
||||
// strip the leading boolean we will do so when using as the only where.
|
||||
$joinWheres = $this->compileUpdateJoinWheres($query);
|
||||
|
||||
if (trim($baseWheres) == '') {
|
||||
return 'where '.$this->removeLeadingBoolean($joinWheres);
|
||||
}
|
||||
|
||||
return $baseWheres.' '.$joinWheres;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile the "join" clause where clauses for an update.
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $query
|
||||
* @return string
|
||||
*/
|
||||
protected function compileUpdateJoinWheres(Builder $query)
|
||||
{
|
||||
$joinWheres = [];
|
||||
|
||||
// Here we will just loop through all of the join constraints and compile them
|
||||
// all out then implode them. This should give us "where" like syntax after
|
||||
// everything has been built and then we will join it to the real wheres.
|
||||
foreach ($query->joins as $join) {
|
||||
foreach ($join->wheres as $where) {
|
||||
$method = "where{$where['type']}";
|
||||
|
||||
$joinWheres[] = $where['boolean'].' '.$this->$method($query, $where);
|
||||
}
|
||||
}
|
||||
|
||||
return implode(' ', $joinWheres);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare the bindings for an update statement.
|
||||
*
|
||||
* @param array $bindings
|
||||
* @param array $values
|
||||
* @return array
|
||||
*/
|
||||
public function prepareBindingsForUpdateFrom(array $bindings, array $values)
|
||||
{
|
||||
$values = collect($values)->map(function ($value, $column) {
|
||||
return is_array($value) || ($this->isJsonSelector($column) && ! $this->isExpression($value))
|
||||
? json_encode($value)
|
||||
: $value;
|
||||
})->all();
|
||||
|
||||
$bindingsWithoutWhere = Arr::except($bindings, ['select', 'where']);
|
||||
|
||||
return array_values(
|
||||
array_merge($values, $bindings['where'], Arr::flatten($bindingsWithoutWhere))
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile an update statement with joins or limit into SQL.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user