|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @package dbal
|
|
|
4c79b5 |
* @version $Id: oracle.php 9175 2008-12-05 11:18:59Z acydburn $
|
|
|
4c79b5 |
* @copyright (c) 2005 phpBB Group
|
|
|
4c79b5 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* @ignore
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
if (!defined('IN_PHPBB'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
exit;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Oracle Database Abstraction Layer
|
|
|
4c79b5 |
* @package dbal
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
class dbal_oracle extends dbal
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
var $last_query_text = '';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Connect to server
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->persistency = $persistency;
|
|
|
4c79b5 |
$this->user = $sqluser;
|
|
|
4c79b5 |
$this->server = $sqlserver . (($port) ? ':' . $port : '');
|
|
|
4c79b5 |
$this->dbname = $database;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$connect = $database;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// support for "easy connect naming"
|
|
|
4c79b5 |
if ($sqlserver !== '' && $sqlserver !== '/')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (substr($sqlserver, -1, 1) == '/')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$sqlserver == substr($sqlserver, 0, -1);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$connect = $sqlserver . (($port) ? ':' . $port : '') . '/' . $database;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$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'));
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return ($this->db_connect_id) ? $this->db_connect_id : $this->sql_error('');
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Version information about used database
|
|
|
4c79b5 |
* @param bool $raw if true, only return the fetched sql_server_version
|
|
|
4c79b5 |
* @return string sql server version
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_server_info($raw = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
/*
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (empty($cache) || ($this->sql_server_version = $cache->get('oracle_version')) === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$result = @ociparse($this->db_connect_id, 'SELECT * FROM v$version WHERE banner LIKE \'Oracle%\'');
|
|
|
4c79b5 |
@ociexecute($result, OCI_DEFAULT);
|
|
|
4c79b5 |
@ocicommit($this->db_connect_id);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$row = array();
|
|
|
4c79b5 |
@ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS);
|
|
|
4c79b5 |
@ocifreestatement($result);
|
|
|
4c79b5 |
$this->sql_server_version = trim($row['BANNER']);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$cache->put('oracle_version', $this->sql_server_version);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
$this->sql_server_version = @ociserverversion($this->db_connect_id);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->sql_server_version;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* SQL Transaction
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_transaction($status = 'begin')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($status)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'begin':
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'commit':
|
|
|
4c79b5 |
return @ocicommit($this->db_connect_id);
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'rollback':
|
|
|
4c79b5 |
return @ocirollback($this->db_connect_id);
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Oracle specific code to handle the fact that it does not compare columns properly
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _rewrite_col_compare($args)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (sizeof($args) == 4)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($args[2] == '=')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return '(' . $args[0] . ' OR (' . $args[1] . ' is NULL AND ' . $args[3] . ' is NULL))';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if ($args[2] == '<>')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// really just a fancy way of saying foo <> bar or (foo is NULL XOR bar is NULL) but SQL has no XOR :P
|
|
|
4c79b5 |
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)))';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $this->_rewrite_where($args[0]);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Oracle specific code to handle it's lack of sanity
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _rewrite_where($where_clause)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
preg_match_all('/\s*(AND|OR)?\s*([\w_.]++)\s*(?:(=|<[=>]?|>=?)\s*((?>\'(?>[^\']++|\'\')*+\'|[\d-.]+))|((NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))/', $where_clause, $result, PREG_SET_ORDER);
|
|
|
4c79b5 |
$out = '';
|
|
|
4c79b5 |
foreach ($result as $val)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!isset($val[5]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($val[4] !== "''")
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$out .= $val[0];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$out .= ' ' . $val[1] . ' ' . $val[2];
|
|
|
4c79b5 |
if ($val[3] == '=')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$out .= ' is NULL';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if ($val[3] == '<>')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$out .= ' is NOT NULL';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$in_clause = array();
|
|
|
4c79b5 |
$sub_exp = substr($val[5], strpos($val[5], '(') + 1, -1);
|
|
|
4c79b5 |
$extra = false;
|
|
|
4c79b5 |
preg_match_all('/\'(?>[^\']++|\'\')*+\'|[\d-.]++/', $sub_exp, $sub_vals, PREG_PATTERN_ORDER);
|
|
|
4c79b5 |
$i = 0;
|
|
|
4c79b5 |
foreach ($sub_vals[0] as $sub_val)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// two things:
|
|
|
4c79b5 |
// 1) This determines if an empty string was in the IN clausing, making us turn it into a NULL comparison
|
|
|
4c79b5 |
// 2) This fixes the 1000 list limit that Oracle has (ORA-01795)
|
|
|
4c79b5 |
if ($sub_val !== "''")
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$in_clause[(int) $i++/1000][] = $sub_val;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$extra = true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
if (!$extra && $i < 1000)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$out .= $val[0];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$out .= ' ' . $val[1] . '(';
|
|
|
4c79b5 |
$in_array = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// constuct each IN() clause
|
|
|
4c79b5 |
foreach ($in_clause as $in_values)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$in_array[] = $val[2] . ' ' . (isset($val[6]) ? $val[6] : '') . 'IN(' . implode(', ', $in_values) . ')';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Join the IN() clauses against a few ORs (IN is just a nicer OR anyway)
|
|
|
4c79b5 |
$out .= implode(' OR ', $in_array);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// handle the empty string case
|
|
|
4c79b5 |
if ($extra)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$out .= ' OR ' . $val[2] . ' is ' . (isset($val[6]) ? $val[6] : '') . 'NULL';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
$out .= ')';
|
|
|
4c79b5 |
|
|
|
4c79b5 |
unset($in_array, $in_clause);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $out;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Base query method
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @param string $query Contains the SQL query which shall be executed
|
|
|
4c79b5 |
* @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
|
|
|
4c79b5 |
* @return mixed When casted to bool the returned value returns true on success and false on failure
|
|
|
4c79b5 |
*
|
|
|
4c79b5 |
* @access public
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_query($query = '', $cache_ttl = 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if ($query != '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// EXPLAIN only in extra debug mode
|
|
|
4c79b5 |
if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('start', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->last_query_text = $query;
|
|
|
4c79b5 |
$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
|
|
|
4c79b5 |
$this->sql_add_num_queries($this->query_result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($this->query_result === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$in_transaction = false;
|
|
|
4c79b5 |
if (!$this->transaction)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_transaction('begin');
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$in_transaction = true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$array = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// We overcome Oracle's 4000 char limit by binding vars
|
|
|
4c79b5 |
if (strlen($query) > 4000)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (preg_match('/^(INSERT INTO[^(]++)\\(([^()]+)\\) VALUES[^(]++\\((.*?)\\)$/s', $query, $regs))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (strlen($regs[3]) > 4000)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$cols = explode(', ', $regs[2]);
|
|
|
4c79b5 |
preg_match_all('/\'(?:[^\']++|\'\')*+\'|[\d-.]+/', $regs[3], $vals, PREG_PATTERN_ORDER);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$inserts = $vals[0];
|
|
|
4c79b5 |
unset($vals);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($inserts as $key => $value)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!empty($value) && $value[0] === "'" && strlen($value) > 4002) // check to see if this thing is greater than the max + 'x2
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$inserts[$key] = ':' . strtoupper($cols[$key]);
|
|
|
4c79b5 |
$array[$inserts[$key]] = str_replace("''", "'", substr($value, 1, -1));
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$query = $regs[1] . '(' . $regs[2] . ') VALUES (' . implode(', ', $inserts) . ')';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
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))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (strlen($data[0][2]) > 4000)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$update = $data[0][1];
|
|
|
4c79b5 |
$where = $data[0][3];
|
|
|
4c79b5 |
preg_match_all('/([\\w_]++)\\s*=\\s*(\'(?:[^\']++|\'\')*+\'|[\d-.]++)/', $data[0][2], $temp, PREG_SET_ORDER);
|
|
|
4c79b5 |
unset($data);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$cols = array();
|
|
|
4c79b5 |
foreach ($temp as $value)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!empty($value[2]) && $value[2][0] === "'" && strlen($value[2]) > 4002) // check to see if this thing is greater than the max + 'x2
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$cols[] = $value[1] . '=:' . strtoupper($value[1]);
|
|
|
4c79b5 |
$array[$value[1]] = str_replace("''", "'", substr($value[2], 1, -1));
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$cols[] = $value[1] . '=' . $value[2];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$query = $update . implode(', ', $cols) . ' ' . $where;
|
|
|
4c79b5 |
unset($cols);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
switch (substr($query, 0, 6))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'DELETE':
|
|
|
4c79b5 |
if (preg_match('/^(DELETE FROM [\w_]++ WHERE)((?:\s*(?:AND|OR)?\s*[\w_]+\s*(?:(?:=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]+)|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]+,? ?)*+\)))*+)$/', $query, $regs))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query = $regs[1] . $this->_rewrite_where($regs[2]);
|
|
|
4c79b5 |
unset($regs);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'UPDATE':
|
|
|
4c79b5 |
if (preg_match('/^(UPDATE [\\w_]++\\s+SET [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++)(?:, [\\w_]+\s*=\s*(?:\'(?:[^\']++|\'\')*+\'|[\d-.]++|:\w++))*+\\s+WHERE)(.*)$/s', $query, $regs))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query = $regs[1] . $this->_rewrite_where($regs[2]);
|
|
|
4c79b5 |
unset($regs);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'SELECT':
|
|
|
4c79b5 |
$query = preg_replace_callback('/([\w_.]++)\s*(?:(=|<>)\s*(?>\'(?>[^\']++|\'\')*+\'|[\d-.]++|([\w_.]++))|(?:NOT )?IN\s*\((?>\'(?>[^\']++|\'\')*+\',? ?|[\d-.]++,? ?)*+\))/', array($this, '_rewrite_col_compare'), $query);
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->query_result = @ociparse($this->db_connect_id, $query);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
foreach ($array as $key => $value)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
@ocibindbyname($this->query_result, $key, $array[$key], -1);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$success = @ociexecute($this->query_result, OCI_DEFAULT);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$success)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_error($query);
|
|
|
4c79b5 |
$this->query_result = false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!$in_transaction)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_transaction('commit');
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('stop', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($cache_ttl && method_exists($cache, 'sql_save'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
4c79b5 |
$cache->sql_save($query, $this->query_result, $cache_ttl);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (strpos($query, 'SELECT') === 0 && $this->query_result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->open_queries[(int) $this->query_result] = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else if (defined('DEBUG_EXTRA'))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->sql_report('fromcache', $query);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build LIMIT query
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->query_result = false;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$query = 'SELECT * FROM (SELECT /*+ FIRST_ROWS */ rownum AS xrownum, a.* FROM (' . $query . ') a WHERE rownum <= ' . ($offset + $total) . ') WHERE xrownum >= ' . $offset;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $this->sql_query($query, $cache_ttl);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Return number of affected rows
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_affectedrows()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return ($this->query_result) ? @ocirowcount($this->query_result) : false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Fetch current row
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_fetchrow($query_id = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_fetchrow($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id !== false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$row = array();
|
|
|
4c79b5 |
$result = @ocifetchinto($query_id, $row, OCI_ASSOC + OCI_RETURN_NULLS);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (!$result || !$row)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result_row = array();
|
|
|
4c79b5 |
foreach ($row as $key => $value)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// Oracle treats empty strings as null
|
|
|
4c79b5 |
if (is_null($value))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$value = '';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// OCI->CLOB?
|
|
|
4c79b5 |
if (is_object($value))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$value = $value->load();
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result_row[strtolower($key)] = $value;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $result_row;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Seek to given row number
|
|
|
4c79b5 |
* rownum is zero-based
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_rowseek($rownum, &$query_id)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_rowseek($rownum, $query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Reset internal pointer
|
|
|
4c79b5 |
@ociexecute($query_id, OCI_DEFAULT);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// We do not fetch the row for rownum == 0 because then the next resultset would be the second row
|
|
|
4c79b5 |
for ($i = 0; $i < $rownum; $i++)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (!$this->sql_fetchrow($query_id))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return true;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Get last inserted id after insert statement
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_nextid()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id !== false && $this->last_query_text != '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
if (preg_match('#^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)#is', $this->last_query_text, $tablename))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query = 'SELECT ' . $tablename[1] . '_seq.currval FROM DUAL';
|
|
|
4c79b5 |
$stmt = @ociparse($this->db_connect_id, $query);
|
|
|
4c79b5 |
@ociexecute($stmt, OCI_DEFAULT);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$temp_result = @ocifetchinto($stmt, $temp_array, OCI_ASSOC + OCI_RETURN_NULLS);
|
|
|
4c79b5 |
@ocifreestatement($stmt);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($temp_result)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $temp_array['CURRVAL'];
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Free sql result
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_freeresult($query_id = false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
global $cache;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($query_id === false)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$query_id = $this->query_result;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($cache->sql_rowset[$query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $cache->sql_freeresult($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (isset($this->open_queries[(int) $query_id]))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
unset($this->open_queries[(int) $query_id]);
|
|
|
4c79b5 |
return @ocifreestatement($query_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return false;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Escape string used in sql query
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function sql_escape($msg)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return str_replace(array("'", "\0"), array("''", ''), $msg);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build LIKE expression
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_like_expression($expression)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $expression . " ESCAPE '\\'";
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
function _sql_custom_build($stage, $data)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return $data;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* return sql error array
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_error()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$error = @ocierror();
|
|
|
4c79b5 |
$error = (!$error) ? @ocierror($this->query_result) : $error;
|
|
|
4c79b5 |
$error = (!$error) ? @ocierror($this->db_connect_id) : $error;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($error)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->last_error_result = $error;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
else
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$error = (isset($this->last_error_result) && $this->last_error_result) ? $this->last_error_result : array();
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
return $error;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Close sql connection
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_close()
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
return @ocilogoff($this->db_connect_id);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
/**
|
|
|
4c79b5 |
* Build db-specific report
|
|
|
4c79b5 |
* @access private
|
|
|
4c79b5 |
*/
|
|
|
4c79b5 |
function _sql_report($mode, $query = '')
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
switch ($mode)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
case 'start':
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$html_table = false;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Grab a plan table, any will do
|
|
|
4c79b5 |
$sql = "SELECT table_name
|
|
|
4c79b5 |
FROM USER_TABLES
|
|
|
4c79b5 |
WHERE table_name LIKE '%PLAN_TABLE%'";
|
|
|
4c79b5 |
$stmt = ociparse($this->db_connect_id, $sql);
|
|
|
4c79b5 |
ociexecute($stmt);
|
|
|
4c79b5 |
$result = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if (ocifetchinto($stmt, $result, OCI_ASSOC + OCI_RETURN_NULLS))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$table = $result['TABLE_NAME'];
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// This is the statement_id that will allow us to track the plan
|
|
|
4c79b5 |
$statement_id = substr(md5($query), 0, 30);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Remove any stale plans
|
|
|
4c79b5 |
$stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
|
|
|
4c79b5 |
ociexecute($stmt2);
|
|
|
4c79b5 |
ocifreestatement($stmt2);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Explain the plan
|
|
|
4c79b5 |
$sql = "EXPLAIN PLAN
|
|
|
4c79b5 |
SET STATEMENT_ID = '$statement_id'
|
|
|
4c79b5 |
FOR $query";
|
|
|
4c79b5 |
$stmt2 = ociparse($this->db_connect_id, $sql);
|
|
|
4c79b5 |
ociexecute($stmt2);
|
|
|
4c79b5 |
ocifreestatement($stmt2);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Get the data from the plan
|
|
|
4c79b5 |
$sql = "SELECT operation, options, object_name, object_type, cardinality, cost
|
|
|
4c79b5 |
FROM plan_table
|
|
|
4c79b5 |
START WITH id = 0 AND statement_id = '$statement_id'
|
|
|
4c79b5 |
CONNECT BY PRIOR id = parent_id
|
|
|
4c79b5 |
AND statement_id = '$statement_id'";
|
|
|
4c79b5 |
$stmt2 = ociparse($this->db_connect_id, $sql);
|
|
|
4c79b5 |
ociexecute($stmt2);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$row = array();
|
|
|
4c79b5 |
while (ocifetchinto($stmt2, $row, OCI_ASSOC + OCI_RETURN_NULLS))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
ocifreestatement($stmt2);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
// Remove the plan we just made, we delete them on request anyway
|
|
|
4c79b5 |
$stmt2 = ociparse($this->db_connect_id, "DELETE FROM $table WHERE statement_id='$statement_id'");
|
|
|
4c79b5 |
ociexecute($stmt2);
|
|
|
4c79b5 |
ocifreestatement($stmt2);
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
ocifreestatement($stmt);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
if ($html_table)
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
$this->html_hold .= '';
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
|
|
|
4c79b5 |
case 'fromcache':
|
|
|
4c79b5 |
$endtime = explode(' ', microtime());
|
|
|
4c79b5 |
$endtime = $endtime[0] + $endtime[1];
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$result = @ociparse($this->db_connect_id, $query);
|
|
|
4c79b5 |
$success = @ociexecute($result, OCI_DEFAULT);
|
|
|
4c79b5 |
$row = array();
|
|
|
4c79b5 |
|
|
|
4c79b5 |
while (@ocifetchinto($result, $row, OCI_ASSOC + OCI_RETURN_NULLS))
|
|
|
4c79b5 |
{
|
|
|
4c79b5 |
// Take the time spent on parsing rows into account
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
@ocifreestatement($result);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$splittime = explode(' ', microtime());
|
|
|
4c79b5 |
$splittime = $splittime[0] + $splittime[1];
|
|
|
4c79b5 |
|
|
|
4c79b5 |
$this->sql_report('record_fromcache', $query, $endtime, $splittime);
|
|
|
4c79b5 |
|
|
|
4c79b5 |
break;
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
}
|
|
|
4c79b5 |
|
|
|
4c79b5 |
?>
|