Skip to content

Commit 029e993

Browse files
Allow for custom Postgres operators to be added (#53324)
* Allow for custom Postgres operators to be added * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 0f835f6 commit 029e993

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/Illuminate/Database/Query/Grammars/PostgresGrammar.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class PostgresGrammar extends Grammar
2222
'is distinct from', 'is not distinct from',
2323
];
2424

25+
/**
26+
* The Postgres grammar specific custom operators.
27+
*
28+
* @var array
29+
*/
30+
protected static $customOperators = [];
31+
2532
/**
2633
* The grammar specific bitwise operators.
2734
*
@@ -772,4 +779,27 @@ public function substituteBindingsIntoRawSql($sql, $bindings)
772779

773780
return $query;
774781
}
782+
783+
/**
784+
* Get the Postgres grammar specific operators.
785+
*
786+
* @return array
787+
*/
788+
public function getOperators()
789+
{
790+
return array_values(array_unique(array_merge(parent::getOperators(), static::$customOperators)));
791+
}
792+
793+
/**
794+
* Set any Postgres grammar specific custom operators.
795+
*
796+
* @param array $operators
797+
* @return void
798+
*/
799+
public static function customOperators(array $operators)
800+
{
801+
static::$customOperators = array_values(
802+
array_merge(static::$customOperators, array_filter(array_filter($operators, 'is_string')))
803+
);
804+
}
775805
}

tests/Database/DatabasePostgresQueryGrammarTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,23 @@ public function testToRawSql()
2828

2929
$this->assertSame('select * from "users" where \'{}\' ? \'Hello\\\'\\\'World?\' AND "email" = \'foo\'', $query);
3030
}
31+
32+
public function testCustomOperators()
33+
{
34+
PostgresGrammar::customOperators(['@@@', '@>', '']);
35+
PostgresGrammar::customOperators(['@@>', 1]);
36+
37+
$connection = m::mock(Connection::class);
38+
$grammar = new PostgresGrammar;
39+
$grammar->setConnection($connection);
40+
41+
$operators = $grammar->getOperators();
42+
43+
$this->assertIsList($operators);
44+
$this->assertContains('@@@', $operators);
45+
$this->assertContains('@@>', $operators);
46+
$this->assertNotContains('', $operators);
47+
$this->assertNotContains(1, $operators);
48+
$this->assertSame(array_unique($operators), $operators);
49+
}
3150
}

0 commit comments

Comments
 (0)