|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 /* 00003 V5.14 8 Sept 2011 (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved. 00004 Released under both BSD license and Lesser GPL library license. 00005 Whenever there is any discrepancy between the two licenses, 00006 the BSD license will take precedence. 00007 00008 Latest version is available at http://adodb.sourceforge.net 00009 00010 Oracle data driver. Requires Oracle client. Works on Windows and Unix and Oracle 7. 00011 00012 If you are using Oracle 8 or later, use the oci8 driver which is much better and more reliable. 00013 */ 00014 00015 // security - hide paths 00016 if (!defined('ADODB_DIR')) die(); 00017 00018 class ADODB_oracle extends ADOConnection { 00019 var $databaseType = "oracle"; 00020 var $replaceQuote = "''"; // string to use to replace quotes 00021 var $concat_operator='||'; 00022 var $_curs; 00023 var $_initdate = true; // init date to YYYY-MM-DD 00024 var $metaTablesSQL = 'select table_name from cat'; 00025 var $metaColumnsSQL = "select cname,coltype,width from col where tname='%s' order by colno"; 00026 var $sysDate = "TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD')"; 00027 var $sysTimeStamp = 'SYSDATE'; 00028 var $connectSID = true; 00029 00030 function ADODB_oracle() 00031 { 00032 } 00033 00034 // format and return date string in database date format 00035 function DBDate($d) 00036 { 00037 if (is_string($d)) $d = ADORecordSet::UnixDate($d); 00038 if (is_object($d)) $ds = $d->format($this->fmtDate); 00039 else $ds = adodb_date($this->fmtDate,$d); 00040 return 'TO_DATE('.$ds.",'YYYY-MM-DD')"; 00041 } 00042 00043 // format and return date string in database timestamp format 00044 function DBTimeStamp($ts) 00045 { 00046 00047 if (is_string($ts)) $ts = ADORecordSet::UnixTimeStamp($ts); 00048 if (is_object($ts)) $ds = $ts->format($this->fmtDate); 00049 else $ds = adodb_date($this->fmtTimeStamp,$ts); 00050 return 'TO_DATE('.$ds.",'RRRR-MM-DD, HH:MI:SS AM')"; 00051 } 00052 00053 00054 function BindDate($d) 00055 { 00056 $d = ADOConnection::DBDate($d); 00057 if (strncmp($d,"'",1)) return $d; 00058 00059 return substr($d,1,strlen($d)-2); 00060 } 00061 00062 function BindTimeStamp($d) 00063 { 00064 $d = ADOConnection::DBTimeStamp($d); 00065 if (strncmp($d,"'",1)) return $d; 00066 00067 return substr($d,1,strlen($d)-2); 00068 } 00069 00070 00071 00072 function BeginTrans() 00073 { 00074 $this->autoCommit = false; 00075 ora_commitoff($this->_connectionID); 00076 return true; 00077 } 00078 00079 00080 function CommitTrans($ok=true) 00081 { 00082 if (!$ok) return $this->RollbackTrans(); 00083 $ret = ora_commit($this->_connectionID); 00084 ora_commiton($this->_connectionID); 00085 return $ret; 00086 } 00087 00088 00089 function RollbackTrans() 00090 { 00091 $ret = ora_rollback($this->_connectionID); 00092 ora_commiton($this->_connectionID); 00093 return $ret; 00094 } 00095 00096 00097 /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */ 00098 function ErrorMsg() 00099 { 00100 if ($this->_errorMsg !== false) return $this->_errorMsg; 00101 00102 if (is_resource($this->_curs)) $this->_errorMsg = @ora_error($this->_curs); 00103 if (empty($this->_errorMsg)) $this->_errorMsg = @ora_error($this->_connectionID); 00104 return $this->_errorMsg; 00105 } 00106 00107 00108 function ErrorNo() 00109 { 00110 if ($this->_errorCode !== false) return $this->_errorCode; 00111 00112 if (is_resource($this->_curs)) $this->_errorCode = @ora_errorcode($this->_curs); 00113 if (empty($this->_errorCode)) $this->_errorCode = @ora_errorcode($this->_connectionID); 00114 return $this->_errorCode; 00115 } 00116 00117 00118 00119 // returns true or false 00120 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0) 00121 { 00122 if (!function_exists('ora_plogon')) return null; 00123 00124 // <G. Giunta 2003/03/03/> Reset error messages before connecting 00125 $this->_errorMsg = false; 00126 $this->_errorCode = false; 00127 00128 // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set 00129 // the oracle home to the host name of remote DB? 00130 // if ($argHostname) putenv("ORACLE_HOME=$argHostname"); 00131 00132 if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi> 00133 if (empty($argDatabasename)) $argDatabasename = $argHostname; 00134 else { 00135 if(strpos($argHostname,":")) { 00136 $argHostinfo=explode(":",$argHostname); 00137 $argHostname=$argHostinfo[0]; 00138 $argHostport=$argHostinfo[1]; 00139 } else { 00140 $argHostport="1521"; 00141 } 00142 00143 00144 if ($this->connectSID) { 00145 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname 00146 .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))"; 00147 } else 00148 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname 00149 .")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))"; 00150 } 00151 00152 } 00153 00154 if ($argDatabasename) $argUsername .= "@$argDatabasename"; 00155 00156 //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>"; 00157 if ($mode == 1) 00158 $this->_connectionID = ora_plogon($argUsername,$argPassword); 00159 else 00160 $this->_connectionID = ora_logon($argUsername,$argPassword); 00161 if ($this->_connectionID === false) return false; 00162 if ($this->autoCommit) ora_commiton($this->_connectionID); 00163 if ($this->_initdate) { 00164 $rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'"); 00165 if ($rs) ora_close($rs); 00166 } 00167 00168 return true; 00169 } 00170 00171 00172 // returns true or false 00173 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename) 00174 { 00175 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1); 00176 } 00177 00178 00179 // returns query ID if successful, otherwise false 00180 function _query($sql,$inputarr=false) 00181 { 00182 // <G. Giunta 2003/03/03/> Reset error messages before executing 00183 $this->_errorMsg = false; 00184 $this->_errorCode = false; 00185 00186 $curs = ora_open($this->_connectionID); 00187 00188 if ($curs === false) return false; 00189 $this->_curs = $curs; 00190 if (!ora_parse($curs,$sql)) return false; 00191 if (ora_exec($curs)) return $curs; 00192 // <G. Giunta 2004/03/03> before we close the cursor, we have to store the error message 00193 // that we can obtain ONLY from the cursor (and not from the connection) 00194 $this->_errorCode = @ora_errorcode($curs); 00195 $this->_errorMsg = @ora_error($curs); 00196 // </G. Giunta 2004/03/03> 00197 @ora_close($curs); 00198 return false; 00199 } 00200 00201 00202 // returns true or false 00203 function _close() 00204 { 00205 return @ora_logoff($this->_connectionID); 00206 } 00207 00208 00209 00210 } 00211 00212 00213 /*-------------------------------------------------------------------------------------- 00214 Class Name: Recordset 00215 --------------------------------------------------------------------------------------*/ 00216 00217 class ADORecordset_oracle extends ADORecordSet { 00218 00219 var $databaseType = "oracle"; 00220 var $bind = false; 00221 00222 function ADORecordset_oracle($queryID,$mode=false) 00223 { 00224 00225 if ($mode === false) { 00226 global $ADODB_FETCH_MODE; 00227 $mode = $ADODB_FETCH_MODE; 00228 } 00229 $this->fetchMode = $mode; 00230 00231 $this->_queryID = $queryID; 00232 00233 $this->_inited = true; 00234 $this->fields = array(); 00235 if ($queryID) { 00236 $this->_currentRow = 0; 00237 $this->EOF = !$this->_fetch(); 00238 @$this->_initrs(); 00239 } else { 00240 $this->_numOfRows = 0; 00241 $this->_numOfFields = 0; 00242 $this->EOF = true; 00243 } 00244 00245 return $this->_queryID; 00246 } 00247 00248 00249 00250 /* Returns: an object containing field information. 00251 Get column information in the Recordset object. fetchField() can be used in order to obtain information about 00252 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by 00253 fetchField() is retrieved. */ 00254 00255 function FetchField($fieldOffset = -1) 00256 { 00257 $fld = new ADOFieldObject; 00258 $fld->name = ora_columnname($this->_queryID, $fieldOffset); 00259 $fld->type = ora_columntype($this->_queryID, $fieldOffset); 00260 $fld->max_length = ora_columnsize($this->_queryID, $fieldOffset); 00261 return $fld; 00262 } 00263 00264 /* Use associative array to get fields array */ 00265 function Fields($colname) 00266 { 00267 if (!$this->bind) { 00268 $this->bind = array(); 00269 for ($i=0; $i < $this->_numOfFields; $i++) { 00270 $o = $this->FetchField($i); 00271 $this->bind[strtoupper($o->name)] = $i; 00272 } 00273 } 00274 00275 return $this->fields[$this->bind[strtoupper($colname)]]; 00276 } 00277 00278 function _initrs() 00279 { 00280 $this->_numOfRows = -1; 00281 $this->_numOfFields = @ora_numcols($this->_queryID); 00282 } 00283 00284 00285 function _seek($row) 00286 { 00287 return false; 00288 } 00289 00290 function _fetch($ignore_fields=false) { 00291 // should remove call by reference, but ora_fetch_into requires it in 4.0.3pl1 00292 if ($this->fetchMode & ADODB_FETCH_ASSOC) 00293 return @ora_fetch_into($this->_queryID,$this->fields,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC); 00294 else 00295 return @ora_fetch_into($this->_queryID,$this->fields,ORA_FETCHINTO_NULLS); 00296 } 00297 00298 /* close() only needs to be called if you are worried about using too much memory while your script 00299 is running. All associated result memory for the specified result identifier will automatically be freed. */ 00300 00301 function _close() 00302 { 00303 return @ora_close($this->_queryID); 00304 } 00305 00306 function MetaType($t,$len=-1) 00307 { 00308 if (is_object($t)) { 00309 $fieldobj = $t; 00310 $t = $fieldobj->type; 00311 $len = $fieldobj->max_length; 00312 } 00313 00314 switch (strtoupper($t)) { 00315 case 'VARCHAR': 00316 case 'VARCHAR2': 00317 case 'CHAR': 00318 case 'VARBINARY': 00319 case 'BINARY': 00320 if ($len <= $this->blobSize) return 'C'; 00321 case 'LONG': 00322 case 'LONG VARCHAR': 00323 case 'CLOB': 00324 return 'X'; 00325 case 'LONG RAW': 00326 case 'LONG VARBINARY': 00327 case 'BLOB': 00328 return 'B'; 00329 00330 case 'DATE': return 'D'; 00331 00332 //case 'T': return 'T'; 00333 00334 case 'BIT': return 'L'; 00335 case 'INT': 00336 case 'SMALLINT': 00337 case 'INTEGER': return 'I'; 00338 default: return 'N'; 00339 } 00340 } 00341 } 00342 ?>