Location: PHPKode > projects > OvBB > ovbb/upload/includes/pgsql.inc.php
<?php
//***************************************************************************//
//                                                                           //
//  Copyright (c) 2007 Brian Otto                                            //
//  Copyright (c) 2007 Jonathon Freeman                                      //
//  All rights reserved.                                                     //
//                                                                           //
//  This program is free software. You may use, modify, and/or redistribute  //
//  it under the terms of the MIT License.                                   //
//                                                                           //
//***************************************************************************//

// Special query constants
define('CONCAT_ATTACHMENT',  0);
define('CONCAT_AVATAR',      1);

// Class definition
class DBConnection
{
	var $objConnection;
	var $objSelect;
	var $objResult;
	var $aVersions = array('0.16a');
	var $aSpecialQueries = array();

	// Initializes the database connection.
	function DBConnection($aDBInfo)
	{
		global $aQueries;

		// Initialize query array.
		$aQueries = array();

		// Populate the special queries list.
		$this->aSpecialQueries[CONCAT_ATTACHMENT] = "UPDATE attachment SET filedata = filedata || '%s' WHERE id=%u";
		$this->aSpecialQueries[CONCAT_AVATAR] = "UPDATE avatar SET datum = datum || '%s' WHERE id=%u";

		// Some hosting providers aren't setup to accept "localhost" properly,
		// so only add the "host" parameter when the script is not connecting locally.
		if ($aDBInfo['address'] != 'localhost') {
			$host = "host='" . $aDBInfo['address'] . "' ";
		}

		// Try and connect to the database server.
		if($this->objConnection = @pg_connect($host . "dbname='" . $aDBInfo['database'] . "' user='" . $aDBInfo['username'] . "' password='" . $aDBInfo['password'] . "'"))
		{
				// Destroy the database connection information.
				unset($aDBInfo['address']);
				unset($aDBInfo['username']);
				unset($aDBInfo['password']);
				unset($aDBInfo['database']);

				// Flag success.
				$this->objSelect = TRUE;
		}
		else
		{
			// Flag failure.
			$this->objSelect = FALSE;
		}
	}

	// Executes a given SQL statement.
	function query($strSQL)
	{
		global $aQueries;

		// Save the query.
		$aQueries[] = $strSQL;

		// Execute the query.
		$querySuccess = pg_send_query($this->objConnection, $strSQL);
		$this->objResult = @pg_get_result($this->objConnection);

		// Return the result.
		if($querySuccess === FALSE)
		{
			DatabaseError();
		}
		else
		{
			return TRUE;
		}
	}

	// Executes a special, pre-defined query.
	function squery($iQueryID)
	{
		// Get the function arguments.
		$aArgs = func_get_args();

		// The first argument is supposedly the index into the special queries array,
		// so use it to grab the special query requested.
		$aArgs[0] = $this->aSpecialQueries[$iQueryID];

		// Create the SQL statement.
		$strSQL = call_user_func_array('sprintf', $aArgs);

		// Execute the query.
		return $this->query($strSQL);
	}

	// Retrieves a resultset as an array.
	function getresult($bIsAssoc = FALSE)
	{
		return($bIsAssoc ? pg_fetch_assoc($this->objResult) : pg_fetch_row($this->objResult));
	}

	// Retrieves all the resultsets.
	function getall($bIsAssoc = FALSE)
	{
		$allResults = array();

		while($result = $this->getresult($bIsAssoc))
		{
			$allResults[] = $result;
		}

		return $allResults;
	}

	// Returns the ID generated by the last INSERT.
	function getinsertid($table, $column = 'id')
	{
		$this->query("SELECT currval('" . $table . "_" . $column . "_seq')");
		$result = $this->getresult();
		return $result[0];
	}

	function geterror()
	{
		return pg_result_error($this->objResult);
	}

	// Sanitizes the input for use in a query.
	function sanitize($str, $binary = FALSE)
	{
		return ($binary ? pg_escape_bytea($str) : pg_escape_string($str));
	}

	// Unescapes binary data return in a result.
	function unescape($str)
	{
		return pg_unescape_bytea($str);
	}
}
?>
Return current item: OvBB