/** Generate SQL query for creating routine * @param string "PROCEDURE" or "FUNCTION" * @param array result of routine() * @return string */ function create_routine($routine, $row) { global $inout; $set = array(); $fields = (array) $row["fields"]; ksort($fields); // enforce fields order foreach ($fields as $field) { if ($field["field"] != "") { $set[] = (preg_match("~^({$inout})\$~", $field["inout"]) ? "{$field['inout']} " : "") . idf_escape($field["field"]) . process_type($field, "CHARACTER SET"); } } return "CREATE {$routine} " . idf_escape(trim($row["name"])) . " (" . implode(", ", $set) . ")" . (isset($_GET["function"]) ? " RETURNS" . process_type($row["returns"], "CHARACTER SET") : "") . ($row["language"] ? " LANGUAGE {$row['language']}" : "") . rtrim("\n{$row['definition']}", ";") . ";"; }
/** Create SQL string from field * @param array basic field information * @param array information about field type * @return array array("field", "type", "NULL", "DEFAULT", "ON UPDATE", "COMMENT", "AUTO_INCREMENT") */ function process_field($field, $type_field) { return array(idf_escape($field["field"]), process_type($type_field), $field["null"] ? " NULL" : " NOT NULL", isset($field["default"]) ? " DEFAULT " . ($field["type"] == "timestamp" && eregi('^CURRENT_TIMESTAMP$', $field["default"]) || $field["type"] == "bit" && ereg("^([0-9]+|b'[0-1]+')\$", $field["default"]) ? $field["default"] : q($field["default"])) : "", $field["on_update"] ? " ON UPDATE {$field['on_update']}" : "", support("comment") && $field["comment"] != "" ? " COMMENT " . q($field["comment"]) : "", $field["auto_increment"] ? auto_increment() : null); }
if (strlen($_GET["procedure"]) && ($_POST["dropped"] || $mysql->query("DROP {$routine} " . idf_escape($_GET["procedure"])))) { if ($_POST["drop"]) { redirect(substr($SELF, 0, -1), lang('Routine has been dropped.')); } $dropped = true; } if (!$_POST["drop"]) { $set = array(); $fields = array_filter((array) $_POST["fields"], 'strlen'); ksort($fields); foreach ($fields as $field) { if (strlen($field["field"])) { $set[] = (in_array($field["inout"], $inout) ? "{$field['inout']} " : "") . idf_escape($field["field"]) . process_type($field, "CHARACTER SET"); } } if ($mysql->query("CREATE {$routine} " . idf_escape($_POST["name"]) . " (" . implode(", ", $set) . ")" . (isset($_GET["function"]) ? " RETURNS" . process_type($_POST["returns"], "CHARACTER SET") : "") . "\n\t\t\t{$_POST['definition']}")) { redirect(substr($SELF, 0, -1), strlen($_GET["procedure"]) ? lang('Routine has been altered.') : lang('Routine has been created.')); } } $error = $mysql->error; } page_header(strlen($_GET["procedure"]) ? (isset($_GET["function"]) ? lang('Alter function') : lang('Alter procedure')) . ": " . htmlspecialchars($_GET["procedure"]) : (isset($_GET["function"]) ? lang('Create function') : lang('Create procedure')), $error); $collations = get_vals("SHOW CHARACTER SET"); if ($_POST) { $row = $_POST; $row["fields"] = (array) $row["fields"]; process_fields($row["fields"]); } elseif (strlen($_GET["procedure"])) { $row = routine($_GET["procedure"], $routine); $row["name"] = $_GET["procedure"]; } else {
$PROCEDURE = $_GET["procedure"]; $routine = isset($_GET["function"]) ? "FUNCTION" : "PROCEDURE"; $routine_languages = routine_languages(); $dropped = false; if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] && !$_POST["down"]) { $set = array(); $fields = (array) $_POST["fields"]; ksort($fields); // enforce fields order foreach ($fields as $field) { if ($field["field"] != "") { $set[] = (ereg("^({$inout})\$", $field["inout"]) ? "{$field['inout']} " : "") . idf_escape($field["field"]) . process_type($field, "CHARACTER SET"); } } $dropped = drop_create("DROP {$routine} " . idf_escape($PROCEDURE), "CREATE {$routine} " . idf_escape(trim($_POST["name"])) . " (" . implode(", ", $set) . ")" . (isset($_GET["function"]) ? " RETURNS" . process_type($_POST["returns"], "CHARACTER SET") : "") . (in_array($_POST["language"], $routine_languages) ? " LANGUAGE {$_POST['language']}" : "") . rtrim("\n{$_POST['definition']}", ";") . ";", substr(ME, 0, -1), lang('Routine has been dropped.'), lang('Routine has been altered.'), lang('Routine has been created.'), $PROCEDURE); } page_header($PROCEDURE != "" ? (isset($_GET["function"]) ? lang('Alter function') : lang('Alter procedure')) . ": " . h($PROCEDURE) : (isset($_GET["function"]) ? lang('Create function') : lang('Create procedure')), $error); $collations = get_vals("SHOW CHARACTER SET"); sort($collations); $row = array("fields" => array()); if ($_POST) { $row = $_POST; $row["fields"] = (array) $row["fields"]; process_fields($row["fields"]); } elseif ($PROCEDURE != "") { $row = routine($PROCEDURE, $routine); $row["name"] = $PROCEDURE; } ?>