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

DibiConnection::getDriver() automatically connects lazy connection; connect() and disconnect() are imperative now!

This commit is contained in:
David Grudl
2010-08-03 18:00:23 +02:00
parent 6bb2bc489d
commit dc3b1ff399

View File

@@ -129,7 +129,7 @@ class DibiConnection extends DibiObject
public function __destruct() public function __destruct()
{ {
// disconnects and rolls back transaction - do not rely on auto-disconnect and rollback! // disconnects and rolls back transaction - do not rely on auto-disconnect and rollback!
$this->disconnect(); $this->connected && $this->disconnect();
} }
@@ -140,15 +140,13 @@ class DibiConnection extends DibiObject
*/ */
final public function connect() final public function connect()
{ {
if (!$this->connected) { if ($this->profiler !== NULL) {
if ($this->profiler !== NULL) { $ticket = $this->profiler->before($this, IDibiProfiler::CONNECT);
$ticket = $this->profiler->before($this, IDibiProfiler::CONNECT); }
} $this->driver->connect($this->config);
$this->driver->connect($this->config); $this->connected = TRUE;
$this->connected = TRUE; if (isset($ticket)) {
if (isset($ticket)) { $this->profiler->after($ticket);
$this->profiler->after($ticket);
}
} }
} }
@@ -160,10 +158,8 @@ class DibiConnection extends DibiObject
*/ */
final public function disconnect() final public function disconnect()
{ {
if ($this->connected) { $this->driver->disconnect();
$this->driver->disconnect(); $this->connected = FALSE;
$this->connected = FALSE;
}
} }
@@ -222,11 +218,12 @@ class DibiConnection extends DibiObject
/** /**
* Returns the connection resource. * Returns the driver and connects to a database in lazy mode.
* @return IDibiDriver * @return IDibiDriver
*/ */
final public function getDriver() final public function getDriver()
{ {
$this->connected || $this->connect();
return $this->driver; return $this->driver;
} }
@@ -240,8 +237,8 @@ class DibiConnection extends DibiObject
*/ */
final public function query($args) final public function query($args)
{ {
$this->connected || $this->connect();
$args = func_get_args(); $args = func_get_args();
$this->connect();
return $this->nativeQuery($this->translator->translate($args)); return $this->nativeQuery($this->translator->translate($args));
} }
@@ -255,8 +252,8 @@ class DibiConnection extends DibiObject
*/ */
final public function sql($args) final public function sql($args)
{ {
$this->connected || $this->connect();
$args = func_get_args(); $args = func_get_args();
$this->connect();
return $this->translator->translate($args); return $this->translator->translate($args);
} }
@@ -269,8 +266,8 @@ class DibiConnection extends DibiObject
*/ */
final public function test($args) final public function test($args)
{ {
$this->connected || $this->connect();
$args = func_get_args(); $args = func_get_args();
$this->connect();
try { try {
dibi::dump($this->translator->translate($args)); dibi::dump($this->translator->translate($args));
return TRUE; return TRUE;
@@ -291,8 +288,8 @@ class DibiConnection extends DibiObject
*/ */
final public function dataSource($args) final public function dataSource($args)
{ {
$this->connected || $this->connect();
$args = func_get_args(); $args = func_get_args();
$this->connect();
return new DibiDataSource($this->translator->translate($args), $this); return new DibiDataSource($this->translator->translate($args), $this);
} }
@@ -306,7 +303,7 @@ class DibiConnection extends DibiObject
*/ */
final public function nativeQuery($sql) final public function nativeQuery($sql)
{ {
$this->connect(); $this->connected || $this->connect();
if ($this->profiler !== NULL) { if ($this->profiler !== NULL) {
$event = IDibiProfiler::QUERY; $event = IDibiProfiler::QUERY;
@@ -342,6 +339,7 @@ class DibiConnection extends DibiObject
*/ */
public function getAffectedRows() public function getAffectedRows()
{ {
$this->connected || $this->connect();
$rows = $this->driver->getAffectedRows(); $rows = $this->driver->getAffectedRows();
if (!is_int($rows) || $rows < 0) throw new DibiException('Cannot retrieve number of affected rows.'); if (!is_int($rows) || $rows < 0) throw new DibiException('Cannot retrieve number of affected rows.');
return $rows; return $rows;
@@ -369,6 +367,7 @@ class DibiConnection extends DibiObject
*/ */
public function getInsertId($sequence = NULL) public function getInsertId($sequence = NULL)
{ {
$this->connected || $this->connect();
$id = $this->driver->getInsertId($sequence); $id = $this->driver->getInsertId($sequence);
if ($id < 1) throw new DibiException('Cannot retrieve last generated ID.'); if ($id < 1) throw new DibiException('Cannot retrieve last generated ID.');
return (int) $id; return (int) $id;
@@ -396,7 +395,7 @@ class DibiConnection extends DibiObject
*/ */
public function begin($savepoint = NULL) public function begin($savepoint = NULL)
{ {
$this->connect(); $this->connected || $this->connect();
if ($this->profiler !== NULL) { if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::BEGIN, $savepoint); $ticket = $this->profiler->before($this, IDibiProfiler::BEGIN, $savepoint);
} }
@@ -415,6 +414,7 @@ class DibiConnection extends DibiObject
*/ */
public function commit($savepoint = NULL) public function commit($savepoint = NULL)
{ {
$this->connected || $this->connect();
if ($this->profiler !== NULL) { if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::COMMIT, $savepoint); $ticket = $this->profiler->before($this, IDibiProfiler::COMMIT, $savepoint);
} }
@@ -433,6 +433,7 @@ class DibiConnection extends DibiObject
*/ */
public function rollback($savepoint = NULL) public function rollback($savepoint = NULL)
{ {
$this->connected || $this->connect();
if ($this->profiler !== NULL) { if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::ROLLBACK, $savepoint); $ticket = $this->profiler->before($this, IDibiProfiler::ROLLBACK, $savepoint);
} }
@@ -611,8 +612,7 @@ class DibiConnection extends DibiObject
*/ */
public function loadFile($file) public function loadFile($file)
{ {
$this->connect(); $this->connected || $this->connect();
@set_time_limit(0); // intentionally @ @set_time_limit(0); // intentionally @
$handle = @fopen($file, 'r'); // intentionally @ $handle = @fopen($file, 'r'); // intentionally @
@@ -646,7 +646,7 @@ class DibiConnection extends DibiObject
if (!($this->driver instanceof IDibiReflector)) { if (!($this->driver instanceof IDibiReflector)) {
throw new NotSupportedException('Driver '. get_class($this->driver) . ' has not reflection capabilities.'); throw new NotSupportedException('Driver '. get_class($this->driver) . ' has not reflection capabilities.');
} }
$this->connect(); $this->connected || $this->connect();
return new DibiDatabaseInfo($this->driver, isset($this->config['database']) ? $this->config['database'] : NULL); return new DibiDatabaseInfo($this->driver, isset($this->config['database']) ? $this->config['database'] : NULL);
} }