Reorder and Added missing databases
This commit is contained in:
@ -1,58 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Alias;
|
||||
|
||||
use ProVM\Concept\Database as DatabaseInterface;
|
||||
|
||||
abstract class Database implements DatabaseInterface
|
||||
{
|
||||
protected string $host;
|
||||
public function setHost(string $host): DatabaseInterface
|
||||
{
|
||||
$this->host = $host;
|
||||
return $this;
|
||||
}
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
protected int $port;
|
||||
public function setPort(int $port): DatabaseInterface
|
||||
{
|
||||
$this->port = $port;
|
||||
return $this;
|
||||
}
|
||||
public function getPort(): int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
protected string $name;
|
||||
public function setName(string $name): DatabaseInterface
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
protected string $username;
|
||||
public function setUsername(string $username): DatabaseInterface
|
||||
{
|
||||
$this->username = $username;
|
||||
return $this;
|
||||
}
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
protected string $password;
|
||||
public function setPassword(string $password): DatabaseInterface
|
||||
{
|
||||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
}
|
@ -1,20 +1,18 @@
|
||||
<?php
|
||||
namespace ProVM\Concept;
|
||||
|
||||
use PDO;
|
||||
|
||||
interface Database
|
||||
{
|
||||
public function setHost(string $host): Database;
|
||||
public function getHost(): string;
|
||||
public function setPort(int $port): Database;
|
||||
public function getPort(): int;
|
||||
public function setName(string $name): Database;
|
||||
public function getPort(): int|bool;
|
||||
public function getName(): string;
|
||||
public function setUsername(string $username): Database;
|
||||
public function getUsername(): string;
|
||||
public function setPassword(string $password): Database;
|
||||
public function getUser(): string;
|
||||
public function getPassword(): string;
|
||||
public function needsUser(): bool;
|
||||
public function getDSN(): string;
|
||||
public function setHost(string $host): Database;
|
||||
public function setPort(int $port): Database;
|
||||
public function setName(string $name): Database;
|
||||
public function setUser(string $username): Database;
|
||||
public function setPassword(string $password): Database;
|
||||
public function getDsn(): string;
|
||||
}
|
||||
|
@ -1,18 +1,15 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database;
|
||||
|
||||
use PDO;
|
||||
use ProVM\Concept\Database;
|
||||
|
||||
interface Connection
|
||||
{
|
||||
public function setDatabase(Database $database): Connection;
|
||||
public function getDatabase(): Database;
|
||||
public function connect(): Connection;
|
||||
public function setPDO(PDO $pdo): Connection;
|
||||
public function getPDO(): PDO;
|
||||
public function connect(): \PDO;
|
||||
|
||||
public function transaction(): Transaction;
|
||||
|
||||
public function query(string $query): ResultSet;
|
||||
public function prepare(string $query): ResultSet;
|
||||
public function execute(string $query, array $values): ResultSet;
|
||||
public function transaction(): Transaction;
|
||||
public function execute(string $query, ?array $data = null): ResultSet;
|
||||
}
|
||||
|
@ -1,14 +1,12 @@
|
||||
<?php
|
||||
namespace ProVM\Concept\Database;
|
||||
|
||||
use PDOStatement;
|
||||
|
||||
interface ResultSet
|
||||
{
|
||||
public function __construct(PDOStatement $statement);
|
||||
public function execute(array $values): ResultSet;
|
||||
public function getAsArray(): array;
|
||||
public function getAsObject(): array;
|
||||
public function getFirstAsArray(): array;
|
||||
public function getFirstAsObject(): object;
|
||||
public function execute(array $data): ResultSet;
|
||||
|
||||
public function fetchFirst(): array;
|
||||
public function fetchAll(): array;
|
||||
public function fetchFirstAsObject(): object;
|
||||
public function fetchAllAsObjects(): array;
|
||||
}
|
||||
|
@ -4,9 +4,6 @@ namespace ProVM\Concept\Database;
|
||||
interface Transaction
|
||||
{
|
||||
public function begin(): Transaction;
|
||||
public function query(string $query): ResultSet;
|
||||
public function prepare(string $query): ResultSet;
|
||||
public function execute(string $query, array $values): ResultSet;
|
||||
public function commit(): void;
|
||||
public function rollBack(): void;
|
||||
}
|
||||
|
@ -2,74 +2,67 @@
|
||||
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
|
||||
class Connection implements Database\Connection
|
||||
{
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->setDatabase($database);
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
protected Database $database;
|
||||
public function setDatabase(Database $database): ConnectionInterface
|
||||
|
||||
protected function getDatabase(): Database
|
||||
{
|
||||
return $this->database;
|
||||
}
|
||||
|
||||
protected function setDatabase(Database $database): Database\Connection
|
||||
{
|
||||
$this->database = $database;
|
||||
return $this;
|
||||
}
|
||||
public function getDatabase(): Database
|
||||
{
|
||||
return $this->database;
|
||||
}
|
||||
public function connect(): ConnectionInterface
|
||||
|
||||
protected PDO $pdo;
|
||||
public function connect(): PDO
|
||||
{
|
||||
if (!isset($this->pdo)) {
|
||||
$dsn = $this->getDatabase()->getDsn();
|
||||
if ($this->getDatabase()->needsUser()) {
|
||||
$pdo = new PDO($this->getDatabase()->getDSN(), $this->getDatabase()->getUsername(), $this->getDatabase()->getPassword());
|
||||
$this->pdo = new PDO($dsn, $this->getDatabase()->getUser(), $this->getDatabase()->getPassword());
|
||||
} else {
|
||||
$pdo = new PDO($this->getDatabase()->getDSN());
|
||||
$this->pdo = new PDO($dsn);
|
||||
}
|
||||
$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
|
||||
|
||||
protected Database\Transaction $transaction;
|
||||
|
||||
public function transaction(): Database\Transaction
|
||||
{
|
||||
$st = $this->getPDO()->query($query);
|
||||
if (!$st) {
|
||||
throw new PDOException("Could not run query {$query}");
|
||||
if (!isset($this->transaction)) {
|
||||
$this->transaction = new Transaction($this);
|
||||
}
|
||||
return new ResultSet($st);
|
||||
return $this->transaction;
|
||||
}
|
||||
public function prepare(string $query): ResultSetInterface
|
||||
|
||||
public function query(string $query): Database\ResultSet
|
||||
{
|
||||
$st = $this->getPDO()->prepare($query);
|
||||
if (!$st) {
|
||||
throw new PDOException("Could not prepare query {$query}");
|
||||
return new ResultSet($this->connect()->query($query));
|
||||
}
|
||||
public function prepare(string $query): Database\ResultSet
|
||||
{
|
||||
return new ResultSet($this->connect()->prepare($query));
|
||||
}
|
||||
public function execute(string $query, ?array $data = null): Database\ResultSet
|
||||
{
|
||||
if ($data !== null) {
|
||||
$rs = $this->prepare($query);
|
||||
$rs->execute($data);
|
||||
return $rs;
|
||||
}
|
||||
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());
|
||||
return $this->query($query);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use ProVM\Alias\Database;
|
||||
use ProVM\Implement\Database;
|
||||
|
||||
class MySQL extends Database
|
||||
{
|
||||
@ -9,20 +9,13 @@ class MySQL extends Database
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getDSN(): string
|
||||
public function getDsn(): string
|
||||
{
|
||||
$arr = [
|
||||
"host={$this->getHost()}"
|
||||
];
|
||||
if (isset($this->port)) {
|
||||
$arr []= "port={$this->getPort()}";
|
||||
$dsn = ["mysql:host={$this->getHost()}"];
|
||||
if ($this->getPort()) {
|
||||
$dsn []= "port={$this->getPort()}";
|
||||
}
|
||||
$arr []= "dbname={$this->getName()}";
|
||||
|
||||
return implode(':', [
|
||||
'mysql',
|
||||
implode(';', $arr)
|
||||
]);
|
||||
$dsn []= "dbname={$this->getName()}";
|
||||
return implode(';', $dsn);
|
||||
}
|
||||
}
|
||||
|
19
src/Database/PostgreSQL.php
Normal file
19
src/Database/PostgreSQL.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use ProVM\Implement\Database;
|
||||
|
||||
class PostgreSQL extends Database
|
||||
{
|
||||
public function getDsn(): string
|
||||
{
|
||||
$dsn = ["pgsql:host={$this->getHost()}"];
|
||||
if ($this->getPort()) {
|
||||
$dsn []= "port={$this->getPort()}";
|
||||
}
|
||||
$dsn []= "dbname={$this->getName()}";
|
||||
$dsn []= "user={$this->getUser()}";
|
||||
$dsn []= "password={$this->getPassword()}";
|
||||
return implode(';', $dsn);
|
||||
}
|
||||
}
|
@ -3,10 +3,10 @@ namespace ProVM\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use ProVM\Concept\Database\ResultSet as RSInterface;
|
||||
use ProVM\Exception\BlankResult;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Exception\Database\BlankResult;
|
||||
|
||||
class ResultSet implements RSInterface
|
||||
class ResultSet implements Database\ResultSet
|
||||
{
|
||||
public function __construct(PDOStatement $statement)
|
||||
{
|
||||
@ -14,52 +14,44 @@ class ResultSet implements RSInterface
|
||||
}
|
||||
|
||||
protected PDOStatement $statement;
|
||||
public function setStatement(PDOStatement $statement): RSInterface
|
||||
|
||||
protected function getStatement(): PDOStatement
|
||||
{
|
||||
return $this->statement;
|
||||
}
|
||||
protected function setStatement(PDOStatement $statement): ResultSet
|
||||
{
|
||||
$this->statement = $statement;
|
||||
return $this;
|
||||
}
|
||||
public function getStatement(): PDOStatement
|
||||
{
|
||||
return $this->statement;
|
||||
}
|
||||
|
||||
public function execute(array $values): RSInterface
|
||||
public function execute(array $data): Database\ResultSet
|
||||
{
|
||||
$this->getStatement()->execute($values);
|
||||
$this->statement->execute($data);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAsArray(): array
|
||||
protected function checkResults(): PDOStatement
|
||||
{
|
||||
$rs = $this->getStatement()->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (!$rs) {
|
||||
throw new BlankResult();
|
||||
if ($this->getStatement()->rowCount() === 0) {
|
||||
throw new BlankResult(query: $this->getStatement()->queryString);
|
||||
}
|
||||
return $rs;
|
||||
return $this->getStatement();
|
||||
}
|
||||
public function getAsObject(): array
|
||||
public function fetchFirst(): array
|
||||
{
|
||||
$rs = $this->getStatement()->fetchAll(PDO::FETCH_OBJ);
|
||||
if (!$rs) {
|
||||
throw new BlankResult();
|
||||
}
|
||||
return $rs;
|
||||
return $this->checkResults()->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function getFirstAsArray(): array
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$rs = $this->getStatement()->fetch(PDO::FETCH_ASSOC);
|
||||
if (!$rs or count($rs) === 0) {
|
||||
throw new BlankResult();
|
||||
}
|
||||
return $rs;
|
||||
return $this->checkResults()->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function getFirstAsObject(): object
|
||||
public function fetchFirstAsObject(): object
|
||||
{
|
||||
$rs = $this->getStatement()->fetch(PDO::FETCH_OBJ);
|
||||
if (!$rs or count($rs) === 0) {
|
||||
throw new BlankResult();
|
||||
}
|
||||
return $rs;
|
||||
return $this->checkResults()->fetch(PDO::FETCH_OBJ);
|
||||
}
|
||||
public function fetchAllAsObjects(): array
|
||||
{
|
||||
return $this->checkResults()->fetchAll(PDO::FETCH_OBJ);
|
||||
}
|
||||
}
|
||||
|
12
src/Database/SQLite.php
Normal file
12
src/Database/SQLite.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
|
||||
use ProVM\Implement\Database;
|
||||
|
||||
class SQLite extends Database
|
||||
{
|
||||
public function getDsn(): string
|
||||
{
|
||||
return "sqlite:{$this->getHost()}";
|
||||
}
|
||||
}
|
@ -2,58 +2,39 @@
|
||||
namespace ProVM\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use ProVM\Concept\Database\ResultSet as ResultSetInterface;
|
||||
use ProVM\Concept\Database\Transaction as TransactionInterface;
|
||||
use ProVM\Concept;
|
||||
|
||||
class Transaction implements TransactionInterface
|
||||
class Transaction implements Concept\Database\Transaction
|
||||
{
|
||||
public function __construct(PDO $pdo)
|
||||
public function __construct(Concept\Database\Connection $connection)
|
||||
{
|
||||
$this->setPDO($pdo);
|
||||
$this->setConnection($connection);
|
||||
}
|
||||
|
||||
protected PDO $pdo;
|
||||
public function setPDO(PDO $pdo): TransactionInterface
|
||||
protected Concept\Database\Connection $connection;
|
||||
|
||||
public function getConnection(): Concept\Database\Connection
|
||||
{
|
||||
$this->pdo = $pdo;
|
||||
return $this->connection;
|
||||
}
|
||||
|
||||
public function setConnection(Concept\Database\Connection $connection): Concept\Database\Transaction
|
||||
{
|
||||
$this->connection = $connection;
|
||||
return $this;
|
||||
}
|
||||
public function getPDO(): PDO
|
||||
|
||||
public function begin(): Concept\Database\Transaction
|
||||
{
|
||||
return $this->pdo;
|
||||
}
|
||||
public function begin(): TransactionInterface
|
||||
{
|
||||
$this->getPDO()->beginTransaction();
|
||||
$this->getConnection()->connect()->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();
|
||||
$this->getConnection()->connect()->commit();
|
||||
}
|
||||
public function rollBack(): void
|
||||
{
|
||||
$this->getPDO()->rollBack();
|
||||
$this->getConnection()->connect()->rollBack();
|
||||
}
|
||||
}
|
||||
|
@ -1,15 +0,0 @@
|
||||
<?php
|
||||
namespace ProVM\Exception;
|
||||
|
||||
use ProVM\Database\Exception;
|
||||
use Throwable;
|
||||
|
||||
class BlankResult extends Exception
|
||||
{
|
||||
public function __construct(?Throwable $previous = null)
|
||||
{
|
||||
$message = "No results found";
|
||||
$code = 0;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
@ -1,11 +1,16 @@
|
||||
<?php
|
||||
namespace ProVM\Database;
|
||||
namespace ProVM\Exception;
|
||||
|
||||
class Exception extends \Exception
|
||||
use Exception;
|
||||
use Throwable;
|
||||
|
||||
abstract class Database extends Exception
|
||||
{
|
||||
const BASE_CODE = 600;
|
||||
|
||||
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
|
||||
{
|
||||
$code += 300;
|
||||
$code += Database::BASE_CODE;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
20
src/Exception/Database/BlankResult.php
Normal file
20
src/Exception/Database/BlankResult.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
namespace ProVM\Exception\Database;
|
||||
|
||||
use Throwable;
|
||||
use ProVM\Exception\Database;
|
||||
|
||||
class BlankResult extends Database
|
||||
{
|
||||
public function __construct(?string $table = null, ?string $query = null, ?Throwable $previous = null)
|
||||
{
|
||||
$message = implode('', [
|
||||
"No results found",
|
||||
($query !== null) ? " in {$query}" : '',
|
||||
($table !== null) ? " in {$table}" : '',
|
||||
'.'
|
||||
]);
|
||||
$code = 1;
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
}
|
65
src/Implement/Database.php
Normal file
65
src/Implement/Database.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
namespace ProVM\Implement;
|
||||
|
||||
use ProVM\Concept;
|
||||
|
||||
abstract class Database implements Concept\Database
|
||||
{
|
||||
protected string $host;
|
||||
protected int $port;
|
||||
protected string $name;
|
||||
protected string $user;
|
||||
protected string $password;
|
||||
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
public function getPort(): int|bool
|
||||
{
|
||||
return $this->port ?? false;
|
||||
}
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
public function getUser(): string
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function setHost(string $host): Concept\Database
|
||||
{
|
||||
$this->host = $host;
|
||||
return $this;
|
||||
}
|
||||
public function setPort(int $port): Concept\Database
|
||||
{
|
||||
$this->port = $port;
|
||||
return $this;
|
||||
}
|
||||
public function setName(string $name): Concept\Database
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
public function setUser(string $username): Concept\Database
|
||||
{
|
||||
$this->user = $username;
|
||||
return $this;
|
||||
}
|
||||
public function setPassword(string $password): Concept\Database
|
||||
{
|
||||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function needsUser(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user