From bc5064cf37d91ce6ccc1364ec14eb6558b914b5b Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 8 Oct 2015 03:46:16 +0200 Subject: [PATCH] alternative: \dibi --- src/Dibi/Bridges/Nette/Panel.php | 4 +- src/Dibi/Bridges/Tracy/Panel.php | 4 +- src/Dibi/Connection.php | 4 +- src/Dibi/Dibi.php | 144 ++++++++++++++-------------- src/Dibi/Drivers/FirebirdDriver.php | 8 +- src/Dibi/Event.php | 10 +- src/Dibi/Fluent.php | 2 +- src/Dibi/Helpers.php | 2 +- src/dibi.php | 5 +- 9 files changed, 90 insertions(+), 93 deletions(-) diff --git a/src/Dibi/Bridges/Nette/Panel.php b/src/Dibi/Bridges/Nette/Panel.php index a27e1f149..cc7e439f0 100644 --- a/src/Dibi/Bridges/Nette/Panel.php +++ b/src/Dibi/Bridges/Nette/Panel.php @@ -107,13 +107,13 @@ public function getPanel() $explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS() if ($this->explain && $event->type === Event::SELECT) { try { - $backup = [$event->connection->onEvent, Dibi\Dibi::$numOfQueries, Dibi\Dibi::$totalTime]; + $backup = [$event->connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime]; $event->connection->onEvent = NULL; $cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN'); $explain = Helpers::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE); } catch (Dibi\Exception $e) { } - list($event->connection->onEvent, Dibi\Dibi::$numOfQueries, Dibi\Dibi::$totalTime) = $backup; + list($event->connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime) = $backup; } $s .= '' . sprintf('%0.3f', $event->time * 1000); diff --git a/src/Dibi/Bridges/Tracy/Panel.php b/src/Dibi/Bridges/Tracy/Panel.php index 0ec0f9439..828c6008a 100644 --- a/src/Dibi/Bridges/Tracy/Panel.php +++ b/src/Dibi/Bridges/Tracy/Panel.php @@ -107,13 +107,13 @@ public function getPanel() $explain = NULL; // EXPLAIN is called here to work SELECT FOUND_ROWS() if ($this->explain && $event->type === Event::SELECT) { try { - $backup = [$event->connection->onEvent, Dibi\Dibi::$numOfQueries, Dibi\Dibi::$totalTime]; + $backup = [$event->connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime]; $event->connection->onEvent = NULL; $cmd = is_string($this->explain) ? $this->explain : ($event->connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN'); $explain = Helpers::dump($event->connection->nativeQuery("$cmd $event->sql"), TRUE); } catch (Dibi\Exception $e) { } - list($event->connection->onEvent, Dibi\Dibi::$numOfQueries, Dibi\Dibi::$totalTime) = $backup; + list($event->connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime) = $backup; } $s .= '' . sprintf('%0.3f', $event->time * 1000); diff --git a/src/Dibi/Connection.php b/src/Dibi/Connection.php index 0ae2deb61..c6c96959d 100644 --- a/src/Dibi/Connection.php +++ b/src/Dibi/Connection.php @@ -76,7 +76,7 @@ public function __construct($config, $name = NULL) self::alias($config, 'result|formatDateTime', 'resultDateTime'); if (!isset($config['driver'])) { - $config['driver'] = Dibi::$defaultDriver; + $config['driver'] = \dibi::$defaultDriver; } $class = $tmp = preg_replace(['#\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($config['driver']))); @@ -313,7 +313,7 @@ final public function nativeQuery($sql) { $this->connected || $this->connect(); - Dibi::$sql = $sql; + \dibi::$sql = $sql; $event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : NULL; try { $res = $this->driver->query($sql); diff --git a/src/Dibi/Dibi.php b/src/Dibi/Dibi.php index 9329ff61f..c589020e3 100644 --- a/src/Dibi/Dibi.php +++ b/src/Dibi/Dibi.php @@ -5,16 +5,14 @@ * Copyright (c) 2005 David Grudl (https://davidgrudl.com) */ -namespace Dibi; - /** * This class is static container class for creating DB objects and * store connections info. */ -class Dibi +class dibi { - use Strict; + use Dibi\Strict; const AFFECTED_ROWS = 'a', @@ -32,27 +30,27 @@ class Dibi /** @deprecated */ const - TEXT = Types::TEXT, - BINARY = Types::BINARY, - BOOL = Types::BOOL, - INTEGER = Types::INTEGER, - FLOAT = Types::FLOAT, - DATE = Types::DATE, - DATETIME = Types::DATETIME, - TIME = Types::TIME, - FIELD_TEXT = Types::TEXT, - FIELD_BINARY = Types::BINARY, - FIELD_BOOL = Types::BOOL, - FIELD_INTEGER = Types::INTEGER, - FIELD_FLOAT = Types::FLOAT, - FIELD_DATE = Types::DATE, - FIELD_DATETIME = Types::DATETIME, - FIELD_TIME = Types::TIME; - - /** @var Connection[] Connection registry storage for DibiConnection objects */ + TEXT = Dibi\Types::TEXT, + BINARY = Dibi\Types::BINARY, + BOOL = Dibi\Types::BOOL, + INTEGER = Dibi\Types::INTEGER, + FLOAT = Dibi\Types::FLOAT, + DATE = Dibi\Types::DATE, + DATETIME = Dibi\Types::DATETIME, + TIME = Dibi\Types::TIME, + FIELD_TEXT = Dibi\Types::TEXT, + FIELD_BINARY = Dibi\Types::BINARY, + FIELD_BOOL = Dibi\Types::BOOL, + FIELD_INTEGER = Dibi\Types::INTEGER, + FIELD_FLOAT = Dibi\Types::FLOAT, + FIELD_DATE = Dibi\Types::DATE, + FIELD_DATETIME = Dibi\Types::DATETIME, + FIELD_TIME = Dibi\Types::TIME; + + /** @var Dibi\Connection[] Connection registry storage for DibiConnection objects */ private static $registry = []; - /** @var Connection Current connection */ + /** @var Dibi\Connection Current connection */ private static $connection; /** @var string Last SQL command @see dibi::query() */ @@ -87,12 +85,12 @@ final public function __construct() * Creates a new Connection object and connects it to specified database. * @param mixed connection parameters * @param string connection name - * @return Connection - * @throws Exception + * @return Dibi\Connection + * @throws Dibi\Exception */ public static function connect($config = [], $name = 0) { - return self::$connection = self::$registry[$name] = new Connection($config, $name); + return self::$connection = self::$registry[$name] = new Dibi\Connection($config, $name); } @@ -119,21 +117,21 @@ public static function isConnected() /** * Retrieve active connection. * @param string connection registy name - * @return Connection - * @throws Exception + * @return Dibi\Connection + * @throws Dibi\Exception */ public static function getConnection($name = NULL) { if ($name === NULL) { if (self::$connection === NULL) { - throw new Exception('Dibi is not connected to database.'); + throw new Dibi\Exception('Dibi is not connected to database.'); } return self::$connection; } if (!isset(self::$registry[$name])) { - throw new Exception("There is no connection named '$name'."); + throw new Dibi\Exception("There is no connection named '$name'."); } return self::$registry[$name]; @@ -142,10 +140,10 @@ public static function getConnection($name = NULL) /** * Sets connection. - * @param Connection - * @return Connection + * @param Dibi\Connection + * @return Dibi\Connection */ - public static function setConnection(Connection $connection) + public static function setConnection(Dibi\Connection $connection) { return self::$connection = $connection; } @@ -165,10 +163,10 @@ public static function activate($name) /** - * Generates and executes SQL query - Monostate for Connection::query(). + * Generates and executes SQL query - Monostate for Dibi\Connection::query(). * @param array|mixed one or more arguments - * @return Result|int result set object (if any) - * @throws Exception + * @return Dibi\Result|int result set object (if any) + * @throws Dibi\Exception */ public static function query($args) { @@ -178,9 +176,9 @@ public static function query($args) /** - * Executes the SQL query - Monostate for Connection::nativeQuery(). + * Executes the SQL query - Monostate for Dibi\Connection::nativeQuery(). * @param string SQL statement. - * @return Result|int result set object (if any) + * @return Dibi\Result|int result set object (if any) */ public static function nativeQuery($sql) { @@ -189,7 +187,7 @@ public static function nativeQuery($sql) /** - * Generates and prints SQL query - Monostate for Connection::test(). + * Generates and prints SQL query - Monostate for Dibi\Connection::test(). * @param array|mixed one or more arguments * @return bool */ @@ -201,9 +199,9 @@ public static function test($args) /** - * Generates and returns SQL query as DataSource - Monostate for Connection::test(). + * Generates and returns SQL query as DataSource - Monostate for Dibi\Connection::test(). * @param array|mixed one or more arguments - * @return DataSource + * @return Dibi\DataSource */ public static function dataSource($args) { @@ -213,10 +211,10 @@ public static function dataSource($args) /** - * Executes SQL query and fetch result - Monostate for Connection::query() & fetch(). + * Executes SQL query and fetch result - Monostate for Dibi\Connection::query() & fetch(). * @param array|mixed one or more arguments - * @return Row - * @throws Exception + * @return Dibi\Row + * @throws Dibi\Exception */ public static function fetch($args) { @@ -226,10 +224,10 @@ public static function fetch($args) /** - * Executes SQL query and fetch results - Monostate for Connection::query() & fetchAll(). + * Executes SQL query and fetch results - Monostate for Dibi\Connection::query() & fetchAll(). * @param array|mixed one or more arguments - * @return Row[] - * @throws Exception + * @return Dibi\Row[] + * @throws Dibi\Exception */ public static function fetchAll($args) { @@ -239,10 +237,10 @@ public static function fetchAll($args) /** - * Executes SQL query and fetch first column - Monostate for Connection::query() & fetchSingle(). + * Executes SQL query and fetch first column - Monostate for Dibi\Connection::query() & fetchSingle(). * @param array|mixed one or more arguments * @return string - * @throws Exception + * @throws Dibi\Exception */ public static function fetchSingle($args) { @@ -252,10 +250,10 @@ public static function fetchSingle($args) /** - * Executes SQL query and fetch pairs - Monostate for Connection::query() & fetchPairs(). + * Executes SQL query and fetch pairs - Monostate for Dibi\Connection::query() & fetchPairs(). * @param array|mixed one or more arguments * @return string - * @throws Exception + * @throws Dibi\Exception */ public static function fetchPairs($args) { @@ -266,9 +264,9 @@ public static function fetchPairs($args) /** * Gets the number of affected rows. - * Monostate for Connection::getAffectedRows() + * Monostate for Dibi\Connection::getAffectedRows() * @return int number of rows - * @throws Exception + * @throws Dibi\Exception */ public static function getAffectedRows() { @@ -279,7 +277,7 @@ public static function getAffectedRows() /** * Gets the number of affected rows. Alias for getAffectedRows(). * @return int number of rows - * @throws Exception + * @throws Dibi\Exception */ public static function affectedRows() { @@ -289,10 +287,10 @@ public static function affectedRows() /** * Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query. - * Monostate for Connection::getInsertId() + * Monostate for Dibi\Connection::getInsertId() * @param string optional sequence name * @return int - * @throws Exception + * @throws Dibi\Exception */ public static function getInsertId($sequence = NULL) { @@ -304,7 +302,7 @@ public static function getInsertId($sequence = NULL) * Retrieves the ID generated for an AUTO_INCREMENT column. Alias for getInsertId(). * @param string optional sequence name * @return int - * @throws Exception + * @throws Dibi\Exception */ public static function insertId($sequence = NULL) { @@ -313,10 +311,10 @@ public static function insertId($sequence = NULL) /** - * Begins a transaction - Monostate for Connection::begin(). + * Begins a transaction - Monostate for Dibi\Connection::begin(). * @param string optional savepoint name * @return void - * @throws Exception + * @throws Dibi\Exception */ public static function begin($savepoint = NULL) { @@ -325,10 +323,10 @@ public static function begin($savepoint = NULL) /** - * Commits statements in a transaction - Monostate for Connection::commit($savepoint = NULL). + * Commits statements in a transaction - Monostate for Dibi\Connection::commit($savepoint = NULL). * @param string optional savepoint name * @return void - * @throws Exception + * @throws Dibi\Exception */ public static function commit($savepoint = NULL) { @@ -337,10 +335,10 @@ public static function commit($savepoint = NULL) /** - * Rollback changes in a transaction - Monostate for Connection::rollback(). + * Rollback changes in a transaction - Monostate for Dibi\Connection::rollback(). * @param string optional savepoint name * @return void - * @throws Exception + * @throws Dibi\Exception */ public static function rollback($savepoint = NULL) { @@ -349,8 +347,8 @@ public static function rollback($savepoint = NULL) /** - * Gets a information about the current database - Monostate for Connection::getDatabaseInfo(). - * @return Reflection\Database + * Gets a information about the current database - Monostate for Dibi\Connection::getDatabaseInfo(). + * @return Dibi\Reflection\Database */ public static function getDatabaseInfo() { @@ -385,7 +383,7 @@ public static function __callStatic($name, $args) /** - * @return Fluent + * @return Dibi\Fluent */ public static function command() { @@ -395,7 +393,7 @@ public static function command() /** * @param string column name - * @return Fluent + * @return Dibi\Fluent */ public static function select($args) { @@ -407,7 +405,7 @@ public static function select($args) /** * @param string table * @param array - * @return Fluent + * @return Dibi\Fluent */ public static function update($table, $args) { @@ -418,7 +416,7 @@ public static function update($table, $args) /** * @param string table * @param array - * @return Fluent + * @return Dibi\Fluent */ public static function insert($table, $args) { @@ -428,7 +426,7 @@ public static function insert($table, $args) /** * @param string table - * @return Fluent + * @return Dibi\Fluent */ public static function delete($table) { @@ -440,8 +438,8 @@ public static function delete($table) /** - * Returns substitution hashmap - Monostate for Connection::getSubstitutes(). - * @return HashMap + * Returns substitution hashmap - Monostate for Dibi\Connection::getSubstitutes(). + * @return Dibi\HashMap */ public static function getSubstitutes() { @@ -460,7 +458,7 @@ public static function getSubstitutes() */ public static function dump($sql = NULL, $return = FALSE) { - return Helpers::dump($sql, $return); + return Dibi\Helpers::dump($sql, $return); } } diff --git a/src/Dibi/Drivers/FirebirdDriver.php b/src/Dibi/Drivers/FirebirdDriver.php index 127f51b64..1c40256c0 100644 --- a/src/Dibi/Drivers/FirebirdDriver.php +++ b/src/Dibi/Drivers/FirebirdDriver.php @@ -119,10 +119,10 @@ public function query($sql) if (Dibi\DriverException::catchError($msg)) { if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) { preg_match('/exception (\d+) (\w+) (.*)/i', ibase_errmsg(), $match); - throw new Dibi\ProcedureException($match[3], $match[1], $match[2], Dibi\Dibi::$sql); + throw new Dibi\ProcedureException($match[3], $match[1], $match[2], \dibi::$sql); } else { - throw new Dibi\DriverException(ibase_errmsg(), ibase_errcode(), Dibi\Dibi::$sql); + throw new Dibi\DriverException(ibase_errmsg(), ibase_errcode(), \dibi::$sql); } } @@ -384,10 +384,10 @@ public function fetch($assoc) if (Dibi\DriverException::catchError($msg)) { if (ibase_errcode() == self::ERROR_EXCEPTION_THROWN) { preg_match('/exception (\d+) (\w+) (.*)/is', ibase_errmsg(), $match); - throw new Dibi\ProcedureException($match[3], $match[1], $match[2], Dibi\Dibi::$sql); + throw new Dibi\ProcedureException($match[3], $match[1], $match[2], \dibi::$sql); } else { - throw new Dibi\DriverException($msg, ibase_errcode(), Dibi\Dibi::$sql); + throw new Dibi\DriverException($msg, ibase_errcode(), \dibi::$sql); } } diff --git a/src/Dibi/Event.php b/src/Dibi/Event.php index b72a9cef3..156b1ee9c 100644 --- a/src/Dibi/Event.php +++ b/src/Dibi/Event.php @@ -74,9 +74,9 @@ public function __construct(Connection $connection, $type, $sql = NULL) } } - Dibi::$elapsedTime = FALSE; - Dibi::$numOfQueries++; - Dibi::$sql = $sql; + \dibi::$elapsedTime = FALSE; + \dibi::$numOfQueries++; + \dibi::$sql = $sql; } @@ -90,8 +90,8 @@ public function done($result = NULL) } $this->time += microtime(TRUE); - Dibi::$elapsedTime = $this->time; - Dibi::$totalTime += $this->time; + \dibi::$elapsedTime = $this->time; + \dibi::$totalTime += $this->time; return $this; } diff --git a/src/Dibi/Fluent.php b/src/Dibi/Fluent.php index 046fdc32e..6dfd76b9b 100644 --- a/src/Dibi/Fluent.php +++ b/src/Dibi/Fluent.php @@ -303,7 +303,7 @@ public function execute($return = NULL) switch ($return) { case Dibi::IDENTIFIER: return $this->connection->getInsertId(); - case Dibi::AFFECTED_ROWS: + case \dibi::AFFECTED_ROWS: return $this->connection->getAffectedRows(); default: return $res; diff --git a/src/Dibi/Helpers.php b/src/Dibi/Helpers.php index 7286f4d76..b8b873f8e 100644 --- a/src/Dibi/Helpers.php +++ b/src/Dibi/Helpers.php @@ -26,7 +26,7 @@ public static function dump($sql = NULL, $return = FALSE) } else { if ($sql === NULL) { - $sql = Dibi::$sql; + $sql = \dibi::$sql; } static $keywords1 = 'SELECT|(?:ON\s+DUPLICATE\s+KEY)?UPDATE|INSERT(?:\s+INTO)?|REPLACE(?:\s+INTO)?|DELETE|CALL|UNION|FROM|WHERE|HAVING|GROUP\s+BY|ORDER\s+BY|LIMIT|OFFSET|FETCH\s+NEXT|SET|VALUES|LEFT\s+JOIN|INNER\s+JOIN|TRUNCATE|START\s+TRANSACTION|BEGIN|COMMIT|ROLLBACK(?:\s+TO\s+SAVEPOINT)?|(?:RELEASE\s+)?SAVEPOINT'; diff --git a/src/dibi.php b/src/dibi.php index efdb0c09e..5c89b95e7 100644 --- a/src/dibi.php +++ b/src/dibi.php @@ -14,6 +14,8 @@ spl_autoload_register(function ($class) { static $map = [ + 'dibi' => 'Dibi.php', + 'Dibi' => 'Dibi.php', 'Dibi\Bridges\Nette\DibiExtension22' => 'Bridges/Nette/DibiExtension22.php', 'Dibi\Bridges\Nette\DibiExtension21' => 'Bridges/Nette/DibiExtension21.php', 'Dibi\Bridges\Nette\Panel' => 'Bridges/Nette/Panel.php', @@ -21,7 +23,6 @@ 'Dibi\Connection' => 'Connection.php', 'Dibi\DataSource' => 'DataSource.php', 'Dibi\DateTime' => 'DateTime.php', - 'Dibi\Dibi' => 'Dibi.php', 'Dibi\Driver' => 'interfaces.php', 'Dibi\DriverException' => 'exceptions.php', 'Dibi\Drivers\FirebirdDriver' => 'Drivers/FirebirdDriver.php', @@ -67,8 +68,6 @@ 'Dibi\Translator' => 'Translator.php', 'Dibi\Types' => 'Types.php', ], $old2new = [ - 'dibi' => 'Dibi\Dibi', - 'Dibi' => 'Dibi\Dibi', 'DibiColumnInfo' => 'Dibi\Reflection\Column', 'DibiConnection' => 'Dibi\Connection', 'DibiDatabaseInfo' => 'Dibi\Reflection\Database',