1 | <?php
|
---|
2 | /**
|
---|
3 | *
|
---|
4 | * @package dbal
|
---|
5 | * @version $Id: oracle.php 9175 2008-12-05 11:18:59Z acydburn $
|
---|
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*(?:(=|<[=>]?|>=?)\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[^(]++\\((.*?)\\)$/s', $query, $regs))
|
---|
259 | {
|
---|
260 | if (strlen($regs[3]) > 4000)
|
---|
261 | {
|
---|
262 | $cols = explode(', ', $regs[2]);
|
---|
263 | preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
|
---|
264 |
|
---|
265 | $inserts = $vals[0];
|
---|
266 | unset($vals);
|
---|
267 |
|
---|
268 | foreach ($inserts as $key => $value)
|
---|
269 | {
|
---|
270 | if (!empty($value) && $value[0] === "'" && strlen($value) > 4002) // check to see if this thing is greater than the max + 'x2
|
---|
271 | {
|
---|
272 | $inserts[$key] = ':' . strtoupper($cols[$key]);
|
---|
273 | $array[$inserts[$key]] = str_replace("''", "'", substr($value, 1, -1));
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | $query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
|
---|
278 | }
|
---|
279 | }
|
---|
280 | 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))
|
---|
281 | {
|
---|
282 | if (strlen($data[0][2]) > 4000)
|
---|
283 | {
|
---|
284 | $update = $data[0][1];
|
---|
285 | $where = $data[0][3];
|
---|
286 | preg_match_all('/([\\w_]++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[0][2], $temp, PREG_SET_ORDER);
|
---|
287 | unset($data);
|
---|
288 |
|
---|
289 | $cols = array();
|
---|
290 | foreach ($temp as $value)
|
---|
291 | {
|
---|
292 | if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 4002) // check to see if this thing is greater than the max + 'x2
|
---|
293 | {
|
---|
294 | $cols[] = $value[1] . '=:' . strtoupper($value[1]);
|
---|
295 | $array[$value[1]] = str_replace("''", "'", substr($value[2], 1, -1));
|
---|
296 | }
|
---|
297 | else
|
---|
298 | {
|
---|
299 | $cols[] = $value[1] . '=' . $value[2];
|
---|
300 | }
|
---|
301 | }
|
---|
302 |
|
---|
303 | $query = $update . implode(', ', $cols) . ' ' . $where;
|
---|
304 | unset($cols);
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | switch (substr($query, 0, 6))
|
---|
310 | {
|
---|
311 | case 'DELETE':
|
---|
312 | if (preg_match('/^(DELETE FROM [\w_]++ WHERE)((?:\s*(?:AND|OR)?\s*[\w_]+\s*(?:(?:=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]+)|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))*+)$/', $query, $regs))
|
---|
313 | {
|
---|
314 | $query = $regs[1] . $this->_rewrite_where($regs[2]);
|
---|
315 | unset($regs);
|
---|
316 | }
|
---|
317 | break;
|
---|
318 |
|
---|
319 | case 'UPDATE':
|
---|
320 | if (preg_match('/^(UPDATE [\\w_]++\\s+SET [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++)(?:, [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++))*+\\s+WHERE)(.*)$/s', $query, $regs))
|
---|
321 | {
|
---|
322 | $query = $regs[1] . $this->_rewrite_where($regs[2]);
|
---|
323 | unset($regs);
|
---|
324 | }
|
---|
325 | break;
|
---|
326 |
|
---|
327 | case 'SELECT':
|
---|
328 | $query = preg_replace_callback('/([\w_.]++)\s*(?:(=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]++|([\w_.]++))|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]++,? ?)*+\))/', array($this, '_rewrite_col_compare'), $query);
|
---|
329 | break;
|
---|
330 | }
|
---|
331 |
|
---|
332 | $this->query_result = @ociparse($this->db_connect_id, $query);
|
---|
333 |
|
---|
334 | foreach ($array as $key => $value)
|
---|
335 | {
|
---|
336 | @ocibindbyname($this->query_result, $key, $array[$key], -1);
|
---|
337 | }
|
---|
338 |
|
---|
339 | $success = @ociexecute($this->query_result, OCI_DEFAULT);
|
---|
340 |
|
---|
341 | if (!$success)
|
---|
342 | {
|
---|
343 | $this->sql_error($query);
|
---|
344 | $this->query_result = false;
|
---|
345 | }
|
---|
346 | else
|
---|
347 | {
|
---|
348 | if (!$in_transaction)
|
---|
349 | {
|
---|
350 | $this->sql_transaction('commit');
|
---|
351 | }
|
---|
352 | }
|
---|
353 |
|
---|
354 | if (defined('DEBUG_EXTRA'))
|
---|
355 | {
|
---|
356 | $this->sql_report('stop', $query);
|
---|
357 | }
|
---|
358 |
|
---|
359 | if ($cache_ttl && method_exists($cache, 'sql_save'))
|
---|
360 | {
|
---|
361 | $this->open_queries[(int) $this->query_result] = $this->query_result;
|
---|
362 | $cache->sql_save($query, $this->query_result, $cache_ttl);
|
---|
363 | }
|
---|
364 | else if (strpos($query, 'SELECT') === 0 && $this->query_result)
|
---|
365 | {
|
---|
366 | $this->open_queries[(int) $this->query_result] = $this->query_result;
|
---|
367 | }
|
---|
368 | }
|
---|
369 | else if (defined('DEBUG_EXTRA'))
|
---|
370 | {
|
---|
371 | $this->sql_report('fromcache', $query);
|
---|
372 | }
|
---|
373 | }
|
---|
374 | else
|
---|
375 | {
|
---|
376 | return false;
|
---|
377 | }
|
---|
378 |
|
---|
379 | return $this->query_result;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * Build LIMIT query
|
---|
384 | */
|
---|
385 | function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
---|
386 | {
|
---|
387 | $this->query_result = false;
|
---|
388 |
|
---|
389 | $query = 'SELECT * FROM (SELECT /*+ FIRST_ROWS */ rownum AS xrownum, a.* FROM (' . $query . ') a WHERE rownum <= ' . ($offset + $total) . ') WHERE xrownum >= ' . $offset;
|
---|
390 |
|
---|
391 | return $this->sql_query($query, $cache_ttl);
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Return number of affected rows
|
---|
396 | */
|
---|
397 | function sql_affectedrows()
|
---|
398 | {
|
---|
399 | return ($this->query_result) ? @ocirowcount($this->query_result) : false;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /**
|
---|
403 | * Fetch current row
|
---|
404 | */
|
---|
405 | function sql_fetchrow($query_id = false)
|
---|
406 | {
|
---|
407 | global $cache;
|
---|
408 |
|
---|
409 | if ($query_id === false)
|
---|
410 | {
|
---|
411 | $query_id = $this->query_result;
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (isset($cache->sql_rowset[$query_id]))
|
---|
415 | {
|
---|
416 | return $cache->sql_fetchrow($query_id);
|
---|
417 | }
|
---|
418 |
|
---|
419 | if ($query_id !== false)
|
---|
420 | {
|
---|
421 | $row = array();
|
---|
422 | $result = @ocifetchinto($query_id, $row, OCI_ASSOC + OCI_RETURN_NULLS);
|
---|
423 |
|
---|
424 | if (!$result || !$row)
|
---|
425 | {
|
---|
426 | return false;
|
---|
427 | }
|
---|
428 |
|
---|
429 | $result_row = array();
|
---|
430 | foreach ($row as $key => $value)
|
---|
431 | {
|
---|
432 | // Oracle treats empty strings as null
|
---|
433 | if (is_null($value))
|
---|
434 | {
|
---|
435 | $value = '';
|
---|
436 | }
|
---|
437 |
|
---|
438 | // OCI->CLOB?
|
---|
439 | if (is_object($value))
|
---|
440 | {
|
---|
441 | $value = $value->load();
|
---|
442 | }
|
---|
443 |
|
---|
444 | $result_row[strtolower($key)] = $value;
|
---|
445 | }
|
---|
446 |
|
---|
447 | return $result_row;
|
---|
448 | }
|
---|
449 |
|
---|
450 | return false;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Seek to given row number
|
---|
455 | * rownum is zero-based
|
---|
456 | */
|
---|
457 | function sql_rowseek($rownum, &$query_id)
|
---|
458 | {
|
---|
459 | global $cache;
|
---|
460 |
|
---|
461 | if ($query_id === false)
|
---|
462 | {
|
---|
463 | $query_id = $this->query_result;
|
---|
464 | }
|
---|
465 |
|
---|
466 | if (isset($cache->sql_rowset[$query_id]))
|
---|
467 | {
|
---|
468 | return $cache->sql_rowseek($rownum, $query_id);
|
---|
469 | }
|
---|
470 |
|
---|
471 | if ($query_id === false)
|
---|
472 | {
|
---|
473 | return false;
|
---|
474 | }
|
---|
475 |
|
---|
476 | // Reset internal pointer
|
---|
477 | @ociexecute($query_id, OCI_DEFAULT);
|
---|
478 |
|
---|
479 | // We do not fetch the row for rownum == 0 because then the next resultset would be the second row
|
---|
480 | for ($i = 0; $i < $rownum; $i++)
|
---|
481 | {
|
---|
482 | if (!$this->sql_fetchrow($query_id))
|
---|
483 | {
|
---|
484 | return false;
|
---|
485 | }
|
---|
486 | }
|
---|
487 |
|
---|
488 | return true;
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Get last inserted id after insert statement
|
---|
493 | */
|
---|
494 | function sql_nextid()
|
---|
495 | {
|
---|
496 | $query_id = $this->query_result;
|
---|
497 |
|
---|
498 | if ($query_id !== false && $this->last_query_text != '')
|
---|
499 | {
|
---|
500 | if (preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename))
|
---|
501 | {
|
---|
502 | $query = 'SELECT ' . $tablename[1] . '_seq.currval FROM DUAL';
|
---|
503 | $stmt = @ociparse($this->db_connect_id, $query);
|
---|
504 | @ociexecute($stmt, OCI_DEFAULT);
|
---|
505 |
|
---|
506 | $temp_result = @ocifetchinto($stmt, $temp_array, OCI_ASSOC + OCI_RETURN_NULLS);
|
---|
507 | @ocifreestatement($stmt);
|
---|
508 |
|
---|
509 | if ($temp_result)
|
---|
510 | {
|
---|
511 | return $temp_array['CURRVAL'];
|
---|
512 | }
|
---|
513 | else
|
---|
514 | {
|
---|
515 | return false;
|
---|
516 | }
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | return false;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /**
|
---|
524 | * Free sql result
|
---|
525 | */
|
---|
526 | function sql_freeresult($query_id = false)
|
---|
527 | {
|
---|
528 | global $cache;
|
---|
529 |
|
---|
530 | if ($query_id === false)
|
---|
531 | {
|
---|
532 | $query_id = $this->query_result;
|
---|
533 | }
|
---|
534 |
|
---|
535 | if (isset($cache->sql_rowset[$query_id]))
|
---|
536 | {
|
---|
537 | return $cache->sql_freeresult($query_id);
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (isset($this->open_queries[(int) $query_id]))
|
---|
541 | {
|
---|
542 | unset($this->open_queries[(int) $query_id]);
|
---|
543 | return @ocifreestatement($query_id);
|
---|
544 | }
|
---|
545 |
|
---|
546 | return false;
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Escape string used in sql query
|
---|
551 | */
|
---|
552 | function sql_escape($msg)
|
---|
553 | {
|
---|
554 | return str_replace(array("'", "\0"), array("''", ''), $msg);
|
---|
555 | }
|
---|
556 |
|
---|
557 | /**
|
---|
558 | * Build LIKE expression
|
---|
559 | * @access private
|
---|
560 | */
|
---|
561 | function _sql_like_expression($expression)
|
---|
562 | {
|
---|
563 | return $expression . " ESCAPE '\\'";
|
---|
564 | }
|
---|
565 |
|
---|
566 | function _sql_custom_build($stage, $data)
|
---|
567 | {
|
---|
568 | return $data;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /**
|
---|
572 | * return sql error array
|
---|
573 | * @access private
|
---|
574 | */
|
---|
575 | function _sql_error()
|
---|
576 | {
|
---|
577 | $error = @ocierror();
|
---|
578 | $error = (!$error) ? @ocierror($this->query_result) : $error;
|
---|
579 | $error = (!$error) ? @ocierror($this->db_connect_id) : $error;
|
---|
580 |
|
---|
581 | if ($error)
|
---|
582 | {
|
---|
583 | $this->last_error_result = $error;
|
---|
584 | }
|
---|
585 | else
|
---|
586 | {
|
---|
587 | $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
|
---|
588 | }
|
---|
589 |
|
---|
590 | return $error;
|
---|
591 | }
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Close sql connection
|
---|
595 | * @access private
|
---|
596 | */
|
---|
597 | function _sql_close()
|
---|
598 | {
|
---|
599 | return @ocilogoff($this->db_connect_id);
|
---|
600 | }
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Build db-specific report
|
---|
604 | * @access private
|
---|
605 | */
|
---|
606 | function _sql_report($mode, $query = '')
|
---|
607 | {
|
---|
608 | switch ($mode)
|
---|
609 | {
|
---|
610 | case 'start':
|
---|
611 |
|
---|
612 | $html_table = false;
|
---|
613 |
|
---|
614 | // Grab a plan table, any will do
|
---|
615 | $sql = "SELECT table_name
|
---|
616 | FROM USER_TABLES
|
---|
617 | WHERE table_name LIKE '%PLAN_TABLE%'";
|
---|
618 | $stmt = ociparse($this->db_connect_id, $sql);
|
---|
619 | ociexecute($stmt);
|
---|
620 | $result = array();
|
---|
621 |
|
---|
622 | if (ocifetchinto($stmt, $result, OCI_ASSOC + OCI_RETURN_NULLS))
|
---|
623 | {
|
---|
624 | $table = $result['TABLE_NAME'];
|
---|
625 |
|
---|
626 | // This is the statement_id that will allow us to track the plan
|
---|
627 | $statement_id = substr(md5($query), 0, 30);
|
---|
628 |
|
---|
629 | // Remove any stale plans
|
---|
630 | $stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
|
---|
631 | ociexecute($stmt2);
|
---|
632 | ocifreestatement($stmt2);
|
---|
633 |
|
---|
634 | // Explain the plan
|
---|
635 | $sql = "EXPLAIN PLAN
|
---|
636 | SET STATEMENT_ID = '$statement_id'
|
---|
637 | FOR $query";
|
---|
638 | $stmt2 = ociparse($this->db_connect_id, $sql);
|
---|
639 | ociexecute($stmt2);
|
---|
640 | ocifreestatement($stmt2);
|
---|
641 |
|
---|
642 | // Get the data from the plan
|
---|
643 | $sql = "SELECT operation, options, object_name, object_type, cardinality, cost
|
---|
644 | FROM plan_table
|
---|
645 | START WITH id = 0 AND statement_id = '$statement_id'
|
---|
646 | CONNECT BY PRIOR id = parent_id
|
---|
647 | AND statement_id = '$statement_id'";
|
---|
648 | $stmt2 = ociparse($this->db_connect_id, $sql);
|
---|
649 | ociexecute($stmt2);
|
---|
650 |
|
---|
651 | $row = array();
|
---|
652 | while (ocifetchinto($stmt2, $row, OCI_ASSOC + OCI_RETURN_NULLS))
|
---|
653 | {
|
---|
654 | $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
---|
655 | }
|
---|
656 |
|
---|
657 | ocifreestatement($stmt2);
|
---|
658 |
|
---|
659 | // Remove the plan we just made, we delete them on request anyway
|
---|
660 | $stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
|
---|
661 | ociexecute($stmt2);
|
---|
662 | ocifreestatement($stmt2);
|
---|
663 | }
|
---|
664 |
|
---|
665 | ocifreestatement($stmt);
|
---|
666 |
|
---|
667 | if ($html_table)
|
---|
668 | {
|
---|
669 | $this->html_hold .= '</table>';
|
---|
670 | }
|
---|
671 |
|
---|
672 | break;
|
---|
673 |
|
---|
674 | case 'fromcache':
|
---|
675 | $endtime = explode(' ', microtime());
|
---|
676 | $endtime = $endtime[0] + $endtime[1];
|
---|
677 |
|
---|
678 | $result = @ociparse($this->db_connect_id, $query);
|
---|
679 | $success = @ociexecute($result, OCI_DEFAULT);
|
---|
680 | $row = array();
|
---|
681 |
|
---|
682 | while (@ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS))
|
---|
683 | {
|
---|
684 | // Take the time spent on parsing rows into account
|
---|
685 | }
|
---|
686 | @ocifreestatement($result);
|
---|
687 |
|
---|
688 | $splittime = explode(' ', microtime());
|
---|
689 | $splittime = $splittime[0] + $splittime[1];
|
---|
690 |
|
---|
691 | $this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
---|
692 |
|
---|
693 | break;
|
---|
694 | }
|
---|
695 | }
|
---|
696 | }
|
---|
697 |
|
---|
698 | ?>
|
---|