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