<?php
/*
Last change in version: 2.0
#########################################################################################
# ADbNewsSender 2 #
# Copyright (C) 2009 Florian Grannemann (E-mail: hide@address.com) #
# Website: http://adbnewssender.sf.net #
# #
# 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 3 of the License, or #
# any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see http://www.gnu.org/licenses/. #
#########################################################################################
*/
/*
This file implements the replace_into function.
*/
//$querystring contains the table name
//$params contains the columns as an associative array
//$params["columnname"]=value
//because PostgreSQL does not know replace_into, we have to delete the entry at first:
//fetching PKs and UKs:
$res=@pg_query("SELECT column_name FROM information_schema.key_column_usage, information_schema.table_constraints WHERE information_schema.table_constraints.table_name = '".strtolower($querystring)."' and information_schema.key_column_usage.table_name='".strtolower($querystring)."' and (constraint_type ='PRIMARY KEY');");
$res1=@pg_query("SELECT column_name FROM information_schema.key_column_usage, information_schema.table_constraints WHERE information_schema.table_constraints.table_name = '".strtolower($querystring)."' and information_schema.key_column_usage.table_name='".strtolower($querystring)."' and (constraint_type ='UNIQUE');");
$Pkeys=array();
$Ukeys=array();
//storing primary keys in an array
while($row=pg_fetch_row($res))
{
foreach($params as $name =>$value)
{
if($row[0]==strtolower($name))
{
$Pkeys[strtolower($name)]=$value;
}
}
}
//storing unique keys in an array
while($row=pg_fetch_row($res1))
{
foreach($params as $name =>$value)
{
if($row[0]==strtolower($name))
{
$Ukeys[strtolower($name)]=$value;
}
}
}
//now let's delete concerning entrys:
$delete_string="DELETE From ".$querystring." WHERE ";
//Primary keys:
$i=1;
foreach($Pkeys as $key =>$val)
{
if(is_numeric($params[$Pkeys[$i]]))
{
$delete_string=$delete_string."$key = $val";
}
else
{
$delete_string=$delete_string."$key = '$val'";
}
if($i < count($Pkeys))
{
$delete_string=$delete_string." AND ";
}
$i++;
}
if(count($Ukeys) >0)
{
if(count($Pkeys)>0)
{
$delete_string=$delete_string." OR (";
}
//Unique keys:
$i=1;
foreach($Ukeys as $key =>$val)
{
if(is_numeric($params[$Ukeys[$i]]))
{
$delete_string=$delete_string."$key = $val";
}
else
{
$delete_string=$delete_string."$key = '$val'";
}
if($i <count($Ukeys))
{
$delete_string=$delete_string." OR ";
}
$i++;
}
if(count($Pkeys)>0)
{
$delete_string=$delete_string." )";
}
}
$delete_string=$delete_string.";";
//deleting old entry
@pg_query( $delete_string);
//now inserting new entry:
$columnstring="(";
$valuestring="(";
$i=1;
foreach($params as $columnname => $value)
{
$columnstring=$columnstring.strtolower($columnname);
if(is_numeric($value))
{
$valuestring=$valuestring.$value;
}
else
{
$valuestring=$valuestring."'".$value."'";
}
if($i <count($params))
{
$columnstring=$columnstring.",";
$valuestring=$valuestring.",";
}
$i++;
}
$columnstring=$columnstring.")";
$valuestring=$valuestring.")";
$query="INSERT INTO $querystring $columnstring VALUES $valuestring;";
if(@pg_query($this->session,$query))
{
$result=true;
}
?>