CSV export
git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@445 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
parent
7d09bd7e34
commit
7afa763715
157
dump.inc.php
157
dump.inc.php
|
@ -1,84 +1,44 @@
|
|||
<?php
|
||||
include "./export.inc.php";
|
||||
|
||||
function dump_table($table, $style) {
|
||||
global $mysql, $max_packet, $types;
|
||||
if ($style) {
|
||||
$result = $mysql->query("SHOW CREATE TABLE " . idf_escape($table));
|
||||
if ($result) {
|
||||
if ($style == "DROP, CREATE") {
|
||||
echo "DROP TABLE " . idf_escape($table) . ";\n";
|
||||
}
|
||||
$create = $mysql->result($result, 1);
|
||||
echo ($style == "CREATE, ALTER" ? preg_replace('~^CREATE TABLE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n\n";
|
||||
if ($max_packet < 1073741824) { // protocol limit
|
||||
$row_size = 21 + strlen(idf_escape($table));
|
||||
foreach (fields($table) as $field) {
|
||||
$type = $types[$field["type"]];
|
||||
$row_size += 5 + ($field["length"] ? $field["length"] : $type) * (preg_match('~char|text|enum~', $field["type"]) ? 3 : 1); // UTF-8 in MySQL uses up to 3 bytes
|
||||
if ($_POST["format"] == "csv") {
|
||||
dump_csv(array_keys(fields($table)));
|
||||
} else {
|
||||
$result = $mysql->query("SHOW CREATE TABLE " . idf_escape($table));
|
||||
if ($result) {
|
||||
if ($style == "DROP, CREATE") {
|
||||
echo "DROP TABLE " . idf_escape($table) . ";\n";
|
||||
}
|
||||
if ($row_size > $max_packet) {
|
||||
$max_packet = 1024 * ceil($row_size / 1024);
|
||||
echo "SET max_allowed_packet = $max_packet, GLOBAL max_allowed_packet = $max_packet;\n";
|
||||
}
|
||||
}
|
||||
$result->free();
|
||||
}
|
||||
if ($mysql->server_info >= 5) {
|
||||
$result = $mysql->query("SHOW TRIGGERS LIKE '" . $mysql->escape_string(addcslashes($table, "%_")) . "'");
|
||||
if ($result->num_rows) {
|
||||
echo "DELIMITER ;;\n\n";
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
echo "CREATE TRIGGER " . idf_escape($row["Trigger"]) . " $row[Timing] $row[Event] ON " . idf_escape($row["Table"]) . " FOR EACH ROW $row[Statement];;\n\n";
|
||||
}
|
||||
echo "DELIMITER ;\n\n";
|
||||
}
|
||||
$result->free();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function dump_data($table, $style) {
|
||||
global $mysql, $max_packet;
|
||||
if ($style) {
|
||||
if ($style == "TRUNCATE, INSERT") {
|
||||
echo "TRUNCATE " . idf_escape($table) . ";\n";
|
||||
}
|
||||
$result = $mysql->query("SELECT * FROM " . idf_escape($table)); //! enum and set as numbers, binary as _binary, microtime
|
||||
if ($result) {
|
||||
if ($style == "UPDATE") {
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
$set = array();
|
||||
foreach ($row as $key => $val) {
|
||||
$row[$key] = (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
||||
$set[] = idf_escape($key) . " = " . (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
||||
$create = $mysql->result($result, 1);
|
||||
echo ($style == "CREATE, ALTER" ? preg_replace('~^CREATE TABLE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n\n";
|
||||
if ($max_packet < 1073741824) { // protocol limit
|
||||
$row_size = 21 + strlen(idf_escape($table));
|
||||
foreach (fields($table) as $field) {
|
||||
$type = $types[$field["type"]];
|
||||
$row_size += 5 + ($field["length"] ? $field["length"] : $type) * (preg_match('~char|text|enum~', $field["type"]) ? 3 : 1); // UTF-8 in MySQL uses up to 3 bytes
|
||||
}
|
||||
echo "INSERT INTO " . idf_escape($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES (" . implode(", ", $row) . ") ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n";
|
||||
}
|
||||
} elseif ($result->num_rows) {
|
||||
$insert = "INSERT INTO " . idf_escape($table) . " VALUES ";
|
||||
$length = 0;
|
||||
while ($row = $result->fetch_row()) {
|
||||
foreach ($row as $key => $val) {
|
||||
$row[$key] = (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
||||
}
|
||||
$s = "(" . implode(", ", $row) . ")";
|
||||
if (!$length) {
|
||||
echo $insert, $s;
|
||||
$length = strlen($insert) + strlen($s);
|
||||
} else {
|
||||
$length += 2 + strlen($s);
|
||||
if ($length < $max_packet) {
|
||||
echo ", ", $s;
|
||||
} else {
|
||||
echo ";\n", $insert, $s;
|
||||
$length = strlen($insert) + strlen($s);
|
||||
}
|
||||
if ($row_size > $max_packet) {
|
||||
$max_packet = 1024 * ceil($row_size / 1024);
|
||||
echo "SET max_allowed_packet = $max_packet, GLOBAL max_allowed_packet = $max_packet;\n";
|
||||
}
|
||||
}
|
||||
echo ";\n";
|
||||
$result->free();
|
||||
}
|
||||
if ($mysql->server_info >= 5) {
|
||||
$result = $mysql->query("SHOW TRIGGERS LIKE '" . $mysql->escape_string(addcslashes($table, "%_")) . "'");
|
||||
if ($result->num_rows) {
|
||||
echo "DELIMITER ;;\n\n";
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
echo "CREATE TRIGGER " . idf_escape($row["Trigger"]) . " $row[Timing] $row[Event] ON " . idf_escape($row["Table"]) . " FOR EACH ROW $row[Statement];;\n\n";
|
||||
}
|
||||
echo "DELIMITER ;\n\n";
|
||||
}
|
||||
$result->free();
|
||||
}
|
||||
$result->free();
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,7 +63,7 @@ function dump_routines($db) {
|
|||
|
||||
function dump($db, $style) {
|
||||
global $mysql;
|
||||
if (in_array($style, array("DROP, CREATE", "CREATE", "CREATE, ALTER")) && ($result = $mysql->query("SHOW CREATE DATABASE " . idf_escape($db)))) {
|
||||
if ($_POST["format"] != "csv" && in_array($style, array("DROP, CREATE", "CREATE", "CREATE, ALTER")) && ($result = $mysql->query("SHOW CREATE DATABASE " . idf_escape($db)))) {
|
||||
if ($style == "DROP, CREATE") {
|
||||
echo "DROP DATABASE IF EXISTS " . idf_escape($db) . ";\n";
|
||||
}
|
||||
|
@ -112,26 +72,33 @@ function dump($db, $style) {
|
|||
$result->free();
|
||||
}
|
||||
if ($style) {
|
||||
echo "USE " . idf_escape($db) . ";\n";
|
||||
echo ($_POST["format"] != "csv" ? "USE " . idf_escape($db) . ";\n" : "");
|
||||
if (!strlen($_GET["db"])) {
|
||||
$views = array();
|
||||
$result = $mysql->query("SHOW TABLE STATUS");
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
if (isset($row["Engine"])) {
|
||||
if ($_POST["format"] == "csv") {
|
||||
ob_start();
|
||||
}
|
||||
dump_table($row["Name"], $_POST["tables"][0]);
|
||||
dump_data($row["Name"], $_POST["data"][0]);
|
||||
if ($_POST["format"] == "csv") {
|
||||
echo tar_file("$db/$row[Name].csv", ob_get_clean());
|
||||
}
|
||||
} else {
|
||||
$views[] = $row["Name"];
|
||||
}
|
||||
}
|
||||
$result->free();
|
||||
foreach ($views as $view) {
|
||||
dump_table($view, $_POST["tables"][0]);
|
||||
if ($_POST["format"] != "csv") {
|
||||
foreach ($views as $view) {
|
||||
dump_table($view, $_POST["tables"][0]);
|
||||
}
|
||||
dump_routines($db);
|
||||
}
|
||||
dump_routines($db);
|
||||
}
|
||||
}
|
||||
echo "\n\n";
|
||||
}
|
||||
|
||||
function tar_file($filename, $contents) {
|
||||
|
@ -141,26 +108,22 @@ function tar_file($filename, $contents) {
|
|||
$checksum += ord($return{$i});
|
||||
}
|
||||
$return .= sprintf("%06o", $checksum) . "\0 ";
|
||||
$return .= str_repeat("\0", 512 - strlen($return));
|
||||
$return .= $contents;
|
||||
if (strlen($contents) % 512) {
|
||||
$return .= str_repeat("\0", 512 - strlen($contents) % 512);
|
||||
}
|
||||
return $return;
|
||||
return $return . str_repeat("\0", 512 - strlen($return)) . $contents . str_repeat("\0", 511 - (strlen($contents) + 511) % 512);
|
||||
}
|
||||
|
||||
if ($_POST) {
|
||||
header("Content-Type: text/plain; charset=utf-8");
|
||||
$filename = (strlen($_GET["db"]) ? preg_replace('~[^a-z0-9_]~i', '-', (strlen($_GET["dump"]) ? $_GET["dump"] : $_GET["db"])) : "dump");
|
||||
$filename .= ($_POST["format"] == "sql" ? ".sql" : (!strlen($_GET["db"]) || count(array_filter($_POST["data"])) >= 1 ? ".tar" : ".csv"));
|
||||
header("Content-Disposition: " . ($_POST["output"] == "file" ? "attachment" : "inline") . "; filename=$filename");
|
||||
|
||||
$max_packet = 16777216;
|
||||
echo "SET NAMES utf8;\n";
|
||||
echo "SET foreign_key_checks = 0;\n";
|
||||
echo "SET time_zone = '" . $mysql->escape_string($mysql->result($mysql->query("SELECT @@time_zone"))) . "';\n";
|
||||
echo "SET max_allowed_packet = $max_packet, GLOBAL max_allowed_packet = $max_packet;\n";
|
||||
echo "\n";
|
||||
$ext = ($_POST["format"] == "sql" ? "sql" : (!strlen($_GET["db"]) || count(array_filter($_POST["tables"]) + array_filter($_POST["data"])) > 1 ? "tar" : "csv"));
|
||||
header("Content-Type: " . ($ext == "tar" ? "application/x-tar" : ($ext == "sql" || $_POST["output"] != "file" ? "text/plain" : "text/csv")) . "; charset=utf-8");
|
||||
header("Content-Disposition: " . ($_POST["output"] == "file" ? "attachment" : "inline") . "; filename=$filename.$ext");
|
||||
if ($_POST["format"] != "csv") {
|
||||
$max_packet = 16777216;
|
||||
echo "SET NAMES utf8;\n";
|
||||
echo "SET foreign_key_checks = 0;\n";
|
||||
echo "SET time_zone = '" . $mysql->escape_string($mysql->result($mysql->query("SELECT @@time_zone"))) . "';\n";
|
||||
echo "SET max_allowed_packet = $max_packet, GLOBAL max_allowed_packet = $max_packet;\n";
|
||||
echo "\n";
|
||||
}
|
||||
|
||||
foreach ($_POST["databases"] as $db => $style) {
|
||||
$db = bracket_escape($db, "back");
|
||||
|
@ -171,8 +134,14 @@ if ($_POST) {
|
|||
if (strlen($_GET["db"])) {
|
||||
foreach ($_POST["tables"] as $key => $style) {
|
||||
$table = bracket_escape($key, "back");
|
||||
if ($ext == "tar") {
|
||||
ob_start();
|
||||
}
|
||||
dump_table($table, $style);
|
||||
dump_data($table, $_POST["data"][$key]);
|
||||
if ($ext == "tar") {
|
||||
echo tar_file("$table.csv", ob_get_clean());
|
||||
}
|
||||
}
|
||||
dump_routines($_GET["db"]);
|
||||
}
|
||||
|
@ -195,7 +164,7 @@ function check(td, name, value) {
|
|||
|
||||
<form action="" method="post">
|
||||
<p>
|
||||
<?php echo lang('Output'); ?>: <select name="output"><option value="text"><?php echo lang('text'); ?></option><option value="file"><?php echo lang('file'); ?></option></select>
|
||||
<?php echo lang('Output'); ?>: <select name="output"><option value="text"><?php echo lang('open'); ?></option><option value="file"><?php echo lang('save'); ?></option></select>
|
||||
<?php echo lang('Format'); ?>: <select name="format"><option value="sql"><?php echo lang('SQL'); ?></option><option value="csv"><?php echo lang('CSV'); ?></option></select>
|
||||
</p>
|
||||
|
||||
|
|
57
export.inc.php
Normal file
57
export.inc.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
function dump_csv($row) {
|
||||
foreach ($row as $key => $val) {
|
||||
if (preg_match("~[\"\n,]~", $val)) {
|
||||
$row[$key] = '"' . str_replace('"', '""', $val) . '"';
|
||||
}
|
||||
}
|
||||
echo implode(",", $row) . "\n";
|
||||
}
|
||||
|
||||
function dump_data($table, $style) {
|
||||
global $mysql, $max_packet;
|
||||
if ($style) {
|
||||
if ($_POST["format"] != "csv" && $style == "TRUNCATE, INSERT") {
|
||||
echo "TRUNCATE " . idf_escape($table) . ";\n";
|
||||
}
|
||||
$result = $mysql->query("SELECT * FROM " . idf_escape($table)); //! enum and set as numbers, binary as _binary, microtime
|
||||
if ($result) {
|
||||
$insert = "INSERT INTO " . idf_escape($table) . " VALUES ";
|
||||
$length = 0;
|
||||
while ($row = $result->fetch_assoc()) {
|
||||
if ($_POST["format"] == "csv") {
|
||||
dump_csv($row);
|
||||
} elseif ($style == "UPDATE") {
|
||||
$set = array();
|
||||
foreach ($row as $key => $val) {
|
||||
$row[$key] = (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
||||
$set[] = idf_escape($key) . " = " . (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
||||
}
|
||||
echo "INSERT INTO " . idf_escape($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES (" . implode(", ", $row) . ") ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n";
|
||||
} else {
|
||||
foreach ($row as $key => $val) {
|
||||
$row[$key] = (isset($val) ? "'" . $mysql->escape_string($val) . "'" : "NULL");
|
||||
}
|
||||
$s = "(" . implode(", ", $row) . ")";
|
||||
if (!$length) {
|
||||
echo $insert, $s;
|
||||
$length = strlen($insert) + strlen($s);
|
||||
} else {
|
||||
$length += 2 + strlen($s);
|
||||
if ($length < $max_packet) {
|
||||
echo ", ", $s;
|
||||
} else {
|
||||
echo ";\n", $insert, $s;
|
||||
$length = strlen($insert) + strlen($s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($_POST["format"] != "csv" && $style != "UPDATE" && $result->num_rows) {
|
||||
echo ";\n";
|
||||
}
|
||||
$result->free();
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
}
|
|
@ -159,4 +159,17 @@ $translations = array(
|
|||
'Logged as: %s' => 'Přihlášen jako: %s',
|
||||
'Move up' => 'Přesunout nahoru',
|
||||
'Move down' => 'Přesunout dolů',
|
||||
'Functions' => 'Funkce',
|
||||
'Aggregation' => 'Agregace',
|
||||
'Export' => 'Export',
|
||||
'Output' => 'Výstup',
|
||||
'open' => 'otevřít',
|
||||
'save' => 'uložit',
|
||||
'Format' => 'Formát',
|
||||
'SQL' => 'SQL',
|
||||
'CSV' => 'CSV',
|
||||
'skip' => 'přeskočit',
|
||||
'Tables' => 'Tabulky',
|
||||
'Structure' => 'Struktura',
|
||||
'Data' => 'Data',
|
||||
);
|
||||
|
|
6
todo.txt
6
todo.txt
|
@ -6,8 +6,10 @@ Rename table to other database
|
|||
Clone row
|
||||
Execution time in sql.inc.php
|
||||
Input function results in edit
|
||||
Choose tables and data to export
|
||||
? CSV export
|
||||
Bulk update - leave original, set to value, set to NULL
|
||||
Save uploaded files after error to session variable instead of hidden field
|
||||
Export transactions
|
||||
Export compress
|
||||
? Query print
|
||||
? Access without login - accept $_GET
|
||||
? Save token also to cookie - for session expiration and login in other window
|
||||
|
|
Loading…
Reference in a new issue