From 8e5a360419ded2816b4774531fbb70a78f411612 Mon Sep 17 00:00:00 2001 From: Miloslav Kmet Date: Thu, 21 Oct 2010 22:29:33 +0200 Subject: [PATCH 1/2] Added new parameters into OCI8Connection constructor - $charset and $session_mode Charset is 4th parameter of oci_connect and cannot be specified as part of TNS string like in PDOOci. I've added session_mode param, because OCI_DEFAULT mode is insufficient when new user/database will be created and it is necessary to connect as OCI_SYSDBA. I saw some CREATE USER statements in schema manager, so this param is just in case. Fixed error handling in OCI8Connection. When oci_connect failed, $_dbh is boolean, so cannot be used in oci_error(); --- lib/Doctrine/DBAL/Driver/OCI8/Driver.php | 8 +++----- lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/Doctrine/DBAL/Driver/OCI8/Driver.php b/lib/Doctrine/DBAL/Driver/OCI8/Driver.php index 4ff6d72d12d..ddba3bf94af 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/Driver.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/Driver.php @@ -36,7 +36,9 @@ public function connect(array $params, $username = null, $password = null, array return new OCI8Connection( $username, $password, - $this->_constructDsn($params) + $this->_constructDsn($params), + isset($params['charset']) ? $params['charset'] : null, + isset($params['session_mode']) ? $params['session_mode'] : OCI_DEFAULT ); } @@ -67,10 +69,6 @@ private function _constructDsn(array $params) $dsn .= $params['dbname']; } - if (isset($params['charset'])) { - $dsn .= ';charset=' . $params['charset']; - } - return $dsn; } diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php index 987bf6c0c76..82ba0438c2b 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php @@ -30,11 +30,11 @@ class OCI8Connection implements \Doctrine\DBAL\Driver\Connection { private $_dbh; - public function __construct($username, $password, $db) + public function __construct($username, $password, $db, $charset = null, $session_mode = OCI_DEFAULT) { - $this->_dbh = @oci_connect($username, $password, $db); + $this->_dbh = @oci_connect($username, $password, $db, $charset, $session_mode); if (!$this->_dbh) { - throw new OCI8Exception($this->errorInfo()); + throw OCI8Exception::fromErrorInfo($this->errorInfo()); } } @@ -93,7 +93,12 @@ public function rollBack() public function errorCode() { - $error = oci_error($this->_dbh); + if (!is_resource($this->_dbh)) { + $error = oci_error(); + } else { + $error = oci_error($this->_dbh); + } + if ($error !== false) { $error = $error['code']; } @@ -102,6 +107,9 @@ public function errorCode() public function errorInfo() { + if (!is_resource($this->_dbh)) { + return oci_error(); + } return oci_error($this->_dbh); } From 8ade1d9d7f224440ef943b6bc18bf3e2497ac8f2 Mon Sep 17 00:00:00 2001 From: Miloslav Kmet Date: Thu, 21 Oct 2010 22:58:31 +0200 Subject: [PATCH 2/2] OCI8Statement is using for all statements OCI_DEFAULT which is the same like OCI_NO_AUTO_COMMIT - therefor any changes that are made without transaction are rolled back. Now the OCI8 Driver act like any other driver - commit after execute by default, and starting transaction when invoked $conn->beginTransaction() --- .../DBAL/Driver/OCI8/OCI8Connection.php | 17 ++++++++++++++++- lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php | 8 +++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php index 82ba0438c2b..cfbb5962769 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Connection.php @@ -30,6 +30,8 @@ class OCI8Connection implements \Doctrine\DBAL\Driver\Connection { private $_dbh; + private $_executionMode = OCI_COMMIT_ON_SUCCESS; + public function __construct($username, $password, $db, $charset = null, $session_mode = OCI_DEFAULT) { $this->_dbh = @oci_connect($username, $password, $db, $charset, $session_mode); @@ -40,7 +42,7 @@ public function __construct($username, $password, $db, $charset = null, $session public function prepare($prepareString) { - return new OCI8Statement($this->_dbh, $prepareString); + return new OCI8Statement($this, $prepareString); } public function query() @@ -72,11 +74,13 @@ public function lastInsertId($name = null) public function beginTransaction() { + $this->_executionMode = OCI_NO_AUTO_COMMIT; return true; } public function commit() { + $this->_executionMode = OCI_COMMIT_ON_SUCCESS; if (!oci_commit($this->_dbh)) { throw OCI8Exception::fromErrorInfo($this->errorInfo()); } @@ -85,6 +89,7 @@ public function commit() public function rollBack() { + $this->_executionMode = OCI_COMMIT_ON_SUCCESS; if (!oci_rollback($this->_dbh)) { throw OCI8Exception::fromErrorInfo($this->errorInfo()); } @@ -113,4 +118,14 @@ public function errorInfo() return oci_error($this->_dbh); } + public function getDbh() + { + return $this->_dbh; + } + + public function getExecutionMode() + { + return $this->_executionMode; + } + } \ No newline at end of file diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php index d1168d12468..fdee78316f7 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Statement.php @@ -41,6 +41,7 @@ class OCI8Statement implements \Doctrine\DBAL\Driver\Statement PDO::FETCH_NUM => OCI_NUM ); private $_paramMap = array(); + private $_connection; /** * Creates a new OCI8Statement that uses the given connection handle and SQL statement. @@ -48,9 +49,10 @@ class OCI8Statement implements \Doctrine\DBAL\Driver\Statement * @param resource $dbh The connection handle. * @param string $statement The SQL statement. */ - public function __construct($dbh, $statement) + public function __construct($connection, $statement) { - $this->_sth = oci_parse($dbh, $this->_convertPositionalToNamedPlaceholders($statement)); + $this->_connection = $connection; + $this->_sth = oci_parse($connection->getDbh(), $this->_convertPositionalToNamedPlaceholders($statement)); } /** @@ -146,7 +148,7 @@ public function execute($params = null) } } - $ret = @oci_execute($this->_sth, OCI_DEFAULT); + $ret = @oci_execute($this->_sth, $this->_connection->getExecutionMode()); if ( ! $ret) { throw OCI8Exception::fromErrorInfo($this->errorInfo()); }