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-04-19 14:13:48 +00:00
|
|
|
* Copyright (c) 2005, 2008 David Grudl (http://davidgrudl.com)
|
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
|
|
|
/**
|
2008-02-01 02:12:36 +00:00
|
|
|
* Check PHP configuration.
|
2007-11-01 05:52:13 +00:00
|
|
|
*/
|
2008-02-14 02:02:27 +00:00
|
|
|
if (version_compare(PHP_VERSION, '5.1.0', '<')) {
|
2008-05-12 00:30:59 +00:00
|
|
|
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
|
|
|
|
2008-07-12 23:11:57 +00:00
|
|
|
/**
|
|
|
|
* Compatibility with Nette
|
|
|
|
*/
|
2008-05-20 08:15:30 +00:00
|
|
|
if (!class_exists('NotImplementedException', FALSE)) {
|
2008-07-12 23:11:57 +00:00
|
|
|
class NotImplementedException extends LogicException {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists('NotSupportedException', FALSE)) {
|
|
|
|
class NotSupportedException extends LogicException {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists('MemberAccessException', FALSE)) {
|
|
|
|
class MemberAccessException extends LogicException {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists('InvalidStateException', FALSE)) {
|
|
|
|
class InvalidStateException extends RuntimeException {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists('IOException', FALSE)) {
|
|
|
|
class IOException extends RuntimeException {}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists('FileNotFoundException', FALSE)) {
|
|
|
|
class FileNotFoundException extends IOException {}
|
2008-05-20 08:15:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!class_exists(/*Nette::*/'Object', FALSE)) {
|
|
|
|
require_once dirname(__FILE__) . '/Nette/Object.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!interface_exists(/*Nette::*/'IDebuggable', FALSE)) {
|
|
|
|
require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
|
|
|
|
}
|
2007-11-15 00:19:41 +00:00
|
|
|
|
|
|
|
// dibi libraries
|
2008-01-15 03:43:03 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/interfaces.php';
|
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/DibiResult.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiVariable.php';
|
2008-01-15 03:43:03 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiTable.php';
|
2008-01-18 02:57:43 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiDataSource.php';
|
2008-05-21 11:20:46 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiFluent.php';
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-02-01 02:12:36 +00:00
|
|
|
* Interface for database drivers.
|
2006-06-04 23:06:33 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
{
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Column type in relation to PHP native type.
|
|
|
|
*/
|
|
|
|
const
|
|
|
|
FIELD_TEXT = 's', // as 'string'
|
2008-05-25 18:44:43 +00:00
|
|
|
FIELD_BINARY = 'bin',
|
2008-05-12 00:30:59 +00:00
|
|
|
FIELD_BOOL = 'b',
|
|
|
|
FIELD_INTEGER = 'i',
|
|
|
|
FIELD_FLOAT = 'f',
|
|
|
|
FIELD_DATE = 'd',
|
|
|
|
FIELD_DATETIME = 't',
|
|
|
|
|
|
|
|
// special
|
2008-05-12 22:55:51 +00:00
|
|
|
IDENTIFIER = 'n';
|
2008-05-12 00:30:59 +00:00
|
|
|
|
2008-05-12 22:55:51 +00:00
|
|
|
/**
|
|
|
|
* dibi version
|
|
|
|
*/
|
|
|
|
const
|
2008-05-12 00:30:59 +00:00
|
|
|
VERSION = '0.9 (Revision: $WCREV$, Date: $WCDATE$)';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Connection registry storage for DibiConnection objects.
|
|
|
|
* @var DibiConnection[]
|
|
|
|
*/
|
|
|
|
private static $registry = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current connection.
|
|
|
|
* @var DibiConnection
|
|
|
|
*/
|
|
|
|
private static $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Substitutions for identifiers.
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $substs = array();
|
|
|
|
|
2008-06-08 12:44:44 +00:00
|
|
|
/**
|
|
|
|
* Substitution fallback.
|
|
|
|
* @var callback
|
|
|
|
*/
|
|
|
|
private static $substFallBack;
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* @see addHandler
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $handlers = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Last SQL command @see dibi::query()
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $sql;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Elapsed time for last query.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $elapsedTime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Elapsed time for all queries.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $totalTime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number or queries.
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $numOfQueries = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default dibi driver.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public static $defaultDriver = 'mysql';
|
2007-10-26 17:44:24 +00:00
|
|
|
|
2007-09-29 07:53:25 +00:00
|
|
|
|
|
|
|
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Static class - cannot be instantiated.
|
|
|
|
*/
|
|
|
|
final public function __construct()
|
|
|
|
{
|
|
|
|
throw new LogicException("Cannot instantiate static class " . get_class($this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************* connections handling ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new DibiConnection object and connects it to specified database.
|
|
|
|
*
|
2008-05-13 13:02:26 +00:00
|
|
|
* @param array|string|Nette::Collections::Hashtable connection parameters
|
2008-05-12 00:30:59 +00:00
|
|
|
* @param string connection name
|
|
|
|
* @return DibiConnection
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function connect($config = array(), $name = 0)
|
|
|
|
{
|
|
|
|
if (class_exists(/*Nette::*/'Debug', FALSE)) {
|
|
|
|
/*Nette::*/Debug::addColophon(array(__CLASS__, 'getColophon'));
|
|
|
|
}
|
2008-04-03 12:40:04 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
return self::$connection = self::$registry[$name] = new DibiConnection($config, $name);
|
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Disconnects from database (doesn't destroy DibiConnection object).
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function disconnect()
|
|
|
|
{
|
|
|
|
self::getConnection()->disconnect();
|
|
|
|
}
|
2007-11-10 07:37:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Returns TRUE when connection was established.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isConnected()
|
|
|
|
{
|
2008-05-16 17:56:24 +00:00
|
|
|
return (self::$connection !== NULL) && self::$connection->isConnected();
|
2008-05-12 00:30:59 +00:00
|
|
|
}
|
|
|
|
|
2006-06-04 23:06:33 +00:00
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Retrieve active connection.
|
|
|
|
*
|
|
|
|
* @param string connection registy name
|
|
|
|
* @return object DibiConnection object.
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function getConnection($name = NULL)
|
|
|
|
{
|
|
|
|
if ($name === NULL) {
|
2008-05-16 17:56:24 +00:00
|
|
|
if (self::$connection === NULL) {
|
2008-05-12 00:30:59 +00:00
|
|
|
throw new DibiException('Dibi is not connected to database.');
|
|
|
|
}
|
2007-04-06 07:34:48 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
return self::$connection;
|
|
|
|
}
|
2006-11-13 06:32:16 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
if (!isset(self::$registry[$name])) {
|
|
|
|
throw new DibiException("There is no connection named '$name'.");
|
|
|
|
}
|
2007-04-06 07:34:48 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
return self::$registry[$name];
|
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
2006-09-23 06:34:44 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Change active connection.
|
|
|
|
*
|
|
|
|
* @param string connection registy name
|
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function activate($name)
|
|
|
|
{
|
|
|
|
self::$connection = self::getConnection($name);
|
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/********************* monostate for active connection ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates and executes SQL query - Monostate for DibiConnection::query().
|
|
|
|
*
|
2008-05-20 08:15:30 +00:00
|
|
|
* @param array|mixed one or more arguments
|
|
|
|
* @return DibiResult|NULL result set object (if any)
|
2008-05-12 00:30:59 +00:00
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function query($args)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
return self::getConnection()->query($args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes the SQL query - Monostate for DibiConnection::nativeQuery().
|
|
|
|
*
|
2008-05-20 08:15:30 +00:00
|
|
|
* @param string SQL statement.
|
|
|
|
* @return DibiResult|NULL result set object (if any)
|
2008-05-12 00:30:59 +00:00
|
|
|
*/
|
|
|
|
public static function nativeQuery($sql)
|
|
|
|
{
|
|
|
|
return self::getConnection()->nativeQuery($sql);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates and prints SQL query - Monostate for DibiConnection::test().
|
|
|
|
*
|
|
|
|
* @param array|mixed one or more arguments
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function test($args)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
return self::getConnection()->test($args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
$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)
|
|
|
|
{
|
|
|
|
$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)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
return self::getConnection()->query($args)->fetchSingle();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of affected rows.
|
|
|
|
* Monostate for DibiConnection::affectedRows()
|
|
|
|
*
|
|
|
|
* @return int number of rows
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function affectedRows()
|
|
|
|
{
|
|
|
|
return self::getConnection()->affectedRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
|
|
|
* Monostate for DibiConnection::insertId()
|
|
|
|
*
|
|
|
|
* @param string optional sequence name
|
|
|
|
* @return int
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function insertId($sequence=NULL)
|
|
|
|
{
|
|
|
|
return self::getConnection()->insertId($sequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins a transaction - Monostate for DibiConnection::begin().
|
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function begin()
|
|
|
|
{
|
|
|
|
self::getConnection()->begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Commits statements in a transaction - Monostate for DibiConnection::commit().
|
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function commit()
|
|
|
|
{
|
|
|
|
self::getConnection()->commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollback changes in a transaction - Monostate for DibiConnection::rollback().
|
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function rollback()
|
|
|
|
{
|
|
|
|
self::getConnection()->rollback();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import SQL dump from file - extreme fast!
|
|
|
|
*
|
|
|
|
* @param string filename
|
|
|
|
* @return int count of sql commands
|
|
|
|
*/
|
|
|
|
public static function loadFile($file)
|
|
|
|
{
|
|
|
|
return self::getConnection()->loadFile($file);
|
|
|
|
}
|
|
|
|
|
2008-01-12 01:20:23 +00:00
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Replacement for majority of dibi::methods() in future.
|
|
|
|
*/
|
|
|
|
protected static function __callStatic($name, $args)
|
|
|
|
{
|
2008-05-21 11:20:46 +00:00
|
|
|
//if ($name = 'select', 'update', ...') {
|
|
|
|
// return self::command()->$name($args);
|
|
|
|
//}
|
2008-05-12 00:30:59 +00:00
|
|
|
return call_user_func_array(array(self::getConnection(), $name), $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-21 11:20:46 +00:00
|
|
|
/********************* fluent SQL builders ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
|
|
|
public static function command()
|
|
|
|
{
|
|
|
|
return new DibiFluent(self::getConnection());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string column name
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
|
|
|
public static function select($args)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
return self::command()->__call('select', $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string table
|
|
|
|
* @param array
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
|
|
|
public static function update($table, array $args)
|
|
|
|
{
|
|
|
|
return self::command()->update('%n', $table)->set($args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string table
|
|
|
|
* @param array
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
|
|
|
public static function insert($table, array $args)
|
|
|
|
{
|
|
|
|
return self::command()->insert()
|
|
|
|
->into('%n', $table, '(%n)', array_keys($args))->values('%l', array_values($args));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string table
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
|
|
|
public static function delete($table)
|
|
|
|
{
|
|
|
|
return self::command()->delete()->from('%n', $table);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/********************* data types ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pseudotype for timestamp representation.
|
|
|
|
*
|
|
|
|
* @param mixed datetime
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2007-11-23 23:27:14 +00:00
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Pseudotype for date representation.
|
|
|
|
*
|
|
|
|
* @param mixed date
|
|
|
|
* @return DibiVariable
|
|
|
|
*/
|
|
|
|
public static function date($date = NULL)
|
|
|
|
{
|
|
|
|
$var = self::datetime($date);
|
|
|
|
$var->modifier = dibi::FIELD_DATE;
|
|
|
|
return $var;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************* substitutions ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new substitution pair for indentifiers.
|
|
|
|
*
|
|
|
|
* @param string from
|
|
|
|
* @param string to
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function addSubst($expr, $subst)
|
|
|
|
{
|
2008-06-08 12:44:44 +00:00
|
|
|
self::$substs[$expr] = $subst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets substitution fallback handler.
|
|
|
|
*
|
|
|
|
* @param callback
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function setSubstFallback($callback)
|
|
|
|
{
|
|
|
|
if (!is_callable($callback)) {
|
|
|
|
throw new InvalidArgumentException("Invalid callback.");
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$substFallBack = $callback;
|
2008-05-12 00:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove substitution pair.
|
|
|
|
*
|
|
|
|
* @param mixed from or TRUE
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function removeSubst($expr)
|
|
|
|
{
|
|
|
|
if ($expr === TRUE) {
|
|
|
|
self::$substs = array();
|
|
|
|
} else {
|
|
|
|
unset(self::$substs[':'.$expr.':']);
|
|
|
|
}
|
|
|
|
}
|
2006-06-04 23:06:33 +00:00
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
2008-06-08 12:44:44 +00:00
|
|
|
* Provides substitution.
|
2008-05-12 00:30:59 +00:00
|
|
|
*
|
2008-06-08 12:44:44 +00:00
|
|
|
* @param string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function substitute($value)
|
|
|
|
{
|
|
|
|
if (strpos($value, ':') === FALSE) {
|
|
|
|
return $value;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return preg_replace_callback('#:(.*):#U', array('dibi', 'subCb'), $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Substitution callback.
|
|
|
|
*
|
|
|
|
* @param array
|
|
|
|
* @return string
|
2008-05-12 00:30:59 +00:00
|
|
|
*/
|
2008-06-08 12:44:44 +00:00
|
|
|
private static function subCb($m)
|
2008-05-12 00:30:59 +00:00
|
|
|
{
|
2008-06-08 12:44:44 +00:00
|
|
|
$m = $m[1];
|
|
|
|
if (isset(self::$substs[$m])) {
|
|
|
|
return self::$substs[$m];
|
|
|
|
|
|
|
|
} elseif (self::$substFallBack) {
|
|
|
|
return self::$substs[$m] = call_user_func(self::$substFallBack, $m);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return $m;
|
|
|
|
}
|
2008-05-12 00:30:59 +00:00
|
|
|
}
|
2007-03-27 23:12:36 +00:00
|
|
|
|
|
|
|
|
2007-08-23 00:57:28 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/********************* event handling ****************d*g**/
|
2008-04-03 12:40:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Add new event handler.
|
|
|
|
*
|
|
|
|
* @param callback
|
|
|
|
* @return void
|
|
|
|
* @throws InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public static function addHandler($callback)
|
|
|
|
{
|
|
|
|
if (!is_callable($callback)) {
|
|
|
|
throw new InvalidArgumentException("Invalid callback.");
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$handlers[] = $callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/**
|
|
|
|
* Event notification (events: exception, connected, beforeQuery, afterQuery, begin, commit, rollback).
|
|
|
|
*
|
|
|
|
* @param DibiConnection
|
|
|
|
* @param string event name
|
|
|
|
* @param mixed
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function notify(DibiConnection $connection = NULL, $event, $arg = NULL)
|
|
|
|
{
|
|
|
|
foreach (self::$handlers as $handler) {
|
|
|
|
call_user_func($handler, $connection, $event, $arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable profiler & logger.
|
|
|
|
*
|
|
|
|
* @param string filename
|
|
|
|
* @param bool log all queries?
|
|
|
|
* @return DibiProfiler
|
|
|
|
*/
|
|
|
|
public static function startLogger($file, $logQueries = FALSE)
|
|
|
|
{
|
2008-05-20 08:15:30 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiLogger.php';
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
$logger = new DibiLogger($file);
|
|
|
|
$logger->logQueries = $logQueries;
|
|
|
|
self::addHandler(array($logger, 'handler'));
|
|
|
|
return $logger;
|
|
|
|
}
|
2007-08-29 08:17:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
/********************* misc tools ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints out a syntax highlighted version of the SQL command or DibiResult.
|
|
|
|
*
|
|
|
|
* @param string|DibiResult
|
|
|
|
* @param bool return output instead of printing it?
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function dump($sql = NULL, $return = FALSE)
|
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
if ($sql instanceof DibiResult) {
|
|
|
|
$sql->dump();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if ($sql === NULL) $sql = self::$sql;
|
|
|
|
|
2008-05-21 11:20:46 +00:00
|
|
|
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|TRUNCATE';
|
2008-05-12 00:30:59 +00:00
|
|
|
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|TRUE|FALSE';
|
|
|
|
|
|
|
|
// insert new lines
|
|
|
|
$sql = ' ' . $sql;
|
|
|
|
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
|
2007-09-29 07:53:25 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
// reduce spaces
|
|
|
|
$sql = preg_replace('#[ \t]{2,}#', " ", $sql);
|
2007-09-29 07:53:25 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
$sql = wordwrap($sql, 100);
|
|
|
|
$sql = htmlSpecialChars($sql);
|
|
|
|
$sql = preg_replace("#\n{2,}#", "\n", $sql);
|
|
|
|
|
|
|
|
// syntax highlight
|
|
|
|
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", array('dibi', 'highlightCallback'), $sql);
|
|
|
|
$sql = trim($sql);
|
|
|
|
echo '<pre class="dump">', $sql, "</pre>\n";
|
|
|
|
}
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
if ($return) {
|
|
|
|
return ob_get_clean();
|
|
|
|
} else {
|
|
|
|
ob_end_flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static function highlightCallback($matches)
|
|
|
|
{
|
|
|
|
if (!empty($matches[1])) // comment
|
|
|
|
return '<em style="color:gray">' . $matches[1] . '</em>';
|
|
|
|
|
|
|
|
if (!empty($matches[2])) // error
|
|
|
|
return '<strong style="color:red">' . $matches[2] . '</strong>';
|
|
|
|
|
|
|
|
if (!empty($matches[3])) // most important keywords
|
|
|
|
return '<strong style="color:blue">' . $matches[3] . '</strong>';
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
if (!empty($matches[4])) // other keywords
|
|
|
|
return '<strong style="color:green">' . $matches[4] . '</strong>';
|
|
|
|
}
|
2007-08-29 08:17:45 +00:00
|
|
|
|
2008-05-12 00:30:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns brief descriptions.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getColophon()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'dibi version: ' . dibi::VERSION,
|
2008-05-21 11:20:46 +00:00
|
|
|
'Number or queries: ' . dibi::$numOfQueries
|
|
|
|
. (dibi::$totalTime === NULL ? '' : ' (elapsed time: ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms)'),
|
2008-05-12 00:30:59 +00:00
|
|
|
);
|
|
|
|
}
|
2008-04-03 12:40:04 +00:00
|
|
|
|
2007-11-12 00:08:29 +00:00
|
|
|
}
|