Reviewed-on: #3
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -4,3 +4,6 @@
|
|||||||
|
|
||||||
# PHPStorm
|
# PHPStorm
|
||||||
**/.idea/
|
**/.idea/
|
||||||
|
|
||||||
|
**/.cache/
|
||||||
|
**/.phpunit.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" ]
|
5
compose.yml
Normal file
5
compose.yml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
services:
|
||||||
|
database:
|
||||||
|
build: .
|
||||||
|
volumes:
|
||||||
|
- ./:/app
|
@ -1,23 +1,33 @@
|
|||||||
{
|
{
|
||||||
"name": "provm/database",
|
"name": "provm/database",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"authors": [
|
"version": "1.2.0",
|
||||||
{
|
"authors": [
|
||||||
"name": "Aldarien",
|
{
|
||||||
"email": "aldarien85@gmail.com"
|
"name": "Aldarien",
|
||||||
}
|
"email": "aldarien85@gmail.com"
|
||||||
],
|
|
||||||
"require": {
|
|
||||||
"php": ">=8",
|
|
||||||
"ext-pdo": "*"
|
|
||||||
},
|
|
||||||
"require-dev": {
|
|
||||||
"phpunit/phpunit": "^10.0",
|
|
||||||
"kint-php/kint": "^5.0"
|
|
||||||
},
|
|
||||||
"autoload": {
|
|
||||||
"psr-4": {
|
|
||||||
"ProVM\\": "src/"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=8",
|
||||||
|
"ext-pdo": "*",
|
||||||
|
"provm/query_builder": "^3.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^10.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Database\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"sort-packages": true
|
||||||
|
},
|
||||||
|
"repositories": [
|
||||||
|
{
|
||||||
|
"type": "vcs",
|
||||||
|
"url": "https://git.provm.cl/ProVM/query_builder.git"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
26
phpunit.xml
Normal file
26
phpunit.xml
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
|
||||||
|
bootstrap="vendor/autoload.php"
|
||||||
|
cacheDirectory=".phpunit.cache"
|
||||||
|
executionOrder="depends,defects"
|
||||||
|
requireCoverageMetadata="false"
|
||||||
|
beStrictAboutCoverageMetadata="true"
|
||||||
|
beStrictAboutOutputDuringTests="true"
|
||||||
|
displayDetailsOnPhpunitDeprecations="true"
|
||||||
|
colors="true"
|
||||||
|
failOnPhpunitDeprecation="true"
|
||||||
|
failOnRisky="true"
|
||||||
|
failOnWarning="true">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="default">
|
||||||
|
<directory>tests</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
|
||||||
|
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
|
||||||
|
<include>
|
||||||
|
<directory>src</directory>
|
||||||
|
</include>
|
||||||
|
</source>
|
||||||
|
</phpunit>
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Concept\Database;
|
|
||||||
|
|
||||||
use ProVM\Concept\Database;
|
|
||||||
|
|
||||||
interface Connection
|
|
||||||
{
|
|
||||||
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 $data = null): ResultSet;
|
|
||||||
}
|
|
@ -1,68 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database;
|
|
||||||
|
|
||||||
use PDO;
|
|
||||||
use ProVM\Concept\Database;
|
|
||||||
|
|
||||||
class Connection implements Database\Connection
|
|
||||||
{
|
|
||||||
public function __construct(Database $database)
|
|
||||||
{
|
|
||||||
$this->setDatabase($database);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Database $database;
|
|
||||||
|
|
||||||
protected function getDatabase(): Database
|
|
||||||
{
|
|
||||||
return $this->database;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function setDatabase(Database $database): Database\Connection
|
|
||||||
{
|
|
||||||
$this->database = $database;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected PDO $pdo;
|
|
||||||
public function connect(): PDO
|
|
||||||
{
|
|
||||||
if (!isset($this->pdo)) {
|
|
||||||
$dsn = $this->getDatabase()->getDsn();
|
|
||||||
if ($this->getDatabase()->needsUser()) {
|
|
||||||
$this->pdo = new PDO($dsn, $this->getDatabase()->getUser(), $this->getDatabase()->getPassword());
|
|
||||||
} else {
|
|
||||||
$this->pdo = new PDO($dsn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return $this->pdo;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Database\Transaction $transaction;
|
|
||||||
|
|
||||||
public function transaction(): Database\Transaction
|
|
||||||
{
|
|
||||||
if (!isset($this->transaction)) {
|
|
||||||
$this->transaction = new Transaction($this);
|
|
||||||
}
|
|
||||||
return $this->transaction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function query(string $query): Database\ResultSet
|
|
||||||
{
|
|
||||||
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 $this->query($query);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,40 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace ProVM\Database;
|
|
||||||
|
|
||||||
use PDO;
|
|
||||||
use ProVM\Concept;
|
|
||||||
|
|
||||||
class Transaction implements Concept\Database\Transaction
|
|
||||||
{
|
|
||||||
public function __construct(Concept\Database\Connection $connection)
|
|
||||||
{
|
|
||||||
$this->setConnection($connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Concept\Database\Connection $connection;
|
|
||||||
|
|
||||||
public function getConnection(): Concept\Database\Connection
|
|
||||||
{
|
|
||||||
return $this->connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setConnection(Concept\Database\Connection $connection): Concept\Database\Transaction
|
|
||||||
{
|
|
||||||
$this->connection = $connection;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function begin(): Concept\Database\Transaction
|
|
||||||
{
|
|
||||||
$this->getConnection()->connect()->beginTransaction();
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
public function commit(): void
|
|
||||||
{
|
|
||||||
$this->getConnection()->connect()->commit();
|
|
||||||
}
|
|
||||||
public function rollBack(): void
|
|
||||||
{
|
|
||||||
$this->getConnection()->connect()->rollBack();
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept;
|
namespace Database\Define;
|
||||||
|
|
||||||
interface Database
|
interface Database
|
||||||
{
|
{
|
||||||
@ -9,10 +9,10 @@ interface Database
|
|||||||
public function getUser(): string;
|
public function getUser(): string;
|
||||||
public function getPassword(): string;
|
public function getPassword(): string;
|
||||||
public function needsUser(): bool;
|
public function needsUser(): bool;
|
||||||
public function setHost(string $host): Database;
|
public function setHost(string $host): self;
|
||||||
public function setPort(int $port): Database;
|
public function setPort(int $port): self;
|
||||||
public function setName(string $name): Database;
|
public function setName(string $name): self;
|
||||||
public function setUser(string $username): Database;
|
public function setUser(string $username): self;
|
||||||
public function setPassword(string $password): Database;
|
public function setPassword(string $password): self;
|
||||||
public function getDsn(): string;
|
public function getDsn(): string;
|
||||||
}
|
}
|
43
src/Define/Database/Connection.php
Normal file
43
src/Define/Database/Connection.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Define\Database;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
use PDOException;
|
||||||
|
use Database\Define\Query\Builder;
|
||||||
|
use Database\Exception\Database\InvalidQuery;
|
||||||
|
|
||||||
|
interface Connection
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return self
|
||||||
|
* @throws PDOException
|
||||||
|
*/
|
||||||
|
public function connect(): self;
|
||||||
|
public function getPDO(): PDO;
|
||||||
|
|
||||||
|
public function queryBuilder(): Builder;
|
||||||
|
|
||||||
|
public function transaction(): Transaction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @return ResultSet
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function query(string $query): ResultSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @return ResultSet
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function prepare(string $query): ResultSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @param array|null $data
|
||||||
|
* @return ResultSet
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function execute(string $query, ?array $data = null): ResultSet;
|
||||||
|
}
|
@ -1,9 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept\Database;
|
namespace Database\Define\Database;
|
||||||
|
|
||||||
|
use PDOStatement;
|
||||||
|
|
||||||
interface ResultSet
|
interface ResultSet
|
||||||
{
|
{
|
||||||
public function execute(array $data): ResultSet;
|
public function getStatement(): PDOStatement;
|
||||||
|
public function execute(array $data): self;
|
||||||
|
|
||||||
public function fetchFirst(): array;
|
public function fetchFirst(): array;
|
||||||
public function fetchAll(): array;
|
public function fetchAll(): array;
|
@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Concept\Database;
|
namespace Database\Define\Database;
|
||||||
|
|
||||||
interface Transaction
|
interface Transaction
|
||||||
{
|
{
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Exception;
|
namespace Database\Exception;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
abstract class Database extends Exception
|
abstract class Database extends Exception
|
||||||
{
|
{
|
||||||
const BASE_CODE = 600;
|
const BASE_CODE = 1000;
|
||||||
|
|
||||||
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
|
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
|
||||||
{
|
{
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Exception\Database;
|
namespace Database\Exception\Database;
|
||||||
|
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use ProVM\Exception\Database;
|
use Database\Exception\Database;
|
||||||
|
|
||||||
class BlankResult extends Database
|
class BlankResult extends Database
|
||||||
{
|
{
|
||||||
|
14
src/Exception/Database/InvalidQuery.php
Normal file
14
src/Exception/Database/InvalidQuery.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Exception\Database;
|
||||||
|
|
||||||
|
use Throwable;
|
||||||
|
use Database\Exception;
|
||||||
|
|
||||||
|
class InvalidQuery extends Exception\Database
|
||||||
|
{
|
||||||
|
public function __construct(string $query, ?Throwable $previous = null)
|
||||||
|
{
|
||||||
|
$message = "Invalid query \"{$query}\"";
|
||||||
|
parent::__construct($message, 0, $previous);
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement;
|
namespace Database\Ideal;
|
||||||
|
|
||||||
use ProVM\Concept;
|
use Database\Define;
|
||||||
|
|
||||||
abstract class Database implements Concept\Database
|
abstract class Database implements Define\Database
|
||||||
{
|
{
|
||||||
protected string $host;
|
protected string $host;
|
||||||
protected int $port;
|
protected int $port;
|
||||||
@ -32,27 +32,27 @@ abstract class Database implements Concept\Database
|
|||||||
return $this->password;
|
return $this->password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setHost(string $host): Concept\Database
|
public function setHost(string $host): Define\Database
|
||||||
{
|
{
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setPort(int $port): Concept\Database
|
public function setPort(int $port): Define\Database
|
||||||
{
|
{
|
||||||
$this->port = $port;
|
$this->port = $port;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setName(string $name): Concept\Database
|
public function setName(string $name): Define\Database
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setUser(string $username): Concept\Database
|
public function setUser(string $username): Define\Database
|
||||||
{
|
{
|
||||||
$this->user = $username;
|
$this->user = $username;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
public function setPassword(string $password): Concept\Database
|
public function setPassword(string $password): Define\Database
|
||||||
{
|
{
|
||||||
$this->password = $password;
|
$this->password = $password;
|
||||||
return $this;
|
return $this;
|
121
src/Implement/Connection.php
Normal file
121
src/Implement/Connection.php
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement;
|
||||||
|
|
||||||
|
use PDO;
|
||||||
|
use PDOException;
|
||||||
|
use Database\Define;
|
||||||
|
use Database\Exception\Database\InvalidQuery;
|
||||||
|
|
||||||
|
class Connection implements Define\Database\Connection
|
||||||
|
{
|
||||||
|
public function __construct(Define\Database $database)
|
||||||
|
{
|
||||||
|
$this->setDatabase($database);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Define\Database $database;
|
||||||
|
|
||||||
|
protected function getDatabase(): Define\Database
|
||||||
|
{
|
||||||
|
return $this->database;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setDatabase(Define\Database $database): self
|
||||||
|
{
|
||||||
|
$this->database = $database;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
protected Define\Query\Builder $builder;
|
||||||
|
public function queryBuilder(): Define\Query\Builder
|
||||||
|
{
|
||||||
|
return $this->builder;
|
||||||
|
}
|
||||||
|
public function setBuilder(Define\Query\Builder $builder): self
|
||||||
|
{
|
||||||
|
$this->builder = $builder;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected PDO $pdo;
|
||||||
|
public function connect(): self
|
||||||
|
{
|
||||||
|
if (!isset($this->pdo)) {
|
||||||
|
$dsn = $this->getDatabase()->getDsn();
|
||||||
|
if ($this->getDatabase()->needsUser()) {
|
||||||
|
$this->pdo = new PDO($dsn, $this->getDatabase()->getUser(), $this->getDatabase()->getPassword());
|
||||||
|
} else {
|
||||||
|
$this->pdo = new PDO($dsn);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function getPDO(): PDO
|
||||||
|
{
|
||||||
|
return $this->connect()->pdo;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Define\Database\Transaction $transaction;
|
||||||
|
|
||||||
|
public function transaction(): Define\Database\Transaction
|
||||||
|
{
|
||||||
|
if (!isset($this->transaction)) {
|
||||||
|
$this->transaction = new Transaction($this);
|
||||||
|
}
|
||||||
|
return $this->transaction;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function query(string $query): Define\Database\ResultSet
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$statement = $this->getPDO()->query($query);
|
||||||
|
} catch (PDOException $exception) {
|
||||||
|
throw new InvalidQuery($query, $exception);
|
||||||
|
}
|
||||||
|
if ($statement === false) {
|
||||||
|
throw new InvalidQuery($query);
|
||||||
|
}
|
||||||
|
return new ResultSet($statement);
|
||||||
|
}
|
||||||
|
public function prepare(string $query): Define\Database\ResultSet
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$statement = $this->getPDO()->prepare($query);
|
||||||
|
} catch (PDOException $exception) {
|
||||||
|
throw new InvalidQuery($query, $exception);
|
||||||
|
}
|
||||||
|
if ($statement === false) {
|
||||||
|
throw new InvalidQuery($query);
|
||||||
|
}
|
||||||
|
return new ResultSet($statement);
|
||||||
|
}
|
||||||
|
public function execute(string $query, ?array $data = null): Define\Database\ResultSet
|
||||||
|
{
|
||||||
|
if ($data !== null) {
|
||||||
|
return $this->prepare($query)
|
||||||
|
->execute($data);
|
||||||
|
}
|
||||||
|
return $this->query($query);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @param array|null $data
|
||||||
|
* @return array
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function fetchOne(string $query, ?array $data = null): array
|
||||||
|
{
|
||||||
|
return $this->execute($query, $data)->fetchFirst();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $query
|
||||||
|
* @param array|null $data
|
||||||
|
* @return array
|
||||||
|
* @throws InvalidQuery
|
||||||
|
*/
|
||||||
|
public function fetchMany(string $query, ?array $data = null): array
|
||||||
|
{
|
||||||
|
return $this->execute($query, $data)->fetchAll();
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database;
|
namespace Database\Implement;
|
||||||
|
|
||||||
use ProVM\Implement\Database;
|
use Database\Ideal\Database;
|
||||||
|
|
||||||
class MySQL extends Database
|
class MySQL extends Database
|
||||||
{
|
{
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database;
|
namespace Database\Implement;
|
||||||
|
|
||||||
use ProVM\Implement\Database;
|
use Database\Ideal\Database;
|
||||||
|
|
||||||
class PostgreSQL extends Database
|
class PostgreSQL extends Database
|
||||||
{
|
{
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database;
|
namespace Database\Implement;
|
||||||
|
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOStatement;
|
use PDOStatement;
|
||||||
use ProVM\Concept\Database;
|
use Database\Define;
|
||||||
use ProVM\Exception\Database\BlankResult;
|
use Database\Exception\Database\BlankResult;
|
||||||
|
|
||||||
class ResultSet implements Database\ResultSet
|
class ResultSet implements Define\Database\ResultSet
|
||||||
{
|
{
|
||||||
public function __construct(PDOStatement $statement)
|
public function __construct(PDOStatement $statement)
|
||||||
{
|
{
|
||||||
@ -15,22 +15,26 @@ class ResultSet implements Database\ResultSet
|
|||||||
|
|
||||||
protected PDOStatement $statement;
|
protected PDOStatement $statement;
|
||||||
|
|
||||||
protected function getStatement(): PDOStatement
|
public function getStatement(): PDOStatement
|
||||||
{
|
{
|
||||||
return $this->statement;
|
return $this->statement;
|
||||||
}
|
}
|
||||||
protected function setStatement(PDOStatement $statement): ResultSet
|
protected function setStatement(PDOStatement $statement): self
|
||||||
{
|
{
|
||||||
$this->statement = $statement;
|
$this->statement = $statement;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function execute(array $data): Database\ResultSet
|
public function execute(array $data): self
|
||||||
{
|
{
|
||||||
$this->statement->execute($data);
|
$this->statement->execute($data);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return PDOStatement
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
protected function checkResults(): PDOStatement
|
protected function checkResults(): PDOStatement
|
||||||
{
|
{
|
||||||
if ($this->getStatement()->rowCount() === 0) {
|
if ($this->getStatement()->rowCount() === 0) {
|
||||||
@ -38,18 +42,38 @@ class ResultSet implements Database\ResultSet
|
|||||||
}
|
}
|
||||||
return $this->getStatement();
|
return $this->getStatement();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
public function fetchFirst(): array
|
public function fetchFirst(): array
|
||||||
{
|
{
|
||||||
return $this->checkResults()->fetch(PDO::FETCH_ASSOC);
|
return $this->checkResults()->fetch(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
public function fetchAll(): array
|
public function fetchAll(): array
|
||||||
{
|
{
|
||||||
return $this->checkResults()->fetchAll(PDO::FETCH_ASSOC);
|
return $this->checkResults()->fetchAll(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return object
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
public function fetchFirstAsObject(): object
|
public function fetchFirstAsObject(): object
|
||||||
{
|
{
|
||||||
return $this->checkResults()->fetch(PDO::FETCH_OBJ);
|
return $this->checkResults()->fetch(PDO::FETCH_OBJ);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws BlankResult
|
||||||
|
*/
|
||||||
public function fetchAllAsObjects(): array
|
public function fetchAllAsObjects(): array
|
||||||
{
|
{
|
||||||
return $this->checkResults()->fetchAll(PDO::FETCH_OBJ);
|
return $this->checkResults()->fetchAll(PDO::FETCH_OBJ);
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Database;
|
namespace Database\Implement;
|
||||||
|
|
||||||
use ProVM\Implement\Database;
|
use Database\Ideal\Database;
|
||||||
|
|
||||||
class SQLite extends Database
|
class SQLite extends Database
|
||||||
{
|
{
|
39
src/Implement/Transaction.php
Normal file
39
src/Implement/Transaction.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
namespace Database\Implement;
|
||||||
|
|
||||||
|
use Database\Define;
|
||||||
|
|
||||||
|
class Transaction implements Define\Database\Transaction
|
||||||
|
{
|
||||||
|
public function __construct(Define\Database\Connection $connection)
|
||||||
|
{
|
||||||
|
$this->setConnection($connection);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Define\Database\Connection $connection;
|
||||||
|
|
||||||
|
public function getConnection(): Define\Database\Connection
|
||||||
|
{
|
||||||
|
return $this->connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setConnection(Define\Database\Connection $connection): self
|
||||||
|
{
|
||||||
|
$this->connection = $connection;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function begin(): self
|
||||||
|
{
|
||||||
|
$this->getConnection()->getPDO()->beginTransaction();
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function commit(): void
|
||||||
|
{
|
||||||
|
$this->getConnection()->getPDO()->commit();
|
||||||
|
}
|
||||||
|
public function rollBack(): void
|
||||||
|
{
|
||||||
|
$this->getConnection()->getPDO()->rollBack();
|
||||||
|
}
|
||||||
|
}
|
61
tests/ConnectionTest.php
Normal file
61
tests/ConnectionTest.php
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
use Database\Implement\Connection;
|
||||||
|
use Database\Define;
|
||||||
|
|
||||||
|
class ConnectionTest extends TestCase
|
||||||
|
{
|
||||||
|
protected string $host = '/tmp/test.db3';
|
||||||
|
protected string $tableName = 'test_table';
|
||||||
|
protected PDO $pdo;
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->pdo = new PDO("sqlite:{$this->host}");
|
||||||
|
$query = "CREATE TABLE IF NOT EXISTS {$this->tableName} (id INTEGER PRIMARY KEY, test TEXT)";
|
||||||
|
$this->pdo->query($query);
|
||||||
|
$query = "INSERT INTO {$this->tableName} (test) VALUES ('test')";
|
||||||
|
$this->pdo->query($query);
|
||||||
|
}
|
||||||
|
protected function tearDown(): void
|
||||||
|
{
|
||||||
|
unset($this->pdo);
|
||||||
|
unlink($this->host);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getConnection(): Connection
|
||||||
|
{
|
||||||
|
$host = $this->host;
|
||||||
|
|
||||||
|
$database = $this->getMockBuilder(Define\Database::class)->getMock();
|
||||||
|
$database->method('getHost')->willReturn($host);
|
||||||
|
$database->method('getDsn')->willReturn("sqlite:{$host}");
|
||||||
|
$database->method('needsUser')->willReturn(false);
|
||||||
|
|
||||||
|
return new Connection($database);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConnection()
|
||||||
|
{
|
||||||
|
$connection = $this->getConnection();
|
||||||
|
$this->assertEquals($this->pdo, $connection->getPDO());
|
||||||
|
}
|
||||||
|
public function testQuery()
|
||||||
|
{
|
||||||
|
$connection = $this->getConnection();
|
||||||
|
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||||
|
$connection->query($query);
|
||||||
|
$query = "SELECT * FROM test_table";
|
||||||
|
$connection->query($query);
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
public function testPrepare()
|
||||||
|
{
|
||||||
|
$connection = $this->getConnection();
|
||||||
|
$query = "CREATE TABLE IF NOT EXISTS test_table (id INTEGER PRIMARY KEY, test TEXT)";
|
||||||
|
$connection->query($query);
|
||||||
|
$query = "SELECT * FROM test_table";
|
||||||
|
$connection->prepare($query);
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
32
tests/MySQLTest.php
Normal file
32
tests/MySQLTest.php
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?php
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Database\Implement\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 Database\Implement\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 Database\Implement\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 Database\Implement\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 Database\Implement\Transaction;
|
||||||
|
|
||||||
|
class TransactionTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testTransaction()
|
||||||
|
{
|
||||||
|
$connection = $this->createMock(Database\Define\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