Added testing
This commit is contained in:
@ -3,6 +3,7 @@ namespace ProVM\Database;
|
||||
|
||||
use PDO;
|
||||
use ProVM\Concept\Database;
|
||||
use ProVM\Exception\Database\InvalidQuery;
|
||||
|
||||
class Connection implements Database\Connection
|
||||
{
|
||||
@ -50,11 +51,19 @@ class Connection implements Database\Connection
|
||||
|
||||
public function query(string $query): Database\ResultSet
|
||||
{
|
||||
return new ResultSet($this->connect()->query($query));
|
||||
$statement = $this->connect()->query($query);
|
||||
if ($statement === false) {
|
||||
throw new InvalidQuery($query);
|
||||
}
|
||||
return new ResultSet($statement);
|
||||
}
|
||||
public function prepare(string $query): Database\ResultSet
|
||||
{
|
||||
return new ResultSet($this->connect()->prepare($query));
|
||||
$statement = $this->connect()->prepare($query);
|
||||
if ($statement === false) {
|
||||
throw new InvalidQuery($query);
|
||||
}
|
||||
return new ResultSet($statement);
|
||||
}
|
||||
public function execute(string $query, ?array $data = null): Database\ResultSet
|
||||
{
|
||||
@ -65,4 +74,13 @@ class Connection implements Database\Connection
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user