2014-04-28 21:55:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DDDBL;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* create an exception with relevant information, if a query fails
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
class QueryException extends \Exception {
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param $objPDO - the PDO object which caused the error when executed
|
|
|
|
* @param $arrQueryDefinition - the complete query definition
|
|
|
|
*
|
|
|
|
* create an error message which contains all relevant informations
|
|
|
|
* and print them as exception
|
|
|
|
*
|
|
|
|
**/
|
2014-07-24 20:53:09 +00:00
|
|
|
public function __construct(\PDOStatement $objPDO, array $arrQueryDefinition) {
|
2014-04-28 21:55:47 +00:00
|
|
|
|
|
|
|
$strMessage = self::createErrorMessage($objPDO, $arrQueryDefinition);
|
|
|
|
|
|
|
|
parent::__construct($strMessage);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param $objPDO - the PDO object related with the error
|
|
|
|
* @param $arrQueryDefinition - the complete query definition
|
|
|
|
*
|
|
|
|
* @return (string) the complete exception message
|
|
|
|
*
|
|
|
|
* build and return the exception message out of the given error info
|
|
|
|
* and query definition
|
|
|
|
*
|
|
|
|
**/
|
2014-07-24 20:53:09 +00:00
|
|
|
private function createErrorMessage(\PDOStatement $objPDO, array $arrQueryDefinition) {
|
2014-04-28 21:55:47 +00:00
|
|
|
|
|
|
|
$strMessage = self::flattenQueryErrorInfo($objPDO);
|
|
|
|
$strMessage .= self::flattenQueryDefiniton($arrQueryDefinition);
|
|
|
|
|
|
|
|
return $strMessage;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param $objPDO - PDO object to get error information from
|
|
|
|
*
|
|
|
|
* @return (string) a flatten error info from the query object
|
|
|
|
*
|
|
|
|
* build and return a flatten error-info
|
|
|
|
* from the driver specific error message
|
|
|
|
*
|
|
|
|
**/
|
2014-07-24 20:53:09 +00:00
|
|
|
private function flattenQueryErrorInfo(\PDOStatement $objPDO) {
|
2014-04-28 21:55:47 +00:00
|
|
|
|
|
|
|
$arrErrorInfo = $objPDO->errorInfo();
|
|
|
|
|
|
|
|
$strMessage = '';
|
|
|
|
|
|
|
|
if(!empty($arrErrorInfo) && !empty($arrErrorInfo[0]) && '00000' !== $arrErrorInfo[0])
|
|
|
|
$strMessage = "\nError-Code: {$arrErrorInfo[0]}\nError-Message: {$arrErrorInfo[2]}\n";
|
|
|
|
|
|
|
|
return $strMessage;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param $arrQueryDefinition - the complete query definition
|
|
|
|
*
|
|
|
|
* @return (string) a text version of the query definition
|
|
|
|
*
|
2014-07-24 20:53:09 +00:00
|
|
|
* create an text, which contains all *scalar* information
|
|
|
|
* of the query definition. if there are non-scalar information
|
|
|
|
* added, the will be excluded from output
|
2014-04-28 21:55:47 +00:00
|
|
|
*
|
|
|
|
**/
|
2014-07-24 20:53:09 +00:00
|
|
|
private function flattenQueryDefiniton(array $arrQueryDefinition) {
|
2014-04-28 21:55:47 +00:00
|
|
|
|
|
|
|
$strMessage = "\nQuery-Definiton:\n";
|
|
|
|
|
|
|
|
foreach($arrQueryDefinition AS $strKeyword => $strContent)
|
2014-07-24 20:53:09 +00:00
|
|
|
if(is_scalar($strContent))
|
|
|
|
$strMessage .= "$strKeyword: $strContent\n";
|
2014-04-28 21:55:47 +00:00
|
|
|
|
|
|
|
return $strMessage . "\n";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|