From e8a9624ba1d3b273322af876bca33d19164d7605 Mon Sep 17 00:00:00 2001 From: Dave Smith-Hayes Date: Mon, 18 Nov 2024 14:41:49 +0000 Subject: [PATCH] Add more documention and use a more generic name for the connection string. --- .../Api/Database/DatabaseHandlerInterface.php | 13 +++++++++---- app/src/Infrastructure/Database/DatabaseHandler.php | 8 ++++++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php index d14bfeb..f0a9c82 100644 --- a/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php +++ b/app/src/Infrastructure/Api/Database/DatabaseHandlerInterface.php @@ -2,8 +2,6 @@ namespace Slovocast\Infrastructure\Api\Database; -use PDO; - interface DatabaseHandlerInterface { /** @@ -13,11 +11,14 @@ interface DatabaseHandlerInterface /** * Get the connection string required for establishing connections to the - * database + * database. */ - public function getDsn(): string; + public function getConnectionString(): string; /** + * Retrieve a results set from the database. If there are no results, opt + * for returning an empty array rather than throwing an Exception. + * * @param string $query * @param array $params Either a list of params of the KV params * @return array The results as an array, empty if no results @@ -25,6 +26,10 @@ interface DatabaseHandlerInterface public function query(string $query, array $params = []): array; /** + * Used for performing inserts, updates, deletes. We should expect the + * statement to either be successful or not - avoid throwing Exceptions + * here as well. + * * @param string $query * @param array $params Either a list of params, or the KV of params * @return bool True if successful diff --git a/app/src/Infrastructure/Database/DatabaseHandler.php b/app/src/Infrastructure/Database/DatabaseHandler.php index 7e0b5c5..a903239 100644 --- a/app/src/Infrastructure/Database/DatabaseHandler.php +++ b/app/src/Infrastructure/Database/DatabaseHandler.php @@ -16,7 +16,11 @@ class DatabaseHandler implements DatabaseHandlerInterface private string $database, private int $port = 3306 ) { - $this->pdo = new PDO($this->getDsn(), $username, $password); + $this->pdo = new PDO( + $this->getConnectionString(), + $username, + $password + ); } /** @@ -27,7 +31,7 @@ class DatabaseHandler implements DatabaseHandlerInterface return $this->pdo; } - public function getDsn(): string + public function getConnectionString(): string { return "mysql:dbname={$this->database};host={$this->host};port={$this->port}"; }