1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-22 09:53:11 +01: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;
$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) {
}

View File

@ -131,8 +131,9 @@ class Connection implements IConnection
*/
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();
}
}
@ -145,10 +146,14 @@ class Connection implements IConnection
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;
}
}
@ -191,7 +196,9 @@ class Connection implements IConnection
*/
final public function getDriver(): Driver
{
$this->connected || $this->connect();
if (!$this->connected) {
$this->connect();
}
return $this->driver;
}
@ -256,7 +263,9 @@ class Connection implements IConnection
*/
protected function translateArgs(array $args): string
{
$this->connected || $this->connect();
if (!$this->connected) {
$this->connect();
}
if (!$this->translator) {
$this->translator = new Translator($this);
}
@ -272,7 +281,9 @@ class Connection implements IConnection
*/
final public function nativeQuery(string $sql)
{
$this->connected || $this->connect();
if (!$this->connected) {
$this->connect();
}
\dibi::$sql = $sql;
$event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : null;
@ -280,7 +291,9 @@ class Connection implements IConnection
$res = $this->driver->query($sql);
} catch (Exception $e) {
$event && $this->onEvent($event->done($e));
if ($event) {
$this->onEvent($event->done($e));
}
throw $e;
}
@ -290,7 +303,9 @@ class Connection implements IConnection
$res = $this->driver->getAffectedRows();
}
$event && $this->onEvent($event->done($res));
if ($event) {
$this->onEvent($event->done($res));
}
return $res;
}
@ -301,7 +316,9 @@ class Connection implements IConnection
*/
public function getAffectedRows(): int
{
$this->connected || $this->connect();
if (!$this->connected) {
$this->connect();
}
$rows = $this->driver->getAffectedRows();
if ($rows === null || $rows < 0) {
throw new Exception('Cannot retrieve number of affected rows.');
@ -326,7 +343,9 @@ class Connection implements IConnection
*/
public function getInsertId(string $sequence = null): int
{
$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.');
@ -350,14 +369,20 @@ class Connection implements IConnection
*/
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;
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;
}
}
@ -368,14 +393,20 @@ class Connection implements IConnection
*/
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;
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;
}
}
@ -386,14 +417,20 @@ class Connection implements IConnection
*/
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;
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;
}
}
@ -550,7 +587,9 @@ class Connection implements IConnection
*/
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);
}

View File

@ -326,7 +326,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();
}
}

View File

@ -283,7 +283,9 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
*/
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 = [];
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',
@ -105,17 +102,12 @@ 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' => $row[$size_cols[$type]] ?? null,
'nullable' => $row['IS_NULLABLE'] === 'YES',
'default' => $row['COLUMN_DEFAULT'],
'autoincrement' => false,

View File

@ -366,7 +366,9 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
*/
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()
{
$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()
{
$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()
{
$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()
{
$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()
{
$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])];
}
$rc = new \ReflectionClass('dibi');
$dibiDir = dirname($rc->getFileName()) . DIRECTORY_SEPARATOR;
$dibiDir = dirname((new \ReflectionClass('dibi'))->getFileName()) . DIRECTORY_SEPARATOR;
foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $row) {
if (isset($row['file']) && is_file($row['file']) && strpos($row['file'], $dibiDir) !== 0) {
$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 distinct()

View File

@ -25,7 +25,6 @@ interface IDataSource extends \Countable, \IteratorAggregate
*/
interface Driver
{
/**
* Connects to a database.
* @throws Exception
@ -121,7 +120,6 @@ interface Driver
*/
interface ResultDriver
{
/**
* Returns the number of rows in a result set.
*/
@ -170,7 +168,6 @@ interface ResultDriver
*/
interface Reflector
{
/**
* Returns list of tables.
* @return array of {name [, (bool) view ]}