1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/includes/db/oracle.php Wed Mar 07 16:41:10 2012 +0400
1.3 @@ -0,0 +1,771 @@
1.4 +<?php
1.5 +/**
1.6 +*
1.7 +* @package dbal
1.8 +* @version $Id$
1.9 +* @copyright (c) 2005 phpBB Group
1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
1.11 +*
1.12 +*/
1.13 +
1.14 +/**
1.15 +* @ignore
1.16 +*/
1.17 +if (!defined('IN_PHPBB'))
1.18 +{
1.19 + exit;
1.20 +}
1.21 +
1.22 +include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
1.23 +
1.24 +/**
1.25 +* Oracle Database Abstraction Layer
1.26 +* @package dbal
1.27 +*/
1.28 +class dbal_oracle extends dbal
1.29 +{
1.30 + var $last_query_text = '';
1.31 +
1.32 + /**
1.33 + * Connect to server
1.34 + */
1.35 + function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
1.36 + {
1.37 + $this->persistency = $persistency;
1.38 + $this->user = $sqluser;
1.39 + $this->server = $sqlserver . (($port) ? ':' . $port : '');
1.40 + $this->dbname = $database;
1.41 +
1.42 + $connect = $database;
1.43 +
1.44 + // support for "easy connect naming"
1.45 + if ($sqlserver !== '' && $sqlserver !== '/')
1.46 + {
1.47 + if (substr($sqlserver, -1, 1) == '/')
1.48 + {
1.49 + $sqlserver == substr($sqlserver, 0, -1);
1.50 + }
1.51 + $connect = $sqlserver . (($port) ? ':' . $port : '') . '/' . $database;
1.52 + }
1.53 +
1.54 + $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'));
1.55 +
1.56 + return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
1.57 + }
1.58 +
1.59 + /**
1.60 + * Version information about used database
1.61 + * @param bool $raw if true, only return the fetched sql_server_version
1.62 + * @param bool $use_cache forced to false for Oracle
1.63 + * @return string sql server version
1.64 + */
1.65 + function sql_server_info($raw = false, $use_cache = true)
1.66 + {
1.67 + /**
1.68 + * force $use_cache false. I didn't research why the caching code below is commented out
1.69 + * but I assume its because the Oracle extension provides a direct method to access it
1.70 + * without a query.
1.71 + */
1.72 +
1.73 + $use_cache = false;
1.74 +/*
1.75 + global $cache;
1.76 +
1.77 + if (empty($cache) || ($this->sql_server_version = $cache->get('oracle_version')) === false)
1.78 + {
1.79 + $result = @ociparse($this->db_connect_id, 'SELECT * FROM v$version WHERE banner LIKE \'Oracle%\'');
1.80 + @ociexecute($result, OCI_DEFAULT);
1.81 + @ocicommit($this->db_connect_id);
1.82 +
1.83 + $row = array();
1.84 + @ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS);
1.85 + @ocifreestatement($result);
1.86 + $this->sql_server_version = trim($row['BANNER']);
1.87 +
1.88 + $cache->put('oracle_version', $this->sql_server_version);
1.89 + }
1.90 +*/
1.91 + $this->sql_server_version = @ociserverversion($this->db_connect_id);
1.92 +
1.93 + return $this->sql_server_version;
1.94 + }
1.95 +
1.96 + /**
1.97 + * SQL Transaction
1.98 + * @access private
1.99 + */
1.100 + function _sql_transaction($status = 'begin')
1.101 + {
1.102 + switch ($status)
1.103 + {
1.104 + case 'begin':
1.105 + return true;
1.106 + break;
1.107 +
1.108 + case 'commit':
1.109 + return @ocicommit($this->db_connect_id);
1.110 + break;
1.111 +
1.112 + case 'rollback':
1.113 + return @ocirollback($this->db_connect_id);
1.114 + break;
1.115 + }
1.116 +
1.117 + return true;
1.118 + }
1.119 +
1.120 + /**
1.121 + * Oracle specific code to handle the fact that it does not compare columns properly
1.122 + * @access private
1.123 + */
1.124 + function _rewrite_col_compare($args)
1.125 + {
1.126 + if (sizeof($args) == 4)
1.127 + {
1.128 + if ($args[2] == '=')
1.129 + {
1.130 + return '(' . $args[0] . ' OR (' . $args[1] . ' is NULL AND ' . $args[3] . ' is NULL))';
1.131 + }
1.132 + else if ($args[2] == '<>')
1.133 + {
1.134 + // really just a fancy way of saying foo <> bar or (foo is NULL XOR bar is NULL) but SQL has no XOR :P
1.135 + 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)))';
1.136 + }
1.137 + }
1.138 + else
1.139 + {
1.140 + return $this->_rewrite_where($args[0]);
1.141 + }
1.142 + }
1.143 +
1.144 + /**
1.145 + * Oracle specific code to handle it's lack of sanity
1.146 + * @access private
1.147 + */
1.148 + function _rewrite_where($where_clause)
1.149 + {
1.150 + preg_match_all('/\s*(AND|OR)?\s*([\w_.()]++)\s*(?:(=|<[=>]?|>=?|LIKE)\s*((?>\'(?>[^\']++|\'\')*+\'|[\d-.()]+))|((NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))/', $where_clause, $result, PREG_SET_ORDER);
1.151 + $out = '';
1.152 + foreach ($result as $val)
1.153 + {
1.154 + if (!isset($val[5]))
1.155 + {
1.156 + if ($val[4] !== "''")
1.157 + {
1.158 + $out .= $val[0];
1.159 + }
1.160 + else
1.161 + {
1.162 + $out .= ' ' . $val[1] . ' ' . $val[2];
1.163 + if ($val[3] == '=')
1.164 + {
1.165 + $out .= ' is NULL';
1.166 + }
1.167 + else if ($val[3] == '<>')
1.168 + {
1.169 + $out .= ' is NOT NULL';
1.170 + }
1.171 + }
1.172 + }
1.173 + else
1.174 + {
1.175 + $in_clause = array();
1.176 + $sub_exp = substr($val[5], strpos($val[5], '(') + 1, -1);
1.177 + $extra = false;
1.178 + preg_match_all('/\'(?>[^\']++|\'\')*+\'|[\d-.]++/', $sub_exp, $sub_vals, PREG_PATTERN_ORDER);
1.179 + $i = 0;
1.180 + foreach ($sub_vals[0] as $sub_val)
1.181 + {
1.182 + // two things:
1.183 + // 1) This determines if an empty string was in the IN clausing, making us turn it into a NULL comparison
1.184 + // 2) This fixes the 1000 list limit that Oracle has (ORA-01795)
1.185 + if ($sub_val !== "''")
1.186 + {
1.187 + $in_clause[(int) $i++/1000][] = $sub_val;
1.188 + }
1.189 + else
1.190 + {
1.191 + $extra = true;
1.192 + }
1.193 + }
1.194 + if (!$extra && $i < 1000)
1.195 + {
1.196 + $out .= $val[0];
1.197 + }
1.198 + else
1.199 + {
1.200 + $out .= ' ' . $val[1] . '(';
1.201 + $in_array = array();
1.202 +
1.203 + // constuct each IN() clause
1.204 + foreach ($in_clause as $in_values)
1.205 + {
1.206 + $in_array[] = $val[2] . ' ' . (isset($val[6]) ? $val[6] : '') . 'IN(' . implode(', ', $in_values) . ')';
1.207 + }
1.208 +
1.209 + // Join the IN() clauses against a few ORs (IN is just a nicer OR anyway)
1.210 + $out .= implode(' OR ', $in_array);
1.211 +
1.212 + // handle the empty string case
1.213 + if ($extra)
1.214 + {
1.215 + $out .= ' OR ' . $val[2] . ' is ' . (isset($val[6]) ? $val[6] : '') . 'NULL';
1.216 + }
1.217 + $out .= ')';
1.218 +
1.219 + unset($in_array, $in_clause);
1.220 + }
1.221 + }
1.222 + }
1.223 +
1.224 + return $out;
1.225 + }
1.226 +
1.227 + /**
1.228 + * Base query method
1.229 + *
1.230 + * @param string $query Contains the SQL query which shall be executed
1.231 + * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
1.232 + * @return mixed When casted to bool the returned value returns true on success and false on failure
1.233 + *
1.234 + * @access public
1.235 + */
1.236 + function sql_query($query = '', $cache_ttl = 0)
1.237 + {
1.238 + if ($query != '')
1.239 + {
1.240 + global $cache;
1.241 +
1.242 + // EXPLAIN only in extra debug mode
1.243 + if (defined('DEBUG_EXTRA'))
1.244 + {
1.245 + $this->sql_report('start', $query);
1.246 + }
1.247 +
1.248 + $this->last_query_text = $query;
1.249 + $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
1.250 + $this->sql_add_num_queries($this->query_result);
1.251 +
1.252 + if ($this->query_result === false)
1.253 + {
1.254 + $in_transaction = false;
1.255 + if (!$this->transaction)
1.256 + {
1.257 + $this->sql_transaction('begin');
1.258 + }
1.259 + else
1.260 + {
1.261 + $in_transaction = true;
1.262 + }
1.263 +
1.264 + $array = array();
1.265 +
1.266 + // We overcome Oracle's 4000 char limit by binding vars
1.267 + if (strlen($query) > 4000)
1.268 + {
1.269 + if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/sU', $query, $regs))
1.270 + {
1.271 + if (strlen($regs[3]) > 4000)
1.272 + {
1.273 + $cols = explode(', ', $regs[2]);
1.274 +
1.275 + preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
1.276 +
1.277 +/* The code inside this comment block breaks clob handling, but does allow the
1.278 + database restore script to work. If you want to allow no posts longer than 4KB
1.279 + and/or need the db restore script, uncomment this.
1.280 +
1.281 +
1.282 + if (sizeof($cols) !== sizeof($vals))
1.283 + {
1.284 + // Try to replace some common data we know is from our restore script or from other sources
1.285 + $regs[3] = str_replace("'||chr(47)||'", '/', $regs[3]);
1.286 + $_vals = explode(', ', $regs[3]);
1.287 +
1.288 + $vals = array();
1.289 + $is_in_val = false;
1.290 + $i = 0;
1.291 + $string = '';
1.292 +
1.293 + foreach ($_vals as $value)
1.294 + {
1.295 + if (strpos($value, "'") === false && !$is_in_val)
1.296 + {
1.297 + $vals[$i++] = $value;
1.298 + continue;
1.299 + }
1.300 +
1.301 + if (substr($value, -1) === "'")
1.302 + {
1.303 + $vals[$i] = $string . (($is_in_val) ? ', ' : '') . $value;
1.304 + $string = '';
1.305 + $is_in_val = false;
1.306 +
1.307 + if ($vals[$i][0] !== "'")
1.308 + {
1.309 + $vals[$i] = "''" . $vals[$i];
1.310 + }
1.311 + $i++;
1.312 + continue;
1.313 + }
1.314 + else
1.315 + {
1.316 + $string .= (($is_in_val) ? ', ' : '') . $value;
1.317 + $is_in_val = true;
1.318 + }
1.319 + }
1.320 +
1.321 + if ($string)
1.322 + {
1.323 + // New value if cols != value
1.324 + $vals[(sizeof($cols) !== sizeof($vals)) ? $i : $i - 1] .= $string;
1.325 + }
1.326 +
1.327 + $vals = array(0 => $vals);
1.328 + }
1.329 +*/
1.330 +
1.331 + $inserts = $vals[0];
1.332 + unset($vals);
1.333 +
1.334 + foreach ($inserts as $key => $value)
1.335 + {
1.336 + if (!empty($value) && $value[0] === "'" && strlen($value) > 4002) // check to see if this thing is greater than the max + 'x2
1.337 + {
1.338 + $inserts[$key] = ':' . strtoupper($cols[$key]);
1.339 + $array[$inserts[$key]] = str_replace("''", "'", substr($value, 1, -1));
1.340 + }
1.341 + }
1.342 +
1.343 + $query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
1.344 + }
1.345 + }
1.346 + 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))
1.347 + {
1.348 + if (strlen($data[0][2]) > 4000)
1.349 + {
1.350 + $update = $data[0][1];
1.351 + $where = $data[0][3];
1.352 + preg_match_all('/([\\w_]++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[0][2], $temp, PREG_SET_ORDER);
1.353 + unset($data);
1.354 +
1.355 + $cols = array();
1.356 + foreach ($temp as $value)
1.357 + {
1.358 + if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 4002) // check to see if this thing is greater than the max + 'x2
1.359 + {
1.360 + $cols[] = $value[1] . '=:' . strtoupper($value[1]);
1.361 + $array[$value[1]] = str_replace("''", "'", substr($value[2], 1, -1));
1.362 + }
1.363 + else
1.364 + {
1.365 + $cols[] = $value[1] . '=' . $value[2];
1.366 + }
1.367 + }
1.368 +
1.369 + $query = $update . implode(', ', $cols) . ' ' . $where;
1.370 + unset($cols);
1.371 + }
1.372 + }
1.373 + }
1.374 +
1.375 + switch (substr($query, 0, 6))
1.376 + {
1.377 + case 'DELETE':
1.378 + if (preg_match('/^(DELETE FROM [\w_]++ WHERE)((?:\s*(?:AND|OR)?\s*[\w_]+\s*(?:(?:=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]+)|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))*+)$/', $query, $regs))
1.379 + {
1.380 + $query = $regs[1] . $this->_rewrite_where($regs[2]);
1.381 + unset($regs);
1.382 + }
1.383 + break;
1.384 +
1.385 + case 'UPDATE':
1.386 + if (preg_match('/^(UPDATE [\\w_]++\\s+SET [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++)(?:, [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++))*+\\s+WHERE)(.*)$/s', $query, $regs))
1.387 + {
1.388 + $query = $regs[1] . $this->_rewrite_where($regs[2]);
1.389 + unset($regs);
1.390 + }
1.391 + break;
1.392 +
1.393 + case 'SELECT':
1.394 + $query = preg_replace_callback('/([\w_.]++)\s*(?:(=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]++|([\w_.]++))|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]++,? ?)*+\))/', array($this, '_rewrite_col_compare'), $query);
1.395 + break;
1.396 + }
1.397 +
1.398 + $this->query_result = @ociparse($this->db_connect_id, $query);
1.399 +
1.400 + foreach ($array as $key => $value)
1.401 + {
1.402 + @ocibindbyname($this->query_result, $key, $array[$key], -1);
1.403 + }
1.404 +
1.405 + $success = @ociexecute($this->query_result, OCI_DEFAULT);
1.406 +
1.407 + if (!$success)
1.408 + {
1.409 + $this->sql_error($query);
1.410 + $this->query_result = false;
1.411 + }
1.412 + else
1.413 + {
1.414 + if (!$in_transaction)
1.415 + {
1.416 + $this->sql_transaction('commit');
1.417 + }
1.418 + }
1.419 +
1.420 + if (defined('DEBUG_EXTRA'))
1.421 + {
1.422 + $this->sql_report('stop', $query);
1.423 + }
1.424 +
1.425 + if ($cache_ttl && method_exists($cache, 'sql_save'))
1.426 + {
1.427 + $this->open_queries[(int) $this->query_result] = $this->query_result;
1.428 + $cache->sql_save($query, $this->query_result, $cache_ttl);
1.429 + }
1.430 + else if (strpos($query, 'SELECT') === 0 && $this->query_result)
1.431 + {
1.432 + $this->open_queries[(int) $this->query_result] = $this->query_result;
1.433 + }
1.434 + }
1.435 + else if (defined('DEBUG_EXTRA'))
1.436 + {
1.437 + $this->sql_report('fromcache', $query);
1.438 + }
1.439 + }
1.440 + else
1.441 + {
1.442 + return false;
1.443 + }
1.444 +
1.445 + return $this->query_result;
1.446 + }
1.447 +
1.448 + /**
1.449 + * Build LIMIT query
1.450 + */
1.451 + function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
1.452 + {
1.453 + $this->query_result = false;
1.454 +
1.455 + $query = 'SELECT * FROM (SELECT /*+ FIRST_ROWS */ rownum AS xrownum, a.* FROM (' . $query . ') a WHERE rownum <= ' . ($offset + $total) . ') WHERE xrownum >= ' . $offset;
1.456 +
1.457 + return $this->sql_query($query, $cache_ttl);
1.458 + }
1.459 +
1.460 + /**
1.461 + * Return number of affected rows
1.462 + */
1.463 + function sql_affectedrows()
1.464 + {
1.465 + return ($this->query_result) ? @ocirowcount($this->query_result) : false;
1.466 + }
1.467 +
1.468 + /**
1.469 + * Fetch current row
1.470 + */
1.471 + function sql_fetchrow($query_id = false)
1.472 + {
1.473 + global $cache;
1.474 +
1.475 + if ($query_id === false)
1.476 + {
1.477 + $query_id = $this->query_result;
1.478 + }
1.479 +
1.480 + if (isset($cache->sql_rowset[$query_id]))
1.481 + {
1.482 + return $cache->sql_fetchrow($query_id);
1.483 + }
1.484 +
1.485 + if ($query_id !== false)
1.486 + {
1.487 + $row = array();
1.488 + $result = @ocifetchinto($query_id, $row, OCI_ASSOC + OCI_RETURN_NULLS);
1.489 +
1.490 + if (!$result || !$row)
1.491 + {
1.492 + return false;
1.493 + }
1.494 +
1.495 + $result_row = array();
1.496 + foreach ($row as $key => $value)
1.497 + {
1.498 + // Oracle treats empty strings as null
1.499 + if (is_null($value))
1.500 + {
1.501 + $value = '';
1.502 + }
1.503 +
1.504 + // OCI->CLOB?
1.505 + if (is_object($value))
1.506 + {
1.507 + $value = $value->load();
1.508 + }
1.509 +
1.510 + $result_row[strtolower($key)] = $value;
1.511 + }
1.512 +
1.513 + return $result_row;
1.514 + }
1.515 +
1.516 + return false;
1.517 + }
1.518 +
1.519 + /**
1.520 + * Seek to given row number
1.521 + * rownum is zero-based
1.522 + */
1.523 + function sql_rowseek($rownum, &$query_id)
1.524 + {
1.525 + global $cache;
1.526 +
1.527 + if ($query_id === false)
1.528 + {
1.529 + $query_id = $this->query_result;
1.530 + }
1.531 +
1.532 + if (isset($cache->sql_rowset[$query_id]))
1.533 + {
1.534 + return $cache->sql_rowseek($rownum, $query_id);
1.535 + }
1.536 +
1.537 + if ($query_id === false)
1.538 + {
1.539 + return false;
1.540 + }
1.541 +
1.542 + // Reset internal pointer
1.543 + @ociexecute($query_id, OCI_DEFAULT);
1.544 +
1.545 + // We do not fetch the row for rownum == 0 because then the next resultset would be the second row
1.546 + for ($i = 0; $i < $rownum; $i++)
1.547 + {
1.548 + if (!$this->sql_fetchrow($query_id))
1.549 + {
1.550 + return false;
1.551 + }
1.552 + }
1.553 +
1.554 + return true;
1.555 + }
1.556 +
1.557 + /**
1.558 + * Get last inserted id after insert statement
1.559 + */
1.560 + function sql_nextid()
1.561 + {
1.562 + $query_id = $this->query_result;
1.563 +
1.564 + if ($query_id !== false && $this->last_query_text != '')
1.565 + {
1.566 + if (preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename))
1.567 + {
1.568 + $query = 'SELECT ' . $tablename[1] . '_seq.currval FROM DUAL';
1.569 + $stmt = @ociparse($this->db_connect_id, $query);
1.570 + @ociexecute($stmt, OCI_DEFAULT);
1.571 +
1.572 + $temp_result = @ocifetchinto($stmt, $temp_array, OCI_ASSOC + OCI_RETURN_NULLS);
1.573 + @ocifreestatement($stmt);
1.574 +
1.575 + if ($temp_result)
1.576 + {
1.577 + return $temp_array['CURRVAL'];
1.578 + }
1.579 + else
1.580 + {
1.581 + return false;
1.582 + }
1.583 + }
1.584 + }
1.585 +
1.586 + return false;
1.587 + }
1.588 +
1.589 + /**
1.590 + * Free sql result
1.591 + */
1.592 + function sql_freeresult($query_id = false)
1.593 + {
1.594 + global $cache;
1.595 +
1.596 + if ($query_id === false)
1.597 + {
1.598 + $query_id = $this->query_result;
1.599 + }
1.600 +
1.601 + if (isset($cache->sql_rowset[$query_id]))
1.602 + {
1.603 + return $cache->sql_freeresult($query_id);
1.604 + }
1.605 +
1.606 + if (isset($this->open_queries[(int) $query_id]))
1.607 + {
1.608 + unset($this->open_queries[(int) $query_id]);
1.609 + return @ocifreestatement($query_id);
1.610 + }
1.611 +
1.612 + return false;
1.613 + }
1.614 +
1.615 + /**
1.616 + * Escape string used in sql query
1.617 + */
1.618 + function sql_escape($msg)
1.619 + {
1.620 + return str_replace(array("'", "\0"), array("''", ''), $msg);
1.621 + }
1.622 +
1.623 + /**
1.624 + * Build LIKE expression
1.625 + * @access private
1.626 + */
1.627 + function _sql_like_expression($expression)
1.628 + {
1.629 + return $expression . " ESCAPE '\\'";
1.630 + }
1.631 +
1.632 + function _sql_custom_build($stage, $data)
1.633 + {
1.634 + return $data;
1.635 + }
1.636 +
1.637 + function _sql_bit_and($column_name, $bit, $compare = '')
1.638 + {
1.639 + return 'BITAND(' . $column_name . ', ' . (1 << $bit) . ')' . (($compare) ? ' ' . $compare : '');
1.640 + }
1.641 +
1.642 + function _sql_bit_or($column_name, $bit, $compare = '')
1.643 + {
1.644 + return 'BITOR(' . $column_name . ', ' . (1 << $bit) . ')' . (($compare) ? ' ' . $compare : '');
1.645 + }
1.646 +
1.647 + /**
1.648 + * return sql error array
1.649 + * @access private
1.650 + */
1.651 + function _sql_error()
1.652 + {
1.653 + $error = @ocierror();
1.654 + $error = (!$error) ? @ocierror($this->query_result) : $error;
1.655 + $error = (!$error) ? @ocierror($this->db_connect_id) : $error;
1.656 +
1.657 + if ($error)
1.658 + {
1.659 + $this->last_error_result = $error;
1.660 + }
1.661 + else
1.662 + {
1.663 + $error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
1.664 + }
1.665 +
1.666 + return $error;
1.667 + }
1.668 +
1.669 + /**
1.670 + * Close sql connection
1.671 + * @access private
1.672 + */
1.673 + function _sql_close()
1.674 + {
1.675 + return @ocilogoff($this->db_connect_id);
1.676 + }
1.677 +
1.678 + /**
1.679 + * Build db-specific report
1.680 + * @access private
1.681 + */
1.682 + function _sql_report($mode, $query = '')
1.683 + {
1.684 + switch ($mode)
1.685 + {
1.686 + case 'start':
1.687 +
1.688 + $html_table = false;
1.689 +
1.690 + // Grab a plan table, any will do
1.691 + $sql = "SELECT table_name
1.692 + FROM USER_TABLES
1.693 + WHERE table_name LIKE '%PLAN_TABLE%'";
1.694 + $stmt = ociparse($this->db_connect_id, $sql);
1.695 + ociexecute($stmt);
1.696 + $result = array();
1.697 +
1.698 + if (ocifetchinto($stmt, $result, OCI_ASSOC + OCI_RETURN_NULLS))
1.699 + {
1.700 + $table = $result['TABLE_NAME'];
1.701 +
1.702 + // This is the statement_id that will allow us to track the plan
1.703 + $statement_id = substr(md5($query), 0, 30);
1.704 +
1.705 + // Remove any stale plans
1.706 + $stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
1.707 + ociexecute($stmt2);
1.708 + ocifreestatement($stmt2);
1.709 +
1.710 + // Explain the plan
1.711 + $sql = "EXPLAIN PLAN
1.712 + SET STATEMENT_ID = '$statement_id'
1.713 + FOR $query";
1.714 + $stmt2 = ociparse($this->db_connect_id, $sql);
1.715 + ociexecute($stmt2);
1.716 + ocifreestatement($stmt2);
1.717 +
1.718 + // Get the data from the plan
1.719 + $sql = "SELECT operation, options, object_name, object_type, cardinality, cost
1.720 + FROM plan_table
1.721 + START WITH id = 0 AND statement_id = '$statement_id'
1.722 + CONNECT BY PRIOR id = parent_id
1.723 + AND statement_id = '$statement_id'";
1.724 + $stmt2 = ociparse($this->db_connect_id, $sql);
1.725 + ociexecute($stmt2);
1.726 +
1.727 + $row = array();
1.728 + while (ocifetchinto($stmt2, $row, OCI_ASSOC + OCI_RETURN_NULLS))
1.729 + {
1.730 + $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
1.731 + }
1.732 +
1.733 + ocifreestatement($stmt2);
1.734 +
1.735 + // Remove the plan we just made, we delete them on request anyway
1.736 + $stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
1.737 + ociexecute($stmt2);
1.738 + ocifreestatement($stmt2);
1.739 + }
1.740 +
1.741 + ocifreestatement($stmt);
1.742 +
1.743 + if ($html_table)
1.744 + {
1.745 + $this->html_hold .= '</table>';
1.746 + }
1.747 +
1.748 + break;
1.749 +
1.750 + case 'fromcache':
1.751 + $endtime = explode(' ', microtime());
1.752 + $endtime = $endtime[0] + $endtime[1];
1.753 +
1.754 + $result = @ociparse($this->db_connect_id, $query);
1.755 + $success = @ociexecute($result, OCI_DEFAULT);
1.756 + $row = array();
1.757 +
1.758 + while (@ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS))
1.759 + {
1.760 + // Take the time spent on parsing rows into account
1.761 + }
1.762 + @ocifreestatement($result);
1.763 +
1.764 + $splittime = explode(' ', microtime());
1.765 + $splittime = $splittime[0] + $splittime[1];
1.766 +
1.767 + $this->sql_report('record_fromcache', $query, $endtime, $splittime);
1.768 +
1.769 + break;
1.770 + }
1.771 + }
1.772 +}
1.773 +
1.774 +?>
1.775 \ No newline at end of file