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