Location: PHPKode > scripts > postgreSQL > postgresql/SQL.php
<?php
/*
 * Copyright (c) 2009 
 *                   Stefano De Nardis hide@address.com 
 *
 */

define ('DEFAULT_SSL', 'false');
define ('AUTOCONNECT','false');
define ('ERRORONLINE','false');
define ('SQL_DEBUG','false');

class SQL 
{

   /*** Attributes: ***/

  protected $Hostname;

  protected $Database;

  protected $Username;

  protected $Password;

  protected $Port;

  protected $LinkID;

  protected $QueryID;

  protected $Row;

  protected $Record;

  protected $SSL;
  
  protected $Query;

  protected $AutoConnect;
 
  protected $ErrorOnline;
 
  protected $SQLDebug;	

  
public function __construct($args,$DefaultPort,$DefaultSSL,$AutoConnect,$ErrorOnLine,$SQLDebug){


	if ($args!=NULL){

		if (trim($args['Hostname'])=="")
			$this->Hostname="localhost";
		else
			$this->Hostname=$args['Hostname'];


		if (trim($args['Database'])!="")
			$this->Database=$args['Database'];
		else
			$this->Database=NULL;	

		if (trim($args['Username'])!="")
			$this->Username=$args['Username'];
		else
			$this->Username=NULL;	


		if (trim($args['Password'])=="")
			$this->Password=NULL;	
		else
			$this->Password=$args['Password'];

	
		if (trim($args['SSL'])=="")
			$this->SSL=$DefaultSSL;
		else
			$this->SSL=$this->checkSSL($args['SSL']);
	

		if (trim($args['Port'])=="")
			$this->Port=$DefaultPort;
		else
			$this->Port=$this->checkPort($args['Port']);
	}
	else{
	
			$this->Hostname="localhost";
			$this->Database=NULL;
			$this->Username=NULL;
			$this->Password=NULL;
			$this->SSL=self::$DefaultSSL;
			$this->Port=self::$DefaultPort;	

	}
		$this->AutoConnect=$AutoConnect;
	        $this->ErrorOnline=$ErrorOnline;
		$this->SQLDebug=$SQLDebug;	
		$this->LinkID=0;
		$this->QueryID=0;
		$this->Row=0;
		$this->Record= NULL;
		$this->Query="";
    
  } // end of member function __construct

 public function __destruct( ) {
	
	unset($this->Record);
	if ($this->QueryID != 0)
		$this->freeResult();
	
  	if ($this->LinkID != 0) 
		$this->close();

  } // end of member function __destruct
 
protected function setHostname($value){
    trigger_error("Implement " . __FUNCTION__);
	if (trim($value) !=""){
		$this->Hostname=$value;
		return true;
	}else 
		return false;
} // End of function setHostname


 protected function setDatabase($value){
    trigger_error("Implement " . __FUNCTION__);
	if (trim($value) !=""){
		$this->Database=$value;
		return true;
}	else 
		return false;
} // End of function setDatabase
 
 protected function setUsername($value){
    trigger_error("Implement " . __FUNCTION__);
	if (trim($value) !=""){
		$this->Username=$value;
		return true;
	}
	else 
		return false;
} // End of function setUsername
 
 protected function setPassword($value){
    trigger_error("Implement " . __FUNCTION__);
	if (trim($value) !=""){
		$this->Password=$value;
		return true;
	}else if ($value == NULL){
		$this->Password=NULL;
		return true;
	}else 
		return false;
} // End of function setPassword
 
protected function setPort($value){
    trigger_error("Implement " . __FUNCTION__);

	if($this->checkPort($value)){
		$this->Port=$value;
		return true;
	}else 
		return false;
} // End of function setPort

 protected function setSSL($value){
    trigger_error("Implement " . __FUNCTION__);

	switch ($value){
	
	case true: 
		$this->SSL=$value;
		return true;
		break;
	case false:
		$this->SSL=$value;
		return true;
		break;

	default:
		return false;
	}
} // End of function setSSL


 protected function setAutoConnect($value){
    trigger_error("Implement " . __FUNCTION__);

	switch ($value){
	
	case true: 
		$this->AutoConnect=$value;
		return true;
		break;
	case false:
		$this->AutoConnect=$value;
		return true;
		break;

	default:
		return false;
	}
} // End of function setAutoConnect


