Permalink
Browse files

Avoid fatal errors

  • Loading branch information...
1 parent d2e49b1 commit b0d637b638118ce5ebc64214ca577dc3313dd2ac @vrana committed Oct 13, 2010
View
@@ -122,8 +122,7 @@
list($row["partition_by"], $row["partitions"], $row["partition"]) = $result->fetch_row();
$row["partition_names"] = array();
$row["partition_values"] = array();
- $result = $connection->query("SELECT PARTITION_NAME, PARTITION_DESCRIPTION $from AND PARTITION_NAME != '' ORDER BY PARTITION_ORDINAL_POSITION");
- while ($row1 = $result->fetch_assoc()) {
+ foreach (get_rows("SELECT PARTITION_NAME, PARTITION_DESCRIPTION $from AND PARTITION_NAME != '' ORDER BY PARTITION_ORDINAL_POSITION") as $row1) {
$row["partition_names"][] = $row1["PARTITION_NAME"];
$row["partition_values"][] = $row1["PARTITION_DESCRIPTION"];
}
View
@@ -141,11 +141,11 @@
if (support("event")) {
echo "<h3>" . lang('Events') . "</h3>\n";
- $result = $connection->query("SHOW EVENTS");
- if ($result && $result->num_rows) {
+ $rows = get_rows("SHOW EVENTS");
+ if ($rows) {
echo "<table cellspacing='0'>\n";
echo "<thead><tr><th>" . lang('Name') . "<td>" . lang('Schedule') . "<td>" . lang('Start') . "<td>" . lang('End') . "</thead>\n";
- while ($row = $result->fetch_assoc()) {
+ foreach ($rows as $row) {
echo "<tr>";
echo '<th><a href="' . h(ME) . 'event=' . urlencode($row["Name"]) . '">' . h($row["Name"]) . "</a>";
echo "<td>" . ($row["Execute at"] ? lang('At given time') . "<td>" . $row["Execute at"] : lang('Every') . " " . $row["Interval value"] . " " . $row["Interval field"] . "<td>$row[Starts]");
@@ -294,8 +294,7 @@ function count_tables($databases) {
function table_status($name = "") {
global $connection;
$return = array();
- $result = $connection->query("SELECT name AS Name, type_desc AS Engine FROM sys.all_objects WHERE schema_id = SCHEMA_ID(" . $connection->quote(get_schema()) . ") AND type IN ('S', 'U', 'V')" . ($name != "" ? " AND name = " . $connection->quote($name) : ""));
- while ($row = $result->fetch_assoc()) {
+ foreach (get_rows("SELECT name AS Name, type_desc AS Engine FROM sys.all_objects WHERE schema_id = SCHEMA_ID(" . $connection->quote(get_schema()) . ") AND type IN ('S', 'U', 'V')" . ($name != "" ? " AND name = " . $connection->quote($name) : "")) as $row) {
if ($name != "") {
return $row;
}
@@ -315,14 +314,13 @@ function fk_support($table_status) {
function fields($table, $hidden = false) {
global $connection;
$return = array();
- $result = $connection->query("SELECT c.*, t.name type, d.definition [default]
+ foreach (get_rows("SELECT c.*, t.name type, d.definition [default]
FROM sys.all_columns c
JOIN sys.all_objects o ON c.object_id = o.object_id
JOIN sys.types t ON c.user_type_id = t.user_type_id
LEFT JOIN sys.default_constraints d ON c.default_object_id = d.parent_column_id
WHERE o.schema_id = SCHEMA_ID(" . $connection->quote(get_schema()) . ") AND o.type IN ('S', 'U', 'V') AND o.name = " . $connection->quote($table)
- );
- while ($row = $result->fetch_assoc()) {
+ ) as $row) {
$type = $row["type"];
$length = (ereg("char|binary", $type) ? $row["max_length"] : ($type == "decimal" ? "$row[precision],$row[scale]" : ""));
$return[$row["name"]] = array(
@@ -506,9 +504,8 @@ function explain($connection, $query) {
function foreign_keys($table) {
global $connection;
- $result = $connection->query("EXEC sp_fkeys @fktable_name = " . $connection->quote($table));
$return = array();
- while ($row = $result->fetch_assoc()) {
+ foreach (get_rows("EXEC sp_fkeys @fktable_name = " . $connection->quote($table)) as $row) {
$foreign_key = &$return[$row["FK_NAME"]];
$foreign_key["table"] = $row["PKTABLE_NAME"];
$foreign_key["source"][] = $row["FKCOLUMN_NAME"];
@@ -535,30 +532,31 @@ function move_tables($tables, $views, $target) {
function trigger($name) {
global $connection;
- $result = $connection->query("SELECT s.name [Trigger],
+ $rows = get_rows("SELECT s.name [Trigger],
CASE WHEN OBJECTPROPERTY(s.id, 'ExecIsInsertTrigger') = 1 THEN 'INSERT' WHEN OBJECTPROPERTY(s.id, 'ExecIsUpdateTrigger') = 1 THEN 'UPDATE' WHEN OBJECTPROPERTY(s.id, 'ExecIsDeleteTrigger') = 1 THEN 'DELETE' END [Event],
CASE WHEN OBJECTPROPERTY(s.id, 'ExecIsInsteadOfTrigger') = 1 THEN 'INSTEAD OF' ELSE 'AFTER' END [Timing],
c.text
FROM sysobjects s
JOIN syscomments c ON s.id = c.id
WHERE s.xtype = 'TR' AND s.name = " . $connection->quote($name)
); // triggers are not schema-scoped
- $row = $result->fetch_assoc();
- $row["Statement"] = preg_replace('~^.+\\s+AS\\s+~isU', '', $row["text"]); //! identifiers, comments
- return $row;
+ $return = reset($rows);
+ if ($return) {
+ $return["Statement"] = preg_replace('~^.+\\s+AS\\s+~isU', '', $return["text"]); //! identifiers, comments
+ }
+ return $return;
}
function triggers($table) {
global $connection;
$return = array();
- $result = $connection->query("SELECT sys1.name,
+ foreach (get_rows("SELECT sys1.name,
CASE WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsertTrigger') = 1 THEN 'INSERT' WHEN OBJECTPROPERTY(sys1.id, 'ExecIsUpdateTrigger') = 1 THEN 'UPDATE' WHEN OBJECTPROPERTY(sys1.id, 'ExecIsDeleteTrigger') = 1 THEN 'DELETE' END [Event],
CASE WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsteadOfTrigger') = 1 THEN 'INSTEAD OF' ELSE 'AFTER' END [Timing]
FROM sysobjects sys1
JOIN sysobjects sys2 ON sys1.parent_obj = sys2.id
WHERE sys1.xtype = 'TR' AND sys2.name = " . $connection->quote($table)
- ); // triggers are not schema-scoped
- while ($row = $result->fetch_assoc()) {
+ ) as $row) { // triggers are not schema-scoped
$return[$row["name"]] = array($row["Timing"], $row["Event"]);
}
return $return;
@@ -307,10 +307,8 @@ function db_collation($db, $collations) {
* @return array
*/
function engines() {
- global $connection;
$return = array();
- $result = $connection->query("SHOW ENGINES");
- while ($row = $result->fetch_assoc()) {
+ foreach (get_rows("SHOW ENGINES") as $row) {
if (ereg("YES|DEFAULT", $row["Support"])) {
$return[] = $row["Engine"];
}
@@ -353,8 +351,7 @@ function count_tables($databases) {
function table_status($name = "") {
global $connection;
$return = array();
- $result = $connection->query("SHOW TABLE STATUS" . ($name != "" ? " LIKE " . $connection->quote(addcslashes($name, "%_")) : ""));
- while ($row = $result->fetch_assoc()) {
+ foreach (get_rows("SHOW TABLE STATUS" . ($name != "" ? " LIKE " . $connection->quote(addcslashes($name, "%_")) : "")) as $row) {
if ($row["Engine"] == "InnoDB") {
// ignore internal comment, unnecessary since MySQL 5.1.21
$row["Comment"] = preg_replace('~(?:(.+); )?InnoDB free: .*~', '\\1', $row["Comment"]);
@@ -392,28 +389,24 @@ function fk_support($table_status) {
* @return array array($name => array("field" => , "full_type" => , "type" => , "length" => , "unsigned" => , "default" => , "null" => , "auto_increment" => , "on_update" => , "collation" => , "privileges" => , "comment" => , "primary" => ))
*/
function fields($table, $hidden = false) {
- global $connection;
$return = array();
- $result = $connection->query("SHOW FULL COLUMNS FROM " . table($table));
- if ($result) {
- while ($row = $result->fetch_assoc()) {
- preg_match('~^([^( ]+)(?:\\((.+)\\))?( unsigned)?( zerofill)?$~', $row["Type"], $match);
- $return[$row["Field"]] = array(
- "field" => $row["Field"],
- "full_type" => $row["Type"],
- "type" => $match[1],
- "length" => $match[2],
- "unsigned" => ltrim($match[3] . $match[4]),
- "default" => ($row["Default"] != "" || ereg("char", $match[1]) ? $row["Default"] : null),
- "null" => ($row["Null"] == "YES"),
- "auto_increment" => ($row["Extra"] == "auto_increment"),
- "on_update" => (eregi('^on update (.+)', $row["Extra"], $match) ? $match[1] : ""), //! available since MySQL 5.1.23
- "collation" => $row["Collation"],
- "privileges" => array_flip(explode(",", $row["Privileges"])),
- "comment" => $row["Comment"],
- "primary" => ($row["Key"] == "PRI"),
- );
- }
+ foreach (get_rows("SHOW FULL COLUMNS FROM " . table($table)) as $row) {
+ preg_match('~^([^( ]+)(?:\\((.+)\\))?( unsigned)?( zerofill)?$~', $row["Type"], $match);
+ $return[$row["Field"]] = array(
+ "field" => $row["Field"],
+ "full_type" => $row["Type"],
+ "type" => $match[1],
+ "length" => $match[2],
+ "unsigned" => ltrim($match[3] . $match[4]),
+ "default" => ($row["Default"] != "" || ereg("char", $match[1]) ? $row["Default"] : null),
+ "null" => ($row["Null"] == "YES"),
+ "auto_increment" => ($row["Extra"] == "auto_increment"),
+ "on_update" => (eregi('^on update (.+)', $row["Extra"], $match) ? $match[1] : ""), //! available since MySQL 5.1.23
+ "collation" => $row["Collation"],
+ "privileges" => array_flip(explode(",", $row["Privileges"])),
+ "comment" => $row["Comment"],
+ "primary" => ($row["Key"] == "PRI"),
+ );
}
return $return;
}
@@ -429,13 +422,10 @@ function indexes($table, $connection2 = null) {
$connection2 = $connection;
}
$return = array();
- $result = $connection2->query("SHOW INDEX FROM " . table($table));
- if ($result) {
- while ($row = $result->fetch_assoc()) {
- $return[$row["Key_name"]]["type"] = ($row["Key_name"] == "PRIMARY" ? "PRIMARY" : ($row["Index_type"] == "FULLTEXT" ? "FULLTEXT" : ($row["Non_unique"] ? "INDEX" : "UNIQUE")));
- $return[$row["Key_name"]]["columns"][] = $row["Column_name"];
- $return[$row["Key_name"]]["lengths"][] = $row["Sub_part"];
- }
+ foreach (get_rows("SHOW INDEX FROM " . table($table), $connection2) as $row) {
+ $return[$row["Key_name"]]["type"] = ($row["Key_name"] == "PRIMARY" ? "PRIMARY" : ($row["Index_type"] == "FULLTEXT" ? "FULLTEXT" : ($row["Non_unique"] ? "INDEX" : "UNIQUE")));
+ $return[$row["Key_name"]]["columns"][] = $row["Column_name"];
+ $return[$row["Key_name"]]["lengths"][] = $row["Sub_part"];
}
return $return;
}
@@ -480,10 +470,8 @@ function view($name) {
* @return array
*/
function collations() {
- global $connection;
$return = array();
- $result = $connection->query("SHOW COLLATION");
- while ($row = $result->fetch_assoc()) {
+ foreach (get_rows("SHOW COLLATION") as $row) {
$return[$row["Charset"]][] = $row["Collation"];
}
ksort($return);
@@ -544,7 +532,6 @@ function drop_databases($databases) {
* @return bool
*/
function rename_database($name, $collation) {
- global $connection;
if (create_database($name, $collation)) {
//! move triggers
$rename = array();
@@ -673,8 +660,8 @@ function move_tables($tables, $views, $target) {
*/
function trigger($name) {
global $connection;
- $result = $connection->query("SHOW TRIGGERS WHERE `Trigger` = " . $connection->quote($name));
- return $result->fetch_assoc();
+ $rows = get_rows("SHOW TRIGGERS WHERE `Trigger` = " . $connection->quote($name));
+ return reset($rows);
}
/** Get defined triggers
@@ -684,8 +671,7 @@ function trigger($name) {
function triggers($table) {
global $connection;
$return = array();
- $result = $connection->query("SHOW TRIGGERS LIKE " . $connection->quote(addcslashes($table, "%_")));
- while ($row = $result->fetch_assoc()) {
+ foreach (get_rows("SHOW TRIGGERS LIKE " . $connection->quote(addcslashes($table, "%_"))) as $row) {
$return[$row["Trigger"]] = array($row["Timing"], $row["Event"]);
}
return $return;
@@ -740,12 +726,7 @@ function routine($name, $type) {
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;
+ return get_rows("SELECT * FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA = " . $connection->quote(DB));
}
/** Begin transaction
@@ -861,13 +842,10 @@ function use_sql($database) {
*/
function trigger_sql($table, $style) {
global $connection;
- $result = $connection->query("SHOW TRIGGERS LIKE " . $connection->quote(addcslashes($table, "%_")));
$return = "";
- if ($result->num_rows) {
- while ($row = $result->fetch_assoc()) {
- $return .= "\n" . ($style == 'CREATE+ALTER' ? "DROP TRIGGER IF EXISTS " . idf_escape($row["Trigger"]) . ";;\n" : "")
- . "CREATE TRIGGER " . idf_escape($row["Trigger"]) . " $row[Timing] $row[Event] ON " . table($row["Table"]) . " FOR EACH ROW\n$row[Statement];;\n";
- }
+ foreach (get_rows("SHOW TRIGGERS LIKE " . $connection->quote(addcslashes($table, "%_"))) as $row) {
+ $return .= "\n" . ($style == 'CREATE+ALTER' ? "DROP TRIGGER IF EXISTS " . idf_escape($row["Trigger"]) . ";;\n" : "")
+ . "CREATE TRIGGER " . idf_escape($row["Trigger"]) . " $row[Timing] $row[Event] ON " . table($row["Table"]) . " FOR EACH ROW\n$row[Statement];;\n";
}
return $return;
}
@@ -189,10 +189,9 @@ function table_status($name = "") {
global $connection;
$return = array();
$search = $connection->quote($name);
- $result = $connection->query('SELECT table_name "Name", \'table\' "Engine" FROM all_tables WHERE tablespace_name = ' . $connection->quote(DB) . ($name != "" ? " AND table_name = $search" : "") . "
+ foreach (get_rows('SELECT table_name "Name", \'table\' "Engine" FROM all_tables WHERE tablespace_name = ' . $connection->quote(DB) . ($name != "" ? " AND table_name = $search" : "") . "
UNION SELECT view_name, 'view' FROM user_views" . ($name != "" ? " WHERE view_name = $search" : "")
- );
- while ($row = $result->fetch_assoc()) {
+ ) as $row) {
if ($name != "") {
return $row;
}
@@ -212,28 +211,25 @@ function fk_support($table_status) {
function fields($table, $hidden = false) {
global $connection;
$return = array();
- $result = $connection->query("SELECT * FROM all_tab_columns WHERE table_name = " . $connection->quote($table) . " ORDER BY column_id");
- if ($result) {
- while ($row = $result->fetch_assoc()) {
- $type = $row["DATA_TYPE"];
- $length = "$row[DATA_PRECISION],$row[DATA_SCALE]";
- if ($length == ",") {
- $length = $row["DATA_LENGTH"];
- } //! int
- $return[$row["COLUMN_NAME"]] = array(
- "field" => $row["COLUMN_NAME"],
- "full_type" => $type . ($length ? "($length)" : ""),
- "type" => strtolower($type),
- "length" => $length,
- "default" => $row["DATA_DEFAULT"],
- "null" => ($row["NULLABLE"] == "Y"),
- //! "auto_increment" => false,
- //! "collation" => $row["CHARACTER_SET_NAME"],
- "privileges" => array("insert" => 1, "select" => 1, "update" => 1),
- //! "comment" => $row["Comment"],
- //! "primary" => ($row["Key"] == "PRI"),
- );
- }
+ foreach (get_rows("SELECT * FROM all_tab_columns WHERE table_name = " . $connection->quote($table) . " ORDER BY column_id") as $row) {
+ $type = $row["DATA_TYPE"];
+ $length = "$row[DATA_PRECISION],$row[DATA_SCALE]";
+ if ($length == ",") {
+ $length = $row["DATA_LENGTH"];
+ } //! int
+ $return[$row["COLUMN_NAME"]] = array(
+ "field" => $row["COLUMN_NAME"],
+ "full_type" => $type . ($length ? "($length)" : ""),
+ "type" => strtolower($type),
+ "length" => $length,
+ "default" => $row["DATA_DEFAULT"],
+ "null" => ($row["NULLABLE"] == "Y"),
+ //! "auto_increment" => false,
+ //! "collation" => $row["CHARACTER_SET_NAME"],
+ "privileges" => array("insert" => 1, "select" => 1, "update" => 1),
+ //! "comment" => $row["Comment"],
+ //! "primary" => ($row["Key"] == "PRI"),
+ );
}
return $return;
}
@@ -244,8 +240,8 @@ function indexes($table, $connection2 = null) {
function view($name) {
global $connection;
- $result = $connection->query('SELECT text "select" FROM user_views WHERE view_name = ' . $connection->quote($name));
- return $result->fetch_assoc();
+ $rows = get_rows('SELECT text "select" FROM user_views WHERE view_name = ' . $connection->quote($name));
+ return reset($rows);
}
function collations() {
@@ -338,13 +334,8 @@ function show_variables() {
}
function show_status() {
- global $connection;
- $return = array();
- $result = $connection->query('SELECT * FROM v$instance');
- foreach ($result->fetch_assoc() as $key => $val) {
- $return[$key] = $val;
- }
- return $return;
+ $rows = get_rows('SELECT * FROM v$instance');
+ return reset($rows);
}
function support($feature) {
Oops, something went wrong.

0 comments on commit b0d637b

Please sign in to comment.