Skip to content

Commit

Permalink
Allow for custom Postgres operators to be added (#53324)
Browse files Browse the repository at this point in the history
* Allow for custom Postgres operators to be added

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
boris-glumpler and taylorotwell authored Oct 29, 2024
1 parent 0f835f6 commit 029e993
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Illuminate/Database/Query/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ class PostgresGrammar extends Grammar
'is distinct from', 'is not distinct from',
];

/**
* The Postgres grammar specific custom operators.
*
* @var array
*/
protected static $customOperators = [];

/**
* The grammar specific bitwise operators.
*
Expand Down Expand Up @@ -772,4 +779,27 @@ public function substituteBindingsIntoRawSql($sql, $bindings)

return $query;
}

/**
* Get the Postgres grammar specific operators.
*
* @return array
*/
public function getOperators()
{
return array_values(array_unique(array_merge(parent::getOperators(), static::$customOperators)));
}

/**
* Set any Postgres grammar specific custom operators.
*
* @param array $operators
* @return void
*/
public static function customOperators(array $operators)
{
static::$customOperators = array_values(
array_merge(static::$customOperators, array_filter(array_filter($operators, 'is_string')))
);
}
}
19 changes: 19 additions & 0 deletions tests/Database/DatabasePostgresQueryGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,23 @@ public function testToRawSql()

$this->assertSame('select * from "users" where \'{}\' ? \'Hello\\\'\\\'World?\' AND "email" = \'foo\'', $query);
}

public function testCustomOperators()
{
PostgresGrammar::customOperators(['@@@', '@>', '']);
PostgresGrammar::customOperators(['@@>', 1]);

$connection = m::mock(Connection::class);
$grammar = new PostgresGrammar;
$grammar->setConnection($connection);

$operators = $grammar->getOperators();

$this->assertIsList($operators);
$this->assertContains('@@@', $operators);
$this->assertContains('@@>', $operators);
$this->assertNotContains('', $operators);
$this->assertNotContains(1, $operators);
$this->assertSame(array_unique($operators), $operators);
}
}

0 comments on commit 029e993

Please sign in to comment.