Skip to content

Commit

Permalink
Merge pull request #28 from kazsaj/throw-exception-on-errors
Browse files Browse the repository at this point in the history
Throw exception when executing prepared statement fails
  • Loading branch information
markahesketh authored Jan 29, 2018
2 parents db41d0c + ffe3368 commit 5c97a7c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/Docnet/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function delete($str_sql, $arr_params)
* @param array $arr_params
* @param String $str_result_class manually override result class (just for
* this query)
* @return array|NULL
* @return array|null
*/
public function fetchAll($str_sql, $arr_params = null, $str_result_class = null)
{
Expand All @@ -210,7 +210,7 @@ public function fetchAll($str_sql, $arr_params = null, $str_result_class = null)
* @param array $arr_params
* @param String $str_result_class manually override result class (just for
* this query)
* @return array|NULL
* @return array|object|null
*/
public function fetchOne($str_sql, $arr_params = null, $str_result_class = null)
{
Expand All @@ -224,7 +224,7 @@ public function fetchOne($str_sql, $arr_params = null, $str_result_class = null)
* @param null $arr_params
* @param String $str_result_class
* @param int $int_fetch_mode
* @return array|NULL|void
* @return array|object|null
*/
private function delegateFetch($str_sql, $arr_params, $str_result_class, $int_fetch_mode)
{
Expand Down
7 changes: 3 additions & 4 deletions src/Docnet/DB/Exception/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class Factory
const CODE_MYSQL_GONE_AWAY = 2006;

/**
* @param $strMessage
* @param null $intCode
* @throws DatabaseConnectionException
* @throws \Exception
* @param string $strMessage
* @param int|null $intCode
* @return \Exception
*/
public static function build($strMessage, $intCode = null) {

Expand Down
31 changes: 22 additions & 9 deletions src/Docnet/DB/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@ public function __construct(\mysqli $obj_db, $str_sql, $str_result_class = NULL)
/**
* Execute a query, return the first result.
*
* @param Array $arr_params
* @return Array|NULL
* @param array $arr_params
* @return array|object|null
*/
public function fetchOne($arr_params = NULL)
{
Expand All @@ -165,8 +165,8 @@ public function fetchOne($arr_params = NULL)
/**
* Execute a query, return ALL the results.
*
* @param Array $arr_params
* @return array|NULL
* @param array $arr_params
* @return array|null
*/
public function fetchAll($arr_params = NULL)
{
Expand All @@ -177,7 +177,7 @@ public function fetchAll($arr_params = NULL)
* Execute an update statement
*
* @param array $arr_params
* @return array|null|object
* @return bool
*/
public function update(array $arr_params = NULL)
{
Expand All @@ -188,7 +188,7 @@ public function update(array $arr_params = NULL)
* Execute an insert statement
*
* @param array $arr_params
* @return array|null|object
* @return bool
*/
public function insert(array $arr_params = NULL)
{
Expand All @@ -199,7 +199,7 @@ public function insert(array $arr_params = NULL)
* Execute a delete statement
*
* @param array $arr_params
* @return array|null|object
* @return bool
*/
public function delete(array $arr_params = NULL)
{
Expand All @@ -217,7 +217,8 @@ public function delete(array $arr_params = NULL)
* This method used by SELECT/UPDATE/INSERT/DELETE
*
* @param null $arr_params
* @return array|null|object
* @return bool
* @throws \Exception if fails to process the prepared statement
*/
private function process($arr_params = NULL)
{
Expand Down Expand Up @@ -254,7 +255,19 @@ private function process($arr_params = NULL)
}
$this->int_state = self::STATE_EXECUTED;
self::$int_execute++;
return $this->obj_stmt->execute();

$bol_result = $this->obj_stmt->execute();

if ($this->obj_stmt->errno != 0) {
$str_message = sprintf(
'Error processing statement - Code: %d, Message: "%s"',
$this->obj_stmt->errno,
$this->obj_stmt->error
);
throw DB\Exception\Factory::build($str_message, $this->obj_stmt->errno);
}

return $bol_result;
}

/**
Expand Down

0 comments on commit 5c97a7c

Please sign in to comment.