Skip to content

Commit

Permalink
Merge pull request #472 from utopia-php/connection-id
Browse files Browse the repository at this point in the history
Get Connection Id
  • Loading branch information
abnegate authored Nov 7, 2024
2 parents 07e3e5a + 13eb1db commit 44ea79a
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
9 changes: 9 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
15 changes: 15 additions & 0 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -1926,4 +1936,9 @@ public function getMaxIndexLength(): int
{
return 0;
}

public function getConnectionId(): string
{
return '0';
}
}
9 changes: 9 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
10 changes: 10 additions & 0 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
10 changes: 10 additions & 0 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
11 changes: 11 additions & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit 44ea79a

Please sign in to comment.