1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

Connection: $connected replaced with $driver

This commit is contained in:
David Grudl
2018-05-02 12:40:31 +02:00
parent 51fa3b9086
commit 923fc0f3c3

View File

@@ -34,9 +34,6 @@ class Connection implements IConnection
/** @var Translator|null */ /** @var Translator|null */
private $translator; private $translator;
/** @var bool Is connected? */
private $connected = false;
/** @var HashMap Substitutes for identifiers */ /** @var HashMap Substitutes for identifiers */
private $substitutes; private $substitutes;
@@ -104,7 +101,7 @@ class Connection implements IConnection
*/ */
public function __destruct() public function __destruct()
{ {
if ($this->connected && $this->driver->getResource()) { if ($this->driver && $this->driver->getResource()) {
$this->disconnect(); $this->disconnect();
} }
} }
@@ -128,7 +125,6 @@ class Connection implements IConnection
$event = $this->onEvent ? new Event($this, Event::CONNECT) : null; $event = $this->onEvent ? new Event($this, Event::CONNECT) : null;
try { try {
$this->driver = new $class($this->config); $this->driver = new $class($this->config);
$this->connected = true;
if ($event) { if ($event) {
$this->onEvent($event->done()); $this->onEvent($event->done());
} }
@@ -147,9 +143,9 @@ class Connection implements IConnection
*/ */
final public function disconnect(): void final public function disconnect(): void
{ {
if ($this->connected) { if ($this->driver) {
$this->driver->disconnect(); $this->driver->disconnect();
$this->connected = false; $this->driver = null;
} }
} }
@@ -159,7 +155,7 @@ class Connection implements IConnection
*/ */
final public function isConnected(): bool final public function isConnected(): bool
{ {
return $this->connected; return (bool) $this->driver;
} }
@@ -181,7 +177,7 @@ class Connection implements IConnection
*/ */
final public function getDriver(): Driver final public function getDriver(): Driver
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
return $this->driver; return $this->driver;
@@ -248,7 +244,7 @@ class Connection implements IConnection
*/ */
protected function translateArgs(array $args): string protected function translateArgs(array $args): string
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
if (!$this->translator) { if (!$this->translator) {
@@ -266,7 +262,7 @@ class Connection implements IConnection
*/ */
final public function nativeQuery(string $sql) final public function nativeQuery(string $sql)
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
@@ -301,7 +297,7 @@ class Connection implements IConnection
*/ */
public function getAffectedRows(): int public function getAffectedRows(): int
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
$rows = $this->driver->getAffectedRows(); $rows = $this->driver->getAffectedRows();
@@ -328,7 +324,7 @@ class Connection implements IConnection
*/ */
public function getInsertId(string $sequence = null): int public function getInsertId(string $sequence = null): int
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
$id = $this->driver->getInsertId($sequence); $id = $this->driver->getInsertId($sequence);
@@ -354,7 +350,7 @@ class Connection implements IConnection
*/ */
public function begin(string $savepoint = null): void public function begin(string $savepoint = null): void
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
$event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : null; $event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : null;
@@ -378,7 +374,7 @@ class Connection implements IConnection
*/ */
public function commit(string $savepoint = null): void public function commit(string $savepoint = null): void
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
$event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : null; $event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : null;
@@ -402,7 +398,7 @@ class Connection implements IConnection
*/ */
public function rollback(string $savepoint = null): void public function rollback(string $savepoint = null): void
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
$event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : null; $event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : null;
@@ -572,7 +568,7 @@ class Connection implements IConnection
*/ */
public function getDatabaseInfo(): Reflection\Database public function getDatabaseInfo(): Reflection\Database
{ {
if (!$this->connected) { if (!$this->driver) {
$this->connect(); $this->connect();
} }
return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? null); return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? null);