Skip to content

Commit fac2d77

Browse files
[12.x] Adds value check between two columns (#56119)
1 parent 9f71c7b commit fac2d77

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
@@ -1489,6 +1489,63 @@ public function orWhereNotBetweenColumns($column, array $values)
14891489
return $this->whereNotBetweenColumns($column, $values, 'or');
14901490
}
14911491

1492+
/**
1493+
* Add a where between columns statement using a value to the query.
1494+
*
1495+
* @param mixed $value
1496+
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
1497+
* @param string $boolean
1498+
* @param bool $not
1499+
* @return $this
1500+
*/
1501+
public function whereValueBetween($value, array $columns, $boolean = 'and', $not = false)
1502+
{
1503+
$type = 'valueBetween';
1504+
1505+
$this->wheres[] = compact('type', 'value', 'columns', 'boolean', 'not');
1506+
1507+
$this->addBinding($value, 'where');
1508+
1509+
return $this;
1510+
}
1511+
1512+
/**
1513+
* Add an or where between columns statement using a value to the query.
1514+
*
1515+
* @param mixed $value
1516+
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
1517+
* @return $this
1518+
*/
1519+
public function orWhereValueBetween($value, array $columns)
1520+
{
1521+
return $this->whereValueBetween($value, $columns, 'or');
1522+
}
1523+
1524+
/**
1525+
* Add a where not between columns statement using a value to the query.
1526+
*
1527+
* @param mixed $value
1528+
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
1529+
* @param string $boolean
1530+
* @return $this
1531+
*/
1532+
public function whereValueNotBetween($value, array $columns, $boolean = 'and')
1533+
{
1534+
return $this->whereValueBetween($value, $columns, $boolean, true);
1535+
}
1536+
1537+
/**
1538+
* Add an or where not between columns statement using a value to the query.
1539+
*
1540+
* @param mixed $value
1541+
* @param array{\Illuminate\Contracts\Database\Query\Expression|string, \Illuminate\Contracts\Database\Query\Expression|string} $columns
1542+
* @return $this
1543+
*/
1544+
public function orWhereValueNotBetween($value, array $columns)
1545+
{
1546+
return $this->whereValueNotBetween($value, $columns, 'or');
1547+
}
1548+
14921549
/**
14931550
* Add an "or where not null" clause to the query.
14941551
*

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

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

458+
/**
459+
* Compile a "value between" where clause.
460+
*
461+
* @param \Illuminate\Database\Query\Builder $query
462+
* @param array $where
463+
* @return string
464+
*/
465+
protected function whereValueBetween(Builder $query, $where)
466+
{
467+
$between = $where['not'] ? 'not between' : 'between';
468+
469+
$min = $this->wrap(is_array($where['columns']) ? reset($where['columns']) : $where['columns'][0]);
470+
471+
$max = $this->wrap(is_array($where['columns']) ? end($where['columns']) : $where['columns'][1]);
472+
473+
return $this->parameter($where['value']).' '.$between.' '.$min.' and '.$max;
474+
}
475+
458476
/**
459477
* Compile a "where date" clause.
460478
*

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)