3 V4.20 22 Feb 2004 (c) 2000-2004 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://php.weblogs.com/
10 Oracle data driver. Requires Oracle client. Works on Windows and Unix and Oracle 7 and 8.
12 If you are using Oracle 8, use the oci8 driver which is much better and more reliable.
16 class ADODB_oracle
extends ADOConnection
{
17 var $databaseType = "oracle";
18 var $replaceQuote = "''"; // string to use to replace quotes
19 var $concat_operator='||';
21 var $_initdate = true; // init date to YYYY-MM-DD
22 var $metaTablesSQL = 'select table_name from cat';
23 var $metaColumnsSQL = "select cname,coltype,width from col where tname='%s' order by colno";
24 var $sysDate = "TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD')";
25 var $sysTimeStamp = 'SYSDATE';
26 var $connectSID = true;
28 function ADODB_oracle()
32 // format and return date string in database date format
35 if (is_string($d)) $d = ADORecordSet
::UnixDate($d);
36 return 'TO_DATE('.adodb_date($this->fmtDate
,$d).",'YYYY-MM-DD')";
39 // format and return date string in database timestamp format
40 function DBTimeStamp($ts)
43 if (is_string($ts)) $d = ADORecordSet
::UnixTimeStamp($ts);
44 return 'TO_DATE('.adodb_date($this->fmtTimeStamp
,$ts).",'RRRR-MM-DD, HH:MI:SS AM')";
50 $this->autoCommit
= false;
51 ora_commitoff($this->_connectionID
);
56 function CommitTrans($ok=true)
58 if (!$ok) return $this->RollbackTrans();
59 $ret = ora_commit($this->_connectionID
);
60 ora_commiton($this->_connectionID
);
65 function RollbackTrans()
67 $ret = ora_rollback($this->_connectionID
);
68 ora_commiton($this->_connectionID
);
73 /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
76 $this->_errorMsg
= @ora_error
($this->_curs
);
77 if (!$this->_errorMsg
) $this->_errorMsg
= @ora_error
($this->_connectionID
);
78 return $this->_errorMsg
;
84 $err = @ora_errorcode
($this->_curs
);
85 if (!$err) return @ora_errorcode
($this->_connectionID
);
90 // returns true or false
91 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
93 // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set
94 // the oracle home to the host name of remote DB?
95 // if ($argHostname) putenv("ORACLE_HOME=$argHostname");
97 if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
98 if (empty($argDatabasename)) $argDatabasename = $argHostname;
100 if(strpos($argHostname,":")) {
101 $argHostinfo=explode(":",$argHostname);
102 $argHostname=$argHostinfo[0];
103 $argHostport=$argHostinfo[1];
109 if ($this->connectSID
) {
110 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
111 .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
113 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
114 .")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
119 if ($argDatabasename) $argUsername .= "@$argDatabasename";
121 //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
123 $this->_connectionID
= ora_plogon($argUsername,$argPassword);
125 $this->_connectionID
= ora_logon($argUsername,$argPassword);
126 if ($this->_connectionID
=== false) return false;
127 if ($this->autoCommit
) ora_commiton($this->_connectionID
);
128 if ($this->_initdate
) {
129 $rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
130 if ($rs) ora_close($rs);
137 // returns true or false
138 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
140 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
144 // returns query ID if successful, otherwise false
145 function _query($sql,$inputarr=false)
147 $curs = ora_open($this->_connectionID
);
149 if ($curs === false) return false;
150 $this->_curs
= $curs;
151 if (!ora_parse($curs,$sql)) return false;
152 if (ora_exec($curs)) return $curs;
159 // returns true or false
162 return @ora_logoff
($this->_connectionID
);
170 /*--------------------------------------------------------------------------------------
171 Class Name: Recordset
172 --------------------------------------------------------------------------------------*/
174 class ADORecordset_oracle
extends ADORecordSet
{
176 var $databaseType = "oracle";
179 function ADORecordset_oracle($queryID,$mode=false)
182 if ($mode === false) {
183 global $ADODB_FETCH_MODE;
184 $mode = $ADODB_FETCH_MODE;
186 $this->fetchMode
= $mode;
188 $this->_queryID
= $queryID;
190 $this->_inited
= true;
191 $this->fields
= array();
193 $this->_currentRow
= 0;
194 $this->EOF
= !$this->_fetch();
197 $this->_numOfRows
= 0;
198 $this->_numOfFields
= 0;
202 return $this->_queryID
;
207 /* Returns: an object containing field information.
208 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
209 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
210 fetchField() is retrieved. */
212 function FetchField($fieldOffset = -1)
214 $fld = new ADOFieldObject
;
215 $fld->name
= ora_columnname($this->_queryID
, $fieldOffset);
216 $fld->type
= ora_columntype($this->_queryID
, $fieldOffset);
217 $fld->max_length
= ora_columnsize($this->_queryID
, $fieldOffset);
221 /* Use associative array to get fields array */
222 function Fields($colname)
225 $this->bind
= array();
226 for ($i=0; $i < $this->_numOfFields
; $i++
) {
227 $o = $this->FetchField($i);
228 $this->bind
[strtoupper($o->name
)] = $i;
232 return $this->fields
[$this->bind
[strtoupper($colname)]];
237 $this->_numOfRows
= -1;
238 $this->_numOfFields
= @ora_numcols
($this->_queryID
);
247 function _fetch($ignore_fields=false) {
248 // should remove call by reference, but ora_fetch_into requires it in 4.0.3pl1
249 if ($this->fetchMode
& ADODB_FETCH_ASSOC
)
250 return @ora_fetch_into
($this->_queryID
,&$this->fields
,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC
);
252 return @ora_fetch_into
($this->_queryID
,&$this->fields
,ORA_FETCHINTO_NULLS
);
255 /* close() only needs to be called if you are worried about using too much memory while your script
256 is running. All associated result memory for the specified result identifier will automatically be freed. */
260 return @ora_close
($this->_queryID
);
263 function MetaType($t,$len=-1)
267 $t = $fieldobj->type
;
268 $len = $fieldobj->max_length
;
271 switch (strtoupper($t)) {
277 if ($len <= $this->blobSize
) return 'C';
283 case 'LONG VARBINARY':
287 case 'DATE': return 'D';
289 //case 'T': return 'T';
291 case 'BIT': return 'L';
294 case 'INTEGER': return 'I';