Location: PHPKode > projects > O - OpenSource GroupWare > osgw/osgw/classes/class.db_pgsql.php
<?php
/**************************************************************************
   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.

   @Authors:	Ryan Thompson(hide@address.com)
***************************************************************************/

class db
{

	var $host = '';
	var $user = '';
	var $password = '';
	var $database = '';
	var $conn;
	var $db;
	var $db_type = 'pgsql';
	var $query = '';
	var $insert_id;
	var $num_rows = 0;
	var $row;
	var $record;
	var $error;
	var $config = FALSE;
	var $port = 5432;
	

	/*
	@Function: 	db_connect()
 	@Date: 	29-March-2003
 	@Author: 	Ryan Thompson
 	@Description:	Connect to PostgreSQL Database
 	@Variables:
 	*/
	function db_connect()
	{
	
	
		GLOBAL $O;
			
		$host = $this->host;
		$dbname = $this->database;
		$user = $this->user;
		$password = $this->password;
	
		$port = $this->port;
		
		$this->conn = @pg_connect("host=$host port=$port dbname=$dbname user=$user password=$password");//)
		//Some reason this is always return FALSE if checked to see
		//if connection worked.
		return TRUE;
	}


	/*!
		@function query()
		@author Ryan Thompson
		@abstract Sends query to database
		@version 0.1
		@params $sql - SQL statment to query database
		@return TRUE/FALSE
	*/
	function query($sql)
	{
		GLOBAL $O;
		if($sql == '')
		{
			return FALSE;
		}

		if($this->query = @pg_query($this->conn, $sql))
		{
			 
			$this->num_rows = @pg_num_rows($this->query);

		 	$this->error = @pg_last_error();
			$this->insert_id = pg_last_oid($this->query);
			return TRUE;
		} else {
		
			$this->error = @pg_last_error();
			
			$debug_data = array($sql, $this->error);
			$O->error->debug('SQL', $debug_data);
		}
	}

	/*!
		@function fetch_results()
		@author Ryan Thompson
		@abstract Fetchs results from query
		@version 0.1
		@return $data - array containing the current record
	*/
	function fetch_results()
	{

		$this->record = pg_fetch_array($this->query);
		$this->row++;
		return $data = is_array($this->record);
	}

	/*!
		@function db_close()
		@author Ryan Thompson
		@abstract Close connection to dabase
		@version 0.1
		@return TRUE/FALSE
	*/
	function db_close()
	{
		pg_close($this->conn);
		return TRUE;
	}
	
	
	/*!
		@function create_table()
		@author Ryan Thompson
		@abstract Creates SQL statement for a new table
		@version 0.1
		@params $table_name
		@params $fields - Array containing the fields and their information
		@return $sql
	*/
	function create_table($table_name, $table_data)
	{
		unset($sql);
		$sql = "CREATE TABLE $table_name (\n";
		
	foreach($table_data as $key=>$value)
	{
		$sql .= $key;

		foreach($value as $field=>$data)
		{
			if(in_array('AUTO_INCREMENT', $value))
			{
				$extra = " SERIAL";
			} else {
				
			switch($field)
			{
				case 'type':
					if($data == 'ENUM')
					{
						$data = 'VARCHAR';	
					} elseif($data == 'BOOL')
					{
						$data = 'INT';
					}
					$sql .= " $data";
					$type = $data;
				break;
				case 'size':
					if($type == 'CHAR')
					{
						$sql .= "($data)";
					}
				break;
				case 'null':
					if($data == 'NO')
					{
						$sql .= " NOT NULL\n";
					}
				break;
				case 'key':
					switch($data)
					{
						case 'PRI':
							if(strlen($primary) > 1)
							{
								$primary .= ", ";
							}
							$primary .= $key;
						break;
						case 'IND':
							if(strlen($index) > 1)
							{
							//	$index .= ", ";
							}
							//$index .= $key;
						break;
						case 'UNI':
							if(strlen($unique) > 1)
							{
								$unique .= ", ";
							}
							$unique .= $key;
						break;
						
					}
				break;
				case 'ex':
			
					if($data == "INC")
					{
						$extra = " SERIAL";	
					}
					
				break;
				case 'default':
				break;
				
			}
			}
			
		
		}
		if(isset($extra))
		{
			$sql .=  $extra;	
			unset($extra);
		}
		$sql .= ", ";
		
	}
	if(isset($primary))
	{	
		$sql .= " PRIMARY KEY ($primary)";
		if(isset($index) || isset($unique))
		{
			$sql .= ", ";	
		}
	}
	if(isset($index))
	{
		$sql .= " INDEX ($index)";
		if(isset($unique))
		{
			$sql .= ", ";	
		}
	}
	if(isset($unique))
	{
		$sql .= " UNIQUE ($unique)";
	}
	$sql = rtrim($sql);
	$sql = rtrim($sql,',');
	
	$sql .= ")";
		
		
		unset($primary);
		unset($index);
		unset($extra);
		unset($unique);
		
		
		return $sql;
		
	}
	/*!
		@function tables()
		@author Ryan Thompson
		@abstract Gets a list of tables in database
		@version 0.1
		@return $table_list
		@since 22-10-2003
	*/
	function tables()
	{
		
		$sql = "SELECT relname FROM pg_class WHERE relname !~ '^pg_'";
		
		$data = pg_query($this->conn, $sql);
		
		for($i = 0; $results = pg_fetch_array($data); $i++)
		{
			$table_list[$i] = $results['relname'];
		}
			
		return $table_list;
		
	}


	/*!
		@function drop_table()
		@author Ryan Thompson
		@abstract Drops an existing table
		@version 0.1
		@return TRUE
	*/
	function drop_table($table)
	{
		
		if(substr($table, '-4', 4) == '_seq')
		{
			$this->query("DROP SEQUENCE $table");
		} else {
			$this->query("DROP TABLE $table");
		}
		return TRUE;
	}
	
	/*!
		@function seatch_tables()
		@author Ryan Thompson
		@abstract Searches tables for fields
		@version 0.1
		@params $table_list
		@deprecated
	*/
	function search_tables($table_list)
	{
		
	}
}

?>
Return current item: O - OpenSource GroupWare