]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-oracle.inc.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / drivers / adodb-oracle.inc.php
1 <?php
2 /*
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.
7
8   Latest version is available at http://php.weblogs.com/
9
10   Oracle data driver. Requires Oracle client. Works on Windows and Unix and Oracle 7 and 8.
11
12   If you are using Oracle 8, use the oci8 driver which is much better and more reliable.
13
14 */
15
16 class ADODB_oracle extends ADOConnection
17 {
18     var $databaseType = "oracle";
19     var $replaceQuote = "''"; // string to use to replace quotes
20     var $concat_operator = '||';
21     var $_curs;
22     var $_initdate = true; // init date to YYYY-MM-DD
23     var $metaTablesSQL = 'select table_name from cat';
24     var $metaColumnsSQL = "select cname,coltype,width from col where tname='%s' order by colno";
25     var $sysDate = "TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD')";
26     var $sysTimeStamp = 'SYSDATE';
27     var $connectSID = true;
28
29     function ADODB_oracle()
30     {
31     }
32
33     // format and return date string in database date format
34     function DBDate($d)
35     {
36         if (is_string($d)) $d = ADORecordSet::UnixDate($d);
37         return 'TO_DATE(' . adodb_date($this->fmtDate, $d) . ",'YYYY-MM-DD')";
38     }
39
40     // format and return date string in database timestamp format
41     function DBTimeStamp($ts)
42     {
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')";
45     }
46
47     function BeginTrans()
48     {
49         $this->autoCommit = false;
50         ora_commitoff($this->_connectionID);
51         return true;
52     }
53
54     function CommitTrans($ok = true)
55     {
56         if (!$ok) return $this->RollbackTrans();
57         $ret = ora_commit($this->_connectionID);
58         ora_commiton($this->_connectionID);
59         return $ret;
60     }
61
62     function RollbackTrans()
63     {
64         $ret = ora_rollback($this->_connectionID);
65         ora_commiton($this->_connectionID);
66         return $ret;
67     }
68
69     /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
70     function ErrorMsg()
71     {
72         $this->_errorMsg = @ora_error($this->_curs);
73         if (!$this->_errorMsg) $this->_errorMsg = @ora_error($this->_connectionID);
74         return $this->_errorMsg;
75     }
76
77     function ErrorNo()
78     {
79         $err = @ora_errorcode($this->_curs);
80         if (!$err) return @ora_errorcode($this->_connectionID);
81     }
82
83     // returns true or false
84     function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode = 0)
85     {
86         // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set
87         // the oracle home to the host name of remote DB?
88 //                      if ($argHostname) putenv("ORACLE_HOME=$argHostname");
89
90         if ($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
91             if (empty($argDatabasename)) $argDatabasename = $argHostname;
92             else {
93                 if (strpos($argHostname, ":")) {
94                     $argHostinfo = explode(":", $argHostname);
95                     $argHostname = $argHostinfo[0];
96                     $argHostport = $argHostinfo[1];
97                 } else {
98                     $argHostport = "1521";
99                 }
100
101                 if ($this->connectSID) {
102                     $argDatabasename = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=" . $argHostname
103                         . ")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
104                 } else
105                     $argDatabasename = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=" . $argHostname
106                         . ")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
107             }
108
109         }
110
111         if ($argDatabasename) $argUsername .= "@$argDatabasename";
112
113         //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
114         if ($mode = 1)
115             $this->_connectionID = ora_plogon($argUsername, $argPassword);
116         else
117             $this->_connectionID = ora_logon($argUsername, $argPassword);
118         if ($this->_connectionID === false) return false;
119         if ($this->autoCommit) ora_commiton($this->_connectionID);
120         if ($this->_initdate) {
121             $rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
122             if ($rs) ora_close($rs);
123         }
124
125         return true;
126     }
127
128     // returns true or false
129     function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
130     {
131         return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
132     }
133
134     // returns query ID if successful, otherwise false
135     function _query($sql, $inputarr = false)
136     {
137         $curs = ora_open($this->_connectionID);
138
139         if ($curs === false) return false;
140         $this->_curs = $curs;
141         if (!ora_parse($curs, $sql)) return false;
142         if (ora_exec($curs)) return $curs;
143
144         @ora_close($curs);
145         return false;
146     }
147
148     // returns true or false
149     function _close()
150     {
151         return @ora_logoff($this->_connectionID);
152     }
153
154 }
155
156 /*--------------------------------------------------------------------------------------
157          Class Name: Recordset
158 --------------------------------------------------------------------------------------*/
159
160 class ADORecordset_oracle extends ADORecordSet
161 {
162
163     var $databaseType = "oracle";
164     var $bind = false;
165
166     function ADORecordset_oracle($queryID, $mode = false)
167     {
168
169         if ($mode === false) {
170             global $ADODB_FETCH_MODE;
171             $mode = $ADODB_FETCH_MODE;
172         }
173         $this->fetchMode = $mode;
174
175         $this->_queryID = $queryID;
176
177         $this->_inited = true;
178         $this->fields = array();
179         if ($queryID) {
180             $this->_currentRow = 0;
181             $this->EOF = !$this->_fetch();
182             @$this->_initrs();
183         } else {
184             $this->_numOfRows = 0;
185             $this->_numOfFields = 0;
186             $this->EOF = true;
187         }
188
189         return $this->_queryID;
190     }
191
192     /*          Returns: an object containing field information.
193             Get column information in the Recordset object. fetchField() can be used in order to obtain information about
194             fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
195             fetchField() is retrieved.          */
196
197     function FetchField($fieldOffset = -1)
198     {
199         $fld = new ADOFieldObject;
200         $fld->name = ora_columnname($this->_queryID, $fieldOffset);
201         $fld->type = ora_columntype($this->_queryID, $fieldOffset);
202         $fld->max_length = ora_columnsize($this->_queryID, $fieldOffset);
203         return $fld;
204     }
205
206     /* Use associative array to get fields array */
207     function Fields($colname)
208     {
209         if (!$this->bind) {
210             $this->bind = array();
211             for ($i = 0; $i < $this->_numOfFields; $i++) {
212                 $o = $this->FetchField($i);
213                 $this->bind[strtoupper($o->name)] = $i;
214             }
215         }
216
217         return $this->fields[$this->bind[strtoupper($colname)]];
218     }
219
220     function _initrs()
221     {
222         $this->_numOfRows = -1;
223         $this->_numOfFields = @ora_numcols($this->_queryID);
224     }
225
226     function _seek($row)
227     {
228         return false;
229     }
230
231     function _fetch($ignore_fields = false)
232     {
233         if ($this->fetchMode & ADODB_FETCH_ASSOC)
234             return @ora_fetch_into($this->_queryID, $this->fields, ORA_FETCHINTO_NULLS | ORA_FETCHINTO_ASSOC);
235         else
236             return @ora_fetch_into($this->_queryID, $this->fields, ORA_FETCHINTO_NULLS);
237     }
238
239     /*          close() only needs to be called if you are worried about using too much memory while your script
240             is running. All associated result memory for the specified result identifier will automatically be freed.           */
241
242     function _close()
243     {
244         return @ora_close($this->_queryID);
245     }
246
247     function MetaType($t, $len = -1)
248     {
249         if (is_object($t)) {
250             $fieldobj = $t;
251             $t = $fieldobj->type;
252             $len = $fieldobj->max_length;
253         }
254
255         switch (strtoupper($t)) {
256             case 'VARCHAR':
257             case 'VARCHAR2':
258             case 'CHAR':
259             case 'VARBINARY':
260             case 'BINARY':
261                 if ($len <= $this->blobSize) return 'C';
262             case 'LONG':
263             case 'LONG VARCHAR':
264             case 'CLOB':
265                 return 'X';
266             case 'LONG RAW':
267             case 'LONG VARBINARY':
268             case 'BLOB':
269                 return 'B';
270
271             case 'DATE':
272                 return 'D';
273
274             //case 'T': return 'T';
275
276             case 'BIT':
277                 return 'L';
278             case 'INT':
279             case 'SMALLINT':
280             case 'INTEGER':
281                 return 'I';
282             default:
283                 return 'N';
284         }
285     }
286 }