Location: PHPKode > projects > WB News > includes/lib/db/pgsql/DB_Sql.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2007                          #||
||#     Created: 22nd Dec 2006                           #||
||#     Filename: DB_Sql.php                             #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: DB_Sql.php,v 1.1.2.3 2008/06/21 02:28:51 pmcilwaine Exp $
 */

class DB_Sql
{

	var $dbhost = NULL;
	var $dbuser = NULL;
	var $dbpass = NULL;
	var $dbname = NULL;
	var $dbport = 5432;

	var $dbconn;
	var $dbres;

	var $connected = FALSE;

	var $current_record = array();
	var $last_sql = NULL;

	var $err_msg;

	/** DEBUG **/
	var $query_counter = 0;
	
	/** public properties used to determine what the DB can do **/
	var $triggers = TRUE;
	var $transactions = TRUE;
	
	/** get type **/
	var $db_type = "PostgreSQL";
	
	function DB_Sql( $dbhost, $dbuser, $dbpass, $dbname, $dbport = NULL )
	{
		$this->dbhost = $dbhost;
		$this->dbuser = $dbuser;
		$this->dbpass = $dbpass;
		$this->dbname = $dbname;

		if ( !is_null( $dbport ) )
		{
			$this->dbport = $dbport;
		}
		
		return $this->connect();
	}

	/**
	 *	Make a connection to the database
	 */
	function connect()
	{
		$dbconstr = sprintf(
			"host=%s port=%d dbname=%s user=%s password=%s",
			$this->dbhost,
			$this->dbport,
			$this->dbname,
			$this->dbuser,
			$this->dbpass
			);

		$this->dbconn = @pg_connect( $dbconstr );
		if ( !$this->dbconn )
		{
			$this->err_msg = "Cannot connect to database";
			return FALSE;
		}

		$this->connected = TRUE;
		return TRUE;
	}

	/**
	 *	Run an SQL Query on the database
	 *	
	 *	@param String $sql A query string
	 *	@return bool
	 */
	function query( $sql )
	{
		$this->last_sql = trim($sql);
		$this->current_record = array();
		
		$this->dbres = @pg_query( $this->dbconn, $sql );
		$this->query_counter++;

		if ( !$this->dbres )
		{
			$this->err_msg = pg_last_error( $this->dbconn );
			return FALSE;
		}

		return TRUE;
	}

	/**
	 *	We just need to return mysql_fetch_assoc 
	 *	an empty array means FALSE whilst an array
	 *	with values with be TRUE thats all we need to know
	 *	
	 *	@return bool
	 */
	function next_record()
	{
		if ( !is_resource( $this->dbres ) )
		{
			return FALSE;
		}
		
		$this->current_record = pg_fetch_assoc( $this->dbres );
		if ( $this->current_record )
		{
			return TRUE;
		}

		return FALSE;
	}
	
	/**
	 *	@return mixed
	 */
	function lastid()
	{
	}

	/**
	 *	@return int
	 */
	function affectedrows()
	{
		if ( !is_resource( $this->dbres ) )
		{
			return FALSE;
		}
		
		return pg_affected_rows( $this->dbres );
	}

	/**
	 *
	 *	@param String $name
	 *	@return mixed
	 */
	function field( $name )
	{
		if ( !$this->dbres )
		{
			return FALSE;
		}

		if ( !$this->current_record[$name] )
		{
			return FALSE;
		}

		return $this->current_record[$name];
	}
	
	/**
	 * string NewID( $seq );
	 *
	 * Get the next sequence
	 * 
	 * @param string $seq the sequence to get the id from
	 * @return string
	 */
	function NewID( $seq )
	{
		$seq = pg_escape_string( $seq );
		$this->query( "SELECT nextval('$seq')" );
		$this->next_record();
		
		return $this->field( "nextval" );
	}

	/**
	 * string escape( string $string );
	 * 
	 * Return an escaped string
	 * 
	 * @param string $string
	 * @return string
	 */
	function escape( $string )
	{
		return pg_escape_string( $this->dbconn, $string );
	}
	
	function GetVersion()
	{
		$this->query( "SELECT version() as v" );
		if ( $this->next_record() )
		{
			return $this->field( "v" );
		}

		return FALSE;
	}
	
}

?>
Return current item: WB News