Skip to content

Commit beffa3a

Browse files
authored
Added method Hyperf\Database\Query\Grammars\Grammar::whereValueBetween(). (#7518)
1 parent 40eca7d commit beffa3a

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed

src/Query/Builder.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,6 +1478,48 @@ public function orWhereJsonDoesntContain($column, $value)
14781478
return $this->whereJsonDoesntContain($column, $value, 'or');
14791479
}
14801480

1481+
/**
1482+
* Add a where between columns statement using a value to the query.
1483+
* @param array{Expression|string, Expression|string} $columns
1484+
*/
1485+
public function whereValueBetween(mixed $value, array $columns, string $boolean = 'and', bool $not = false): static
1486+
{
1487+
$type = 'valueBetween';
1488+
1489+
$this->wheres[] = compact('type', 'value', 'columns', 'boolean', 'not');
1490+
1491+
$this->addBinding($value, 'where');
1492+
1493+
return $this;
1494+
}
1495+
1496+
/**
1497+
* Add an or where between columns statement using a value to the query.
1498+
* @param array{Expression|string, Expression|string} $columns
1499+
*/
1500+
public function orWhereValueBetween(mixed $value, array $columns): static
1501+
{
1502+
return $this->whereValueBetween($value, $columns, 'or');
1503+
}
1504+
1505+
/**
1506+
* Add a where not between columns statement using a value to the query.
1507+
* @param array{Expression|string, Expression|string} $columns
1508+
*/
1509+
public function whereValueNotBetween(mixed $value, array $columns, string $boolean = 'and'): static
1510+
{
1511+
return $this->whereValueBetween($value, $columns, $boolean, true);
1512+
}
1513+
1514+
/**
1515+
* Add an or where not between columns statement using a value to the query.
1516+
* @param array{Expression|string, Expression|string} $columns
1517+
*/
1518+
public function orWhereValueNotBetween(mixed $value, array $columns): static
1519+
{
1520+
return $this->whereValueNotBetween($value, $columns, 'or');
1521+
}
1522+
14811523
/**
14821524
* Add a "where JSON overlaps" clause to the query.
14831525
*/

src/Query/Grammars/Grammar.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,20 @@ protected function compileColumns(Builder $query, $columns): ?string
457457
return $select . $this->columnize($columns);
458458
}
459459

460+
/**
461+
* Compile a "value between" where clause.
462+
*/
463+
protected function whereValueBetween(Builder $query, array $where): string
464+
{
465+
$between = $where['not'] ? 'not between' : 'between';
466+
467+
$min = $this->wrap(is_array($where['columns']) ? reset($where['columns']) : $where['columns'][0]);
468+
469+
$max = $this->wrap(is_array($where['columns']) ? end($where['columns']) : $where['columns'][1]);
470+
471+
return $this->parameter($where['value']) . ' ' . $between . ' ' . $min . ' and ' . $max;
472+
}
473+
460474
/**
461475
* Compile the "from" portion of the query.
462476
*

tests/DatabaseQueryBuilderTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,94 @@ protected function tearDown(): void
5656
m::close();
5757
}
5858

59+
public function testWhereValueBetween()
60+
{
61+
$builder = $this->getBuilder();
62+
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
63+
$this->assertSame('select * from "users" where ? between "created_at" and "updated_at"', $builder->toSql());
64+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
65+
66+
$builder = $this->getBuilder();
67+
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
68+
$this->assertSame('select * from "users" where ? between "created_at" and "updated_at"', $builder->toSql());
69+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
70+
71+
$builder = $this->getBuilder();
72+
$builder->select('*')->from('users')->whereValueBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
73+
$this->assertSame('select * from "users" where ? between 1 and 2', $builder->toSql());
74+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
75+
76+
$builder = $this->getBuilder();
77+
$builder->select('*')->from('users')->whereValueBetween(new Raw(1), ['created_at', 'updated_at']);
78+
$this->assertSame('select * from "users" where 1 between "created_at" and "updated_at"', $builder->toSql());
79+
}
80+
81+
public function testOrWhereValueBetween()
82+
{
83+
$builder = $this->getBuilder();
84+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
85+
$this->assertSame('select * from "users" where "id" = ? or ? between "created_at" and "updated_at"', $builder->toSql());
86+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
87+
88+
$builder = $this->getBuilder();
89+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
90+
$this->assertSame('select * from "users" where "id" = ? or ? between "created_at" and "updated_at"', $builder->toSql());
91+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
92+
93+
$builder = $this->getBuilder();
94+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
95+
$this->assertSame('select * from "users" where "id" = ? or ? between 1 and 2', $builder->toSql());
96+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
97+
98+
$builder = $this->getBuilder();
99+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueBetween(new Raw(1), ['created_at', 'updated_at']);
100+
$this->assertSame('select * from "users" where "id" = ? or 1 between "created_at" and "updated_at"', $builder->toSql());
101+
}
102+
103+
public function testWhereValueNotBetween()
104+
{
105+
$builder = $this->getBuilder();
106+
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
107+
$this->assertSame('select * from "users" where ? not between "created_at" and "updated_at"', $builder->toSql());
108+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
109+
110+
$builder = $this->getBuilder();
111+
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
112+
$this->assertSame('select * from "users" where ? not between "created_at" and "updated_at"', $builder->toSql());
113+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
114+
115+
$builder = $this->getBuilder();
116+
$builder->select('*')->from('users')->whereValueNotBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
117+
$this->assertSame('select * from "users" where ? not between 1 and 2', $builder->toSql());
118+
$this->assertEquals([0 => '2020-01-01 19:30:00'], $builder->getBindings());
119+
120+
$builder = $this->getBuilder();
121+
$builder->select('*')->from('users')->whereValueNotBetween(new Raw(1), ['created_at', 'updated_at']);
122+
$this->assertSame('select * from "users" where 1 not between "created_at" and "updated_at"', $builder->toSql());
123+
}
124+
125+
public function testOrWhereValueNotBetween()
126+
{
127+
$builder = $this->getBuilder();
128+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
129+
$this->assertSame('select * from "users" where "id" = ? or ? not between "created_at" and "updated_at"', $builder->toSql());
130+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
131+
132+
$builder = $this->getBuilder();
133+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', ['created_at', 'updated_at']);
134+
$this->assertSame('select * from "users" where "id" = ? or ? not between "created_at" and "updated_at"', $builder->toSql());
135+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
136+
137+
$builder = $this->getBuilder();
138+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween('2020-01-01 19:30:00', [new Raw(1), new Raw(2)]);
139+
$this->assertSame('select * from "users" where "id" = ? or ? not between 1 and 2', $builder->toSql());
140+
$this->assertEquals([0 => 2, 1 => '2020-01-01 19:30:00'], $builder->getBindings());
141+
142+
$builder = $this->getBuilder();
143+
$builder->select('*')->from('users')->where('id', 2)->orWhereValueNotBetween(new Raw(1), ['created_at', 'updated_at']);
144+
$this->assertSame('select * from "users" where "id" = ? or 1 not between "created_at" and "updated_at"', $builder->toSql());
145+
}
146+
59147
public function testBasicSelect(): void
60148
{
61149
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)