mirror of
https://github.com/dg/dibi.git
synced 2025-08-12 09:04:24 +02:00
added DibiDriver::disconnect()
This commit is contained in:
@@ -64,19 +64,48 @@ abstract class DibiDriver extends NObject
|
||||
$this->config = $config;
|
||||
|
||||
if (empty($config['lazy'])) {
|
||||
$this->connection = $this->connect();
|
||||
$this->connect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Automatically frees the resources allocated for this result set
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __destruct()
|
||||
{
|
||||
$this->disconnect();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return resource
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function connect();
|
||||
final public function connect()
|
||||
{
|
||||
$this->connection = $this->doConnect();
|
||||
dibi::notify('connected');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Disconnects from a database
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
final public function disconnect()
|
||||
{
|
||||
$this->doDisconnect();
|
||||
$this->connection = NULL;
|
||||
dibi::notify('disconnected');
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -110,8 +139,8 @@ abstract class DibiDriver extends NObject
|
||||
*/
|
||||
final public function getConnection()
|
||||
{
|
||||
if (!$this->connection) {
|
||||
$this->connection = $this->connect();
|
||||
if ($this->connection === NULL) {
|
||||
$this->connect();
|
||||
}
|
||||
|
||||
return $this->connection;
|
||||
@@ -123,7 +152,7 @@ abstract class DibiDriver extends NObject
|
||||
* Generates (translates) and executes SQL query
|
||||
*
|
||||
* @param array|mixed one or more arguments
|
||||
* @return DibiResult|TRUE
|
||||
* @return DibiResult Result set object (if any)
|
||||
* @throws DibiException
|
||||
*/
|
||||
final public function query($args)
|
||||
@@ -161,16 +190,17 @@ abstract class DibiDriver extends NObject
|
||||
/**
|
||||
* Executes the SQL query
|
||||
*
|
||||
* @param string SQL statement.
|
||||
* @return DibiResult|TRUE Result set object
|
||||
* @param string SQL statement.
|
||||
* @return DibiResult Result set object (if any)
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function nativeQuery($sql)
|
||||
final public function nativeQuery($sql)
|
||||
{
|
||||
dibi::notify('beforeQuery', $this, $sql);
|
||||
$res = $this->doQuery($sql);
|
||||
dibi::notify('afterQuery', $this, $res);
|
||||
return $res;
|
||||
// backward compatibility - will be removed!
|
||||
return $res instanceof DibiResult ? $res : TRUE;
|
||||
}
|
||||
|
||||
|
||||
@@ -197,11 +227,31 @@ abstract class DibiDriver extends NObject
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Internal: Connects to a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return resource
|
||||
*/
|
||||
abstract protected function doConnect();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Internal: Disconnects from a database
|
||||
*
|
||||
* @throws DibiException
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function doDisconnect();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Internal: Executes the SQL query
|
||||
*
|
||||
* @param string SQL statement.
|
||||
* @return DibiResult|TRUE Result set object
|
||||
* @return DibiResult Result set object
|
||||
* @throws DibiDatabaseException
|
||||
*/
|
||||
abstract protected function doQuery($sql);
|
||||
|
@@ -1,22 +1,22 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Nette Framework
|
||||
* dibi - tiny'n'smart database abstraction layer
|
||||
* ----------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2004, 2007 David Grudl aka -dgx- (http://www.dgx.cz)
|
||||
* Copyright (c) 2005, 2007 David Grudl aka -dgx- (http://www.dgx.cz)
|
||||
*
|
||||
* This source file is subject to the "Nette license" that is bundled
|
||||
* This source file is subject to the "dibi license" that is bundled
|
||||
* with this package in the file license.txt.
|
||||
*
|
||||
* For more information please see http://php7.org/nette/
|
||||
* For more information please see http://php7.org/dibi/
|
||||
*
|
||||
* @author David Grudl
|
||||
* @copyright Copyright (c) 2004, 2007 David Grudl
|
||||
* @license http://php7.org/nette/license (Nette license)
|
||||
* @link http://php7.org/nette/
|
||||
* @version $Revision$ $Date$
|
||||
* @category Nette
|
||||
* @package Nette-Core
|
||||
* @copyright Copyright (c) 2005, 2007 David Grudl
|
||||
* @license http://php7.org/dibi/license (dibi license)
|
||||
* @category Database
|
||||
* @package Dibi
|
||||
* @link http://php7.org/dibi/
|
||||
*/
|
||||
|
||||
|
||||
@@ -52,6 +52,11 @@ if (!class_exists('NObject', FALSE)) {
|
||||
* $obj = new MyClass;
|
||||
* $obj->newMethod($x); // equivalent to MyClass_prototype_newMethod($obj, $x);
|
||||
* </code>
|
||||
*
|
||||
* @author David Grudl
|
||||
* @copyright Copyright (c) 2004, 2007 David Grudl
|
||||
* @license http://php7.org/nette/license (Nette license)
|
||||
* @link http://php7.org/nette/
|
||||
*/
|
||||
abstract class NObject
|
||||
{
|
||||
@@ -102,6 +107,10 @@ abstract class NObject
|
||||
*/
|
||||
protected function __call($name, $args)
|
||||
{
|
||||
if ($name === '') {
|
||||
throw new BadMethodCallException("Call to method without name");
|
||||
}
|
||||
|
||||
// object prototypes support Class__method()
|
||||
// (or use class Class__method { static function ... } with autoloading?)
|
||||
$cl = $class = get_class($this);
|
||||
@@ -122,12 +131,12 @@ abstract class NObject
|
||||
*
|
||||
* @param string property name
|
||||
* @return mixed property value or the event handler list
|
||||
* @throws NPropertyException if the property is not defined.
|
||||
* @throws LogicException if the property is not defined.
|
||||
*/
|
||||
protected function &__get($name)
|
||||
{
|
||||
if ($name === '') {
|
||||
throw new NPropertyException('Property name must be a non-empty string');
|
||||
throw new LogicException("Cannot read an property without name");
|
||||
}
|
||||
|
||||
// property getter support
|
||||
@@ -141,7 +150,7 @@ abstract class NObject
|
||||
return $val;
|
||||
|
||||
} else {
|
||||
throw new NPropertyException("Cannot read an undeclared property $class::\$$name");
|
||||
throw new LogicException("Cannot read an undeclared property $class::\$$name");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,12 +162,12 @@ abstract class NObject
|
||||
* @param string property name
|
||||
* @param mixed property value
|
||||
* @return void
|
||||
* @throws NPropertyException if the property is not defined or is read-only
|
||||
* @throws LogicException if the property is not defined or is read-only
|
||||
*/
|
||||
protected function __set($name, $value)
|
||||
{
|
||||
if ($name === '') {
|
||||
throw new NPropertyException('Property name must be a non-empty string');
|
||||
throw new LogicException('Cannot assign to an property without name');
|
||||
}
|
||||
|
||||
// property setter support
|
||||
@@ -169,11 +178,11 @@ abstract class NObject
|
||||
$this->$m($value);
|
||||
|
||||
} else {
|
||||
throw new NPropertyException("Cannot assign to a read-only property $class::\$$name");
|
||||
throw new LogicException("Cannot assign to a read-only property $class::\$$name");
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new NPropertyException("Cannot assign to an undeclared property $class::\$$name");
|
||||
throw new LogicException("Cannot assign to an undeclared property $class::\$$name");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,12 +206,12 @@ abstract class NObject
|
||||
*
|
||||
* @param string property name
|
||||
* @return void
|
||||
* @throws NPropertyException
|
||||
* @throws LogicException
|
||||
*/
|
||||
protected function __unset($name)
|
||||
{
|
||||
$class = get_class($this);
|
||||
throw new NPropertyException("Cannot unset an declared or undeclared property $class::\$$name");
|
||||
throw new LogicException("Cannot unset an property $class::\$$name");
|
||||
}
|
||||
|
||||
|
||||
@@ -227,12 +236,4 @@ abstract class NObject
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Occurs on unsuccessful attempts to get or set the value of a property
|
||||
*/
|
||||
class NPropertyException extends Exception
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user