 protected function setErrorOnLine($value){
    trigger_error("Implement " . __FUNCTION__);

	switch ($value){
	
	case true: 
		$this->ErrorOnLine=$value;
		return true;
		break;
	case false:
		$this->ErrorOnLine=$value;
		return true;
		break;

	default:
		return false;
	}
} // End of function setErrorOnLine

 protected function setSQLDebug($value){
    trigger_error("Implement " . __FUNCTION__);

	switch ($value){
	
	case true: 
		$this->SQLDebug=$value;
		return true;
		break;
	case false:
		$this->SQLDebug=$value;
		return true;
		break;

	default:
		return false;
	}
} // End of function setSQLDebug

 public function getAutoConnect(){ return $this->AutoConnect;}

 public function getErrorOnLine(){ return $this->ErrorOnline;}

 public function getSQLDebug(){ return $this->SQLDebug;}

 public function getLinkID(){ return $this->LinkID; }

 public function getQueryID(){ return $this->QueryID; }

 public function getRecord(){ return $this->Record; }
 
 public function getHostname(){ return $this->Hostname; }

 public function getPort(){ return $this->Port; }
 
 public function getDatabase(){ return $this->Database;}

 public function getUsername(){ return $this->Username; }

 public function getPassword(){ return $this->Password; }

 public function getSSL(){ return $this->SSL; }

protected function checkPort($value){
    trigger_error("Implement " . __FUNCTION__);

	if (is_numeric($value)){
		if ($value > 1024 and $value < 65535)	
			return $true;
		else{
			
			$this->error ("WARNING: Port Number is not corrected");
			return $false;
			}	
	}
	
	else{
		$this->error ("FATAL: Port numeber MUST be a number!");	
		return $false;
	}			
}

 protected function checkSSL($value){
    trigger_error("Implement " . __FUNCTION__);

	switch ($value){
	
	case true: 
		return true;
		break;
	case false:
		return false;
		break;

	default:
		return false;
	}
} // End of function checkSSL


protected function explodeOptions($options,$mode){
    trigger_error("Implement " . __FUNCTION__);

	switch($mode){
	case "WHERE":
		

		$WHERE="";
		$WHERE_OPTIONS=sizeof($options);

		$option_count=0;
	
		if ( $WHERE_OPTIONS >0){
			$WHERE=" where ";
	
			foreach ($options as $key=>$value){
			
				$option_count++;
				$OR=substr_count($value, '|'); 	
			
				if ($OR != 0){
				
					$SUB_WHERE=' (';
					$tok = strtok($value, "|");
					$value_count=0;	
			

					while ($tok!==false){
					
						if (substr_count($tok, '%')>0)
							$SUB_WHERE .= " $key like '$tok' ";
						else
							$SUB_WHERE .= " $key = '$tok' ";
				
				
						if ($value_count < $OR )
							$SUB_WHERE.=" OR ";
			
						$tok = strtok("|");	
						$value_count++;

					}
			
					$SUB_WHERE.=') ';
			
					$WHERE .= " $SUB_WHERE ";
			 	}	

				 else{
					if (substr_count($value, '%')>0)
						$WHERE .= " $key like '$value' ";
					else
						$WHERE .= " $key = '$value' ";
				}

				if ($option_count < $WHERE_OPTIONS)
					$WHERE.= " AND ";
				else
					$WHERE.= " ";
				
		}//foreach

		return $WHERE;
		}

		break;

		case "SET":
			$SET="";
			$option_count=0;
			$SET_OPTIONS=sizeof($options);
	
			if ( $SET_OPTIONS >0){
				$SET=" SET ";
	
				foreach ($options as $key=>$value){
					$option_count++;
					if ($option_count < $SET_OPTIONS)
						$SET.=" $key='$value',";
					else
						$SET.=" $key='$value'";
				}
			}					


			return $SET;
		break;
		case "INSERT":
			$INSERT="";
			$FIELDS="";
			$VALUES="";
			$option_count=0;

			$INSERT_OPTIONS=sizeof($options);
	
			if ( $INSERT_OPTIONS >0){
				$FIELDS="(";
				$VALUES="(";
	
				foreach ($options as $key=>$value){
					$option_count++;
					if ($option_count < $INSERT_OPTIONS){
						$FIELDS.=" $key,";
						$VALUES.="'$value',";
					}	

					else{
						$FIELDS.=" $key";
						$VALUES.="'$value'";
					}
				}
			 $FIELDS.=")";
                         $VALUES.=")";

			}
		return "$FIELDS values $VALUES" ;
	        								
		break;


	}//switch
}//End function explodeOptions

} // end of SQL
?>
Return current item: postgreSQL