Systemcheck!'; echo '

Pruefe Tabellen und Attribute!

'; //var_dump($datatypes); $tabs=array_keys($tabellen); //print_r($tabs); $i=0; foreach ($tabellen AS $tabelle) { $sql_query2=''; $pk=''; // Tabelle pruefen //var_dump($tabelle); $sql_query="SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='".$schemas[$tabelle['schemaid']]['caption']."' AND tablename='".$tabelle['caption']."';"; if (!$result=pg_query($conn,$sql_query)) echo '
'.$tabs[$i].': '.pg_last_error($conn).'
'; else { if (pg_num_rows($result)==0) { $sql_query= 'CREATE TABLE '.$schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption']." ("; foreach ($tabelle['attribute'] AS $attribut) { if ($datatypes[$attribut['datatypeid']]['caption']!='geometry') { //echo $datatypes[$attribut['datatypeid']]['caption']; $sql_query.= $attribut['caption'].' '; if ($attribut['pk']) $pk.=$attribut['caption'].','; $sql_query.=$datatypes[$attribut['datatypeid']]['caption']; if ($datatypes[$attribut['datatypeid']]['length']==1) $sql_query.='('.$attribut['datatypeparam1'].')'; if ($datatypes[$attribut['datatypeid']]['length']==2) $sql_query.='('.$attribut['datatypeparam1'].','.$attribut['datatypeparam2'].')'; if ($attribut['notnull']) $sql_query.=' NOT NULL'; if ($attribut['unique']) $sql_query.=' UNIQUE'; if ($attribut['defaultvalue']!="") $sql_query.=' DEFAULT '.$attribut['defaultvalue']; if ($attribut['checkconstraint']!="") $sql_query.=' CHECK ('.$attribut['checkconstraint'].')'; $sql_query.=', '; } else $sql_query2.="SELECT AddGeometryColumn('','".$tabelle['caption']."','".$attribut['caption']."',-1,'POINT',2);"; } $sql_query=substr($sql_query,0,-2); if ($pk!="") $sql_query.=', CONSTRAINT "pk_'.$schemas[$tabelle['schemaid']]['caption'].'_'.$tabelle['caption'].'" PRIMARY KEY ('.substr($pk,0,-1).')'; $sql_query.=');'; //echo $sql_query.'
'.$sql_query2; if (!$res_attr=pg_query($conn,$sql_query.$sql_query2)) echo '
'.$schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption'].': '.pg_last_error($conn).'
'.$sql_query.'
'.$sql_query_nn.'
'; else echo 'Tabelle '.$schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption'].' wurde erfolgreich angelegt!
'; } else { // Attribute pruefen foreach ($tabelle['attribute'] AS $attribut) { //var_dump($attribut); $sql_query="SELECT nspname AS schemaname, relname AS tablename, pg_get_userbyid(relowner) AS tableowner, attname AS attribute FROM pg_catalog.pg_attribute JOIN pg_catalog.pg_class ON (attrelid=relfilenode) JOIN pg_namespace ON (oid=relnamespace) WHERE relkind='r' AND nspname='".$schemas[$tabelle['schemaid']]['caption']."' AND relname='".$tabelle['caption']."' AND attname='".$attribut['caption']."'; "; if (!$res_attr=pg_query($conn,$sql_query)) echo '
'.$attribut['caption'].': '.pg_last_error($conn).'
'; else if (pg_num_rows($res_attr)==1) echo $schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption'].'.'.$attribut['caption'].': OK - '; else if (pg_num_rows($res_attr)==0) { $sql_query_nn=''; echo $schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption'].'.'.$attribut['caption'].' ist nicht angelegt!
'; $sql_query='ALTER TABLE '.$schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption'].' ADD COLUMN '.$attribut['caption'].' '; $sql_query.=$datatypes[$attribut['datatypeid']]['caption']; if ($datatypes[$attribut['datatypeid']]['length']==1) $sql_query.='('.$attribut['datatypeparam1'].')'; if ($datatypes[$attribut['datatypeid']]['length']==2) $sql_query.='('.$attribut['datatypeparam1'].','.$attribut['datatypeparam2'].')'; if ($attribut['unique']) $sql_query.=' UNIQUE'; if ($attribut['defaultvalue']!="") $sql_query.=' DEFAULT '.$attribut['defaultvalue']; else $attribut['defaultvalue']=$datatypes[$attribut['datatypeid']]['default']; if ($attribut['checkconstraint']!="") $sql_query.=' CHECK ('.$attribut['checkconstraint'].')'; if ($attribut['notnull']) { $sql_query_nn.='UPDATE '.$schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption'].' SET '.$attribut['caption'].'='.$attribut['defaultvalue'].';'; $sql_query_nn.='ALTER TABLE '.$schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption'].' ALTER COLUMN '.$attribut['caption'].' SET NOT NULL;'; } $sql_query.=';'; //echo $sql_query; if (!$res_attr=pg_query($conn,$sql_query.$sql_query_nn)) echo '
'.$attribut['caption'].': '.pg_last_error($conn).'
'.$sql_query.'
'.$sql_query_nn.'
'; else echo $schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption'].'.'.$attribut['caption'].' wurde erfolgreich hinzugefuegt!
'; } } } } flush(); $i++; } echo '

