Permalink
Browse files

Use output buffering for dump compression

git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1161 7c3ca157-0c34-0410-bff1-cbf682f78f5c
  • Loading branch information...
1 parent e40612a commit 89a60e77b2270d0aa76f0fd7245258c3c68d18d9 jakubvrana committed Oct 2, 2009
Showing with 41 additions and 58 deletions.
  1. +16 −17 adminer/dump.inc.php
  2. +23 −17 adminer/include/export.inc.php
  3. +1 −22 adminer/include/functions.inc.php
  4. +0 −1 adminer/select.inc.php
  5. +1 −1 editor/include/export.inc.php
View
33 adminer/dump.inc.php
@@ -4,28 +4,28 @@
if ($_POST) {
$ext = dump_headers((strlen($TABLE) ? $TABLE : DB), (!strlen(DB) || count((array) $_POST["tables"] + (array) $_POST["data"]) > 1));
if ($_POST["format"] == "sql") {
- dump("-- Adminer $VERSION dump
+ echo "-- Adminer $VERSION dump
SET NAMES utf8;
SET foreign_key_checks = 0;
SET time_zone = " . $connection->quote($connection->result($connection->query("SELECT @@time_zone"))) . ";
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
-");
+";
}
$style = $_POST["db_style"];
foreach ((strlen(DB) ? array(DB) : (array) $_POST["databases"]) as $db) {
if ($connection->select_db($db)) {
if ($_POST["format"] == "sql" && ereg('CREATE', $style) && ($result = $connection->query("SHOW CREATE DATABASE " . idf_escape($db)))) {
if ($style == "DROP+CREATE") {
- dump("DROP DATABASE IF EXISTS " . idf_escape($db) . ";\n");
+ echo "DROP DATABASE IF EXISTS " . idf_escape($db) . ";\n";
}
$create = $connection->result($result, 1);
- dump(($style == "CREATE+ALTER" ? preg_replace('~^CREATE DATABASE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n");
+ echo ($style == "CREATE+ALTER" ? preg_replace('~^CREATE DATABASE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n";
}
if ($_POST["format"] == "sql") {
if ($style) {
- dump("USE " . idf_escape($db) . ";\n" . ($style == "CREATE+ALTER" ? "SET @adminer_alter = '';\n" : "") . "\n");
+ echo "USE " . idf_escape($db) . ";\n" . ($style == "CREATE+ALTER" ? "SET @adminer_alter = '';\n" : "") . "\n";
}
$out = "";
if ($_POST["routines"]) {
@@ -45,7 +45,7 @@
}
}
if ($out) {
- dump("DELIMITER ;;\n\n$out" . "DELIMITER ;\n\n");
+ echo "DELIMITER ;;\n\n$out" . "DELIMITER ;\n\n";
}
}
@@ -67,9 +67,9 @@
dump_triggers($row["Name"], $_POST["table_style"]);
}
if ($ext == "tar") {
- dump(tar_file((strlen(DB) ? "" : "$db/") . "$row[Name].csv", ob_get_clean()));
+ echo tar_file((strlen(DB) ? "" : "$db/") . "$row[Name].csv", ob_get_clean());
} elseif ($_POST["format"] == "sql") {
- dump("\n");
+ echo "\n";
}
} elseif ($_POST["format"] == "sql") {
$views[] = $row["Name"];
@@ -80,14 +80,14 @@
dump_table($view, $_POST["table_style"], true);
}
if ($ext == "tar") {
- dump(pack("x512"));
+ echo pack("x512");
}
}
if ($style == "CREATE+ALTER" && $_POST["format"] == "sql") {
// drop old tables
$query = "SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE()";
- dump("DELIMITER ;;
+ echo "DELIMITER ;;
CREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN
DECLARE _table_name, _engine, _table_collation varchar(64);
DECLARE _table_comment varchar(64);
@@ -98,17 +98,17 @@
REPEAT
FETCH tables INTO _table_name, _engine, _table_collation, _table_comment;
IF NOT done THEN
- CASE _table_name");
+ CASE _table_name";
$result = $connection->query($query);
while ($row = $result->fetch_assoc()) {
$comment = $connection->quote($row["ENGINE"] == "InnoDB" ? preg_replace('~(?:(.+); )?InnoDB free: .*~', '\\1', $row["TABLE_COMMENT"]) : $row["TABLE_COMMENT"]);
- dump("
+ echo "
WHEN " . $connection->quote($row["TABLE_NAME"]) . " THEN
" . (isset($row["ENGINE"]) ? "IF _engine != '$row[ENGINE]' OR _table_collation != '$row[TABLE_COLLATION]' OR _table_comment != $comment THEN
ALTER TABLE " . idf_escape($row["TABLE_NAME"]) . " ENGINE=$row[ENGINE] COLLATE=$row[TABLE_COLLATION] COMMENT=$comment;
- END IF" : "BEGIN END") . ";");
+ END IF" : "BEGIN END") . ";";
}
- dump("
+ echo "
ELSE
SET alter_command = CONCAT(alter_command, 'DROP TABLE `', REPLACE(_table_name, '`', '``'), '`;\\n');
END CASE;
@@ -119,14 +119,13 @@
DELIMITER ;
CALL adminer_alter(@adminer_alter);
DROP PROCEDURE adminer_alter;
-");
+";
}
if (in_array("CREATE+ALTER", array($style, $_POST["table_style"])) && $_POST["format"] == "sql") {
- dump("SELECT @adminer_alter;\n");
+ echo "SELECT @adminer_alter;\n";
}
}
}
- dump();
exit;
}
View
40 adminer/include/export.inc.php
@@ -19,39 +19,39 @@ function dump_triggers($table, $style) {
$s .= "\n" . ($style == 'CREATE+ALTER' ? "DROP TRIGGER IF EXISTS " . idf_escape($row["Trigger"]) . ";;\n" : "")
. "CREATE TRIGGER " . idf_escape($row["Trigger"]) . " $row[Timing] $row[Event] ON " . idf_escape($row["Table"]) . " FOR EACH ROW\n$row[Statement];;\n";
}
- dump("$s\nDELIMITER ;\n");
+ echo "$s\nDELIMITER ;\n";
}
}
}
function dump_table($table, $style, $is_view = false) {
global $connection;
if ($_POST["format"] == "csv") {
- dump("\xef\xbb\xbf"); // UTF-8 byte order mark
+ echo "\xef\xbb\xbf"; // UTF-8 byte order mark
if ($style) {
dump_csv(array_keys(fields($table)));
}
} elseif ($style) {
$result = $connection->query("SHOW CREATE TABLE " . idf_escape($table));
if ($result) {
if ($style == "DROP+CREATE") {
- dump("DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . idf_escape($table) . ";\n");
+ echo "DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . idf_escape($table) . ";\n";
}
$create = $connection->result($result, 1);
- dump(($style != "CREATE+ALTER" ? $create : ($is_view ? substr_replace($create, " OR REPLACE", 6, 0) : substr_replace($create, " IF NOT EXISTS", 12, 0))) . ";\n\n");
+ echo ($style != "CREATE+ALTER" ? $create : ($is_view ? substr_replace($create, " OR REPLACE", 6, 0) : substr_replace($create, " IF NOT EXISTS", 12, 0))) . ";\n\n";
}
if ($style == "CREATE+ALTER" && !$is_view) {
// create procedure which iterates over original columns and adds new and removes old
$query = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLLATION_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . $connection->quote($table) . " ORDER BY ORDINAL_POSITION";
- dump("DELIMITER ;;
+ echo "DELIMITER ;;
CREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN
DECLARE _column_name, _collation_name, _column_type, after varchar(64) DEFAULT '';
DECLARE _column_default longtext;
DECLARE _is_nullable char(3);
DECLARE _extra varchar(20);
DECLARE _column_comment varchar(255);
DECLARE done, set_after bool DEFAULT 0;
- DECLARE add_columns text DEFAULT '");
+ DECLARE add_columns text DEFAULT '";
$fields = array();
$result = $connection->query($query);
$after = "";
@@ -67,11 +67,11 @@ function dump_table($table, $style, $is_view = false) {
. ($row["COLUMN_COMMENT"] ? " COMMENT " . $connection->quote($row["COLUMN_COMMENT"]) : "")
. ($after ? " AFTER " . idf_escape($after) : " FIRST")
);
- dump(", ADD $row[alter]");
+ echo ", ADD $row[alter]";
$fields[] = $row;
$after = $row["COLUMN_NAME"];
}
- dump("';
+ echo "';
DECLARE columns CURSOR FOR $query;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
SET @alter_table = '';
@@ -80,16 +80,16 @@ function dump_table($table, $style, $is_view = false) {
FETCH columns INTO _column_name, _column_default, _is_nullable, _collation_name, _column_type, _extra, _column_comment;
IF NOT done THEN
SET set_after = 1;
- CASE _column_name");
+ CASE _column_name";
foreach ($fields as $row) {
- dump("
+ echo "
WHEN " . $connection->quote($row["COLUMN_NAME"]) . " THEN
SET add_columns = REPLACE(add_columns, ', ADD $row[alter]', '');
IF NOT (_column_default <=> $row[default]) OR _is_nullable != '$row[IS_NULLABLE]' OR _collation_name != '$row[COLLATION_NAME]' OR _column_type != " . $connection->quote($row["COLUMN_TYPE"]) . " OR _extra != '$row[EXTRA]' OR _column_comment != " . $connection->quote($row["COLUMN_COMMENT"]) . " OR after != $row[after] THEN
SET @alter_table = CONCAT(@alter_table, ', MODIFY $row[alter]');
- END IF;"); //! don't replace in comment
+ END IF;"; //! don't replace in comment
}
- dump("
+ echo "
ELSE
SET @alter_table = CONCAT(@alter_table, ', DROP ', _column_name);
SET set_after = 0;
@@ -108,7 +108,7 @@ function dump_table($table, $style, $is_view = false) {
CALL adminer_alter(@adminer_alter);
DROP PROCEDURE adminer_alter;
-");
+";
//! indexes
}
}
@@ -119,7 +119,7 @@ function dump_data($table, $style, $select = "") {
$max_packet = 1048576; // default, minimum is 1024
if ($style) {
if ($_POST["format"] != "csv" && $style == "TRUNCATE+INSERT") {
- dump("TRUNCATE " . idf_escape($table) . ";\n");
+ echo "TRUNCATE " . idf_escape($table) . ";\n";
}
$fields = fields($table);
$result = $connection->query(($select ? $select : "SELECT * FROM " . idf_escape($table)), 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers, microtime
@@ -142,7 +142,7 @@ function dump_data($table, $style, $select = "") {
foreach ($row as $key => $val) {
$set[] = idf_escape($key) . " = $val";
}
- dump("$insert ($s) ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n");
+ echo "$insert ($s) ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n";
} else {
$s = "\n($s)";
if (!$buffer) {
@@ -151,15 +151,15 @@ function dump_data($table, $style, $select = "") {
$buffer .= ",$s";
} else {
$buffer .= ";\n";
- dump($buffer);
+ echo $buffer;
$buffer = $insert . $s;
}
}
}
}
if ($_POST["format"] != "csv" && $style != "INSERT+UPDATE" && $buffer) {
$buffer .= ";\n";
- dump($buffer);
+ echo $buffer;
}
}
}
@@ -179,5 +179,11 @@ function dump_headers($identifier, $multi_table = false) {
header("Content-Disposition: attachment; filename=$filename.$ext" . ($output != "file" && !ereg('[^0-9a-z]', $output) ? ".$output" : ""));
}
session_write_close();
+ if ($_POST["output"] == "bz2") {
+ ob_start('bzcompress', 1e6);
+ }
+ if ($_POST["output"] == "gz") {
+ ob_start('gzencode', 1e6);
+ }
return $ext;
}
View
23 adminer/include/functions.inc.php
@@ -469,27 +469,6 @@ function process_input($field) {
}
}
-/** Print data with optional compression
-* @param string null to force output
-* @return null
-*/
-function dump($string = null) { // null $string forces sending of buffer
- static $buffer = ""; // used to improve compression and to allow GZ archives unpackable in Total Commander
- if (!ereg("text|file", $_POST["output"])) {
- $buffer .= $string;
- if (!isset($string) || strlen($buffer) > 1e6) {
- if ($_POST["output"] == "bz2") {
- echo bzcompress($buffer); // should not be called repeatedly but it would require whole buffer in memory or temporary file
- } else {
- echo gzencode($buffer);
- }
- $buffer = "";
- }
- } else {
- echo $string;
- }
-}
-
/** Print CSV row
* @param array
* @return null
@@ -500,7 +479,7 @@ function dump_csv($row) {
$row[$key] = '"' . str_replace('"', '""', $val) . '"';
}
}
- dump(implode(",", $row) . "\n");
+ echo implode(",", $row) . "\n";
}
/** Apply SQL function
View
1 adminer/select.inc.php
@@ -51,7 +51,6 @@
}
dump_data($TABLE, "INSERT", implode(" UNION ALL ", $union));
}
- dump();
exit;
}
if (!$adminer->selectEmailProcess($where, $foreign_keys)) {
View
2 editor/include/export.inc.php
@@ -1,6 +1,6 @@
<?php
function dump_table($table) {
- dump("\xef\xbb\xbf"); // UTF-8 byte order mark
+ echo "\xef\xbb\xbf"; // UTF-8 byte order mark
}
function dump_data($table, $style, $select = "") {

0 comments on commit 89a60e7

Please sign in to comment.