1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-12 10:06:25 +02:00

refactoring

This commit is contained in:
David Grudl
2018-04-17 11:57:49 +02:00
parent 5a2332899b
commit 045e45a8c6
14 changed files with 92 additions and 49 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) {
try {
$backup = [$connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime]; $backup = [$connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime];
$connection->onEvent = null; $connection->onEvent = null;
$cmd = is_string($this->explain) ? $this->explain : ($connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN'); $cmd = is_string($this->explain) ? $this->explain : ($connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
try {
$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 implements IConnection
*/ */
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();
}
} }
@ -145,10 +146,14 @@ class Connection implements IConnection
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;
} }
} }
@ -191,7 +196,9 @@ class Connection implements IConnection
*/ */
final public function getDriver(): Driver final public function getDriver(): Driver
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
return $this->driver; return $this->driver;
} }
@ -256,7 +263,9 @@ class Connection implements IConnection
*/ */
protected function translateArgs(array $args): string protected function translateArgs(array $args): string
{ {
$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);
} }
@ -272,7 +281,9 @@ class Connection implements IConnection
*/ */
final public function nativeQuery(string $sql) final public function nativeQuery(string $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;
@ -280,7 +291,9 @@ class Connection implements IConnection
$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;
} }
@ -290,7 +303,9 @@ class Connection implements IConnection
$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;
} }
@ -301,7 +316,9 @@ class Connection implements IConnection
*/ */
public function getAffectedRows(): int public function getAffectedRows(): int
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
$rows = $this->driver->getAffectedRows(); $rows = $this->driver->getAffectedRows();
if ($rows === null || $rows < 0) { if ($rows === null || $rows < 0) {
throw new Exception('Cannot retrieve number of affected rows.'); throw new Exception('Cannot retrieve number of affected rows.');
@ -326,7 +343,9 @@ class Connection implements IConnection
*/ */
public function getInsertId(string $sequence = null): int public function getInsertId(string $sequence = null): int
{ {
$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.');
@ -350,14 +369,20 @@ class Connection implements IConnection
*/ */
public function begin(string $savepoint = null): void public function begin(string $savepoint = null): void
{ {
$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;
} }
} }
@ -368,14 +393,20 @@ class Connection implements IConnection
*/ */
public function commit(string $savepoint = null): void public function commit(string $savepoint = null): void
{ {
$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;
} }
} }
@ -386,14 +417,20 @@ class Connection implements IConnection
*/ */
public function rollback(string $savepoint = null): void public function rollback(string $savepoint = null): void
{ {
$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;
} }
} }
@ -550,7 +587,9 @@ class Connection implements IConnection
*/ */
public function getDatabaseInfo(): Reflection\Database public function getDatabaseInfo(): Reflection\Database
{ {
$this->connected || $this->connect(); if (!$this->connected) {
$this->connect();
}
return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? null); return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? null);
} }

View File

@ -326,7 +326,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

@ -283,7 +283,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

@ -93,10 +93,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',
@ -105,17 +102,12 @@ 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' => $row[$size_cols[$type]] ?? null,
'nullable' => $row['IS_NULLABLE'] === 'YES', 'nullable' => $row['IS_NULLABLE'] === 'YES',
'default' => $row['COLUMN_DEFAULT'], 'default' => $row['COLUMN_DEFAULT'],
'autoincrement' => false, 'autoincrement' => false,

View File

@ -366,7 +366,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

@ -306,7 +306,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

@ -338,7 +338,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

@ -379,7 +379,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

@ -324,7 +324,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

@ -313,7 +313,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

@ -68,8 +68,7 @@ class Event
$this->type = $types[strtoupper($matches[1])]; $this->type = $types[strtoupper($matches[1])];
} }
$rc = new \ReflectionClass('dibi'); $dibiDir = dirname((new \ReflectionClass('dibi'))->getFileName()) . DIRECTORY_SEPARATOR;
$dibiDir = dirname($rc->getFileName()) . DIRECTORY_SEPARATOR;
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) { foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) {
if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) { if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
$this->source = [$row['file'], (int) $row['line']]; $this->source = [$row['file'], (int) $row['line']];

View File

@ -11,7 +11,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

@ -25,7 +25,6 @@ interface IDataSource extends \Countable, \IteratorAggregate
*/ */
interface Driver interface Driver
{ {
/** /**
* Connects to a database. * Connects to a database.
* @throws Exception * @throws Exception
@ -121,7 +120,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.
*/ */
@ -170,7 +168,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 ]}