Implementations
This commit is contained in:
75
src/Database/Connection.php
Normal file
75
src/Database/Connection.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Concept\Database\Connection as ConnectionInterface;
|
||||
use ProVM\Concept\Database\ResultSet as ResultSetInterface;
|
||||
use ProVM\Concept\Database\Transaction as TransactionInterface;
|
||||
|
||||
class Connection implements ConnectionInterface
|
||||
{
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->setDatabase($database);
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
protected Database $database;
|
||||
public function setDatabase(Database $database): ConnectionInterface
|
||||
{
|
||||
$this->database = $database;
|
||||
return $this;
|
||||
}
|
||||
public function getDatabase(): Database
|
||||
{
|
||||
return $this->database;
|
||||
}
|
||||
public function connect(): ConnectionInterface
|
||||
{
|
||||
if (!isset($this->pdo)) {
|
||||
if ($this->getDatabase()->needsUser()) {
|
||||
$pdo = new PDO($this->getDatabase()->getDSN(), $this->getDatabase()->getUsername(), $this->getDatabase()->getPassword());
|
||||
} else {
|
||||
$pdo = new PDO($this->getDatabase()->getDSN());
|
||||
}
|
||||
$this->setPDO($pdo);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
protected PDO $pdo;
|
||||
public function setPDO(PDO $pdo): ConnectionInterface
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
return $this;
|
||||
}
|
||||
public function getPDO(): PDO
|
||||
{
|
||||
return $this->pdo;
|
||||
}
|
||||
public function query(string $query): ResultSetInterface
|
||||
{
|
||||
$st = $this->getPDO()->query($query);
|
||||
if (!$st) {
|
||||
throw new PDOException("Could not run query {$query}");
|
||||
}
|
||||
return new ResultSet($st);
|
||||
}
|
||||
public function prepare(string $query): ResultSetInterface
|
||||
{
|
||||
$st = $this->getPDO()->prepare($query);
|
||||
if (!$st) {
|
||||
throw new PDOException("Could not prepare query {$query}");
|
||||
}
|
||||
return new ResultSet($st);
|
||||
}
|
||||
public function execute(string $query, array $values): ResultSetInterface
|
||||
{
|
||||
return $this->prepare($query)->execute($values);
|
||||
}
|
||||
public function transaction(): TransactionInterface
|
||||
{
|
||||
return new Transaction($this->getPDO());
|
||||
}
|
||||
}
|
28
src/Database/MySQL.php
Normal file
28
src/Database/MySQL.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use ProVM\Alias\Database;
|
||||
|
||||
class MySQL extends Database
|
||||
{
|
||||
public function needsUser(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getDSN(): string
|
||||
{
|
||||
$arr = [
|
||||
"host={$this->getHost()}"
|
||||
];
|
||||
if (isset($this->port)) {
|
||||
$arr []= "port={$this->getPort()}";
|
||||
}
|
||||
$arr []= "dbname={$this->getName()}";
|
||||
|
||||
return implode(':', [
|
||||
'mysql',
|
||||
implode(';', $arr)
|
||||
]);
|
||||
}
|
||||
}
|
44
src/Database/ResultSet.php
Normal file
44
src/Database/ResultSet.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use ProVM\Concept\Database\ResultSet as RSInterface;
|
||||
|
||||
class ResultSet implements RSInterface
|
||||
{
|
||||
public function __construct(PDOStatement $statement)
|
||||
{
|
||||
$this->setStatement($statement);
|
||||
}
|
||||
|
||||
protected PDOStatement $statement;
|
||||
public function setStatement(PDOStatement $statement): RSInterface
|
||||
{
|
||||
$this->statement = $statement;
|
||||
return $this;
|
||||
}
|
||||
public function getStatement(): PDOStatement
|
||||
{
|
||||
return $this->statement;
|
||||
}
|
||||
|
||||
public function execute(array $values): RSInterface
|
||||
{
|
||||
$this->getStatement()->execute($values);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAsArray(): array
|
||||
{
|
||||
return $this->getStatement()->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function getAsObject(): array
|
||||
{
|
||||
return $this->getStatement()->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
public function getFirst(): mixed
|
||||
{
|
||||
return $this->getStatement()->fetch(PDO::FETCH_OBJ);
|
||||
}
|
||||
}
|
59
src/Database/Transaction.php
Normal file
59
src/Database/Transaction.php
Normal file
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use ProVM\Concept\Database\ResultSet as ResultSetInterface;
|
||||
use ProVM\Concept\Database\Transaction as TransactionInterface;
|
||||
|
||||
class Transaction implements TransactionInterface
|
||||
{
|
||||
public function __construct(PDO $pdo)
|
||||
{
|
||||
$this->setPDO($pdo);
|
||||
}
|
||||
|
||||
protected PDO $pdo;
|
||||
public function setPDO(PDO $pdo): TransactionInterface
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
return $this;
|
||||
}
|
||||
public function getPDO(): PDO
|
||||
{
|
||||
return $this->pdo;
|
||||
}
|
||||
public function begin(): TransactionInterface
|
||||
{
|
||||
$this->getPDO()->beginTransaction();
|
||||
return $this;
|
||||
}
|
||||
public function query(string $query): ResultSetInterface
|
||||
{
|
||||
$st = $this->getPDO()->query($query);
|
||||
if (!$st) {
|
||||
throw new PDOException("Could not run query {$query}");
|
||||
}
|
||||
return new ResultSet($st);
|
||||
}
|
||||
public function prepare(string $query): ResultSetInterface
|
||||
{
|
||||
$st = $this->getPDO()->prepare($query);
|
||||
if (!$st) {
|
||||
throw new PDOException("Could not prepare query {$query}");
|
||||
}
|
||||
return new ResultSet($st);
|
||||
}
|
||||
public function execute(string $query, array $values): ResultSetInterface
|
||||
{
|
||||
return $this->prepare($query)->execute($values);
|
||||
}
|
||||
public function commit(): void
|
||||
{
|
||||
$this->getPDO()->commit();
|
||||
}
|
||||
public function rollBack(): void
|
||||
{
|
||||
$this->getPDO()->rollBack();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user