2008-07-17 03:51:29 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* dibi - tiny'n'smart database abstraction layer
|
|
|
|
* ----------------------------------------------
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* This source file is subject to the "dibi license" that is bundled
|
2010-01-03 15:15:37 +01:00
|
|
|
* with this package in the file license.txt, and/or GPL license.
|
2008-07-17 03:51:29 +00:00
|
|
|
*
|
|
|
|
* For more information please see http://dibiphp.com
|
|
|
|
*
|
2010-01-03 15:15:37 +01:00
|
|
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
2008-07-17 03:51:29 +00:00
|
|
|
* @license http://dibiphp.com/license dibi license
|
|
|
|
* @link http://dibiphp.com
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
if (!class_exists('NotImplementedException', FALSE)) {
|
|
|
|
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-11-17 16:17:16 +00:00
|
|
|
if (!interface_exists(/*Nette\*/'IDebuggable', FALSE)) {
|
2008-07-17 03:51:29 +00:00
|
|
|
require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
|
|
|
|
}
|
|
|
|
|
2009-11-16 01:44:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Back-compatibility
|
|
|
|
*/
|
|
|
|
class DibiVariable extends DateTime
|
|
|
|
{
|
|
|
|
function __construct($val)
|
|
|
|
{
|
|
|
|
parent::__construct($val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
// dibi libraries
|
|
|
|
require_once dirname(__FILE__) . '/libs/interfaces.php';
|
2008-09-05 05:35:15 +00:00
|
|
|
require_once dirname(__FILE__) . '/libs/DibiObject.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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interface for database drivers.
|
|
|
|
*
|
|
|
|
* This class is static container class for creating DB objects and
|
|
|
|
* store connections info.
|
|
|
|
*
|
2010-01-03 15:15:37 +01:00
|
|
|
* @copyright Copyright (c) 2005, 2010 David Grudl
|
2008-07-17 03:51:29 +00:00
|
|
|
* @package dibi
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**#@+
|
2008-10-10 01:35:33 +00:00
|
|
|
* Configuration options
|
|
|
|
*/
|
2008-10-28 15:24:47 +00:00
|
|
|
const RESULT_WITH_TABLES = 'resultWithTables'; // for MySQL
|
2009-02-05 01:26:08 +00:00
|
|
|
const ROW_CLASS = 'rowClass';
|
|
|
|
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 Substitutions for identifiers */
|
2009-02-22 18:35:18 +00:00
|
|
|
public static $substs = array();
|
2008-07-17 03:51:29 +00:00
|
|
|
|
2008-10-28 15:24:47 +00:00
|
|
|
/** @var callback Substitution fallback */
|
2009-03-08 23:27:31 +00:00
|
|
|
public static $substFallBack = array(__CLASS__, 'defaultSubstFallback');
|
2008-07-17 03:51:29 +00:00
|
|
|
|
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.
|
2008-08-25 18:55:50 +00:00
|
|
|
* @param array|string|ArrayObject connection parameters
|
2008-07-17 03:51:29 +00:00
|
|
|
* @param string connection name
|
|
|
|
* @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];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2009-11-16 01:44:10 +01:00
|
|
|
return new DateTime(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)
|
|
|
|
{
|
2009-11-16 01:44:10 +01:00
|
|
|
return new DateTime(is_numeric($date) ? date('Y-m-d', $date) : $date);
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********************* substitutions ****************d*g**/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new substitution pair for indentifiers.
|
|
|
|
* @param string from
|
|
|
|
* @param string to
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function addSubst($expr, $subst)
|
|
|
|
{
|
|
|
|
self::$substs[$expr] = $subst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.':']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
{
|
2009-02-22 18:35:18 +00:00
|
|
|
if (!is_callable($callback)) {
|
2009-04-16 02:15:20 +00:00
|
|
|
$able = is_callable($callback, TRUE, $textual);
|
|
|
|
throw new InvalidArgumentException("Handler '$textual' is not " . ($able ? 'callable.' : 'valid PHP callback.'));
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
2009-02-22 18:35:18 +00:00
|
|
|
self::$substFallBack = $callback;
|
2008-07-17 03:51:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-03-08 23:27:31 +00:00
|
|
|
/**
|
|
|
|
* Default substitution fallback handler.
|
|
|
|
* @param string
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function defaultSubstFallback($expr)
|
|
|
|
{
|
|
|
|
throw new InvalidStateException("Missing substitution for '$expr' expression.");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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';
|
|
|
|
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);
|
|
|
|
$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";
|
|
|
|
}
|
|
|
|
|
|
|
|
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>';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns brief descriptions.
|
|
|
|
* @return string
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getColophon($sender = NULL)
|
|
|
|
{
|
|
|
|
$arr = array(
|
|
|
|
'Number of SQL queries: ' . dibi::$numOfQueries
|
|
|
|
. (dibi::$totalTime === NULL ? '' : ', elapsed time: ' . sprintf('%0.3f', dibi::$totalTime * 1000) . ' ms'),
|
|
|
|
);
|
|
|
|
if ($sender === 'bluescreen') {
|
|
|
|
$arr[] = 'dibi ' . dibi::VERSION . ' (revision ' . dibi::REVISION . ')';
|
|
|
|
}
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|