Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 46
MariaDB
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
342.00
0.00% covered (danger)
0.00%
0 / 46
 __construct
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 6
 query
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 3
 runPreparedQuery
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 9
 runNormalQuery
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 6
 beginTransaction
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 4
 commitTransaction
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 rollBackTransaction
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 2
 getExceptionInfo
0.00% covered (danger)
0.00%
0 / 1
42.00
0.00% covered (danger)
0.00%
0 / 14
<?php
namespace Luxian\Database;
use PDO;
use RuntimeException;
class MariaDB implements DatabaseInterface
{
    /** @var \PDO */
    private $pdo;
    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
        $this->pdo->setAttribute(
            PDO::ATTR_STATEMENT_CLASS,
            [MariaDBStatement::class]
        );
        $this->pdo->setAttribute(PDO::ERRMODE_EXCEPTION, true);
    }
    public function query(
        string $sql,
        array $params = []
    ): DatabaseStatementInterface
    {
        if (count($params)) {
            return $this->runPreparedQuery($sql, $params);
        }
        return $this->runNormalQuery($sql);
    }
    private function runPreparedQuery(
        string $sql,
        array $params = []
    ): DatabaseStatementInterface
    {
        /** @var bool|\Luxian\Database\MariaDBStatement $statement */
        $statement = $this->pdo->prepare($sql);
        if (is_bool($statement)) {
            $message = $this->getExceptionInfo($this->pdo->errorInfo(), $sql);
            throw new RuntimeException($message);
        }
        if ($statement->execute($params) === false) {
            $message = $this->getExceptionInfo(
                $statement->errorInfo(),
                $sql,
                $params
            );
            throw new RuntimeException($message);
        }
        return $statement;
    }
    private function runNormalQuery(string $sql): DatabaseStatementInterface
    {
        /** @var bool|\Luxian\Database\MariaDBStatement $statement */
        $statement = $this->pdo->query($sql);
        if (is_bool($statement)) {
            $message = $this->getExceptionInfo(
                $this->pdo->errorInfo(),
                $sql
            );
            throw new RuntimeException($message);
        }
        return $statement;
    }
    public function beginTransaction(): void
    {
        if ($this->pdo->beginTransaction() === false) {
            $message = $this->getExceptionInfo($this->pdo->errorInfo());
            throw new RuntimeException($message);
        }
    }
    public function commitTransaction(): void
    {
        $this->pdo->commit();
    }
    public function rollBackTransaction(): void
    {
        $this->pdo->rollBack();
    }
    private function getExceptionInfo(
        array $errorInfo = [],
        string $sql = '',
        array $params = []
    ): string
    {
        $message = [];
        $message[] = 'Database error! ';
        if (!empty($errorInfo[0])) {
            $message[] = 'SQL error code: ' . $errorInfo[0];
        }
        if (!empty($errorInfo[1])) {
            $message[] = 'PDO error code: ' . $errorInfo[1];
        }
        if (!empty($errorInfo[2])) {
            $message[] = 'Error message: ' . $errorInfo[2];
        }
        if ($sql !== '') {
            $message[] = 'SQL: ' . $sql;
        }
        if (count($params) > 0) {
            $message[] = 'SQL args: ';
            $message[] = var_export($params, true);
        }
        return implode(PHP_EOL, $message);
    }
}