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

[5.7] Fix whereIntegerInRaw() with "not" argument and add whereIntegerNotInRaw() #26531

Merged
merged 4 commits into from
Nov 16, 2018
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
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,19 @@ public function whereIntegerInRaw($column, $values, $boolean = 'and', $not = fal
return $this;
}

/**
* Add a "where not in raw" clause for integer values to the query.
*
* @param string $column
* @param \Illuminate\Contracts\Support\Arrayable|array $values
* @param string $boolean
* @return $this
*/
public function whereIntegerNotInRaw($column, $values, $boolean = 'and')
{
return $this->whereIntegerInRaw($column, $values, $boolean, true);
}

/**
* Add a "where 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 @@ -277,6 +277,24 @@ protected function whereNotIn(Builder $query, $where)
return '1 = 1';
}

/**
* Compile a "where not in raw" clause.
*
* For safety, whereIntegerInRaw ensures this method is only used with integer values.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array $where
* @return string
*/
protected function whereNotInRaw(Builder $query, $where)
{
if (! empty($where['values'])) {
return $this->wrap($where['column']).' not in ('.implode(', ', $where['values']).')';
}

return '1 = 1';
}

/**
* Compile a where in sub-select clause.
*
Expand Down
26 changes: 25 additions & 1 deletion tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,14 +693,38 @@ public function testEmptyWhereNotIns()
$this->assertEquals([0 => 1], $builder->getBindings());
}

public function testwhereIntegerInRaw()
public function testWhereIntegerInRaw()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereIntegerInRaw('id', ['1a', 2]);
$this->assertEquals('select * from "users" where "id" in (1, 2)', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testWhereIntegerNotInRaw()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereIntegerNotInRaw('id', ['1a', 2]);
$this->assertEquals('select * from "users" where "id" not in (1, 2)', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testEmptyWhereIntegerInRaw()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereIntegerInRaw('id', []);
$this->assertEquals('select * from "users" where 0 = 1', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

public function testEmptyWhereIntegerNotInRaw()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->whereIntegerNotInRaw('id', []);
$this->assertEquals('select * from "users" where 1 = 1', $builder->toSql());
$this->assertEquals([], $builder->getBindings());
}

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