Skip to content

Commit

Permalink
Merge branch '7.x' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Aug 4, 2020
2 parents 8613fa7 + 17cbce1 commit eeb98e4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,24 @@ public function whereBetween($column, array $values, $boolean = 'and', $not = fa
return $this;
}

/**
* Add a where between statement using columns to the query.
*
* @param string $column
* @param array $values
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereBetweenColumns($column, array $values, $boolean = 'and', $not = false)
{
$type = 'betweenColumns';

$this->wheres[] = compact('type', 'column', 'values', 'boolean', 'not');

return $this;
}

/**
* Add an or where between statement to the query.
*
Expand All @@ -1095,6 +1113,18 @@ public function orWhereBetween($column, array $values)
return $this->whereBetween($column, $values, 'or');
}

/**
* Add an or where between statement using columns to the query.
*
* @param string $column
* @param array $values
* @return $this
*/
public function orWhereBetweenColumns($column, array $values)
{
return $this->whereBetweenColumns($column, $values, 'or');
}

/**
* Add a where not between statement to the query.
*
Expand All @@ -1108,6 +1138,19 @@ public function whereNotBetween($column, array $values, $boolean = 'and')
return $this->whereBetween($column, $values, $boolean, true);
}

/**
* Add a where not between statement using columns to the query.
*
* @param string $column
* @param array $values
* @param string $boolean
* @return $this
*/
public function whereNotBetweenColumns($column, array $values, $boolean = 'and')
{
return $this->whereBetweenColumns($column, $values, $boolean, true);
}

/**
* Add an or where not between statement to the query.
*
Expand All @@ -1120,6 +1163,18 @@ public function orWhereNotBetween($column, array $values)
return $this->whereNotBetween($column, $values, 'or');
}

/**
* Add an or where not between statement using columns to the query.
*
* @param string $column
* @param array $values
* @return $this
*/
public function orWhereNotBetweenColumns($column, array $values)
{
return $this->whereNotBetweenColumns($column, $values, 'or');
}

/**
* Add an "or where not null" clause to the query.
*
Expand Down
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,24 @@ protected function whereBetween(Builder $query, $where)
return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
}

/**
* Compile a "between" where clause.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereBetweenColumns(Builder $query, $where)
{
$between = $where['not'] ? 'not between' : 'between';

$min = $this->wrap(reset($where['values']));

$max = $this->wrap(end($where['values']));

return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
}

/**
* Compile a "where date" clause.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function abort($code, $message = '', array $headers = [])
* Throw an HttpException with the given data if the given condition is true.
*
* @param bool $boolean
* @param int $code
* @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code
* @param string $message
* @param array $headers
* @return void
Expand All @@ -71,7 +71,7 @@ function abort_if($boolean, $code, $message = '', array $headers = [])
* Throw an HttpException with the given data unless the given condition is true.
*
* @param bool $boolean
* @param int $code
* @param \Symfony\Component\HttpFoundation\Response|\Illuminate\Contracts\Support\Responsable|int $code
* @param string $message
* @param array $headers
* @return void
Expand Down
18 changes: 18 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,24 @@ public function testWhereBetweens()
$this->assertEquals([], $builder->getBindings());
}

public function testWhereBetweenColumns()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereBetweenColumns('id', ['users.created_at', 'users.updated_at']);
$this->assertSame('select * from "users" where "id" between "users"."created_at" and "users"."updated_at"', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereNotBetweenColumns('id', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where "id" not between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereBetweenColumns('id', [new Raw(1), new Raw(2)]);
$this->assertSame('select * from "users" where "id" between 1 and 2', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

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

0 comments on commit eeb98e4

Please sign in to comment.