1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-15 10:34:06 +02:00

* modified DibiException (getDbError, ...)

* fix dibi::dumpResult()
This commit is contained in:
David Grudl
2007-02-02 03:51:43 +00:00
parent a2b1036a66
commit 0c86515076
16 changed files with 218 additions and 138 deletions

View File

@@ -14,11 +14,11 @@
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
* @category Database
* @version 0.7b $Revision$ $Date$
* @version 0.7c $Revision$ $Date$
*/
define('DIBI', 'Version 0.7b $Revision$');
define('DIBI', 'Version 0.7c $Revision$');
if (version_compare(PHP_VERSION , '5.0.3', '<'))
@@ -143,7 +143,7 @@ class dibi
* @param array|string connection parameters
* @param string connection name
* @return void
* @throw DibiException
* @throw DibiException
*/
static public function connect($config, $name = '1')
{
@@ -189,7 +189,7 @@ class dibi
* Retrieve active connection
*
* @return object DibiDriver object.
* @throw DibiException
* @throw DibiException
*/
static public function getConnection()
{
@@ -206,7 +206,7 @@ class dibi
*
* @param string connection registy name
* @return void
* @throw DibiException
* @throw DibiException
*/
static public function activate($name)
{
@@ -227,7 +227,7 @@ class dibi
*
* @param array|mixed one or more arguments
* @return int|DibiResult
* @throw DibiException
* @throw DibiException
*/
static public function query($args)
{
@@ -248,7 +248,7 @@ class dibi
);
if (dibi::$throwExceptions)
throw new DibiException('SQL generate error', array('sql' => $trans->sql));
throw new DibiException('SQL generate error', NULL, $trans->sql);
else {
trigger_error("dibi: SQL generate error: $trans->sql", E_USER_WARNING);
return FALSE;
@@ -264,8 +264,9 @@ class dibi
if ($res === FALSE) { // query error
if (self::$logFile) { // log to file
$info = $conn->errorInfo();
if ($info['code']) $info['message'] = "[$info[code]] $info[message]";
self::log(
"ERROR: [$info[code]] $info[message]"
"ERROR: $info[message]"
. "\n-- SQL: " . self::$sql
. ";\n-- " . date('Y-m-d H:i:s ')
);
@@ -273,11 +274,11 @@ class dibi
if (dibi::$throwExceptions) {
$info = $conn->errorInfo();
$info['sql'] = self::$sql;
throw new DibiException('Query error', $info);
throw new DibiException('Query error', $info, self::$sql);
} else {
$info = $conn->errorInfo();
trigger_error("dibi: [$info[code]] $info[message]", E_USER_WARNING);
if ($info['code']) $info['message'] = "[$info[code]] $info[message]";
trigger_error("dibi: $info[message]", E_USER_WARNING);
return FALSE;
}
}
@@ -290,7 +291,7 @@ class dibi
"OK: " . self::$sql
. ";\n-- result: $msg"
. "\n-- takes: " . sprintf('%0.3f', $timer * 1000) . ' ms'
. ";\n-- " . date('Y-m-d H:i:s ')
. "\n-- " . date('Y-m-d H:i:s ')
);
}
@@ -389,7 +390,7 @@ class dibi
// syntax highlight
$sql = preg_replace_callback("#(/\*.+?\*/)|(\*\*.+?\*\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#", array('dibi', 'dumpHighlight'), $sql);
$sql = '<pre class="dibi">' . $sql . "</pre>\n";
$sql = '<pre class="dump">' . $sql . "</pre>\n";
// print & return
if (!$return) echo $sql;
@@ -407,12 +408,9 @@ class dibi
static public function dumpResult(DibiResult $res)
{
echo '<table class="dump"><tr>';
echo '<th>Row</th>';
$fieldCount = $res->fieldCount();
for ($i = 0; $i < $fieldCount; $i++) {
$info = $res->fieldMeta($i);
echo '<th>'.htmlSpecialChars($info['name']).'</th>';
}
echo '<th>#row</th>';
foreach ($res->getFields() as $field)
echo '<th>' . $field . '</th>';
echo '</tr>';
foreach ($res as $row => $fields) {

View File

@@ -49,9 +49,9 @@ abstract class DibiDriver
/**
* DibiDriver factory: creates object and connects to a database
*
* @param array connect configuration
* @param array connect configuration
* @return DibiDriver
* @throw DibiException
* @throw DibiException
*/
/*abstract disallowed since PHP 5.2*/ static public function connect($config) {}

View File

@@ -28,31 +28,44 @@ if (!defined('DIBI')) die();
class DibiException extends Exception
{
private
$info;
$sql,
$dbError;
public function __construct($message, $info=NULL) {
$this->info = $info;
if (isset($info['message']))
$message = "$message: $info[message]";
public function __construct($message, $dbError=NULL, $sql=NULL)
{
$this->dbError = $dbError;
$this->sql = $sql;
parent::__construct($message);
}
public function getSql()
{
return isset($this->info['sql']) ? $this->info['sql'] : NULL;
return $this->sql;
}
public function getDbError()
{
return $this->dbError;
}
public function __toString()
{
$s = parent::__toString();
if (isset($this->info['sql']))
$s .= "\nSQL: " . $this->info['sql'];
if ($this->dbError) {
$s .= "\nERROR: ";
if (isset($this->dbError['code']))
$s .= "[" . $this->dbError['code'] . "] ";
$s .= $this->dbError['message'];
}
if ($this->sql) $s .= "\nSQL: " . $this->sql;
return $s;
}

View File

@@ -200,6 +200,11 @@ abstract class DibiResult implements IteratorAggregate, Countable
return array(); // empty resultset
$assocBy = func_get_args();
// check function parameters - !!! ignore or throw error?
foreach ($assocBy as $n => $assoc) //
if (!array_key_exists($assoc, $rec)) unset($assocBy[$n]);
$arr = array();
do { // make associative arrays
@@ -234,6 +239,9 @@ abstract class DibiResult implements IteratorAggregate, Countable
if (!$rec)
return array(); // empty resultset
if (!array_key_exists($key, $rec) ||
!array_key_exists($value, $rec)) return FALSE;
$arr = array();
do {
$arr[ $rec[$key] ] = $rec[$value];

View File

@@ -52,7 +52,7 @@ class DibiTranslator
*
* @param array
* @return string
* @throw DibiException
* @throw DibiException
*/
public function translate($args)
{
@@ -115,10 +115,6 @@ class DibiTranslator
$this->sql = $sql;
return !$this->hasError;
if ($this->hasError)
throw new DibiException('Errors during generating SQL', array('sql' => $sql));
return $sql;
}