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

added DibiDriver::disconnect()

This commit is contained in:
David Grudl
2007-11-10 07:37:44 +00:00
parent 8a6d664876
commit 9ff43d0ac3
15 changed files with 304 additions and 129 deletions

View File

@@ -14,7 +14,7 @@
* @author David Grudl * @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl
* @license http://php7.org/dibi/license (dibi license) * @license http://php7.org/dibi/license (dibi license)
* @version 0.9b (Revision: $WCREV$, Date: $WCDATE$) * @version 0.9 (Revision: $WCREV$, Date: $WCDATE$)
* @category Database * @category Database
* @package Dibi * @package Dibi
* @link http://php7.org/dibi/ * @link http://php7.org/dibi/
@@ -90,7 +90,7 @@ class dibi
FIELD_COUNTER = 'c', // counter or autoincrement, is integer FIELD_COUNTER = 'c', // counter or autoincrement, is integer
// dibi version // dibi version
VERSION = '0.9b (Revision: $WCREV$, Date: $WCDATE$)'; VERSION = '0.9 (Revision: $WCREV$, Date: $WCDATE$)';
/** /**
@@ -198,6 +198,18 @@ class dibi
/**
* Disconnects from database (doesn't destroy DibiDriver object)
*
* @return void
*/
public static function disconnect()
{
self::getConnection()->disconnect();
}
/** /**
* Returns TRUE when connection was established * Returns TRUE when connection was established
* *
@@ -254,7 +266,7 @@ class dibi
* Generates and executes SQL query - Monostate for DibiDriver::query() * Generates and executes SQL query - Monostate for DibiDriver::query()
* *
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiResult|TRUE * @return DibiResult Result set object (if any)
* @throws DibiException * @throws DibiException
*/ */
public static function query($args) public static function query($args)
@@ -269,8 +281,8 @@ class dibi
/** /**
* Executes the SQL query - Monostate for DibiDriver::nativeQuery() * Executes the SQL query - Monostate for DibiDriver::nativeQuery()
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE * @return DibiResult Result set object (if any)
*/ */
public static function nativeQuery($sql) public static function nativeQuery($sql)
{ {
@@ -351,6 +363,16 @@ class dibi
/**
* Experimental; will be used in PHP 5.3
*/
public static function __callStatic($name, $args)
{
return call_user_func_array(array(self::getConnection(), $name), $args);
}
/** /**
* Create a new substitution pair for indentifiers * Create a new substitution pair for indentifiers
* *

View File

@@ -25,7 +25,7 @@
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
class DibiMsSqlDriver extends DibiDriver final class DibiMsSqlDriver extends DibiDriver
{ {
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -61,7 +61,7 @@ class DibiMsSqlDriver extends DibiDriver
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
protected function connect() protected function doConnect()
{ {
if (!extension_loaded('mssql')) { if (!extension_loaded('mssql')) {
throw new DibiException("PHP extension 'mssql' is not loaded"); throw new DibiException("PHP extension 'mssql' is not loaded");
@@ -83,17 +83,28 @@ class DibiMsSqlDriver extends DibiDriver
throw new DibiDatabaseException("Can't select DB '$config[database]'"); throw new DibiDatabaseException("Can't select DB '$config[database]'");
} }
dibi::notify('connected', $this);
return $connection; return $connection;
} }
/**
* Disconnects from a database
*
* @return void
*/
protected function doDisconnect()
{
mssql_close($this->getConnection());
}
/** /**
* Internal: Executes the SQL query * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
protected function doQuery($sql) protected function doQuery($sql)
@@ -104,7 +115,7 @@ class DibiMsSqlDriver extends DibiDriver
throw new DibiDatabaseException('Query error', 0, $sql); throw new DibiDatabaseException('Query error', 0, $sql);
} }
return is_resource($res) ? new DibiMSSqlResult($res) : TRUE; return is_resource($res) ? new DibiMSSqlResult($res) : NULL;
} }
@@ -258,7 +269,7 @@ class DibiMsSqlDriver extends DibiDriver
class DibiMSSqlResult extends DibiResult final class DibiMSSqlResult extends DibiResult
{ {
/** /**

View File

@@ -25,7 +25,7 @@
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
class DibiMySqlDriver extends DibiDriver final class DibiMySqlDriver extends DibiDriver
{ {
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -70,7 +70,7 @@ class DibiMySqlDriver extends DibiDriver
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
protected function connect() protected function doConnect()
{ {
if (!extension_loaded('mysql')) { if (!extension_loaded('mysql')) {
throw new DibiException("PHP extension 'mysql' is not loaded"); throw new DibiException("PHP extension 'mysql' is not loaded");
@@ -116,17 +116,28 @@ class DibiMySqlDriver extends DibiDriver
throw new DibiDatabaseException(mysql_error($connection), mysql_errno($connection)); throw new DibiDatabaseException(mysql_error($connection), mysql_errno($connection));
} }
dibi::notify('connected', $this);
return $connection; return $connection;
} }
/**
* Disconnects from a database
*
* @return void
*/
protected function doDisconnect()
{
mysql_close($this->getConnection());
}
/** /**
* Internal: Executes the SQL query * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
protected function doQuery($sql) protected function doQuery($sql)
@@ -138,7 +149,7 @@ class DibiMySqlDriver extends DibiDriver
throw new DibiDatabaseException(mysql_error($connection), $errno, $sql); throw new DibiDatabaseException(mysql_error($connection), $errno, $sql);
} }
return is_resource($res) ? new DibiMySqlResult($res) : TRUE; return is_resource($res) ? new DibiMySqlResult($res) : NULL;
} }
@@ -291,7 +302,7 @@ class DibiMySqlDriver extends DibiDriver
class DibiMySqlResult extends DibiResult final class DibiMySqlResult extends DibiResult
{ {
/** /**

View File

@@ -25,7 +25,7 @@
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
class DibiMySqliDriver extends DibiDriver final class DibiMySqliDriver extends DibiDriver
{ {
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -71,7 +71,7 @@ class DibiMySqliDriver extends DibiDriver
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
protected function connect() protected function doConnect()
{ {
if (!extension_loaded('mysqli')) { if (!extension_loaded('mysqli')) {
throw new DibiException("PHP extension 'mysqli' is not loaded"); throw new DibiException("PHP extension 'mysqli' is not loaded");
@@ -89,17 +89,28 @@ class DibiMySqliDriver extends DibiDriver
mysqli_query($connection, "SET NAMES '" . $config['charset'] . "'"); mysqli_query($connection, "SET NAMES '" . $config['charset'] . "'");
} }
dibi::notify('connected', $this);
return $connection; return $connection;
} }
/**
* Disconnects from a database
*
* @return void
*/
protected function doDisconnect()
{
mysqli_close($this->getConnection());
}
/** /**
* Internal: Executes the SQL query * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
protected function doQuery($sql) protected function doQuery($sql)
@@ -111,7 +122,7 @@ class DibiMySqliDriver extends DibiDriver
throw new DibiDatabaseException(mysqli_error($connection), $errno, $sql); throw new DibiDatabaseException(mysqli_error($connection), $errno, $sql);
} }
return is_object($res) ? new DibiMySqliResult($res) : TRUE; return is_object($res) ? new DibiMySqliResult($res) : NULL;
} }
@@ -275,7 +286,7 @@ class DibiMySqliDriver extends DibiDriver
class DibiMySqliResult extends DibiResult final class DibiMySqliResult extends DibiResult
{ {
/** /**

View File

@@ -25,7 +25,7 @@
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
class DibiOdbcDriver extends DibiDriver final class DibiOdbcDriver extends DibiDriver
{ {
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -74,7 +74,7 @@ class DibiOdbcDriver extends DibiDriver
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
protected function connect() protected function doConnect()
{ {
if (!extension_loaded('odbc')) { if (!extension_loaded('odbc')) {
throw new DibiException("PHP extension 'odbc' is not loaded"); throw new DibiException("PHP extension 'odbc' is not loaded");
@@ -92,28 +92,19 @@ class DibiOdbcDriver extends DibiDriver
throw new DibiDatabaseException(odbc_errormsg(), odbc_error()); throw new DibiDatabaseException(odbc_errormsg(), odbc_error());
} }
dibi::notify('connected', $this);
return $connection; return $connection;
} }
/** /**
* Executes the SQL query * Disconnects from a database
* *
* @param string SQL statement. * @return void
* @return DibiResult|TRUE Result set object
* @throws DibiException
*/ */
public function nativeQuery($sql) protected function doDisconnect()
{ {
$this->affectedRows = FALSE; odbc_close($this->getConnection());
$res = parent::nativeQuery($sql);
if ($res instanceof DibiResult) {
$this->affectedRows = odbc_num_rows($res->getResource());
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
}
return $res;
} }
@@ -122,11 +113,12 @@ class DibiOdbcDriver extends DibiDriver
* Internal: Executes the SQL query * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
protected function doQuery($sql) protected function doQuery($sql)
{ {
$this->affectedRows = FALSE;
$connection = $this->getConnection(); $connection = $this->getConnection();
$res = @odbc_exec($connection, $sql); $res = @odbc_exec($connection, $sql);
@@ -134,7 +126,11 @@ class DibiOdbcDriver extends DibiDriver
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection), $sql); throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection), $sql);
} }
return is_resource($res) ? new DibiOdbcResult($res) : TRUE; if (is_resource($res)) {
$this->affectedRows = odbc_num_rows($res);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
return new DibiOdbcResult($res);
}
} }
@@ -295,7 +291,7 @@ class DibiOdbcDriver extends DibiDriver
class DibiOdbcResult extends DibiResult final class DibiOdbcResult extends DibiResult
{ {
private $row = 0; private $row = 0;

View File

@@ -25,7 +25,7 @@
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
class DibiOracleDriver extends DibiDriver final class DibiOracleDriver extends DibiDriver
{ {
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -65,7 +65,7 @@ class DibiOracleDriver extends DibiDriver
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
protected function connect() protected function doConnect()
{ {
if (!extension_loaded('oci8')) { if (!extension_loaded('oci8')) {
throw new DibiException("PHP extension 'oci8' is not loaded"); throw new DibiException("PHP extension 'oci8' is not loaded");
@@ -79,17 +79,28 @@ class DibiOracleDriver extends DibiDriver
throw new DibiDatabaseException($err['message'], $err['code']); throw new DibiDatabaseException($err['message'], $err['code']);
} }
dibi::notify('connected', $this);
return $connection; return $connection;
} }
/**
* Disconnects from a database
*
* @return void
*/
protected function doDisconnect()
{
oci_close($this->getConnection());
}
/** /**
* Internal: Executes the SQL query * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
protected function doQuery($sql) protected function doQuery($sql)
@@ -260,7 +271,7 @@ class DibiOracleDriver extends DibiDriver
class DibiOracleResult extends DibiResult final class DibiOracleResult extends DibiResult
{ {
/** /**

View File

@@ -25,7 +25,7 @@
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
class DibiPdoDriver extends DibiDriver final class DibiPdoDriver extends DibiDriver
{ {
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -61,7 +61,7 @@ class DibiPdoDriver extends DibiDriver
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
protected function connect() protected function doConnect()
{ {
if (!extension_loaded('pdo')) { if (!extension_loaded('pdo')) {
throw new DibiException("PHP extension 'pdo' is not loaded"); throw new DibiException("PHP extension 'pdo' is not loaded");
@@ -70,24 +70,33 @@ class DibiPdoDriver extends DibiDriver
$config = $this->getConfig(); $config = $this->getConfig();
$connection = new PDO($config['dsn'], $config['username'], $config['password']); $connection = new PDO($config['dsn'], $config['username'], $config['password']);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
dibi::notify('connected', $this);
return $connection; return $connection;
} }
/**
* Disconnects from a database
*
* @return void
*/
protected function doDisconnect()
{
}
/** /**
* Internal: Executes the SQL query * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
protected function doQuery($sql) protected function doQuery($sql)
{ {
$res = $this->getConnection()->query($sql); $res = $this->getConnection()->query($sql);
return $res instanceof PDOStatement ? new DibiPdoResult($res) : TRUE; return $res instanceof PDOStatement ? new DibiPdoResult($res) : NULL;
} }
@@ -235,7 +244,7 @@ class DibiPdoDriver extends DibiDriver
class DibiPdoResult extends DibiResult final class DibiPdoResult extends DibiResult
{ {
private $row = 0; private $row = 0;

View File

@@ -25,7 +25,7 @@
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
class DibiPostgreDriver extends DibiDriver final class DibiPostgreDriver extends DibiDriver
{ {
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -67,7 +67,7 @@ class DibiPostgreDriver extends DibiDriver
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
protected function connect() protected function doConnect()
{ {
if (!extension_loaded('pgsql')) { if (!extension_loaded('pgsql')) {
throw new DibiException("PHP extension 'pgsql' is not loaded"); throw new DibiException("PHP extension 'pgsql' is not loaded");
@@ -101,28 +101,19 @@ class DibiPostgreDriver extends DibiDriver
// don't handle this error... // don't handle this error...
} }
dibi::notify('connected', $this);
return $connection; return $connection;
} }
/** /**
* Executes the SQL query * Disconnects from a database
* *
* @param string SQL statement. * @return void
* @return DibiResult|TRUE Result set object
* @throws DibiException
*/ */
public function nativeQuery($sql) protected function doDisconnect()
{ {
$this->affectedRows = FALSE; pg_close($this->getConnection());
$res = parent::nativeQuery($sql);
if ($res instanceof DibiResult) {
$this->affectedRows = pg_affected_rows($res->getResource());
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
}
return $res;
} }
@@ -131,10 +122,11 @@ class DibiPostgreDriver extends DibiDriver
* Internal: Executes the SQL query * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @param bool update affected rows?
* @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
protected function doQuery($sql) protected function doQuery($sql, $silent = FALSE)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
$res = @pg_query($connection, $sql); $res = @pg_query($connection, $sql);
@@ -143,7 +135,13 @@ class DibiPostgreDriver extends DibiDriver
throw new DibiDatabaseException(pg_last_error($connection), 0, $sql); throw new DibiDatabaseException(pg_last_error($connection), 0, $sql);
} }
return is_resource($res) ? new DibiPostgreResult($res) : TRUE; if (is_resource($res)) {
if (!$silent) {
$this->affectedRows = pg_affected_rows($res);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
}
return new DibiPostgreResult($res);
}
} }
@@ -169,9 +167,9 @@ class DibiPostgreDriver extends DibiDriver
{ {
if ($sequence === NULL) { if ($sequence === NULL) {
// PostgreSQL 8.1 is needed // PostgreSQL 8.1 is needed
$res = $this->doQuery("SELECT LASTVAL() AS seq"); $res = $this->doQuery("SELECT LASTVAL() AS seq", TRUE);
} else { } else {
$res = $this->doQuery("SELECT CURRVAL('$sequence') AS seq"); $res = $this->doQuery("SELECT CURRVAL('$sequence') AS seq", TRUE);
} }
if (is_resource($res)) { if (is_resource($res)) {
@@ -191,7 +189,7 @@ class DibiPostgreDriver extends DibiDriver
*/ */
public function begin() public function begin()
{ {
$this->doQuery('BEGIN'); $this->doQuery('BEGIN', TRUE);
dibi::notify('begin', $this); dibi::notify('begin', $this);
} }
@@ -203,7 +201,7 @@ class DibiPostgreDriver extends DibiDriver
*/ */
public function commit() public function commit()
{ {
$this->doQuery('COMMIT'); $this->doQuery('COMMIT', TRUE);
dibi::notify('commit', $this); dibi::notify('commit', $this);
} }
@@ -215,7 +213,7 @@ class DibiPostgreDriver extends DibiDriver
*/ */
public function rollback() public function rollback()
{ {
$this->doQuery('ROLLBACK'); $this->doQuery('ROLLBACK', TRUE);
dibi::notify('rollback', $this); dibi::notify('rollback', $this);
} }
@@ -306,7 +304,7 @@ class DibiPostgreDriver extends DibiDriver
class DibiPostgreResult extends DibiResult final class DibiPostgreResult extends DibiResult
{ {
/** /**

View File

@@ -25,7 +25,7 @@
* *
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
class DibiSqliteDriver extends DibiDriver final class DibiSqliteDriver extends DibiDriver
{ {
/** /**
* Describes how convert some datatypes to SQL command * Describes how convert some datatypes to SQL command
@@ -60,7 +60,7 @@ class DibiSqliteDriver extends DibiDriver
* @throws DibiException * @throws DibiException
* @return resource * @return resource
*/ */
protected function connect() protected function doConnect()
{ {
if (!extension_loaded('sqlite')) { if (!extension_loaded('sqlite')) {
throw new DibiException("PHP extension 'sqlite' is not loaded"); throw new DibiException("PHP extension 'sqlite' is not loaded");
@@ -79,29 +79,41 @@ class DibiSqliteDriver extends DibiDriver
throw new DibiDatabaseException($errorMsg); throw new DibiDatabaseException($errorMsg);
} }
dibi::notify('connected', $this);
return $connection; return $connection;
} }
/**
* Disconnects from a database
*
* @return void
*/
protected function doDisconnect()
{
sqlite_close($this->getConnection());
}
/** /**
* Internal: Executes the SQL query * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
protected function doQuery($sql) protected function doQuery($sql)
{ {
$connection = $this->getConnection(); $connection = $this->getConnection();
$res = @sqlite_query($connection, $sql, SQLITE_ASSOC); $errorMsg = NULL;
$res = @sqlite_query($connection, $sql, SQLITE_ASSOC, $errorMsg);
if ($errno = sqlite_last_error($connection)) { if ($errorMsg !== NULL) {
throw new DibiDatabaseException(sqlite_error_string($errno), $errno, $sql); throw new DibiDatabaseException($errorMsg, sqlite_last_error($connection), $sql);
} }
return is_resource($res) ? new DibiSqliteResult($res) : TRUE; return is_resource($res) ? new DibiSqliteResult($res) : NULL;
} }
@@ -249,7 +261,7 @@ class DibiSqliteDriver extends DibiDriver
class DibiSqliteResult extends DibiResult final class DibiSqliteResult extends DibiResult
{ {
/** /**

View File

@@ -64,19 +64,48 @@ abstract class DibiDriver extends NObject
$this->config = $config; $this->config = $config;
if (empty($config['lazy'])) { 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 * Connects to a database
* *
* @throws DibiException * @return void
* @return resource
*/ */
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() final public function getConnection()
{ {
if (!$this->connection) { if ($this->connection === NULL) {
$this->connection = $this->connect(); $this->connect();
} }
return $this->connection; return $this->connection;
@@ -123,7 +152,7 @@ abstract class DibiDriver extends NObject
* Generates (translates) and executes SQL query * Generates (translates) and executes SQL query
* *
* @param array|mixed one or more arguments * @param array|mixed one or more arguments
* @return DibiResult|TRUE * @return DibiResult Result set object (if any)
* @throws DibiException * @throws DibiException
*/ */
final public function query($args) final public function query($args)
@@ -161,16 +190,17 @@ abstract class DibiDriver extends NObject
/** /**
* Executes the SQL query * Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object (if any)
* @throws DibiException * @throws DibiException
*/ */
public function nativeQuery($sql) final public function nativeQuery($sql)
{ {
dibi::notify('beforeQuery', $this, $sql); dibi::notify('beforeQuery', $this, $sql);
$res = $this->doQuery($sql); $res = $this->doQuery($sql);
dibi::notify('afterQuery', $this, $res); 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 * Internal: Executes the SQL query
* *
* @param string SQL statement. * @param string SQL statement.
* @return DibiResult|TRUE Result set object * @return DibiResult Result set object
* @throws DibiDatabaseException * @throws DibiDatabaseException
*/ */
abstract protected function doQuery($sql); abstract protected function doQuery($sql);

View File

@@ -1,22 +1,22 @@
<?php <?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. * 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 * @author David Grudl
* @copyright Copyright (c) 2004, 2007 David Grudl * @copyright Copyright (c) 2005, 2007 David Grudl
* @license http://php7.org/nette/license (Nette license) * @license http://php7.org/dibi/license (dibi license)
* @link http://php7.org/nette/ * @category Database
* @version $Revision$ $Date$ * @package Dibi
* @category Nette * @link http://php7.org/dibi/
* @package Nette-Core
*/ */
@@ -52,6 +52,11 @@ if (!class_exists('NObject', FALSE)) {
* $obj = new MyClass; * $obj = new MyClass;
* $obj->newMethod($x); // equivalent to MyClass_prototype_newMethod($obj, $x); * $obj->newMethod($x); // equivalent to MyClass_prototype_newMethod($obj, $x);
* </code> * </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 abstract class NObject
{ {
@@ -102,6 +107,10 @@ abstract class NObject
*/ */
protected function __call($name, $args) protected function __call($name, $args)
{ {
if ($name === '') {
throw new BadMethodCallException("Call to method without name");
}
// object prototypes support Class__method() // object prototypes support Class__method()
// (or use class Class__method { static function ... } with autoloading?) // (or use class Class__method { static function ... } with autoloading?)
$cl = $class = get_class($this); $cl = $class = get_class($this);
@@ -122,12 +131,12 @@ abstract class NObject
* *
* @param string property name * @param string property name
* @return mixed property value or the event handler list * @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) protected function &__get($name)
{ {
if ($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 // property getter support
@@ -141,7 +150,7 @@ abstract class NObject
return $val; return $val;
} else { } 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 string property name
* @param mixed property value * @param mixed property value
* @return void * @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) protected function __set($name, $value)
{ {
if ($name === '') { 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 // property setter support
@@ -169,11 +178,11 @@ abstract class NObject
$this->$m($value); $this->$m($value);
} else { } 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 { } 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 * @param string property name
* @return void * @return void
* @throws NPropertyException * @throws LogicException
*/ */
protected function __unset($name) protected function __unset($name)
{ {
$class = get_class($this); $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
{
} }
}

View File

@@ -30,10 +30,10 @@ try {
// connects to MySQL / MySQLi // connects to MySQLi using array
try { try {
dibi::connect(array( dibi::connect(array(
'driver' => 'mysql', // or 'mysqli' 'driver' => 'mysqli',
'host' => 'localhost', 'host' => 'localhost',
'username' => 'root', 'username' => 'root',
'password' => 'xxx', 'password' => 'xxx',
@@ -109,3 +109,19 @@ try {
} catch (DibiException $e) { } catch (DibiException $e) {
echo '<pre>', $e, '</pre>'; echo '<pre>', $e, '</pre>';
} }
// connects to Oracle
try {
dibi::connect(array(
'driver' => 'oracle',
'username' => 'root',
'password' => 'xxx',
'database' => 'db',
));
echo '<p>Connected to Oracle</p>';
} catch (DibiException $e) {
echo '<pre>', $e, '</pre>';
}

View File

@@ -0,0 +1,27 @@
<h1>dibi extension method example</h1>
<pre>
<?php
require_once '../dibi/dibi.php';
dibi::connect(array(
'driver' => 'sqlite',
'database' => 'sample.sdb',
));
// using the "prototype" to add custom method to class DibiResult
function DibiResult_prototype_fetchShuffle(DibiResult $obj)
{
$all = $obj->fetchAll();
shuffle($all);
return $all;
}
// fetch complete result set shuffled
$res = dibi::query('SELECT * FROM [customers]');
$all = $res->fetchShuffle();
print_r($all);

View File

@@ -1,2 +1,2 @@
<a href="http://php7.org/dibi/" title="dibi - tiny'n'smart database abstraction layer" <a href="http://php7.org/dibi/" title="dibi - tiny 'n' smart database abstraction layer"
><img src="dibi-powered.gif" width="80" height="15" alt="dibi powered" /></a> ><img src="dibi-powered.gif" width="80" height="15" alt="dibi powered" /></a>

View File

@@ -1,4 +1,4 @@
Dibi version 0.9b Dibi version 0.9
Revision: $WCREV$ Revision: $WCREV$
Date: $WCDATE$ Date: $WCDATE$