mirror of
https://github.com/dg/dibi.git
synced 2025-08-14 01:54:08 +02:00
added NException
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
82
dibi/libs/NException.php
Normal file
82
dibi/libs/NException.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user