mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-09 03:52:15 +09:00
Improve error handling by using wrapper classes for PDO and PDOStatement
This commit is contained in:
parent
47bb30c535
commit
936568a8a5
3 changed files with 265 additions and 129 deletions
116
common/framework/helpers/dbhelper.php
Normal file
116
common/framework/helpers/dbhelper.php
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Helpers;
|
||||
|
||||
use Rhymix\Framework\DB;
|
||||
use Rhymix\Framework\Debug;
|
||||
use Rhymix\Framework\Exceptions\DBError;
|
||||
|
||||
/**
|
||||
* DB helper class.
|
||||
*/
|
||||
class DBHelper extends \PDO
|
||||
{
|
||||
/**
|
||||
* Store the database type (e.g. master) here.
|
||||
*/
|
||||
protected $_type = 'master';
|
||||
|
||||
/**
|
||||
* Set the database type.
|
||||
*/
|
||||
public function setType(string $type)
|
||||
{
|
||||
$this->_type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a prepared statement.
|
||||
*
|
||||
* @param string $statement
|
||||
* @param array $driver_options
|
||||
* @return PDOStatement|DBStmtHelper
|
||||
*/
|
||||
public function prepare($statement, $driver_options = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if ($driver_options)
|
||||
{
|
||||
$stmt = parent::prepare($statement, $driver_options);
|
||||
}
|
||||
else
|
||||
{
|
||||
$stmt = parent::prepare($statement);
|
||||
}
|
||||
$stmt->setFetchMode(\PDO::FETCH_OBJ);
|
||||
$stmt->setType($this->_type);
|
||||
}
|
||||
catch (\PDOException $e)
|
||||
{
|
||||
throw new DBError($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query.
|
||||
*
|
||||
* @param string $statement
|
||||
* @return PDOStatement|DBStmtHelper
|
||||
*/
|
||||
public function query($statement)
|
||||
{
|
||||
$start_time = microtime(true);
|
||||
$db_class = DB::getInstance($this->_type);
|
||||
$args = func_get_args();
|
||||
array_shift($args);
|
||||
|
||||
try
|
||||
{
|
||||
$stmt = parent::query($statement, ...$args);
|
||||
$stmt->setFetchMode(\PDO::FETCH_OBJ);
|
||||
$stmt->setType($this->_type);
|
||||
$db_class->clearError();
|
||||
}
|
||||
catch (\PDOException $e)
|
||||
{
|
||||
$db_class->setError(-1, $e->getMessage());
|
||||
}
|
||||
|
||||
$elapsed_time = microtime(true) - $start_time;
|
||||
$db_class->addElapsedTime($elapsed_time);
|
||||
Debug::addQuery($db_class->getQueryLog($statement, '', $elapsed_time));
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a query and return the number of affected rows.
|
||||
*
|
||||
* @param string $statement
|
||||
* @return bool
|
||||
*/
|
||||
public function exec($query)
|
||||
{
|
||||
$start_time = microtime(true);
|
||||
$db_class = DB::getInstance($this->_type);
|
||||
|
||||
try
|
||||
{
|
||||
$result = parent::exec($query);
|
||||
$db_class->clearError();
|
||||
}
|
||||
catch (\PDOException $e)
|
||||
{
|
||||
$db_class->setError(-1, $e->getMessage());
|
||||
}
|
||||
|
||||
$elapsed_time = microtime(true) - $start_time;
|
||||
$db_class->addElapsedTime($elapsed_time);
|
||||
Debug::addQuery($db_class->getQueryLog($query, '', $elapsed_time));
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
59
common/framework/helpers/dbstmthelper.php
Normal file
59
common/framework/helpers/dbstmthelper.php
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Rhymix\Framework\Helpers;
|
||||
|
||||
use Rhymix\Framework\DB;
|
||||
use Rhymix\Framework\Debug;
|
||||
use Rhymix\Framework\Exceptions\DBError;
|
||||
|
||||
/**
|
||||
* DB Statement helper class.
|
||||
*/
|
||||
class DBStmtHelper extends \PDOStatement
|
||||
{
|
||||
/**
|
||||
* Store the database type (e.g. master) here.
|
||||
*/
|
||||
protected $_type = 'master';
|
||||
|
||||
/**
|
||||
* Set the database type.
|
||||
*/
|
||||
public function setType(string $type)
|
||||
{
|
||||
$this->_type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a prepared statement.
|
||||
*
|
||||
* @param array $input_parameters
|
||||
* @return bool
|
||||
*/
|
||||
public function execute($input_parameters = null): bool
|
||||
{
|
||||
$start_time = microtime(true);
|
||||
$db_class = DB::getInstance($this->_type);
|
||||
|
||||
try
|
||||
{
|
||||
$result = parent::execute($input_parameters);
|
||||
$db_class->clearError();
|
||||
|
||||
$elapsed_time = microtime(true) - $start_time;
|
||||
$db_class->addElapsedTime($elapsed_time);
|
||||
Debug::addQuery($db_class->getQueryLog($this->queryString, '', $elapsed_time));
|
||||
}
|
||||
catch (\PDOException $e)
|
||||
{
|
||||
$db_class->setError(-1, $e->getMessage());
|
||||
|
||||
$elapsed_time = microtime(true) - $start_time;
|
||||
$db_class->addElapsedTime($elapsed_time);
|
||||
Debug::addQuery($db_class->getQueryLog($this->queryString, '', $elapsed_time));
|
||||
throw new DBError($e->getMessage(), 0, $e);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue