From a32165c193fff73d326eff8460d4c94bb992f81c Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 5 Nov 2024 17:45:58 +0200 Subject: [PATCH 1/3] Get connection id --- src/Database/Adapter.php | 14 ++++++++++++++ src/Database/Adapter/MariaDB.php | 9 +++++++++ src/Database/Adapter/Mongo.php | 15 +++++++++++++++ src/Database/Adapter/Postgres.php | 9 +++++++++ src/Database/Adapter/SQL.php | 10 ++++++++++ src/Database/Adapter/SQLite.php | 10 ++++++++++ src/Database/Database.php | 11 +++++++++++ tests/e2e/Adapter/Base.php | 12 ++++++++++++ 8 files changed, 90 insertions(+) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index f0f63b659..4bb576d8c 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -833,6 +833,13 @@ abstract public function getSupportForBatchOperations(): bool; */ abstract public function getSupportForAttributeResizing(): bool; + /** + * Are attributes supported? + * + * @return bool + */ + abstract public function getSupportForGetConnectionId(): bool; + /** * Get current attribute count from collection document * @@ -980,4 +987,11 @@ public function escapeWildcards(string $value): string * @throws Exception */ abstract public function increaseDocumentAttribute(string $collection, string $id, string $attribute, int|float $value, string $updatedAt, int|float|null $min = null, int|float|null $max = null): bool; + + /** + * Returns the connection ID identifier + * + * @return string + */ + abstract public function getConnectionId(): string; } diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 0e72eee4e..964a0626a 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -2398,4 +2398,13 @@ protected function processException(PDOException $e): void throw $e; } + + /** + * @return string + */ + public function getConnectionId(): string + { + $stmt = $this->getPDO()->query("SELECT CONNECTION_ID();"); + return $stmt->fetchColumn(); + } } diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 433068396..14d58aff1 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -1777,6 +1777,16 @@ public function getSupportForBatchOperations(): bool return false; } + /** + * Is get connection id supported? + * + * @return bool + */ + public function getSupportForGetConnectionId(): bool + { + return false; + } + /** * Get current attribute count from collection document * @@ -1926,4 +1936,9 @@ public function getMaxIndexLength(): int { return 0; } + + public function getConnectionId(): string + { + return '0'; + } } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 983e112dd..ecc6ae905 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -2337,4 +2337,13 @@ protected function processException(PDOException $e): void throw $e; } + + /** + * @return string + */ + public function getConnectionId(): string + { + $stmt = $this->getPDO()->query("SELECT pg_backend_pid();"); + return $stmt->fetchColumn(); + } } diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 745d2e59a..2a384d543 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -375,6 +375,16 @@ public function getSupportForBatchOperations(): bool return true; } + /** + * Is get connection id supported? + * + * @return bool + */ + public function getSupportForGetConnectionId(): bool + { + return true; + } + /** * Get current attribute count from collection document * diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index 5d1945598..44941b36a 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -866,6 +866,16 @@ public function getSupportForAttributeResizing(): bool return false; } + /** + * Is get connection id supported? + * + * @return bool + */ + public function getSupportForGetConnectionId(): bool + { + return false; + } + /** * Get SQL Index Type * diff --git a/src/Database/Database.php b/src/Database/Database.php index 13645e36f..2fe136ef8 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -493,6 +493,17 @@ public function silent(callable $callback, array $listeners = null): mixed } } + /** + * Get getConnection Id + * + * @return string + * @throws Exception + */ + public function getConnectionId(): string + { + return $this->adapter->getConnectionId(); + } + /** * Skip relationships for all the calls inside the callback * diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 6fde24f1f..ab3a3784f 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -99,6 +99,18 @@ public function testUpdateDeleteCollectionNotFound(): void } } + public function testGetCollectionId(): void + { + if (!static::getDatabase()->getAdapter()->getSupportForGetConnectionId()) { + $this->expectNotToPerformAssertions(); + return; + } + + $this->assertIsString(static::getDatabase()->getConnectionId()); + + $this->assertEquals(999, 11111); + } + public function testDeleteRelatedCollection(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) { From ca07a57dc8c051a819ba7e426ceb24e11f69a8ab Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 5 Nov 2024 17:52:08 +0200 Subject: [PATCH 2/3] Remove comment --- tests/e2e/Adapter/Base.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index ab3a3784f..dcc0c5e2c 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -107,8 +107,6 @@ public function testGetCollectionId(): void } $this->assertIsString(static::getDatabase()->getConnectionId()); - - $this->assertEquals(999, 11111); } public function testDeleteRelatedCollection(): void From 204177056faec1453c7f239b14a0ed9d6d66bbc2 Mon Sep 17 00:00:00 2001 From: fogelito Date: Wed, 6 Nov 2024 10:12:05 +0200 Subject: [PATCH 3/3] Fix comment --- src/Database/Adapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 4bb576d8c..f58f8928f 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -834,7 +834,7 @@ abstract public function getSupportForBatchOperations(): bool; abstract public function getSupportForAttributeResizing(): bool; /** - * Are attributes supported? + * Is get connection id supported? * * @return bool */