From 02a172e1ac8515ca8880b8f1792caa712e301fd7 Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Wed, 28 Apr 2010 00:11:39 +0000 Subject: [PATCH] Driver specific routines git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1508 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- adminer/db.inc.php | 8 +++--- adminer/drivers/mysql.inc.php | 47 +++++++++++++++++++++++++++++++++ adminer/drivers/sqlite.inc.php | 8 ++++++ adminer/include/editing.inc.php | 37 -------------------------- 4 files changed, 59 insertions(+), 41 deletions(-) diff --git a/adminer/db.inc.php b/adminer/db.inc.php index 0e5c9dce..13c210bc 100644 --- a/adminer/db.inc.php +++ b/adminer/db.inc.php @@ -94,12 +94,12 @@ if (support("view")) { } if (support("routine")) { echo "

" . lang('Routines') . "

\n"; - $result = $connection->query("SELECT * FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA = " . $connection->quote(DB)); - if ($result->num_rows) { - odd(''); + $routines = routines(); + if ($routines) { echo "\n"; echo '\n"; - while ($row = $result->fetch_assoc()) { + odd(''); + foreach ($routines as $row) { echo ''; echo '
' . lang('Name') . '' . lang('Type') . '' . lang('Return type') . " 
' . h($row["ROUTINE_NAME"]) . ''; echo '' . h($row["ROUTINE_TYPE"]); diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index 96317faa..00b57864 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -681,6 +681,53 @@ if (!defined("DRIVER")) { ); } + /** Get information about stored routine + * @param string + * @param string FUNCTION or PROCEDURE + * @return array ("fields" => array("field" => , "type" => , "length" => , "unsigned" => , "inout" => , "collation" => ), "returns" => , "definition" => ) + */ + function routine($name, $type) { + global $connection, $enum_length, $inout, $types; + $aliases = array("bit" => "tinyint", "bool" => "tinyint", "boolean" => "tinyint", "integer" => "int", "double precision" => "float", "real" => "float", "dec" => "decimal", "numeric" => "decimal", "fixed" => "decimal", "national char" => "char", "national varchar" => "varchar"); + $type_pattern = "((" . implode("|", array_keys($types + $aliases)) . ")(?:\\s*\\(((?:[^'\")]*|$enum_length)+)\\))?\\s*(zerofill\\s*)?(unsigned(?:\\s+zerofill)?)?)(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s]+)['\"]?)?"; + $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern"; + $create = $connection->result("SHOW CREATE $type " . idf_escape($name), 2); + preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match); + $fields = array(); + preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER); + foreach ($matches as $param) { + $name = str_replace("``", "`", $param[2]) . $param[3]; + $data_type = strtolower($param[5]); + $fields[] = array( + "field" => $name, + "type" => (isset($aliases[$data_type]) ? $aliases[$data_type] : $data_type), + "length" => preg_replace_callback("~$enum_length~s", 'normalize_enum', $param[6]), + "unsigned" => strtolower(preg_replace('~\\s+~', ' ', trim("$param[8] $param[7]"))), + "full_type" => $param[4], + "inout" => strtoupper($param[1]), + "collation" => strtolower($param[9]), + ); + } + if ($type != "FUNCTION") { + return array("fields" => $fields, "definition" => $match[11]); + } + return array( + "fields" => $fields, + "returns" => array("type" => $match[12], "length" => $match[13], "unsigned" => $match[15], "collation" => $match[16]), + "definition" => $match[17], + ); + } + + function routines() { + global $connection; + $return = array(); + $result = $connection->query("SELECT * FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA = " . $connection->quote(DB)); + while ($row = $result->fetch_assoc()) { + $return[] = $row; + } + return $return; + } + /** Begin transaction * @return bool */ diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index 6582120a..58b5f026 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -474,6 +474,14 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) { ); } + function routine($name, $type) { + // not supported by SQLite + } + + function routines() { + // not supported by SQLite + } + function begin() { return queries("BEGIN"); } diff --git a/adminer/include/editing.inc.php b/adminer/include/editing.inc.php index c53b04c4..6aa71835 100644 --- a/adminer/include/editing.inc.php +++ b/adminer/include/editing.inc.php @@ -280,43 +280,6 @@ function normalize_enum($match) { return "'" . str_replace("'", "''", addcslashes(stripcslashes(str_replace($match[0][0] . $match[0][0], $match[0][0], substr($match[0], 1, -1))), '\\')) . "'"; } -/** Get information about stored routine -* @param string -* @param string FUNCTION or PROCEDURE -* @return array ("fields" => array("field" => , "type" => , "length" => , "unsigned" => , "inout" => , "collation" => ), "returns" => , "definition" => ) -*/ -function routine($name, $type) { - global $connection, $enum_length, $inout, $types; - $aliases = array("bit" => "tinyint", "bool" => "tinyint", "boolean" => "tinyint", "integer" => "int", "double precision" => "float", "real" => "float", "dec" => "decimal", "numeric" => "decimal", "fixed" => "decimal", "national char" => "char", "national varchar" => "varchar"); - $type_pattern = "((" . implode("|", array_keys($types + $aliases)) . ")(?:\\s*\\(((?:[^'\")]*|$enum_length)+)\\))?\\s*(zerofill\\s*)?(unsigned(?:\\s+zerofill)?)?)(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s]+)['\"]?)?"; - $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern"; - $create = $connection->result("SHOW CREATE $type " . idf_escape($name), 2); - preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match); - $fields = array(); - preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER); - foreach ($matches as $param) { - $name = str_replace("``", "`", $param[2]) . $param[3]; - $data_type = strtolower($param[5]); - $fields[] = array( - "field" => $name, - "type" => (isset($aliases[$data_type]) ? $aliases[$data_type] : $data_type), - "length" => preg_replace_callback("~$enum_length~s", 'normalize_enum', $param[6]), - "unsigned" => strtolower(preg_replace('~\\s+~', ' ', trim("$param[8] $param[7]"))), - "full_type" => $param[4], - "inout" => strtoupper($param[1]), - "collation" => strtolower($param[9]), - ); - } - if ($type != "FUNCTION") { - return array("fields" => $fields, "definition" => $match[11]); - } - return array( - "fields" => $fields, - "returns" => array("type" => $match[12], "length" => $match[13], "unsigned" => $match[15], "collation" => $match[16]), - "definition" => $match[17], - ); -} - /** Issue grant or revoke commands * @param string GRANT or REVOKE * @param array