2006-06-04 23:06:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dibi - Database Abstraction Layer according to dgx
|
|
|
|
* --------------------------------------------------
|
|
|
|
*
|
2007-06-19 21:12:28 +00:00
|
|
|
* Copyright (c) 2005-2007 David Grudl aka -dgx- (http://www.dgx.cz)
|
2006-06-07 11:48:20 +00:00
|
|
|
*
|
2007-04-11 18:30:30 +00:00
|
|
|
* for PHP 5.0.3 and newer
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
2007-06-24 23:49:57 +00:00
|
|
|
* This source file is subject to the new BSD license that is bundled
|
2007-06-19 21:12:28 +00:00
|
|
|
* with this package in the file license.txt.
|
|
|
|
*
|
2007-06-24 23:49:57 +00:00
|
|
|
* @author David Grudl
|
2007-06-25 17:02:12 +00:00
|
|
|
* @copyright Copyright (c) 2005-2007 David Grudl aka -dgx- (http://www.dgx.cz)
|
2007-06-24 23:49:57 +00:00
|
|
|
* @license New BSD License
|
2007-09-29 07:53:25 +00:00
|
|
|
* @version 0.9a (Revision: $WCREV$, Date: $WCDATE$)
|
2007-06-24 23:49:57 +00:00
|
|
|
* @category Database
|
|
|
|
* @package Dibi
|
2007-10-13 22:50:29 +00:00
|
|
|
* @link http://php7.org/dibi/
|
2007-04-11 18:30:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-10-13 22:50:29 +00:00
|
|
|
* This file is part of the "dibi" project (http://php7.org/dibi/)
|
2007-04-11 18:30:30 +00:00
|
|
|
*
|
2007-06-24 23:49:57 +00:00
|
|
|
* @version $Revision$ $Date$
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
if (version_compare(PHP_VERSION , '5.0.3', '<')) {
|
2006-06-04 23:06:33 +00:00
|
|
|
die('dibi needs PHP 5.0.3 or newer');
|
2007-09-29 07:53:25 +00:00
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
// libraries
|
|
|
|
require_once dirname(__FILE__).'/libs/driver.php';
|
|
|
|
require_once dirname(__FILE__).'/libs/resultset.php';
|
2006-10-26 13:09:56 +00:00
|
|
|
require_once dirname(__FILE__).'/libs/translator.php';
|
2006-06-04 23:06:33 +00:00
|
|
|
require_once dirname(__FILE__).'/libs/exception.php';
|
2007-09-29 07:53:25 +00:00
|
|
|
require_once dirname(__FILE__).'/libs/logger.php';
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for user variable, used for generating SQL
|
|
|
|
*/
|
2007-08-20 22:17:52 +00:00
|
|
|
interface DibiVariableInterface
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Format for SQL
|
|
|
|
*
|
|
|
|
* @param object destination DibiDriver
|
|
|
|
* @param string optional modifier
|
|
|
|
* @return string SQL code
|
|
|
|
*/
|
|
|
|
public function toSQL($driver, $modifier = NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for database drivers
|
|
|
|
*
|
|
|
|
* This class is static container class for creating DB objects and
|
2007-01-29 05:08:52 +00:00
|
|
|
* store connections info.
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
class dibi
|
|
|
|
{
|
2006-06-08 01:35:44 +00:00
|
|
|
/**
|
|
|
|
* Column type in relation to PHP native type
|
|
|
|
*/
|
|
|
|
const
|
|
|
|
FIELD_TEXT = 's', // as 'string'
|
2006-06-08 02:02:05 +00:00
|
|
|
FIELD_BINARY = 'S',
|
|
|
|
FIELD_BOOL = 'b',
|
2006-06-08 01:35:44 +00:00
|
|
|
FIELD_INTEGER = 'i',
|
|
|
|
FIELD_FLOAT = 'f',
|
|
|
|
FIELD_DATE = 'd',
|
|
|
|
FIELD_DATETIME = 't',
|
|
|
|
FIELD_UNKNOWN = '?',
|
|
|
|
|
|
|
|
// special
|
2007-05-13 18:32:03 +00:00
|
|
|
FIELD_COUNTER = 'c', // counter or autoincrement, is integer
|
|
|
|
|
|
|
|
// dibi version
|
2007-09-29 07:53:25 +00:00
|
|
|
VERSION = '0.9a (Revision: $WCREV$, Date: $WCDATE$)';
|
2006-06-08 01:35:44 +00:00
|
|
|
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
/**
|
|
|
|
* Connection registry storage for DibiDriver objects
|
2007-01-29 05:08:52 +00:00
|
|
|
* @var DibiDriver[]
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
private static $registry = array();
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current connection
|
2007-01-29 05:08:52 +00:00
|
|
|
* @var DibiDriver
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
private static $connection;
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Substitutions for identifiers
|
|
|
|
* @var array
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
private static $substs = array();
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* @see addHandler
|
|
|
|
* @var array
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
private static $handlers = array();
|
2006-11-22 12:55:24 +00:00
|
|
|
|
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Last SQL command @see dibi::query()
|
2007-01-29 05:08:52 +00:00
|
|
|
* @var string
|
2006-11-22 12:55:24 +00:00
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
public static $sql;
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-01-29 05:08:52 +00:00
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Elapsed time for last query
|
|
|
|
* @var int
|
2007-01-29 05:08:52 +00:00
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
public static $elapsedTime;
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2006-09-23 06:34:44 +00:00
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Elapsed time for all queries
|
|
|
|
* @var int
|
2006-09-23 06:34:44 +00:00
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
public static $totalTime;
|
2006-09-23 06:34:44 +00:00
|
|
|
|
2007-08-29 08:17:45 +00:00
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Number or queries
|
|
|
|
* @var int
|
2007-08-29 08:17:45 +00:00
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
public static $numOfQueries = 0;
|
2007-08-29 08:17:45 +00:00
|
|
|
|
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Start time
|
2007-08-29 08:17:45 +00:00
|
|
|
* @var int
|
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
private static $time;
|
|
|
|
|
|
|
|
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
2006-10-26 13:09:56 +00:00
|
|
|
/**
|
|
|
|
* Monostate class
|
|
|
|
*/
|
2007-08-27 22:38:14 +00:00
|
|
|
final private function __construct()
|
2006-10-26 13:09:56 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
/**
|
|
|
|
* Creates a new DibiDriver object and connects it to specified database
|
|
|
|
*
|
2006-07-19 01:40:29 +00:00
|
|
|
* @param array|string connection parameters
|
2006-06-04 23:06:33 +00:00
|
|
|
* @param string connection name
|
2007-05-30 00:01:10 +00:00
|
|
|
* @return DibiDriver
|
2007-06-24 23:49:57 +00:00
|
|
|
* @throws DibiException
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-09-27 07:56:43 +00:00
|
|
|
public static function connect($config = 'driver=mysql', $name = 0)
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2006-07-19 01:40:29 +00:00
|
|
|
// DSN string
|
2007-08-23 17:12:58 +00:00
|
|
|
if (is_string($config)) {
|
2006-07-19 01:40:29 +00:00
|
|
|
parse_str($config, $config);
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-08-23 17:12:58 +00:00
|
|
|
if (empty($config['driver'])) {
|
2006-11-13 06:32:16 +00:00
|
|
|
throw new DibiException('Driver is not specified.');
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-08-27 22:38:14 +00:00
|
|
|
$class = "Dibi$config[driver]Driver";
|
|
|
|
if (!class_exists($class)) {
|
2006-09-13 12:02:38 +00:00
|
|
|
include_once dirname(__FILE__) . "/drivers/$config[driver].php";
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-08-27 22:38:14 +00:00
|
|
|
if (!class_exists($class)) {
|
|
|
|
throw new DibiException("Unable to create instance of dibi driver class '$class'.");
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2006-09-13 12:02:38 +00:00
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
// create connection object and store in list; like $connection = $class::connect($config);
|
|
|
|
return self::$connection = self::$registry[$name] = new $class($config);
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE when connection was established
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
public static function isConnected()
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-06-25 17:02:12 +00:00
|
|
|
return (bool) self::$connection;
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
/**
|
|
|
|
* Retrieve active connection
|
|
|
|
*
|
2007-04-06 07:34:48 +00:00
|
|
|
* @param string connection registy name
|
2006-06-04 23:06:33 +00:00
|
|
|
* @return object DibiDriver object.
|
2007-06-24 23:49:57 +00:00
|
|
|
* @throws DibiException
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-08-09 03:31:34 +00:00
|
|
|
public static function getConnection($name = NULL)
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-04-06 07:34:48 +00:00
|
|
|
if ($name === NULL) {
|
2007-08-23 17:12:58 +00:00
|
|
|
if (!self::$connection) {
|
2007-04-06 07:34:48 +00:00
|
|
|
throw new DibiException('Dibi is not connected to database');
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2007-04-06 07:34:48 +00:00
|
|
|
|
2007-06-25 17:02:12 +00:00
|
|
|
return self::$connection;
|
2007-04-06 07:34:48 +00:00
|
|
|
}
|
2006-11-13 06:32:16 +00:00
|
|
|
|
2007-08-23 17:12:58 +00:00
|
|
|
if (!isset(self::$registry[$name])) {
|
2007-04-06 07:34:48 +00:00
|
|
|
throw new DibiException("There is no connection named '$name'.");
|
2007-08-23 17:12:58 +00:00
|
|
|
}
|
2007-04-06 07:34:48 +00:00
|
|
|
|
|
|
|
return self::$registry[$name];
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-23 06:34:44 +00:00
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
/**
|
|
|
|
* Change active connection
|
|
|
|
*
|
|
|
|
* @param string connection registy name
|
|
|
|
* @return void
|
2007-06-24 23:49:57 +00:00
|
|
|
* @throws DibiException
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
public static function activate($name)
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-06-25 17:02:12 +00:00
|
|
|
self::$connection = self::getConnection($name);
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-04-25 06:44:38 +00:00
|
|
|
* Generates and executes SQL query - Monostate for DibiDriver::query()
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
2006-07-19 01:40:29 +00:00
|
|
|
* @param array|mixed one or more arguments
|
2007-10-01 05:34:50 +00:00
|
|
|
* @return DibiResult|TRUE
|
2007-06-24 23:49:57 +00:00
|
|
|
* @throws DibiException
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
public static function query($args)
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-08-23 17:12:58 +00:00
|
|
|
if (!is_array($args)) $args = func_get_args();
|
2007-05-11 22:25:32 +00:00
|
|
|
|
2007-03-27 23:12:36 +00:00
|
|
|
return self::getConnection()->query($args);
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Executes the SQL query - Monostate for DibiDriver::nativeQuery()
|
|
|
|
*
|
|
|
|
* @param string SQL statement.
|
2007-10-01 05:34:50 +00:00
|
|
|
* @return DibiResult|TRUE
|
2007-09-29 07:53:25 +00:00
|
|
|
*/
|
|
|
|
public static function nativeQuery($sql)
|
|
|
|
{
|
|
|
|
return self::getConnection()->nativeQuery($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates and prints SQL query - Monostate for DibiDriver::test()
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
2006-07-19 01:40:29 +00:00
|
|
|
* @param array|mixed one or more arguments
|
2007-01-29 05:08:52 +00:00
|
|
|
* @return bool
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
public static function test($args)
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-08-23 17:12:58 +00:00
|
|
|
if (!is_array($args)) $args = func_get_args();
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
return self::getConnection()->test($args);
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-04-25 06:44:38 +00:00
|
|
|
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
|
2006-06-04 23:06:33 +00:00
|
|
|
* Monostate for DibiDriver::insertId()
|
|
|
|
*
|
2007-08-28 23:17:34 +00:00
|
|
|
* @param string optional sequence name for DibiPostgreDriver
|
|
|
|
* @return int|FALSE int on success or FALSE on failure
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-08-28 23:17:34 +00:00
|
|
|
public static function insertId($sequence=NULL)
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-08-28 23:17:34 +00:00
|
|
|
return self::getConnection()->insertId($sequence);
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-04-25 06:44:38 +00:00
|
|
|
* Gets the number of affected rows
|
2006-06-04 23:06:33 +00:00
|
|
|
* Monostate for DibiDriver::affectedRows()
|
|
|
|
*
|
2007-04-25 06:44:38 +00:00
|
|
|
* @return int number of rows or FALSE on error
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
public static function affectedRows()
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2006-11-13 06:32:16 +00:00
|
|
|
return self::getConnection()->affectedRows();
|
2006-08-25 15:17:40 +00:00
|
|
|
}
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
2007-08-09 03:31:34 +00:00
|
|
|
/**
|
|
|
|
* Begins a transaction - Monostate for DibiDriver::begin()
|
|
|
|
*/
|
|
|
|
public static function begin()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
self::getConnection()->begin();
|
2007-08-09 03:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits statements in a transaction - Monostate for DibiDriver::commit()
|
|
|
|
*/
|
|
|
|
public static function commit()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
self::getConnection()->commit();
|
2007-08-09 03:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollback changes in a transaction - Monostate for DibiDriver::rollback()
|
|
|
|
*/
|
|
|
|
public static function rollback()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
self::getConnection()->rollback();
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-23 06:34:44 +00:00
|
|
|
/**
|
|
|
|
* Create a new substitution pair for indentifiers
|
2007-08-28 23:17:34 +00:00
|
|
|
*
|
2006-09-23 06:34:44 +00:00
|
|
|
* @param string from
|
|
|
|
* @param string to
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
public static function addSubst($expr, $subst)
|
2006-09-23 06:34:44 +00:00
|
|
|
{
|
|
|
|
self::$substs[':'.$expr.':'] = $subst;
|
|
|
|
}
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-09-23 06:34:44 +00:00
|
|
|
/**
|
|
|
|
* Remove substitution pair
|
2007-08-28 23:17:34 +00:00
|
|
|
*
|
2007-09-27 07:56:43 +00:00
|
|
|
* @param mixed from or TRUE
|
2006-09-23 06:34:44 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2007-06-25 23:47:05 +00:00
|
|
|
public static function removeSubst($expr)
|
2006-09-23 06:34:44 +00:00
|
|
|
{
|
2007-09-27 07:56:43 +00:00
|
|
|
if ($expr === TRUE) {
|
|
|
|
self::$substs = array();
|
|
|
|
} else {
|
|
|
|
unset(self::$substs[':'.$expr.':']);
|
|
|
|
}
|
2006-09-23 06:34:44 +00:00
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2007-03-27 23:12:36 +00:00
|
|
|
/**
|
2007-09-27 07:56:43 +00:00
|
|
|
* Returns substitution pairs
|
2007-08-28 23:17:34 +00:00
|
|
|
*
|
2007-09-27 07:56:43 +00:00
|
|
|
* @return array
|
2007-03-27 23:12:36 +00:00
|
|
|
*/
|
2007-09-27 07:56:43 +00:00
|
|
|
public static function getSubst()
|
2007-03-27 23:12:36 +00:00
|
|
|
{
|
2007-09-27 07:56:43 +00:00
|
|
|
return self::$substs;
|
2007-03-27 23:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2006-11-13 06:32:16 +00:00
|
|
|
/**
|
2007-08-29 08:17:45 +00:00
|
|
|
* Add new event handler
|
|
|
|
*
|
|
|
|
* @param callback
|
|
|
|
* @return void
|
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
public static function addHandler($callback)
|
2007-08-29 08:17:45 +00:00
|
|
|
{
|
|
|
|
if (!is_callable($callback)) {
|
|
|
|
throw new DibiException("Invalid callback");
|
|
|
|
}
|
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
self::$handlers[] = $callback;
|
2007-08-29 08:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Event notification (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback)
|
2007-08-29 08:17:45 +00:00
|
|
|
*
|
2007-09-29 07:53:25 +00:00
|
|
|
* @param string event name
|
|
|
|
* @param DibiDriver
|
|
|
|
* @param mixed
|
2007-08-29 08:17:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
public static function notify($event, DibiDriver $driver = NULL, $arg = NULL)
|
2007-08-29 08:17:45 +00:00
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
if ($event === 'beforeQuery') {
|
|
|
|
self::$numOfQueries++;
|
|
|
|
self::$elapsedTime = FALSE;
|
|
|
|
self::$time = -microtime(TRUE);
|
|
|
|
self::$sql = $arg;
|
|
|
|
|
|
|
|
} elseif ($event === 'afterQuery') {
|
|
|
|
self::$elapsedTime = self::$time + microtime(TRUE);
|
|
|
|
self::$totalTime += self::$elapsedTime;
|
|
|
|
}
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
foreach (self::$handlers as $handler) {
|
|
|
|
call_user_func($handler, $event, $driver, $arg);
|
2007-08-29 08:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
/**
|
|
|
|
* Enable profiler & logger
|
|
|
|
*
|
|
|
|
* @param string filename
|
|
|
|
* @param bool log all queries?
|
|
|
|
* @return DibiProfiler
|
|
|
|
*/
|
|
|
|
public static function startLogger($file, $logQueries = FALSE)
|
2007-08-29 08:17:45 +00:00
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
$logger = new DibiLogger($file);
|
|
|
|
$logger->logQueries = $logQueries;
|
|
|
|
self::addHandler(array($logger, 'handler'));
|
|
|
|
return $logger;
|
2007-08-29 08:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-09-29 07:53:25 +00:00
|
|
|
* Prints out a syntax highlighted version of the SQL command or DibiResult
|
|
|
|
*
|
|
|
|
* @param string|DibiResult
|
|
|
|
* @param bool return or print?
|
|
|
|
* @return string
|
2007-08-29 08:17:45 +00:00
|
|
|
*/
|
2007-09-29 07:53:25 +00:00
|
|
|
public static function dump($sql = NULL, $return = FALSE)
|
2007-08-29 08:17:45 +00:00
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
ob_start();
|
|
|
|
if ($sql instanceof DibiResult) {
|
|
|
|
$sql->dump();
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
} else {
|
|
|
|
if ($sql === NULL) $sql = self::$sql;
|
|
|
|
|
|
|
|
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';
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
// insert new lines
|
|
|
|
$sql = preg_replace("#\\b(?:$keywords1)\\b#", "\n\$0", $sql);
|
|
|
|
|
|
|
|
$sql = trim($sql);
|
|
|
|
// reduce spaces
|
|
|
|
$sql = preg_replace('# {2,}#', ' ', $sql);
|
|
|
|
|
|
|
|
$sql = wordwrap($sql, 100);
|
|
|
|
$sql = htmlSpecialChars($sql);
|
|
|
|
$sql = preg_replace("#\n{2,}#", "\n", $sql);
|
|
|
|
|
|
|
|
// syntax highlight
|
|
|
|
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|\\b($keywords1)\\b|\\b($keywords2)\\b#", array('dibi', 'highlightCallback'), $sql);
|
|
|
|
echo '<pre class="dump">', $sql, "</pre>\n";
|
2007-08-29 08:17:45 +00:00
|
|
|
}
|
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
if ($return) {
|
|
|
|
return ob_get_clean();
|
|
|
|
} else {
|
|
|
|
ob_end_flush();
|
|
|
|
}
|
2007-08-29 08:17:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
private static function highlightCallback($matches)
|
2006-11-13 06:32:16 +00:00
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
if (!empty($matches[1])) // comment
|
|
|
|
return '<em style="color:gray">'.$matches[1].'</em>';
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
if (!empty($matches[2])) // error
|
|
|
|
return '<strong style="color:red">'.$matches[2].'</strong>';
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
if (!empty($matches[3])) // most important keywords
|
|
|
|
return '<strong style="color:blue">'.$matches[3].'</strong>';
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
if (!empty($matches[4])) // other keywords
|
|
|
|
return '<strong style="color:green">'.$matches[4].'</strong>';
|
|
|
|
}
|
2007-08-29 08:17:45 +00:00
|
|
|
|
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
} // class dibi
|