Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 280 lines (268 sloc) 12.18 kB
a7096dd Separate editing functions
jakubvrana authored
1 <?php
471ae2b Move select function
jakubvrana authored
2 /** Print select result
3 * @param Min_Result
4 * @param Min_DB connection to examine indexes
5 * @return null
6 */
64d616c Rename get_dbh to connection
jakubvrana authored
7 function select($result, $connection2 = null) {
471ae2b Move select function
jakubvrana authored
8 if (!$result->num_rows) {
9 echo "<p class='message'>" . lang('No rows.') . "\n";
10 } else {
11 echo "<table cellspacing='0' class='nowrap'>\n";
12 $links = array(); // colno => orgtable - create links from these columns
13 $indexes = array(); // orgtable => array(column => colno) - primary keys
14 $columns = array(); // orgtable => array(column => ) - not selected columns in primary key
15 $blobs = array(); // colno => bool - display bytes for blobs
16 $types = array(); // colno => type - display char in <code>
17 odd(''); // reset odd for each result
18 for ($i=0; $row = $result->fetch_row(); $i++) {
19 if (!$i) {
20 echo "<thead><tr>";
21 for ($j=0; $j < count($row); $j++) {
22 $field = $result->fetch_field();
23 if (strlen($field->orgtable)) {
24 if (!isset($indexes[$field->orgtable])) {
25 // find primary key in each table
26 $indexes[$field->orgtable] = array();
64d616c Rename get_dbh to connection
jakubvrana authored
27 foreach (indexes($field->orgtable, $connection2) as $index) {
471ae2b Move select function
jakubvrana authored
28 if ($index["type"] == "PRIMARY") {
29 $indexes[$field->orgtable] = array_flip($index["columns"]);
30 break;
31 }
32 }
33 $columns[$field->orgtable] = $indexes[$field->orgtable];
34 }
35 if (isset($columns[$field->orgtable][$field->orgname])) {
36 unset($columns[$field->orgtable][$field->orgname]);
37 $indexes[$field->orgtable][$field->orgname] = $j;
38 $links[$j] = $field->orgtable;
39 }
40 }
41 if ($field->charsetnr == 63) {
42 $blobs[$j] = true;
43 }
44 $types[$j] = $field->type;
45 echo "<th>" . h($field->name);
46 }
47 echo "</thead>\n";
48 }
49 echo "<tr" . odd() . ">";
50 foreach ($row as $key => $val) {
51 if (!isset($val)) {
52 $val = "<i>NULL</i>";
53 } else {
54 if ($blobs[$key] && !is_utf8($val)) {
55 $val = "<i>" . lang('%d byte(s)', strlen($val)) . "</i>"; //! link to download
24801a0 Display whitespace in texts (bug #2858042)
jakubvrana authored
56 } elseif (!strlen($val)) {
471ae2b Move select function
jakubvrana authored
57 $val = "&nbsp;"; // some content to print a border
58 } else {
24801a0 Display whitespace in texts (bug #2858042)
jakubvrana authored
59 $val = whitespace(h($val));
471ae2b Move select function
jakubvrana authored
60 if ($types[$key] == 254) {
61 $val = "<code>$val</code>";
62 }
63 }
64 if (isset($links[$key]) && !$columns[$links[$key]]) {
65 $link = "edit=" . urlencode($links[$key]);
66 foreach ($indexes[$links[$key]] as $col => $j) {
67 $link .= "&where" . urlencode("[" . bracket_escape($col) . "]") . "=" . urlencode($row[$j]);
68 }
69 $val = "<a href='" . h(ME . $link) . "'>$val</a>";
70 }
71 }
72 echo "<td>$val";
73 }
74 }
75 echo "</table>\n";
76 }
77 }
78
ced9de9 Create single column foreign key in table structure
jakubvrana authored
79 function referencable_primary($self) {
80 $return = array(); // table_name => field
81 foreach (table_status_referencable() as $table_name => $table) {
82 if ($table_name != $self) {
83 foreach (fields($table_name) as $field) {
84 if ($field["primary"]) {
85 if ($return[$table_name]) { // multi column primary key
86 unset($return[$table_name]);
87 break;
88 }
89 $return[$table_name] = $field;
90 }
91 }
92 }
93 }
94 return $return;
95 }
96
97 function edit_type($key, $field, $collations, $foreign_keys = array()) {
26b07bf Separate types to groups in table creation
jakubvrana authored
98 global $structured_types, $unsigned, $inout;
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored
99 ?>
0698409 Utilize html_select
jakubvrana authored
100 <td><select name="<?php echo $key; ?>[type]" class="type" onchange="editing_type_change(this);"><?php echo optionlist($structured_types + ($foreign_keys ? array(lang('Foreign keys') => $foreign_keys) : array()), $field["type"]); ?></select>
689699a Shortcut for htmlspecialchars
jakubvrana authored
101 <td><input name="<?php echo $key; ?>[length]" value="<?php echo h($field["length"]); ?>" size="3">
493eb6b Remove space
jakubvrana authored
102 <td><?php
12c042c Function htmlspecialchars now uses ENT_QUOTES
jakubvrana authored
103 echo "<select name='$key" . "[collation]'" . (ereg('(char|text|enum|set)$', $field["type"]) ? "" : " class='hidden'") . '><option value="">(' . lang('collation') . ')' . optionlist($collations, $field["collation"]) . '</select>';
104 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
105 ?>
a7096dd Separate editing functions
jakubvrana authored
106 <?php
107 }
108
f93c84e Function process_length used only in Adminer
jakubvrana authored
109 function process_length($length) {
110 global $enum_length;
111 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));
112 }
113
a3c969c Routines uses CHARACTER SET
jakubvrana authored
114 function process_type($field, $collate = "COLLATE") {
64d616c Rename get_dbh to connection
jakubvrana authored
115 global $connection, $enum_length, $unsigned;
a7096dd Separate editing functions
jakubvrana authored
116 return " $field[type]"
5abd943 Change simple preg_match to ereg
jakubvrana authored
117 . ($field["length"] && !ereg('^date|time$', $field["type"]) ? "(" . process_length($field["length"]) . ")" : "")
118 . (ereg('int|float|double|decimal', $field["type"]) && in_array($field["unsigned"], $unsigned) ? " $field[unsigned]" : "")
64d616c Rename get_dbh to connection
jakubvrana authored
119 . (ereg('char|text|enum|set', $field["type"]) && $field["collation"] ? " $collate " . $connection->quote($field["collation"]) : "")
a7096dd Separate editing functions
jakubvrana authored
120 ;
121 }
122
2e680bc Speedup of simple alter table
jakubvrana authored
123 function process_field($field, $type_field) {
64d616c Rename get_dbh to connection
jakubvrana authored
124 global $connection;
69f8ad5 Detect unchanged timestamp
jakubvrana authored
125 $default = $field["default"] . ($field["on_update"] ? " ON UPDATE $field[on_update]" : "");
2e680bc Speedup of simple alter table
jakubvrana authored
126 return idf_escape($field["field"]) . process_type($type_field)
127 . ($field["null"] ? " NULL" : " NOT NULL") // NULL for timestamp
64d616c Rename get_dbh to connection
jakubvrana authored
128 . (!isset($field["default"]) || $field["auto_increment"] || ereg('text|blob', $field["type"]) ? "" : " DEFAULT " . ($field["type"] == "timestamp" && eregi("^CURRENT_TIMESTAMP( on update CURRENT_TIMESTAMP)?$", $default) ? $default : $connection->quote($default)))
129 . " COMMENT " . $connection->quote($field["comment"])
2e680bc Speedup of simple alter table
jakubvrana authored
130 ;
131 }
132
3015aed Separate type_class
jakubvrana authored
133 function type_class($type) {
d144e9c Save bytes
jakubvrana authored
134 foreach (array(
135 'char' => 'text',
136 'date' => 'time|year',
137 'binary' => 'blob',
138 'enum' => 'set',
139 ) as $key => $val) {
c0c7ab3 Typo
jakubvrana authored
140 if (ereg("$key|$val", $type)) {
d144e9c Save bytes
jakubvrana authored
141 return " class='$key'";
142 }
3015aed Separate type_class
jakubvrana authored
143 }
144 }
145
ced9de9 Create single column foreign key in table structure
jakubvrana authored
146 function edit_fields($fields, $collations, $type = "TABLE", $allowed = 0, $foreign_keys = array()) {
d31d4e9 Emulate REQUEST_URI
jakubvrana authored
147 global $inout;
540e217 Separate JavaScript
jakubvrana authored
148 $column_comments = false;
149 foreach ($fields as $field) {
150 if (strlen($field["comment"])) {
151 $column_comments = true;
152 }
153 }
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored
154 ?>
4e5b126 Highlight odd and hover rows
jakubvrana authored
155 <thead><tr>
b349612 Remove useless translations
jakubvrana authored
156 <?php if ($type == "PROCEDURE") { ?><td>&nbsp;<?php } ?>
ace55ed HTML instead of XHTML
jakubvrana authored
157 <th><?php echo ($type == "TABLE" ? lang('Column name') : lang('Parameter name')); ?>
158 <td><?php echo lang('Type'); ?>
159 <td><?php echo lang('Length'); ?>
160 <td><?php echo lang('Options'); ?>
a7096dd Separate editing functions
jakubvrana authored
161 <?php if ($type == "TABLE") { ?>
b349612 Remove useless translations
jakubvrana authored
162 <td>NULL
ace55ed HTML instead of XHTML
jakubvrana authored
163 <td><input type="radio" name="auto_increment_col" value=""><?php echo lang('Auto Increment'); ?>
4cead56 Edit default values directly in table creation
jakubvrana authored
164 <td class="hidden"><?php echo lang('Default values'); ?>
ace55ed HTML instead of XHTML
jakubvrana authored
165 <td<?php echo ($column_comments ? "" : " class='hidden'"); ?>><?php echo lang('Comment'); ?>
a7096dd Separate editing functions
jakubvrana authored
166 <?php } ?>
ab3c366 Separate static files
jakubvrana authored
167 <td><?php echo "<input type='image' name='add[0]' src='../adminer/static/plus.gif' alt='+' title='" . lang('Add next') . "'>"; ?><script type="text/javascript">row_count = <?php echo count($fields); ?>;</script>
ace55ed HTML instead of XHTML
jakubvrana authored
168 </thead>
a7096dd Separate editing functions
jakubvrana authored
169 <?php
93c27c9 Separate type_change()
jakubvrana authored
170 foreach ($fields as $i => $field) {
171 $i++;
3cd113f Procedures test
jakubvrana authored
172 $display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !$_POST["drop_col"][$i]));
93c27c9 Separate type_change()
jakubvrana authored
173 ?>
a76c555 Remove odd() from create
jakubvrana authored
174 <tr<?php echo ($display ? "" : " style='display: none;'"); ?>>
0698409 Utilize html_select
jakubvrana authored
175 <?php
176 if ($type == "PROCEDURE") {
177 echo "<td>" . html_select("fields[$i][inout]", $inout, $field["inout"]);
178 }
179 ?>
689699a Shortcut for htmlspecialchars
jakubvrana authored
180 <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
181 <?php edit_type("fields[$i]", $field, $collations, $foreign_keys); ?>
a7096dd Separate editing functions
jakubvrana authored
182 <?php if ($type == "TABLE") { ?>
6b30cfa Separate checkbox
jakubvrana authored
183 <td><?php echo checkbox("fields[$i][null]", 1, $field["null"]); ?>
cb6d36c HTML instead of XHTML
jakubvrana authored
184 <td><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked<?php } ?>>
6b30cfa Separate checkbox
jakubvrana authored
185 <td class="nowrap hidden"><?php echo checkbox("fields[$i][has_default]", 1, $field["has_default"]); ?><input name="fields[<?php echo $i; ?>][default]" value="<?php echo h($field["default"]); ?>" onchange="this.previousSibling.checked = true;">
689699a Shortcut for htmlspecialchars
jakubvrana authored
186 <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
187 <?php } ?>
188 <?php
8ddbf43 Don't remove onchange after click on add
jakubvrana authored
189 echo "<td class='nowrap'><input type='image' name='add[$i]' src='../adminer/static/plus.gif' alt='+' title='" . lang('Add next') . "' onclick='return !editing_add_row(this, $allowed, 1);'>";
ab3c366 Separate static files
jakubvrana authored
190 echo "&nbsp;<input type='image' name='drop_col[$i]' src='../adminer/static/cross.gif' alt='x' title='" . lang('Remove') . "' onclick='return !editing_remove_row(this);'>";
191 echo "&nbsp;<input type='image' name='up[$i]' src='../adminer/static/up.gif' alt='^' title='" . lang('Move up') . "'>";
192 echo "&nbsp;<input type='image' name='down[$i]' src='../adminer/static/down.gif' alt='v' title='" . lang('Move down') . "'>";
ace55ed HTML instead of XHTML
jakubvrana authored
193 echo "\n\n";
a7096dd Separate editing functions
jakubvrana authored
194 }
93c27c9 Separate type_change()
jakubvrana authored
195 return $column_comments;
a7096dd Separate editing functions
jakubvrana authored
196 }
93c27c9 Separate type_change()
jakubvrana authored
197
fd8948f Order of columns in table
jakubvrana authored
198 function process_fields(&$fields) {
199 ksort($fields);
200 $offset = 0;
201 if ($_POST["up"]) {
202 $last = 0;
203 foreach ($fields as $key => $field) {
204 if (key($_POST["up"]) == $key) {
205 unset($fields[$key]);
206 array_splice($fields, $last, 0, array($field));
207 break;
208 }
209 if (isset($field["field"])) {
210 $last = $offset;
211 }
212 $offset++;
213 }
214 }
215 if ($_POST["down"]) {
216 $found = false;
217 foreach ($fields as $key => $field) {
218 if (isset($field["field"]) && $found) {
219 unset($fields[key($_POST["down"])]);
220 array_splice($fields, $offset, 0, array($found));
221 break;
222 }
223 if (key($_POST["down"]) == $key) {
224 $found = $field;
225 }
226 $offset++;
227 }
228 }
229 $fields = array_values($fields);
230 if ($_POST["add"]) {
231 array_splice($fields, key($_POST["add"]), 0, array(array()));
232 }
233 }
234
8060c5b Routines
jakubvrana authored
235 function normalize_enum($match) {
236 return "'" . str_replace("'", "''", addcslashes(stripcslashes(str_replace($match[0]{0} . $match[0]{0}, $match[0]{0}, substr($match[0], 1, -1))), '\\')) . "'";
237 }
238
a7096dd Separate editing functions
jakubvrana authored
239 function routine($name, $type) {
64d616c Rename get_dbh to connection
jakubvrana authored
240 global $connection, $enum_length, $inout, $types;
9fa3b9d Type aliases in routine
jakubvrana authored
241 $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");
79c70ad Change regular expression (bug #2859386)
jakubvrana authored
242 $type_pattern = "(" . implode("|", array_keys($types + $aliases)) . ")(?:\\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
243 $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern";
64d616c Rename get_dbh to connection
jakubvrana authored
244 $create = $connection->result($connection->query("SHOW CREATE $type " . idf_escape($name)), 2);
8060c5b Routines
jakubvrana authored
245 preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match);
a7096dd Separate editing functions
jakubvrana authored
246 $fields = array();
8060c5b Routines
jakubvrana authored
247 preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER);
2184679 Use field name in routine
jakubvrana authored
248 foreach ($matches as $param) {
249 $name = str_replace("``", "`", $param[2]) . $param[3];
9fa3b9d Type aliases in routine
jakubvrana authored
250 $data_type = strtolower($param[4]);
2184679 Use field name in routine
jakubvrana authored
251 $fields[$name] = array(
252 "field" => $name,
9fa3b9d Type aliases in routine
jakubvrana authored
253 "type" => (isset($aliases[$data_type]) ? $aliases[$data_type] : $data_type),
a7096dd Separate editing functions
jakubvrana authored
254 "length" => preg_replace_callback("~$enum_length~s", 'normalize_enum', $param[5]),
255 "unsigned" => strtolower(preg_replace('~\\s+~', ' ', trim("$param[7] $param[6]"))),
256 "inout" => strtoupper($param[1]),
8060c5b Routines
jakubvrana authored
257 "collation" => strtolower($param[8]),
a7096dd Separate editing functions
jakubvrana authored
258 );
259 }
260 if ($type != "FUNCTION") {
8060c5b Routines
jakubvrana authored
261 return array("fields" => $fields, "definition" => $match[10]);
a7096dd Separate editing functions
jakubvrana authored
262 }
8060c5b Routines
jakubvrana authored
263 $returns = array("type" => $match[10], "length" => $match[11], "unsigned" => $match[13], "collation" => $match[14]);
264 return array("fields" => $fields, "returns" => $returns, "definition" => $match[15]);
a7096dd Separate editing functions
jakubvrana authored
265 }
464d84a Define functions unconditionally
jakubvrana authored
266
267 function grant($grant, $privileges, $columns, $on) {
268 if (!$privileges) {
269 return true;
270 }
271 if ($privileges == array("ALL PRIVILEGES", "GRANT OPTION")) {
272 // can't be granted or revoked together
273 return ($grant == "GRANT"
274 ? queries("$grant ALL PRIVILEGES$on WITH GRANT OPTION")
275 : queries("$grant ALL PRIVILEGES$on") && queries("$grant GRANT OPTION$on")
276 );
277 }
278 return queries("$grant " . preg_replace('~(GRANT OPTION)\\([^)]*\\)~', '\\1', implode("$columns, ", $privileges) . $columns) . $on);
279 }
Something went wrong with that request. Please try again.