1 | <?php
|
---|
2 | /**
|
---|
3 | *
|
---|
4 | * @package dbal
|
---|
5 | * @version $Id$
|
---|
6 | * @copyright (c) 2005 phpBB Group
|
---|
7 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
---|
8 | *
|
---|
9 | */
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * @ignore
|
---|
13 | */
|
---|
14 | if (!defined('IN_PHPBB'))
|
---|
15 | {
|
---|
16 | exit;
|
---|
17 | }
|
---|
18 |
|
---|
19 | include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Oracle Database Abstraction Layer
|
---|
23 | * @package dbal
|
---|
24 | */
|
---|
25 | class dbal_oracle extends dbal
|
---|
26 | {
|
---|
27 | var $last_query_text = '';
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Connect to server
|
---|
31 | */
|
---|
32 | function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
|
---|
33 | {
|
---|
34 | $this->persistency = $persistency;
|
---|
35 | $this->user = $sqluser;
|
---|
36 | $this->server = $sqlserver . (($port) ? ':' . $port : '');
|
---|
37 | $this->dbname = $database;
|
---|
38 |
|
---|
39 | $connect = $database;
|
---|
40 |
|
---|
41 | // support for "easy connect naming"
|
---|
42 | if ($sqlserver !== '' && $sqlserver !== '/')
|
---|
43 | {
|
---|
44 | if (substr($sqlserver, -1, 1) == '/')
|
---|
45 | {
|
---|
46 | $sqlserver == substr($sqlserver, 0, -1);
|
---|
47 | }
|
---|
48 | $connect = $sqlserver . (($port) ? ':' . $port : '') . '/' . $database;
|
---|
49 | }
|
---|
50 |
|
---|
51 | $this->db_connect_id = ($new_link) ? @ocinlogon($this->user, $sqlpassword, $connect, 'UTF8') : (($this->persistency) ? @ociplogon($this->user, $sqlpassword, $connect, 'UTF8') : @ocilogon($this->user, $sqlpassword, $connect, 'UTF8'));
|
---|
52 |
|
---|
53 | return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Version information about used database
|
---|
58 | * @param bool $raw if true, only return the fetched sql_server_version
|
---|
59 | * @return string sql server version
|
---|
60 | */
|
---|
61 | function sql_server_info($raw = false)
|
---|
62 | {
|
---|
63 | /*
|
---|
64 | global $cache;
|
---|
65 |
|
---|
66 | if (empty($cache) || ($this->sql_server_version = $cache->get('oracle_version')) === false)
|
---|
67 | {
|
---|
68 | $result = @ociparse($this->db_connect_id, 'SELECT * FROM v$version WHERE banner LIKE \'Oracle%\'');
|
---|
69 | @ociexecute($result, OCI_DEFAULT);
|
---|
70 | @ocicommit($this->db_connect_id);
|
---|
71 |
|
---|
72 | $row = array();
|
---|
73 | @ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS);
|
---|
74 | @ocifreestatement($result);
|
---|
75 | $this->sql_server_version = trim($row['BANNER']);
|
---|
76 |
|
---|
77 | $cache->put('oracle_version', $this->sql_server_version);
|
---|
78 | }
|
---|
79 | */
|
---|
80 | $this->sql_server_version = @ociserverversion($this->db_connect_id);
|
---|
81 |
|
---|
82 | return $this->sql_server_version;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * SQL Transaction
|
---|
87 | * @access private
|
---|
88 | */
|
---|
89 | function _sql_transaction($status = 'begin')
|
---|
90 | {
|
---|
91 | switch ($status)
|
---|
92 | {
|
---|
93 | case 'begin':
|
---|
94 | return true;
|
---|
95 | break;
|
---|
96 |
|
---|
97 | case 'commit':
|
---|
98 | return @ocicommit($this->db_connect_id);
|
---|
99 | break;
|
---|
100 |
|
---|
101 | case 'rollback':
|
---|
102 | return @ocirollback($this->db_connect_id);
|
---|
103 | break;
|
---|
104 | }
|
---|
105 |
|
---|
106 | return true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Oracle specific code to handle the fact that it does not compare columns properly
|
---|
111 | * @access private
|
---|
112 | */
|
---|
113 | function _rewrite_col_compare($args)
|
---|
114 | {
|
---|
115 | if (sizeof($args) == 4)
|
---|
116 | {
|
---|
117 | if ($args[2] == '=')
|
---|
118 | {
|
---|
119 | return '(' . $args[0] . ' OR (' . $args[1] . ' is NULL AND ' . $args[3] . ' is NULL))';
|
---|
120 | }
|
---|
121 | else if ($args[2] == '<>')
|
---|
122 | {
|
---|
123 | // really just a fancy way of saying foo <> bar or (foo is NULL XOR bar is NULL) but SQL has no XOR :P
|
---|
124 | return '(' . $args[0] . ' OR ((' . $args[1] . ' is NULL AND ' . $args[3] . ' is NOT NULL) OR (' . $args[1] . ' is NOT NULL AND ' . $args[3] . ' is NULL)))';
|
---|
125 | }
|
---|
126 | }
|
---|
127 | else
|
---|
128 | {
|
---|
129 | return $this->_rewrite_where($args[0]);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Oracle specific code to handle it's lack of sanity
|
---|
135 | * @access private
|
---|
136 | */
|
---|
137 | function _rewrite_where($where_clause)
|
---|
138 | {
|
---|
139 | preg_match_all('/\s*(AND|OR)?\s*([\w_.()]++)\s*(?:(=|<[=>]?|>=?|LIKE)\s*((?>\'(?>[^\']++|\'\')*+\'|[\d-.()]+))|((NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))/', $where_clause, $result, PREG_SET_ORDER);
|
---|
140 | $out = '';
|
---|
141 | foreach ($result as $val)
|
---|
142 | {
|
---|
143 | if (!isset($val[5]))
|
---|
144 | {
|
---|
145 | if ($val[4] !== "''")
|
---|
146 | {
|
---|
147 | $out .= $val[0];
|
---|
148 | }
|
---|
149 | else
|
---|
150 | {
|
---|
151 | $out .= ' ' . $val[1] . ' ' . $val[2];
|
---|
152 | if ($val[3] == '=')
|
---|
153 | {
|
---|
154 | $out .= ' is NULL';
|
---|
155 | }
|
---|
156 | else if ($val[3] == '<>')
|
---|
157 | {
|
---|
158 | $out .= ' is NOT NULL';
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | else
|
---|
163 | {
|
---|
164 | $in_clause = array();
|
---|
165 | $sub_exp = substr($val[5], strpos($val[5], '(') + 1, -1);
|
---|
166 | $extra = false;
|
---|
167 | preg_match_all('/\'(?>[^\']++|\'\')*+\'|[\d-.]++/', $sub_exp, $sub_vals, PREG_PATTERN_ORDER);
|
---|
168 | $i = 0;
|
---|
169 | foreach ($sub_vals[0] as $sub_val)
|
---|
170 | {
|
---|
171 | // two things:
|
---|
172 | // 1) This determines if an empty string was in the IN clausing, making us turn it into a NULL comparison
|
---|
173 | // 2) This fixes the 1000 list limit that Oracle has (ORA-01795)
|
---|
174 | if ($sub_val !== "''")
|
---|
175 | {
|
---|
176 | $in_clause[(int) $i++/1000][] = $sub_val;
|
---|
177 | }
|
---|
178 | else
|
---|
179 | {
|
---|
180 | $extra = true;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | if (!$extra && $i < 1000)
|
---|
184 | {
|
---|
185 | $out .= $val[0];
|
---|
186 | }
|
---|
187 | else
|
---|
188 | {
|
---|
189 | $out .= ' ' . $val[1] . '(';
|
---|
190 | $in_array = array();
|
---|
191 |
|
---|
192 | // constuct each IN() clause
|
---|
193 | foreach ($in_clause as $in_values)
|
---|
194 | {
|
---|
195 | $in_array[] = $val[2] . ' ' . (isset($val[6]) ? $val[6] : '') . 'IN(' . implode(', ', $in_values) . ')';
|
---|
196 | }
|
---|
197 |
|
---|
198 | // Join the IN() clauses against a few ORs (IN is just a nicer OR anyway)
|
---|
199 | $out .= implode(' OR ', $in_array);
|
---|
200 |
|
---|
201 | // handle the empty string case
|
---|
202 | if ($extra)
|
---|
203 | {
|
---|
204 | $out .= ' OR ' . $val[2] . ' is ' . (isset($val[6]) ? $val[6] : '') . 'NULL';
|
---|
205 | }
|
---|
206 | $out .= ')';
|
---|
207 |
|
---|
208 | unset($in_array, $in_clause);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | return $out;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Base query method
|
---|
218 | *
|
---|
219 | * @param string $query Contains the SQL query which shall be executed
|
---|
220 | * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
|
---|
221 | * @return mixed When casted to bool the returned value returns true on success and false on failure
|
---|
222 | *
|
---|
223 | * @access public
|
---|
224 | */
|
---|
225 | function sql_query($query = '', $cache_ttl = 0)
|
---|
226 | {
|
---|
227 | if ($query != '')
|
---|
228 | {
|
---|
229 | global $cache;
|
---|
230 |
|
---|
231 | // EXPLAIN only in extra debug mode
|
---|
232 | if (defined('DEBUG_EXTRA'))
|
---|
233 | {
|
---|
234 | $this->sql_report('start', $query);
|
---|
235 | }
|
---|
236 |
|
---|
237 | $this->last_query_text = $query;
|
---|
238 | $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
|
---|
239 | $this->sql_add_num_queries($this->query_result);
|
---|
240 |
|
---|
241 | if ($this->query_result === false)
|
---|
242 | {
|
---|
243 | $in_transaction = false;
|
---|
244 | if (!$this->transaction)
|
---|
245 | {
|
---|
246 | $this->sql_transaction('begin');
|
---|
247 | }
|
---|
248 | else
|
---|
249 | {
|
---|
250 | $in_transaction = true;
|
---|
251 | }
|
---|
252 |
|
---|
253 | $array = array();
|
---|
254 |
|
---|
255 | // We overcome Oracle's 4000 char limit by binding vars
|
---|
256 | if (strlen($query) > 4000)
|
---|
257 | {
|
---|
258 | if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/sU', $query, $regs))
|
---|
259 | {
|
---|
260 | if (strlen($regs[3]) > 4000)
|
---|
261 | {
|
---|
262 | $cols = explode(', ', $regs[2]);
|
---|
263 |
|
---|
264 | preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
|
---|
265 |
|
---|
266 | if (sizeof($cols) !== sizeof($vals))
|
---|
267 | {
|
---|
268 | // Try to replace some common data we know is from our restore script or from other sources
|
---|
269 | $regs[3] = str_replace("'||chr(47)||'", '/', $regs[3]);
|
---|
270 | $_vals = explode(', ', $regs[3]);
|
---|
271 |
|
---|
272 | $vals = array();
|
---|
273 | $is_in_val = false;
|
---|
274 | $i = 0;
|
---|
275 | $string = '';
|
---|
276 |
|
---|
277 | foreach ($_vals as $value)
|
---|
278 | {
|
---|
279 | if (strpos($value, "'") === false && !$is_in_val)
|
---|
280 | {
|
---|
281 | $vals[$i++] = $value;
|
---|
282 | continue;
|
---|
283 | }
|
---|
284 |
|
---|
285 | if (substr($value, -1) === "'")
|
---|
286 | {
|
---|
287 | $vals[$i] = $string . (($is_in_val) ? ', ' : '') . $value;
|
---|
288 | $string = '';
|
---|
289 | $is_in_val = false;
|
---|
290 |
|
---|
291 | if ($vals[$i][0] !== "'")
|
---|
292 | {
|
---|
293 | $vals[$i] = "''" . $vals[$i];
|
---|
294 | }
|
---|
295 | $i++;
|
---|
296 | continue;
|
---|
297 | }
|
---|
298 | else
|
---|
299 | {
|
---|
300 | $string .= (($is_in_val) ? ', ' : '') . $value;
|
---|
301 | $is_in_val = true;
|
---|
302 | }
|
---|
303 | }
|
---|
304 |
|
---|
305 | if ($string)
|
---|
306 | {
|
---|
307 | // New value if cols != value
|
---|
308 | $vals[(sizeof($cols) !== sizeof($vals)) ? $i : $i - 1] .= $string;
|
---|
309 | }
|
---|
310 |
|
---|
311 | $vals = array(0 => $vals);
|
---|
312 | }
|
---|
313 |
|
---|
314 | $inserts = $vals[0];
|
---|
315 | unset($vals);
|
---|
316 |
|
---|
317 | foreach ($inserts as $key => $value)
|
---|
318 | {
|
---|
319 | if (!empty($value) && $value[0] === "'" && strlen($value) > 4002) // check to see if this thing is greater than the max + 'x2
|
---|
320 | {
|
---|
321 | $inserts[$key] = ':' . strtoupper($cols[$key]);
|
---|
322 | $array[$inserts[$key]] = str_replace("''", "'", substr($value, 1, -1));
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | $query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
|
---|
327 | }
|
---|
328 | }
|
---|
329 | else if (preg_match_all('/^(UPDATE [\\w_]++\\s+SET )([\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+)(?:,\\s*[\\w_]++\\s*=\\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]+))*+)\\s+(WHERE.*)$/s', $query, $data, PREG_SET_ORDER))
|
---|
330 | {
|
---|
331 | if (strlen($data[0][2]) > 4000)
|
---|
332 | {
|
---|
333 | $update = $data[0][1];
|
---|
334 | $where = $data[0][3];
|
---|
335 | preg_match_all('/([\\w_]++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[0][2], $temp, PREG_SET_ORDER);
|
---|
336 | unset($data);
|
---|
337 |
|
---|
338 | $cols = array();
|
---|
339 | foreach ($temp as $value)
|
---|
340 | {
|
---|
341 | if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 4002) // check to see if this thing is greater than the max + 'x2
|
---|
342 | {
|
---|
343 | $cols[] = $value[1] . '=:' . strtoupper($value[1]);
|
---|
344 | $array[$value[1]] = str_replace("''", "'", substr($value[2], 1, -1));
|
---|
345 | }
|
---|
346 | else
|
---|
347 | {
|
---|
348 | $cols[] = $value[1] . '=' . $value[2];
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | $query = $update . implode(', ', $cols) . ' ' . $where;
|
---|
353 | unset($cols);
|
---|
354 | }
|
---|
355 | }
|
---|
356 | }
|
---|
357 |
|
---|
358 | switch (substr($query, 0, 6))
|
---|
359 | {
|
---|
360 | case 'DELETE':
|
---|
361 | if (preg_match('/^(DELETE FROM [\w_]++ WHERE)((?:\s*(?:AND|OR)?\s*[\w_]+\s*(?:(?:=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]+)|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))*+)$/', $query, $regs))
|
---|
362 | {
|
---|
363 | $query = $regs[1] . $this->_rewrite_where($regs[2]);
|
---|
364 | unset($regs);
|
---|
365 | }
|
---|
366 | break;
|
---|
367 |
|
---|
368 | case 'UPDATE':
|
---|
369 | if (preg_match('/^(UPDATE [\\w_]++\\s+SET [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++)(?:, [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++))*+\\s+WHERE)(.*)$/s', $query, $regs))
|
---|
370 | {
|
---|
371 | $query = $regs[1] . $this->_rewrite_where($regs[2]);
|
---|
372 | unset($regs);
|
---|
373 | }
|
---|
374 | break;
|
---|
375 |
|
---|
376 | case 'SELECT':
|
---|
377 | $query = preg_replace_callback('/([\w_.]++)\s*(?:(=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]++|([\w_.]++))|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]++,? ?)*+\))/', array($this, '_rewrite_col_compare'), $query);
|
---|
378 | break;
|
---|
379 | }
|
---|
380 |
|
---|
381 | $this->query_result = @ociparse($this->db_connect_id, $query);
|
---|
382 |
|
---|
383 | foreach ($array as $key => $value)
|
---|
384 | {
|
---|
385 | @ocibindbyname($this->query_result, $key, $array[$key], -1);
|
---|
386 | }
|
---|
387 |
|
---|
388 | $success = @ociexecute($this->query_result, OCI_DEFAULT);
|
---|
389 |
|
---|
390 | if (!$success)
|
---|
391 | {
|
---|
392 | $this->sql_error($query);
|
---|
393 | $this->query_result = false;
|
---|
394 | }
|
---|
395 | else
|
---|
396 | {
|
---|
397 | if (!$in_transaction)
|
---|
398 | {
|
---|
399 | $this->sql_transaction('commit');
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (defined('DEBUG_EXTRA'))
|
---|
404 | {
|
---|
405 | $this->sql_report('stop', $query);
|
---|
406 | }
|
---|
407 |
|
---|
408 | if ($cache_ttl && method_exists($cache, 'sql_save'))
|
---|
409 | {
|
---|
410 | $this->open_queries[(int) $this->query_result] = $this->query_result;
|
---|
411 | $cache->sql_save($query, $this->query_result, $cache_ttl);
|
---|
412 | }
|
---|
413 | else if (strpos($query, 'SELECT') === 0 && $this->query_result)
|
---|
414 | {
|
---|
415 | $this->open_queries[(int) $this->query_result] = $this->query_result;
|
---|
416 | }
|
---|
417 | }
|
---|
418 | else if (defined('DEBUG_EXTRA'))
|
---|
419 | {
|
---|
420 | $this->sql_report('fromcache', $query);
|
---|
421 | }
|
---|
422 | }
|
---|
423 | else
|
---|
424 | {
|
---|
425 | return false;
|
---|
426 | }
|
---|
427 |
|
---|
428 | return $this->query_result;
|
---|
429 | }
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Build LIMIT query
|
---|
433 | */
|
---|
434 | function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
---|
435 | {
|
---|
436 | $this->query_result = false;
|
---|
437 |
|
---|
438 | $query = 'SELECT * FROM (SELECT /*+ FIRST_ROWS */ rownum AS xrownum, a.* FROM (' . $query . ') a WHERE rownum <= ' . ($offset + $total) . ') WHERE xrownum >= ' . $offset;
|
---|
439 |
|
---|
440 | return $this->sql_query($query, $cache_ttl);
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Return number of affected rows
|
---|
445 | */
|
---|
446 | function sql_affectedrows()
|
---|
447 | {
|
---|
448 | return ($this->query_result) ? @ocirowcount($this->query_result) : false;
|
---|
449 | }
|
---|
450 |
|
---|
451 | /**
|
---|
452 | * Fetch current row
|
---|
453 | */
|
---|
454 | function sql_fetchrow($query_id = false)
|
---|
455 | {
|
---|
456 | global $cache;
|
---|
457 |
|
---|
458 | if ($query_id === false)
|
---|
459 | {
|
---|
460 | $query_id = $this->query_result;
|
---|
461 | }
|
---|
462 |
|
---|
463 | if (isset($cache->sql_rowset[$query_id]))
|
---|
464 | {
|
---|
465 | return $cache->sql_fetchrow($query_id);
|
---|
466 | }
|
---|
467 |
|
---|
468 | if ($query_id !== false)
|
---|
469 | {
|
---|
470 | $row = array();
|
---|
471 | $result = @ocifetchinto($query_id, $row, OCI_ASSOC + OCI_RETURN_NULLS);
|
---|
472 |
|
---|
473 | if (!$result || !$row)
|
---|
474 | {
|
---|
475 | return false;
|
---|
476 | }
|
---|
477 |
|
---|
478 | $result_row = array();
|
---|
479 | foreach ($row as $key => $value)
|
---|
480 | {
|
---|
481 | // Oracle treats empty strings as null
|
---|
482 | if (is_null($value))
|
---|
483 | {
|
---|
484 | $value = '';
|
---|
485 | }
|
---|
486 |
|
---|
487 | // OCI->CLOB?
|
---|
488 | if (is_object($value))
|
---|
489 | {
|
---|
490 | $value = $value->load();
|
---|
491 | }
|
---|
492 |
|
---|
493 | $result_row[strtolower($key)] = $value;
|
---|
494 | }
|
---|
495 |
|
---|
496 | return $result_row;
|
---|
497 | }
|
---|
498 |
|
---|
499 | return false;
|
---|
500 | }
|
---|
501 |
|
---|
502 | /**
|
---|
503 | * Seek to given row number
|
---|
504 | * rownum is zero-based
|
---|
505 | */
|
---|
506 | function sql_rowseek($rownum, &$query_id)
|
---|
507 | {
|
---|
508 | global $cache;
|
---|
509 |
|
---|
510 | if ($query_id === false)
|
---|
511 | {
|
---|
512 | $query_id = $this->query_result;
|
---|
513 | }
|
---|
514 |
|
---|
515 | if (isset($cache->sql_rowset[$query_id]))
|
---|
516 | {
|
---|
517 | return $cache->sql_rowseek($rownum, $query_id);
|
---|
518 | }
|
---|
519 |
|
---|
520 | if ($query_id === false)
|
---|
521 | {
|
---|
522 | return false;
|
---|
523 | }
|
---|
524 |
|
---|
525 | // Reset internal pointer
|
---|
526 | @ociexecute($query_id, OCI_DEFAULT);
|
---|
527 |
|
---|
528 | // We do not fetch the row for rownum == 0 because then the next resultset would be the second row
|
---|
529 | for ($i = 0; $i < $rownum; $i++)
|
---|
530 | {
|
---|
531 | if (!$this->sql_fetchrow($query_id))
|
---|
532 | {
|
---|
533 | return false;
|
---|
534 | }
|
---|
535 | }
|
---|
536 |
|
---|
537 | return true;
|
---|
538 | }
|
---|
539 |
|
---|
540 | /**
|
---|
541 | * Get last inserted id after insert statement
|
---|
542 | */
|
---|
543 | function sql_nextid()
|
---|
544 | {
|
---|
545 | $query_id = $this->query_result;
|
---|
546 |
|
---|
547 | if ($query_id !== false && $this->last_query_text != '')
|
---|
548 | {
|
---|
549 | if (preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename))
|
---|
550 | {
|
---|
551 | $query = 'SELECT ' . $tablename[1] . '_seq.currval FROM DUAL';
|
---|
552 | $stmt = @ociparse($this->db_connect_id, $query);
|
---|
553 | @ociexecute($stmt, OCI_DEFAULT);
|
---|
554 |
|
---|
555 | $temp_result = @ocifetchinto($stmt, $temp_array, OCI_ASSOC + OCI_RETURN_NULLS);
|
---|
556 | @ocifreestatement($stmt);
|
---|
557 |
|
---|
558 | if ($temp_result)
|
---|
559 | {
|
---|
560 | return $temp_array['CURRVAL'];
|
---|
561 | }
|
---|
562 | else
|
---|
563 | {
|
---|
564 | return false;
|
---|
565 | }
|
---|
566 | }
|
---|
567 | }
|
---|
568 |
|
---|
569 | return false;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /**
|
---|
573 | * Free sql result
|
---|
574 | */
|
---|
575 | function sql_freeresult($query_id = false)
|
---|
576 | {
|
---|
577 | global $cache;
|
---|
578 |
|
---|
579 | if ($query_id === false)
|
---|
580 | {
|
---|
581 | $query_id = $this->query_result;
|
---|
582 | }
|
---|
583 |
|
---|
584 | if (isset($cache->sql_rowset[$query_id]))
|
---|
585 | {
|
---|
586 | return $cache->sql_freeresult($query_id);
|
---|
587 | }
|
---|
588 |
|
---|
589 | if (isset($this->open_queries[(int) $query_id]))
|
---|
590 | {
|
---|
591 | unset($this->open_queries[(int) $query_id]);
|
---|
592 | return @ocifreestatement($query_id);
|
---|
593 | }
|
---|
594 |
|
---|
595 | return false;
|
---|
596 | }
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * Escape string used in sql query
|
---|
600 | */
|
---|
601 | function sql_escape($msg)
|
---|
602 | {
|
---|
603 | return str_replace(array("'", "\0"), array("''", ''), $msg);
|
---|
604 | }
|
---|
605 |
|
---|
606 | /**
|
---|
607 | * Build LIKE expression
|
---|
608 | * @access private
|
---|
609 | */
|
---|
610 | function _sql_like_expression($expression)
|
---|
611 | {
|
---|
612 | return $expression . " ESCAPE '\\'";
|
---|
613 | }
|
---|
614 |
|
---|
615 | function _sql_custom_build($stage, $data)
|
---|
616 | {
|
---|
617 | return $data;
|
---|
618 | }
|
---|
619 |
|
---|
620 | function _sql_bit_and($column_name, $bit, $compare = '')
|
---|
621 | {
|
---|
622 | return 'BITAND(' . $column_name . ', ' . (1 << $bit) . ')' . (($compare) ? ' ' . $compare : '');
|
---|
623 | }
|
---|
624 |
|
---|
625 | function _sql_bit_or($column_name, $bit, $compare = '')
|
---|
626 | {
|
---|
627 | return 'BITOR(' . $column_name . ', ' . (1 << $bit) . ')' . (($compare) ? ' ' . $compare : '');
|
---|
628 | }
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * return sql error array
|
---|
632 | * @access private
|
---|
633 | */
|
---|
634 | function _sql_error()
|
---|
635 | {
|
---|
636 | $error = @ocierror();
|
---|
637 | $error = (!$error) ? @ocierror($this->query_result) : $error;
|
---|
638 | $error = (!$error) ? @ocierror($this->db_connect_id) : $error;
|
---|
639 |
|
---|
640 | if ($error)
|
---|
641 | {
|
---|
642 | $this->last_error_result = $error;
|
---|
643 | }
|
---|
644 | else
|
---|
645 | {
|
---|
646 | $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
|
---|
647 | }
|
---|
648 |
|
---|
649 | return $error;
|
---|
650 | }
|
---|
651 |
|
---|
652 | /**
|
---|
653 | * Close sql connection
|
---|
654 | * @access private
|
---|
655 | */
|
---|
656 | function _sql_close()
|
---|
657 | {
|
---|
658 | return @ocilogoff($this->db_connect_id);
|
---|
659 | }
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Build db-specific report
|
---|
663 | * @access private
|
---|
664 | */
|
---|
665 | function _sql_report($mode, $query = '')
|
---|
666 | {
|
---|
667 | switch ($mode)
|
---|
668 | {
|
---|
669 | case 'start':
|
---|
670 |
|
---|
671 | $html_table = false;
|
---|
672 |
|
---|
673 | // Grab a plan table, any will do
|
---|
674 | $sql = "SELECT table_name
|
---|
675 | FROM USER_TABLES
|
---|
676 | WHERE table_name LIKE '%PLAN_TABLE%'";
|
---|
677 | $stmt = ociparse($this->db_connect_id, $sql);
|
---|
678 | ociexecute($stmt);
|
---|
679 | $result = array();
|
---|
680 |
|
---|
681 | if (ocifetchinto($stmt, $result, OCI_ASSOC + OCI_RETURN_NULLS))
|
---|
682 | {
|
---|
683 | $table = $result['TABLE_NAME'];
|
---|
684 |
|
---|
685 | // This is the statement_id that will allow us to track the plan
|
---|
686 | $statement_id = substr(md5($query), 0, 30);
|
---|
687 |
|
---|
688 | // Remove any stale plans
|
---|
689 | $stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
|
---|
690 | ociexecute($stmt2);
|
---|
691 | ocifreestatement($stmt2);
|
---|
692 |
|
---|
693 | // Explain the plan
|
---|
694 | $sql = "EXPLAIN PLAN
|
---|
695 | SET STATEMENT_ID = '$statement_id'
|
---|
696 | FOR $query";
|
---|
697 | $stmt2 = ociparse($this->db_connect_id, $sql);
|
---|
698 | ociexecute($stmt2);
|
---|
699 | ocifreestatement($stmt2);
|
---|
700 |
|
---|
701 | // Get the data from the plan
|
---|
702 | $sql = "SELECT operation, options, object_name, object_type, cardinality, cost
|
---|
703 | FROM plan_table
|
---|
704 | START WITH id = 0 AND statement_id = '$statement_id'
|
---|
705 | CONNECT BY PRIOR id = parent_id
|
---|
706 | AND statement_id = '$statement_id'";
|
---|
707 | $stmt2 = ociparse($this->db_connect_id, $sql);
|
---|
708 | ociexecute($stmt2);
|
---|
709 |
|
---|
710 | $row = array();
|
---|
711 | while (ocifetchinto($stmt2, $row, OCI_ASSOC + OCI_RETURN_NULLS))
|
---|
712 | {
|
---|
713 | $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
---|
714 | }
|
---|
715 |
|
---|
716 | ocifreestatement($stmt2);
|
---|
717 |
|
---|
718 | // Remove the plan we just made, we delete them on request anyway
|
---|
719 | $stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
|
---|
720 | ociexecute($stmt2);
|
---|
721 | ocifreestatement($stmt2);
|
---|
722 | }
|
---|
723 |
|
---|
724 | ocifreestatement($stmt);
|
---|
725 |
|
---|
726 | if ($html_table)
|
---|
727 | {
|
---|
728 | $this->html_hold .= '</table>';
|
---|
729 | }
|
---|
730 |
|
---|
731 | break;
|
---|
732 |
|
---|
733 | case 'fromcache':
|
---|
734 | $endtime = explode(' ', microtime());
|
---|
735 | $endtime = $endtime[0] + $endtime[1];
|
---|
736 |
|
---|
737 | $result = @ociparse($this->db_connect_id, $query);
|
---|
738 | $success = @ociexecute($result, OCI_DEFAULT);
|
---|
739 | $row = array();
|
---|
740 |
|
---|
741 | while (@ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS))
|
---|
742 | {
|
---|
743 | // Take the time spent on parsing rows into account
|
---|
744 | }
|
---|
745 | @ocifreestatement($result);
|
---|
746 |
|
---|
747 | $splittime = explode(' ', microtime());
|
---|
748 | $splittime = $splittime[0] + $splittime[1];
|
---|
749 |
|
---|
750 | $this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
---|
751 |
|
---|
752 | break;
|
---|
753 | }
|
---|
754 | }
|
---|
755 | }
|
---|
756 |
|
---|
757 | ?>
|
---|