5329770b7b3ac8371de271f0c67eeaa0890b986b
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / drivers / adodb-oracle.inc.php
1 <?php
2 /*
3 V4.93 10 Oct 2006 (c) 2000-2006 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.
7
8 Latest version is available at http://adodb.sourceforge.net
9
10 Oracle data driver. Requires Oracle client. Works on Windows and Unix and Oracle 7.
11
12 If you are using Oracle 8 or later, use the oci8 driver which is much better and more reliable.
13 */
14
15 // security - hide paths
16 if (!defined('ADODB_DIR')) die();
17
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;
29
30 function ADODB_oracle()
31 {
32 }
33
34 // format and return date string in database date format
35 function DBDate($d)
36 {
37 if (is_string($d)) $d = ADORecordSet::UnixDate($d);
38 return 'TO_DATE('.adodb_date($this->fmtDate,$d).",'YYYY-MM-DD')";
39 }
40
41 // format and return date string in database timestamp format
42 function DBTimeStamp($ts)
43 {
44
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')";
47 }
48
49
50 function BindDate($d)
51 {
52 $d = ADOConnection::DBDate($d);
53 if (strncmp($d,"'",1)) return $d;
54
55 return substr($d,1,strlen($d)-2);
56 }
57
58 function BindTimeStamp($d)
59 {
60 $d = ADOConnection::DBTimeStamp($d);
61 if (strncmp($d,"'",1)) return $d;
62
63 return substr($d,1,strlen($d)-2);
64 }
65
66
67
68 function BeginTrans()
69 {
70 $this->autoCommit = false;
71 ora_commitoff($this->_connectionID);
72 return true;
73 }
74
75
76 function CommitTrans($ok=true)
77 {
78 if (!$ok) return $this->RollbackTrans();
79 $ret = ora_commit($this->_connectionID);
80 ora_commiton($this->_connectionID);
81 return $ret;
82 }
83
84
85 function RollbackTrans()
86 {
87 $ret = ora_rollback($this->_connectionID);
88 ora_commiton($this->_connectionID);
89 return $ret;
90 }
91
92
93 /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
94 function ErrorMsg()
95 {
96 if ($this->_errorMsg !== false) return $this->_errorMsg;
97
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;
101 }
102
103
104 function ErrorNo()
105 {
106 if ($this->_errorCode !== false) return $this->_errorCode;
107
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;
111 }
112
113
114
115 // returns true or false
116 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
117 {
118 if (!function_exists('ora_plogon')) return null;
119
120 // <G. Giunta 2003/03/03/> Reset error messages before connecting
121 $this->_errorMsg = false;
122 $this->_errorCode = false;
123
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");
127
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";
137 }
138
139
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)))";
146 }
147
148 }
149
150 if ($argDatabasename) $argUsername .= "@$argDatabasename";
151
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);
162 }
163
164 return true;
165 }
166
167
168 // returns true or false
169 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
170 {
171 return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
172 }
173
174
175 // returns query ID if successful, otherwise false
176 function _query($sql,$inputarr=false)
177 {
178 // <G. Giunta 2003/03/03/> Reset error messages before executing
179 $this->_errorMsg = false;
180 $this->_errorCode = false;
181
182 $curs = ora_open($this->_connectionID);
183
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;
195 }
196
197
198 // returns true or false
199 function _close()
200 {
201 return @ora_logoff($this->_connectionID);
202 }
203
204
205
206 }
207
208
209 /*--------------------------------------------------------------------------------------
210 Class Name: Recordset
211 --------------------------------------------------------------------------------------*/
212
213 class ADORecordset_oracle extends ADORecordSet {
214
215 var $databaseType = "oracle";
216 var $bind = false;
217
218 function ADORecordset_oracle($queryID,$mode=false)
219 {
220
221 if ($mode === false) {
222 global $ADODB_FETCH_MODE;
223 $mode = $ADODB_FETCH_MODE;
224 }
225 $this->fetchMode = $mode;
226
227 $this->_queryID = $queryID;
228
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;
239 }
240
241 return $this->_queryID;
242 }
243
244
245
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. */
250
251 function &FetchField($fieldOffset = -1)
252 {
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;
258 }
259
260 /* Use associative array to get fields array */
261 function Fields($colname)
262 {
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;
268 }
269 }
270
271 return $this->fields[$this->bind[strtoupper($colname)]];
272 }
273
274 function _initrs()
275 {
276 $this->_numOfRows = -1;
277 $this->_numOfFields = @ora_numcols($this->_queryID);
278 }
279
280
281 function _seek($row)
282 {
283 return false;
284 }
285
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);
292 }
293
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. */
296
297 function _close()
298 {
299 return @ora_close($this->_queryID);
300 }
301
302 function MetaType($t,$len=-1)
303 {
304 if (is_object($t)) {
305 $fieldobj = $t;
306 $t = $fieldobj->type;
307 $len = $fieldobj->max_length;
308 }
309
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';
325
326 case 'DATE': return 'D';
327
328 //case 'T': return 'T';
329
330 case 'BIT': return 'L';
331 case 'INT':
332 case 'SMALLINT':
333 case 'INTEGER': return 'I';
334 default: return 'N';
335 }
336 }
337 }
338 ?>