<?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 create table on PostgreSQL databases
*/
//$querystring contains table name.
//$params["columns"] contains the columns in an associative array
//$params["PKs"] contains the primary keys in an associative array
//$params["UKs"] contains the unique keys in an associative array
$columns=$params["columns"];
//$columns["columnname"]="Datatype|size";
//size only at varchar!
$PKs=$params["PKs"];
$UKs=$params["UKs"];
$null=$params["null"];
//defaults:
//$defaults["columnname"]=defaultvalue
$defaults=$params["defaults"];
//defining datatypes:
//$datatypes["ADBNS_DB_type"]="MySQLtype";
//translating datatypes:
$datatypes=array();
$datatypes["bigserial"]="bigserial";
$datatypes["serial"]="serial";
$datatypes["float"]="double precision";
$datatypes["float8"]=$datatypes["float"];
$datatypes["boolean"]="boolean";
$datatypes["text"]="text";
$datatypes["varchar"]="varchar";
$datatypes["integer"]="integer";
$datatypes["bigint"]="bigint";
$query="CREATE TABLE $querystring (";
$i=1;
$numberofcolumns=count($columns);
foreach($columns as $columnname=>$datatype)
{
$usedatatype="";
$datatype=explode("|",$datatype);
//translating datatypes:
if(strtolower($datatype[0])=="varchar")
{
if($datatype[1] <1)
{
$datatype[1]=1;
}
$usedatatype=$datatypes["varchar"]."(".$datatype[1].")";
}
else
{
$usedatatype=$datatypes[strtolower($datatype[0])];
}
$query=$query."$columnname $usedatatype";
//NULL?
if($null[$columnname]!=true)
{
$query=$query." NOT NULL";
}
//defaults:
if(array_key_exists($columnname, $defaults) && $datatype[0] !="text")
{
$query=$query." Default '".$defaults[$columnname]."'";
}
if($i <$numberofcolumns)
{
$query=$query.",";
}
$i++;
}
//primary keys:
if(count($PKs) >1)
{
$PKstring=",PRIMARY KEY(";
$i=0;
while($i <count($PKs))
{
if($PKs[$i])
{
$PKstring=$PKstring.$PKs[$i];
if($PKs[($i+1)])
{
$PKstring=$PKstring.",";
}
}
$i++;
}
$PKstring=$PKstring.")";
$query=$query.$PKstring;
}
//unique keys:
if(count($UKs) >1)
{
$UKstring=",UNIQUE(";
$i=0;
while($i <count($UKs))
{
if($UKs[$i])
{
$UKstring=$UKstring.strtolower($UKs[$i]);
if($UKs[($i+1)])
{
$UKstring=$UKstring.",";
}
}
$i++;
}
$UKstring=$UKstring.")";
$query=$query.$UKstring;
}
$query=$query.");";
if(@pg_query($this->session,$query))
{
$result=true;
}
?>