1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-04 21:28:02 +02:00

added NException

This commit is contained in:
David Grudl
2007-11-14 23:05:57 +00:00
parent 0eeff53fe8
commit 6bfa40f594
13 changed files with 135 additions and 41 deletions

View File

@@ -32,6 +32,7 @@ if (version_compare(PHP_VERSION , '5.1.0', '<')) {
// libraries // libraries
require_once __FILE__ . '/../libs/NObject.php'; require_once __FILE__ . '/../libs/NObject.php';
require_once __FILE__ . '/../libs/NException.php';
require_once __FILE__ . '/../libs/DibiException.php'; require_once __FILE__ . '/../libs/DibiException.php';
require_once __FILE__ . '/../libs/DibiConnection.php'; require_once __FILE__ . '/../libs/DibiConnection.php';
require_once __FILE__ . '/../libs/DibiDriverInterface.php'; require_once __FILE__ . '/../libs/DibiDriverInterface.php';

View File

@@ -27,6 +27,7 @@
* - 'password' (or 'pass') * - 'password' (or 'pass')
* - 'persistent' - try to find a persistent link? * - 'persistent' - try to find a persistent link?
* - 'database' - the database name to select * - 'database' - the database name to select
* - 'lazy' - if TRUE, connection will be established only when required
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl

View File

@@ -32,6 +32,7 @@
* - 'charset' - sets the encoding * - 'charset' - sets the encoding
* - 'unbuffered' - sends query without fetching and buffering the result rows automatically? * - 'unbuffered' - sends query without fetching and buffering the result rows automatically?
* - 'options' - driver specific constants (MYSQL_*) * - 'options' - driver specific constants (MYSQL_*)
* - 'lazy' - if TRUE, connection will be established only when required
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl
@@ -99,13 +100,13 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
$host = ':' . $config['socket']; $host = ':' . $config['socket'];
} }
DibiDatabaseException::catchError(); NException::catchError('DibiDatabaseException');
if (empty($config['persistent'])) { if (empty($config['persistent'])) {
$this->connection = @mysql_connect($host, $config['username'], $config['password'], TRUE, $config['options']); $this->connection = @mysql_connect($host, $config['username'], $config['password'], TRUE, $config['options']);
} else { } else {
$this->connection = @mysql_pconnect($host, $config['username'], $config['password'], $config['options']); $this->connection = @mysql_pconnect($host, $config['username'], $config['password'], $config['options']);
} }
DibiDatabaseException::restore(); NException::restore();
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new DibiDatabaseException(mysql_error(), mysql_errno()); throw new DibiDatabaseException(mysql_error(), mysql_errno());

View File

@@ -32,6 +32,7 @@
* - 'charset' - sets the encoding * - 'charset' - sets the encoding
* - 'unbuffered' - sends query without fetching and buffering the result rows automatically? * - 'unbuffered' - sends query without fetching and buffering the result rows automatically?
* - 'options' - driver specific constants (MYSQLI_*) * - 'options' - driver specific constants (MYSQLI_*)
* - 'lazy' - if TRUE, connection will be established only when required
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl

View File

@@ -26,6 +26,7 @@
* - 'username' (or 'user') * - 'username' (or 'user')
* - 'password' (or 'pass') * - 'password' (or 'pass')
* - 'persistent' - try to find a persistent link? * - 'persistent' - try to find a persistent link?
* - 'lazy' - if TRUE, connection will be established only when required
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl

View File

@@ -26,6 +26,7 @@
* - 'username' (or 'user') * - 'username' (or 'user')
* - 'password' (or 'pass') * - 'password' (or 'pass')
* - 'charset' - sets the encoding * - 'charset' - sets the encoding
* - 'lazy' - if TRUE, connection will be established only when required
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl

View File

@@ -26,6 +26,7 @@
* - 'username' (or 'user') * - 'username' (or 'user')
* - 'password' (or 'pass') * - 'password' (or 'pass')
* - 'options' - driver specific options array * - 'options' - driver specific options array
* - 'lazy' - if TRUE, connection will be established only when required
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl

View File

