3 V4.22 15 Apr 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)
42 if (is_string($ts)) $d = ADORecordSet::UnixTimeStamp($ts);
43 return 'TO_DATE('.adodb_date($this->fmtTimeStamp,$ts).",'RRRR-MM-DD, HH:MI:SS AM')";
49 $this->autoCommit = false;
50 ora_commitoff($this->_connectionID);
55 function CommitTrans($ok=true)
57 if (!$ok) return $this->RollbackTrans();
58 $ret = ora_commit($this->_connectionID);
59 ora_commiton($this->_connectionID);
64 function RollbackTrans()
66 $ret = ora_rollback($this->_connectionID);
67 ora_commiton($this->_connectionID);
72 /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
75 $this->_errorMsg = @ora_error($this->_curs);
76 if (!$this->_errorMsg) $this->_errorMsg = @ora_error($this->_connectionID);
77 return $this->_errorMsg;
83 $err = @ora_errorcode($this->_curs);
84 if (!$err) return @ora_errorcode($this->_connectionID);
89 // returns true or false
90 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
92 // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set
93 // the oracle home to the host name of remote DB?
94 // if ($argHostname) putenv("ORACLE_HOME=$argHostname");
96 if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
97 if (empty($argDatabasename)) $argDatabasename = $argHostname;
99 if(strpos($argHostname,":")) {
100 $argHostinfo=explode(":",$argHostname);
101 $argHostname=$argHostinfo[0];
102 $argHostport=$argHostinfo[1];
108 if ($this->connectSID) {
109 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
110 .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
112 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
113 .")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
118 if ($argDatabasename) $argUsername .= "@$argDatabasename";
120 //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
122 $this->_connectionID = ora_plogon($argUsername,$argPassword);
124 $this->_connectionID = ora_logon($argUsername,$argPassword);
125 if ($this->_connectionID === false) return false;
126 if ($this->autoCommit) ora_commiton($this->_connectionID);
127 if ($this->_initdate) {
128 $rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
129 if ($rs) ora_close($rs);
136 // returns true or false
137 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
139 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
143 // returns query ID if successful, otherwise false
144 function _query($sql,$inputarr=false)
146 $curs = ora_open($this->_connectionID);
148 if ($curs === false) return false;
149 $this->_curs = $curs;
150 if (!ora_parse($curs,$sql)) return false;
151 if (ora_exec($curs)) return $curs;
158 // returns true or false
161 return @ora_logoff($this->_connectionID);
169 /*--------------------------------------------------------------------------------------
170 Class Name: Recordset
171 --------------------------------------------------------------------------------------*/
173 class ADORecordset_oracle extends ADORecordSet {
175 var $databaseType = "oracle";
178 function ADORecordset_oracle($queryID,$mode=false)
181 if ($mode === false) {
182 global $ADODB_FETCH_MODE;
183 $mode = $ADODB_FETCH_MODE;
185 $this->fetchMode = $mode;
187 $this->_queryID = $queryID;
189 $this->_inited = true;
190 $this->fields = array();
192 $this->_currentRow = 0;
193 $this->EOF = !$this->_fetch();
196 $this->_numOfRows = 0;
197 $this->_numOfFields = 0;
201 return $this->_queryID;
206 /* Returns: an object containing field information.
207 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
208 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
209 fetchField() is retrieved. */
211 function FetchField($fieldOffset = -1)
213 $fld = new ADOFieldObject;
214 $fld->name = ora_columnname($this->_queryID, $fieldOffset);
215 $fld->type = ora_columntype($this->_queryID, $fieldOffset);
216 $fld->max_length = ora_columnsize($this->_queryID, $fieldOffset);
220 /* Use associative array to get fields array */
221 function Fields($colname)
224 $this->bind = array();
225 for ($i=0; $i < $this->_numOfFields; $i++) {
226 $o = $this->FetchField($i);
227 $this->bind[strtoupper($o->name)] = $i;
231 return $this->fields[$this->bind[strtoupper($colname)]];
236 $this->_numOfRows = -1;
237 $this->_numOfFields = @ora_numcols($this->_queryID);
246 function _fetch($ignore_fields=false) {
247 // should remove call by reference, but ora_fetch_into requires it in 4.0.3pl1
248 if ($this->fetchMode & ADODB_FETCH_ASSOC)
249 return @ora_fetch_into($this->_queryID,&$this->fields,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC);
251 return @ora_fetch_into($this->_queryID,&$this->fields,ORA_FETCHINTO_NULLS);
254 /* close() only needs to be called if you are worried about using too much memory while your script
255 is running. All associated result memory for the specified result identifier will automatically be freed. */
259 return @ora_close($this->_queryID);
262 function MetaType($t,$len=-1)
266 $t = $fieldobj->type;
267 $len = $fieldobj->max_length;
270 switch (strtoupper($t)) {
276 if ($len <= $this->blobSize) return 'C';
282 case 'LONG VARBINARY':
286 case 'DATE': return 'D';
288 //case 'T': return 'T';
290 case 'BIT': return 'L';
293 case 'INTEGER': return 'I';