Compare commits
7 Commits
ec19c25681
...
develop
Author | SHA1 | Date | |
---|---|---|---|
96d0232d78 | |||
99344fda7d | |||
a326904825 | |||
2c6c0e6e55 | |||
cc7cd638e3 | |||
758ff0e282 | |||
6dda8e1515 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,3 +4,5 @@
|
||||
|
||||
# PHPStorm
|
||||
**/.idea/
|
||||
|
||||
**/.cache/
|
||||
|
22
Dockerfile
Normal file
22
Dockerfile
Normal file
@ -0,0 +1,22 @@
|
||||
FROM composer:lts as deps
|
||||
WORKDIR /app
|
||||
RUN --mount=type=bind,source=./composer.json,target=composer.json \
|
||||
--mount=type=bind,source=./composer.lock,target=composer.lock \
|
||||
--mount=type=cache,target=/tmp/cache \
|
||||
composer install --no-interaction
|
||||
|
||||
FROM php:8-cli as base
|
||||
WORKDIR /app
|
||||
RUN apt-get update && \
|
||||
apt-get install -yq --no-install-recommends libsqlite3-dev && \
|
||||
rm -rf /var/lib/apt/lists/* && \
|
||||
docker-php-ext-install pdo pdo_sqlite
|
||||
COPY ./src /app/src
|
||||
|
||||
FROM base as dev
|
||||
COPY ./tests /app/tests
|
||||
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
|
||||
COPY --from=deps /app/vendor/ /app/vendor
|
||||
|
||||
FROM dev as test
|
||||
ENTRYPOINT [ "./vendor/bin/phpunit" ]
|
35
Readme.md
35
Readme.md
@ -28,7 +28,7 @@ Database Abstraction Layer
|
||||
...
|
||||
"require": {
|
||||
...
|
||||
"provm/database": "^2.0"
|
||||
"provm/database": "^2.3"
|
||||
...
|
||||
},
|
||||
...
|
||||
@ -41,13 +41,13 @@ For `MySQL`/`MariaDB`
|
||||
|
||||
Without `DI`
|
||||
```
|
||||
$database new MySQL();
|
||||
$database new ProVM\Database\MySQL();
|
||||
$database->setHost(<host>);
|
||||
$database->setPort(<port>); // If diferent from 3306
|
||||
$database->setUsername(<username>);
|
||||
$database->setPassword(<password>);
|
||||
|
||||
$connection = new Connection($database);
|
||||
$connection = new ProVM\Database\Connection($database);
|
||||
```
|
||||
With `DI`
|
||||
```
|
||||
@ -76,8 +76,20 @@ $rs = $connection->execute(<query>, <values>);
|
||||
```
|
||||
Get data from ResultSet
|
||||
```
|
||||
$data = $rs->getAsArray();
|
||||
$data_objs = $rs->getAsObject();
|
||||
$data = $rs->fetchFirst();
|
||||
$data_object = $rs->fetchFirstAsObject();
|
||||
$data_array = $rs->fetchAll();
|
||||
$data_array_of_objects = $rs->fetchAllAsObjects();
|
||||
```
|
||||
Use transactions
|
||||
```
|
||||
$connection->transaction()->begin();
|
||||
try {
|
||||
$connection->execute($query, $values);
|
||||
$connection->transaction()->commit();
|
||||
} catch (PDOException $exception) {
|
||||
$connection->transaction()->rollBack();
|
||||
}
|
||||
```
|
||||
|
||||
## Definitions
|
||||
@ -97,23 +109,20 @@ Connection handling
|
||||
+ `Connection::query` Query the database
|
||||
+ `Connection::prepare` Prepare query statement
|
||||
+ `Connection::execute` Prepare and execute a query statement
|
||||
+ `Conneciton::transaction` Return a transaction
|
||||
+ `Connection::transaction` Return a transaction
|
||||
|
||||
### Transaction
|
||||
Transaction
|
||||
+ `Transaction::begin` Begin transaction
|
||||
+ `Transaction::commit` Commit changes
|
||||
+ `Transaction::rollBack` Roll back changes
|
||||
+ `Transaction::query` Same as `Connection::query`
|
||||
+ `Transaction::prepare` Same as `Connection::prepare`
|
||||
+ `Transaction::execute` Same as `Connection::execute`
|
||||
|
||||
### ResultSet
|
||||
Result set to handle PDOStatement
|
||||
+ `ResultSet::execute` Execute a prepared statement
|
||||
+ `ResultSet::getAsArray` Return query results as array of associative arrays
|
||||
+ `ResultSet::getAsObject` Return query results as array of objects
|
||||
+ `ResultSet::getFirst` Return first result as object
|
||||
+ `ResultSet::fetchAll` Return query results as array of associative arrays
|
||||
+ `ResultSet::fetchAllAsObjects` Return query results as array of objects
|
||||
+ `ResultSet::fetchFirst` Return first result as associative array
|
||||
+ `ResultSet::fetchFirstAsObject` Return first result as object
|
||||
|
||||
## TODO
|
||||
+ Implement other database types. eg: PostgreSQL, SQLite
|
||||
|
5
compose.yml
Normal file
5
compose.yml
Normal file
@ -0,0 +1,5 @@
|
||||
services:
|
||||
database:
|
||||
build: .
|
||||
volumes:
|
||||
- ./:/app
|
@ -1,18 +1,23 @@
|
||||
{
|
||||
"name": "provm/database",
|
||||
"type": "project",
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^9.5",
|
||||
"kint-php/kint": "^4.2"
|
||||
},
|
||||
"type": "library",
|
||||
"version": "1.2.0",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Aldarien",
|
||||
"email": "aldarien85@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {},
|
||||
"require": {
|
||||
"php": ">=8",
|
||||
"ext-pdo": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^10.0"
|
||||
},
|
||||
"autoload": {
|
||||
"ProVM\\": "src/"
|
||||
"psr-4": {
|
||||
"ProVM\\": "src/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
28
phpunit.xml
Normal file
28
phpunit.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
cacheResultFile=".cache/test-results"
|
||||
executionOrder="depends,defects"
|
||||
forceCoversAnnotation="false"
|
||||
beStrictAboutCoversAnnotation="true"
|
||||
beStrictAboutOutputDuringTests="true"
|
||||
beStrictAboutTodoAnnotatedTests="true"
|
||||
convertDeprecationsToExceptions="true"
|
||||
colors="true"
|
||||
failOnRisky="true"
|
||||
failOnWarning="true"
|
||||
verbose="true">
|
||||
<testsuites>
|
||||
<testsuite name="default">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<coverage cacheDirectory=".cache/code-coverage"
|
||||
processUncoveredFiles="true">
|
||||
<include>
|
||||
<directory suffix=".php">src</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
</phpunit>
|
@ -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,13 +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 getFirst(): mixed;
|
||||
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,85 @@
|
||||
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;
|
||||
use ProVM\Exception\Database\InvalidQuery;
|
||||
|
||||
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}");
|
||||
$statement = $this->connect()->query($query);
|
||||
if ($statement === false) {
|
||||
throw new InvalidQuery($query);
|
||||
}
|
||||
return new ResultSet($st);
|
||||
return new ResultSet($statement);
|
||||
}
|
||||
public function execute(string $query, array $values): ResultSetInterface
|
||||
public function prepare(string $query): Database\ResultSet
|
||||
{
|
||||
return $this->prepare($query)->execute($values);
|
||||
$statement = $this->connect()->prepare($query);
|
||||
if ($statement === false) {
|
||||
throw new InvalidQuery($query);
|
||||
}
|
||||
return new ResultSet($statement);
|
||||
}
|
||||
public function transaction(): TransactionInterface
|
||||
public function execute(string $query, ?array $data = null): Database\ResultSet
|
||||
{
|
||||
return new Transaction($this->getPDO());
|
||||
if ($data !== null) {
|
||||
$rs = $this->prepare($query);
|
||||
$rs->execute($data);
|
||||
return $rs;
|
||||
}
|
||||
return $this->query($query);
|
||||
}
|
||||
|
||||
public function fetchOne(string $query, ?array $data = null): array
|
||||
{
|
||||
return $this->execute($query, $data)->fetchFirst();
|
||||
}
|
||||
public function fetchMany(string $query, ?array $data = null): array
|
||||
{
|
||||
return $this->execute($query, $data)->fetchAll();
|
||||
}
|
||||
}
|
||||
|
@ -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,9 +3,10 @@ namespace ProVM\Database;
|
||||
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use ProVM\Concept\Database\ResultSet as RSInterface;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Exception\Database\BlankResult;
|
||||
|
||||
class ResultSet implements RSInterface
|
||||
class ResultSet implements Database\ResultSet
|
||||
{
|
||||
public function __construct(PDOStatement $statement)
|
||||
{
|
||||
@ -13,32 +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
|
||||
{
|
||||
return $this->getStatement()->fetchAll(PDO::FETCH_ASSOC);
|
||||
if ($this->getStatement()->rowCount() === 0) {
|
||||
throw new BlankResult(query: $this->getStatement()->queryString);
|
||||
}
|
||||
return $this->getStatement();
|
||||
}
|
||||
public function getAsObject(): array
|
||||
public function fetchFirst(): array
|
||||
{
|
||||
return $this->getStatement()->fetchAll(PDO::FETCH_OBJ);
|
||||
return $this->checkResults()->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function getFirst(): mixed
|
||||
public function fetchAll(): array
|
||||
{
|
||||
return $this->getStatement()->fetch(PDO::FETCH_OBJ);
|
||||
return $this->checkResults()->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
public function fetchFirstAsObject(): object
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
16
src/Exception/Database.php
Normal file
16
src/Exception/Database.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace ProVM\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 += 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);
|
||||
}
|
||||
}
|
14
src/Exception/Database/InvalidQuery.php
Normal file
14
src/Exception/Database/InvalidQuery.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
namespace ProVM\Exception\Database;
|
||||
|
||||
use Throwable;
|
||||
use ProVM\Exception;
|
||||
|
||||
class InvalidQuery extends Exception\Database
|
||||
{
|
||||
public function __construct(string $query, ?Throwable $previous = null)
|
||||
{
|
||||
$message = "Invalid query \"{$query}\"";
|
||||
parent::__construct($message, 0, $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;
|
||||
}
|
||||
}
|
65
tests/ConnectionTest.php
Normal file
65
tests/ConnectionTest.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use ProVM\Database\Connection;
|
||||
use ProVM\Concept;
|
||||
|
||||
class ConnectionTest extends TestCase
|
||||
{
|
||||
protected PDO $pdo;
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->pdo = new PDO('sqlite::memory:');
|
||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||
$this->pdo->query($query);
|
||||
}
|
||||
protected function tearDown(): void
|
||||
{
|
||||
unset($this->pdo);
|
||||
}
|
||||
|
||||
public function testConnection()
|
||||
{
|
||||
$host = "memory";
|
||||
|
||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
||||
$database->method('getHost')->willReturn($host);
|
||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
||||
$database->method('needsUser')->willReturn(false);
|
||||
|
||||
$connection = new Connection($database);
|
||||
$this->assertEquals($this->pdo, $connection->connect());
|
||||
}
|
||||
public function testQuery()
|
||||
{
|
||||
$host = "memory";
|
||||
|
||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
||||
$database->method('getHost')->willReturn($host);
|
||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
||||
$database->method('needsUser')->willReturn(false);
|
||||
|
||||
$connection = new Connection($database);
|
||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||
$connection->query($query);
|
||||
$query = "SELECT * FROM test_table";
|
||||
$result = $connection->query($query);
|
||||
$this->assertInstanceOf(Concept\Database\ResultSet::class, $result);
|
||||
}
|
||||
public function testPrepare()
|
||||
{
|
||||
$host = "memory";
|
||||
|
||||
$database = $this->getMockBuilder(Concept\Database::class)->getMock();
|
||||
$database->method('getHost')->willReturn($host);
|
||||
$database->method('getDsn')->willReturn("sqlite::{$host}");
|
||||
$database->method('needsUser')->willReturn(false);
|
||||
|
||||
$connection = new Connection($database);
|
||||
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||
$connection->query($query);
|
||||
$query = "SELECT * FROM test_table";
|
||||
$result = $connection->prepare($query);
|
||||
$this->assertInstanceOf(Concept\Database\ResultSet::class, $result);
|
||||
}
|
||||
}
|
32
tests/MySQLTest.php
Normal file
32
tests/MySQLTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ProVM\Database\MySQL;
|
||||
|
||||
class MySQLTest extends TestCase
|
||||
{
|
||||
public function testDatabase()
|
||||
{
|
||||
$host = "testhost";
|
||||
$port = 1234;
|
||||
$name = "testdb";
|
||||
$user = "testuser";
|
||||
$pass = "testpass";
|
||||
|
||||
$dsn = "mysql:host={$host};port={$port};dbname={$name}";
|
||||
|
||||
$database = new MySQL();
|
||||
$database->setHost($host);
|
||||
$database->setPort($port);
|
||||
$database->setName($name);
|
||||
$database->setUser($user);
|
||||
$database->setPassword($pass);
|
||||
|
||||
$this->assertEquals($host, $database->getHost());
|
||||
$this->assertEquals($port, $database->getPort());
|
||||
$this->assertEquals($name, $database->getName());
|
||||
$this->assertEquals($user, $database->getUser());
|
||||
$this->assertEquals($pass, $database->getPassword());
|
||||
$this->assertTrue($database->needsUser());
|
||||
$this->assertEquals($dsn, $database->getDsn());
|
||||
}
|
||||
}
|
32
tests/PostgreSQLTest.php
Normal file
32
tests/PostgreSQLTest.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ProVM\Database\PostgreSQL;
|
||||
|
||||
class PostgreSQLTest extends TestCase
|
||||
{
|
||||
public function testDatabase()
|
||||
{
|
||||
$host = "testhost";
|
||||
$port = 1234;
|
||||
$name = "testdb";
|
||||
$user = "testuser";
|
||||
$pass = "testpass";
|
||||
|
||||
$dsn = "pgsql:host={$host};port={$port};dbname={$name};user={$user};password={$pass}";
|
||||
|
||||
$database = new PostgreSQL();
|
||||
$database->setHost($host);
|
||||
$database->setPort($port);
|
||||
$database->setName($name);
|
||||
$database->setUser($user);
|
||||
$database->setPassword($pass);
|
||||
|
||||
$this->assertEquals($host, $database->getHost());
|
||||
$this->assertEquals($port, $database->getPort());
|
||||
$this->assertEquals($name, $database->getName());
|
||||
$this->assertEquals($user, $database->getUser());
|
||||
$this->assertEquals($pass, $database->getPassword());
|
||||
$this->assertFalse($database->needsUser());
|
||||
$this->assertEquals($dsn, $database->getDsn());
|
||||
}
|
||||
}
|
26
tests/ResultSetTest.php
Normal file
26
tests/ResultSetTest.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
use ProVM\Database\ResultSet;
|
||||
|
||||
class ResultSetTest extends TestCase
|
||||
{
|
||||
public function testResultSet()
|
||||
{
|
||||
$result1 = ['col1', 'col2', 'col3'];
|
||||
$result2 = [['col1', 'col2'], ['col3', 'col4']];
|
||||
|
||||
$statement = $this->getMockBuilder(PDOStatement::class)->getMock();
|
||||
$statement->method('execute')->willReturn(true);
|
||||
$statement->method('fetch')->willReturn($result1);
|
||||
$statement->method('fetchAll')->willReturn($result2);
|
||||
$statement->method('rowCount')->willReturn(2);
|
||||
|
||||
$resultSet = new ResultSet($statement);
|
||||
|
||||
$resultSet->execute(['foo' => 'bar']);
|
||||
$this->assertTrue(true);
|
||||
$this->assertEquals($result1, $resultSet->fetchFirst());
|
||||
$this->assertEquals($result2, $resultSet->fetchAll());
|
||||
}
|
||||
}
|
20
tests/SQLiteTest.php
Normal file
20
tests/SQLiteTest.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ProVM\Database\SQLite;
|
||||
|
||||
class SQLiteTest extends TestCase
|
||||
{
|
||||
public function testDatabase()
|
||||
{
|
||||
$host = ":memory:";
|
||||
|
||||
$dsn = "sqlite:{$host}";
|
||||
|
||||
$database = new SQLite();
|
||||
$database->setHost($host);
|
||||
|
||||
$this->assertEquals($host, $database->getHost());
|
||||
$this->assertFalse($database->needsUser());
|
||||
$this->assertEquals($dsn, $database->getDsn());
|
||||
}
|
||||
}
|
19
tests/TransactionTest.php
Normal file
19
tests/TransactionTest.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ProVM\Database\Transaction;
|
||||
|
||||
class TransactionTest extends TestCase
|
||||
{
|
||||
public function testTransaction()
|
||||
{
|
||||
$connection = $this->createMock(ProVM\Concept\Database\Connection::class);
|
||||
$transaction = new Transaction($connection);
|
||||
$transaction->begin();
|
||||
$this->assertTrue(true);
|
||||
$transaction->commit();
|
||||
$this->assertTrue(true);
|
||||
$transaction->begin();
|
||||
$transaction->rollback();
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user