Skip to content

Commit 75ee960

Browse files
author
MohsenEtemadi
committed
Add native bitwise operator support to Query Builder and Eloquent
[12.x] Add native bitwise operator support to Query Builder and Eloquent fix: fix style ci
1 parent c9af3bc commit 75ee960

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

src/Illuminate/Database/Eloquent/Builder.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,76 @@ public function orWhereNot($column, $operator = null, $value = null)
407407
return $this->whereNot($column, $operator, $value, 'or');
408408
}
409409

410+
/**
411+
* Add a bitwise scope to the query.
412+
*
413+
* @param string $column
414+
* @param int $value
415+
* @param string $boolean
416+
* @return $this
417+
*/
418+
public function whereBitwise($column, $value, $boolean = 'and')
419+
{
420+
$this->query->whereBitwise($column, $value, $boolean);
421+
422+
return $this;
423+
}
424+
425+
/**
426+
* Add a bitwise exact match scope to the query.
427+
*
428+
* @param string $column
429+
* @param int $mask
430+
* @param int $value
431+
* @param string $boolean
432+
* @return $this
433+
*/
434+
public function whereBitwiseExact($column, $mask, $value, $boolean = 'and')
435+
{
436+
$this->query->whereBitwiseExact($column, $mask, $value, $boolean);
437+
438+
return $this;
439+
}
440+
441+
/**
442+
* Add an "or where bitwise" clause to the query.
443+
*
444+
* @param string $column
445+
* @param int $value
446+
* @return $this
447+
*/
448+
public function orWhereBitwise($column, $value)
449+
{
450+
return $this->whereBitwise($column, $value, 'or');
451+
}
452+
453+
/**
454+
* Add a "where not bitwise" clause to the query.
455+
*
456+
* @param string $column
457+
* @param int $value
458+
* @param string $boolean
459+
* @return $this
460+
*/
461+
public function whereNotBitwise($column, $value, $boolean = 'and')
462+
{
463+
$this->query->whereNotBitwise($column, $value, $boolean);
464+
465+
return $this;
466+
}
467+
468+
/**
469+
* Add an "or where not bitwise" clause to the query.
470+
*
471+
* @param string $column
472+
* @param int $value
473+
* @return $this
474+
*/
475+
public function orWhereNotBitwise($column, $value)
476+
{
477+
return $this->whereNotBitwise($column, $value, 'or');
478+
}
479+
410480
/**
411481
* Add an "order by" clause for a timestamp to the query.
412482
*

src/Illuminate/Database/Query/Builder.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2431,6 +2431,70 @@ public function orWhereNone($columns, $operator = null, $value = null)
24312431
return $this->whereNone($columns, $operator, $value, 'or');
24322432
}
24332433

2434+
/**
2435+
* Add a bitwise "AND" clause to the query.
2436+
*
2437+
* @param string $column
2438+
* @param int $value
2439+
* @param string $boolean
2440+
* @return $this
2441+
*/
2442+
public function whereBitwise($column, $value, $boolean = 'and')
2443+
{
2444+
return $this->whereRaw("{$column} & ? > 0", [$value], $boolean);
2445+
}
2446+
2447+
/**
2448+
* Add a bitwise "AND" clause with exact match to the query.
2449+
*
2450+
* @param string $column
2451+
* @param int $mask
2452+
* @param int $value
2453+
* @param string $boolean
2454+
* @return $this
2455+
*/
2456+
public function whereBitwiseExact($column, $mask, $value, $boolean = 'and')
2457+
{
2458+
return $this->whereRaw("({$column} & ?) = ?", [$mask, $value], $boolean);
2459+
}
2460+
2461+
/**
2462+
* Add a bitwise "OR" clause to the query.
2463+
*
2464+
* @param string $column
2465+
* @param int $value
2466+
* @return $this
2467+
*/
2468+
public function orWhereBitwise($column, $value)
2469+
{
2470+
return $this->whereBitwise($column, $value, 'or');
2471+
}
2472+
2473+
/**
2474+
* Add a bitwise "NOT" clause to the query.
2475+
*
2476+
* @param string $column
2477+
* @param int $value
2478+
* @param string $boolean
2479+
* @return $this
2480+
*/
2481+
public function whereNotBitwise($column, $value, $boolean = 'and')
2482+
{
2483+
return $this->whereRaw("{$column} & ? = 0", [$value], $boolean);
2484+
}
2485+
2486+
/**
2487+
* Add a bitwise "OR NOT" clause to the query.
2488+
*
2489+
* @param string $column
2490+
* @param int $value
2491+
* @return $this
2492+
*/
2493+
public function orWhereNotBitwise($column, $value)
2494+
{
2495+
return $this->whereNotBitwise($column, $value, 'or');
2496+
}
2497+
24342498
/**
24352499
* Add a "group by" clause to the query.
24362500
*

tests/Database/DatabaseQueryBuilderTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6893,6 +6893,40 @@ public function testWhereJsonLengthSqlServer()
68936893
$this->assertEquals([1], $builder->getBindings());
68946894
}
68956895

6896+
public function testWhereBitwise()
6897+
{
6898+
$builder = $this->getBuilder();
6899+
$builder->select('*')->from('users')->whereBitwise('permissions', 4);
6900+
$this->assertSame('select * from "users" where permissions & ? > 0', $builder->toSql());
6901+
$this->assertEquals([4], $builder->getBindings());
6902+
}
6903+
6904+
public function testWhereBitwiseExact()
6905+
{
6906+
$builder = $this->getBuilder();
6907+
$builder->select('*')->from('users')->whereBitwiseExact('permissions', 15, 15);
6908+
$this->assertSame('select * from "users" where (permissions & ?) = ?', $builder->toSql());
6909+
$this->assertEquals([15, 15], $builder->getBindings());
6910+
}
6911+
6912+
public function testOrWhereBitwise()
6913+
{
6914+
$builder = $this->getBuilder();
6915+
$builder->select('*')->from('users')
6916+
->whereBitwise('permissions', 1)
6917+
->orWhereBitwise('permissions', 2);
6918+
$this->assertSame('select * from "users" where permissions & ? > 0 or permissions & ? > 0', $builder->toSql());
6919+
$this->assertEquals([1, 2], $builder->getBindings());
6920+
}
6921+
6922+
public function testWhereNotBitwise()
6923+
{
6924+
$builder = $this->getBuilder();
6925+
$builder->select('*')->from('users')->whereNotBitwise('permissions', 4);
6926+
$this->assertSame('select * from "users" where permissions & ? = 0', $builder->toSql());
6927+
$this->assertEquals([4], $builder->getBindings());
6928+
}
6929+
68966930
public function testFrom()
68976931
{
68986932
$builder = $this->getBuilder();

0 commit comments

Comments
 (0)