OCI8Connection.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: OCI8Connection.php,v 1.13 2004/03/20 04:16:50 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00022 require_once 'creole/Connection.php'; 00023 require_once 'creole/common/ConnectionCommon.php'; 00024 00035 class OCI8Connection extends ConnectionCommon implements Connection { 00036 00037 protected $lastStmt = null; 00038 00048 function connect($dsninfo, $flags = 0) 00049 { 00050 if (!extension_loaded('oci8')) { 00051 throw new SQLException('oci8 extension not loaded'); 00052 } 00053 00054 $this->dsn = $dsninfo; 00055 $this->flags = $flags; 00056 00057 $persistent = ($flags & Creole::PERSISTENT === Creole::PERSISTENT); 00058 00059 $user = $dsninfo['username']; 00060 $pw = $dsninfo['password']; 00061 $hostspec = $dsninfo['hostspec']; 00062 00063 $connect_function = $persistent ? 'OCIPLogon' : 'OCILogon'; 00064 00065 @ini_set('track_errors', true); 00066 if ($hostspec && $user && $pw) { 00067 $conn = @$connect_function($user, $pw, $hostspec); 00068 } elseif ($user || $pw) { 00069 $conn = @$connect_function($user, $pw); 00070 } else { 00071 $conn = false; 00072 } 00073 @ini_restore('track_errors'); 00074 00075 if ($conn == false) { 00076 $error = @OCIError(); 00077 $error = (is_array($error)) ? $error['message'] : null; 00078 throw new SQLException("connect failed", $err); 00079 } 00080 00081 $this->dblink = $conn; 00082 } 00083 00084 00088 function close() 00089 { 00090 @OCILogOff($this->dblink); 00091 } 00092 00096 function executeQuery($sql, $fetchmode = null) 00097 { 00098 $this->lastQuery = $sql; 00099 00100 $result = @OCIParse($this->dblink, $sql); 00101 00102 if (!$result) { 00103 throw new SQLException("Unable to prepare query", $this->nativeError(), $sql); 00104 } 00105 00106 $success = @OCIExecute($result,OCI_DEFAULT); 00107 if (!$success) { 00108 throw new SQLException("Unable to execute query", $this->nativeError($result), $sql); 00109 } 00110 00111 return new OCI8ResultSet($this, $result, $fetchmode); 00112 } 00113 00114 00119 function executeUpdate($sql) 00120 { 00121 $this->lastQuery = $sql; 00122 00123 $statement = @OCIParse($this->dblink, $sql); 00124 if (!$statement) { 00125 throw new SQLException("Unable to prepare update", $this->nativeError(), $sql); 00126 } 00127 00128 if ($this->autocommit) { 00129 $success = @OCIExecute($statement, OCI_COMMIT_ON_SUCCESS); 00130 } else { 00131 $success = @OCIExecute($statement, OCI_DEFAULT); 00132 $this->transactionOpcount++; 00133 } 00134 00135 if (!$success) { 00136 throw new SQLException("Unable to execute update", $this->nativeError($statement), $sql); 00137 } 00138 00139 $this->lastStmt = $statement; 00140 00141 return @OCIRowCount($statement); 00142 } 00143 00147 function commit() 00148 { 00149 if ($this->transactionOpcount > 0) { 00150 $result = @OCICommit($this->dblink); 00151 $this->transactionOpcount = 0; 00152 if (!$result) { 00153 throw new SQLException("Unable to commit transaction", $this->nativeError()); 00154 } 00155 return true; 00156 } 00157 } 00158 00159 00165 function rollback() 00166 { 00167 if ($this->transactionOpcount > 0) { 00168 $result = @OCIRollback($this->dblink); 00169 if (!$result) { 00170 throw new SQLException("Unable to rollback transaction", $this->nativeError()); 00171 } 00172 return true; 00173 } 00174 } 00175 00176 00184 function getUpdateCount() 00185 { 00186 if (!$this->lastStmt) { 00187 return 0; 00188 } 00189 $result = @OCIRowCount($this->lastStmt); 00190 if ($result === false) { 00191 throw new SQLException("Update count failed", $this->nativeError($this->lastStmt)); 00192 } 00193 return $result; 00194 } 00195 00196 00215 public function applyLimit(&$sql, $offset, $limit) 00216 { 00217 $sql = 'SELECT B.* FROM ( ' . 00218 'SELECT A.*, rownum AS CREOLE$ROWNUM FROM ( ' . $sql . ' ) A ' . 00219 ') B WHERE '; 00220 if ($offset > 0) { 00221 $sql .= ' B.CREOLE$ROWNUM > ' . $offset; 00222 if ($limit > 0) { 00223 $sql .= ' AND B.CREOLE$ROWNUM <= ' . ($offset + $limit); 00224 } 00225 } else { 00226 $sql .= ' B.CREOLE$ROWNUM <= ' . $limit; 00227 } 00228 } 00229 00236 public function nativeError($result = null) 00237 { 00238 if ($result !== null) { 00239 $error = OCIError($result); 00240 } else { 00241 $error = OCIError($this->dblink); 00242 } 00243 return $error['code'] . ": " . $error['message']; 00244 } 00245 00246 00250 public function getDatabaseInfo() 00251 { 00252 require_once 'creole/drivers/oracle/metadata/OCI8DatabaseInfo.php'; 00253 return new OCI8DatabaseInfo($this); 00254 } 00255 00259 public function getIdGenerator() 00260 { 00261 require_once 'creole/drivers/oracle/OCI8IdGenerator.php'; 00262 return new OCI8IdGenerator($this); 00263 } 00264 00273 public function prepareStatement($sql) 00274 { 00275 require_once 'creole/drivers/oracle/OCI8PreparedStatement.php'; 00276 return new OCI8PreparedStatement($this, $sql); 00277 } 00278 00282 public function prepareCall($sql) { 00283 throw new SQLException('Oracle driver does not yet support stored procedures using CallableStatement.'); 00284 } 00285 00289 public function createStatement() 00290 { 00291 require_once 'creole/drivers/oracle/OCI8Statement.php'; 00292 return new OCI8Statement($this); 00293 } 00294 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS