Add more documention and use a more generic name for the connection string.

This commit is contained in:
Dave Smith-Hayes 2024-11-18 14:41:49 +00:00
parent e5bd4e5841
commit e8a9624ba1
2 changed files with 15 additions and 6 deletions

View File

@ -2,8 +2,6 @@
namespace Slovocast\Infrastructure\Api\Database; namespace Slovocast\Infrastructure\Api\Database;
use PDO;
interface DatabaseHandlerInterface interface DatabaseHandlerInterface
{ {
/** /**
@ -13,11 +11,14 @@ interface DatabaseHandlerInterface
/** /**
* Get the connection string required for establishing connections to the * 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 string $query
* @param array $params Either a list of params of the KV params * @param array $params Either a list of params of the KV params
* @return array The results as an array, empty if no results * @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; 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 string $query
* @param array $params Either a list of params, or the KV of params * @param array $params Either a list of params, or the KV of params
* @return bool True if successful * @return bool True if successful

View File

@ -16,7 +16,11 @@ class DatabaseHandler implements DatabaseHandlerInterface
private string $database, private string $database,
private int $port = 3306 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; return $this->pdo;
} }
public function getDsn(): string public function getConnectionString(): string
{ {
return "mysql:dbname={$this->database};host={$this->host};port={$this->port}"; return "mysql:dbname={$this->database};host={$this->host};port={$this->port}";
} }