Skip to content

Commit

Permalink
Merge branch '9.x' of https://github.com/krve/framework into krve-9.x
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 28, 2022
2 parents 14f9a7e + 4bf6ba1 commit 326b85a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
38 changes: 36 additions & 2 deletions Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,33 @@ public function addNestedWhereQuery($query, $boolean = 'and')
return $this;
}

public function havingNested(Closure $callback, $boolean = 'and')
{
call_user_func($callback, $query = $this->forNestedWhere());

return $this->addNestedHavingQuery($query, $boolean);
}

/**
* Add another query builder as a nested where to the query builder.
*
* @param \Illuminate\Database\Query\Builder $query
* @param string $boolean
* @return $this
*/
public function addNestedHavingQuery($query, $boolean = 'and')
{
if (count($query->havings)) {
$type = 'Nested';

$this->havings[] = compact('type', 'query', 'boolean');

$this->addBinding($query->getRawBindings()['having'], 'having');
}

return $this;
}

/**
* Add a full sub-select to the query.
*
Expand Down Expand Up @@ -1924,7 +1951,7 @@ public function groupByRaw($sql, array $bindings = [])
/**
* Add a "having" clause to the query.
*
* @param string $column
* @param \Closure|string $column
* @param string|null $operator
* @param string|null $value
* @param string $boolean
Expand All @@ -1941,6 +1968,13 @@ public function having($column, $operator = null, $value = null, $boolean = 'and
$value, $operator, func_num_args() === 2
);

// If the columns is actually a Closure instance, we will assume the developer
// wants to begin a nested having statement which is wrapped in parenthesis.
// We'll add that Closure to the query then return back out immediately.
if ($column instanceof Closure && is_null($operator)) {
return $this->havingNested($column, $boolean);
}

// If the given operator is not found in the list of valid operators we will
// assume that the developer is just short-cutting the '=' operators and
// we will set the operators to '=' and set the values appropriately.
Expand All @@ -1964,7 +1998,7 @@ public function having($column, $operator = null, $value = null, $boolean = 'and
/**
* Add an "or having" clause to the query.
*
* @param string $column
* @param \Closure|string $column
* @param string|null $operator
* @param string|null $value
* @return $this
Expand Down
32 changes: 24 additions & 8 deletions Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,16 +686,19 @@ protected function compileGroups(Builder $query, $groups)
* @param array $havings
* @return string
*/
protected function compileHavings(Builder $query, $havings)
protected function compileHavings(Builder $query)
{
$sql = implode(' ', array_map([$this, 'compileHaving'], $havings));
$sql = collect($query->havings)->map(function ($having) {
return $having['boolean'].' '.$this->compileHaving($having);
})->implode(' ');

return 'having '.$this->removeLeadingBoolean($sql);
}

/**
* Compile a single having clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $having
* @return string
*/
Expand All @@ -705,7 +708,7 @@ protected function compileHaving(array $having)
// without doing any more processing on it. Otherwise, we will compile the
// clause into SQL based on the components that make it up from builder.
if ($having['type'] === 'Raw') {
return $having['boolean'].' '.$having['sql'];
return $having['sql'];
} elseif ($having['type'] === 'between') {
return $this->compileHavingBetween($having);
} elseif ($having['type'] === 'Null') {
Expand All @@ -714,11 +717,24 @@ protected function compileHaving(array $having)
return $this->compileHavingNotNull($having);
} elseif ($having['type'] === 'bit') {
return $this->compileHavingBit($having);
} elseif ($having['type'] === 'Nested') {
return $this->compileNestedHavings($having);
}

return $this->compileBasicHaving($having);
}

/**
* Compile a nested having clause.
*
* @param array $having
* @return string
*/
protected function compileNestedHavings($having)
{
return '('.substr($this->compileHavings($having['query']), 7).')';
}

/**
* Compile a basic having clause.
*
Expand All @@ -731,7 +747,7 @@ protected function compileBasicHaving($having)

$parameter = $this->parameter($having['value']);

return $having['boolean'].' '.$column.' '.$having['operator'].' '.$parameter;
return $column.' '.$having['operator'].' '.$parameter;
}

/**
Expand All @@ -750,7 +766,7 @@ protected function compileHavingBetween($having)

$max = $this->parameter(last($having['values']));

return $having['boolean'].' '.$column.' '.$between.' '.$min.' and '.$max;
return $column.' '.$between.' '.$min.' and '.$max;
}

/**
Expand All @@ -763,7 +779,7 @@ protected function compileHavingNull($having)
{
$column = $this->wrap($having['column']);

return $having['boolean'].' '.$column.' is null';
return $column.' is null';
}

/**
Expand All @@ -776,7 +792,7 @@ protected function compileHavingNotNull($having)
{
$column = $this->wrap($having['column']);

return $having['boolean'].' '.$column.' is not null';
return $column.' is not null';
}

/**
Expand All @@ -791,7 +807,7 @@ protected function compileHavingBit($having)

$parameter = $this->parameter($having['value']);

return $having['boolean'].' ('.$column.' '.$having['operator'].' '.$parameter.') != 0';
return '('.$column.' '.$having['operator'].' '.$parameter.') != 0';
}

/**
Expand Down

0 comments on commit 326b85a

Please sign in to comment.