10000 [12.x] Adds checking if a value is between two columns by DarkGhostHunter · Pull Request #56119 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Adds checking if a value is between two columns #56119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1489,6 +1489,63 @@ public function orWhereNotBetweenColumns($column, array $values)
return $this->whereNotBetweenColumns($column, $values, 'or');
}

/**
* Add a where between columns statement using a value to the query.
*
* @param mixed $value
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
* @param string $boolean
* @param bool $not
* @return $this
*/
public function whereValueBetween($value, array $columns, $boolean = 'and', $not = false)
{
$type = 'valueBetween';

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

$this->addBinding($value, 'where');

return $this;
}

/**
* Add an or where between columns statement using a value to the query.
*
* @param mixed $value
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
* @return $this
*/
public function orWhereValueBetween($value, array $columns)
{
return $this->whereValueBetween($value, $columns, 'or');
}

/**
* Add a where not between columns statement using a value to the query.
*
* @param mixed $value
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
* @param string $boolean
* @return $this
*/
public function whereValueNotBetween($value, array $columns, $boolean = 'and')
{
return $this->whereValueBetween($value, $columns, $boolean, true);
}

/**
* Add an or where not between columns statement using a value to the query.
*
* @param mixed $value
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
* @return $this
*/
public function orWhereValueNotBetween($value, array $columns)
{
return $this->whereValueNotBetween($value, $columns, '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 @@ -455,6 +455,24 @@ protected function whereBetweenColumns(Builder $query, $where)
return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
}

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

$min = $this->wrap(is_array($where['columns']) ? reset($where['columns']) : $where['columns'][0]);

$max = $this->wrap(is_array($where['columns']) ? end($where['columns']) : $where['columns'][1]);

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

/**
* Compile a "where date" clause.
*
Expand Down
88 changes: 88 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,94 @@ public function testOrWhereNotBetweenColumns()
$this->assertEquals([0 => 2], $builder->getBindings());
}

public function testWhereValueBetween()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where ? between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where ? between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
$this->assertSame('select * from "users" where ? between 1 and 2', $builder->toSql());
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereValueBetween(new Raw(1), ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where 1 between "created_at" and "updated_at"', $builder->toSql());
}

public function testOrWhereValueBetween()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where "id" = ? or ? between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where "id" = ? or ? between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
$this->assertSame('select * from "users" where "id" = ? or ? between 1 and 2', $builder->toSql());
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween(new Raw(1), ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where "id" = ? or 1 between "created_at" and "updated_at"', $builder->toSql());
}

public function testWhereValueNotBetween()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where ? not between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where ? not between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
$this->assertSame('select * from "users" where ? not between 1 and 2', $builder->toSql());
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereValueNotBetween(new Raw(1), ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where 1 not between "created_at" and "updated_at"', $builder->toSql());
}

public function testOrWhereValueNotBetween()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where "id" = ? or ? not between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where "id" = ? or ? not between "created_at" and "updated_at"', $builder->toSql());
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
$this->assertSame('select * from "users" where "id" = ? or ? not between 1 and 2', $builder->toSql());
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween(new Raw(1), ['created_at', 'updated_at']);
$this->assertSame('select * from "users" where "id" = ? or 1 not between "created_at" and "updated_at"', $builder->toSql());
}

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