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

refactoring in drivers

This commit is contained in:
David Grudl
2008-09-04 18:18:58 +00:00
parent 52d2ecf5b0
commit f935968aa7
5 changed files with 43 additions and 83 deletions

View File

@@ -126,16 +126,24 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
// affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.2.3) // affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.2.3)
$ok = @mysql_set_charset($config['charset'], $this->connection); // intentionally @ $ok = @mysql_set_charset($config['charset'], $this->connection); // intentionally @
} }
if (!$ok) $ok = @mysql_query("SET NAMES '$config[charset]'", $this->connection); // intentionally @ if (!$ok) {
if (!$ok) $this->throwException(); $ok = @mysql_query("SET NAMES '$config[charset]'", $this->connection); // intentionally @
if (!$ok) {
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
}
}
} }
if (isset($config['database'])) { if (isset($config['database'])) {
@mysql_select_db($config['database'], $this->connection) or $this->throwException(); // intentionally @ if (!@mysql_select_db($config['database'], $this->connection)) { // intentionally @
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
}
} }
if (isset($config['sqlmode'])) { if (isset($config['sqlmode'])) {
if (!@mysql_query("SET sql_mode='$config[sqlmode]'", $this->connection)) $this->throwException(); // intentionally @ if (!@mysql_query("SET sql_mode='$config[sqlmode]'", $this->connection)) { // intentionally @
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
}
} }
$this->buffered = empty($config['unbuffered']); $this->buffered = empty($config['unbuffered']);
@@ -171,7 +179,7 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
} }
if (mysql_errno($this->connection)) { if (mysql_errno($this->connection)) {
$this->throwException($sql); throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection), $sql);
} }
return is_resource($this->resultSet) ? clone $this : NULL; return is_resource($this->resultSet) ? clone $this : NULL;
@@ -385,18 +393,6 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver
/**
* Converts database error to DibiDriverException.
*
* @throws DibiDriverException
*/
protected function throwException($sql = NULL)
{
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection), $sql);
}
/** /**
* Returns the connection resource. * Returns the connection resource.
* *

View File

@@ -120,12 +120,18 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
// affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.0.5, fixed in PHP 5.1.5) // affects the character set used by mysql_real_escape_string() (was added in MySQL 5.0.7 and PHP 5.0.5, fixed in PHP 5.1.5)
$ok = @mysqli_set_charset($this->connection, $config['charset']); // intentionally @ $ok = @mysqli_set_charset($this->connection, $config['charset']); // intentionally @
} }
if (!$ok) $ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); // intentionally @ if (!$ok) {
if (!$ok) $this->throwException(); $ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); // intentionally @
if (!$ok) {
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
}
}
} }
if (isset($config['sqlmode'])) { if (isset($config['sqlmode'])) {
if (!@mysqli_query($this->connection, "SET sql_mode='$config[sqlmode]'")) $this->throwException(); // intentionally @ if (!@mysqli_query($this->connection, "SET sql_mode='$config[sqlmode]'")) { // intentionally @
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
}
} }
$this->buffered = empty($config['unbuffered']); $this->buffered = empty($config['unbuffered']);
@@ -157,7 +163,7 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
$this->resultSet = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @ $this->resultSet = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT); // intentionally @
if (mysqli_errno($this->connection)) { if (mysqli_errno($this->connection)) {
$this->throwException($sql); throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection), $sql);
} }
return is_object($this->resultSet) ? clone $this : NULL; return is_object($this->resultSet) ? clone $this : NULL;
@@ -370,18 +376,6 @@ class DibiMySqliDriver extends /*Nette::*/Object implements IDibiDriver
/**
* Converts database error to DibiDriverException.
*
* @throws DibiDriverException
*/
protected function throwException($sql = NULL)
{
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection), $sql);
}
/** /**
* Returns the connection resource. * Returns the connection resource.
* *

View File

@@ -123,7 +123,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
$this->resultSet = @odbc_exec($this->connection, $sql); // intentionally @ $this->resultSet = @odbc_exec($this->connection, $sql); // intentionally @
if ($this->resultSet === FALSE) { if ($this->resultSet === FALSE) {
$this->throwException($sql); throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
} }
return is_resource($this->resultSet) ? clone $this : NULL; return is_resource($this->resultSet) ? clone $this : NULL;
@@ -163,7 +163,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
public function begin() public function begin()
{ {
if (!odbc_autocommit($this->connection, FALSE)) { if (!odbc_autocommit($this->connection, FALSE)) {
$this->throwException(); throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
} }
} }
@@ -177,7 +177,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
public function commit() public function commit()
{ {
if (!odbc_commit($this->connection)) { if (!odbc_commit($this->connection)) {
$this->throwException(); throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
} }
odbc_autocommit($this->connection, TRUE); odbc_autocommit($this->connection, TRUE);
} }
@@ -192,7 +192,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
public function rollback() public function rollback()
{ {
if (!odbc_rollback($this->connection)) { if (!odbc_rollback($this->connection)) {
$this->throwException(); throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
} }
odbc_autocommit($this->connection, TRUE); odbc_autocommit($this->connection, TRUE);
} }
@@ -357,18 +357,6 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver
/**
* Converts database error to DibiDriverException.
*
* @throws DibiDriverException
*/
protected function throwException($sql = NULL)
{
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
}
/** /**
* Returns the connection resource. * Returns the connection resource.
* *

View File

@@ -122,7 +122,8 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver
throw new DibiDriverException($err['message'], $err['code'], $sql); throw new DibiDriverException($err['message'], $err['code'], $sql);
} }
} else { } else {
$this->throwException($sql); $err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code'], $sql);
} }
return is_resource($this->resultSet) ? clone $this : NULL; return is_resource($this->resultSet) ? clone $this : NULL;
@@ -174,7 +175,8 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver
public function commit() public function commit()
{ {
if (!oci_commit($this->connection)) { if (!oci_commit($this->connection)) {
$this->throwException(); $err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code']);
} }
$this->autocommit = TRUE; $this->autocommit = TRUE;
} }
@@ -189,7 +191,8 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver
public function rollback() public function rollback()
{ {
if (!oci_rollback($this->connection)) { if (!oci_rollback($this->connection)) {
$this->throwException(); $err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code']);
} }
$this->autocommit = TRUE; $this->autocommit = TRUE;
} }
@@ -339,19 +342,6 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver
/**
* Converts database error to DibiDriverException.
*
* @throws DibiDriverException
*/
protected function throwException($sql = NULL)
{
$err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code'], $sql);
}
/** /**
* Returns the connection resource. * Returns the connection resource.
* *

View File

@@ -132,7 +132,8 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
$this->affectedRows = $this->connection->exec($sql); $this->affectedRows = $this->connection->exec($sql);
if ($this->affectedRows === FALSE) { if ($this->affectedRows === FALSE) {
$this->throwException($sql); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql);
} }
return NULL; return NULL;
@@ -142,7 +143,8 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
$this->affectedRows = FALSE; $this->affectedRows = FALSE;
if ($this->resultSet === FALSE) { if ($this->resultSet === FALSE) {
$this->throwException($sql); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql);
} }
return clone $this; return clone $this;
@@ -183,7 +185,8 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
public function begin() public function begin()
{ {
if (!$this->connection->beginTransaction()) { if (!$this->connection->beginTransaction()) {
$this->throwException(); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
} }
} }
@@ -197,7 +200,8 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
public function commit() public function commit()
{ {
if (!$this->connection->commit()) { if (!$this->connection->commit()) {
$this->throwException(); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
} }
} }
@@ -211,7 +215,8 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
public function rollback() public function rollback()
{ {
if (!$this->connection->rollBack()) { if (!$this->connection->rollBack()) {
$this->throwException(); $err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1]);
} }
} }
@@ -379,19 +384,6 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver
/**
* Converts database error to DibiDriverException.
*
* @throws DibiDriverException
*/
protected function throwException($sql = NULL)
{
$err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql);
}
/** /**
* Returns the connection resource. * Returns the connection resource.
* *