Skip to content

Fix/pg adapter #566

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
60 changes: 43 additions & 17 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,16 @@ public function setTimeout(int $milliseconds, string $event = Database::EVENT_AL

$this->before($event, 'timeout', function ($sql) use ($milliseconds) {
return "
SET statement_timeout = {$milliseconds};
{$sql};
SET statement_timeout = 0;
";
WITH
__timeout AS (
SELECT pg_catalog.set_config('statement_timeout', '{$milliseconds}', true)
),
actual AS (
{$sql}
)
SELECT * FROM actual;
";
return $sql;
});
}

Expand All @@ -128,9 +134,19 @@ public function create(string $name): bool
$sql = "CREATE SCHEMA \"{$name}\"";
$sql = $this->trigger(Database::EVENT_DATABASE_CREATE, $sql);

return $this->getPDO()
$dbCreation = $this->getPDO()
->prepare($sql)
->execute();

$collation = "
CREATE COLLATION IF NOT EXISTS utf8_ci (
provider = icu,
locale = 'und-u-ks-primary',
deterministic = false
);
";
$this->getPDO()->prepare($collation)->execute();
return $dbCreation;
}

/**
Expand Down Expand Up @@ -203,7 +219,6 @@ public function createCollection(string $name, array $attributes = [], array $in
}

$sqlTenant = $this->sharedTables ? '_tenant INTEGER DEFAULT NULL,' : '';

$collection = "
CREATE TABLE {$this->getSQLTable($id)} (
_id SERIAL NOT NULL,
Expand Down Expand Up @@ -277,7 +292,6 @@ public function createCollection(string $name, array $attributes = [], array $in
$indexType = $index->getAttribute('type');
$indexAttributes = $index->getAttribute('attributes', []);
$indexOrders = $index->getAttribute('orders', []);

$this->createIndex(
$id,
$indexId,
Expand Down Expand Up @@ -817,6 +831,7 @@ public function createIndex(string $collection, string $id, string $type, array
$collection = $this->filter($collection);
$id = $this->filter($id);


foreach ($attributes as $i => $attr) {
$order = empty($orders[$i]) || Database::INDEX_FULLTEXT === $type ? '' : $orders[$i];

Expand All @@ -828,7 +843,12 @@ public function createIndex(string $collection, string $id, string $type, array
};

if (Database::INDEX_UNIQUE === $type) {
$attributes[$i] = "LOWER(\"{$attr}\"::text) {$order}";
// TODO: pass an external map for this
if ($attr === "time") {
$attributes[$i] = "\"{$attr}\" {$order}";
} else {
$attributes[$i] = "\"{$attr}\" COLLATE utf8_ci {$order}";
}
} else {
$attributes[$i] = "\"{$attr}\" {$order}";
}
Expand All @@ -850,6 +870,7 @@ public function createIndex(string $collection, string $id, string $type, array
}

$sql = "CREATE {$sqlType} {$key} ON {$this->getSQLTable($collection)} ({$attributes});";

$sql = $this->trigger(Database::EVENT_INDEX_CREATE, $sql);

try {
Expand All @@ -860,7 +881,6 @@ public function createIndex(string $collection, string $id, string $type, array
throw $this->processException($e);
}
}

/**
* Delete Index
*
Expand Down Expand Up @@ -955,7 +975,6 @@ public function createDocument(string $collection, Document $document): Document
$sql = "
INSERT INTO {$this->getSQLTable($name)} ({$columns} \"_uid\")
VALUES ({$columnNames} :_uid)
RETURNING _id
";

$sql = $this->trigger(Database::EVENT_DOCUMENT_CREATE, $sql);
Expand Down Expand Up @@ -1009,8 +1028,8 @@ public function createDocument(string $collection, Document $document): Document

try {
$stmt->execute();

$document['$internalId'] = $stmt->fetch()["_id"];
$lastInsertedId = $this->getPDO()->lastInsertId();
$document['$internalId'] = $lastInsertedId;

if (isset($stmtPermissions)) {
$stmtPermissions->execute();
Expand Down Expand Up @@ -1642,9 +1661,12 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,

try {
$stmt = $this->getPDO()->prepare($sql);

foreach ($binds as $key => $value) {
$stmt->bindValue($key, $value, $this->getPDOType($value));
if ($key === ":internalId") {
$stmt->bindValue($key, $value, PDO::PARAM_INT);
} else {
$stmt->bindValue($key, $value, $this->getPDOType($value));
}
}

$stmt->execute();
Expand Down Expand Up @@ -1904,10 +1926,14 @@ protected function getSQLCondition(Query $query, array &$binds): string
Query::TYPE_CONTAINS => $query->onArray() ? \json_encode($value) : '%' . $this->escapeWildcards($value) . '%',
default => $value
};
if ($attribute === $this->quote("_id")) {
$binds[":internalId"] = $value;
$conditions[] = "{$alias}.{$attribute} {$operator} :internalId";
} else {
$binds[":{$placeholder}_{$key}"] = $value;
$conditions[] = "{$alias}.{$attribute} {$operator} :{$placeholder}_{$key}";
}

$binds[":{$placeholder}_{$key}"] = $value;

$conditions[] = "{$alias}.{$attribute} {$operator} :{$placeholder}_{$key}";
}

return empty($conditions) ? '' : '(' . implode(' OR ', $conditions) . ')';
Expand Down