MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / lib / adodb / drivers / adodb-oracle.inc.php
blob3c9023ad030e262189ef403cd2aa502269e532de
1 <?php
2 /*
3 V4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence.
8 Latest version is available at http://adodb.sourceforge.net
10 Oracle data driver. Requires Oracle client. Works on Windows and Unix and Oracle 7.
12 If you are using Oracle 8 or later, use the oci8 driver which is much better and more reliable.
15 // security - hide paths
16 if (!defined('ADODB_DIR')) die();
18 class ADODB_oracle extends ADOConnection {
19 var $databaseType = "oracle";
20 var $replaceQuote = "''"; // string to use to replace quotes
21 var $concat_operator='||';
22 var $_curs;
23 var $_initdate = true; // init date to YYYY-MM-DD
24 var $metaTablesSQL = 'select table_name from cat';
25 var $metaColumnsSQL = "select cname,coltype,width from col where tname='%s' order by colno";
26 var $sysDate = "TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD')";
27 var $sysTimeStamp = 'SYSDATE';
28 var $connectSID = true;
30 function ADODB_oracle()
34 // format and return date string in database date format
35 function DBDate($d)
37 if (is_string($d)) $d = ADORecordSet::UnixDate($d);
38 return 'TO_DATE('.adodb_date($this->fmtDate,$d).",'YYYY-MM-DD')";
41 // format and return date string in database timestamp format
42 function DBTimeStamp($ts)
45 if (is_string($ts)) $d = ADORecordSet::UnixTimeStamp($ts);
46 return 'TO_DATE('.adodb_date($this->fmtTimeStamp,$ts).",'RRRR-MM-DD, HH:MI:SS AM')";
50 function BindDate($d)
52 $d = ADOConnection::DBDate($d);
53 if (strncmp($d,"'",1)) return $d;
55 return substr($d,1,strlen($d)-2);
58 function BindTimeStamp($d)
60 $d = ADOConnection::DBTimeStamp($d);
61 if (strncmp($d,"'",1)) return $d;
63 return substr($d,1,strlen($d)-2);
68 function BeginTrans()
70 $this->autoCommit = false;
71 ora_commitoff($this->_connectionID);
72 return true;
76 function CommitTrans($ok=true)
78 if (!$ok) return $this->RollbackTrans();
79 $ret = ora_commit($this->_connectionID);
80 ora_commiton($this->_connectionID);
81 return $ret;
85 function RollbackTrans()
87 $ret = ora_rollback($this->_connectionID);
88 ora_commiton($this->_connectionID);
89 return $ret;
93 /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
94 function ErrorMsg()
96 if ($this->_errorMsg !== false) return $this->_errorMsg;
98 if (is_resource($this->_curs)) $this->_errorMsg = @ora_error($this->_curs);
99 if (empty($this->_errorMsg)) $this->_errorMsg = @ora_error($this->_connectionID);
100 return $this->_errorMsg;
104 function ErrorNo()
106 if ($this->_errorCode !== false) return $this->_errorCode;
108 if (is_resource($this->_curs)) $this->_errorCode = @ora_errorcode($this->_curs);
109 if (empty($this->_errorCode)) $this->_errorCode = @ora_errorcode($this->_connectionID);
110 return $this->_errorCode;
115 // returns true or false
116 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
118 if (!function_exists('ora_plogon')) return null;
120 // <G. Giunta 2003/03/03/> Reset error messages before connecting
121 $this->_errorMsg = false;
122 $this->_errorCode = false;
124 // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set
125 // the oracle home to the host name of remote DB?
126 // if ($argHostname) putenv("ORACLE_HOME=$argHostname");
128 if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
129 if (empty($argDatabasename)) $argDatabasename = $argHostname;
130 else {
131 if(strpos($argHostname,":")) {
132 $argHostinfo=explode(":",$argHostname);
133 $argHostname=$argHostinfo[0];
134 $argHostport=$argHostinfo[1];
135 } else {
136 $argHostport="1521";
140 if ($this->connectSID) {
141 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
142 .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
143 } else
144 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
145 .")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
150 if ($argDatabasename) $argUsername .= "@$argDatabasename";
152 //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
153 if ($mode == 1)
154 $this->_connectionID = ora_plogon($argUsername,$argPassword);
155 else
156 $this->_connectionID = ora_logon($argUsername,$argPassword);
157 if ($this->_connectionID === false) return false;
158 if ($this->autoCommit) ora_commiton($this->_connectionID);
159 if ($this->_initdate) {
160 $rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
161 if ($rs) ora_close($rs);
164 return true;
168 // returns true or false
169 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
171 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
175 // returns query ID if successful, otherwise false
176 function _query($sql,$inputarr=false)
178 // <G. Giunta 2003/03/03/> Reset error messages before executing
179 $this->_errorMsg = false;
180 $this->_errorCode = false;
182 $curs = ora_open($this->_connectionID);
184 if ($curs === false) return false;
185 $this->_curs = $curs;
186 if (!ora_parse($curs,$sql)) return false;
187 if (ora_exec($curs)) return $curs;
188 // <G. Giunta 2004/03/03> before we close the cursor, we have to store the error message
189 // that we can obtain ONLY from the cursor (and not from the connection)
190 $this->_errorCode = @ora_errorcode($curs);
191 $this->_errorMsg = @ora_error($curs);
192 // </G. Giunta 2004/03/03>
193 @ora_close($curs);
194 return false;
198 // returns true or false
199 function _close()
201 return @ora_logoff($this->_connectionID);
209 /*--------------------------------------------------------------------------------------
210 Class Name: Recordset
211 --------------------------------------------------------------------------------------*/
213 class ADORecordset_oracle extends ADORecordSet {
215 var $databaseType = "oracle";
216 var $bind = false;
218 function ADORecordset_oracle($queryID,$mode=false)
221 if ($mode === false) {
222 global $ADODB_FETCH_MODE;
223 $mode = $ADODB_FETCH_MODE;
225 $this->fetchMode = $mode;
227 $this->_queryID = $queryID;
229 $this->_inited = true;
230 $this->fields = array();
231 if ($queryID) {
232 $this->_currentRow = 0;
233 $this->EOF = !$this->_fetch();
234 @$this->_initrs();
235 } else {
236 $this->_numOfRows = 0;
237 $this->_numOfFields = 0;
238 $this->EOF = true;
241 return $this->_queryID;
246 /* Returns: an object containing field information.
247 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
248 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
249 fetchField() is retrieved. */
251 function &FetchField($fieldOffset = -1)
253 $fld = new ADOFieldObject;
254 $fld->name = ora_columnname($this->_queryID, $fieldOffset);
255 $fld->type = ora_columntype($this->_queryID, $fieldOffset);
256 $fld->max_length = ora_columnsize($this->_queryID, $fieldOffset);
257 return $fld;
260 /* Use associative array to get fields array */
261 function Fields($colname)
263 if (!$this->bind) {
264 $this->bind = array();
265 for ($i=0; $i < $this->_numOfFields; $i++) {
266 $o = $this->FetchField($i);
267 $this->bind[strtoupper($o->name)] = $i;
271 return $this->fields[$this->bind[strtoupper($colname)]];
274 function _initrs()
276 $this->_numOfRows = -1;
277 $this->_numOfFields = @ora_numcols($this->_queryID);
281 function _seek($row)
283 return false;
286 function _fetch($ignore_fields=false) {
287 // should remove call by reference, but ora_fetch_into requires it in 4.0.3pl1
288 if ($this->fetchMode & ADODB_FETCH_ASSOC)
289 return @ora_fetch_into($this->_queryID,&$this->fields,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC);
290 else
291 return @ora_fetch_into($this->_queryID,&$this->fields,ORA_FETCHINTO_NULLS);
294 /* close() only needs to be called if you are worried about using too much memory while your script
295 is running. All associated result memory for the specified result identifier will automatically be freed. */
297 function _close()
299 return @ora_close($this->_queryID);
302 function MetaType($t,$len=-1)
304 if (is_object($t)) {
305 $fieldobj = $t;
306 $t = $fieldobj->type;
307 $len = $fieldobj->max_length;
310 switch (strtoupper($t)) {
311 case 'VARCHAR':
312 case 'VARCHAR2':
313 case 'CHAR':
314 case 'VARBINARY':
315 case 'BINARY':
316 if ($len <= $this->blobSize) return 'C';
317 case 'LONG':
318 case 'LONG VARCHAR':
319 case 'CLOB':
320 return 'X';
321 case 'LONG RAW':
322 case 'LONG VARBINARY':
323 case 'BLOB':
324 return 'B';
326 case 'DATE': return 'D';
328 //case 'T': return 'T';
330 case 'BIT': return 'L';
331 case 'INT':
332 case 'SMALLINT':
333 case 'INTEGER': return 'I';
334 default: return 'N';