-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(database): add closable connection wrapper for PDO connection
- Loading branch information
1 parent
08df98c
commit ca29a5e
Showing
19 changed files
with
170 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database; | ||
|
||
use PDOStatement; | ||
|
||
interface Connection | ||
{ | ||
public function beginTransaction(): bool; | ||
|
||
public function commit(): bool; | ||
|
||
public function rollback(): bool; | ||
|
||
public function lastInsertId(): false|string; | ||
|
||
public function prepare(string $sql): false|PDOStatement; | ||
|
||
public function close(): void; | ||
|
||
public function connect(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database; | ||
|
||
use Tempest\Container\Container; | ||
use Tempest\Container\Initializer; | ||
use Tempest\Container\Singleton; | ||
|
||
final class ConnectionInitializer implements Initializer | ||
{ | ||
private static Connection|null $instance = null; | ||
|
||
#[Singleton] | ||
public function initialize(Container $container): Connection | ||
{ | ||
// Reuse same connection instance in unit tests | ||
if (self::$instance !== null) { | ||
return self::$instance; | ||
} | ||
|
||
$databaseConfig = $container->get(DatabaseConfig::class); | ||
|
||
$connection = new PDOConnection($databaseConfig->connection()); | ||
$connection->connect(); | ||
|
||
self::$instance = $connection; | ||
|
||
return $connection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Database; | ||
|
||
use PDO; | ||
use PDOStatement; | ||
use Tempest\Database\Connections\DatabaseConnection; | ||
|
||
final class PDOConnection implements Connection | ||
{ | ||
private PDO|null $pdo = null; | ||
|
||
public function __construct(private readonly DatabaseConnection $connection) | ||
{ | ||
} | ||
|
||
public function beginTransaction(): bool | ||
{ | ||
return $this->pdo->beginTransaction(); | ||
} | ||
|
||
public function commit(): bool | ||
{ | ||
return $this->pdo->commit(); | ||
} | ||
|
||
public function rollback(): bool | ||
{ | ||
return $this->pdo->rollBack(); | ||
} | ||
|
||
public function lastInsertId(): false|string | ||
{ | ||
return $this->pdo->lastInsertId(); | ||
} | ||
|
||
public function prepare(string $sql): false|PDOStatement | ||
{ | ||
return $this->pdo->prepare($sql); | ||
} | ||
|
||
public function close(): void | ||
{ | ||
$this->pdo = null; | ||
} | ||
|
||
public function connect(): void | ||
{ | ||
if ($this->pdo !== null) { | ||
return; | ||
} | ||
|
||
$this->pdo = new PDO( | ||
$this->connection->getDsn(), | ||
$this->connection->getUsername(), | ||
$this->connection->getPassword(), | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.