Skip to content

Commit

Permalink
Extending the exception list, adding QueryException.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsudmalis committed Dec 22, 2015
1 parent 427d0bb commit 3b9e1bf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
16 changes: 9 additions & 7 deletions src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace js\tools\dbhandler;

use js\tools\dbhandler\exceptions\DbException;
use js\tools\dbhandler\exceptions\QueryException;

/**
* @author Juris Sudmalis
Expand All @@ -22,6 +23,7 @@ class Handler
* Optional parameters: socket | host[, port] (by default, host=localhost).
* @param array $customOptions : custom PDO::ATTR_* values, e.g., [ PDO::ATTR_PERSISTENT => true ]
* @param string $name : the name of this particular connection
* @return Handler the connection handler
* @throws DbException if something goes wrong. This is the base exception class for all exceptions thrown by this library.
*/
public static function getConnection($name = 'default', array $connectionParameters = [], array $customOptions = [])
Expand Down Expand Up @@ -183,14 +185,14 @@ public function quote($string, $dataType = \PDO::PARAM_STR)
*
* @param string $query : the SQL query to execute on the database
* @return Handler this Handler object
* @throws DbException if the query is invalid
* @throws QueryException if the query is invalid
* @see query, prepare, quote
*/
public function exec($query)
{
if (!is_string($query))
{
throw new DbException('Invalid SQL query', $query);
throw new QueryException('Invalid SQL query', $query);
}

try
Expand All @@ -200,7 +202,7 @@ public function exec($query)
}
catch (\Exception $e)
{
throw new DbException('Failed to exec(): ' . $e->getMessage(), $query, $this->connection);
throw new QueryException('Failed to exec(): ' . $e->getMessage(), $query, $this->connection);
}
}

Expand All @@ -209,14 +211,14 @@ public function exec($query)
*
* @param string $query : the SQL query to execute on the database
* @return \PDOStatement|false the PDOStatement object if the query was successful, false otherwise
* @throws DbException if the query is invalid
* @throws QueryException if the query is invalid
* @see exec, prepare, quote
*/
public function query($query)
{
if (!is_string($query))
{
throw new DbException('Invalid SQL query', $query);
throw new QueryException('Invalid SQL query', $query);
}

try
Expand All @@ -225,7 +227,7 @@ public function query($query)
}
catch (\Exception $e)
{
throw new DbException('Failed to query(): ' . $e->getMessage(), $query, $this->connection);
throw new QueryException('Failed to query(): ' . $e->getMessage(), $query, $this->connection);
}
}

Expand All @@ -243,7 +245,7 @@ public function prepare($query, array $pdoParams = [])
{
if (!is_string($query))
{
throw new DbException('Invalid SQL query', $query);
throw new QueryException('Invalid SQL query', $query);
}

return new PreparedStatement($this->connection, $query, $pdoParams);
Expand Down
24 changes: 5 additions & 19 deletions src/exceptions/DbException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,34 @@
*/
class DbException extends \Exception
{
/** @var string */
private $query;
/** @var array */
private $errorDetails;
private $errorInfo;

/**
* @param string $message : the error message
* @param string $query : the SQL query that caused the exception (optional)
* @param \PDO|null $connection : the connection to retrieve the error info from
*/
public function __construct($message, $query = '', \PDO $connection = null)
public function __construct($message, \PDO $connection = null)
{
parent::__construct($message);
$this->query = $query;

if (is_null($connection))
{
$this->errorDetails = [];
$this->errorInfo = [];
}
else
{
$this->errorDetails = $connection->errorInfo();
$this->errorInfo = $connection->errorInfo();
}
}

/**
* Get the SQL query that caused this exception.
*
* @return string
*/
public function getQuery()
{
return print_r($this->query, true);
}

/**
* Get more information about the error.
*
* @return array an array containing the error details
*/
public function getErrorInfo()
{
return $this->errorDetails;
return $this->errorInfo;
}
}
2 changes: 1 addition & 1 deletion src/exceptions/PreparedStatementException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
namespace js\tools\dbhandler\exceptions;

class PreparedStatementException extends DbException
class PreparedStatementException extends QueryException
{
}
30 changes: 30 additions & 0 deletions src/exceptions/QueryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
namespace js\tools\dbhandler\exceptions;

class QueryException extends DbException
{
/** @var string */
private $query;

/**
* @param string $message : the error message
* @param string $query : the SQL query that caused the exception (optional)
* @param \PDO|null $connection : the connection to retrieve the error info from
*/
public function __construct($message, $query, \PDO $connection = null)
{
parent::__construct($message, $connection);

$this->query = $query;
}

/**
* Get the SQL query that caused this exception.
*
* @return string
*/
public function getQuery()
{
return print_r($this->query, true);
}
}
19 changes: 14 additions & 5 deletions test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ function ($className)
true
);
// common code:
use \js\tools\dbhandler\exceptions\DbException;
use \js\tools\dbhandler\Handler;
use js\tools\dbhandler\exceptions\DbException;
use js\tools\dbhandler\exceptions\QueryException;
use js\tools\dbhandler\Handler;

try
{
Expand All @@ -44,7 +45,10 @@ function ($className)

try
{
$handler->exec('CREATE TABLE IF NOT EXISTS test (
// Inserting would throw an exception if a table by this name already existed, but didn't match the same structure,
// so we're deleting it first thing. Sorry if it was important!
$handler->exec('DROP TABLE IF EXISTS test');
$handler->exec('CREATE TABLE test (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
access_level INT NOT NULL,
Expand Down Expand Up @@ -122,8 +126,13 @@ function ($className)
}
catch (DbException $e)
{
echo $e->getMessage(), '<br/>',
'Query: ', $e->getQuery(), '<br/>';
echo $e->getMessage(), '<br/>';

if ($e instanceof QueryException)
{
echo 'Query: ', $e->getQuery(), '<br/>';
}

print_r($e->getErrorInfo());
}

Expand Down

0 comments on commit 3b9e1bf

Please sign in to comment.