Phyaura Calendar speed inhancement
[openemr.git] / library / adodb / drivers / adodb-oracle.inc.php
blobbcc49118d7e3bc7114b53947c1e5a1b36dad592e
1 <?php
2 /*
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='||';
20 var $_curs;
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
33 function DBDate($d)
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')";
48 function BeginTrans()
50 $this->autoCommit = false;
51 ora_commitoff($this->_connectionID);
52 return true;
56 function CommitTrans($ok=true)
58 if (!$ok) return $this->RollbackTrans();
59 $ret = ora_commit($this->_connectionID);
60 ora_commiton($this->_connectionID);
61 return $ret;
65 function RollbackTrans()
67 $ret = ora_rollback($this->_connectionID);
68 ora_commiton($this->_connectionID);
69 return $ret;
73 /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
74 function ErrorMsg()
76 $this->_errorMsg = @ora_error($this->_curs);
77 if (!$this->_errorMsg) $this->_errorMsg = @ora_error($this->_connectionID);
78 return $this->_errorMsg;
82 function ErrorNo()
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;
99 else {
100 if(strpos($argHostname,":")) {
101 $argHostinfo=explode(":",$argHostname);
102 $argHostname=$argHostinfo[0];
103 $argHostport=$argHostinfo[1];
104 } else {
105 $argHostport="1521";
109 if ($this->connectSID) {
110 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
111 .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
112 } else
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>";
122 if ($mode = 1)
123 $this->_connectionID = ora_plogon($argUsername,$argPassword);
124 else
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);
133 return true;
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;
154 @ora_close($curs);
155 return false;
159 // returns true or false
160 function _close()
162 return @ora_logoff($this->_connectionID);
170 /*--------------------------------------------------------------------------------------
171 Class Name: Recordset
172 --------------------------------------------------------------------------------------*/
174 class ADORecordset_oracle extends ADORecordSet {
176 var $databaseType = "oracle";
177 var $bind = false;
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();
192 if ($queryID) {
193 $this->_currentRow = 0;
194 $this->EOF = !$this->_fetch();
195 @$this->_initrs();
196 } else {
197 $this->_numOfRows = 0;
198 $this->_numOfFields = 0;
199 $this->EOF = true;
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);
218 return $fld;
221 /* Use associative array to get fields array */
222 function Fields($colname)
224 if (!$this->bind) {
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)]];
235 function _initrs()
237 $this->_numOfRows = -1;
238 $this->_numOfFields = @ora_numcols($this->_queryID);
242 function _seek($row)
244 return false;
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);
251 else
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. */
258 function _close()
260 return @ora_close($this->_queryID);
263 function MetaType($t,$len=-1)
265 if (is_object($t)) {
266 $fieldobj = $t;
267 $t = $fieldobj->type;
268 $len = $fieldobj->max_length;
271 switch (strtoupper($t)) {
272 case 'VARCHAR':
273 case 'VARCHAR2':
274 case 'CHAR':
275 case 'VARBINARY':
276 case 'BINARY':
277 if ($len <= $this->blobSize) return 'C';
278 case 'LONG':
279 case 'LONG VARCHAR':
280 case 'CLOB':
281 return 'X';
282 case 'LONG RAW':
283 case 'LONG VARBINARY':
284 case 'BLOB':
285 return 'B';
287 case 'DATE': return 'D';
289 //case 'T': return 'T';
291 case 'BIT': return 'L';
292 case 'INT':
293 case 'SMALLINT':
294 case 'INTEGER': return 'I';
295 default: return 'N';