1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-16 20:16:28 +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> 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/ or author's weblog: http://www.dgx.cz/trine/
This program is free software; you can redistribute it and/or modify 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. * This source file is subject to the GNU GPL license.
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @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 * @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @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', '<')) if (version_compare(PHP_VERSION , '5.0.3', '<'))
@@ -84,6 +84,15 @@ class dibi
FIELD_COUNTER = 'c'; // counter or autoincrement, is integer 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 * Connection registry storage for DibiDriver objects
* @var array * @var array
@@ -109,7 +118,11 @@ class dibi
*/ */
static public $logFile; static public $logFile;
static public $logMode = 'a'; static public $logMode = 'a';
static public $errorMode;
/**
* Query error mode
*/
static public $errorMode = dibi::ERR_SILENT;
/** /**
* Enable/disable debug mode * Enable/disable debug mode
@@ -234,21 +247,27 @@ class dibi
// and generate SQL // and generate SQL
$trans = new DibiTranslator($conn, self::$substs); $trans = new DibiTranslator($conn, self::$substs);
self::$sql = $trans->translate($args); self::$sql = $trans->translate($args);
if (is_error(self::$sql)) return self::$sql; // reraise the exception
// execute SQL // execute SQL
$timer = -microtime(true); $timer = -microtime(true);
$res = $conn->query(self::$sql); try {
$timer += microtime(true); $res = $conn->query(self::$sql);
self::$error = FALSE;
// todo: } catch (DibiException $e) {
self::$error = is_error($res) ? $res : FALSE; $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 // optional log to file
if (self::$logFile != NULL) if (self::$logFile != NULL)
{ {
if (is_error($res)) if (self::$error)
$msg = $res->getMessage(); $msg = self::$error->getMessage();
elseif ($res instanceof DibiResult) elseif ($res instanceof DibiResult)
$msg = 'object('.get_class($res).') rows: '.$res->rowCount(); $msg = 'object('.get_class($res).') rows: '.$res->rowCount();
else else
@@ -261,6 +280,9 @@ class dibi
); );
} }
if (self::$error && dibi::$errorMode === self::ERR_EXCEPTION)
throw self::$error;
return $res; return $res;
} }
@@ -280,17 +302,19 @@ class dibi
if (!is_array($args)) if (!is_array($args))
$args = func_get_args(); $args = func_get_args();
$dump = TRUE; // !!! todo
// and generate SQL // and generate SQL
$trans = new DibiTranslator(self::getConnection(), self::$substs); try {
$sql = $trans->translate($args); $trans = new DibiTranslator(self::getConnection(), self::$substs);
$dump = TRUE; // !!! $sql = $trans->translate($args);
if ($dump) { if ($dump) self::dump($sql);
if (is_error($sql)) return $sql;
self::dump($sql->getSql());
else } catch (DibiException $e) {
self::dump($sql); if ($dump) self::dump($e->getSql());
return FALSE;
} }
return $sql;
} }
@@ -339,9 +363,10 @@ class dibi
* Prints out a syntax highlighted version of the SQL command * Prints out a syntax highlighted version of the SQL command
* *
* @param string SQL command * @param string SQL command
* @param bool return or print?
* @return void * @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 $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'; 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 // syntax highlight
$sql = preg_replace_callback("#(/\*.+?\*/)|(\*\*.+?\*\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#", array('dibi', 'dumpHighlight'), $sql); $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. * This source file is subject to the GNU GPL license.
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @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 * @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
@@ -111,7 +111,7 @@ class DibiMySqlDriver extends DibiDriver {
return new DibiMySqlResult($res); return new DibiMySqlResult($res);
if ($res === FALSE) if ($res === FALSE)
return new DibiException("Query error", array( throw new DibiException("Query error", array(
'message' => mysql_error($this->conn), 'message' => mysql_error($this->conn),
'code' => mysql_errno($this->conn), 'code' => mysql_errno($this->conn),
'sql' => $sql, 'sql' => $sql,

View File

@@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license. * This source file is subject to the GNU GPL license.
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @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 * @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
@@ -78,7 +78,7 @@ class DibiMySqliDriver extends DibiDriver {
return new DibiMySqliResult($res); return new DibiMySqliResult($res);
if ($res === FALSE) 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); $this->affectedRows = mysqli_affected_rows($this->conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE; if ($this->affectedRows < 0) $this->affectedRows = FALSE;

View File

@@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license. * This source file is subject to the GNU GPL license.
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @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 * @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
@@ -79,7 +79,7 @@ class DibiOdbcDriver extends DibiDriver {
return new DibiOdbcResult($res); return new DibiOdbcResult($res);
if ($res === FALSE) 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); $this->affectedRows = odbc_num_rows($this->conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE; if ($this->affectedRows < 0) $this->affectedRows = FALSE;

View File

@@ -7,12 +7,12 @@
* This source file is subject to the GNU GPL license. * This source file is subject to the GNU GPL license.
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @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 * @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
* @category Database * @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); return new DibiPostgreResult($res);
if ($res === FALSE) if ($res === FALSE)
return new DibiException("Query error", array( throw new DibiException("Query error", array(
'message' => pg_last_error($this->conn), 'message' => pg_last_error($this->conn),
'sql' => $sql, 'sql' => $sql,
)); ));

View File

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

View File

@@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license. * This source file is subject to the GNU GPL license.
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @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 * @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
@@ -51,9 +51,10 @@ abstract class DibiDriver
* DibiDriver factory: creates object and connects to a database * DibiDriver factory: creates object and connects to a database
* *
* @param array connect configuration * @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. * This source file is subject to the GNU GPL license.
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @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 * @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi
@@ -55,16 +55,12 @@ class DibiException extends Exception
} }
public function __toString()
{
$s = parent::__toString();
if (isset($this->info['sql']))
$s .= "\nSQL: " . $this->info['sql'];
return $s;
}
} // class DibiException } // class DibiException
/**
* Checks result state
*/
function is_error($var)
{
return ($var === FALSE) || ($var instanceof Exception);
}

View File

@@ -7,7 +7,7 @@
* This source file is subject to the GNU GPL license. * This source file is subject to the GNU GPL license.
* *
* @author David Grudl aka -dgx- <dave@dgx.cz> * @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 * @copyright Copyright (c) 2005-2006 David Grudl
* @license GNU GENERAL PUBLIC LICENSE v2 * @license GNU GENERAL PUBLIC LICENSE v2
* @package dibi * @package dibi

View File

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

View File

@@ -2,6 +2,9 @@
require_once '../dibi/dibi.php'; 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 // connects to mysqli
dibi::connect(array( dibi::connect(array(
'driver' => 'mysqli', 'driver' => 'mysql',
'host' => 'localhost', 'host' => 'localhost',
'username' => 'root', 'username' => 'root',
'password' => 'xxx', // change to real password! 'password' => 'xxx', // change to real password!

View File

@@ -17,6 +17,8 @@ dibi::connect(array(
$res = dibi::query('SELECT * FROM table'); $res = dibi::query('SELECT * FROM table');
if (is_error($res))
die('SQL error');
// fetch a single value // 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); $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'; 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 // mysql
dibi::connect(array( dibi::connect(array(
'driver' => 'mysql', 'driver' => 'mysql',

View File

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