/************************************************************\
*
* db2SQL Copyright 2005 Howard Yeend
* www.puremango.co.uk
*
* This file is part of db2SQL.
*
* db2SQL 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 2 of the License, or
* (at your option) any later version.
*
* db2SQL 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 db2SQL; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
\************************************************************/
define("DB2SQL_VERSION","1.04",false);
function dump_data($action,$host,$port,$user,$pass,$db,$tables,$get_structure=true,$get_data=true)
{
// host:port format supported since PHP 3.0B4
$host = $host.":".$port;
$cn = @mysql_connect($host,$user,$pass);
if(!$cn)
{
// no mySQL server on that host/port
return mysql_error();
}
if($action=="testconn")
{
// user just wanted to get this far
return "Connection OK";
}
if($action=="listdbs")
{
// user wants list of databases (presumably to choose one to backup)
$db_list_sql = "SHOW DATABASES";
$db_list_res = @mysql_query($db_list_sql,$cn);
if(@mysql_num_rows($db_list_res)==0)
{
// maybe this user has no access
// or maybe there really are no databases...
return "No databases found";
}
$db_list = "";
while($db_list_row = @mysql_fetch_row($db_list_res))
{
$db_list .= " [".$db_list_row[0]."]";
}
return "Databases found:".$db_list;
} else if($action=="getsql"){
// user wants to generate SQL.
if(!@mysql_select_db($db))
{
// no such DB or user has no access to that DB
return mysql_error();
}
// ok, let us begin...
$generated_sql = "# db2SQL v".DB2SQL_VERSION." - www.puremango.co.uk\n";
$generated_sql .= "#---------------------------------------------\n";
$generated_sql .= "# Dump of Host: [".$host."]\n";
$generated_sql .= "# DB: [".$db."]\n";
$generated_sql .= "# Generated: ".date("F jS, Y, g:i:s a")."\n";
$generated_sql .= "#---------------------------------------------\n";
$tables = (!empty($tables)) ? $tables : Array();
if(empty($tables))
{
// no tables sent - get all
$sql = "SHOW TABLES";
$tab_res = mysql_query($sql,$cn);
while($row = @mysql_fetch_row($tab_res))
{
$tables[] = $row[0];
}
} else {
// user specified tables to get - use them.
$tables = explode(",",$tables);
// if the tables they specify don't exist, then that's their problem.
}
if(sizeof($tables)==0)
{
// braindead users again.
return "No tables in database";
}
// do what the user wants, for each table in specified database
foreach($tables as $table_name)
{
// blank out some values
$keys = Array();
$last_key_name = "";
// get structure?
if($get_structure)
{
$generated_sql .= "\n#\n# Structure of table [".$table_name."]\n#\n";
$generated_sql .= "CREATE TABLE IF NOT EXISTS ".$table_name." (\n";
// get fields
$tab_struct_sql = "SHOW FIELDS FROM ".$table_name;
$tab_struct_res = @mysql_query($tab_struct_sql,$cn);
$num_fields = @mysql_num_rows($tab_struct_res);
// get keys
$tab_key_sql = "SHOW KEYS FROM ".$table_name;
$tab_key_res = @mysql_query($tab_key_sql,$cn);
$num_keys = @mysql_num_rows($tab_key_res);
// add fields to SQL
for($i=0 ; ($tab_struct_row = @mysql_fetch_assoc($tab_struct_res)) ; $i++)
{
// work out 'optional extras'
$default_str = (!is_null($tab_struct_row['Default'])) ? " DEFAULT '".$tab_struct_row['Default']."'" : '';
$null_str = ($tab_struct_row['Null']!="YES") ? " NOT NULL" : '';
$extra_str = (!empty($tab_struct_row['Extra'])) ? ' '.$tab_struct_row['Extra'] : '';
// add to SQL
$generated_sql .= " ".$tab_struct_row['Field']." ".$tab_struct_row['Type'].$null_str.$default_str.$extra_str;
// if this is the last field AND there are no keys, don't add a comma
if($i+1<$num_fields || $num_keys>0)
{
// otherwise add comma
$generated_sql .= " ,";
}
$generated_sql .= "\n";
}
// add keys to SQL
// this is a pain (so many ways of doing it (multiple fields -argh!))
// hence this part is a bit hacky
for($i=0 ; ($tab_key_row = @mysql_fetch_assoc($tab_key_res)) ; $i++)
{
// if we're on a new (unprocessed) key
if($last_key_name!=$tab_key_row['Key_name'])
{
if(!empty($last_key_name))
{
// there was a key before this one
// so finish it's SQL before starting this one's
// chop final comma
$generated_sql = substr($generated_sql,0,strlen($generated_sql)-1);
// new line, etc
$generated_sql .= "),\n";
}
// what sort of key is it?
if($tab_key_row['Key_name']=="PRIMARY")
{
$key_prefix = " PRIMARY KEY";
} else if($tab_key_row['Non_unique']!="1") {
$key_prefix = " UNIQUE KEY ".$tab_key_row['Key_name'];
} else {
$key_prefix = " KEY ".$tab_key_row['Key_name'];
}
// add to SQL
$generated_sql .= $key_prefix." (";
}
// add fields to key
$generated_sql .= $tab_key_row['Column_name'].",";
// save key name
$last_key_name = $tab_key_row['Key_name'];
// if the next key name is the same, we continue adding fields to it
// otherwise, we start a new line for the next key
}
// and then finish up keys
if(!empty($last_key_name))
{
// chop final comma
$generated_sql = substr($generated_sql,0,strlen($generated_sql)-1);
// terminate key
$generated_sql .= ")\n";
}
// end table structure SQL
$generated_sql .= ");\n";
}
// get data?
if($get_data)
{
$generated_sql .= "\n#\n# Data for [".$table_name."]\n#\n";
// grab all data
$data_sql = "SELECT * FROM ".$table_name;
$data_res = @mysql_query($data_sql,$cn);
if(mysql_num_rows($data_res)==0)
{
// nice and easy
$generated_sql .= "# (no data in [".$table_name."])\n";
}
while($data_row = @mysql_fetch_array($data_res,MYSQL_NUM))
{
$generated_sql .= "INSERT INTO ".$table_name." VALUES(";
for($i=0 ; $i0)
{
$dump_result = dump_data($_POST['action'],$_POST['host'],$_POST['port'],$_POST['user'],$_POST['pass'],$_POST['db'],$_POST['tables'],$_POST['structure'],$_POST['data']);
if($dump_result===true)
{
// all groovy.
exit();
} // otherwise, $dump_result is a string telling us what's gone horribly wrong.
}
?>
db2SQL v=DB2SQL_VERSION?> - www.puremango.co.uk
// setup default values
if(empty($_POST['port']))
{
$_POST['port'] = 3306;
}
if(empty($_POST['host']))
{
$_POST['host'] = "localhost";
}
?>