CSV Byte-order-mark

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@448 7c3ca157-0c34-0410-bff1-cbf682f78f5c
This commit is contained in:
jakubvrana 2008-08-18 22:40:45 +00:00
parent 9890b6c22d
commit f48161b43e
3 changed files with 51 additions and 49 deletions

View file

@ -1,50 +1,9 @@
<?php
include "./export.inc.php";
function dump_table($table, $style) {
global $mysql, $max_packet, $types;
if ($style) {
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";
}
$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 ($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_routines($db) {
global $mysql;
if ($mysql->server_info >= 5) {
if ($_POST["format"] != "csv" && $mysql->server_info >= 5) {
$out = "";
foreach (array("FUNCTION", "PROCEDURE") as $routine) {
$result = $mysql->query("SHOW $routine STATUS WHERE Db = '" . $mysql->escape_string($db) . "'");
@ -87,18 +46,16 @@ function dump($db, $style) {
if ($_POST["format"] == "csv") {
echo tar_file("$db/$row[Name].csv", ob_get_clean());
}
} else {
} elseif ($_POST["format"] != "csv") {
$views[] = $row["Name"];
}
}
$result->free();
}
if ($_POST["format"] != "csv") {
foreach ($views as $view) {
dump_table($view, $_POST["tables"][0]);
}
dump_routines($db);
foreach ($views as $view) {
dump_table($view, $_POST["tables"][0]);
}
dump_routines($db);
}
}
}

View file

@ -8,6 +8,48 @@ function dump_csv($row) {
echo implode(",", $row) . "\n";
}
function dump_table($table, $style) {
global $mysql, $max_packet, $types;
if ($_POST["format"] == "csv") {
echo "\xef\xbb\xbf";
if ($style) {
dump_csv(array_keys(fields($table)));
}
} elseif ($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 ($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, $from = "") {
global $mysql, $max_packet;
if ($style) {
@ -52,7 +94,9 @@ function dump_data($table, $style, $from = "") {
}
$result->free();
}
echo "\n";
if ($_POST["format"] != "csv") {
echo "\n";
}
}
}

View file

@ -65,6 +65,7 @@ if ($_POST && !$error) {
$deleted = 0;
if ($_POST["export"] || $_POST["export_result"]) {
dump_headers($_GET["select"]);
dump_table($_GET["select"], "");
}
if (isset($_POST["truncate"])) {
$result = $mysql->query($where ? "DELETE FROM " . idf_escape($_GET["select"]) . " WHERE " . implode(" AND ", $where) : "TRUNCATE " . idf_escape($_GET["select"]));