Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
/**
3
 * Oracle data driver
4
 *
5
 * @deprecated Use oci8 driver instead
6
 *
7
 * This file is part of ADOdb, a Database Abstraction Layer library for PHP.
8
 *
9
 * @package ADOdb
10
 * @link https://adodb.org Project's web site and documentation
11
 * @link https://github.com/ADOdb/ADOdb Source code and issue tracker
12
 *
13
 * The ADOdb Library is dual-licensed, released under both the BSD 3-Clause
14
 * and the GNU Lesser General Public Licence (LGPL) v2.1 or, at your option,
15
 * any later version. This means you can use it in proprietary products.
16
 * See the LICENSE.md file distributed with this source code for details.
17
 * @license BSD-3-Clause
18
 * @license LGPL-2.1-or-later
19
 *
20
 * @copyright 2000-2013 John Lim
21
 * @copyright 2014 Damien Regad, Mark Newnham and the ADOdb community
22
 */
23
 
24
// security - hide paths
25
if (!defined('ADODB_DIR')) die();
26
 
27
class ADODB_oracle extends ADOConnection {
28
	var $databaseType = "oracle";
29
	var $replaceQuote = "''"; // string to use to replace quotes
30
	var $concat_operator='||';
31
	var $_curs;
32
	var $_initdate = true; // init date to YYYY-MM-DD
33
	var $metaTablesSQL = 'select table_name from cat';
34
	var $metaColumnsSQL = "select cname,coltype,width from col where tname='%s' order by colno";
35
	var $sysDate = "TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD')";
36
	var $sysTimeStamp = 'SYSDATE';
37
	var $connectSID = true;
38
 
39
	// format and return date string in database date format
40
	function DBDate($d, $isfld = false)
41
	{
42
		if (is_string($d)) $d = ADORecordSet::UnixDate($d);
43
		if (is_object($d)) $ds = $d->format($this->fmtDate);
44
		else $ds = adodb_date($this->fmtDate,$d);
45
		return 'TO_DATE('.$ds.",'YYYY-MM-DD')";
46
	}
47
 
48
	// format and return date string in database timestamp format
49
	function DBTimeStamp($ts, $isfld = false)
50
	{
51
 
52
		if (is_string($ts)) $ts = ADORecordSet::UnixTimeStamp($ts);
53
		if (is_object($ts)) $ds = $ts->format($this->fmtDate);
54
		else $ds = adodb_date($this->fmtTimeStamp,$ts);
55
		return 'TO_DATE('.$ds.",'RRRR-MM-DD, HH:MI:SS AM')";
56
	}
57
 
58
 
59
	function BindDate($d)
60
	{
61
		$d = ADOConnection::DBDate($d);
62
		if (strncmp($d,"'",1)) return $d;
63
 
64
		return substr($d,1,strlen($d)-2);
65
	}
66
 
67
	function BindTimeStamp($d)
68
	{
69
		$d = ADOConnection::DBTimeStamp($d);
70
		if (strncmp($d,"'",1)) return $d;
71
 
72
		return substr($d,1,strlen($d)-2);
73
	}
74
 
75
 
76
 
77
	function BeginTrans()
78
	{
79
		 $this->autoCommit = false;
80
		 ora_commitoff($this->_connectionID);
81
		 return true;
82
	}
83
 
84
 
85
	function CommitTrans($ok=true)
86
	{
87
		   if (!$ok) return $this->RollbackTrans();
88
		   $ret = ora_commit($this->_connectionID);
89
		   ora_commiton($this->_connectionID);
90
		   return $ret;
91
	}
92
 
93
 
94
	function RollbackTrans()
95
	{
96
		$ret = ora_rollback($this->_connectionID);
97
		ora_commiton($this->_connectionID);
98
		return $ret;
99
	}
100
 
101
 
102
	/* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
103
	function ErrorMsg()
104
 	{
105
        if ($this->_errorMsg !== false) return $this->_errorMsg;
106
 
107
        if (is_resource($this->_curs)) $this->_errorMsg = @ora_error($this->_curs);
108
 		if (empty($this->_errorMsg)) $this->_errorMsg = @ora_error($this->_connectionID);
109
		return $this->_errorMsg;
110
	}
111
 
112
 
113
	function ErrorNo()
114
	{
115
		if ($this->_errorCode !== false) return $this->_errorCode;
116
 
117
		if (is_resource($this->_curs)) $this->_errorCode = @ora_errorcode($this->_curs);
118
		if (empty($this->_errorCode)) $this->_errorCode = @ora_errorcode($this->_connectionID);
119
        return $this->_errorCode;
120
	}
121
 
122
 
123
 
124
		// returns true or false
125
		function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
126
		{
127
			if (!function_exists('ora_plogon')) return null;
128
 
129
            // <G. Giunta 2003/03/03/> Reset error messages before connecting
130
            $this->_errorMsg = false;
131
		    $this->_errorCode = false;
132
 
133
            // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set
134
            // the oracle home to the host name of remote DB?
135
//			if ($argHostname) putenv("ORACLE_HOME=$argHostname");
136
 
137
			if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
138
				if (empty($argDatabasename)) $argDatabasename = $argHostname;
139
				else {
140
					if(strpos($argHostname,":")) {
141
						$argHostinfo=explode(":",$argHostname);
142
						$argHostname=$argHostinfo[0];
143
						$argHostport=$argHostinfo[1];
144
					} else {
145
						$argHostport="1521";
146
					}
147
 
148
 
149
					if ($this->connectSID) {
150
						$argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
151
						.")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
152
					} else
153
						$argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
154
						.")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
155
				}
156
 
157
			}
158
 
159
			if ($argDatabasename) $argUsername .= "@$argDatabasename";
160
 
161
		//if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
162
			if ($mode == 1)
163
				$this->_connectionID = ora_plogon($argUsername,$argPassword);
164
			else
165
				$this->_connectionID = ora_logon($argUsername,$argPassword);
166
			if ($this->_connectionID === false) return false;
167
			if ($this->autoCommit) ora_commiton($this->_connectionID);
168
			if ($this->_initdate) {
169
				$rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
170
				if ($rs) ora_close($rs);
171
			}
172
 
173
			return true;
174
		}
175
 
176
 
177
		// returns true or false
178
		function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
179
		{
180
			return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
181
		}
182
 
183
 
184
		function _query($sql,$inputarr=false)
185
		{
186
            // <G. Giunta 2003/03/03/> Reset error messages before executing
187
            $this->_errorMsg = false;
188
		    $this->_errorCode = false;
189
 
190
			$curs = ora_open($this->_connectionID);
191
 
192
		 	if ($curs === false) return false;
193
			$this->_curs = $curs;
194
			if (!ora_parse($curs,$sql)) return false;
195
			if (ora_exec($curs)) return $curs;
196
            // <G. Giunta 2004/03/03> before we close the cursor, we have to store the error message
197
            // that we can obtain ONLY from the cursor (and not from the connection)
198
            $this->_errorCode = @ora_errorcode($curs);
199
            $this->_errorMsg = @ora_error($curs);
200
            // </G. Giunta 2004/03/03>
201
		 	@ora_close($curs);
202
			return false;
203
		}
204
 
205
 
206
		// returns true or false
207
		function _close()
208
		{
209
			return @ora_logoff($this->_connectionID);
210
		}
211
 
212
 
213
 
214
}
215
 
216
 
217
/*--------------------------------------------------------------------------------------
218
		 Class Name: Recordset
219
--------------------------------------------------------------------------------------*/
220
 
221
class ADORecordset_oracle extends ADORecordSet {
222
 
223
	var $databaseType = "oracle";
224
	var $bind = false;
225
 
226
	function __construct($queryID,$mode=false)
227
	{
228
 
229
		if ($mode === false) {
230
			global $ADODB_FETCH_MODE;
231
			$mode = $ADODB_FETCH_MODE;
232
		}
233
		$this->fetchMode = $mode;
234
 
235
		$this->_queryID = $queryID;
236
 
237
		$this->_inited = true;
238
		$this->fields = array();
239
		if ($queryID) {
240
			$this->_currentRow = 0;
241
			$this->EOF = !$this->_fetch();
242
			@$this->_initrs();
243
		} else {
244
			$this->_numOfRows = 0;
245
			$this->_numOfFields = 0;
246
			$this->EOF = true;
247
		}
248
 
249
		return $this->_queryID;
250
	}
251
 
252
 
253
 
254
	   /*		Returns: an object containing field information.
255
			   Get column information in the Recordset object. fetchField() can be used in order to obtain information about
256
			   fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
257
			   fetchField() is retrieved.		*/
258
 
259
	   function FetchField($fieldOffset = -1)
260
	   {
261
			$fld = new ADOFieldObject;
262
			$fld->name = ora_columnname($this->_queryID, $fieldOffset);
263
			$fld->type = ora_columntype($this->_queryID, $fieldOffset);
264
			$fld->max_length = ora_columnsize($this->_queryID, $fieldOffset);
265
			return $fld;
266
	   }
267
 
268
	/* Use associative array to get fields array */
269
	function Fields($colname)
270
	{
271
		if (!$this->bind) {
272
			$this->bind = array();
273
			for ($i=0; $i < $this->_numOfFields; $i++) {
274
				$o = $this->FetchField($i);
275
				$this->bind[strtoupper($o->name)] = $i;
276
			}
277
		}
278
 
279
		 return $this->fields[$this->bind[strtoupper($colname)]];
280
	}
281
 
282
   function _initrs()
283
   {
284
		   $this->_numOfRows = -1;
285
		   $this->_numOfFields = @ora_numcols($this->_queryID);
286
   }
287
 
288
 
289
   function _seek($row)
290
   {
291
		   return false;
292
   }
293
 
294
   function _fetch($ignore_fields=false) {
295
// should remove call by reference, but ora_fetch_into requires it in 4.0.3pl1
296
		if ($this->fetchMode & ADODB_FETCH_ASSOC)
297
			return @ora_fetch_into($this->_queryID,$this->fields,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC);
298
   		else
299
			return @ora_fetch_into($this->_queryID,$this->fields,ORA_FETCHINTO_NULLS);
300
   }
301
 
302
   /*		close() only needs to be called if you are worried about using too much memory while your script
303
		   is running. All associated result memory for the specified result identifier will automatically be freed.		*/
304
 
305
   function _close()
306
{
307
		   return @ora_close($this->_queryID);
308
   }
309
 
310
	function MetaType($t, $len = -1, $fieldobj = false)
311
	{
312
		if (is_object($t)) {
313
			$fieldobj = $t;
314
			$t = $fieldobj->type;
315
			$len = $fieldobj->max_length;
316
		}
317
 
318
		switch (strtoupper($t)) {
319
		case 'VARCHAR':
320
		case 'VARCHAR2':
321
		case 'CHAR':
322
		case 'VARBINARY':
323
		case 'BINARY':
324
				if ($len <= $this->blobSize) return 'C';
325
		case 'LONG':
326
		case 'LONG VARCHAR':
327
		case 'CLOB':
328
		return 'X';
329
		case 'LONG RAW':
330
		case 'LONG VARBINARY':
331
		case 'BLOB':
332
				return 'B';
333
 
334
		case 'DATE': return 'D';
335
 
336
		//case 'T': return 'T';
337
 
338
		case 'BIT': return 'L';
339
		case 'INT':
340
		case 'SMALLINT':
341
		case 'INTEGER': return 'I';
342
		default: return 'N';
343
		}
344
	}
345
}