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

Allow for custom Postgres operators to be added #53324

Merged
merged 2 commits into from
Oct 29, 2024
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
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);
}
}