Skip to content

Commit 830223a

Browse files
DarkGhostHuntermohammad-fouladgar
authored andcommitted
[12.x] Adds value check between two columns (laravel#56119)
1 parent 529be92 commit 830223a

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

src/Illuminate/Database/Query/Builder.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,63 @@ public function orWhereNotBetweenColumns($column, array $values)
15391539
return $this->whereNotBetweenColumns($column, $values, 'or');
15401540
}
15411541

1542+
/**
1543+
* Add a where between columns statement using a value to the query.
1544+
*
1545+
* @param mixed $value
1546+
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
1547+
* @param string $boolean
1548+
* @param bool $not
1549+
* @return $this
1550+
*/
1551+
public function whereValueBetween($value, array $columns, $boolean = 'and', $not = false)
1552+
{
1553+
$type = 'valueBetween';
1554+
1555+
$this->wheres[] = compact('type', 'value', 'columns', 'boolean', 'not');
1556+
1557+
$this->addBinding($value, 'where');
1558+
1559+
return $this;
1560+
}
1561+
1562+
/**
1563+
* Add an or where between columns statement using a value to the query.
1564+
*
1565+
* @param mixed $value
1566+
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
1567+
* @return $this
1568+
*/
1569+
public function orWhereValueBetween($value, array $columns)
1570+
{
1571+
return $this->whereValueBetween($value, $columns, 'or');
1572+
}
1573+
1574+
/**
1575+
* Add a where not between columns statement using a value to the query.
1576+
*
1577+
* @param mixed $value
1578+
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
1579+
* @param string $boolean
1580+
* @return $this
1581+
*/
1582+
public function whereValueNotBetween($value, array $columns, $boolean = 'and')
1583+
{
1584+
return $this->whereValueBetween($value, $columns, $boolean, true);
1585+
}
1586+
1587+
/**
1588+
* Add an or where not between columns statement using a value to the query.
1589+
*
1590+
* @param mixed $value
1591+
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
1592+
* @return $this
1593+
*/
1594+
public function orWhereValueNotBetween($value, array $columns)
1595+
{
1596+
return $this->whereValueNotBetween($value, $columns, 'or');
1597+
}
1598+
15421599
/**
15431600
* Add an "or where not null" clause to the query.
15441601
*

src/Illuminate/Database/Query/Grammars/Grammar.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,24 @@ protected function whereBetweenColumns(Builder $query, $where)
467467
return $this->wrap($where['column']).' '.$between.' '.$min.' and '.$max;
468468
}
469469

470+
/**
471+
* Compile a "value between" where clause.
472+
*
473+
* @param \Illuminate\Database\Query\Builder $query
474+
* @param array $where
475+
* @return string
476+
*/
477+
protected function whereValueBetween(Builder $query, $where)
478+
{
479+
$between = $where['not'] ? 'not between' : 'between';
480+
481+
$min = $this->wrap(is_array($where['columns']) ? reset($where['columns']) : $where['columns'][0]);
482+
483+
$max = $this->wrap(is_array($where['columns']) ? end($where['columns']) : $where['columns'][1]);
484+
485+
return $this->parameter($where['value']).' '.$between.' '.$min.' and '.$max;
486+
}
487+
470488
/**
471489
* Compile a "where date" clause.
472490
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,94 @@ public function testOrWhereNotBetweenColumns()
11891189
$this->assertEquals([0 => 2], $builder->getBindings());
11901190
}
11911191

1192+
public function testWhereValueBetween()
1193+
{
1194+
$builder = $this->getBuilder();
1195+
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
1196+
$this->assertSame('select * from "users" where ? between "created_at" and "updated_at"', $builder->toSql());
1197+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
1198+
1199+
$builder = $this->getBuilder();
1200+
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
1201+
$this->assertSame('select * from "users" where ? between "created_at" and "updated_at"', $builder->toSql());
1202+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
1203+
1204+
$builder = $this->getBuilder();
1205+
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
1206+
$this->assertSame('select * from "users" where ? between 1 and 2', $builder->toSql());
1207+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
1208+
1209+
$builder = $this->getBuilder();
1210+
$builder->select('*')->from('users')->whereValueBetween(new Raw(1), ['created_at', 'updated_at']);
1211+
$this->assertSame('select * from "users" where 1 between "created_at" and "updated_at"', $builder->toSql());
1212+
}
1213+
1214+
public function testOrWhereValueBetween()
1215+
{
1216+
$builder = $this->getBuilder();
1217+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
1218+
$this->assertSame('select * from "users" where "id" = ? or ? between "created_at" and "updated_at"', $builder->toSql());
1219+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
1220+
1221+
$builder = $this->getBuilder();
1222+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
1223+
$this->assertSame('select * from "users" where "id" = ? or ? between "created_at" and "updated_at"', $builder->toSql());
1224+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
1225+
1226+
$builder = $this->getBuilder();
1227+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
1228+
$this->assertSame('select * from "users" where "id" = ? or ? between 1 and 2', $builder->toSql());
1229+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
1230+
1231+
$builder = $this->getBuilder();
1232+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween(new Raw(1), ['created_at', 'updated_at']);
1233+
$this->assertSame('select * from "users" where "id" = ? or 1 between "created_at" and "updated_at"', $builder->toSql());
1234+
}
1235+
1236+
public function testWhereValueNotBetween()
1237+
{
1238+
$builder = $this->getBuilder();
1239+
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
1240+
$this->assertSame('select * from "users" where ? not between "created_at" and "updated_at"', $builder->toSql());
1241+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
1242+
1243+
$builder = $this->getBuilder();
1244+
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
1245+
$this->assertSame('select * from "users" where ? not between "created_at" and "updated_at"', $builder->toSql());
1246+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
1247+
1248+
$builder = $this->getBuilder();
1249+
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
1250+
$this->assertSame('select * from "users" where ? not between 1 and 2', $builder->toSql());
1251+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
1252+
1253+
$builder = $this->getBuilder();
1254+
$builder->select('*')->from('users')->whereValueNotBetween(new Raw(1), ['created_at', 'updated_at']);
1255+
$this->assertSame('select * from "users" where 1 not between "created_at" and "updated_at"', $builder->toSql());
1256+
}
1257+
1258+
public function testOrWhereValueNotBetween()
1259+
{
1260+
$builder = $this->getBuilder();
1261+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
1262+
$this->assertSame('select * from "users" where "id" = ? or ? not between "created_at" and "updated_at"', $builder->toSql());
1263+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
1264+
1265+
$builder = $this->getBuilder();
1266+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
1267+
$this->assertSame('select * from "users" where "id" = ? or ? not between "created_at" and "updated_at"', $builder->toSql());
1268+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
1269+
1270+
$builder = $this->getBuilder();
1271+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
1272+
$this->assertSame('select * from "users" where "id" = ? or ? not between 1 and 2', $builder->toSql());
1273+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
1274+
1275+
$builder = $this->getBuilder();
1276+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween(new Raw(1), ['created_at', 'updated_at']);
1277+
$this->assertSame('select * from "users" where "id" = ? or 1 not between "created_at" and "updated_at"', $builder->toSql());
1278+
}
1279+
11921280
public function testBasicOrWheres()
11931281
{
11941282
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)