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

* renamed DibiDatabaseException to DibiDriverException

* PDO driver convert PDOExceptions to DibiDriverException
This commit is contained in:
David Grudl
2007-11-17 09:37:55 +00:00
parent c174e20135
commit cbb315cbc7
11 changed files with 88 additions and 50 deletions

View File

@@ -76,11 +76,11 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
}
if (!is_resource($this->connection)) {
throw new DibiDatabaseException("Can't connect to DB");
throw new DibiDriverException("Can't connect to DB");
}
if (isset($config['database']) && !@mssql_select_db($config['database'], $this->connection)) {
throw new DibiDatabaseException("Can't select DB '$config[database]'");
throw new DibiDriverException("Can't select DB '$config[database]'");
}
}
@@ -103,14 +103,14 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
*
* @param string SQL statement.
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
public function query($sql)
{
$this->resultset = @mssql_query($sql, $this->connection);
if ($this->resultset === FALSE) {
throw new DibiDatabaseException('Query error', 0, $sql);
throw new DibiDriverException('Query error', 0, $sql);
}
return is_resource($this->resultset);

View File

@@ -106,7 +106,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
}
if (!is_resource($this->connection)) {
throw new DibiDatabaseException(mysql_error(), mysql_errno());
throw new DibiDriverException(mysql_error(), mysql_errno());
}
if (isset($config['charset'])) {
@@ -115,7 +115,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
}
if (isset($config['database']) && !@mysql_select_db($config['database'], $this->connection)) {
throw new DibiDatabaseException(mysql_error($this->connection), mysql_errno($this->connection));
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
}
$this->buffered = empty($config['unbuffered']);
@@ -140,7 +140,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
*
* @param string SQL statement.
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
public function query($sql)
{
@@ -151,7 +151,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
}
if ($errno = mysql_errno($this->connection)) {
throw new DibiDatabaseException(mysql_error($this->connection), $errno, $sql);
throw new DibiDriverException(mysql_error($this->connection), $errno, $sql);
}
return is_resource($this->resultset);
@@ -264,7 +264,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
public function rowCount()
{
if (!$this->buffered) {
throw new DibiDatabaseException('Row count is not available for unbuffered queries');
throw new DibiDriverException('Row count is not available for unbuffered queries');
}
return mysql_num_rows($this->resultset);
}
@@ -294,7 +294,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
public function seek($row)
{
if (!$this->buffered) {
throw new DibiDatabaseException('Cannot seek an unbuffered result set');
throw new DibiDriverException('Cannot seek an unbuffered result set');
}
return mysql_data_seek($this->resultset, $row);

View File

@@ -95,7 +95,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
@mysqli_real_connect($this->connection, $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['options']);
if ($errno = mysqli_connect_errno()) {
throw new DibiDatabaseException(mysqli_connect_error(), $errno);
throw new DibiDriverException(mysqli_connect_error(), $errno);
}
if (isset($config['charset'])) {
@@ -123,14 +123,14 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
*
* @param string SQL statement.
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
public function query($sql)
{
$this->resultset = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
if ($errno = mysqli_errno($this->connection)) {
throw new DibiDatabaseException(mysqli_error($this->connection), $errno, $sql);
throw new DibiDriverException(mysqli_error($this->connection), $errno, $sql);
}
return is_object($this->resultset);
@@ -169,7 +169,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
public function begin()
{
if (!mysqli_autocommit($this->connection, FALSE)) {
throw new DibiDatabaseException(mysqli_error($this->connection), mysqli_errno($this->connection));
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
}
}
@@ -182,7 +182,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
public function commit()
{
if (!mysqli_commit($this->connection)) {
throw new DibiDatabaseException(mysqli_error($this->connection), mysqli_errno($this->connection));
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
}
mysqli_autocommit($this->connection, TRUE);
}
@@ -196,7 +196,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
public function rollback()
{
if (!mysqli_rollback($this->connection)) {
throw new DibiDatabaseException(mysqli_error($this->connection), mysqli_errno($this->connection));
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
}
mysqli_autocommit($this->connection, TRUE);
}
@@ -252,7 +252,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
public function rowCount()
{
if (!$this->buffered) {
throw new DibiDatabaseException('Row count is not available for unbuffered queries');
throw new DibiDriverException('Row count is not available for unbuffered queries');
}
return mysqli_num_rows($this->resultset);
}
@@ -282,7 +282,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
public function seek($row)
{
if (!$this->buffered) {
throw new DibiDatabaseException('Cannot seek an unbuffered result set');
throw new DibiDriverException('Cannot seek an unbuffered result set');
}
return mysqli_data_seek($this->resultset, $row);
}

View File

@@ -86,7 +86,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
}
if (!is_resource($this->connection)) {
throw new DibiDatabaseException(odbc_errormsg() . ' ' . odbc_error());
throw new DibiDriverException(odbc_errormsg() . ' ' . odbc_error());
}
}
@@ -109,14 +109,14 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
*
* @param string SQL statement.
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
public function query($sql)
{
$this->resultset = @odbc_exec($this->connection, $sql);
if ($this->resultset === FALSE) {
throw new DibiDatabaseException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
}
return is_resource($this->resultset);
@@ -155,7 +155,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
public function begin()
{
if (!odbc_autocommit($this->connection, FALSE)) {
throw new DibiDatabaseException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
}
}
@@ -168,7 +168,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
public function commit()
{
if (!odbc_commit($this->connection)) {
throw new DibiDatabaseException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
}
odbc_autocommit($this->connection, TRUE);
}
@@ -182,7 +182,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
public function rollback()
{
if (!odbc_rollback($this->connection)) {
throw new DibiDatabaseException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
}
odbc_autocommit($this->connection, TRUE);
}

View File

@@ -78,7 +78,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
if (!$this->connection) {
$err = oci_error();
throw new DibiDatabaseException($err['message'], $err['code']);
throw new DibiDriverException($err['message'], $err['code']);
}
}
@@ -101,7 +101,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
*
* @param string SQL statement.
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
public function query($sql)
{
@@ -111,11 +111,11 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
oci_execute($this->resultset, $this->autocommit ? OCI_COMMIT_ON_SUCCESS : OCI_DEFAULT);
$err = oci_error($this->resultset);
if ($err) {
throw new DibiDatabaseException($err['message'], $err['code'], $sql);
throw new DibiDriverException($err['message'], $err['code'], $sql);
}
} else {
$err = oci_error($this->connection);
throw new DibiDatabaseException($err['message'], $err['code'], $sql);
throw new DibiDriverException($err['message'], $err['code'], $sql);
}
return is_resource($this->resultset);
@@ -166,7 +166,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
{
if (!oci_commit($this->connection)) {
$err = oci_error($this->connection);
throw new DibiDatabaseException($err['message'], $err['code']);
throw new DibiDriverException($err['message'], $err['code']);
}
$this->autocommit = TRUE;
}
@@ -181,7 +181,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
{
if (!oci_rollback($this->connection)) {
$err = oci_error($this->connection);
throw new DibiDatabaseException($err['message'], $err['code']);
throw new DibiDriverException($err['message'], $err['code']);
}
$this->autocommit = TRUE;
}

View File

@@ -68,7 +68,16 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
throw new DibiException("PHP extension 'pdo' is not loaded");
}
$this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
try {
$this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']);
} catch (PDOException $e) {
throw $this->convertException($e);
}
if (!$this->connection) {
throw new DibiDriverException('Connecting error');
}
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
@@ -90,11 +99,15 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*
* @param string SQL statement.
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
public function query($sql)
{
$this->resultset = $this->connection->query($sql);
try {
$this->resultset = $this->connection->query($sql);
} catch (PDOException $e) {
throw $this->convertException($e);
}
return $this->resultset instanceof PDOStatement;
}
@@ -130,7 +143,11 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/
public function begin()
{
$this->connection->beginTransaction();
try {
$this->connection->beginTransaction();
} catch (PDOException $e) {
throw $this->convertException($e);
}
}
@@ -141,7 +158,11 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/
public function commit()
{
$this->connection->commit();
try {
$this->connection->commit();
} catch (PDOException $e) {
throw $this->convertException($e);
}
}
@@ -152,7 +173,11 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/
public function rollback()
{
$this->connection->rollBack();
try {
$this->connection->rollBack();
} catch (PDOException $e) {
throw $this->convertException($e);
}
}
@@ -199,7 +224,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/
public function rowCount()
{
throw new DibiDatabaseException('Row count is not available for unbuffered queries');
throw new DibiDriverException('Row count is not available for unbuffered queries');
}
@@ -226,7 +251,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/
public function seek($row)
{
throw new DibiDatabaseException('Cannot seek an unbuffered result set');
throw new DibiDriverException('Cannot seek an unbuffered result set');
}
@@ -291,4 +316,17 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
function getDibiReflection()
{}
/**
* Disconnects from a database
*
* @param PDOException
* @return DibiDriverException
*/
private function convertException($e)
{
return new DibiDriverException($e->getMessage(), $e->getCode());
}
}

