Skip to content
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

Support whereAll/orWhereAll whereAny/orWhereAny for Hyperf\Database\Query\Builder. #6767

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG-3.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- [#6757](https://github.com/hyperf/hyperf/pull/6757) Added `Hyperf\Collection\LazyCollection`.
- [#6763](https://github.com/hyperf/hyperf/pull/6763) Added `Premature end of data` into `DetectsLostConnections`.
- [#6767](https://github.com/hyperf/hyperf/pull/6767) Support `whereAll/orWhereAll` `whereAny/orWhereAny` for `Hyperf\Database\Query\Builder`.

# v3.1.22 - 2024-05-16

Expand Down
78 changes: 78 additions & 0 deletions src/database/src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,84 @@ public function orWhereFullText(array|string $columns, string $value, array $opt
return $this->whereFullText($columns, $value, $options, 'or');
}

/**
* Add a "where" clause to the query for multiple columns with "and" conditions between them.
*
* @param string[] $columns
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function whereAll($columns, $operator = null, $value = null, $boolean = 'and')
{
[$value, $operator] = $this->prepareValueAndOperator(
$value,
$operator,
func_num_args() === 2
);

$this->whereNested(function ($query) use ($columns, $operator, $value) {
foreach ($columns as $column) {
$query->where($column, $operator, $value, 'and');
}
}, $boolean);

return $this;
}

/**
* Add an "or where" clause to the query for multiple columns with "and" conditions between them.
*
* @param string[] $columns
* @param string $operator
* @param mixed $value
* @return $this
*/
public function orWhereAll($columns, $operator = null, $value = null)
{
return $this->whereAll($columns, $operator, $value, 'or');
}

/**
* Add an "where" clause to the query for multiple columns with "or" conditions between them.
*
* @param string[] $columns
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function whereAny($columns, $operator = null, $value = null, $boolean = 'and')
{
[$value, $operator] = $this->prepareValueAndOperator(
$value,
$operator,
func_num_args() === 2
);

$this->whereNested(function ($query) use ($columns, $operator, $value) {
foreach ($columns as $column) {
$query->where($column, $operator, $value, 'or');
}
}, $boolean);

return $this;
}

/**
* Add an "or where" clause to the query for multiple columns with "or" conditions between them.
*
* @param string[] $columns
* @param string $operator
* @param mixed $value
* @return $this
*/
public function orWhereAny($columns, $operator = null, $value = null)
{
return $this->whereAny($columns, $operator, $value, 'or');
}

/**
* Add a "group by" clause to the query.
*
Expand Down
62 changes: 62 additions & 0 deletions src/database/tests/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,68 @@ public function testWhereFulltext()
$this->assertEquals(['Car,Plane'], $builder->getBindings());
}

public function testWhereAll()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAll(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where ("last_name" = ? and "email" = ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAll(['last_name', 'email'], 'not like', '%Otwell%');
$this->assertSame('select * from "users" where ("last_name" not like ? and "email" not like ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testOrWhereAll()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAll(['last_name', 'email'], 'like', '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" like ? and "email" like ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereAll(['last_name', 'email'], 'like', '%Otwell%', 'or');
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" like ? and "email" like ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAll(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" = ? and "email" = ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testWhereAny()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAny(['last_name', 'email'], 'like', '%Otwell%');
$this->assertSame('select * from "users" where ("last_name" like ? or "email" like ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereAny(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where ("last_name" = ? or "email" = ?)', $builder->toSql());
$this->assertEquals(['%Otwell%', '%Otwell%'], $builder->getBindings());
}

public function testOrWhereAny()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAny(['last_name', 'email'], 'like', '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" like ? or "email" like ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->whereAny(['last_name', 'email'], 'like', '%Otwell%', 'or');
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" like ? or "email" like ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('first_name', 'like', '%Taylor%')->orWhereAny(['last_name', 'email'], '%Otwell%');
$this->assertSame('select * from "users" where "first_name" like ? or ("last_name" = ? or "email" = ?)', $builder->toSql());
$this->assertEquals(['%Taylor%', '%Otwell%', '%Otwell%'], $builder->getBindings());
}

protected function getBuilder(): Builder
{
$grammar = new Grammar();
Expand Down
Loading