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

[8.x] Add better bitwise operators support #40529

Merged
merged 2 commits into from
Jan 22, 2022
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
23 changes: 23 additions & 0 deletions src/Illuminate/Database/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ class Builder
'not similar to', 'not ilike', '~~*', '!~~*',
];

/**
* All of the available binary operators.
*
* @var string[]
*/
public $binaryOperators = [
'&', '|', '^', '<<', '>>', '&~',
];

/**
* Whether to use write pdo for the select.
*
Expand Down Expand Up @@ -754,6 +763,10 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
}
}

if ($this->isBinaryOperator($operator)) {
$type = 'Binary';
}

// Now that we are working with just a simple query we can put the elements
// in our array and add the query binding to our array of bindings that
// will be bound to each SQL statements when it is finally executed.
Expand Down Expand Up @@ -837,6 +850,12 @@ protected function invalidOperator($operator)
! in_array(strtolower($operator), $this->grammar->getOperators(), true);
}

protected function isBinaryOperator($operator)
{
return in_array(strtolower($operator), $this->binaryOperators, true) ||
in_array(strtolower($operator), $this->grammar->getBinaryOperators(), true);
Comment on lines +855 to +856
Copy link
Contributor

@Jubeki Jubeki Jan 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your reason for calling strtolower($operator)? In my opinion the operator has no lower or upper case.

Suggested change
return in_array(strtolower($operator), $this->binaryOperators, true) ||
in_array(strtolower($operator), $this->grammar->getBinaryOperators(), true);
return in_array($operator, $this->binaryOperators, true) ||
in_array($operator, $this->grammar->getBinaryOperators(), true);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None in particular, I copied what I saw in the invalidOperator.

}

/**
* Add an "or where" clause to the query.
*
Expand Down Expand Up @@ -1915,6 +1934,10 @@ public function having($column, $operator = null, $value = null, $boolean = 'and
[$value, $operator] = [$operator, '='];
}

if ($this->isBinaryOperator($operator)) {
$type = 'binary';
}

$this->havings[] = compact('type', 'column', 'operator', 'value', 'boolean');

if (! $value instanceof Expression) {
Expand Down
50 changes: 48 additions & 2 deletions src/Illuminate/Database/Query/Grammars/Grammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class Grammar extends BaseGrammar
*/
protected $operators = [];

/**
* The grammar specific binary operators.
*
* @var array
*/
protected $binaryOperators = [];

/**
* The components that make up a select clause.
*
Expand Down Expand Up @@ -255,6 +262,15 @@ protected function whereBasic(Builder $query, $where)
return $this->wrap($where['column']).' '.$operator.' '.$value;
}

protected function whereBinary(Builder $query, $where)
{
$value = $this->parameter($where['value']);

$operator = str_replace('?', '??', $where['operator']);

return '('.$this->wrap($where['column']).' '.$operator.' '.$value.') != 0';
}

/**
* Compile a "where in" clause.
*
Expand Down Expand Up @@ -571,7 +587,8 @@ protected function whereJsonContains(Builder $query, $where)
$not = $where['not'] ? 'not ' : '';

return $not.$this->compileJsonContains(
$where['column'], $this->parameter($where['value'])
$where['column'],
$this->parameter($where['value'])
);
}

Expand Down Expand Up @@ -610,7 +627,9 @@ public function prepareBindingForJsonContains($binding)
protected function whereJsonLength(Builder $query, $where)
{
return $this->compileJsonLength(
$where['column'], $where['operator'], $this->parameter($where['value'])
$where['column'],
$where['operator'],
$this->parameter($where['value'])
);
}

Expand Down Expand Up @@ -682,6 +701,8 @@ protected function compileHaving(array $having)
return $having['boolean'].' '.$having['sql'];
} elseif ($having['type'] === 'between') {
return $this->compileHavingBetween($having);
} elseif ($having['type'] === 'binary') {
return $this->compileHavingBinary($having);
}

return $this->compileBasicHaving($having);
Expand Down Expand Up @@ -721,6 +742,21 @@ protected function compileHavingBetween($having)
return $having['boolean'].' '.$column.' '.$between.' '.$min.' and '.$max;
}

/**
* Compile a having clause involving a binary operator.
*
* @param array $having
* @return string
*/
protected function compileHavingBinary($having)
{
$column = $this->wrap($having['column']);

$parameter = $this->parameter($having['value']);

return $having['boolean'].' ('.$column.' '.$having['operator'].' '.$parameter.') != 0';
}

/**
* Compile the "order by" portions of the query.
*
Expand Down Expand Up @@ -1296,4 +1332,14 @@ public function getOperators()
{
return $this->operators;
}

/**
* Get the grammar specific binary operators.
*
* @return array
*/
public function getBinaryOperators()
{
return $this->binaryOperators;
}
}
9 changes: 9 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ class PostgresGrammar extends Grammar
'is distinct from', 'is not distinct from',
];

/**
* The grammar specific binary operators.
*
* @var array
*/
protected $binaryOperators = [
'~', '&', '|', '#', '<<', '>>', '<<=', '>>=',
];

/**
* {@inheritdoc}
*
Expand Down
2 changes: 2 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,7 @@ protected function addMockConnection($model)
$model->setConnectionResolver($resolver = m::mock(ConnectionResolverInterface::class));
$resolver->shouldReceive('connection')->andReturn($connection = m::mock(Connection::class));
$connection->shouldReceive('getQueryGrammar')->andReturn($grammar = m::mock(Grammar::class));
$grammar->shouldReceive('getBinaryOperators')->andReturn([]);
$connection->shouldReceive('getPostProcessor')->andReturn($processor = m::mock(Processor::class));
$connection->shouldReceive('query')->andReturnUsing(function () use ($connection, $grammar, $processor) {
return new BaseBuilder($connection, $grammar, $processor);
Expand Down Expand Up @@ -2440,6 +2441,7 @@ public function getConnection()
{
$mock = m::mock(Connection::class);
$mock->shouldReceive('getQueryGrammar')->andReturn($grammar = m::mock(Grammar::class));
$grammar->shouldReceive('getBinaryOperators')->andReturn([]);
$mock->shouldReceive('getPostProcessor')->andReturn($processor = m::mock(Processor::class));
$mock->shouldReceive('getName')->andReturn('name');
$mock->shouldReceive('query')->andReturnUsing(function () use ($mock, $grammar, $processor) {
Expand Down
19 changes: 19 additions & 0 deletions tests/Database/DatabaseQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3209,6 +3209,25 @@ public function testMySqlSoundsLikeOperator()
$this->assertEquals(['John Doe'], $builder->getBindings());
}

public function testBinaryOperators()
{
$builder = $this->getBuilder();
$builder->select('*')->from('users')->where('bar', '&', 1);
$this->assertSame('select * from "users" where ("bar" & ?) != 0', $builder->toSql());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->where('bar', '#', 1);
$this->assertSame('select * from "users" where ("bar" # ?) != 0', $builder->toSql());

$builder = $this->getBuilder();
$builder->select('*')->from('users')->having('bar', '&', 1);
$this->assertSame('select * from "users" having ("bar" & ?) != 0', $builder->toSql());

$builder = $this->getPostgresBuilder();
$builder->select('*')->from('users')->having('bar', '#', 1);
$this->assertSame('select * from "users" having ("bar" # ?) != 0', $builder->toSql());
}

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