Files
contabilidad/api/ProVM/Alias/Database.php
2022-08-08 22:36:04 -04:00

154 lines
4.4 KiB
PHP

<?php
namespace ProVM\Alias;
use Psr\Database\DatabaseInterface;
use Exception;
use PDO, PDOException, PDOStatement;
abstract class Database implements DatabaseInterface
{
protected string $host;
protected int $port;
public function setHost(string $host, ?int $port = null): DatabaseInterface
{
$this->host = $host;
if ($port !== null) {
$this->port = $port;
}
return $this;
}
public function getHost(): string
{
return $this->host;
}
public function getPort(): int
{
return $this->port;
}
protected string $name;
public function setName(string $database_name): DatabaseInterface
{
$this->name = $database_name;
return $this;
}
public function getName(): string
{
return $this->name;
}
protected string $username;
protected string $password;
public function setUser(string $username, string $password): DatabaseInterface
{
$this->username = $username;
$this->password = $password;
return $this;
}
public function getUser(): string
{
return $this->username;
}
public function getPassword(): string
{
return $this->password;
}
protected PDO $connection;
public function connect(): DatabaseInterface
{
if ($this->needsUser()) {
$this->connection = new PDO($this->getDsn(), $this->getUser(), $this->getPassword());
return $this;
}
$this->connection = new PDO($this->getDsn());
return $this;
}
public function getConnection(): PDO
{
if (!isset($this->connection)) {
return $this->connect()->connection;
}
return $this->connection;
}
public function query(string $query): array
{
$st = $this->getConnection()->query($query);
if (!$st) {
throw new PDOException("Could not retrieve anything with '{$query}'.");
}
$results = $st->fetchAll(PDO::FETCH_ASSOC);
if (!$results) {
throw new PDOException('Could not retrieve any results.');
}
return $results;
}
public function beginTransaction(): void
{
if (!$this->getConnection()->beginTransaction()) {
throw new PDOException('Could not begin transaction.');
}
}
public function commit(): void
{
if (!$this->getConnection()->commit()) {
throw new PDOException('Could not commit');
}
}
public function rollBack(): void
{
if (!$this->getConnection()->rollBack()) {
throw new PDOException('Could not rollback.');
}
}
protected PDOStatement $prepared_statement;
public function prepare(string $query): DatabaseInterface
{
$st = $this->getConnection()->prepare($query);
if (!$st) {
throw new PDOException("Could not prepare query '{$query}'.");
}
$this->prepared_statement = $st;
return $this;
}
public function execute(array $data): array
{
if (!isset($this->prepared_statement)) {
throw new Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new PDOException('Could not execute prepared statement.');
}
$results = $this->prepared_statement->fetchAll(PDO::FETCH_ASSOC);
if (!$results) {
throw new PDOException('Could not retrieve any results.');
}
return $results;
}
public function insert(array $values): void
{
if (!isset($this->prepared_statement)) {
throw new Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($values)) {
throw new PDOException('Could not insert.');
}
}
public function update(array $data): void
{
if (!isset($this->prepared_statement)) {
throw new Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new PDOException('Could not update.');
}
}
public function delete(array $data): void
{
if (!isset($this->prepared_statement)) {
throw new Exception('No prepared statement.');
}
if (!$this->prepared_statement->execute($data)) {
throw new PDOException('Could not delete.');
}
}
}