This repository has been archived by the owner on Jul 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ezdb_oracle.class.php
executable file
·325 lines (282 loc) · 8.58 KB
/
ezdb_oracle.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* Copyright (c) 2010 Nabeel Shahzad
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Nabeel Shahzad
* @copyright Copyright (c) 2008-2010, Nabeel Shahzad
* @link http://github.com/nshahzad/ezdb
* @license MIT License
*
*
* Based on ezSQL by Justin Vincent: http://justinvincent.com/docs/ezsql/ez_sql_help.htm
*
*/
/**********************************************************************
* Author: Justin Vincent ([email protected])
* Web...: http://php.justinvincent.com
* Name..: ezDB_oracle8_9
* Desc..: Oracle 8i/9i component (part of ezDB databse abstraction library)
*
*/
include_once dirname(__FILE__).'/ezdb_base.class.php';
class ezDB_oracle extends ezDB_Base
{
/**
* Connects to database immediately, unless $dbname is blank
*
* @param string $dbuser Database username
* @param string $dbpassword Database password
* @param string $dbname Database name (if blank, will not connect)
* @param string $dbhost Hostname, optional, default is 'localhost'
* @return bool Connect status
*
*/
public function __construct($dbuser='', $dbpassword='', $dbname='', $dbhost='localhost')
{
if($dbname == '') return false;
if($this->connect($dbuser, $dbpassword, $dbhost))
{
return $this->select($dbname);
}
return false;
}
/**
* Explicitly close the connection on destruct
*/
public function __destruct()
{
$this->close();
}
/**
* Connects to database immediately, unless $dbname is blank
*
* In the case of Oracle quick_connect is not really needed
* because std. connect already does what quick connect does -
* but for the sake of consistency it has been included
*
* @param string $dbuser Database username
* @param string $dbpassword Database password
* @param string $dbname Database name (if blank, will not connect)
* @param string $dbhost Hostname, optional, default is 'localhost'
* @return bool Connect status
*
*/
public function quick_connect($dbuser='', $dbpassword='', $dbname='')
{
return $this->connect($dbuser='', $dbpassword='', $dbname='');
}
/**
* Connect to MySQL, but not to a database
*
* @param string $dbuser Username
* @param string $dbpassword Password
* @return bool Success
*
*/
public function connect($dbuser='', $dbpassword='', $dbname='')
{
if (!$this->dbh = oci_new_connect($dbuser, $dbpassword, $dbname))
{
$err = ocierror();
if($this->throw_exceptions)
throw new ezDB_Error($err['message'], $err['code']);
$this->register_error($err['message'], $err['code']);
return false;
}
else
{
$this->clear_errors();
return true;
}
}
/**
* No real equivalent of mySQL select in Oracle
* once again, function included for the sake of consistency
*/
public function select($dbuser='', $dbpassword='', $dbname='')
{
return $this->connect($dbuser='', $dbpassword='', $dbname='');
}
/**
* Format an Oracle string correctly for safe Oracle insert
* (no matter if magic quotes are on or not)
*
* @param string $str String to escape
* @return string Returns the escaped string
*
*/
public function escape($str)
{
return str_replace("'","''",str_replace("''","'",stripslashes($str)));
}
/**
* Returns the DB specific timestamp function (Oracle: SYSDATE, MySQL: NOW())
*
* @return string Timestamp function
*
*/
public function sysdate()
{
return "SYSDATE";
}
/**
* These special Oracle functions make sure that even if your test
* pattern is '' it will still match records that are null if
* you don't use these funcs then oracle will return no results
* if $user = ''; even if there were records that = ''
*
* SELECT * FROM USERS WHERE USER = ".$db->is_equal_str($user)."
*/
public function is_equal_str($str='')
{
return ($str==''?'IS NULL':"= '".$this->escape($str)."'");
}
public function is_equal_int($int)
{
return ($int==''?'IS NULL':'= '.$int);
}
/**
* Another oracle specific function - if you have set up a sequence
* this function returns the next ID from that sequence
*/
public function insert_id($seq_name)
{
$return_val = $this->get_var("SELECT $seq_name.nextVal id FROM Dual");
// If no return value then try to create the sequence
if ( ! $return_val )
{
$this->query("CREATE SEQUENCE $seq_name maxValue 9999999999 INCREMENT BY 1 START WITH 1 CACHE 20 CYCLE");
$return_val = $this->get_var("SELECT $seq_name.nextVal id FROM Dual");
$this->register_error("Oracle CREATE SEQUENCE error: $seq_name");
}
return $return_val;
}
/**
* Run the SQL query, and get the result. Returns false on failure
* Check $this->error() and $this->errno() functions for any errors
* MySQL returns errno() == 0 for no error. That's the most reliable check
*
* @param string $query SQL Query
* @return mixed Return values
*
*/
public function query($query)
{
$return_value = 0;
// Flush cached values..
$this->flush();
// Log how the function was called
$this->func_call = "\$db->query(\"$query\")";
// Keep track of the last query for debug..
$this->last_query = $query;
$this->num_queries++;
// Use core file cache function
if ( $cache = $this->get_cache($query) )
{
return $cache;
}
// If there is no existing database connection then try to connect
if ( ! $this->dbh )
{
if($this->throw_exceptions)
throw new ezDB_Error('No active connection', -1);
$this->register_error('There is no active database connection!');
return false;
}
// Parses the query and returns a statement..
if(($stmt = oci_parse($this->dbh, $query)) === false)
{
$error = oci_error($this->dbh);
if($error['code'] == 0)
{
$this->clear_errors();
return true;
}
else
{
if($this->throw_exceptions)
throw new ezDB_Error($error['message'], $error['code']);
$this->register_error($error['message'], $error['code']);
return false;
}
}
// Execute the query..
elseif (($this->result = oci_execute($stmt)) === false)
{
$error = oci_error($stmt);
if($error['code'] == 0)
{
$this->clear_errors();
return true;
}
if($this->throw_exceptions)
throw new ezDB_Error($error['message'], $error['code']);
$this->register_error($error['message'], $error['code']);
return false;
}
// If query was an insert
$is_insert = false;
if(preg_match('/^(insert|delete|update|create) /i', $query))
{
$is_insert = true;
// num affected rows
$return_value = $this->rows_affected = @oci_num_rows($stmt);
}
// If query was a select
else
{
// Get column information
if($num_cols = @oci_num_fields($stmt))
{
// Fetch the column meta data
for($i=1;$i <= $num_cols;$i++)
{
$this->col_info[($i-1)]->name = @oci_field_name($stmt,$i);
$this->col_info[($i-1)]->type = @oci_field_name($stmt,$i);
$this->col_info[($i-1)]->size = @oci_field_name($stmt,$i);
}
}
// If there are any results then get them
if ($this->num_rows = @oci_fetch_all($stmt,$results))
{
// Convert results into object orientated results..
// Due to Oracle strange return structure - loop through columns
foreach ( $results as $col_title => $col_contents )
{
$row_num=0;
// then - loop through rows
foreach ( $col_contents as $col_content )
{
$this->last_result[$row_num]->{$col_title} = $col_content;
$row_num++;
}
}
}
// num result rows
$return_value = $this->num_rows;
}
// disk caching of queries
$this->store_cache($query,$is_insert);
// If debug ALL queries
$this->trace || $this->debug_all ? $this->debug() : null ;
return $return_value;
}
}
?>