OCI8Connection.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: OCI8Connection.php,v 1.1 2004/05/09 21:33:45 micha 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 00036 class OCI8Connection extends ConnectionCommon 00037 { 00038 var $lastStmt = null; 00039 00048 function connect($dsninfo, $flags = 0) 00049 { 00050 if (!extension_loaded('oci8')) { 00051 return new SQLException(CREOLE_ERROR_EXTENSION_NOT_FOUND, '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 return new SQLException(CREOLE_ERROR_CONNECT_FAILED, "connect failed", $err); 00079 } 00080 00081 $this->dblink = $conn; 00082 return true; 00083 } 00084 00085 00089 function close() 00090 { 00091 @OCILogOff($this->dblink); 00092 } 00093 00097 function & executeQuery($sql, $fetchmode = null) 00098 { 00099 $this->lastQuery = $sql; 00100 00101 $result = @OCIParse($this->dblink, $sql); 00102 00103 if (!$result) { 00104 return new SQLException(CREOLE_ERROR, "Unable to prepare query", $this->nativeError(), $sql); 00105 } 00106 00107 $success = @OCIExecute($result,OCI_DEFAULT); 00108 if (!$success) { 00109 return new SQLException(CREOLE_ERROR, "Unable to execute query", $this->nativeError($result), $sql); 00110 } 00111 00112 return new OCI8ResultSet($this, $result, $fetchmode); 00113 } 00114 00115 00119 function executeUpdate($sql) 00120 { 00121 $this->lastQuery = $sql; 00122 00123 $statement = @OCIParse($this->dblink, $sql); 00124 if (!$statement) { 00125 return new SQLException(CREOLE_ERROR, "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 return new SQLException(CREOLE_ERROR, "Unable to execute update", $this->nativeError($statement), $sql); 00137 } 00138 00139 $this->lastStmt = $statement; 00140 00141 return @OCIRowCount($statement); 00142 } 00143 00148 function commit() 00149 { 00150 if ($this->transactionOpcount > 0) { 00151 $result = @OCICommit($this->dblink); 00152 $this->transactionOpcount = 0; 00153 if (!$result) { 00154 return new SQLException(CREOLE_ERROR, "Unable to commit transaction", $this->nativeError()); 00155 } 00156 } 00157 return true; 00158 } 00159 00160 00165 function rollback() 00166 { 00167 if ($this->transactionOpcount > 0) { 00168 $result = @OCIRollback($this->dblink); 00169 if (!$result) { 00170 return new SQLException(CREOLE_ERROR, "Unable to rollback transaction", $this->nativeError()); 00171 } 00172 } 00173 return true; 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 return new SQLException(CREOLE_ERROR, "Update count failed", $this->nativeError($this->lastStmt)); 00192 } 00193 return $result; 00194 } 00195 00196 00215 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 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 function & getDatabaseInfo() 00251 { 00252 require_once 'creole/drivers/oracle/metadata/OCI8DatabaseInfo.php'; 00253 return new OCI8DatabaseInfo($this); 00254 } 00255 00259 function & getIdGenerator() 00260 { 00261 require_once 'creole/drivers/oracle/OCI8IdGenerator.php'; 00262 return new OCI8IdGenerator($this); 00263 } 00264 00273 function & prepareStatement($sql) 00274 { 00275 require_once 'creole/drivers/oracle/OCI8PreparedStatement.php'; 00276 return new OCI8PreparedStatement($this, $sql); 00277 } 00278 00282 function prepareCall($sql) 00283 { 00284 return new SQLException(CREOLE_ERROR, 'Oracle driver does not yet support stored procedures using CallableStatement.'); 00285 } 00286 00290 function & createStatement() 00291 { 00292 require_once 'creole/drivers/oracle/OCI8Statement.php'; 00293 return new OCI8Statement($this); 00294 } 00295 }

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


Copyright © 2004 Hans Lellelid  
Creole[php4] CVS