1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-07 14:46:50 +02:00

refactoring

This commit is contained in:
David Grudl
2018-04-17 11:57:49 +02:00
parent 0d5fd9d65b
commit 8985c71276
13 changed files with 91 additions and 46 deletions

View File

@@ -110,10 +110,10 @@ class Panel implements Tracy\IBarPanel
$connection = $event->connection; $connection = $event->connection;
$explain = null; // EXPLAIN is called here to work SELECT FOUND_ROWS() $explain = null; // EXPLAIN is called here to work SELECT FOUND_ROWS()
if ($this->explain && $event->type === Event::SELECT) { 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 { 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); $explain = @Helpers::dump($connection->nativeQuery("$cmd $event->sql"), true);
} catch (Dibi\Exception $e) { } catch (Dibi\Exception $e) {
} }

View File

@@ -131,8 +131,9 @@ class Connection
*/ */
public function __destruct() public function __destruct()
{ {
// disconnects and rolls back transaction - do not rely on auto-disconnect and rollback! if ($this->connected && $this->driver->getResource()) {
$this->connected && $this->driver->getResource() && $this->disconnect(); $this->disconnect();
}
} }
@@ -146,10 +147,14 @@ class Connection
try { try {
$this->driver->connect($this->config); $this->driver->connect($this->config);
$this->connected = true; $this->connected = true;
$event && $this->onEvent($event->done()); if ($event) {
$this->onEvent($event->done());
}
} catch (Exception $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); if ($event) {
$this->onEvent($event->done($e));
}
throw $e; throw $e;
} }
} }
@@ -211,7 +216,9 @@ class Connection
*/ */
final public function getDriver() final public function getDriver()
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
return $this->driver; return $this->driver;
} }
@@ -285,7 +292,9 @@ class Connection
*/ */
protected function translateArgs($args) protected function translateArgs($args)
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
if (!$this->translator) { if (!$this->translator) {
$this->translator = new Translator($this); $this->translator = new Translator($this);
} }
@@ -302,7 +311,9 @@ class Connection
*/ */
final public function nativeQuery($sql) final public function nativeQuery($sql)
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
\dibi::$sql = $sql; \dibi::$sql = $sql;
$event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : null; $event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : null;
@@ -310,7 +321,9 @@ class Connection
$res = $this->driver->query($sql); $res = $this->driver->query($sql);
} catch (Exception $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); if ($event) {
$this->onEvent($event->done($e));
}
throw $e; throw $e;
} }
@@ -320,7 +333,9 @@ class Connection
$res = $this->driver->getAffectedRows(); $res = $this->driver->getAffectedRows();
} }
$event && $this->onEvent($event->done($res)); if ($event) {
$this->onEvent($event->done($res));
}
return $res; return $res;
} }
@@ -332,7 +347,9 @@ class Connection
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
$rows = $this->driver->getAffectedRows(); $rows = $this->driver->getAffectedRows();
if (!is_int($rows) || $rows < 0) { if (!is_int($rows) || $rows < 0) {
throw new Exception('Cannot retrieve number of affected rows.'); throw new Exception('Cannot retrieve number of affected rows.');
@@ -359,7 +376,9 @@ class Connection
*/ */
public function getInsertId($sequence = null) public function getInsertId($sequence = null)
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
$id = $this->driver->getInsertId($sequence); $id = $this->driver->getInsertId($sequence);
if ($id < 1) { if ($id < 1) {
throw new Exception('Cannot retrieve last generated ID.'); throw new Exception('Cannot retrieve last generated ID.');
@@ -385,14 +404,20 @@ class Connection
*/ */
public function begin($savepoint = null) public function begin($savepoint = null)
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
$event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : null; $event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : null;
try { try {
$this->driver->begin($savepoint); $this->driver->begin($savepoint);
$event && $this->onEvent($event->done()); if ($event) {
$this->onEvent($event->done());
}
} catch (Exception $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); if ($event) {
$this->onEvent($event->done($e));
}
throw $e; throw $e;
} }
} }
@@ -405,14 +430,20 @@ class Connection
*/ */
public function commit($savepoint = null) public function commit($savepoint = null)
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
$event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : null; $event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : null;
try { try {
$this->driver->commit($savepoint); $this->driver->commit($savepoint);
$event && $this->onEvent($event->done()); if ($event) {
$this->onEvent($event->done());
}
} catch (Exception $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); if ($event) {
$this->onEvent($event->done($e));
}
throw $e; throw $e;
} }
} }
@@ -425,14 +456,20 @@ class Connection
*/ */
public function rollback($savepoint = null) public function rollback($savepoint = null)
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
$event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : null; $event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : null;
try { try {
$this->driver->rollback($savepoint); $this->driver->rollback($savepoint);
$event && $this->onEvent($event->done()); if ($event) {
$this->onEvent($event->done());
}
} catch (Exception $e) { } catch (Exception $e) {
$event && $this->onEvent($event->done($e)); if ($event) {
$this->onEvent($event->done($e));
}
throw $e; throw $e;
} }
} }
@@ -625,7 +662,9 @@ class Connection
*/ */
public function getDatabaseInfo() 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); return new Reflection\Database($this->driver->getReflector(), isset($this->config['database']) ? $this->config['database'] : null);
} }

View File

@@ -375,7 +375,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->getResultResource() && $this->free(); if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
} }

View File

@@ -329,7 +329,9 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->getResultResource() && $this->free(); if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
} }

View File

@@ -96,10 +96,7 @@ class MsSqlReflector implements Dibi\Reflector
"); ");
$columns = []; $columns = [];
while ($row = $res->fetch(true)) { while ($row = $res->fetch(true)) {
$size = false; static $size_cols = [
$type = strtoupper($row['DATA_TYPE']);
$size_cols = [
'DATETIME' => 'DATETIME_PRECISION', 'DATETIME' => 'DATETIME_PRECISION',
'DECIMAL' => 'NUMERIC_PRECISION', 'DECIMAL' => 'NUMERIC_PRECISION',
'CHAR' => 'CHARACTER_MAXIMUM_LENGTH', 'CHAR' => 'CHARACTER_MAXIMUM_LENGTH',
@@ -108,17 +105,13 @@ class MsSqlReflector implements Dibi\Reflector
'VARCHAR' => 'CHARACTER_OCTET_LENGTH', 'VARCHAR' => 'CHARACTER_OCTET_LENGTH',
]; ];
if (isset($size_cols[$type])) { $type = strtoupper($row['DATA_TYPE']);
if ($size_cols[$type]) {
$size = $row[$size_cols[$type]];
}
}
$columns[] = [ $columns[] = [
'name' => $row['COLUMN_NAME'], 'name' => $row['COLUMN_NAME'],
'table' => $table, 'table' => $table,
'nativetype' => $type, 'nativetype' => $type,
'size' => $size, 'size' => isset($size_cols[$type], $row[$size_cols[$type]]) ? $row[$size_cols[$type]] : null,
'unsigned' => null, 'unsigned' => null,
'nullable' => $row['IS_NULLABLE'] === 'YES', 'nullable' => $row['IS_NULLABLE'] === 'YES',
'default' => $row['COLUMN_DEFAULT'], 'default' => $row['COLUMN_DEFAULT'],

View File

@@ -417,7 +417,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->getResultResource() && @$this->free(); if ($this->autoFree && $this->getResultResource()) {
@$this->free();
}
} }

View File

@@ -353,7 +353,9 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->getResultResource() && $this->free(); if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
} }

View File

@@ -392,7 +392,9 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->getResultResource() && $this->free(); if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
} }

View File

@@ -429,7 +429,9 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->getResultResource() && $this->free(); if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
} }

View File

@@ -375,7 +375,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->resultSet && @$this->free(); if ($this->autoFree && $this->getResultResource()) {
@$this->free();
}
} }

View File

@@ -361,7 +361,9 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->getResultResource() && $this->free(); if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
} }

View File

@@ -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 select(...$field)
* @method Fluent distinct() * @method Fluent distinct()

View File

@@ -23,7 +23,6 @@ interface IDataSource extends \Countable, \IteratorAggregate
*/ */
interface Driver interface Driver
{ {
/** /**
* Connects to a database. * Connects to a database.
* @param array * @param array
@@ -156,7 +155,6 @@ interface Driver
*/ */
interface ResultDriver interface ResultDriver
{ {
/** /**
* Returns the number of rows in a result set. * Returns the number of rows in a result set.
* @return int * @return int
@@ -212,7 +210,6 @@ interface ResultDriver
*/ */
interface Reflector interface Reflector
{ {
/** /**
* Returns list of tables. * Returns list of tables.
* @return array of {name [, (bool) view ]} * @return array of {name [, (bool) view ]}