mirror of
https://github.com/dg/dibi.git
synced 2025-08-07 14:46:50 +02:00
refactoring
This commit is contained in:
@@ -110,10 +110,10 @@ class Panel implements Tracy\IBarPanel
|
||||
$connection = $event->connection;
|
||||
$explain = null; // EXPLAIN is called here to work SELECT FOUND_ROWS()
|
||||
if ($this->explain && $event->type === Event::SELECT) {
|
||||
$backup = [$connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime];
|
||||
$connection->onEvent = null;
|
||||
$cmd = is_string($this->explain) ? $this->explain : ($connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
|
||||
try {
|
||||
$backup = [$connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime];
|
||||
$connection->onEvent = null;
|
||||
$cmd = is_string($this->explain) ? $this->explain : ($connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
|
||||
$explain = @Helpers::dump($connection->nativeQuery("$cmd $event->sql"), true);
|
||||
} catch (Dibi\Exception $e) {
|
||||
}
|
||||
|
@@ -131,8 +131,9 @@ class Connection
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
// disconnects and rolls back transaction - do not rely on auto-disconnect and rollback!
|
||||
$this->connected && $this->driver->getResource() && $this->disconnect();
|
||||
if ($this->connected && $this->driver->getResource()) {
|
||||
$this->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -146,10 +147,14 @@ class Connection
|
||||
try {
|
||||
$this->driver->connect($this->config);
|
||||
$this->connected = true;
|
||||
$event && $this->onEvent($event->done());
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
$event && $this->onEvent($event->done($e));
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@@ -211,7 +216,9 @@ class Connection
|
||||
*/
|
||||
final public function getDriver()
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
@@ -285,7 +292,9 @@ class Connection
|
||||
*/
|
||||
protected function translateArgs($args)
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
if (!$this->translator) {
|
||||
$this->translator = new Translator($this);
|
||||
}
|
||||
@@ -302,7 +311,9 @@ class Connection
|
||||
*/
|
||||
final public function nativeQuery($sql)
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
\dibi::$sql = $sql;
|
||||
$event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : null;
|
||||
@@ -310,7 +321,9 @@ class Connection
|
||||
$res = $this->driver->query($sql);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$event && $this->onEvent($event->done($e));
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
@@ -320,7 +333,9 @@ class Connection
|
||||
$res = $this->driver->getAffectedRows();
|
||||
}
|
||||
|
||||
$event && $this->onEvent($event->done($res));
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($res));
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
@@ -332,7 +347,9 @@ class Connection
|
||||
*/
|
||||
public function getAffectedRows()
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
$rows = $this->driver->getAffectedRows();
|
||||
if (!is_int($rows) || $rows < 0) {
|
||||
throw new Exception('Cannot retrieve number of affected rows.');
|
||||
@@ -359,7 +376,9 @@ class Connection
|
||||
*/
|
||||
public function getInsertId($sequence = null)
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
$id = $this->driver->getInsertId($sequence);
|
||||
if ($id < 1) {
|
||||
throw new Exception('Cannot retrieve last generated ID.');
|
||||
@@ -385,14 +404,20 @@ class Connection
|
||||
*/
|
||||
public function begin($savepoint = null)
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
$event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : null;
|
||||
try {
|
||||
$this->driver->begin($savepoint);
|
||||
$event && $this->onEvent($event->done());
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
$event && $this->onEvent($event->done($e));
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@@ -405,14 +430,20 @@ class Connection
|
||||
*/
|
||||
public function commit($savepoint = null)
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
$event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : null;
|
||||
try {
|
||||
$this->driver->commit($savepoint);
|
||||
$event && $this->onEvent($event->done());
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
$event && $this->onEvent($event->done($e));
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@@ -425,14 +456,20 @@ class Connection
|
||||
*/
|
||||
public function rollback($savepoint = null)
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
$event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : null;
|
||||
try {
|
||||
$this->driver->rollback($savepoint);
|
||||
$event && $this->onEvent($event->done());
|
||||
if ($event) {
|
||||
$this->onEvent($event->done());
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
$event && $this->onEvent($event->done($e));
|
||||
if ($event) {
|
||||
$this->onEvent($event->done($e));
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
@@ -625,7 +662,9 @@ class Connection
|
||||
*/
|
||||
public function getDatabaseInfo()
|
||||
{
|
||||
$this->connected || $this->connect();
|
||||
if (!$this->connected) {
|
||||
$this->connect();
|
||||
}
|
||||
return new Reflection\Database($this->driver->getReflector(), isset($this->config['database']) ? $this->config['database'] : null);
|
||||
}
|
||||
|
||||
|
@@ -375,7 +375,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->autoFree && $this->getResultResource() && $this->free();
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -329,7 +329,9 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->autoFree && $this->getResultResource() && $this->free();
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -96,10 +96,7 @@ class MsSqlReflector implements Dibi\Reflector
|
||||
");
|
||||
$columns = [];
|
||||
while ($row = $res->fetch(true)) {
|
||||
$size = false;
|
||||
$type = strtoupper($row['DATA_TYPE']);
|
||||
|
||||
$size_cols = [
|
||||
static $size_cols = [
|
||||
'DATETIME' => 'DATETIME_PRECISION',
|
||||
'DECIMAL' => 'NUMERIC_PRECISION',
|
||||
'CHAR' => 'CHARACTER_MAXIMUM_LENGTH',
|
||||
@@ -108,17 +105,13 @@ class MsSqlReflector implements Dibi\Reflector
|
||||
'VARCHAR' => 'CHARACTER_OCTET_LENGTH',
|
||||
];
|
||||
|
||||
if (isset($size_cols[$type])) {
|
||||
if ($size_cols[$type]) {
|
||||
$size = $row[$size_cols[$type]];
|
||||
}
|
||||
}
|
||||
$type = strtoupper($row['DATA_TYPE']);
|
||||
|
||||
$columns[] = [
|
||||
'name' => $row['COLUMN_NAME'],
|
||||
'table' => $table,
|
||||
'nativetype' => $type,
|
||||
'size' => $size,
|
||||
'size' => isset($size_cols[$type], $row[$size_cols[$type]]) ? $row[$size_cols[$type]] : null,
|
||||
'unsigned' => null,
|
||||
'nullable' => $row['IS_NULLABLE'] === 'YES',
|
||||
'default' => $row['COLUMN_DEFAULT'],
|
||||
|
@@ -417,7 +417,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->autoFree && $this->getResultResource() && @$this->free();
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
@$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -353,7 +353,9 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->autoFree && $this->getResultResource() && $this->free();
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -392,7 +392,9 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->autoFree && $this->getResultResource() && $this->free();
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -429,7 +429,9 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->autoFree && $this->getResultResource() && $this->free();
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -375,7 +375,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->autoFree && $this->resultSet && @$this->free();
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
@$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -361,7 +361,9 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->autoFree && $this->getResultResource() && $this->free();
|
||||
if ($this->autoFree && $this->getResultResource()) {
|
||||
$this->free();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@@ -9,7 +9,7 @@ namespace Dibi;
|
||||
|
||||
|
||||
/**
|
||||
* dibi SQL builder via fluent interfaces. EXPERIMENTAL!
|
||||
* dibi SQL builder via fluent interfaces.
|
||||
*
|
||||
* @method Fluent select(...$field)
|
||||
* @method Fluent distinct()
|
||||
|
@@ -23,7 +23,6 @@ interface IDataSource extends \Countable, \IteratorAggregate
|
||||
*/
|
||||
interface Driver
|
||||
{
|
||||
|
||||
/**
|
||||
* Connects to a database.
|
||||
* @param array
|
||||
@@ -156,7 +155,6 @@ interface Driver
|
||||
*/
|
||||
interface ResultDriver
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns the number of rows in a result set.
|
||||
* @return int
|
||||
@@ -212,7 +210,6 @@ interface ResultDriver
|
||||
*/
|
||||
interface Reflector
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns list of tables.
|
||||
* @return array of {name [, (bool) view ]}
|
||||
|
Reference in New Issue
Block a user