View File

@@ -66,7 +66,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
}
NException::catchError('DibiDatabaseException');
NException::catchError('DibiDriverException');
if (isset($config['persistent'])) {
$this->connection = @pg_connect($config['database'], PGSQL_CONNECT_FORCE_NEW);
} else {
@@ -75,7 +75,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
NException::restore();
if (!is_resource($this->connection)) {
throw new DibiDatabaseException('unknown error');
throw new DibiDriverException('Connecting error');
}
if (isset($config['charset'])) {
@@ -104,14 +104,14 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
* @param string SQL statement.
* @param bool update affected rows?
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
public function query($sql)
{
$this->resultset = @pg_query($this->connection, $sql);
if ($this->resultset === FALSE) {
throw new DibiDatabaseException(pg_last_error($this->connection), 0, $sql);
throw new DibiDriverException(pg_last_error($this->connection), 0, $sql);
}
return is_resource($this->resultset);

View File

@@ -78,7 +78,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
}
if (!$this->connection) {
throw new DibiDatabaseException($errorMsg);
throw new DibiDriverException($errorMsg);
}
$this->buffered = empty($config['unbuffered']);
@@ -102,7 +102,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
*
* @param string SQL statement.
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
public function query($sql)
{
@@ -115,7 +115,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
}
if ($errorMsg !== NULL) {
throw new DibiDatabaseException($errorMsg, sqlite_last_error($this->connection), $sql);
throw new DibiDriverException($errorMsg, sqlite_last_error($this->connection), $sql);
}
return is_resource($this->resultset);
@@ -225,7 +225,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
public function rowCount()
{
if (!$this->buffered) {
throw new DibiDatabaseException('Row count is not available for unbuffered queries');
throw new DibiDriverException('Row count is not available for unbuffered queries');
}
return sqlite_num_rows($this->resultset);
}
@@ -255,7 +255,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
public function seek($row)
{
if (!$this->buffered) {
throw new DibiDatabaseException('Cannot seek an unbuffered result set');
throw new DibiDriverException('Cannot seek an unbuffered result set');
}
return sqlite_seek($this->resultset, $row);
}

View File

@@ -56,7 +56,7 @@ interface DibiDriverInterface
*
* @param string SQL statement.
* @return bool have resultset?
* @throws DibiDatabaseException
* @throws DibiDriverException
*/
function query($sql);

View File

@@ -40,7 +40,7 @@ class DibiException extends Exception
* @package dibi
* @version $Revision$ $Date$
*/
class DibiDatabaseException extends DibiException
class DibiDriverException extends DibiException
{
/** @var string */
private $sql;

View File

@@ -73,7 +73,7 @@ final class DibiLogger extends NObject
}
if ($event === 'exception' && $this->logErrors) {
// $arg is DibiDatabaseException
// $arg is DibiDriverException
$message = $arg->getMessage();
$code = $arg->getCode();
if ($code) {