Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 154 lines (147 sloc) 7.769 kB
a7096dd Separate editing functions
jakubvrana authored
1 <?php
ced9de9 Create single column foreign key in table structure
jakubvrana authored
2 function referencable_primary($self) {
3 $return = array(); // table_name => field
4 foreach (table_status_referencable() as $table_name => $table) {
5 if ($table_name != $self) {
6 foreach (fields($table_name) as $field) {
7 if ($field["primary"]) {
8 if ($return[$table_name]) { // multi column primary key
9 unset($return[$table_name]);
10 break;
11 }
12 $return[$table_name] = $field;
13 }
14 }
15 }
16 }
17 return $return;
18 }
19
20 function edit_type($key, $field, $collations, $foreign_keys = array()) {
a7096dd Separate editing functions
jakubvrana authored
21 global $types, $unsigned, $inout;
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored
22 ?>
ace55ed HTML instead of XHTML
jakubvrana authored
23 <td><select name="<?php echo $key; ?>[type]" onchange="editing_type_change(this);"><?php echo optionlist(array_keys($types) + ($foreign_keys ? array(lang('Foreign keys') => $foreign_keys) : array()), $field["type"]); ?></select>
24 <td><input name="<?php echo $key; ?>[length]" value="<?php echo htmlspecialchars($field["length"]); ?>" size="3">
493eb6b Remove space
jakubvrana authored
25 <td><?php
ace55ed HTML instead of XHTML
jakubvrana authored
26 echo "<select name=\"$key" . '[collation]"' . (ereg('(char|text|enum|set)$', $field["type"]) ? "" : " class='hidden'") . '><option value="">(' . lang('collation') . ')' . optionlist($collations, $field["collation"]) . '</select>';
27 echo ($unsigned ? " <select name=\"$key" . '[unsigned]"' . (!$field["type"] || ereg('(int|float|double|decimal)$', $field["type"]) ? "" : " class='hidden'") . '><option>' . optionlist($unsigned, $field["unsigned"]) . '</select>' : '');
28 ?>
a7096dd Separate editing functions
jakubvrana authored
29 <?php
30 }
31
a3c969c Routines uses CHARACTER SET
jakubvrana authored
32 function process_type($field, $collate = "COLLATE") {
5f16c75 Prepare for version 2
jakubvrana authored
33 global $dbh, $enum_length, $unsigned;
a7096dd Separate editing functions
jakubvrana authored
34 return " $field[type]"
5abd943 Change simple preg_match to ereg
jakubvrana authored
35 . ($field["length"] && !ereg('^date|time$', $field["type"]) ? "(" . process_length($field["length"]) . ")" : "")
36 . (ereg('int|float|double|decimal', $field["type"]) && in_array($field["unsigned"], $unsigned) ? " $field[unsigned]" : "")
37 . (ereg('char|text|enum|set', $field["type"]) && $field["collation"] ? " $collate " . $dbh->quote($field["collation"]) : "")
a7096dd Separate editing functions
jakubvrana authored
38 ;
39 }
40
ced9de9 Create single column foreign key in table structure
jakubvrana authored
41 function edit_fields($fields, $collations, $type = "TABLE", $allowed = 0, $foreign_keys = array()) {
a7096dd Separate editing functions
jakubvrana authored
42 global $inout;
540e217 Separate JavaScript
jakubvrana authored
43 $column_comments = false;
44 foreach ($fields as $field) {
45 if (strlen($field["comment"])) {
46 $column_comments = true;
47 }
48 }
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored
49 ?>
4e5b126 Highlight odd and hover rows
jakubvrana authored
50 <thead><tr>
ace55ed HTML instead of XHTML
jakubvrana authored
51 <?php if ($type == "PROCEDURE") { ?><td><?php echo lang('IN-OUT'); ?><?php } ?>
52 <th><?php echo ($type == "TABLE" ? lang('Column name') : lang('Parameter name')); ?>
53 <td><?php echo lang('Type'); ?>
54 <td><?php echo lang('Length'); ?>
55 <td><?php echo lang('Options'); ?>
a7096dd Separate editing functions
jakubvrana authored
56 <?php if ($type == "TABLE") { ?>
ace55ed HTML instead of XHTML
jakubvrana authored
57 <td><?php echo lang('NULL'); ?>
58 <td><input type="radio" name="auto_increment_col" value=""><?php echo lang('Auto Increment'); ?>
59 <td<?php echo ($column_comments ? "" : " class='hidden'"); ?>><?php echo lang('Comment'); ?>
a7096dd Separate editing functions
jakubvrana authored
60 <?php } ?>
ace55ed HTML instead of XHTML
jakubvrana authored
61 <td><?php echo "<input type='image' name='add[0]' src='../adminer/plus.gif' alt='+' title='" . lang('Add next') . "'>"; ?><script type="text/javascript">row_count = <?php echo count($fields); ?>;</script>
62 </thead>
a7096dd Separate editing functions
jakubvrana authored
63 <?php
93c27c9 Separate type_change()
jakubvrana authored
64 foreach ($fields as $i => $field) {
65 $i++;
3cd113f Procedures test
jakubvrana authored
66 $display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !$_POST["drop_col"][$i]));
93c27c9 Separate type_change()
jakubvrana authored
67 ?>
a76c555 Remove odd() from create
jakubvrana authored
68 <tr<?php echo ($display ? "" : " style='display: none;'"); ?>>
ace55ed HTML instead of XHTML
jakubvrana authored
69 <?php if ($type == "PROCEDURE") { ?><td><select name="fields[<?php echo $i; ?>][inout]"><?php echo optionlist($inout, $field["inout"]); ?></select><?php } ?>
70 <th><?php if ($display) { ?><input name="fields[<?php echo $i; ?>][field]" value="<?php echo htmlspecialchars($field["field"]); ?>" onchange="<?php echo (strlen($field["field"]) || count($fields) > 1 ? "" : "editing_add_row(this, $allowed); "); ?>editing_name_change(this);" maxlength="64"><?php } ?><input type="hidden" name="fields[<?php echo $i; ?>][orig]" value="<?php echo htmlspecialchars($field[($_POST ? "orig" : "field")]); ?>">
ced9de9 Create single column foreign key in table structure
jakubvrana authored
71 <?php edit_type("fields[$i]", $field, $collations, $foreign_keys); ?>
a7096dd Separate editing functions
jakubvrana authored
72 <?php if ($type == "TABLE") { ?>
ace55ed HTML instead of XHTML
jakubvrana authored
73 <td><input type="checkbox" name="fields[<?php echo $i; ?>][null]" value="1"<?php if ($field["null"]) { ?> checked="checked"<?php } ?>>
74 <td><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked="checked"<?php } ?>>
75 <td<?php echo ($column_comments ? "" : " class='hidden'"); ?>><input name="fields[<?php echo $i; ?>][comment]" value="<?php echo htmlspecialchars($field["comment"]); ?>" maxlength="255">
a7096dd Separate editing functions
jakubvrana authored
76 <?php } ?>
77 <?php
ace55ed HTML instead of XHTML
jakubvrana authored
78 echo "<td class='nowrap'><input type='image' name='add[$i]' src='../adminer/plus.gif' alt='+' title='" . lang('Add next') . "' onclick='var x = editing_add_row(this, $allowed); if (x) { x.focus(); x.onchange = function () { }; } return !x;'>";
79 echo "&nbsp;<input type='image' name='drop_col[$i]' src='../adminer/cross.gif' alt='x' title='" . lang('Remove') . "' onclick='return !editing_remove_row(this);'>";
80 echo "&nbsp;<input type='image' name='up[$i]' src='../adminer/up.gif' alt='^' title='" . lang('Move up') . "'>";
81 echo "&nbsp;<input type='image' name='down[$i]' src='../adminer/down.gif' alt='v' title='" . lang('Move down') . "'>";
82 echo "\n\n";
a7096dd Separate editing functions
jakubvrana authored
83 }
93c27c9 Separate type_change()
jakubvrana authored
84 return $column_comments;
a7096dd Separate editing functions
jakubvrana authored
85 }
93c27c9 Separate type_change()
jakubvrana authored
86
fd8948f Order of columns in table
jakubvrana authored
87 function process_fields(&$fields) {
88 ksort($fields);
89 $offset = 0;
90 if ($_POST["up"]) {
91 $last = 0;
92 foreach ($fields as $key => $field) {
93 if (key($_POST["up"]) == $key) {
94 unset($fields[$key]);
95 array_splice($fields, $last, 0, array($field));
96 break;
97 }
98 if (isset($field["field"])) {
99 $last = $offset;
100 }
101 $offset++;
102 }
103 }
104 if ($_POST["down"]) {
105 $found = false;
106 foreach ($fields as $key => $field) {
107 if (isset($field["field"]) && $found) {
108 unset($fields[key($_POST["down"])]);
109 array_splice($fields, $offset, 0, array($found));
110 break;
111 }
112 if (key($_POST["down"]) == $key) {
113 $found = $field;
114 }
115 $offset++;
116 }
117 }
118 $fields = array_values($fields);
119 if ($_POST["add"]) {
120 array_splice($fields, key($_POST["add"]), 0, array(array()));
121 }
122 }
123
8060c5b Routines
jakubvrana authored
124 function normalize_enum($match) {
125 return "'" . str_replace("'", "''", addcslashes(stripcslashes(str_replace($match[0]{0} . $match[0]{0}, $match[0]{0}, substr($match[0], 1, -1))), '\\')) . "'";
126 }
127
a7096dd Separate editing functions
jakubvrana authored
128 function routine($name, $type) {
5f16c75 Prepare for version 2
jakubvrana authored
129 global $dbh, $enum_length, $inout;
9fa3b9d Type aliases in routine
jakubvrana authored
130 $aliases = array("bit" => "tinyint", "bool" => "tinyint", "boolean" => "tinyint", "integer" => "int", "double precision" => "float", "real" => "float", "dec" => "decimal", "numeric" => "decimal", "fixed" => "decimal", "national char" => "char", "national varchar" => "varchar");
a9eab4c Space in type (bug 1896054)
jakubvrana authored
131 $type_pattern = "([a-z]+)(?:\\s*\\(((?:[^'\")]*|$enum_length)+)\\))?\\s*(zerofill\\s*)?(unsigned(?:\\s+zerofill)?)?(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s]+)['\"]?)?";
a7096dd Separate editing functions
jakubvrana authored
132 $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]+|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern";
5f16c75 Prepare for version 2
jakubvrana authored
133 $create = $dbh->result($dbh->query("SHOW CREATE $type " . idf_escape($name)), 2);
8060c5b Routines
jakubvrana authored
134 preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match);
a7096dd Separate editing functions
jakubvrana authored
135 $fields = array();
8060c5b Routines
jakubvrana authored
136 preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER);
a7096dd Separate editing functions
jakubvrana authored
137 foreach ($matches as $i => $param) {
9fa3b9d Type aliases in routine
jakubvrana authored
138 $data_type = strtolower($param[4]);
a7096dd Separate editing functions
jakubvrana authored
139 $fields[$i] = array(
140 "field" => str_replace("``", "`", $param[2]) . $param[3],
9fa3b9d Type aliases in routine
jakubvrana authored
141 "type" => (isset($aliases[$data_type]) ? $aliases[$data_type] : $data_type),
a7096dd Separate editing functions
jakubvrana authored
142 "length" => preg_replace_callback("~$enum_length~s", 'normalize_enum', $param[5]),
143 "unsigned" => strtolower(preg_replace('~\\s+~', ' ', trim("$param[7] $param[6]"))),
144 "inout" => strtoupper($param[1]),
8060c5b Routines
jakubvrana authored
145 "collation" => strtolower($param[8]),
a7096dd Separate editing functions
jakubvrana authored
146 );
147 }
148 if ($type != "FUNCTION") {
8060c5b Routines
jakubvrana authored
149 return array("fields" => $fields, "definition" => $match[10]);
a7096dd Separate editing functions
jakubvrana authored
150 }
8060c5b Routines
jakubvrana authored
151 $returns = array("type" => $match[10], "length" => $match[11], "unsigned" => $match[13], "collation" => $match[14]);
152 return array("fields" => $fields, "returns" => $returns, "definition" => $match[15]);
a7096dd Separate editing functions
jakubvrana authored
153 }
Something went wrong with that request. Please try again.