2008-07-17 03:51:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2010-09-14 18:40:41 +02:00
|
|
|
* dibi - smart database abstraction layer.
|
2008-07-17 03:51:29 +00:00
|
|
|
*
|
2010-01-03 15:15:37 +01:00
|
|
|
* Copyright (c) 2005, 2010 David Grudl (http://davidgrudl.com)
|
2008-07-17 03:51:29 +00:00
|
|
|
*
|
2010-09-14 18:40:41 +02:00
|
|
|
* This source file is subject to the "dibi license", and/or
|
|
|
|
* GPL license. For more information please see http://dibiphp.com
|
2008-07-17 03:51:29 +00:00
|
|
|
* @package dibi
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check PHP configuration.
|
|
|
|
*/
|
2009-11-16 01:44:10 +01:00
|
|
|
if (version_compare(PHP_VERSION, '5.2.0', '<')) {
|
|
|
|
throw new Exception('dibi needs PHP 5.2.0 or newer.');
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2009-04-16 02:15:20 +00:00
|
|
|
@set_magic_quotes_runtime(FALSE); // intentionally @
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compatibility with Nette
|
|
|
|
*/
|
2010-09-28 17:29:02 +02:00
|
|
|
if (interface_exists('Nette\\IDebugPanel')) {
|
2010-08-25 01:15:59 +02:00
|
|
|
class_alias('Nette\\IDebugPanel', 'IDebugPanel');
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2010-09-28 17:29:02 +02:00
|
|
|
} elseif (!interface_exists('IDebugPanel')) {
|
2010-08-25 01:15:59 +02:00
|
|
|
interface IDebugPanel {}
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2010-08-25 01:15:59 +02:00
|
|
|
if (!defined('NETTE')) {
|
|
|
|
/**#@+ @package exceptions */
|
|
|
|
class NotImplementedException extends LogicException {}
|
|
|
|
class NotSupportedException extends LogicException {}
|
2008-07-17 03:51:29 +00:00
|
|
|
class MemberAccessException extends LogicException {}
|
|
|
|
class InvalidStateException extends RuntimeException {}
|
|
|
|
class IOException extends RuntimeException {}
|
|
|
|
class FileNotFoundException extends IOException {}
|
2010-08-25 01:15:59 +02:00
|
|
|
/**#@-*/
|
2010-05-16 22:06:29 +02:00
|
|
|
}
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2010-10-06 17:23:31 +02:00
|
|
|
class DibiPcreException extends Exception {
|
2010-08-25 01:15:59 +02:00
|
|
|
|
|
|
|
public function __construct($message = '%msg.')
|
|
|
|
{
|
|
|
|
static $messages = array(
|
|
|
|
PREG_INTERNAL_ERROR => 'Internal error',
|
|
|
|
PREG_BACKTRACK_LIMIT_ERROR => 'Backtrack limit was exhausted',
|
|
|
|
PREG_RECURSION_LIMIT_ERROR => 'Recursion limit was exhausted',
|
|
|
|
PREG_BAD_UTF8_ERROR => 'Malformed UTF-8 data',
|
|
|
|
5 => 'Offset didn\'t correspond to the begin of a valid UTF-8 code point', // PREG_BAD_UTF8_OFFSET_ERROR
|
|
|
|
);
|
|
|
|
$code = preg_last_error();
|
|
|
|
parent::__construct(str_replace('%msg', isset($messages[$code]) ? $messages[$code] : 'Unknown error', $message), $code);
|
|
|
|
}
|
2010-01-14 23:41:37 +01:00
|
|
|
}
|
|
|
|
|
2009-11-16 01:44:10 +01:00
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
// dibi libraries
|
|
|
|
require_once dirname(__FILE__) . '/libs/interfaces.php';
|
2010-10-06 16:02:20 +02:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiDateTime.php';
|
2008-09-05 05:35:15 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiObject.php';
|
2010-08-03 06:28:17 +02:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiLazyStorage.php';
|
2008-07-17 03:51:29 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiException.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiConnection.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiResult.php';
|
2009-01-07 13:48:01 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiResultIterator.php';
|
2009-02-05 01:26:08 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiRow.php';
|
2008-07-17 03:51:29 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiTranslator.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiDataSource.php';
|
|
|
|
require_once dirname(__FILE__) . '/libs/DibiFluent.php';
|
2008-10-02 17:13:43 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiDatabaseInfo.php';
|
2008-10-22 11:44:11 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiProfiler.php';
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-10-06 16:02:20 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
|
|
|
class DibiVariable extends DibiDateTime
|
|
|
|
{
|
|
|
|
function __construct($val)
|
|
|
|
{
|
|
|
|
parent::__construct($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for database drivers.
|
|
|
|
*
|
|
|
|
* This class is static container class for creating DB objects and
|
|
|
|
* store connections info.
|
|
|
|
*
|
2010-09-14 18:40:41 +02:00
|
|
|
* @author David Grudl
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
|
|
|
class dibi
|
|
|
|
{
|
2008-10-28 15:24:47 +00:00
|
|
|
/**#@+
|
2009-03-16 06:48:27 +00:00
|
|
|
* dibi data type
|
|
|
|
*/
|
|
|
|
const TEXT = 's'; // as 'string'
|
|
|
|
const BINARY = 'bin';
|
|
|
|
const BOOL = 'b';
|
|
|
|
const INTEGER = 'i';
|
|
|
|
const FLOAT = 'f';
|
|
|
|
const DATE = 'd';
|
|
|
|
const DATETIME = 't';
|
|
|
|
const TIME = 't';
|
|
|
|
const IDENTIFIER = 'n';
|
2008-10-28 15:24:47 +00:00
|
|
|
/**#@-*/
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2009-03-16 06:48:27 +00:00
|
|
|
/**#@+
|
|
|
|
* @deprecated column types
|
|
|
|
*/
|
|
|
|
const FIELD_TEXT = self::TEXT;
|
|
|
|
const FIELD_BINARY = self::BINARY;
|
|
|
|
const FIELD_BOOL = self::BOOL;
|
|
|
|
const FIELD_INTEGER = self::INTEGER;
|
|
|
|
const FIELD_FLOAT = self::FLOAT;
|
|
|
|
const FIELD_DATE = self::DATE;
|
|
|
|
const FIELD_DATETIME = self::DATETIME;
|
|
|
|
const FIELD_TIME = self::TIME;
|
|
|
|
/**#@-*/
|
2008-10-28 15:24:47 +00:00
|
|
|
|
|
|
|
/**#@+
|
2008-07-17 03:51:29 +00:00
|
|
|
* dibi version
|
|
|
|
*/
|
2009-09-30 22:50:23 +02:00
|
|
|
const VERSION = '1.3-dev';
|
2008-10-28 15:24:47 +00:00
|
|
|
const REVISION = '$WCREV$ released on $WCDATE$';
|
|
|
|
/**#@-*/
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2009-02-05 01:26:08 +00:00
|
|
|
const ASC = 'ASC', DESC = 'DESC';
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var DibiConnection[] Connection registry storage for DibiConnection objects */
|
2008-07-17 03:51:29 +00:00
|
|
|
private static $registry = array();
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var DibiConnection Current connection */
|
2008-07-17 03:51:29 +00:00
|
|
|
private static $connection;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var array @see addHandler */
|
2008-07-17 03:51:29 +00:00
|
|
|
private static $handlers = array();
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var string Last SQL command @see dibi::query() */
|
2008-07-17 03:51:29 +00:00
|
|
|
public static $sql;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var int Elapsed time for last query */
|
2008-07-17 03:51:29 +00:00
|
|
|
public static $elapsedTime;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var int Elapsed time for all queries */
|
2008-07-17 03:51:29 +00:00
|
|
|
public static $totalTime;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var int Number or queries */
|
2008-07-17 03:51:29 +00:00
|
|
|
public static $numOfQueries = 0;
|
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var string Default dibi driver */
|
2008-07-17 03:51:29 +00:00
|
|
|
public static $defaultDriver = 'mysql';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2010-04-26 20:43:04 +02:00
|
|
|
* @param mixed connection parameters
|
|
|
|
* @param string connection name
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return DibiConnection
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function connect($config = array(), $name = 0)
|
|
|
|
{
|
|
|
|
return self::$connection = self::$registry[$name] = new DibiConnection($config, $name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects from database (doesn't destroy DibiConnection object).
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function disconnect()
|
|
|
|
{
|
|
|
|
self::getConnection()->disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns TRUE when connection was established.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function isConnected()
|
|
|
|
{
|
|
|
|
return (self::$connection !== NULL) && self::$connection->isConnected();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve active connection.
|
|
|
|
* @param string connection registy name
|
2008-10-22 11:44:11 +00:00
|
|
|
* @return DibiConnection
|
2008-07-17 03:51:29 +00:00
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function getConnection($name = NULL)
|
|
|
|
{
|
|
|
|
if ($name === NULL) {
|
|
|
|
if (self::$connection === NULL) {
|
|
|
|
throw new DibiException('Dibi is not connected to database.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset(self::$registry[$name])) {
|
|
|
|
throw new DibiException("There is no connection named '$name'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$registry[$name];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-08-31 22:39:57 +02:00
|
|
|
/**
|
|
|
|
* Sets connection.
|
|
|
|
* @param DibiConnection
|
|
|
|
* @return DibiConnection
|
|
|
|
*/
|
|
|
|
public static function setConnection(DibiConnection $connection)
|
|
|
|
{
|
|
|
|
return self::$connection = $connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
|
|
|
* Change active connection.
|
|
|
|
* @param string connection registy name
|
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function activate($name)
|
|
|
|
{
|
|
|
|
self::$connection = self::getConnection($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-22 11:44:11 +00:00
|
|
|
/**
|
|
|
|
* Retrieve active connection profiler.
|
|
|
|
* @return IDibiProfiler
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function getProfiler()
|
|
|
|
{
|
|
|
|
return self::getConnection()->getProfiler();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/********************* monostate for active connection ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates and executes SQL query - Monostate for DibiConnection::query().
|
|
|
|
* @param array|mixed one or more arguments
|
2009-02-05 02:13:15 +00:00
|
|
|
* @return DibiResult|int result set object (if any)
|
2008-07-17 03:51:29 +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().
|
|
|
|
* @param string SQL statement.
|
2009-02-05 02:13:15 +00:00
|
|
|
* @return DibiResult|int result set object (if any)
|
2008-07-17 03:51:29 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-02-08 21:15:54 +00:00
|
|
|
/**
|
|
|
|
* Generates and returns SQL query as DibiDataSource - Monostate for DibiConnection::test().
|
|
|
|
* @param array|mixed one or more arguments
|
|
|
|
* @return DibiDataSource
|
|
|
|
*/
|
|
|
|
public static function dataSource($args)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
return self::getConnection()->dataSource($args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
|
|
|
* Executes SQL query and fetch result - Monostate for DibiConnection::query() & fetch().
|
|
|
|
* @param array|mixed one or more arguments
|
2008-10-10 17:39:33 +00:00
|
|
|
* @return DibiRow
|
2008-07-17 03:51:29 +00:00
|
|
|
* @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
|
2008-10-10 17:39:33 +00:00
|
|
|
* @return array of DibiRow
|
2008-07-17 03:51:29 +00:00
|
|
|
* @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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-10-02 09:01:38 +00:00
|
|
|
/**
|
|
|
|
* Executes SQL query and fetch pairs - Monostate for DibiConnection::query() & fetchPairs().
|
|
|
|
* @param array|mixed one or more arguments
|
|
|
|
* @return string
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function fetchPairs($args)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
return self::getConnection()->query($args)->fetchPairs();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
|
|
|
* Gets the number of affected rows.
|
2009-02-26 20:02:14 +00:00
|
|
|
* Monostate for DibiConnection::getAffectedRows()
|
|
|
|
* @return int number of rows
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function getAffectedRows()
|
|
|
|
{
|
|
|
|
return self::getConnection()->getAffectedRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the number of affected rows. Alias for getAffectedRows().
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return int number of rows
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function affectedRows()
|
|
|
|
{
|
2009-02-26 20:02:14 +00:00
|
|
|
return self::getConnection()->getAffectedRows();
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
|
2009-02-26 20:02:14 +00:00
|
|
|
* Monostate for DibiConnection::getInsertId()
|
|
|
|
* @param string optional sequence name
|
|
|
|
* @return int
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function getInsertId($sequence=NULL)
|
|
|
|
{
|
|
|
|
return self::getConnection()->getInsertId($sequence);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId().
|
2008-07-17 03:51:29 +00:00
|
|
|
* @param string optional sequence name
|
|
|
|
* @return int
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
|
|
|
public static function insertId($sequence=NULL)
|
|
|
|
{
|
2009-02-26 20:02:14 +00:00
|
|
|
return self::getConnection()->getInsertId($sequence);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins a transaction - Monostate for DibiConnection::begin().
|
2009-06-30 14:08:49 +00:00
|
|
|
* @param string optional savepoint name
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
2008-11-17 16:17:16 +00:00
|
|
|
public static function begin($savepoint = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-11-17 16:17:16 +00:00
|
|
|
self::getConnection()->begin($savepoint);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2008-11-17 16:17:16 +00:00
|
|
|
* Commits statements in a transaction - Monostate for DibiConnection::commit($savepoint = NULL).
|
2009-06-30 14:08:49 +00:00
|
|
|
* @param string optional savepoint name
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
2008-11-17 16:17:16 +00:00
|
|
|
public static function commit($savepoint = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-11-17 16:17:16 +00:00
|
|
|
self::getConnection()->commit($savepoint);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rollback changes in a transaction - Monostate for DibiConnection::rollback().
|
2009-06-30 14:08:49 +00:00
|
|
|
* @param string optional savepoint name
|
2008-07-17 03:51:29 +00:00
|
|
|
* @return void
|
|
|
|
* @throws DibiException
|
|
|
|
*/
|
2008-11-17 16:17:16 +00:00
|
|
|
public static function rollback($savepoint = NULL)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-11-17 16:17:16 +00:00
|
|
|
self::getConnection()->rollback($savepoint);
|
2008-10-02 17:13:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a information about the current database - Monostate for DibiConnection::getDatabaseInfo().
|
|
|
|
* @return DibiDatabaseInfo
|
|
|
|
*/
|
|
|
|
public static function getDatabaseInfo()
|
|
|
|
{
|
|
|
|
return self::getConnection()->getDatabaseInfo();
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replacement for majority of dibi::methods() in future.
|
|
|
|
*/
|
2008-07-22 22:37:14 +00:00
|
|
|
public static function __callStatic($name, $args)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
|
|
|
//if ($name = 'select', 'update', ...') {
|
|
|
|
// return self::command()->$name($args);
|
|
|
|
//}
|
|
|
|
return call_user_func_array(array(self::getConnection(), $name), $args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************* fluent SQL builders ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
|
|
|
public static function command()
|
|
|
|
{
|
2008-09-13 16:38:59 +00:00
|
|
|
return self::getConnection()->command();
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string column name
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
|
|
|
public static function select($args)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
2009-04-16 02:15:20 +00:00
|
|
|
return call_user_func_array(array(self::getConnection(), 'select'), $args);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string table
|
|
|
|
* @param array
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
2009-06-19 11:28:42 +00:00
|
|
|
public static function update($table, $args)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-09-13 16:38:59 +00:00
|
|
|
return self::getConnection()->update($table, $args);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string table
|
|
|
|
* @param array
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
2009-06-19 11:28:42 +00:00
|
|
|
public static function insert($table, $args)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2008-09-13 16:38:59 +00:00
|
|
|
return self::getConnection()->insert($table, $args);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string table
|
|
|
|
* @return DibiFluent
|
|
|
|
*/
|
|
|
|
public static function delete($table)
|
|
|
|
{
|
2008-09-13 16:38:59 +00:00
|
|
|
return self::getConnection()->delete($table);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************* data types ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-17 20:45:30 +01:00
|
|
|
* @deprecated
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
|
|
|
public static function datetime($time = NULL)
|
|
|
|
{
|
2010-10-06 16:02:20 +02:00
|
|
|
return new DibiDateTime(is_numeric($time) ? date('Y-m-d H:i:s', $time) : $time);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-12-17 20:45:30 +01:00
|
|
|
* @deprecated
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
|
|
|
public static function date($date = NULL)
|
|
|
|
{
|
2010-10-06 16:02:20 +02:00
|
|
|
return new DibiDateTime(is_numeric($date) ? date('Y-m-d', $date) : $date);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************* substitutions ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-01-25 17:41:44 +01:00
|
|
|
/**
|
|
|
|
* Returns substitution hashmap - Monostate for DibiConnection::getSubstitutes().
|
|
|
|
* @return DibiLazyStorage
|
|
|
|
*/
|
|
|
|
public static function getSubstitutes()
|
|
|
|
{
|
|
|
|
return self::getConnection()->getSubstitutes();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
/**
|
|
|
|
* Create a new substitution pair for indentifiers.
|
|
|
|
* @param string from
|
|
|
|
* @param string to
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function addSubst($expr, $subst)
|
|
|
|
{
|
2011-01-25 17:41:44 +01:00
|
|
|
self::getSubstitutes()->$expr = $subst;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove substitution pair.
|
|
|
|
* @param mixed from or TRUE
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function removeSubst($expr)
|
|
|
|
{
|
2011-01-25 17:41:44 +01:00
|
|
|
$substitutes = self::getSubstitutes();
|
2008-07-17 03:51:29 +00:00
|
|
|
if ($expr === TRUE) {
|
2011-01-25 17:41:44 +01:00
|
|
|
foreach ($substitutes as $expr => $foo) {
|
|
|
|
unset($substitutes->$expr);
|
|
|
|
}
|
2008-07-17 03:51:29 +00:00
|
|
|
} else {
|
2011-01-25 17:41:44 +01:00
|
|
|
unset($substitutes->$expr);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2009-02-22 18:35:18 +00:00
|
|
|
* Sets substitution fallback handler.
|
|
|
|
* @param callback
|
|
|
|
* @return void
|
2008-07-17 03:51:29 +00:00
|
|
|
*/
|
2009-02-22 18:35:18 +00:00
|
|
|
public static function setSubstFallback($callback)
|
2008-07-17 03:51:29 +00:00
|
|
|
{
|
2011-01-25 17:41:44 +01:00
|
|
|
self::getSubstitutes()->setCallback($callback);
|
2009-03-08 23:27:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +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;
|
|
|
|
|
2010-08-03 03:09:45 +02:00
|
|
|
static $keywords1 = 'SELECT|UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE';
|
2008-07-17 03:51:29 +00:00
|
|
|
static $keywords2 = 'ALL|DISTINCT|DISTINCTROW|AS|USING|ON|AND|OR|IN|IS|NOT|NULL|LIKE|TRUE|FALSE';
|
|
|
|
|
|
|
|
// insert new lines
|
2009-06-19 11:03:24 +00:00
|
|
|
$sql = " $sql ";
|
2008-07-17 03:51:29 +00:00
|
|
|
$sql = preg_replace("#(?<=[\\s,(])($keywords1)(?=[\\s,)])#i", "\n\$1", $sql);
|
|
|
|
|
|
|
|
// reduce spaces
|
|
|
|
$sql = preg_replace('#[ \t]{2,}#', " ", $sql);
|
|
|
|
|
|
|
|
$sql = wordwrap($sql, 100);
|
2010-04-04 02:57:43 +02:00
|
|
|
$sql = preg_replace("#([ \t]*\r?\n){2,}#", "\n", $sql);
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2010-08-03 07:55:54 +02:00
|
|
|
if (PHP_SAPI === 'cli') {
|
|
|
|
echo trim($sql) . "\n\n";
|
|
|
|
} else {
|
|
|
|
// syntax highlight
|
2010-08-27 01:06:05 +02:00
|
|
|
$sql = htmlSpecialChars($sql);
|
2010-08-03 07:55:54 +02:00
|
|
|
$sql = preg_replace_callback("#(/\\*.+?\\*/)|(\\*\\*.+?\\*\\*)|(?<=[\\s,(])($keywords1)(?=[\\s,)])|(?<=[\\s,(=])($keywords2)(?=[\\s,)=])#is", array('dibi', 'highlightCallback'), $sql);
|
|
|
|
echo '<pre class="dump">', trim($sql), "</pre>\n";
|
|
|
|
}
|
2008-07-17 03:51:29 +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>';
|
|
|
|
|
|
|
|
if (!empty($matches[4])) // other keywords
|
|
|
|
return '<strong style="color:green">' . $matches[4] . '</strong>';
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|