1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-16 12:06:24 +02:00

* throwing exception in DibiTranslator and DibiDriver

+ added dibi::$errorMode
This commit is contained in:
David Grudl
2006-11-22 12:55:24 +00:00
parent f447a03c96
commit 3b8766d376
17 changed files with 95 additions and 58 deletions

View File

@ -3,7 +3,7 @@ Copyright notice
dibi (C) David Grudl, 2005-2006 <dave@dgx.cz>
For more information, visit the homepage http://texy.info/dibi
For more information, visit the homepage http://dibi.texy.info/
or author's weblog: http://www.dgx.cz/trine/
This program is free software; you can redistribute it and/or modify

File diff suppressed because one or more lines are too long

View File

@ -9,16 +9,16 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
* @category Database
* @version 0.6d $Revision$ $Date$
* @version 0.6e $Revision$ $Date$
*/
define('DIBI', 'Version 0.6d $Revision$');
define('DIBI', 'Version 0.6e $Revision$');
if (version_compare(PHP_VERSION , '5.0.3', '<'))
@ -84,6 +84,15 @@ class dibi
FIELD_COUNTER = 'c'; // counter or autoincrement, is integer
/**
* Query rrror modes
*/
const
ERR_SILENT = 1,
ERR_WARNING = 2,
ERR_EXCEPTION = 3;
/**
* Connection registry storage for DibiDriver objects
* @var array
@ -109,7 +118,11 @@ class dibi
*/
static public $logFile;
static public $logMode = 'a';
static public $errorMode;
/**
* Query error mode
*/
static public $errorMode = dibi::ERR_SILENT;
/**
* Enable/disable debug mode
@ -234,21 +247,27 @@ class dibi
// and generate SQL
$trans = new DibiTranslator($conn, self::$substs);
self::$sql = $trans->translate($args);
if (is_error(self::$sql)) return self::$sql; // reraise the exception
// execute SQL
$timer = -microtime(true);
try {
$res = $conn->query(self::$sql);
$timer += microtime(true);
self::$error = FALSE;
// todo:
self::$error = is_error($res) ? $res : FALSE;
} catch (DibiException $e) {
$res = FALSE;
self::$error = $e;
if (dibi::$errorMode === self::ERR_WARNING) {
trigger_error('[dibi] ' . $e->getMessage(), E_USER_WARNING);
}
}
$timer += microtime(true);
// optional log to file
if (self::$logFile != NULL)
{
if (is_error($res))
$msg = $res->getMessage();
if (self::$error)
$msg = self::$error->getMessage();
elseif ($res instanceof DibiResult)
$msg = 'object('.get_class($res).') rows: '.$res->rowCount();
else
@ -261,6 +280,9 @@ class dibi
);
}
if (self::$error && dibi::$errorMode === self::ERR_EXCEPTION)
throw self::$error;
return $res;
}
@ -280,17 +302,19 @@ class dibi
if (!is_array($args))
$args = func_get_args();
$dump = TRUE; // !!! todo
// and generate SQL
try {
$trans = new DibiTranslator(self::getConnection(), self::$substs);
$sql = $trans->translate($args);
$dump = TRUE; // !!!
if ($dump) {
if (is_error($sql))
self::dump($sql->getSql());
else
self::dump($sql);
}
if ($dump) self::dump($sql);
return $sql;
} catch (DibiException $e) {
if ($dump) self::dump($e->getSql());
return FALSE;
}
}
@ -339,9 +363,10 @@ class dibi
* Prints out a syntax highlighted version of the SQL command
*
* @param string SQL command
* @param bool return or print?
* @return void
*/
static public function dump($sql) {
static public function dump($sql, $return=FALSE) {
static $keywords2 = 'ALL|DISTINCT|AS|ON|INTO|AND|OR|AS';
static $keywords1 = 'SELECT|UPDATE|INSERT|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN';
@ -358,8 +383,11 @@ class dibi
// syntax highlight
$sql = preg_replace_callback("#(/\*.+?\*/)|(\*\*.+?\*\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#", array('dibi', 'dumpHighlight'), $sql);
$sql = '<pre class="dibi">' . $sql . '</pre>';
echo '<pre class="dibi">', $sql, '</pre>';
// print & return
if (!$return) echo $sql;
return $sql;
}

View File

@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
@ -111,7 +111,7 @@ class DibiMySqlDriver extends DibiDriver {
return new DibiMySqlResult($res);
if ($res === FALSE)
return new DibiException("Query error", array(
throw new DibiException("Query error", array(
'message' => mysql_error($this->conn),
'code' => mysql_errno($this->conn),
'sql' => $sql,

View File

@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
@ -78,7 +78,7 @@ class DibiMySqliDriver extends DibiDriver {
return new DibiMySqliResult($res);
if ($res === FALSE)
return new DibiException("Query error", $this->errorInfo($sql));
throw new DibiException("Query error", $this->errorInfo($sql));
$this->affectedRows = mysqli_affected_rows($this->conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;

View File

@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
@ -79,7 +79,7 @@ class DibiOdbcDriver extends DibiDriver {
return new DibiOdbcResult($res);
if ($res === FALSE)
return new DibiException("Query error", $this->errorInfo($sql));
throw new DibiException("Query error", $this->errorInfo($sql));
$this->affectedRows = odbc_num_rows($this->conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;

View File

@ -7,12 +7,12 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
* @category Database
* @version $Revision: 17 $ $Date: 2006-08-25 20:10:30 +0200 (pá, 25 VIII 2006) $
* @version $Revision$ $Date$
*/
@ -84,7 +84,7 @@ class DibiPostgreDriver extends DibiDriver {
return new DibiPostgreResult($res);
if ($res === FALSE)
return new DibiException("Query error", array(
throw new DibiException("Query error", array(
'message' => pg_last_error($this->conn),
'sql' => $sql,
));

View File

@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
@ -78,7 +78,7 @@ class DibiSqliteDriver extends DibiDriver {
$res = @sqlite_query($this->conn, $sql, SQLITE_ASSOC, $errorMsg);
if ($res === FALSE)
return new DibiException("Query error", array(
throw new DibiException("Query error", array(
'message' => $errorMsg,
'sql' => $sql,
));

View File

@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
@ -51,9 +51,10 @@ abstract class DibiDriver
* DibiDriver factory: creates object and connects to a database
*
* @param array connect configuration
* @return bool|object DibiDriver object on success, FALSE or Exception on failure
* @return DibiDriver
* @throw DibiException
*/
/*abstract PHP 5.2*/ static public function connect($config) {}
/*abstract disallowed PHP 5.2*/ static public function connect($config) {}

View File

@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
@ -55,16 +55,12 @@ class DibiException extends Exception
}
} // class DibiException
/**
* Checks result state
*/
function is_error($var)
public function __toString()
{
return ($var === FALSE) || ($var instanceof Exception);
$s = parent::__toString();
if (isset($this->info['sql']))
$s .= "\nSQL: " . $this->info['sql'];
return $s;
}
} // class DibiException

View File

@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi

View File

@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license.
*
* @author David Grudl aka -dgx- <dave@dgx.cz>
* @link http://texy.info/dibi/
* @link http://dibi.texy.info/
* @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi
@ -50,6 +50,7 @@ class DibiTranslator
*
* @param array
* @return string
* @throw DibiException
*/
public function translate($args)
{
@ -109,7 +110,7 @@ class DibiTranslator
$sql = preg_replace('#\/\*.*?\*\/#s', '', $sql);
if ($this->hasError)
return new DibiException('Errors during generating SQL', array('sql' => $sql));
throw new DibiException('Errors during generating SQL', array('sql' => $sql));
return $sql;
}

View File

@ -2,6 +2,9 @@
require_once '../dibi/dibi.php';
// required since PHP 5.1.0
if (function_exists('date_default_timezone_set'))
date_default_timezone_set('Europe/Prague'); // or 'GMT'
/**
@ -54,7 +57,7 @@ class TDateTime implements IDibiVariable
// connects to mysqli
dibi::connect(array(
'driver' => 'mysqli',
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'xxx', // change to real password!

View File

@ -17,6 +17,8 @@ dibi::connect(array(
$res = dibi::query('SELECT * FROM table');
if (is_error($res))
die('SQL error');
// fetch a single value

View File

@ -28,5 +28,6 @@ $res = dibi::query('SELECT * FROM [nucleus_item] WHERE [inumber] < %i', 38);
$res = dibi::query('SELECT * FROM [*nucleus_item] WHERE [inumber] < %i', 38);
echo 'See file ', dibi::$logFile;
?>

View File

@ -7,6 +7,11 @@ pre.dibi { padding-bottom: 10px; }
require_once '../dibi/dibi.php';
// required since PHP 5.1.0
if (function_exists('date_default_timezone_set'))
date_default_timezone_set('Europe/Prague'); // or 'GMT'
// mysql
dibi::connect(array(
'driver' => 'mysql',

View File

@ -1 +1 @@
Dibi Version 0.6d
Dibi Version 0.6e