, * Andreas Oesterreicher , * Rudolf Hangl and * Gerald Simane-Sequens */ /* * Script zur Pruefung der Datenbank * * database.inc.php enthaelt die Struktur der Datenbank. Diese wird mit der Produktivdatenbank * verglichen und eventuelle Aenderungen werden angezeigt. */ require_once('../config/system.config.inc.php'); require_once('database.inc.php'); require_once('../include/functions.inc.php'); require_once('../include/benutzerberechtigung.class.php'); // Datenbank Verbindung if (!$conn = pg_pconnect('host='.DB_HOST.' port='.DB_PORT.' dbname='.DB_NAME.' user='.DB_USER.' password='.DB_PASSWORD)) die('Es konnte keine Verbindung zum Server aufgebaut werden!'.pg_last_error($conn)); $uid=get_uid(); $rechte = new benutzerberechtigung(); $rechte->getBerechtigungen($uid); if(!$rechte->isBerechtigt('admin')) die('Sie haben keine Berechtigung für diese Seite'); echo ' Datenbank Check '; echo '

Datenbank Prüfung

'; if(isset($_POST['commit'])) { if(isset($_POST['qry'])) { if(!pg_query($conn, $_POST['qry'])) echo pg_last_error($conn); } } $obj=array(); $obj['']=array(); $obj['']['error']=false; //Schema pruefen foreach ($schemas as $schema) { $obj[$schema['name']]=array(); $obj[$schema['name']]['error']=false; $qry = "SELECT nspname FROM pg_namespace WHERE nspname='".$schema['name']."'"; if($result = pg_query($conn, $qry)) { if(!pg_num_rows($result)>0) { $obj[$schema['name']]['qry']='CREATE SCHEMA '.$schema['name'].';'; $obj[$schema['name']]['error']=true; } } } //var_dump($datatypes); $tabs=array_keys($tabellen); //print_r($tabs); $i=0; foreach ($tabellen AS $tabelle) { $obj[$schemas[$tabelle['schemaid']]['name']]['tables'][$tabelle['name']]=array(); $sql_query2=''; $pk=''; // Tabelle pruefen //var_dump($tabelle); $sql_query="SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname='".$schemas[$tabelle['schemaid']]['name']."' AND tablename='".$tabelle['name']."';"; 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']]['name'].'.'.$tabelle['name']." ("; foreach ($tabelle['attribute'] AS $attribut) { if ($datatypes[$attribut['datatypeid']]['name']!='geometry') { //echo $datatypes[$attribut['datatypeid']]['name']; $sql_query.= $attribut['name'].' '; if ($attribut['pk']) $pk.=$attribut['name'].','; $sql_query.=$datatypes[$attribut['datatypeid']]['name']; 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['name']."','".$attribut['name']."',-1,'POINT',2);"; } $sql_query=substr($sql_query,0,-2); if ($pk!="") $sql_query.=', CONSTRAINT "pk_'.$schemas[$tabelle['schemaid']]['name'].'_'.$tabelle['name'].'" 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']]['name'].'.'.$tabelle['name'].': '.pg_last_error($conn).'
'.$sql_query.'
'.$sql_query_nn.'
'; //else // echo 'Tabelle '.$schemas[$tabelle['schemaid']]['name'].'.'.$tabelle['name'].' wurde erfolgreich angelegt!
'; $obj[$schemas[$tabelle['schemaid']]['name']]['tables'][$tabelle['name']]['qry']=$sql_query; $obj[$schemas[$tabelle['schemaid']]['name']]['error']=true; } else { // Attribute pruefen foreach ($tabelle['attribute'] AS $attribut) { $obj[$schemas[$tabelle['schemaid']]['name']]['tables'][$tabelle['name']]['attribute'][$attribut['name']]=array(); //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']]['name']."' AND relname='".$tabelle['name']."' AND attname='".$attribut['name']."'; "; if (!$res_attr=pg_query($conn,$sql_query)) { //echo '
'.$attribut['name'].': '.pg_last_error($conn).'
'; } else { if (pg_num_rows($res_attr)==1) { //echo $schemas[$tabelle['schemaid']]['name'].'.'.$tabelle['name'].'.'.$attribut['name'].': OK - '; } else if (pg_num_rows($res_attr)==0) { $sql_query_nn=''; //echo $schemas[$tabelle['schemaid']]['name'].'.'.$tabelle['name'].'.'.$attribut['name'].' ist nicht angelegt!
'; $sql_query='ALTER TABLE '.$schemas[$tabelle['schemaid']]['name'].'.'.$tabelle['name'].' ADD COLUMN '.$attribut['name'].' '; $sql_query.=$datatypes[$attribut['datatypeid']]['name']; 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']]['name'].'.'.$tabelle['name'].' SET '.$attribut['name'].'='.$attribut['defaultvalue'].';'; $sql_query_nn.='ALTER TABLE '.$schemas[$tabelle['schemaid']]['name'].'.'.$tabelle['name'].' ALTER COLUMN '.$attribut['name'].' SET NOT NULL;'; } $sql_query.=';'; //echo $sql_query; //if (!$res_attr=pg_query($conn,$sql_query.$sql_query_nn)) // echo '
'.$attribut['name'].': '.pg_last_error($conn).'
'.$sql_query.'
'.$sql_query_nn.'
'; //else // echo $schemas[$tabelle['schemaid']]['name'].'.'.$tabelle['name'].'.'.$attribut['name'].' wurde erfolgreich hinzugefuegt!
'; $obj[$schemas[$tabelle['schemaid']]['name']]['tables'][$tabelle['name']]['attribute'][$attribut['name']]['qry']=$sql_query; $obj[$schemas[$tabelle['schemaid']]['name']]['error']=true; $obj[$schemas[$tabelle['schemaid']]['name']]['tables'][$tabelle['name']]['error']=true; } $obj[$schemas[$tabelle['schemaid']]['name']]['tables'][$tabelle['name']]['attribute'][$attribut['name']]['datatype']=$datatypes[$attribut['datatypeid']]['name']; $obj[$schemas[$tabelle['schemaid']]['name']]['tables'][$tabelle['name']]['attribute'][$attribut['name']]['attribute']=$attribut; } } } } 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']]['name'].'.'.$tabelle['name']; 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['name'].', '; 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) { $sql_query=''; $parenttable=getTablenameFromAttributIDs($foreignkey['attrparent']); $childtable=getTablenameFromAttributIDs($foreignkey['attrchild']); $parentattr=getAttributesnameFromAttributIDs($foreignkey['attrparent']); $childattr=getAttributesnameFromAttributIDs($foreignkey['attrchild']); //$constrname=str_replace('.','_',); list($schema, $tablename) = explode(".", $childtable); $qry = "SELECT * FROM pg_constraint WHERE contype='f' AND conrelid=( SELECT oid FROM pg_class WHERE relname='".$tablename."' AND relnamespace=( SELECT oid FROM pg_namespace WHERE nspname='".$schema."')) AND conname='".$relation['name']."'"; if($result = pg_query($conn, $qry)) { if(pg_num_rows($result)==0) { $sql_query='ALTER TABLE '.$childtable.' ADD CONSTRAINT '.$relation['name'].' FOREIGN KEY ('.$childattr.') REFERENCES '.$parenttable.' ('.$parentattr.') '; $sql_query.='ON UPDATE CASCADE ON DELETE RESTRICT;'; if(isset($obj[$schema]) && isset($obj[$schema]['tables'][$tablename]) && isset($obj[$schema]['tables'][$tablename]['attribute']) && isset($obj[$schema]['tables'][$tablename]['attribute'][$childattr]['qry'])) $obj[$schema]['tables'][$tablename]['attribute'][$childattr]['qry'].=$sql_query; else $obj[$schema]['tables'][$tablename]['attribute'][$childattr]['qry']=$sql_query; $obj[$schema]['error']=true; $obj[$schema]['tables'][$tablename]['error']=true; } } //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!
'; } } */ $out_schema="\n"; $out_schema_data="\n"; $out_tbl="\n"; $out_tbl_data="\n"; $out_att="\n"; $out_att_data="\n"; function querybox($title, $qry, $id) { $ret="\n"; $ret.=''; return $ret; } $out_schema.= '
'; $out_schema.= '
Schema
'; $out_schema.= ''; $gesamtqry = ''; foreach ($obj as $schema=>$value) { //Schema $out_schema.= "\n"; $out_schema.= ''; if($value['error']) { if(isset($value['qry']) && $value['qry']!='') { $gesamtqry .= $value['qry']."\n"; $out_schema_data.=querybox($schema, $value['qry'], 'schema.'.$schema); $img = "exclamation.png"; } else { $img = "error_go.png"; } $out_schema.= ''; $out_schema.= ''; } else { $out_schema.= ''; $out_schema.= ''; } $out_schema.= ''; //Tabelle if(isset($value['tables'])) { $out_tbl.= "\n"; $out_tbl.= '
'.$schema.''.$schema.'
'; foreach ($value['tables'] as $tables=>$tabvalue) { $out_tbl.= ''; if(isset($tabvalue['qry']) && $tabvalue['qry']!='') { $gesamtqry .= $tabvalue['qry']."\n"; $out_tbl.= ''; $out_tbl.= ''; $out_tbl_data.=querybox($tables, $tabvalue['qry'], 'table.'.$schema.$tables); } else { if(isset($tabvalue['error']) && $tabvalue['error']) { $out_tbl.= ''; $out_tbl.= ''; } else { $out_tbl.= ''; $out_tbl.= ''; } } $out_tbl.= ''; //Attribute if(isset($tabvalue['attribute'])) { $out_att.= "\n"; $out_att.= '
'.$tables.''.$tables.''.$tables.'
'; foreach ($tabvalue['attribute'] as $attrib=>$attvalue) { $out_att.= ''; if(isset($attvalue['qry']) && $attvalue['qry']!='') { $gesamtqry .= $attvalue['qry']."\n"; $out_att.= ''; $out_att.= ''; $out_att_data.=querybox($attrib, $attvalue['qry'], 'attrib.'.$schema.$tables.$attrib); } else { $out_att.= ''; $out_att.= ''; $out_att.= ''; $out_att.= ''; } $out_att.= ''; } $out_att.= '
'.$attrib.''.$attrib.' '.$attvalue['datatype'].($attvalue['attribute']['length']!=''?' ('.$attvalue['attribute']['length'].')':'').' '. ($attvalue['attribute']['unique']=='1'?'U':''). ($attvalue['attribute']['notnull']=='1'?'NN':''). '
'; $out_att.= '
'; } } $out_tbl.= ''; $out_tbl.= ''; } } $out_schema.= ''; $out_schema.= ''; echo 'Gesamtsystem aktualisieren
'; echo $out_schema; echo querybox('Gesamtsystem aktualisieren',$gesamtqry, 'schema.gesamtqry'); echo $out_schema_data; echo $out_tbl; echo $out_tbl_data; echo $out_att; echo $out_att_data; ?>