<?php
/*
* Session Management for PHP3
*
* Copyright (c) 1998-2000 NetUSE AG
* Boris Erdmann, Kristian Koehntopp
*
* modified by Ben Drushell http://www.technobreeze.com/
*
* modified by Stefano De Nardis hide@address.com
*
* 2009-05-25 postgreSQL.php
*
*/
require_once 'iSQL.php';
require_once 'SQL.php';
define ('DEFAULT_PORT', '5432');
class postgreSQL extends SQL implements iSQL
{
public function __construct($args) {
if (!is_array($args) and count($args)==0)
$args=NULL;
parent::__construct($args,DEFAULT_PORT,DEFAULT_SSL,AUTOCONNECT,ERROR_ONLINE,SQL_DEBUG);
} // end of member function __construct
public function __destruct( ) {
parent::__destruct();
} // end of member function __destruct
public function connect() {
trigger_error("Implement " . __FUNCTION__);
if (trim($this->Hostname)!="")
$cstr="host=$this->Hostname ";
if (trim($this->Port)!="")
$cstr .="port=$this->Port ";
if (trim($this->Database)!="")
$cstr .="dbname=$this->Database ";
if (trim($this->Username)!="")
$cstr .="user=$this->Username ";
if (trim($this->Password)!="" or ($this->Password !=NULL))
$cstr .="password=$this->Password ";
if (trim($this->SSL)==true)
$cstr .="sslmode=require";
$this->LinkID=pg_connect($cstr);
if (!$this->LinkID){
$this->error("LinkID == false, connect failed");
return false;
}
else
return true;
} // end of member function connect
public function close( ) {
trigger_error("Implement " . __FUNCTION__);
if ($this->LinkID != 0) {
if(pg_close($this->LinkID)){
$this->LinkID=0;
return true;
}
else
return false;
}
else{
$this->LinkID=0;
return true;
}
} // end of member function close
public function query( $query_string ) {
trigger_error("Implement " . __FUNCTION__);
if ($query_string == "")
return false;
if ($this->AutoConnect)
$this->connect();
$this->QueryID = pg_query($this->LinkID, $query_string);
$this->Row=0;
if ($this->SQLDebug==true)
printf("<b>Query [%d]: %s </b>\n",$this->QueryID,$query_string );
if ($this->LinkID){
if (!$this->QueryID){
$this->error("Query Error:$query_string");
return false;
}
}
return true;
} // end of member function query
public function nextRecord( ) {
trigger_error("Implement " . __FUNCTION__);
$this->Record = pg_fetch_array($this->QueryID, $this->Row++, PGSQL_BOTH);
$stat = is_array($this->Record);
if (!$stat) {
$this->freeResult($this->QueryID);
return false;
}
else{
return true;
}
} // end of member function nextRecord
public function field($fieldName) {
trigger_error("Implement " . __FUNCTION__);
return $this->Record["$fieldName"];
} // end of member function field
public function affectedRows( ) {
trigger_error("Implement " . __FUNCTION__);
return pg_affected_rows($this->QueryID);
} // end of member function affectedRows
public function numRows( ) {
trigger_error("Implement " . __FUNCTION__);
return pg_num_rows($this->QueryID);
} // end of member function numRows
public function numFields( ) {
trigger_error("Implement " . __FUNCTION__);
return pg_num_fields($this->QueryID);
} // end of member function numFields
public function seek($position) {
trigger_error("Implement " . __FUNCTION__);
$this->Row=$position;
} // end of member function seek
public function lock( $table, $mode ){
trigger_error("Implement " . __FUNCTION__);
if ($this->transaction()){
switch ($mode){
case "read":
$mode_string=" IN SHARE MODE";
break;
case "write":
$mode_string=" IN ACCESS EXCLUSIVE MODE";
break;
default:
$mode_string=" IN SHARE MODE";
}
return $this->query("LOCK TABLE $table");
}
return false;
} // end of member function lock
public function unlock( ) {
trigger_error("Implement " . __FUNCTION__);
return $this->commit();
} // end of member function unlock
public function nextID($sequence_name ) {
trigger_error("Implement " . __FUNCTION__);
return ($this->currentID($sequence_name)+1);
} // end of member function nextId
public function serverVersion( ) {
trigger_error("Implement " . __FUNCTION__);
$v = pg_version($this->LinkID);
return $v['server'];
} // end of member function serverVersion
public function clientVersion( ) {
trigger_error("Implement " . __FUNCTION__);
$v = pg_version($this->LinkID);
return $v['client'];
} // end of member function clientVersion
public function currentID($sequence_name ) {
trigger_error("Implement " . __FUNCTION__);
$this->query("SELECT last_value FROM $sequence_name");
$this->nextRecord();
return $this->field('last_value');
} // end of member function currentId
public function transaction() {
trigger_error("Implement " . __FUNCTION__);
return $this->query("START TRANSACTION");
} // end of member function transaction
public function commit() {
trigger_error("Implement " . __FUNCTION__);
return $this->query("COMMIT");
} // end of member function commit
public function rollback() {
trigger_error("Implement " . __FUNCTION__);
return $this->query("rollback");
} // end of member function rollback
public function error($msg ) {
trigger_error("Implement " . __FUNCTION__);
if ( self::$ErrorOnLine){
printf("<b>Database %s error:</b> %s<br>\n",$this->Database, $msg);
printf("<b>PostgreSQL Error</b>: %s <br>\n", pg_last_error($this->LinkID));
}
} // end of member function error
public function freeResult( ) {
trigger_error("Implement " . __FUNCTION__);
$result=pg_free_result($this->QueryID);
if ($result)
$this->QueryID=0;
return $result;
} // end of member function freeResult
public function delete( $table, $options ) {
trigger_error("Implement " . __FUNCTION__);
$WHERE = $this->explodeOptions($options,"WHERE");
if (trim($table)=="")
return false;
$cstr=sprintf("DELETE FROM %s %s", $table, $WHERE);
return $this->query($cstr);
} // end of member function delete
public function update( $table, $options_set, $options_where ) {
trigger_error("Implement " . __FUNCTION__);
$SET = $this->explodeOptions($options_set,"SET");
$WHERE = $this->explodeOptions($options_where,"WHERE");
if (trim($table)=="")
return false;
if (trim($SET)=="")
return false;
$cstr=sprintf("UPDATE %s %s %s", $table, $SET,$WHERE);
return $this->query($cstr);
} // end of member function update
public function insert( $table, $options, $primary_key="" ) {
trigger_error("Implement " . __FUNCTION__);
if ( $primary_key != ""){
if ( is_array($primary_key))
$WHERE= $primary_key;
else
$WHERE= array($primary_key=>$options[$primary_key]);
$this->delete($table,$WHERE);
}
$INSERT = $this->explodeOptions($options,"INSERT");
if (trim($table)=="")
return false;
if (trim($INSERT)=="")
return false;
$cstr=sprintf("INSERT INTO %s %s", $table, $INSERT);
return $this->query($cstr);
} // end of member function insert
public function select( $table, $fields,$options, $order,$order_type, $limit,$offset ) {
trigger_error("Implement " . __FUNCTION__);
if (trim($table)=="")
return false;
if (count($fields)==0 or trim($fields)=="" or trim($field)=="*")
$query_fields="*";
else{
$f=count($fields);
for ($i=0;$i<=$f;$i++){
if ( $i+1 < $f )
$query_fields .= "$fields[$i], ";
else
$query_fields .= "$fields[$i] ";
}
}
$WHERE="";
$LIMIT_OFFSET="";
$WHERE = $this->explodeOptions($options,"WHERE");
if (trim($limit)!="")
$LIMIT_OFFSET.=" LIMIT $limit";
else
$LIMIT_OFFSET.="";
if (trim($offset)!="")
$LIMIT_OFFSET.=" OFFSET $offset";
$LIMIT_OFFSET.="";
if (trim($order)!="")
$ORDER="ORDER BY $order $order_type";
$cstr=sprintf("SELECT %s FROM %s %s %s %s", $query_fields,$table, $WHERE, $ORDER,$LIMIT_OFFSET);
return $this->query($cstr);
} // end of member function select
} // end of postgreSQL
?>