function dumpTable($table, $style, $is_view = false) { if ($_POST["format"] == "sql_alter") { $create = create_sql($table, $_POST["auto_increment"]); if ($is_view) { echo substr_replace($create, " OR REPLACE", 6, 0) . ";\n\n"; } else { echo substr_replace($create, " IF NOT EXISTS", 12, 0) . ";\n\n"; // create procedure which iterates over original columns and adds new and removes old $query = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLLATION_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . q($table) . " ORDER BY ORDINAL_POSITION"; echo "DELIMITER ;;\nCREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN\n\tDECLARE _column_name, _collation_name, after varchar(64) DEFAULT '';\n\tDECLARE _column_type, _column_default text;\n\tDECLARE _is_nullable char(3);\n\tDECLARE _extra varchar(30);\n\tDECLARE _column_comment varchar(255);\n\tDECLARE done, set_after bool DEFAULT 0;\n\tDECLARE add_columns text DEFAULT '"; $fields = array(); $after = ""; foreach (get_rows($query) as $row) { $default = $row["COLUMN_DEFAULT"]; $row["default"] = $default !== null ? q($default) : "NULL"; $row["after"] = q($after); //! rgt AFTER lft, lft AFTER id doesn't work $row["alter"] = escape_string(idf_escape($row["COLUMN_NAME"]) . " {$row['COLUMN_TYPE']}" . ($row["COLLATION_NAME"] ? " COLLATE {$row['COLLATION_NAME']}" : "") . ($default !== null ? " DEFAULT " . ($default == "CURRENT_TIMESTAMP" ? $default : $row["default"]) : "") . ($row["IS_NULLABLE"] == "YES" ? "" : " NOT NULL") . ($row["EXTRA"] ? " {$row['EXTRA']}" : "") . ($row["COLUMN_COMMENT"] ? " COMMENT " . q($row["COLUMN_COMMENT"]) : "") . ($after ? " AFTER " . idf_escape($after) : " FIRST")); echo ", ADD {$row['alter']}"; $fields[] = $row; $after = $row["COLUMN_NAME"]; } echo "';\n\tDECLARE columns CURSOR FOR {$query};\n\tDECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;\n\tSET @alter_table = '';\n\tOPEN columns;\n\tREPEAT\n\t\tFETCH columns INTO _column_name, _column_default, _is_nullable, _collation_name, _column_type, _extra, _column_comment;\n\t\tIF NOT done THEN\n\t\t\tSET set_after = 1;\n\t\t\tCASE _column_name"; foreach ($fields as $row) { echo "\n\t\t\t\tWHEN " . q($row["COLUMN_NAME"]) . " THEN\n\t\t\t\t\tSET add_columns = REPLACE(add_columns, ', ADD {$row['alter']}', IF(\n\t\t\t\t\t\t_column_default <=> {$row['default']} AND _is_nullable = '{$row['IS_NULLABLE']}' AND _collation_name <=> " . (isset($row["COLLATION_NAME"]) ? "'{$row['COLLATION_NAME']}'" : "NULL") . " AND _column_type = " . q($row["COLUMN_TYPE"]) . " AND _extra = '{$row['EXTRA']}' AND _column_comment = " . q($row["COLUMN_COMMENT"]) . " AND after = {$row['after']}\n\t\t\t\t\t, '', ', MODIFY {$row['alter']}'));"; //! don't replace in comment } echo "\n\t\t\t\tELSE\n\t\t\t\t\tSET @alter_table = CONCAT(@alter_table, ', DROP ', '`', REPLACE(_column_name, '`', '``'), '`');\n\t\t\t\t\tSET set_after = 0;\n\t\t\tEND CASE;\n\t\t\tIF set_after THEN\n\t\t\t\tSET after = _column_name;\n\t\t\tEND IF;\n\t\tEND IF;\n\tUNTIL done END REPEAT;\n\tCLOSE columns;\n\tIF @alter_table != '' OR add_columns != '' THEN\n\t\tSET alter_command = CONCAT(alter_command, 'ALTER TABLE " . adminer_table($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2), ';\\n');\n\tEND IF;\nEND;;\nDELIMITER ;\nCALL adminer_alter(@adminer_alter);\nDROP PROCEDURE adminer_alter;\n\n"; //! indexes } return true; } }
function editInput($table, $field, $attrs, $value) { static $foreignTables = array(); static $values = array(); $foreignKeys =& $foreignTables[$table]; if ($foreignKeys === null) { $foreignKeys = column_foreign_keys($table); } foreach ((array) $foreignKeys[$field["field"]] as $foreignKey) { if (count($foreignKey["source"]) == 1) { $target = $foreignKey["table"]; $id = $foreignKey["target"][0]; $options =& $values[$target][$id]; if (!$options) { $options = array("" => "") + get_vals("SELECT " . idf_escape($id) . " FROM " . adminer_table($target) . " ORDER BY 1"); if ($this->_limit && count($options) - 1 > $this->_limit) { return; } } return "<select{$attrs}>" . optionlist($options, $value) . "</select>"; } } }
/** Insert data into table * @param string * @param array escaped columns in keys, quoted data in values * @return bool */ function insert($table, $set) { return queries("INSERT INTO " . adminer_table($table) . ($set ? " (" . implode(", ", array_keys($set)) . ")\nVALUES (" . implode(", ", $set) . ")" : " DEFAULT VALUES")); }
/** Format foreign key to use in SQL query * @param array ("table" => string, "source" => array, "target" => array, "on_delete" => one of $on_actions, "on_update" => one of $on_actions) * @return string */ function format_foreign_key($foreign_key) { global $on_actions; return " FOREIGN KEY (" . implode(", ", array_map('idf_escape', $foreign_key["source"])) . ") REFERENCES " . adminer_table($foreign_key["table"]) . " (" . implode(", ", array_map('idf_escape', $foreign_key["target"])) . ")" . (preg_match("~^({$on_actions})\$~", $foreign_key["on_delete"]) ? " ON DELETE {$foreign_key['on_delete']}" : "") . (preg_match("~^({$on_actions})\$~", $foreign_key["on_update"]) ? " ON UPDATE {$foreign_key['on_update']}" : ""); }
function _extractIds($table, $queryWhere, $limit) { $return = array(); if (preg_match_all("~itemName\\(\\) = (('[^']*+')+)~", $queryWhere, $matches)) { $return = array_map('idf_unescape', $matches[1]); } else { foreach (sdb_request_all('Select', 'Item', array('SelectExpression' => 'SELECT itemName() FROM ' . adminer_table($table) . $queryWhere . ($limit ? " LIMIT 1" : ""))) as $item) { $return[] = $item->Name; } } return $return; }
// edit $result = true; $affected = 0; $set = array(); if (!$_POST["delete"]) { foreach ($columns as $name => $val) { //! should check also for edit or insert privileges $val = process_input($fields[$name]); if ($val !== null && ($_POST["clone"] || $val !== false)) { $set[idf_escape($name)] = $val !== false ? $val : idf_escape($name); } } } if ($_POST["delete"] || $set) { if ($_POST["clone"]) { $query = "INTO " . adminer_table($TABLE) . " (" . implode(", ", array_keys($set)) . ")\nSELECT " . implode(", ", $set) . "\nFROM " . adminer_table($TABLE); } if ($_POST["all"] || $unselected === array() && is_array($_POST["check"]) || $is_group) { $result = $_POST["delete"] ? $driver->delete($TABLE, $where_check) : ($_POST["clone"] ? queries("INSERT {$query}{$where_check}") : $driver->update($TABLE, $set, $where_check)); $affected = $connection->affected_rows; } else { foreach ((array) $_POST["check"] as $val) { // where is not unique so OR can't be used $where2 = "\nWHERE " . ($where ? implode(" AND ", $where) . " AND " : "") . where_check($val, $fields); $result = $_POST["delete"] ? $driver->delete($TABLE, $where2, 1) : ($_POST["clone"] ? queries("INSERT" . limit1($query, $where2)) : $driver->update($TABLE, $set, $where2)); if (!$result) { break; } $affected += $connection->affected_rows; } }
$location = ME . "table=" . urlencode($name); $message = lang('View has been altered.'); if ($_GET["materialized"]) { $type = "MATERIALIZED VIEW"; } else { $type = "VIEW"; if ($jush == "pgsql") { $status = table_status($name); $type = $status ? strtoupper($status["Engine"]) : $type; } } if (!$_POST["drop"] && $TABLE == $name && $jush != "sqlite" && $type != "MATERIALIZED VIEW") { query_adminer_redirect(($jush == "mssql" ? "ALTER" : "CREATE OR REPLACE") . " VIEW " . adminer_table($name) . $as, $location, $message); } else { $temp_name = $name . "_adminer_" . uniqid(); drop_create("DROP {$type} " . adminer_table($TABLE), "CREATE {$type} " . adminer_table($name) . $as, "DROP {$type} " . adminer_table($name), "CREATE {$type} " . adminer_table($temp_name) . $as, "DROP {$type} " . adminer_table($temp_name), $_POST["drop"] ? substr(ME, 0, -1) : $location, lang('View has been dropped.'), $message, lang('View has been created.'), $TABLE, $name); } } if (!$_POST && $TABLE != "") { $row = view($TABLE); $row["name"] = $TABLE; if (!$error) { $error = error(); } } page_header($TABLE != "" ? lang('Alter view') : lang('Create view'), $error, array("table" => $TABLE), h($TABLE)); ?> <form action="" method="post"> <p><?php echo lang('Name');
if ($_POST["table_style"] || $_POST["data_style"]) { $views = array(); foreach (table_status('', true) as $name => $table_status) { $table = DB == "" || in_array($name, (array) $_POST["tables"]); $data = DB == "" || in_array($name, (array) $_POST["data"]); if ($table || $data) { if ($ext == "tar") { $tmp_file = new TmpFile(); ob_start(array($tmp_file, 'write'), 100000.0); } $adminer->dumpTable($name, $table ? $_POST["table_style"] : "", is_view($table_status) ? 2 : 0); if (is_view($table_status)) { $views[] = $name; } elseif ($data) { $fields = fields($name); $adminer->dumpData($name, $_POST["data_style"], "SELECT *" . convert_fields($fields, $fields) . " FROM " . adminer_table($name)); } if ($is_sql && $_POST["triggers"] && $table && ($triggers = trigger_sql($name, $_POST["table_style"]))) { echo "\nDELIMITER ;;\n{$triggers}\nDELIMITER ;\n"; } if ($ext == "tar") { ob_end_flush(); tar_file((DB != "" ? "" : "{$db}/") . "{$name}.csv", $tmp_file); } elseif ($is_sql) { echo "\n"; } } } foreach ($views as $view) { $adminer->dumpTable($view, $_POST["table_style"], 1); }
<?php $TABLE = $_GET["trigger"]; $name = $_GET["name"]; $trigger_options = trigger_options(); $row = (array) trigger($name) + array("Trigger" => $TABLE . "_bi"); if ($_POST) { if (!$error && in_array($_POST["Timing"], $trigger_options["Timing"]) && in_array($_POST["Event"], $trigger_options["Event"]) && in_array($_POST["Type"], $trigger_options["Type"])) { // don't use drop_create() because there may not be more triggers for the same action $on = " ON " . adminer_table($TABLE); $drop = "DROP TRIGGER " . idf_escape($name) . ($jush == "pgsql" ? $on : ""); $location = ME . "table=" . urlencode($TABLE); if ($_POST["drop"]) { query_adminer_redirect($drop, $location, lang('Trigger has been dropped.')); } else { if ($name != "") { queries($drop); } queries_adminer_redirect($location, $name != "" ? lang('Trigger has been altered.') : lang('Trigger has been created.'), queries(create_trigger($on, $_POST))); if ($name != "") { queries(create_trigger($on, $row + array("Type" => reset($trigger_options["Type"])))); } } } $row = $_POST; } page_header($name != "" ? lang('Alter trigger') . ": " . h($name) : lang('Create trigger'), $error, array("table" => $TABLE)); ?> <form action="" method="post" id="form"> <table cellspacing="0">
$row = $_POST; if ($_POST && !$error && !$_POST["add"] && !$_POST["change"] && !$_POST["change-js"]) { $message = $_POST["drop"] ? lang('Foreign key has been dropped.') : ($name != "" ? lang('Foreign key has been altered.') : lang('Foreign key has been created.')); $location = ME . "table=" . urlencode($TABLE); $row["source"] = array_filter($row["source"], 'strlen'); ksort($row["source"]); // enforce input order $target = array(); foreach ($row["source"] as $key => $val) { $target[$key] = $row["target"][$key]; } $row["target"] = $target; if ($jush == "sqlite") { queries_adminer_redirect($location, $message, recreate_table($TABLE, $TABLE, array(), array(), array(" {$name}" => $_POST["drop"] ? "" : " " . format_foreign_key($row)))); } else { $alter = "ALTER TABLE " . adminer_table($TABLE); $drop = "\nDROP " . ($jush == "sql" ? "FOREIGN KEY " : "CONSTRAINT ") . idf_escape($name); if ($_POST["drop"]) { query_adminer_redirect($alter . $drop, $location, $message); } else { query_adminer_redirect($alter . ($name != "" ? "{$drop}," : "") . "\nADD" . format_foreign_key($row), $location, $message); $error = lang('Source and target columns must have the same data type, there must be an index on the target columns and referenced data must exist.') . "<br>{$error}"; //! no partitioning } } } page_header(lang('Foreign key'), $error, array("table" => $TABLE), h($TABLE)); if ($_POST) { ksort($row["source"]); if ($_POST["add"]) { $row["source"][] = "";