3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\DBAL\Driver\OCI8;
22 use Doctrine\DBAL\Platforms\OraclePlatform;
25 * OCI8 implementation of the Connection interface.
29 class OCI8Connection implements \Doctrine\DBAL\Driver\Connection
39 protected $executeMode = OCI_COMMIT_ON_SUCCESS;
42 * Create a Connection to an Oracle Database using oci8 extension.
44 * @param string $username
45 * @param string $password
48 public function __construct($username, $password, $db, $charset = null, $sessionMode = OCI_DEFAULT, $persistent = false)
50 if (!defined('OCI_NO_AUTO_COMMIT')) {
51 define('OCI_NO_AUTO_COMMIT', 0);
54 $this->dbh = $persistent
55 ? @oci_pconnect($username, $password, $db, $charset, $sessionMode)
56 : @oci_connect($username, $password, $db, $charset, $sessionMode);
59 throw OCI8Exception::fromErrorInfo(oci_error());
64 * Create a non-executed prepared statement.
66 * @param string $prepareString
67 * @return OCI8Statement
69 public function prepare($prepareString)
71 return new OCI8Statement($this->dbh, $prepareString, $this);
76 * @return OCI8Statement
78 public function query()
80 $args = func_get_args();
82 //$fetchMode = $args[1];
83 $stmt = $this->prepare($sql);
92 * @param int $type PDO::PARAM*
95 public function quote($value, $type=\PDO::PARAM_STR)
97 if (is_int($value) || is_float($value)) {
100 $value = str_replace("'", "''", $value);
101 return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
106 * @param string $statement
109 public function exec($statement)
111 $stmt = $this->prepare($statement);
113 return $stmt->rowCount();
119 public function lastInsertId($name = null)
121 if ($name === null) {
125 OraclePlatform::assertValidIdentifier($name);
127 $sql = 'SELECT ' . $name . '.CURRVAL FROM DUAL';
128 $stmt = $this->query($sql);
129 $result = $stmt->fetch(\PDO::FETCH_ASSOC);
131 if ($result === false || !isset($result['CURRVAL'])) {
132 throw new OCI8Exception("lastInsertId failed: Query was executed but no result was returned.");
135 return (int) $result['CURRVAL'];
139 * Return the current execution mode.
141 public function getExecuteMode()
143 return $this->executeMode;
147 * Start a transactiom
149 * Oracle has to explicitly set the autocommit mode off. That means
150 * after connection, a commit or rollback there is always automatically
151 * opened a new transaction.
155 public function beginTransaction()
157 $this->executeMode = OCI_NO_AUTO_COMMIT;
162 * @throws OCI8Exception
165 public function commit()
167 if (!oci_commit($this->dbh)) {
168 throw OCI8Exception::fromErrorInfo($this->errorInfo());
170 $this->executeMode = OCI_COMMIT_ON_SUCCESS;
175 * @throws OCI8Exception
178 public function rollBack()
180 if (!oci_rollback($this->dbh)) {
181 throw OCI8Exception::fromErrorInfo($this->errorInfo());
183 $this->executeMode = OCI_COMMIT_ON_SUCCESS;
187 public function errorCode()
189 $error = oci_error($this->dbh);
190 if ($error !== false) {
191 $error = $error['code'];
196 public function errorInfo()
198 return oci_error($this->dbh);