2006-06-04 23:06:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2007-10-30 00:58:15 +00:00
|
|
|
* dibi - tiny'n'smart database abstraction layer
|
|
|
|
* ----------------------------------------------
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
2008-01-02 05:25:21 +00:00
|
|
|
* Copyright (c) 2005, 2008 David Grudl aka -dgx- (http://www.dgx.cz)
|
2006-06-07 11:48:20 +00:00
|
|
|
*
|
2007-10-30 00:58:15 +00:00
|
|
|
* This source file is subject to the "dibi license" that is bundled
|
2007-06-19 21:12:28 +00:00
|
|
|
* with this package in the file license.txt.
|
|
|
|
*
|
2007-12-05 09:27:55 +00:00
|
|
|
* For more information please see http://dibiphp.com/
|
2007-10-30 00:58:15 +00:00
|
|
|
*
|
2007-06-24 23:49:57 +00:00
|
|
|
* @author David Grudl
|
2008-01-02 05:25:21 +00:00
|
|
|
* @copyright Copyright (c) 2005, 2008 David Grudl
|
2007-12-05 09:27:55 +00:00
|
|
|
* @license http://dibiphp.com/license dibi license
|
2007-11-10 07:37:44 +00:00
|
|
|
* @version 0.9 (Revision: $WCREV$, Date: $WCDATE$)
|
2007-12-05 09:27:55 +00:00
|
|
|
* @link http://dibiphp.com/
|
2007-11-12 00:08:29 +00:00
|
|
|
* @package dibi
|
2007-04-11 18:30:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2007-11-01 05:52:13 +00:00
|
|
|
/**
|
|
|
|
* Check PHP configuration
|
|
|
|
*/
|
2007-11-08 03:32:37 +00:00
|
|
|
if (version_compare(PHP_VERSION , '5.1.0', '<')) {
|
|
|
|
throw new Exception('dibi needs PHP 5.1.0 or newer');
|
2007-09-29 07:53:25 +00:00
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-11-01 05:52:13 +00:00
|
|
|
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-11-15 00:19:41 +00:00
|
|
|
// nette libraries
|
2008-01-02 05:25:21 +00:00
|
|
|
if (!class_exists('NObject', FALSE)) { require_once dirname(__FILE__) . '/libs/NObject.php'; }
|
|
|
|
if (!class_exists('NException', FALSE)) { require_once dirname(__FILE__) . '/libs/NException.php'; }
|
2007-11-15 00:19:41 +00:00
|
|
|
|
|
|
|
// dibi libraries
|
2008-01-02 05:25:21 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiException.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiConnection.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiDriverInterface.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiResult.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiResultIterator.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiLogger.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiVariable.php';
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*
|
2007-11-12 00:08:29 +00:00
|
|
|
* @author David Grudl
|
2008-01-02 05:25:21 +00:00
|
|
|
* @copyright Copyright (c) 2005, 2008 David Grudl
|
2007-11-12 00:08:29 +00:00
|
|
|
* @package dibi
|
|
|
|
* @version $Revision$ $Date$
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-12-11 07:28:55 +00:00
|
|
|
class dibi
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
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-11-12 06:41:59 +00:00
|
|
|
FIELD_COUNTER = 'C', // counter or autoincrement, is integer
|
|
|
|
IDENTIFIER = 'I',
|
2007-05-13 18:32:03 +00:00
|
|
|
|
|
|
|
// dibi version
|
2007-11-10 07:37:44 +00:00
|
|
|
VERSION = '0.9 (Revision: $WCREV$, Date: $WCDATE$)';
|
2006-06-08 01:35:44 +00:00
|
|
|
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
/**
|
2007-11-12 06:41:59 +00:00
|
|
|
* Connection registry storage for DibiConnection objects
|
|
|
|
* @var DibiConnection[]
|
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-11-12 06:41:59 +00:00
|
|
|
* @var DibiConnection
|
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-10-26 17:44:24 +00:00
|
|
|
/**
|
|
|
|
* Default dibi driver
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $defaultDriver = 'mysql';
|
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
|
|
|
|
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
2007-12-11 07:28:55 +00:00
|
|
|
/**
|
2008-01-02 05:25:21 +00:00
|
|
|
* Static class - cannot be instantiated
|
2007-12-11 07:28:55 +00:00
|
|
|
*/
|
|
|
|
final public function __construct()
|
|
|
|
{
|
|
|
|
throw new LogicException("Cannot instantiate static class " . get_class($this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
/**
|
2007-11-12 06:41:59 +00:00
|
|
|
* Creates a new DibiConnection object and connects it to specified database
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
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-11-12 06:41:59 +00:00
|
|
|
* @return DibiConnection
|
2007-06-24 23:49:57 +00:00
|
|
|
* @throws DibiException
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-10-26 17:44:24 +00:00
|
|
|
public static function connect($config = array(), $name = 0)
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-11-13 01:51:44 +00:00
|
|
|
if (is_array($config)) {
|
|
|
|
$config['name'] = $name;
|
|
|
|
} else {
|
|
|
|
$config .= '&name=' . urlencode($name);
|
|
|
|
}
|
2007-11-12 06:41:59 +00:00
|
|
|
return self::$connection = self::$registry[$name] = new DibiConnection($config);
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-10 07:37:44 +00:00
|
|
|
/**
|
2007-11-12 06:41:59 +00:00
|
|
|
* Disconnects from database (doesn't destroy DibiConnection object)
|
2007-11-10 07:37:44 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function disconnect()
|
|
|
|
{
|
|
|
|
self::getConnection()->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2007-11-12 06:41:59 +00:00
|
|
|
* @return object DibiConnection 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-11-12 06:41:59 +00:00
|
|
|
* Generates and executes SQL query - Monostate for DibiConnection::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-11-10 07:37:44 +00:00
|
|
|
* @return DibiResult Result set object (if any)
|
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-11-12 06:41:59 +00:00
|
|
|
* Executes the SQL query - Monostate for DibiConnection::nativeQuery()
|
2007-09-29 07:53:25 +00:00
|
|
|
*
|
2007-12-11 07:28:55 +00:00
|
|
|
* @param string SQL statement.
|
2007-11-10 07:37:44 +00:00
|
|
|
* @return DibiResult Result set object (if any)
|
2007-09-29 07:53:25 +00:00
|
|
|
*/
|
|
|
|
public static function nativeQuery($sql)
|
|
|
|
{
|
|
|
|
return self::getConnection()->nativeQuery($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-11-12 06:41:59 +00:00
|
|
|
* Generates and prints SQL query - Monostate for DibiConnection::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-11-12 16:20:44 +00:00
|
|
|
/**
|
|
|
|
* Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch()
|
|
|
|
*
|
|
|
|
* @param array|mixed one or more arguments
|
|
|
|
* @return array
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function fetch($args)
|
|
|
|
{
|
|
|
|
if (!is_array($args)) $args = func_get_args();
|
|
|
|
|
|
|
|
return self::getConnection()->query($args)->fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes SQL query and fetch results - Monostate for DibiConnection::query() & fetchAll()
|
|
|
|
*
|
|
|
|
* @param array|mixed one or more arguments
|
|
|
|
* @return array
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function fetchAll($args)
|
|
|
|
{
|
|
|
|
if (!is_array($args)) $args = func_get_args();
|
|
|
|
|
|
|
|
return self::getConnection()->query($args)->fetchAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes SQL query and fetch first column - Monostate for DibiConnection::query() & fetchSingle()
|
|
|
|
*
|
|
|
|
* @param array|mixed one or more arguments
|
|
|
|
* @return string
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function fetchSingle($args)
|
|
|
|
{
|
|
|
|
if (!is_array($args)) $args = func_get_args();
|
|
|
|
|
|
|
|
return self::getConnection()->query($args)->fetchSingle();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
/**
|
2007-11-23 23:27:14 +00:00
|
|
|
* Gets the number of affected rows
|
|
|
|
* Monostate for DibiConnection::affectedRows()
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
2007-11-23 23:27:14 +00:00
|
|
|
* @return int number of rows
|
|
|
|
* @throws DibiException
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-11-23 23:27:14 +00:00
|
|
|
public static function affectedRows()
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-11-23 23:27:14 +00:00
|
|
|
return self::getConnection()->affectedRows();
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-11-23 23:27:14 +00:00
|
|
|
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
|
|
|
|
* Monostate for DibiConnection::insertId()
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
2007-11-23 23:27:14 +00:00
|
|
|
* @param string optional sequence name
|
|
|
|
* @return int
|
|
|
|
* @throws DibiException
|
2006-06-04 23:06:33 +00:00
|
|
|
*/
|
2007-11-23 23:27:14 +00:00
|
|
|
public static function insertId($sequence=NULL)
|
2006-06-04 23:06:33 +00:00
|
|
|
{
|
2007-11-23 23:27:14 +00:00
|
|
|
return self::getConnection()->insertId($sequence);
|
2006-08-25 15:17:40 +00:00
|
|
|
}
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
2007-08-09 03:31:34 +00:00
|
|
|
/**
|
2007-11-12 06:41:59 +00:00
|
|
|
* Begins a transaction - Monostate for DibiConnection::begin()
|
2007-11-23 23:27:14 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
2007-08-09 03:31:34 +00:00
|
|
|
*/
|
|
|
|
public static function begin()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
self::getConnection()->begin();
|
2007-08-09 03:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-11-12 06:41:59 +00:00
|
|
|
* Commits statements in a transaction - Monostate for DibiConnection::commit()
|
2007-11-23 23:27:14 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
2007-08-09 03:31:34 +00:00
|
|
|
*/
|
|
|
|
public static function commit()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
self::getConnection()->commit();
|
2007-08-09 03:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2007-11-12 06:41:59 +00:00
|
|
|
* Rollback changes in a transaction - Monostate for DibiConnection::rollback()
|
2007-11-23 23:27:14 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
2007-08-09 03:31:34 +00:00
|
|
|
*/
|
|
|
|
public static function rollback()
|
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
self::getConnection()->rollback();
|
2006-06-04 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-12 01:20:23 +00:00
|
|
|
/**
|
|
|
|
* Import SQL dump from file - extreme fast!
|
|
|
|
*
|
|
|
|
* @param filename
|
|
|
|
* @return int count of sql commands
|
|
|
|
*/
|
|
|
|
public static function loadFile($file)
|
|
|
|
{
|
|
|
|
return self::getConnection()->loadFile($file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-10 07:37:44 +00:00
|
|
|
/**
|
|
|
|
* Experimental; will be used in PHP 5.3
|
|
|
|
*/
|
|
|
|
public static function __callStatic($name, $args)
|
|
|
|
{
|
|
|
|
return call_user_func_array(array(self::getConnection(), $name), $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-23 23:27:14 +00:00
|
|
|
/**
|
|
|
|
* Pseudotype for timestamp representation
|
|
|
|
*
|
2007-12-11 07:28:55 +00:00
|
|
|
* @param mixed datetime
|
2007-11-23 23:27:14 +00:00
|
|
|
* @return DibiVariable
|
|
|
|
*/
|
|
|
|
public static function datetime($time = NULL)
|
|
|
|
{
|
|
|
|
if ($time === NULL) {
|
|
|
|
$time = time(); // current time
|
|
|
|
} elseif (is_string($time)) {
|
|
|
|
$time = strtotime($time); // try convert to timestamp
|
|
|
|
} else {
|
|
|
|
$time = (int) $time;
|
|
|
|
}
|
|
|
|
return new DibiVariable($time, dibi::FIELD_DATETIME);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pseudotype for date representation
|
|
|
|
*
|
2007-12-11 07:28:55 +00:00
|
|
|
* @param mixed date
|
2007-11-23 23:27:14 +00:00
|
|
|
* @return DibiVariable
|
|
|
|
*/
|
|
|
|
public static function date($date = NULL)
|
|
|
|
{
|
|
|
|
$var = self::datetime($date);
|
|
|
|
$var->type = dibi::FIELD_DATE;
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-23 06:34:44 +00:00
|
|
|
/**
|
|
|
|
* Create a new substitution pair for indentifiers
|
2007-08-28 23:17:34 +00:00
|
|
|
*
|
2007-12-11 07:28:55 +00:00
|
|
|
* @param string from
|
|
|
|
* @param string to
|
2006-09-23 06:34:44 +00:00
|
|
|
* @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-12-11 07:28:55 +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
|
|
|
|
*
|
2007-12-11 07:28:55 +00:00
|
|
|
* @param callback
|
2007-08-29 08:17:45 +00:00
|
|
|
* @return void
|
2007-11-08 03:32:37 +00:00
|
|
|
* @throws InvalidArgumentException
|
2007-08-29 08:17:45 +00:00
|
|
|
*/
|
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)) {
|
2007-11-08 03:32:37 +00:00
|
|
|
throw new InvalidArgumentException("Invalid callback");
|
2007-08-29 08:17:45 +00:00
|
|
|
}
|
|
|
|
|
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-12-11 07:28:55 +00:00
|
|
|
* @param DibiConnection
|
|
|
|
* @param string event name
|
|
|
|
* @param mixed
|
2007-08-29 08:17:45 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2007-11-12 07:33:23 +00:00
|
|
|
public static function notify(DibiConnection $connection = NULL, $event, $arg = NULL)
|
2007-08-29 08:17:45 +00:00
|
|
|
{
|
2007-09-29 07:53:25 +00:00
|
|
|
foreach (self::$handlers as $handler) {
|
2007-11-12 07:33:23 +00:00
|
|
|
call_user_func($handler, $connection, $event, $arg);
|
2007-08-29 08:17:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
/**
|
|
|
|
* Enable profiler & logger
|
|
|
|
*
|
2007-12-11 07:28:55 +00:00
|
|
|
* @param string filename
|
|
|
|
* @param bool log all queries?
|
2007-09-29 07:53:25 +00:00
|
|
|
* @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
|
|
|
|
*
|
2007-12-11 07:28:55 +00:00
|
|
|
* @param string|DibiResult
|
|
|
|
* @param bool return output instead of printing it?
|
2007-09-29 07:53:25 +00:00
|
|
|
* @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;
|
|
|
|
|
2007-11-28 15:56:57 +00:00
|
|
|
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|TRUE|FALSE';
|
|
|
|
static $keywords1 = 'SELECT|UPDATE|INSERT(?:\s+INTO)|REPLACE(?:\s+INTO)|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
|
2007-12-01 17:24:49 +00:00
|
|
|
$sql = ' ' . $sql;
|
|
|
|
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
|
2007-09-29 07:53:25 +00:00
|
|
|
|
|
|
|
// reduce spaces
|
|
|
|
$sql = preg_replace('# {2,}#', ' ', $sql);
|
|
|
|
|
|
|
|
$sql = wordwrap($sql, 100);
|
|
|
|
$sql = htmlSpecialChars($sql);
|
|
|
|
$sql = preg_replace("#\n{2,}#", "\n", $sql);
|
|
|
|
|
|
|
|
// syntax highlight
|
2007-12-01 17:24:49 +00:00
|
|
|
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(])($keywords2)(?=[\\s,)])#i", array('dibi', 'highlightCallback'), $sql);
|
|
|
|
$sql = trim($sql);
|
2007-09-29 07:53:25 +00:00
|
|
|
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-11-12 00:08:29 +00:00
|
|
|
}
|