Blame Identity/Webenv/phpBB/3.0.4/includes/db/oracle.php

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