]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-oracle.inc.php
trailing_spaces
[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         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;
27
28         function ADODB_oracle()
29         {
30         }
31
32         // format and return date string in database date format
33         function DBDate($d)
34         {
35                 if (is_string($d)) $d = ADORecordSet::UnixDate($d);
36                 return 'TO_DATE('.adodb_date($this->fmtDate,$d).",'YYYY-MM-DD')";
37         }
38
39         // format and return date string in database timestamp format
40         function DBTimeStamp($ts)
41         {
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')";
44         }
45
46
47         function BeginTrans()
48         {
49                  $this->autoCommit = false;
50                  ora_commitoff($this->_connectionID);
51                  return true;
52         }
53
54
55         function CommitTrans($ok=true)
56         {
57                    if (!$ok) return $this->RollbackTrans();
58                    $ret = ora_commit($this->_connectionID);
59                    ora_commiton($this->_connectionID);
60                    return $ret;
61         }
62
63
64         function RollbackTrans()
65         {
66                 $ret = ora_rollback($this->_connectionID);
67                 ora_commiton($this->_connectionID);
68                 return $ret;
69         }
70
71
72         /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
73         function ErrorMsg()
74         {
75                 $this->_errorMsg = @ora_error($this->_curs);
76                 if (!$this->_errorMsg) $this->_errorMsg = @ora_error($this->_connectionID);
77                 return $this->_errorMsg;
78         }
79
80
81         function ErrorNo()
82         {
83                 $err = @ora_errorcode($this->_curs);
84                 if (!$err) return @ora_errorcode($this->_connectionID);
85         }
86
87
88
89                 // returns true or false
90                 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
91                 {
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");
95
96                         if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
97                                 if (empty($argDatabasename)) $argDatabasename = $argHostname;
98                                 else {
99                                         if(strpos($argHostname,":")) {
100                                                 $argHostinfo=explode(":",$argHostname);
101                                                 $argHostname=$argHostinfo[0];
102                                                 $argHostport=$argHostinfo[1];
103                                         } else {
104                                                 $argHostport="1521";
105                                         }
106
107
108                                         if ($this->connectSID) {
109                                                 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
110                                                 .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
111                                         } else
112                                                 $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
113                                                 .")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
114                                 }
115
116                         }
117
118                         if ($argDatabasename) $argUsername .= "@$argDatabasename";
119
120                 //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
121                         if ($mode = 1)
122                                 $this->_connectionID = ora_plogon($argUsername,$argPassword);
123                         else
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);
130                         }
131
132                         return true;
133                 }
134
135
136                 // returns true or false
137                 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
138                 {
139                         return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
140                 }
141
142
143                 // returns query ID if successful, otherwise false
144                 function _query($sql,$inputarr=false)
145                 {
146                         $curs = ora_open($this->_connectionID);
147
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;
152
153                         @ora_close($curs);
154                         return false;
155                 }
156
157
158                 // returns true or false
159                 function _close()
160                 {
161                         return @ora_logoff($this->_connectionID);
162                 }
163
164
165
166 }
167
168
169 /*--------------------------------------------------------------------------------------
170                  Class Name: Recordset
171 --------------------------------------------------------------------------------------*/
172
173 class ADORecordset_oracle extends ADORecordSet {
174
175         var $databaseType = "oracle";
176         var $bind = false;
177
178         function ADORecordset_oracle($queryID,$mode=false)
179         {
180
181                 if ($mode === false) {
182                         global $ADODB_FETCH_MODE;
183                         $mode = $ADODB_FETCH_MODE;
184                 }
185                 $this->fetchMode = $mode;
186
187                 $this->_queryID = $queryID;
188
189                 $this->_inited = true;
190                 $this->fields = array();
191                 if ($queryID) {
192                         $this->_currentRow = 0;
193                         $this->EOF = !$this->_fetch();
194                         @$this->_initrs();
195                 } else {
196                         $this->_numOfRows = 0;
197                         $this->_numOfFields = 0;
198                         $this->EOF = true;
199                 }
200
201                 return $this->_queryID;
202         }
203
204
205
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.           */
210
211            function FetchField($fieldOffset = -1)
212            {
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);
217                         return $fld;
218            }
219
220         /* Use associative array to get fields array */
221         function Fields($colname)
222         {
223                 if (!$this->bind) {
224                         $this->bind = array();
225                         for ($i=0; $i < $this->_numOfFields; $i++) {
226                                 $o = $this->FetchField($i);
227                                 $this->bind[strtoupper($o->name)] = $i;
228                         }
229                 }
230
231                  return $this->fields[$this->bind[strtoupper($colname)]];
232         }
233
234    function _initrs()
235    {
236                    $this->_numOfRows = -1;
237                    $this->_numOfFields = @ora_numcols($this->_queryID);
238    }
239
240
241    function _seek($row)
242    {
243                    return false;
244    }
245
246    function _fetch($ignore_fields=false) {
247                 if ($this->fetchMode & ADODB_FETCH_ASSOC)
248                         return @ora_fetch_into($this->_queryID,$this->fields,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC);
249                 else
250                         return @ora_fetch_into($this->_queryID,$this->fields,ORA_FETCHINTO_NULLS);
251    }
252
253    /*           close() only needs to be called if you are worried about using too much memory while your script
254                    is running. All associated result memory for the specified result identifier will automatically be freed.            */
255
256    function _close()
257 {
258                    return @ora_close($this->_queryID);
259    }
260
261         function MetaType($t,$len=-1)
262         {
263                 if (is_object($t)) {
264                         $fieldobj = $t;
265                         $t = $fieldobj->type;
266                         $len = $fieldobj->max_length;
267                 }
268
269                 switch (strtoupper($t)) {
270                 case 'VARCHAR':
271                 case 'VARCHAR2':
272                 case 'CHAR':
273                 case 'VARBINARY':
274                 case 'BINARY':
275                                 if ($len <= $this->blobSize) return 'C';
276                 case 'LONG':
277                 case 'LONG VARCHAR':
278                 case 'CLOB':
279                 return 'X';
280                 case 'LONG RAW':
281                 case 'LONG VARBINARY':
282                 case 'BLOB':
283                                 return 'B';
284
285                 case 'DATE': return 'D';
286
287                 //case 'T': return 'T';
288
289                 case 'BIT': return 'L';
290                 case 'INT':
291                 case 'SMALLINT':
292                 case 'INTEGER': return 'I';
293                 default: return 'N';
294                 }
295         }
296 }
297 ?>