<?php

session_start();
if($_SESSION['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("function.inc.php");
include("Functions_Trans.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");

$schema1 = "siegwerk_acc2016";
$schema2 = "siegwerk_acc2017";



$sql = "CREATE DATABASE `$schema2`;";
if (mysql_query($sql, $connection)) {
    echo "Database my_db created successfully\n";


mysql_select_db($schema2,$connection) or die("Could not find DB");
$query = "SHOW TABLES FROM $schema1";
$res = mysql_query($query,$connection);
while($rw = mysql_fetch_row($res)){
	
	$query2 = "SHOW CREATE TABLE $schema1.$rw[0]";
	$res2 = mysql_query($query2,$connection);
	$row2 = mysql_fetch_row($res2);
	$stt = $row2[1];
	
	$res3 = mysql_query($stt,$connection);
	if(!$res3) echo "Error creating $query2: " . mysql_error() . "<br><br>";
}

} else {
    echo 'Error creating database: ' . mysql_error() . "\n";
}

echo "<br>--------------------------------Functions/Procedures--------------------------------------------<br>";
mysql_select_db($schema2,$connection) or die("Could not find DB");

$query = "SHOW PROCEDURE STATUS";
$res = mysql_query($query,$connection);
while($rw = mysql_fetch_row($res)){
	
	IF($rw[0] == $schema1){
		$query2 = "show create procedure $schema1.$rw[1]";
		echo $query2."<br>";
		$res2 = mysql_query($query2,$connection);
		$row2 = mysql_fetch_row($res2);
		$stt = $row2[2];
		
		$res3 = mysql_query($stt,$connection);
		if(!$res3) echo "Error creating $query2: " . mysql_error() . "<br><br>";
	}
}

$query = "SHOW FUNCTION STATUS";
$res = mysql_query($query,$connection);
while($rw = mysql_fetch_row($res)){
	
	IF($rw[0] == $schema1){
		$query2 = "show create function $schema1.$rw[1]";
		echo $query2."<br>";
		$res2 = mysql_query($query2,$connection);
		$row2 = mysql_fetch_row($res2);
		$stt = $row2[2];
		
		$res3 = mysql_query($stt,$connection);
		if(!$res3) echo "Error creating $query2: " . mysql_error() . "<br><br>";
	}
}

echo "<br>--------------------------------triggers--------------------------------------------<br>";
mysql_select_db($schema2,$connection) or die("Could not find DB");
$query = "show triggers from $schema1";
$res = mysql_query($query,$connection);
while($rw = mysql_fetch_row($res)){
	
	
		$query2 = "show create trigger $schema1.$rw[0]";
		
		$res2 = mysql_query($query2,$connection);
		$row2 = mysql_fetch_row($res2);
		$stt = $row2[2];
		
		$res3 = mysql_query($stt,$connection);
		if(!$res3) echo "Error creating $query2: " . mysql_error() . "<br><br>";
}
?>