Skip to content

Commit d54a540

Browse files
pg adapter
* added settimeouts for DML, selects * added index attributes types in create index * added way of manually and automatic internal id in the create document
1 parent d43815d commit d54a540

2 files changed

Lines changed: 31 additions & 27 deletions

File tree

src/Database/Adapter/Postgres.php

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,32 @@ public function rollbackTransaction(): bool
8484
return $result;
8585
}
8686

87-
private function execute(\PDOStatement $stmt): bool
87+
private function execute(mixed $stmt): bool
8888
{
89+
$pdo = $this->getPDO();
8990
try {
91+
if ($this->inTransaction === 0) {
92+
$pdo->exec("SET statement_timeout = '{$this->timeout}ms'");
93+
} else {
94+
$pdo->exec("SET LOCAL statement_timeout = '{$this->timeout}'");
95+
}
96+
9097
$result = $stmt?->execute();
91-
} catch (PDOException $e) {
98+
return $result;
99+
100+
} catch (\PDOException $e) {
92101
throw $e;
93102
} finally {
94-
try {
95-
$this->getPDO()->prepare('SET statement_timeout = 0')->execute();
96-
} catch (PDOException $e) {
97-
throw $e;
103+
if ($this->inTransaction === 0) {
104+
$pdo->exec("RESET statement_timeout");
98105
}
99106
}
100107

101-
return $result;
108+
102109
}
103110

111+
112+
104113
/**
105114
* Returns Max Execution Time
106115
* @param int $milliseconds
@@ -118,8 +127,6 @@ public function setTimeout(int $milliseconds, string $event = Database::EVENT_AL
118127
}
119128

120129
$this->timeout = $milliseconds;
121-
$this->getPDO()->exec("ALTER SYSTEM SET statement_timeout = '{$milliseconds}'");
122-
$this->getPDO()->exec("SELECT pg_reload_conf()");
123130
}
124131

125132
/**
@@ -141,8 +148,9 @@ public function create(string $name): bool
141148
$sql = "CREATE SCHEMA \"{$name}\"";
142149
$sql = $this->trigger(Database::EVENT_DATABASE_CREATE, $sql);
143150

144-
$dbCreation = $this->execute($this->getPDO()
145-
->prepare($sql));
151+
$dbCreation = $this->getPDO()
152+
->prepare($sql)
153+
->execute();
146154

147155
$collation = "
148156
CREATE COLLATION IF NOT EXISTS utf8_ci (
@@ -151,7 +159,7 @@ public function create(string $name): bool
151159
deterministic = false
152160
);
153161
";
154-
$this->execute($this->getPDO()->prepare($collation));
162+
$this->getPDO()->prepare($collation)->execute();
155163
return $dbCreation;
156164
}
157165

@@ -170,8 +178,7 @@ public function delete(string $name): bool
170178
$sql = "DROP SCHEMA IF EXISTS \"{$name}\" CASCADE";
171179
$sql = $this->trigger(Database::EVENT_DATABASE_DELETE, $sql);
172180

173-
return $this->execute($this->getPDO()
174-
->prepare($sql));
181+
return $this->getPDO()->prepare($sql)->execute();
175182
}
176183

177184
/**
@@ -284,11 +291,9 @@ public function createCollection(string $name, array $attributes = [], array $in
284291
$permissions = $this->trigger(Database::EVENT_COLLECTION_CREATE, $permissions);
285292

286293
try {
287-
$this->execute($this->getPDO()
288-
->prepare($collection));
294+
$this->getPDO()->prepare($collection)->execute();
289295

290-
$this->execute($this->getPDO()
291-
->prepare($permissions));
296+
$this->getPDO()->prepare($permissions)->execute();
292297

293298
foreach ($indexes as $index) {
294299
$indexId = $this->filter($index->getId());
@@ -409,8 +414,7 @@ public function deleteCollection(string $id): bool
409414
$sql = "DROP TABLE {$this->getSQLTable($id)}, {$this->getSQLTable($id . '_perms')}";
410415
$sql = $this->trigger(Database::EVENT_COLLECTION_DELETE, $sql);
411416

412-
return $this->execute($this->getPDO()
413-
->prepare($sql));
417+
return $this->getPDO()->prepare($sql)->execute();
414418
}
415419

416420
/**
@@ -846,7 +850,7 @@ public function createIndex(string $collection, string $id, string $type, array
846850
};
847851

848852
if (Database::INDEX_UNIQUE === $type) {
849-
if ($indexAttributeTypes[$attr] === Database::VAR_STRING) {
853+
if (isset($indexAttributeTypes[$attr]) && $indexAttributeTypes[$attr] === Database::VAR_STRING) {
850854
$attributes[$i] = "\"{$attr}\" COLLATE utf8_ci {$order}";
851855
} else {
852856
$attributes[$i] = "\"{$attr}\" {$order}";
@@ -876,8 +880,7 @@ public function createIndex(string $collection, string $id, string $type, array
876880
$sql = $this->trigger(Database::EVENT_INDEX_CREATE, $sql);
877881

878882
try {
879-
return $this->execute($this->getPDO()
880-
->prepare($sql));
883+
return $this->getPDO()->prepare($sql)->execute();
881884
} catch (PDOException $e) {
882885
throw $this->processException($e);
883886
}
@@ -1028,7 +1031,8 @@ public function createDocument(string $collection, Document $document): Document
10281031
try {
10291032
$this->execute($stmt);
10301033
$lastInsertedId = $this->getPDO()->lastInsertId();
1031-
$document['$internalId'] = $lastInsertedId;
1034+
// internalId can be manually as well
1035+
$document['$internalId'] ??= $lastInsertedId;
10321036

10331037
if (isset($stmtPermissions)) {
10341038
$this->execute($stmtPermissions);

src/Database/Database.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3046,10 +3046,11 @@ public function createIndex(string $collection, string $id, string $type, array
30463046

30473047
/** @var array<Document> $collectionAttributes */
30483048
$collectionAttributes = $collection->getAttribute('attributes', []);
3049-
3049+
$indexAttributesWithTypes = [];
30503050
foreach ($attributes as $i => $attr) {
30513051
foreach ($collectionAttributes as $collectionAttribute) {
30523052
if ($collectionAttribute->getAttribute('key') === $attr) {
3053+
$indexAttributesWithTypes[$attr] = $collectionAttribute->getAttribute('type');
30533054

30543055
/**
30553056
* mysql does not save length in collection when length = attributes size
@@ -3095,7 +3096,7 @@ public function createIndex(string $collection, string $id, string $type, array
30953096
}
30963097

30973098
try {
3098-
$created = $this->adapter->createIndex($collection->getId(), $id, $type, $attributes, $lengths, $orders);
3099+
$created = $this->adapter->createIndex($collection->getId(), $id, $type, $attributes, $lengths, $orders, $indexAttributesWithTypes);
30993100

31003101
if (!$created) {
31013102
throw new DatabaseException('Failed to create index');
@@ -3656,7 +3657,6 @@ public function createDocument(string $collection, Document $document): Document
36563657
if ($this->resolveRelationships) {
36573658
$document = $this->silent(fn () => $this->createDocumentRelationships($collection, $document));
36583659
}
3659-
36603660
return $this->adapter->createDocument($collection->getId(), $document);
36613661
});
36623662

0 commit comments

Comments
 (0)