From f935968aa7b0ff8186a881c3ac0274b2baf311e4 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 4 Sep 2008 18:18:58 +0000 Subject: [PATCH] refactoring in drivers --- dibi/drivers/mysql.php | 30 +++++++++++++----------------- dibi/drivers/mysqli.php | 26 ++++++++++---------------- dibi/drivers/odbc.php | 20 ++++---------------- dibi/drivers/oracle.php | 22 ++++++---------------- dibi/drivers/pdo.php | 28 ++++++++++------------------ 5 files changed, 43 insertions(+), 83 deletions(-) diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 2a3fbf39..1b2dff21 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -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) $ok = @mysql_set_charset($config['charset'], $this->connection); // intentionally @ } - if (!$ok) $ok = @mysql_query("SET NAMES '$config[charset]'", $this->connection); // intentionally @ - if (!$ok) $this->throwException(); + if (!$ok) { + $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'])) { - @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 (!@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']); @@ -171,7 +179,7 @@ class DibiMySqlDriver extends /*Nette::*/Object implements IDibiDriver } 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; @@ -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. * diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 50e370f0..e5fb2d4a 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -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) $ok = @mysqli_set_charset($this->connection, $config['charset']); // intentionally @ } - if (!$ok) $ok = @mysqli_query($this->connection, "SET NAMES '$config[charset]'"); // intentionally @ - if (!$ok) $this->throwException(); + if (!$ok) { + $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 (!@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']); @@ -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 @ 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; @@ -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. * diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 52e70440..f91b8fe5 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -123,7 +123,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver $this->resultSet = @odbc_exec($this->connection, $sql); // intentionally @ 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; @@ -163,7 +163,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver public function begin() { 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() { if (!odbc_commit($this->connection)) { - $this->throwException(); + throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); } odbc_autocommit($this->connection, TRUE); } @@ -192,7 +192,7 @@ class DibiOdbcDriver extends /*Nette::*/Object implements IDibiDriver public function rollback() { if (!odbc_rollback($this->connection)) { - $this->throwException(); + throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); } 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. * diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index 993e68e6..7e9fcc8c 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -122,7 +122,8 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver throw new DibiDriverException($err['message'], $err['code'], $sql); } } 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; @@ -174,7 +175,8 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver public function commit() { if (!oci_commit($this->connection)) { - $this->throwException(); + $err = oci_error($this->connection); + throw new DibiDriverException($err['message'], $err['code']); } $this->autocommit = TRUE; } @@ -189,7 +191,8 @@ class DibiOracleDriver extends /*Nette::*/Object implements IDibiDriver public function rollback() { if (!oci_rollback($this->connection)) { - $this->throwException(); + $err = oci_error($this->connection); + throw new DibiDriverException($err['message'], $err['code']); } $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. * diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 5f50829b..b192ea3e 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -132,7 +132,8 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver $this->affectedRows = $this->connection->exec($sql); if ($this->affectedRows === FALSE) { - $this->throwException($sql); + $err = $this->connection->errorInfo(); + throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql); } return NULL; @@ -142,7 +143,8 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver $this->affectedRows = 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; @@ -183,7 +185,8 @@ class DibiPdoDriver extends /*Nette::*/Object implements IDibiDriver public function begin() { 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() { 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() { 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. *