@@ -25,6 +25,7 @@
* - 'database' (or 'string') - connection string * - 'database' (or 'string') - connection string
* - 'persistent' - try to find a persistent link? * - 'persistent' - try to find a persistent link?
* - 'charset' - sets the encoding * - 'charset' - sets the encoding
* - 'lazy' - if TRUE, connection will be established only when required
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 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'])) { if (isset($config['persistent'])) {
$this->connection = @pg_connect($config['database'], PGSQL_CONNECT_FORCE_NEW); $this->connection = @pg_connect($config['database'], PGSQL_CONNECT_FORCE_NEW);
} else { } else {
$this->connection = @pg_pconnect($config['database'], PGSQL_CONNECT_FORCE_NEW); $this->connection = @pg_pconnect($config['database'], PGSQL_CONNECT_FORCE_NEW);
} }
DibiDatabaseException::restore(); NException::restore();
if (!is_resource($this->connection)) { if (!is_resource($this->connection)) {
throw new DibiDatabaseException('unknown error'); throw new DibiDatabaseException('unknown error');

View File

@@ -25,6 +25,7 @@
* - 'database' (or 'file') - the filename of the SQLite database * - 'database' (or 'file') - the filename of the SQLite database
* - 'persistent' - try to find a persistent link? * - 'persistent' - try to find a persistent link?
* - 'unbuffered' - sends query without fetching and buffering the result rows automatically? * - 'unbuffered' - sends query without fetching and buffering the result rows automatically?
* - 'lazy' - if TRUE, connection will be established only when required
* *
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl

View File

@@ -66,9 +66,10 @@ class DibiConnection extends NObject
$config['driver'] = dibi::$defaultDriver; $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)) { if (!class_exists($class)) {
include_once __FILE__ . "/../../drivers/$config[driver].php"; include_once __FILE__ . "/../../drivers/$driver.php";
if (!class_exists($class)) { if (!class_exists($class)) {
throw new DibiException("Unable to create instance of dibi driver class '$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(); if (!is_array($args)) $args = func_get_args();
$this->connect();
$trans = new DibiTranslator($this->driver); $trans = new DibiTranslator($this->driver);
if ($trans->translate($args)) { if ($trans->translate($args)) {
return $this->nativeQuery($trans->sql); return $this->nativeQuery($trans->sql);
@@ -242,8 +244,7 @@ class DibiConnection extends NObject
$time = -microtime(TRUE); $time = -microtime(TRUE);
dibi::notify($this, 'beforeQuery', $sql); dibi::notify($this, 'beforeQuery', $sql);
$res = $this->driver->query($sql); $res = $this->driver->query($sql) ? new DibiResult(clone $this->driver) : TRUE; // backward compatibility - will be changed to NULL
$res = $res ? new DibiResult(clone $this->driver) : TRUE; // backward compatibility - will be changed to NULL
$time += microtime(TRUE); $time += microtime(TRUE);
dibi::$elapsedTime = $time; dibi::$elapsedTime = $time;
@@ -328,6 +329,7 @@ class DibiConnection extends NObject
*/ */
public function escape($value) public function escape($value)
{ {
$this->connect(); // MySQL & PDO require connection
return $this->driver->format($value, dibi::FIELD_TEXT); return $this->driver->format($value, dibi::FIELD_TEXT);
} }

View File

@@ -45,9 +45,6 @@ class DibiDatabaseException extends DibiException
/** @var string */ /** @var string */
private $sql; private $sql;
/** @var callback */
private static $oldHandler;
public function __construct($message = NULL, $code = 0, $sql = NULL) 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 : ''); 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();
}
}
} }

82
dibi/libs/NException.php Normal file
View File

@@ -0,0 +1,82 @@
<?php
/**
* dibi - tiny'n'smart database abstraction layer
* ----------------------------------------------
*
* Copyright (c) 2005, 2007 David Grudl aka -dgx- (http://www.dgx.cz)
*
* This source file is subject to the "dibi license" that is bundled
* with this package in the file license.txt.
*
* For more information please see http://php7.org/dibi/
*
* @copyright Copyright (c) 2004, 2007 David Grudl
* @license http://php7.org/nette/license Nette license
* @link http://php7.org/nette/
* @package Nette
*/
/**
* Nette Exception base class
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2007 David Grudl
* @package Nette-Exception
*/
class NException extends Exception
{
/** @var callback */
private static $oldHandler;
/** @var string */
private static $handlerClass;
/**
* Enables converting all PHP errors to exceptions
*
* @param Exception class to be thrown
* @return void
*/
public static function catchError($class = __CLASS__)
{
self::$oldHandler = set_error_handler(array(__CLASS__, '_errorHandler'), E_ALL);
self::$handlerClass = $class;
}
/**
* Disables converting errors to exceptions
*
* @return void
*/
public static function restore()
{
if (self::$oldHandler !== NULL) {
set_error_handler(self::$oldHandler);
self::$oldHandler = NULL;
} else {
restore_error_handler();
}
}
/**
* Internal error handler
*/
public static function _errorHandler($code, $message, $file, $line, $context)
{
self::restore();
if (ini_get('html_errors')) {
$message = strip_tags($message);
}
throw new self::$handlerClass($message, $code);
}
}

View File

@@ -0,0 +1,34 @@
OK: SELECT * FROM [customers] WHERE [customer_id] = 1;
-- rows: 1
-- takes: 0.294 ms
-- driver: sqlite
-- 2007-11-15 00:01:13
OK: SELECT * FROM [customers] WHERE [customer_id] < 5;
-- rows: 4
-- takes: 0.366 ms
-- driver: sqlite
-- 2007-11-15 00:01:13
ERROR: [1] near "FROM": syntax error
-- SQL: SELECT FROM [customers] WHERE [customer_id] < 38
-- driver: ;
-- 2007-11-15 00:01:13
OK: SELECT * FROM [customers] WHERE [customer_id] = 1;
-- rows: 1
-- takes: 0.299 ms
-- driver: sqlite
-- 2007-11-15 00:04:41
OK: SELECT * FROM [customers] WHERE [customer_id] < 5;
-- rows: 4
-- takes: 0.274 ms
-- driver: sqlite
-- 2007-11-15 00:04:41
ERROR: [1] near "FROM": syntax error
-- SQL: SELECT FROM [customers] WHERE [customer_id] < 38
-- driver: ;
-- 2007-11-15 00:04:41