Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 79 lines (74 sloc) 3.755 kb
736c733 Prepare for foreign keys edit
jakubvrana authored
1 <?php
79d1902 Substitute table name to $TABLE
jakubvrana authored
2 $TABLE = $_GET["foreign"];
cbe443c Foreign keys
jakubvrana authored
3 if ($_POST && !$error && !$_POST["add"] && !$_POST["change"] && !$_POST["change-js"]) {
2a03e63 Drop and re-create at once
jakubvrana authored
4 if ($_POST["drop"]) {
60c7ed9 Jakub Vrána MS SQL schema support
authored
5 query_redirect("ALTER TABLE " . table($TABLE) . "\nDROP " . ($jush == "sql" ? "FOREIGN KEY " : "CONSTRAINT ") . idf_escape($_GET["name"]), ME . "table=" . urlencode($TABLE), lang('Foreign key has been dropped.'));
2a03e63 Drop and re-create at once
jakubvrana authored
6 } else {
c9c2a9e Don't create in case of unsuccessful drop
jakubvrana authored
7 $source = array_filter($_POST["source"], 'strlen');
64ba924 Comments
jakubvrana authored
8 ksort($source); // enforce input order
c9c2a9e Don't create in case of unsuccessful drop
jakubvrana authored
9 $target = array();
10 foreach ($source as $key => $val) {
11 $target[$key] = $_POST["target"][$key];
12 }
60c7ed9 Jakub Vrána MS SQL schema support
authored
13 query_redirect("ALTER TABLE " . table($TABLE)
db3ae28 Jakub Vrána PostgreSQL: fix alter foreign key
authored
14 . ($_GET["name"] != "" ? "\nDROP " . ($jush == "sql" ? "FOREIGN KEY " : "CONSTRAINT ") . idf_escape($_GET["name"]) . "," : "")
87888f1 Jakub Vrána Comment
authored
15 . "\nADD FOREIGN KEY (" . implode(", ", array_map('idf_escape', $source)) . ") REFERENCES " . table($_POST["table"]) . " (" . implode(", ", array_map('idf_escape', $target)) . ")" //! reuse $_GET["name"] - check in older MySQL versions
8e75d54 Jakub Vrána Save bytes
authored
16 . (ereg("^($on_actions)\$", $_POST["on_delete"]) ? " ON DELETE $_POST[on_delete]" : "")
17 . (ereg("^($on_actions)\$", $_POST["on_update"]) ? " ON UPDATE $_POST[on_update]" : "")
7352c28 Replace strlen() by != ""
jakubvrana authored
18 , ME . "table=" . urlencode($TABLE), ($_GET["name"] != "" ? lang('Foreign key has been altered.') : lang('Foreign key has been created.')));
73504b9 English corrections (thanks to crazy1729)
jakubvrana authored
19 $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
cbe443c Foreign keys
jakubvrana authored
20 }
21 }
36d6864 Whitespace
jakubvrana authored
22
79d1902 Substitute table name to $TABLE
jakubvrana authored
23 page_header(lang('Foreign key'), $error, array("table" => $TABLE), $TABLE);
736c733 Prepare for foreign keys edit
jakubvrana authored
24
79d1902 Substitute table name to $TABLE
jakubvrana authored
25 $row = array("table" => $TABLE, "source" => array(""));
cbe443c Foreign keys
jakubvrana authored
26 if ($_POST) {
27 $row = $_POST;
28 ksort($row["source"]);
29 if ($_POST["add"]) {
30 $row["source"][] = "";
31 } elseif ($_POST["change"] || $_POST["change-js"]) {
32 $row["target"] = array();
33 }
7352c28 Replace strlen() by != ""
jakubvrana authored
34 } elseif ($_GET["name"] != "") {
79d1902 Substitute table name to $TABLE
jakubvrana authored
35 $foreign_keys = foreign_keys($TABLE);
cbe443c Foreign keys
jakubvrana authored
36 $row = $foreign_keys[$_GET["name"]];
2a03e63 Drop and re-create at once
jakubvrana authored
37 $row["source"][] = "";
cbe443c Foreign keys
jakubvrana authored
38 }
aa14be0 $target uses $row
jakubvrana authored
39
5e01a62 Prepare for SQLite
jakubvrana authored
40 $source = array_keys(fields($TABLE)); //! no text and blob
41 $target = ($TABLE === $row["table"] ? $source : array_keys(fields($row["table"])));
3f5b683 Reintegrate sqlite branch
jakubvrana authored
42 $referencable = array();
43 foreach (table_status() as $name => $table_status) {
44 if (fk_support($table_status)) {
45 $referencable[] = $name;
46 }
47 }
cbe443c Foreign keys
jakubvrana authored
48 ?>
2a03e63 Drop and re-create at once
jakubvrana authored
49
cbe443c Foreign keys
jakubvrana authored
50 <form action="" method="post">
51 <p>
9e0e161 Jakub Vrána Display foreign keys from other schemas in PostgreSQL (thanks to Ondrej ...
authored
52 <?php if ($row["db"] == "" && $row["ns"] == "") { ?>
cbe443c Foreign keys
jakubvrana authored
53 <?php echo lang('Target table'); ?>:
4815c7d Jakub Vrána Don't use AJAX links and forms
authored
54 <?php echo html_select("table", $referencable, $row["table"], "this.form['change-js'].value = '1'; this.form.submit();"); ?>
ace55ed HTML instead of XHTML
jakubvrana authored
55 <input type="hidden" name="change-js" value="">
56 <noscript><p><input type="submit" name="change" value="<?php echo lang('Change'); ?>"></noscript>
d30face Remove useless table attributes (thanks to Juraj Krivda)
jakubvrana authored
57 <table cellspacing="0">
ace55ed HTML instead of XHTML
jakubvrana authored
58 <thead><tr><th><?php echo lang('Source'); ?><th><?php echo lang('Target'); ?></thead>
cbe443c Foreign keys
jakubvrana authored
59 <?php
2a03e63 Drop and re-create at once
jakubvrana authored
60 $j = 0;
cbe443c Foreign keys
jakubvrana authored
61 foreach ($row["source"] as $key => $val) {
62 echo "<tr>";
04eccba Jakub Vrána Allow bigger numbers under 32 bits
authored
63 echo "<td>" . html_select("source[" . (+$key) . "]", array(-1 => "") + $source, $val, ($j == count($row["source"]) - 1 ? "foreignAddRow(this);" : 1));
64 echo "<td>" . html_select("target[" . (+$key) . "]", $target, $row["target"][$key]);
2a03e63 Drop and re-create at once
jakubvrana authored
65 $j++;
736c733 Prepare for foreign keys edit
jakubvrana authored
66 }
cbe443c Foreign keys
jakubvrana authored
67 ?>
68 </table>
69 <p>
8e75d54 Jakub Vrána Save bytes
authored
70 <?php echo lang('ON DELETE'); ?>: <?php echo html_select("on_delete", array(-1 => "") + explode("|", $on_actions), $row["on_delete"]); ?>
71 <?php echo lang('ON UPDATE'); ?>: <?php echo html_select("on_update", array(-1 => "") + explode("|", $on_actions), $row["on_update"]); ?>
2188934 Separate ON UPDATE and ON DELETE
jakubvrana authored
72 <p>
ace55ed HTML instead of XHTML
jakubvrana authored
73 <input type="submit" value="<?php echo lang('Save'); ?>">
74 <noscript><p><input type="submit" name="add" value="<?php echo lang('Add column'); ?>"></noscript>
36c2ae7 Allow dropping remote foreign key
jakubvrana authored
75 <?php } ?>
a93bc19 Jakub Vrána Create confirm function
authored
76 <?php if ($_GET["name"] != "") { ?><input type="submit" name="drop" value="<?php echo lang('Drop'); ?>"<?php echo confirm(); ?>><?php } ?>
36c2ae7 Allow dropping remote foreign key
jakubvrana authored
77 <input type="hidden" name="token" value="<?php echo $token; ?>">
cbe443c Foreign keys
jakubvrana authored
78 </form>
Something went wrong with that request. Please try again.