Skip to content

Commit

Permalink
Merge branch 'where-column' of https://github.com/acasar/framework in…
Browse files Browse the repository at this point in the history
…to acasar-where-column
  • Loading branch information
taylorotwell committed May 15, 2016
2 parents 5fa3e4e + ca86102 commit 5de4291
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
55 changes: 51 additions & 4 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,16 +550,17 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
*
* @param array $column
* @param string $boolean
* @param string $method
* @return $this
*/
protected function addArrayOfWheres($column, $boolean)
protected function addArrayOfWheres($column, $boolean, $method = 'where')
{
return $this->whereNested(function ($query) use ($column) {
return $this->whereNested(function ($query) use ($column, $method) {
foreach ($column as $key => $value) {
if (is_numeric($key) && is_array($value)) {
call_user_func_array([$query, 'where'], $value);
call_user_func_array([$query, $method], $value);
} else {
$query->where($key, '=', $value);
$query->$method($key, '=', $value);
}
}
}, $boolean);
Expand All @@ -578,6 +579,52 @@ public function orWhere($column, $operator = null, $value = null)
return $this->where($column, $operator, $value, 'or');
}

/**
* Add a "where" clause comparing two columns to the query.
*
* @param string|array $first
* @param string|null $operator
* @param string|null $second
* @param string|null $boolean
* @return \Illuminate\Database\Query\Builder|static
*/
public function whereColumn($first, $operator = null, $second = null, $boolean = 'and')
{
// If the column is an array, we will assume it is an array of key-value pairs
// and can add them each as a where clause. We will maintain the boolean we
// received when the method was called and pass it into the nested where.
if (is_array($first)) {
return $this->addArrayOfWheres($first, $boolean, 'whereColumn');
}

// 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.
if (! in_array(strtolower($operator), $this->operators, true) &&
! in_array(strtolower($operator), $this->grammar->getOperators(), true)) {
list($second, $operator) = [$operator, '='];
}

$type = 'Column';

$this->wheres[] = compact('type', 'first', 'operator', 'second', 'boolean');

return $this;
}

/**
* Add an "or where" clause comparing two columns to the query.
*
* @param string|array $first
* @param string|null $operator
* @param string|null $second
* @return \Illuminate\Database\Query\Builder|static
*/
public function orWhereColumn($first, $operator = null, $second = null)
{
return $this->whereColumn($first, $operator, $second, 'or');
}

/**
* Determine if the given operator and value combination is legal.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,20 @@ protected function whereBasic(Builder $query, $where)
return $this->wrap($where['column']).' '.$where['operator'].' '.$value;
}

/**
* Compile a where clause comparing two columns..
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereColumn(Builder $query, $where)
{
$second = $this->wrap($where['second']);

return $this->wrap($where['first']).' '.$where['operator'].' '.$second;
}

/**
* Compile a "between" where clause.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,32 @@ public function testEmptyWhereNotIns()
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testBasicWhereColumn()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereColumn('first_name', 'last_name')->orWhereColumn('first_name', 'middle_name');
$this->assertEquals('select * from "users" where "first_name" = "last_name" or "first_name" = "middle_name"', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereColumn('updated_at', '>', 'created_at');
$this->assertEquals('select * from "users" where "updated_at" > "created_at"', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testArrayWhereColumn()
{
$conditions = [
['first_name', 'last_name'],
['updated_at', '>', 'created_at'],
];

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereColumn($conditions);
$this->assertEquals('select * from "users" where ("first_name" = "last_name" and "updated_at" > "created_at")', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testUnions()
{
$builder = $this->getBuilder();
Expand Down

0 comments on commit 5de4291

Please sign in to comment.