Add PostgreSQL Support (#3)

Reviewed-on: #3
This commit was merged in pull request #3.
This commit is contained in:
2024-06-21 13:46:41 +00:00
parent 330e272187
commit 124426fa12
61 changed files with 2290 additions and 223 deletions

View File

@@ -52,23 +52,25 @@ class Field
*/
public function toWhere(): string
{
$fieldName = ($this->qualifier == '' ? '' : "$this->qualifier.") . 'data' . match (true) {
!str_contains($this->fieldName, '.') => "->>'$this->fieldName'",
Configuration::$mode == Mode::PgSQL => "#>>'{" . implode(',', explode('.', $this->fieldName)) . "}'",
Configuration::$mode == Mode::SQLite => "->>'" . implode("'->>'", explode('.', $this->fieldName)) . "'",
default => throw new DocumentException('Database mode not set; cannot make field WHERE clause')
};
$fieldPath = match (Configuration::$mode) {
Mode::PgSQL => match (true) {
$this->op == Op::BT => is_numeric($this->value[0]) ? "($fieldName)::numeric" : $fieldName,
is_numeric($this->value) => "($fieldName)::numeric",
default => $fieldName
},
default => $fieldName
};
$criteria = match ($this->op) {
Op::EX, Op::NEX => '',
Op::BT => " {$this->paramName}min AND {$this->paramName}max",
default => " $this->paramName"
};
$prefix = $this->qualifier == '' ? '' : "$this->qualifier.";
$fieldPath = match (Configuration::$mode) {
Mode::SQLite => "{$prefix}data->>'"
. (str_contains($this->fieldName, '.')
? implode("'->>'", explode('.', $this->fieldName))
: $this->fieldName)
. "'",
Mode::PgSQL => $this->op == Op::BT && is_numeric($this->value[0])
? "({$prefix}data->>'$this->fieldName')::numeric"
: "{$prefix}data->>'$this->fieldName'",
default => throw new DocumentException('Database mode not set; cannot make field WHERE clause')
};
return $fieldPath . ' ' . $this->op->toString() . $criteria;
}