Optimize table_status()

This commit is contained in:
Jakub Vrana 2013-04-26 20:04:57 -07:00
parent 3cae3e2f7f
commit e24d1fcb02
12 changed files with 31 additions and 24 deletions

View file

@ -366,11 +366,16 @@ if (!defined("DRIVER")) {
/** Get table status /** Get table status
* @param string * @param string
* @param bool return only "Name", "Engine" and "Comment" fields
* @return array array($name => array("Name" => , "Engine" => , "Comment" => , "Oid" => , "Rows" => , "Collation" => , "Auto_increment" => , "Data_length" => , "Index_length" => , "Data_free" => )) or only inner array with $name * @return array array($name => array("Name" => , "Engine" => , "Comment" => , "Oid" => , "Rows" => , "Collation" => , "Auto_increment" => , "Data_length" => , "Index_length" => , "Data_free" => )) or only inner array with $name
*/ */
function table_status($name = "") { function table_status($name = "", $fast = false) {
global $connection;
$return = array(); $return = array();
foreach (get_rows("SHOW TABLE STATUS" . ($name != "" ? " LIKE " . q(addcslashes($name, "%_\\")) : "")) as $row) { foreach (get_rows($fast && $connection->server_info >= 5
? "SELECT table_name AS Name, Engine, TABLE_COMMENT AS Comment FROM information_schema.TABLES WHERE table_schema = " . q(DB) . ($name != "" ? " AND table_name = " . q($name) : "")
: "SHOW TABLE STATUS" . ($name != "" ? " LIKE " . q(addcslashes($name, "%_\\")) : "")
) as $row) {
if ($row["Engine"] == "InnoDB") { if ($row["Engine"] == "InnoDB") {
// ignore internal comment, unnecessary since MySQL 5.1.21 // ignore internal comment, unnecessary since MySQL 5.1.21
$row["Comment"] = preg_replace('~(?:(.+); )?InnoDB free: .*~', '\\1', $row["Comment"]); $row["Comment"] = preg_replace('~(?:(.+); )?InnoDB free: .*~', '\\1', $row["Comment"]);
@ -391,7 +396,7 @@ if (!defined("DRIVER")) {
* @return bool * @return bool
*/ */
function is_view($table_status) { function is_view($table_status) {
return !isset($table_status["Rows"]); return !isset($table_status["Engine"]);
} }
/** Check if table supports foreign keys /** Check if table supports foreign keys
@ -960,6 +965,9 @@ if (!defined("DRIVER")) {
if (ereg("binary", $field["type"])) { if (ereg("binary", $field["type"])) {
return "HEX(" . idf_escape($field["field"]) . ")"; return "HEX(" . idf_escape($field["field"]) . ")";
} }
if ($field["type"] == "bit") {
return "BIN(" . idf_escape($field["field"]) . " + 0)"; // + 0 is required outside MySQLnd
}
if (ereg("geometry|point|linestring|polygon", $field["type"])) { if (ereg("geometry|point|linestring|polygon", $field["type"])) {
return "AsWKT(" . idf_escape($field["field"]) . ")"; return "AsWKT(" . idf_escape($field["field"]) . ")";
} }
@ -974,6 +982,9 @@ if (!defined("DRIVER")) {
if (ereg("binary", $field["type"])) { if (ereg("binary", $field["type"])) {
$return = "UNHEX($return)"; $return = "UNHEX($return)";
} }
if ($field["type"] == "bit") {
return "CONV($return, 2, 10) + 0";
}
if (ereg("geometry|point|linestring|polygon", $field["type"])) { if (ereg("geometry|point|linestring|polygon", $field["type"])) {
$return = "GeomFromText($return)"; $return = "GeomFromText($return)";
} }

View file

@ -66,7 +66,7 @@ SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
if ($_POST["table_style"] || $_POST["data_style"]) { if ($_POST["table_style"] || $_POST["data_style"]) {
$views = array(); $views = array();
foreach (table_status() as $name => $table_status) { foreach (table_status('', true) as $name => $table_status) {
$table = (DB == "" || in_array($name, (array) $_POST["tables"])); $table = (DB == "" || in_array($name, (array) $_POST["tables"]));
$data = (DB == "" || in_array($name, (array) $_POST["data"])); $data = (DB == "" || in_array($name, (array) $_POST["data"]));
if ($table || $data) { if ($table || $data) {

View file

@ -38,7 +38,7 @@ if ($_POST && !$error && !isset($_GET["select"])) {
} }
} }
$table_name = $adminer->tableName(table_status($TABLE)); $table_name = $adminer->tableName(table_status($TABLE, true));
page_header( page_header(
($update ? lang('Edit') : lang('Insert')), ($update ? lang('Edit') : lang('Insert')),
$error, $error,

View file

@ -39,12 +39,7 @@ if ($_POST) {
$source = array_keys(fields($TABLE)); //! no text and blob $source = array_keys(fields($TABLE)); //! no text and blob
$target = ($TABLE === $row["table"] ? $source : array_keys(fields($row["table"]))); $target = ($TABLE === $row["table"] ? $source : array_keys(fields($row["table"])));
$referencable = array(); $referencable = array_keys(array_filter(table_status('', true), 'fk_support'));
foreach (table_status() as $name => $table_status) {
if (fk_support($table_status)) {
$referencable[] = $name;
}
}
?> ?>
<form action="" method="post"> <form action="" method="post">

View file

@ -777,7 +777,7 @@ username.form['auth[driver]'].onchange();
$this->databasesPrint($missing); $this->databasesPrint($missing);
if ($_GET["ns"] !== "" && !$missing && DB != "") { if ($_GET["ns"] !== "" && !$missing && DB != "") {
echo '<p><a href="' . h(ME) . 'create="' . bold($_GET["create"] === "") . ">" . lang('Create new table') . "</a>\n"; echo '<p><a href="' . h(ME) . 'create="' . bold($_GET["create"] === "") . ">" . lang('Create new table') . "</a>\n";
$tables = tables_list(); $tables = table_status('', true);
if (!$tables) { if (!$tables) {
echo "<p class='message'>" . lang('No tables.') . "\n"; echo "<p class='message'>" . lang('No tables.') . "\n";
} else { } else {
@ -832,14 +832,14 @@ echo ($databases
} }
/** Prints table list in menu /** Prints table list in menu
* @param array * @param array result of table_status('', true)
* @return null * @return null
*/ */
function tablesPrint($tables) { function tablesPrint($tables) {
echo "<p id='tables' onmouseover='menuOver(this, event);' onmouseout='menuOut(this);'>\n"; echo "<p id='tables' onmouseover='menuOver(this, event);' onmouseout='menuOut(this);'>\n";
foreach ($tables as $table => $type) { foreach ($tables as $table => $status) {
echo '<a href="' . h(ME) . 'select=' . urlencode($table) . '"' . bold($_GET["select"] == $table) . ">" . lang('select') . "</a> "; echo '<a href="' . h(ME) . 'select=' . urlencode($table) . '"' . bold($_GET["select"] == $table) . ">" . lang('select') . "</a> ";
echo '<a href="' . h(ME) . 'table=' . urlencode($table) . '"' . bold($_GET["table"] == $table) . " title='" . lang('Show structure') . "'>" . $this->tableName(array("Name" => $table)) . "</a><br>\n"; //! Adminer::tableName may work with full table status echo '<a href="' . h(ME) . 'table=' . urlencode($table) . '"' . bold($_GET["table"] == $table) . " title='" . lang('Show structure') . "'>" . $this->tableName($status) . "</a><br>\n";
} }
} }

View file

@ -94,7 +94,7 @@ function select($result, $connection2 = null, $href = "", $orgtables = array())
*/ */
function referencable_primary($self) { function referencable_primary($self) {
$return = array(); // table_name => field $return = array(); // table_name => field
foreach (table_status() as $table_name => $table) { foreach (table_status('', true) as $table_name => $table) {
if ($table_name != $self && fk_support($table)) { if ($table_name != $self && fk_support($table)) {
foreach (fields($table_name) as $field) { foreach (fields($table_name) as $field) {
if ($field["primary"]) { if ($field["primary"]) {

View file

@ -822,7 +822,7 @@ function search_tables() {
$_GET["where"][0]["op"] = "LIKE %%"; $_GET["where"][0]["op"] = "LIKE %%";
$_GET["where"][0]["val"] = $_POST["query"]; $_GET["where"][0]["val"] = $_POST["query"];
$found = false; $found = false;
foreach (table_status() as $table => $table_status) { foreach (table_status('', true) as $table => $table_status) {
$name = $adminer->tableName($table_status); $name = $adminer->tableName($table_status);
if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) { if (isset($table_status["Engine"]) && $name != "" && (!$_POST["tables"] || in_array($table, $_POST["tables"]))) {
$result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1)); $result = $connection->query("SELECT" . limit("1 FROM " . table($table), " WHERE " . implode(" AND ", $adminer->selectSearchProcess(fields($table), array())), 1));

View file

@ -1,7 +1,7 @@
<?php <?php
$TABLE = $_GET["indexes"]; $TABLE = $_GET["indexes"];
$index_types = array("PRIMARY", "UNIQUE", "INDEX"); $index_types = array("PRIMARY", "UNIQUE", "INDEX");
$table_status = table_status($TABLE); $table_status = table_status($TABLE, true);
if (eregi("MyISAM|M?aria" . ($connection->server_info >= 5.6 ? "|InnoDB" : ""), $table_status["Engine"])) { if (eregi("MyISAM|M?aria" . ($connection->server_info >= 5.6 ? "|InnoDB" : ""), $table_status["Engine"])) {
$index_types[] = "FULLTEXT"; $index_types[] = "FULLTEXT";
} }

View file

@ -16,7 +16,7 @@ $base_left = -1;
$schema = array(); // table => array("fields" => array(name => field), "pos" => array(top, left), "references" => array(table => array(left => array(source, target)))) $schema = array(); // table => array("fields" => array(name => field), "pos" => array(top, left), "references" => array(table => array(left => array(source, target))))
$referenced = array(); // target_table => array(table => array(left => target_column)) $referenced = array(); // target_table => array(table => array(left => target_column))
$lefts = array(); // float => bool $lefts = array(); // float => bool
foreach (table_status() as $table => $table_status) { foreach (table_status('', true) as $table => $table_status) {
if (is_view($table_status)) { if (is_view($table_status)) {
continue; continue;
} }

View file

@ -4,7 +4,7 @@ $fields = fields($TABLE);
if (!$fields) { if (!$fields) {
$error = error(); $error = error();
} }
$table_status = ($fields ? table_status($TABLE) : array()); $table_status = ($fields ? table_status($TABLE, true) : array());
page_header(($fields && is_view($table_status) ? lang('View') : lang('Table')) . ": " . h($TABLE), $error); page_header(($fields && is_view($table_status) ? lang('View') : lang('Table')) . ": " . h($TABLE), $error);
$adminer->selectLinks($table_status); $adminer->selectLinks($table_status);

View file

@ -2,8 +2,9 @@ Adminer 3.7.0-dev:
Allow more SQL files to be uploaded at the same time Allow more SQL files to be uploaded at the same time
Print run time next to executed queries Print run time next to executed queries
Disable SQL export when applying functions in select Disable SQL export when applying functions in select
Fix handling of POINT data type (bug #3582578) MySQL: Optimize create table page and Editor navigation
Don't export binary and geometry columns twice in select MySQL: Fix handling of POINT data type (bug #3582578)
MySQL: Don't export binary and geometry columns twice in select
Adminer 3.6.4 (released 2013-04-26): Adminer 3.6.4 (released 2013-04-26):
Display pagination on a fixed position Display pagination on a fixed position

View file

@ -92,7 +92,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
$return[$row["TABLE_NAME"]]["keys"][$row["CONSTRAINT_NAME"]][$row["COLUMN_NAME"]] = $row["REFERENCED_COLUMN_NAME"]; $return[$row["TABLE_NAME"]]["keys"][$row["CONSTRAINT_NAME"]][$row["COLUMN_NAME"]] = $row["REFERENCED_COLUMN_NAME"];
} }
foreach ($return as $key => $val) { foreach ($return as $key => $val) {
$name = $this->tableName(table_status($key)); $name = $this->tableName(table_status($key, true));
if ($name != "") { if ($name != "") {
$search = preg_quote($tableName); $search = preg_quote($tableName);
$separator = "(:|\\s*-)?\\s+"; $separator = "(:|\\s*-)?\\s+";
@ -562,7 +562,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row) { //! requires MySQL 5
<?php <?php
$this->databasesPrint($missing); $this->databasesPrint($missing);
if ($missing != "db" && $missing != "ns") { if ($missing != "db" && $missing != "ns") {
$table_status = table_status(); $table_status = table_status('', true);
if (!$table_status) { if (!$table_status) {
echo "<p class='message'>" . lang('No tables.') . "\n"; echo "<p class='message'>" . lang('No tables.') . "\n";
} else { } else {