5 * @deprecated Use oci8 driver instead
7 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
10 * @link https://adodb.org Project's web site and documentation
11 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
13 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
14 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
15 * any later version. This means you can use it in proprietary products.
16 * See the LICENSE.md file distributed with this source code for details.
17 * @license BSD-3-Clause
18 * @license LGPL-2.1-or-later
20 * @copyright 2000-2013 John Lim
21 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
24 // security - hide paths
25 if (!defined('ADODB_DIR')) die();
27 class ADODB_oracle
extends ADOConnection
{
28 var $databaseType = "oracle";
29 var $replaceQuote = "''"; // string to use to replace quotes
30 var $concat_operator='||';
32 var $_initdate = true; // init date to YYYY-MM-DD
33 var $metaTablesSQL = 'select table_name from cat';
34 var $metaColumnsSQL = "select cname,coltype,width from col where tname='%s' order by colno";
35 var $sysDate = "TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD')";
36 var $sysTimeStamp = 'SYSDATE';
37 var $connectSID = true;
39 // format and return date string in database date format
40 function DBDate($d, $isfld = false)
42 if (is_string($d)) $d = ADORecordSet
::UnixDate($d);
43 if (is_object($d)) $ds = $d->format($this->fmtDate
);
44 else $ds = adodb_date($this->fmtDate
,$d);
45 return 'TO_DATE('.$ds.",'YYYY-MM-DD')";
48 // format and return date string in database timestamp format
49 function DBTimeStamp($ts, $isfld = false)
52 if (is_string($ts)) $ts = ADORecordSet
::UnixTimeStamp($ts);
53 if (is_object($ts)) $ds = $ts->format($this->fmtDate
);
54 else $ds = adodb_date($this->fmtTimeStamp
,$ts);
55 return 'TO_DATE('.$ds.",'RRRR-MM-DD, HH:MI:SS AM')";
61 $d = ADOConnection
::DBDate($d);
62 if (strncmp($d,"'",1)) return $d;
64 return substr($d,1,strlen($d)-2);
67 function BindTimeStamp($d)
69 $d = ADOConnection
::DBTimeStamp($d);
70 if (strncmp($d,"'",1)) return $d;
72 return substr($d,1,strlen($d)-2);
79 $this->autoCommit
= false;
80 ora_commitoff($this->_connectionID
);
85 function CommitTrans($ok=true)
87 if (!$ok) return $this->RollbackTrans();
88 $ret = ora_commit($this->_connectionID
);
89 ora_commiton($this->_connectionID
);
94 function RollbackTrans()
96 $ret = ora_rollback($this->_connectionID
);
97 ora_commiton($this->_connectionID
);
102 /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
105 if ($this->_errorMsg
!== false) return $this->_errorMsg
;
107 if (is_resource($this->_curs
)) $this->_errorMsg
= @ora_error
($this->_curs
);
108 if (empty($this->_errorMsg
)) $this->_errorMsg
= @ora_error
($this->_connectionID
);
109 return $this->_errorMsg
;
115 if ($this->_errorCode
!== false) return $this->_errorCode
;
117 if (is_resource($this->_curs
)) $this->_errorCode
= @ora_errorcode
($this->_curs
);
118 if (empty($this->_errorCode
)) $this->_errorCode
= @ora_errorcode
($this->_connectionID
);
119 return $this->_errorCode
;
124 // returns true or false
125 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
127 if (!function_exists('ora_plogon')) return null;
129 // <G. Giunta 2003/03/03/> Reset error messages before connecting
130 $this->_errorMsg
= false;
131 $this->_errorCode
= false;
133 // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set
134 // the oracle home to the host name of remote DB?
135 // if ($argHostname) putenv("ORACLE_HOME=$argHostname");
137 if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
138 if (empty($argDatabasename)) $argDatabasename = $argHostname;
140 if(strpos($argHostname,":")) {
141 $argHostinfo=explode(":",$argHostname);
142 $argHostname=$argHostinfo[0];
143 $argHostport=$argHostinfo[1];
149 if ($this->connectSID
) {
150 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
151 .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
153 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
154 .")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
159 if ($argDatabasename) $argUsername .= "@$argDatabasename";
161 //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
163 $this->_connectionID
= ora_plogon($argUsername,$argPassword);
165 $this->_connectionID
= ora_logon($argUsername,$argPassword);
166 if ($this->_connectionID
=== false) return false;
167 if ($this->autoCommit
) ora_commiton($this->_connectionID
);
168 if ($this->_initdate
) {
169 $rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
170 if ($rs) ora_close($rs);
177 // returns true or false
178 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
180 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
184 // returns query ID if successful, otherwise false
185 function _query($sql,$inputarr=false)
187 // <G. Giunta 2003/03/03/> Reset error messages before executing
188 $this->_errorMsg
= false;
189 $this->_errorCode
= false;
191 $curs = ora_open($this->_connectionID
);
193 if ($curs === false) return false;
194 $this->_curs
= $curs;
195 if (!ora_parse($curs,$sql)) return false;
196 if (ora_exec($curs)) return $curs;
197 // <G. Giunta 2004/03/03> before we close the cursor, we have to store the error message
198 // that we can obtain ONLY from the cursor (and not from the connection)
199 $this->_errorCode
= @ora_errorcode
($curs);
200 $this->_errorMsg
= @ora_error
($curs);
201 // </G. Giunta 2004/03/03>
207 // returns true or false
210 return @ora_logoff
($this->_connectionID
);
218 /*--------------------------------------------------------------------------------------
219 Class Name: Recordset
220 --------------------------------------------------------------------------------------*/
222 class ADORecordset_oracle
extends ADORecordSet
{
224 var $databaseType = "oracle";
227 function __construct($queryID,$mode=false)
230 if ($mode === false) {
231 global $ADODB_FETCH_MODE;
232 $mode = $ADODB_FETCH_MODE;
234 $this->fetchMode
= $mode;
236 $this->_queryID
= $queryID;
238 $this->_inited
= true;
239 $this->fields
= array();
241 $this->_currentRow
= 0;
242 $this->EOF
= !$this->_fetch();
245 $this->_numOfRows
= 0;
246 $this->_numOfFields
= 0;
250 return $this->_queryID
;
255 /* Returns: an object containing field information.
256 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
257 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
258 fetchField() is retrieved. */
260 function FetchField($fieldOffset = -1)
262 $fld = new ADOFieldObject
;
263 $fld->name
= ora_columnname($this->_queryID
, $fieldOffset);
264 $fld->type
= ora_columntype($this->_queryID
, $fieldOffset);
265 $fld->max_length
= ora_columnsize($this->_queryID
, $fieldOffset);
269 /* Use associative array to get fields array */
270 function Fields($colname)
273 $this->bind
= array();
274 for ($i=0; $i < $this->_numOfFields
; $i++
) {
275 $o = $this->FetchField($i);
276 $this->bind
[strtoupper($o->name
)] = $i;
280 return $this->fields
[$this->bind
[strtoupper($colname)]];
285 $this->_numOfRows
= -1;
286 $this->_numOfFields
= @ora_numcols
($this->_queryID
);
295 function _fetch($ignore_fields=false) {
296 // should remove call by reference, but ora_fetch_into requires it in 4.0.3pl1
297 if ($this->fetchMode
& ADODB_FETCH_ASSOC
)
298 return @ora_fetch_into
($this->_queryID
,$this->fields
,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC
);
300 return @ora_fetch_into
($this->_queryID
,$this->fields
,ORA_FETCHINTO_NULLS
);
303 /* close() only needs to be called if you are worried about using too much memory while your script
304 is running. All associated result memory for the specified result identifier will automatically be freed. */
308 return @ora_close
($this->_queryID
);
311 function MetaType($t, $len = -1, $fieldobj = false)
315 $t = $fieldobj->type
;
316 $len = $fieldobj->max_length
;
319 switch (strtoupper($t)) {
325 if ($len <= $this->blobSize
) return 'C';
331 case 'LONG VARBINARY':
335 case 'DATE': return 'D';
337 //case 'T': return 'T';
339 case 'BIT': return 'L';
342 case 'INTEGER': return 'I';