Skip to content
Newer
Older
100644 197 lines (186 sloc) 9.5 KB
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
1 <?php
ced9de9 Create single column foreign key in table structure
jakubvrana authored Jul 2, 2009
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()) {
26b07bf Separate types to groups in table creation
jakubvrana authored Jul 14, 2009
21 global $structured_types, $unsigned, $inout;
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored Mar 20, 2008
22 ?>
464d84a Define functions unconditionally
jakubvrana authored Sep 9, 2009
23 <td><select name="<?php echo $key; ?>[type]" onchange="editing_type_change(this);"><?php echo optionlist($structured_types + ($foreign_keys ? array(lang('Foreign keys') => $foreign_keys) : array()), $field["type"]); // foreign keys can be wide but style="width: 15ex;" narrows expanded optionlist in IE too ?></select>
689699a Shortcut for htmlspecialchars
jakubvrana authored Jul 28, 2009
24 <td><input name="<?php echo $key; ?>[length]" value="<?php echo h($field["length"]); ?>" size="3">
493eb6b Remove space
jakubvrana authored Jun 19, 2009
25 <td><?php
12c042c Function htmlspecialchars now uses ENT_QUOTES
jakubvrana authored Jul 30, 2009
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>' : '');
ace55ed HTML instead of XHTML
jakubvrana authored Jul 11, 2009
28 ?>
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
29 <?php
30 }
31
f93c84e Function process_length used only in Adminer
jakubvrana authored Jul 27, 2009
32 function process_length($length) {
33 global $enum_length;
34 return (preg_match("~^\\s*(?:$enum_length)(?:\\s*,\\s*(?:$enum_length))*\\s*\$~", $length) && preg_match_all("~$enum_length~", $length, $matches) ? implode(",", $matches[0]) : preg_replace('~[^0-9,+-]~', '', $length));
35 }
36
a3c969c Routines uses CHARACTER SET
jakubvrana authored Jul 17, 2007
37 function process_type($field, $collate = "COLLATE") {
5f16c75 Prepare for version 2
jakubvrana authored May 8, 2009
38 global $dbh, $enum_length, $unsigned;
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
39 return " $field[type]"
5abd943 Change simple preg_match to ereg
jakubvrana authored Jul 13, 2009
40 . ($field["length"] && !ereg('^date|time$', $field["type"]) ? "(" . process_length($field["length"]) . ")" : "")
41 . (ereg('int|float|double|decimal', $field["type"]) && in_array($field["unsigned"], $unsigned) ? " $field[unsigned]" : "")
42 . (ereg('char|text|enum|set', $field["type"]) && $field["collation"] ? " $collate " . $dbh->quote($field["collation"]) : "")
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
43 ;
44 }
45
2e680bc Speedup of simple alter table
jakubvrana authored Sep 9, 2009
46 function process_field($field, $type_field) {
47 global $dbh;
48 return idf_escape($field["field"]) . process_type($type_field)
49 . ($field["null"] ? " NULL" : " NOT NULL") // NULL for timestamp
50 . (!$field["has_default"] || $field["auto_increment"] || ereg('text|blob', $field["type"]) ? "" : " DEFAULT " . ($field["type"] == "timestamp" && eregi("^CURRENT_TIMESTAMP( on update CURRENT_TIMESTAMP)?$", $field["default"]) ? $field["default"] : $dbh->quote($field["default"])))
51 . " COMMENT " . $dbh->quote($field["comment"])
52 ;
53 }
54
3015aed Separate type_class
jakubvrana authored Jul 14, 2009
55 function type_class($type) {
56 if (ereg('char|text', $type)) {
57 return " class='char'";
58 } elseif (ereg('date|time|year', $type)) {
59 return " class='date'";
60 } elseif (ereg('binary|blob', $type)) {
61 return " class='binary'";
62 } elseif (ereg('enum|set', $type)) {
63 return " class='enum'";
64 }
65 }
66
ced9de9 Create single column foreign key in table structure
jakubvrana authored Jul 2, 2009
67 function edit_fields($fields, $collations, $type = "TABLE", $allowed = 0, $foreign_keys = array()) {
d31d4e9 Emulate REQUEST_URI
jakubvrana authored Jul 28, 2009
68 global $inout;
540e217 Separate JavaScript
jakubvrana authored May 29, 2009
69 $column_comments = false;
70 foreach ($fields as $field) {
71 if (strlen($field["comment"])) {
72 $column_comments = true;
73 }
74 }
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored Mar 20, 2008
75 ?>
4e5b126 Highlight odd and hover rows
jakubvrana authored Apr 30, 2009
76 <thead><tr>
b349612 Remove useless translations
jakubvrana authored Aug 25, 2009
77 <?php if ($type == "PROCEDURE") { ?><td>&nbsp;<?php } ?>
ace55ed HTML instead of XHTML
jakubvrana authored Jul 11, 2009
78 <th><?php echo ($type == "TABLE" ? lang('Column name') : lang('Parameter name')); ?>
79 <td><?php echo lang('Type'); ?>
80 <td><?php echo lang('Length'); ?>
81 <td><?php echo lang('Options'); ?>
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
82 <?php if ($type == "TABLE") { ?>
b349612 Remove useless translations
jakubvrana authored Aug 25, 2009
83 <td>NULL
ace55ed HTML instead of XHTML
jakubvrana authored Jul 11, 2009
84 <td><input type="radio" name="auto_increment_col" value=""><?php echo lang('Auto Increment'); ?>
4cead56 Edit default values directly in table creation
jakubvrana authored Aug 21, 2009
85 <td class="hidden"><?php echo lang('Default values'); ?>
ace55ed HTML instead of XHTML
jakubvrana authored Jul 11, 2009
86 <td<?php echo ($column_comments ? "" : " class='hidden'"); ?>><?php echo lang('Comment'); ?>
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
87 <?php } ?>
ace55ed HTML instead of XHTML
jakubvrana authored Jul 11, 2009
88 <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>
89 </thead>
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
90 <?php
93c27c9 Separate type_change()
jakubvrana authored Jul 16, 2007
91 foreach ($fields as $i => $field) {
92 $i++;
3cd113f Procedures test
jakubvrana authored Oct 3, 2008
93 $display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !$_POST["drop_col"][$i]));
93c27c9 Separate type_change()
jakubvrana authored Jul 16, 2007
94 ?>
a76c555 Remove odd() from create
jakubvrana authored Jun 11, 2009
95 <tr<?php echo ($display ? "" : " style='display: none;'"); ?>>
ace55ed HTML instead of XHTML
jakubvrana authored Jul 11, 2009
96 <?php if ($type == "PROCEDURE") { ?><td><select name="fields[<?php echo $i; ?>][inout]"><?php echo optionlist($inout, $field["inout"]); ?></select><?php } ?>
689699a Shortcut for htmlspecialchars
jakubvrana authored Jul 28, 2009
97 <th><?php if ($display) { ?><input name="fields[<?php echo $i; ?>][field]" value="<?php echo h($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 h($field[($_POST ? "orig" : "field")]); ?>">
ced9de9 Create single column foreign key in table structure
jakubvrana authored Jul 2, 2009
98 <?php edit_type("fields[$i]", $field, $collations, $foreign_keys); ?>
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
99 <?php if ($type == "TABLE") { ?>
cb6d36c HTML instead of XHTML
jakubvrana authored Jul 28, 2009
100 <td><input type="checkbox" name="fields[<?php echo $i; ?>][null]" value="1"<?php if ($field["null"]) { ?> checked<?php } ?>>
101 <td><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked<?php } ?>>
4cead56 Edit default values directly in table creation
jakubvrana authored Aug 21, 2009
102 <td class="nowrap hidden"><input type="checkbox" name="fields[<?php echo $i; ?>][has_default]" value="1"<?php echo ($field["has_default"] ? " checked" : ""); ?>><input name="fields[<?php echo $i; ?>][default]" value="<?php echo h($field["default"]); ?>" onchange="this.previousSibling.checked = true;">
689699a Shortcut for htmlspecialchars
jakubvrana authored Jul 28, 2009
103 <td<?php echo ($column_comments ? "" : " class='hidden'"); ?>><input name="fields[<?php echo $i; ?>][comment]" value="<?php echo h($field["comment"]); ?>" maxlength="255">
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
104 <?php } ?>
105 <?php
ace55ed HTML instead of XHTML
jakubvrana authored Jul 11, 2009
106 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;'>";
107 echo "&nbsp;<input type='image' name='drop_col[$i]' src='../adminer/cross.gif' alt='x' title='" . lang('Remove') . "' onclick='return !editing_remove_row(this);'>";
108 echo "&nbsp;<input type='image' name='up[$i]' src='../adminer/up.gif' alt='^' title='" . lang('Move up') . "'>";
109 echo "&nbsp;<input type='image' name='down[$i]' src='../adminer/down.gif' alt='v' title='" . lang('Move down') . "'>";
110 echo "\n\n";
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
111 }
93c27c9 Separate type_change()
jakubvrana authored Jul 16, 2007
112 return $column_comments;
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
113 }
93c27c9 Separate type_change()
jakubvrana authored Jul 16, 2007
114
fd8948f Order of columns in table
jakubvrana authored May 7, 2008
115 function process_fields(&$fields) {
116 ksort($fields);
117 $offset = 0;
118 if ($_POST["up"]) {
119 $last = 0;
120 foreach ($fields as $key => $field) {
121 if (key($_POST["up"]) == $key) {
122 unset($fields[$key]);
123 array_splice($fields, $last, 0, array($field));
124 break;
125 }
126 if (isset($field["field"])) {
127 $last = $offset;
128 }
129 $offset++;
130 }
131 }
132 if ($_POST["down"]) {
133 $found = false;
134 foreach ($fields as $key => $field) {
135 if (isset($field["field"]) && $found) {
136 unset($fields[key($_POST["down"])]);
137 array_splice($fields, $offset, 0, array($found));
138 break;
139 }
140 if (key($_POST["down"]) == $key) {
141 $found = $field;
142 }
143 $offset++;
144 }
145 }
146 $fields = array_values($fields);
147 if ($_POST["add"]) {
148 array_splice($fields, key($_POST["add"]), 0, array(array()));
149 }
150 }
151
8060c5b Routines
jakubvrana authored Jul 17, 2007
152 function normalize_enum($match) {
153 return "'" . str_replace("'", "''", addcslashes(stripcslashes(str_replace($match[0]{0} . $match[0]{0}, $match[0]{0}, substr($match[0], 1, -1))), '\\')) . "'";
154 }
155
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
156 function routine($name, $type) {
5f16c75 Prepare for version 2
jakubvrana authored May 8, 2009
157 global $dbh, $enum_length, $inout;
9fa3b9d Type aliases in routine
jakubvrana authored Aug 16, 2007
158 $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 Mar 20, 2008
159 $type_pattern = "([a-z]+)(?:\\s*\\(((?:[^'\")]*|$enum_length)+)\\))?\\s*(zerofill\\s*)?(unsigned(?:\\s+zerofill)?)?(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s]+)['\"]?)?";
26c9d64 Fix long SQL query crash (bug #2839231)
jakubvrana authored Aug 19, 2009
160 $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern";
5f16c75 Prepare for version 2
jakubvrana authored May 8, 2009
161 $create = $dbh->result($dbh->query("SHOW CREATE $type " . idf_escape($name)), 2);
8060c5b Routines
jakubvrana authored Jul 17, 2007
162 preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match);
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
163 $fields = array();
8060c5b Routines
jakubvrana authored Jul 17, 2007
164 preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER);
2184679 Use field name in routine
jakubvrana authored Jul 24, 2009
165 foreach ($matches as $param) {
166 $name = str_replace("``", "`", $param[2]) . $param[3];
9fa3b9d Type aliases in routine
jakubvrana authored Aug 16, 2007
167 $data_type = strtolower($param[4]);
2184679 Use field name in routine
jakubvrana authored Jul 24, 2009
168 $fields[$name] = array(
169 "field" => $name,
9fa3b9d Type aliases in routine
jakubvrana authored Aug 16, 2007
170 "type" => (isset($aliases[$data_type]) ? $aliases[$data_type] : $data_type),
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
171 "length" => preg_replace_callback("~$enum_length~s", 'normalize_enum', $param[5]),
172 "unsigned" => strtolower(preg_replace('~\\s+~', ' ', trim("$param[7] $param[6]"))),
173 "inout" => strtoupper($param[1]),
8060c5b Routines
jakubvrana authored Jul 17, 2007
174 "collation" => strtolower($param[8]),
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
175 );
176 }
177 if ($type != "FUNCTION") {
8060c5b Routines
jakubvrana authored Jul 17, 2007
178 return array("fields" => $fields, "definition" => $match[10]);
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
179 }
8060c5b Routines
jakubvrana authored Jul 17, 2007
180 $returns = array("type" => $match[10], "length" => $match[11], "unsigned" => $match[13], "collation" => $match[14]);
181 return array("fields" => $fields, "returns" => $returns, "definition" => $match[15]);
a7096dd Separate editing functions
jakubvrana authored Jul 16, 2007
182 }
464d84a Define functions unconditionally
jakubvrana authored Sep 9, 2009
183
184 function grant($grant, $privileges, $columns, $on) {
185 if (!$privileges) {
186 return true;
187 }
188 if ($privileges == array("ALL PRIVILEGES", "GRANT OPTION")) {
189 // can't be granted or revoked together
190 return ($grant == "GRANT"
191 ? queries("$grant ALL PRIVILEGES$on WITH GRANT OPTION")
192 : queries("$grant ALL PRIVILEGES$on") && queries("$grant GRANT OPTION$on")
193 );
194 }
195 return queries("$grant " . preg_replace('~(GRANT OPTION)\\([^)]*\\)~', '\\1', implode("$columns, ", $privileges) . $columns) . $on);
196 }
Something went wrong with that request. Please try again.