Report errors in get_rows()

This commit is contained in:
Jakub Vrana 2010-10-13 18:59:15 +02:00
parent 7e644b4346
commit 96544baf69
7 changed files with 20 additions and 34 deletions

View file

@ -343,18 +343,15 @@ WHERE o.schema_id = SCHEMA_ID(" . q(get_schema()) . ") AND o.type IN ('S', 'U',
}
$return = array();
// sp_statistics doesn't return information about primary key
$result = $connection2->query("SELECT indexes.name, key_ordinal, is_unique, is_primary_key, columns.name AS column_name
foreach (get_rows("SELECT indexes.name, key_ordinal, is_unique, is_primary_key, columns.name AS column_name
FROM sys.indexes
INNER JOIN sys.index_columns ON indexes.object_id = index_columns.object_id AND indexes.index_id = index_columns.index_id
INNER JOIN sys.columns ON index_columns.object_id = columns.object_id AND index_columns.column_id = columns.column_id
WHERE OBJECT_NAME(indexes.object_id) = " . q($table)
);
if ($result) {
while ($row = $result->fetch_assoc()) {
$return[$row["name"]]["type"] = ($row["is_primary_key"] ? "PRIMARY" : ($row["is_unique"] ? "UNIQUE" : "INDEX"));
$return[$row["name"]]["lengths"] = array();
$return[$row["name"]]["columns"][$row["key_ordinal"]] = $row["column_name"];
}
, $connection2) as $row) {
$return[$row["name"]]["type"] = ($row["is_primary_key"] ? "PRIMARY" : ($row["is_unique"] ? "UNIQUE" : "INDEX"));
$return[$row["name"]]["lengths"] = array();
$return[$row["name"]]["columns"][$row["key_ordinal"]] = $row["column_name"];
}
return $return;
}

View file

@ -835,7 +835,7 @@ if (!defined("DRIVER")) {
*/
function trigger_sql($table, $style) {
$return = "";
foreach (get_rows("SHOW TRIGGERS LIKE " . q(addcslashes($table, "%_"))) as $row) {
foreach (get_rows("SHOW TRIGGERS LIKE " . q(addcslashes($table, "%_")), null, "-- ") 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";
}

View file

@ -251,7 +251,7 @@ ORDER BY a.attnum < 0, a.attnum"
$return = array();
$table_oid = $connection2->result("SELECT oid FROM pg_class WHERE relname = " . q($table));
$columns = get_key_vals("SELECT attnum, attname FROM pg_attribute WHERE attrelid = $table_oid AND attnum > 0", $connection2);
foreach (get_rows("SELECT relname, indisunique, indisprimary, indkey FROM pg_index i, pg_class ci WHERE i.indrelid = $table_oid AND ci.oid = i.indexrelid") as $row) {
foreach (get_rows("SELECT relname, indisunique, indisprimary, indkey FROM pg_index i, pg_class ci WHERE i.indrelid = $table_oid AND ci.oid = i.indexrelid", $connection2) as $row) {
$return[$row["relname"]]["type"] = ($row["indisprimary"] == "t" ? "PRIMARY" : ($row["indisunique"] == "t" ? "UNIQUE" : "INDEX"));
$return[$row["relname"]]["columns"] = array();
foreach (explode(" ", $row["indkey"]) as $indkey) {
@ -269,7 +269,8 @@ FROM information_schema.table_constraints tc
LEFT JOIN information_schema.key_column_usage kcu USING (constraint_catalog, constraint_schema, constraint_name)
LEFT JOIN information_schema.referential_constraints rc USING (constraint_catalog, constraint_schema, constraint_name)
LEFT JOIN information_schema.constraint_column_usage ccu ON rc.unique_constraint_catalog = ccu.constraint_catalog AND rc.unique_constraint_schema = ccu.constraint_schema AND rc.unique_constraint_name = ccu.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = " . q($table)) as $row) { //! there can be more unique_constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_name = " . q($table) //! there can be more unique_constraint_name
) as $row) {
$foreign_key = &$return[$row["constraint_name"]];
if (!$foreign_key) {
$foreign_key = $row;

View file

@ -254,7 +254,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) {
$row["Auto_increment"] = "";
$return[$row["Name"]] = $row;
}
foreach (get_rows("SELECT * FROM sqlite_sequence") as $row) {
foreach (get_rows("SELECT * FROM sqlite_sequence", null, "") as $row) {
$return[$row["name"]]["Auto_increment"] = $row["seq"];
}
return ($name != "" ? $return[$name] : $return);

View file

@ -45,26 +45,16 @@ SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
$out = "";
if ($_POST["routines"]) {
foreach (array("FUNCTION", "PROCEDURE") as $routine) {
$result = $connection->query("SHOW $routine STATUS WHERE Db = " . q($db));
if ($result) {
while ($row = $result->fetch_assoc()) {
$out .= ($style != 'DROP+CREATE' ? "DROP $routine IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
. $connection->result("SHOW CREATE $routine " . idf_escape($row["Name"]), 2) . ";;\n\n";
}
} else {
echo "-- $connection->error\n\n";
foreach (get_rows("SHOW $routine STATUS WHERE Db = " . q($db), null, "-- ") as $row) {
$out .= ($style != 'DROP+CREATE' ? "DROP $routine IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
. $connection->result("SHOW CREATE $routine " . idf_escape($row["Name"]), 2) . ";;\n\n";
}
}
}
if ($_POST["events"]) {
$result = $connection->query("SHOW EVENTS");
if ($result) {
while ($row = $result->fetch_assoc()) {
$out .= ($style != 'DROP+CREATE' ? "DROP EVENT IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
. $connection->result("SHOW CREATE EVENT " . idf_escape($row["Name"]), 3) . ";;\n\n";
}
} else {
echo "-- $connection->error\n\n";
foreach (get_rows("SHOW EVENTS", null, "-- ") as $row) {
$out .= ($style != 'DROP+CREATE' ? "DROP EVENT IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
. $connection->result("SHOW CREATE EVENT " . idf_escape($row["Name"]), 3) . ";;\n\n";
}
}
if ($out) {

View file

@ -135,10 +135,6 @@ function html_select($name, $options, $value = "", $onchange = true) {
return $return;
}
// other functions
/** Get INI boolean value
* @param string
* @return bool
@ -192,7 +188,7 @@ function get_key_vals($query, $connection2 = null) {
* @param string
* @return array associative
*/
function get_rows($query, $connection2 = null) {
function get_rows($query, $connection2 = null, $error = "<p class='error'>") {
global $connection;
if (!is_object($connection2)) {
$connection2 = $connection;
@ -203,6 +199,8 @@ function get_rows($query, $connection2 = null) {
while ($row = $result->fetch_assoc()) {
$return[] = $row;
}
} elseif ($error && (headers_sent() || ob_get_level())) {
echo $error . error() . "\n";
}
return $return;
}

View file

@ -67,7 +67,7 @@ FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA = " . q($this->database()) . "
AND REFERENCED_TABLE_SCHEMA = " . q($this->database()) . "
AND REFERENCED_TABLE_NAME = " . q($table) . "
ORDER BY ORDINAL_POSITION") as $row) { //! requires MySQL 5
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"];
}
foreach ($return as $key => $val) {