diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 2b0cb04fd..acd2e6019 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -854,6 +854,13 @@ abstract public function getSupportForBatchOperations(): bool; */ abstract public function getSupportForAttributeResizing(): bool; + /** + * Is get connection id supported? + * + * @return bool + */ + abstract public function getSupportForGetConnectionId(): bool; + /** * Get current attribute count from collection document * @@ -1001,4 +1008,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 4bd39827c..53ea8447f 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -2392,4 +2392,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 abf74e987..9a54ebe69 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -387,6 +387,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 0b6bc7b99..4c8464271 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -897,6 +897,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 d29580226..197995774 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 986887f5e..bc8b65546 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -99,6 +99,16 @@ public function testUpdateDeleteCollectionNotFound(): void } } + public function testGetCollectionId(): void + { + if (!static::getDatabase()->getAdapter()->getSupportForGetConnectionId()) { + $this->expectNotToPerformAssertions(); + return; + } + + $this->assertIsString(static::getDatabase()->getConnectionId()); + } + public function testDeleteRelatedCollection(): void { if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {