All pastes #145986 Raw Edit

PHPBB db2.php v1

public php v1 · immutable
#145986 ·published 2006-08-23 08:06 UTC
rendered paste body
<?php/*************************************************************************** *                                 db2.php *                            ------------------- *   begin                : Saturday, Feb 13, 2001 *   copyright            : (C) 2001 The phpBB Group *   email                : support@phpbb.com * *   $Id: db2.php,v 1.2 2002/01/28 17:24:45 psotfx Exp $ * ***************************************************************************/  /*************************************************************************** * @author sphericus * *   This program is free software; you can redistribute it and/or modify *   it under the terms of the GNU General Public License as published by *   the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * ***************************************************************************/if (!defined('IN_PHPBB')){	exit;}if(!defined("SQL_LAYER")){	define("SQL_LAYER","db2");	include($phpbb_root_path . 'includes/db/dbal.' . $phpEx);class dbal_db2 extends dbal{	var $db_connect_id;	var $query_result;	var $query_resultset;	var $query_numrows;	var $next_id;	var $row = array();	var $rowset = array();	var $row_index;	var $num_queries = 0;	//	// Constructor	//	function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = true)	{		$this->persistency = $persistency;		$this->user = $sqluser;		$this->password = $sqlpassword;		$this->dbname = $database;		$this->server = $sqlserver;		if($this->persistency)		{			$this->db_connect_id = db2_pconnect($this->server, "", "");		}		else		{			$this->db_connect_id = db2_connect($this->server, "", "");		}		if($this->db_connect_id)		{			@db2_autocommit($this->db_connect_id, off);			return $this->db_connect_id;		}		else		{			return false;		}	}	function _sql_transaction($status = 'begin')	{		switch ($status)		{			case 'begin':				return true;			break;			case 'commit':				return @db2_commit($this->db_connect_id);			break;			case 'rollback':				return @db2_rollback($this->db_connect_id);			break;		}		return true;	}	//	// Other base methods	//	function sql_close()	{		if($this->db_connect_id)		{			if($this->query_result)			{				@db2_free_result($this->query_result);			}			$result = @db2_close($this->db_connect_id);			return $result;		}		else		{			return false;		}	}	//	// Query method	//	function sql_query($query = '', $cache_ttl = 0)	{		if ($query != '')		{			global $cache;			// EXPLAIN only in extra debug mode			if (defined('DEBUG_EXTRA'))			{				$this->sql_report('start', $query);			}			$this->last_query_text = $query;			$this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;			$this->sql_add_num_queries($this->query_result);			if (!$this->query_result)			{				$in_transaction = false;				if (!$this->transaction)				{					$this->sql_transaction('begin');				}				else				{					$in_transaction = true;				}				$this->query_result = db2_execute($this->db_connect_id, $query);				$success = db2_execute($this->db_connect_id, $query);				if (!$success)				{					$this->sql_error($query);					$this->query_result = false;				}				else				{					if (!$in_transaction)					{						$this->sql_transaction('commit');					}				}				if (defined('DEBUG_EXTRA'))				{					$this->sql_report('stop', $query);				}				if ($cache_ttl && method_exists($cache, 'sql_save'))				{					$this->open_queries[(int) $this->query_result] = $this->query_result;					$cache->sql_save($query, $this->query_result, $cache_ttl);				}				else if (strpos($query, 'SELECT') === 0 && $this->query_result)				{					$this->open_queries[(int) $this->query_result] = $this->query_result;				}			}			else if (defined('DEBUG_EXTRA'))			{				$this->sql_report('fromcache', $query);			}		}		else		{			return false;		}		return ($this->query_result) ? $this->query_result : false;	}		/* From wikipedia http://en.wikipedia.org/wiki/SELECT	 * ROW_NUMBER() window function	 * Several window functions exist. ROW_NUMBER() OVER may be used for a simple limit on the returned rows. 	 * E.g., to return no more than ten rows:	 * 	SELECT * FROM 	 * 		(	 * 			SELECT	 * 				ROW_NUMBER() OVER (ORDER BY key ASC) AS rownumber,	 * 				columns	 * 			FROM tablename	 * 		) AS foo	 * 	WHERE rownumber <= 10	 * ROW_NUMBER can be non-deterministic: if key is not unique, each time you run the query it is 	 * possible to get different row numbers assigned to any rows where key is the same. 	 * When key is unique, each row will always get a unique row number.	 */			/**	* Build LIMIT query	*/	function sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0) 	{		if ($query != '')		{			$this->query_result = false; 			$query = 'SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY <some unique column> ASC) AS rownumber , a.* FROM (' . $query . ') AS a) WHERE rownumber <= ' . ($offset + $total) . ' AND WHERE rownumber >= ' . $offset;			return $this->sql_query($query, $cache_ttl); 		}		else		{			return false; 		}	}	/**	* Return number of rows	* Not used within core code	*/	function sql_numrows($query_id = false)	{		if (!$query_id)		{			$query_id = $this->query_result;		}		return ($query_id) ? @db2_num_rows($query_id) : false;	}		/**	* Fetch current row	*/	function sql_fetchrow($query_id = false)	{		global $cache;		if (!$query_id)		{			$query_id = $this->query_result;		}		if (isset($cache->sql_rowset[$query_id]))		{			return $cache->sql_fetchrow($query_id);		}		return ($query_id) ? @db2_fetch_array($query_id) : false;	}	/**	* Fetch field	* if rownum is false, the current row is used, else it is pointing to the row (zero-based)	*/	function sql_fetchfield($field, $rownum = false, $query_id = false)	{		if (!$query_id)		{			$query_id = $this->query_result;		}		if ($query_id)		{			if ($rownum !== false)			{				$this->sql_rowseek($rownum, $query_id);			}			$row = $this->sql_fetchrow($query_id);			return isset($row[$field]) ? $row[$field] : false;		}		return false;	}	/**	* Seek to given row number	* rownum is zero-based	*/	function sql_rowseek($rownum, $query_id = false)	{		if (!$query_id)		{			$query_id = $this->query_result;		}		$this->sql_freeresult($query_id);		$query_id = $this->sql_query($this->last_query_text);		if (!$query_id)		{			return false;		}		// We do not fetch the row for rownum == 0 because then the next resultset would be the second row		for ($i = 0; $i < $rownum; $i++)		{			if (!$this->sql_fetchrow($query_id))			{				return false;			}		}		return true;	}		/**	* Get last inserted id after insert statement	*/	function sql_nextid()	{		$result_id = @db2_exec($this->db_connect_id, "SELECT SYSIBM.IDENTITY_VAL_LOCAL() AS id FROM " . $tablename);		if ($result_id)		{			if (@db2_fetch_array($result_id))			{				$id = @db2_result($result_id, 1);					@db2_free_result($result_id);				return $id;			}			@db2_free_result($result_id);		}		return false;	}	/**	* Free sql result	*/	function sql_freeresult($query_id = false)	{		if (!$query_id)		{			$query_id = $this->query_result;		}		if (isset($this->open_queries[(int) $query_id]))		{			unset($this->open_queries[(int) $query_id]);			return @odbc_free_result($query_id);		}		return false;	}	/**	* Escape string used in sql query	*/	function sql_escape($msg)	{		return str_replace("'", "''", $msg);	}	/**	* Build db-specific query data	* @access: private	*/	function _sql_custom_build($stage, $data)	{		return $data;	}	/**	* return sql error array	* @access: private	*/	function _sql_error()	{		return array(			'message'	=> @db2_stmt_errormsg(),			'code'		=> @db2_stmt_error()		);	}	/**	* Close sql connection	* @access: private	*/	function _sql_close()	{		return @db2_close($this->db_connect_id);	}			/*	// Some code snippets from old db2.php							if(!eregi("^INSERT ",$query))			{								else				{					$this->query_result = db2_exec($this->db_connect_id, $query);					$row_offset = 0;					$this->result_numrows[$this->query_result] = 5E6;				}				$result_id = $this->query_result;				if($this->query_result && eregi("^SELECT", $query))				{					for($i = 1; $i < db2_num_fields($result_id)+1; $i++)					{						$this->result_field_names[$result_id][] = db2_field_name($result_id, $i);					}					$i =  $row_offset + 1;					$k = 0;					while(db2_fetch_row($result_id, $i) && $k < $this->result_numrows[$result_id])					{						for($j = 1; $j < count($this->result_field_names[$result_id])+1; $j++)						{							$this->result_rowset[$result_id][$k][$this->result_field_names[$result_id][$j-1]] = db2_result($result_id, $j);						}						$i++;						$k++;					}					$this->result_numrows[$result_id] = $k;					$this->row_index[$result_id] = 0;				}				else				{					$this->result_numrows[$result_id] = @db2_num_rows($result_id);					$this->row_index[$result_id] = 0;				}			}			else			{				if(eregi("^(INSERT|UPDATE) ", $query))				{					$query = preg_replace("/\\\'/s", "''", $query);				}				$this->query_result = db2_exec($this->db_connect_id, $query);				if($this->query_result)				{					$sql_id = "VALUES(IDENTITY_VAL_LOCAL())";					$id_result = db2_exec($this->db_connect_id, $sql_id);					if($id_result)					{						$row_result = db2_fetch_row($id_result);						if($row_result)						{							$this->next_id[$this->query_result] = db2_result($id_result, 1);						}					}				}				db2_commit($this->db_connect_id);				$this->query_limit_offset[$this->query_result] = 0;				$this->result_numrows[$this->query_result] = 0;			}			return $this->query_result;		}		else		{			return false;		}	}	//	// Other query methods	//	function sql_fieldtype($offset, $query_id = 0)	{		if(!$query_id)		{			$query_id = $this->query_result;		}		if($query_id)		{			$result = @db2_field_type($query_id, $offset);			return $result;		}		else		{			return false;		}	}	function sql_freeresult($query_id = 0)	{		if(!$query_id)		{			$query_id = $this->query_result;		}		if($query_id)		{			$result = @db2_free_result($query_id);			return $result;		}		else		{			return false;		}	}	function sql_error($query_id = 0)	{//		$result['code'] = @db2_error($this->db_connect_id);//		$result['message'] = @db2_errormsg($this->db_connect_id);		return "";	}} // class dbal_db2} // if ... define*/?>