Pruefe Constraints!

'; function getTablenameFromAttributIDs($attr) { global $tabellen; global $schemas; $attributid=null; foreach ($attr AS $attribut) $attributid=$attribut; foreach ($tabellen AS $tabelle) foreach ($tabelle['attribute'] AS $attribut) if ($attribut['id']==$attributid) return $schemas[$tabelle['schemaid']]['caption'].'.'.$tabelle['caption']; return false; } function getAttributesnameFromAttributIDs($attr) { global $tabellen; global $schemas; $attributes=''; foreach ($attr AS $attributid) foreach ($tabellen AS $tabelle) foreach ($tabelle['attribute'] AS $attribute) if ($attribute['id']==$attributid) $attributes.=$attribute['caption'].', '; return substr($attributes,0,-2); } //Alter table campus.tbl_paabgabe add Constraint projektarbeit_paabgabe foreign key (projektarbeit_id) references lehre.tbl_projektarbeit (projektarbeit_id) on update cascade on delete restrict; foreach ($relations AS $relation) { $sql_query=''; $pk=''; // Auf Foreign Key pruefen //var_dump($relation); if (count($relation['foreignkeys'])>0) { foreach ($relation['foreignkeys'] AS $foreignkey) { $parenttable=getTablenameFromAttributIDs($foreignkey['attrparent']); $childtable=getTablenameFromAttributIDs($foreignkey['attrchild']); $parentattr=getAttributesnameFromAttributIDs($foreignkey['attrparent']); $childattr=getAttributesnameFromAttributIDs($foreignkey['attrchild']); //$constrname=str_replace('.','_',); $sql_query='ALTER TABLE '.$childtable.' ADD CONSTRAINT '.$relation['caption'].' FOREIGN KEY ('.$childattr.') REFERENCES '.$parenttable.' ('.$parentattr.') '; $sql_query.='ON UPDATE CASCADE ON DELETE RESTRICT;'; //if (refintegritychildupdate) // $sql_query.=' echo $sql_query.'
'; } } flush(); $i++; } echo '

Gegenpruefung!

'; $sql_query="SELECT schemaname,tablename FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema' AND schemaname != 'sync' AND schemaname != 'papaya';"; if (!$result=@pg_query($conn,$sql_query)) echo '
'.pg_last_error($conn).'
'; else while ($row=pg_fetch_object($result)) { $fulltablename=$row->schemaname.'.'.$row->tablename; if (!isset($tabellen[$fulltablename])) echo 'Tabelle '.$fulltablename.' existiert in der DB, aber nicht in diesem Skript!
'; else if (!$result_fields=@pg_query($conn,"SELECT * FROM $fulltablename LIMIT 1;")) echo '
'.pg_last_error($conn).'
'; else for ($i=0; $i'.$fieldnameDB.' existiert in der DB, aber nicht in diesem Skript!
'; } } ?>