From 6bfa40f594006083055eb75d4b8b0f53b586672b Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 14 Nov 2007 23:05:57 +0000 Subject: [PATCH] added NException --- dibi/dibi.php | 1 + dibi/drivers/mssql.php | 1 + dibi/drivers/mysql.php | 5 ++- dibi/drivers/mysqli.php | 1 + dibi/drivers/odbc.php | 1 + dibi/drivers/oracle.php | 1 + dibi/drivers/pdo.php | 1 + dibi/drivers/postgre.php | 5 ++- dibi/drivers/sqlite.php | 1 + dibi/libs/DibiConnection.php | 10 +++-- dibi/libs/DibiException.php | 33 --------------- dibi/libs/NException.php | 82 ++++++++++++++++++++++++++++++++++++ examples/log.sql | 34 +++++++++++++++ 13 files changed, 135 insertions(+), 41 deletions(-) create mode 100644 dibi/libs/NException.php diff --git a/dibi/dibi.php b/dibi/dibi.php index 5a61df90..99ac9216 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -32,6 +32,7 @@ if (version_compare(PHP_VERSION , '5.1.0', '<')) { // libraries require_once __FILE__ . '/../libs/NObject.php'; +require_once __FILE__ . '/../libs/NException.php'; require_once __FILE__ . '/../libs/DibiException.php'; require_once __FILE__ . '/../libs/DibiConnection.php'; require_once __FILE__ . '/../libs/DibiDriverInterface.php'; diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index 32dd9eee..fb99d564 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -27,6 +27,7 @@ * - 'password' (or 'pass') * - 'persistent' - try to find a persistent link? * - 'database' - the database name to select + * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index a88b2959..4413a1bd 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -32,6 +32,7 @@ * - 'charset' - sets the encoding * - 'unbuffered' - sends query without fetching and buffering the result rows automatically? * - 'options' - driver specific constants (MYSQL_*) + * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl @@ -99,13 +100,13 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface $host = ':' . $config['socket']; } - DibiDatabaseException::catchError(); + NException::catchError('DibiDatabaseException'); if (empty($config['persistent'])) { $this->connection = @mysql_connect($host, $config['username'], $config['password'], TRUE, $config['options']); } else { $this->connection = @mysql_pconnect($host, $config['username'], $config['password'], $config['options']); } - DibiDatabaseException::restore(); + NException::restore(); if (!is_resource($this->connection)) { throw new DibiDatabaseException(mysql_error(), mysql_errno()); diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 4b294984..6b056f5e 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -32,6 +32,7 @@ * - 'charset' - sets the encoding * - 'unbuffered' - sends query without fetching and buffering the result rows automatically? * - 'options' - driver specific constants (MYSQLI_*) + * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 2ae30032..d0d6bc3e 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -26,6 +26,7 @@ * - 'username' (or 'user') * - 'password' (or 'pass') * - 'persistent' - try to find a persistent link? + * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index 4c10bec1..160f1f08 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -26,6 +26,7 @@ * - 'username' (or 'user') * - 'password' (or 'pass') * - 'charset' - sets the encoding + * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 0b288b1b..96cdd10b 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -26,6 +26,7 @@ * - 'username' (or 'user') * - 'password' (or 'pass') * - 'options' - driver specific options array + * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 94f15844..a5c8bb53 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -25,6 +25,7 @@ * - 'database' (or 'string') - connection string * - 'persistent' - try to find a persistent link? * - 'charset' - sets the encoding + * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl @@ -65,13 +66,13 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface } - DibiDatabaseException::catchError(); + NException::catchError('DibiDatabaseException'); if (isset($config['persistent'])) { $this->connection = @pg_connect($config['database'], PGSQL_CONNECT_FORCE_NEW); } else { $this->connection = @pg_pconnect($config['database'], PGSQL_CONNECT_FORCE_NEW); } - DibiDatabaseException::restore(); + NException::restore(); if (!is_resource($this->connection)) { throw new DibiDatabaseException('unknown error'); diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 630eb9cf..74eb323b 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -25,6 +25,7 @@ * - 'database' (or 'file') - the filename of the SQLite database * - 'persistent' - try to find a persistent link? * - 'unbuffered' - sends query without fetching and buffering the result rows automatically? + * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index bc7587f3..fd391280 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -66,9 +66,10 @@ class DibiConnection extends NObject $config['driver'] = dibi::$defaultDriver; } - $class = "Dibi$config[driver]Driver"; + $driver = preg_replace('#[^a-z0-9_]#', '_', $config['driver']); + $class = "Dibi" . $driver . "Driver"; if (!class_exists($class)) { - include_once __FILE__ . "/../../drivers/$config[driver].php"; + include_once __FILE__ . "/../../drivers/$driver.php"; if (!class_exists($class)) { throw new DibiException("Unable to create instance of dibi driver class '$class'."); @@ -197,6 +198,7 @@ class DibiConnection extends NObject { if (!is_array($args)) $args = func_get_args(); + $this->connect(); $trans = new DibiTranslator($this->driver); if ($trans->translate($args)) { return $this->nativeQuery($trans->sql); @@ -242,8 +244,7 @@ class DibiConnection extends NObject $time = -microtime(TRUE); dibi::notify($this, 'beforeQuery', $sql); - $res = $this->driver->query($sql); - $res = $res ? new DibiResult(clone $this->driver) : TRUE; // backward compatibility - will be changed to NULL + $res = $this->driver->query($sql) ? new DibiResult(clone $this->driver) : TRUE; // backward compatibility - will be changed to NULL $time += microtime(TRUE); dibi::$elapsedTime = $time; @@ -328,6 +329,7 @@ class DibiConnection extends NObject */ public function escape($value) { + $this->connect(); // MySQL & PDO require connection return $this->driver->format($value, dibi::FIELD_TEXT); } diff --git a/dibi/libs/DibiException.php b/dibi/libs/DibiException.php index 2a119b99..f8651112 100644 --- a/dibi/libs/DibiException.php +++ b/dibi/libs/DibiException.php @@ -45,9 +45,6 @@ class DibiDatabaseException extends DibiException /** @var string */ private $sql; - /** @var callback */ - private static $oldHandler; - public function __construct($message = NULL, $code = 0, $sql = NULL) { @@ -70,34 +67,4 @@ class DibiDatabaseException extends DibiException return parent::__toString() . ($this->sql ? "\nSQL: " . $this->sql : ''); } - - - public static function _catchErrorHandler($errno, $errstr, $errfile, $errline, $errcontext) - { - self::restore(); - if (ini_get('html_errors')) { - $errstr = strip_tags($errstr); - } - throw new self($errstr, $errno); - } - - - - public static function catchError() - { - self::$oldHandler = set_error_handler(array(__CLASS__, '_catchErrorHandler'), E_ALL); - } - - - - public static function restore() - { - if (self::$oldHandler) { - set_error_handler(self::$oldHandler); - self::$oldHandler = NULL; - } else { - restore_error_handler(); - } - } - } \ No newline at end of file diff --git a/dibi/libs/NException.php b/dibi/libs/NException.php new file mode 100644 index 00000000..48a94882 --- /dev/null +++ b/dibi/libs/NException.php @@ -0,0 +1,82 @@ +