Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 172 lines (163 sloc) 8.29 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()) {
26b07bf Separate types to groups in table creation
jakubvrana authored
21 global $structured_types, $unsigned, $inout;
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored
22 ?>
c8dd281 Revert type group description separation for performance reasons
jakubvrana authored
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"]); ?></select>
689699a Shortcut for htmlspecialchars
jakubvrana authored
24 <td><input name="<?php echo $key; ?>[length]" value="<?php echo h($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
f93c84e Function process_length used only in Adminer
jakubvrana authored
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
37 function process_type($field, $collate = "COLLATE") {
5f16c75 Prepare for version 2
jakubvrana authored
38 global $dbh, $enum_length, $unsigned;
a7096dd Separate editing functions
jakubvrana authored
39 return " $field[type]"
5abd943 Change simple preg_match to ereg
jakubvrana authored
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
43 ;
44 }
45
3015aed Separate type_class
jakubvrana authored
46 function type_class($type) {
47 if (ereg('char|text', $type)) {
48 return " class='char'";
49 } elseif (ereg('date|time|year', $type)) {
50 return " class='date'";
51 } elseif (ereg('binary|blob', $type)) {
52 return " class='binary'";
53 } elseif (ereg('enum|set', $type)) {
54 return " class='enum'";
55 }
56 }
57
ced9de9 Create single column foreign key in table structure
jakubvrana authored
58 function edit_fields($fields, $collations, $type = "TABLE", $allowed = 0, $foreign_keys = array()) {
d31d4e9 Emulate REQUEST_URI
jakubvrana authored
59 global $inout;
540e217 Separate JavaScript
jakubvrana authored
60 $column_comments = false;
61 foreach ($fields as $field) {
62 if (strlen($field["comment"])) {
63 $column_comments = true;
64 }
65 }
f2813b5 Don't hide/show added/dropped columns (bug 1920167)
jakubvrana authored
66 ?>
4e5b126 Highlight odd and hover rows
jakubvrana authored
67 <thead><tr>
ace55ed HTML instead of XHTML
jakubvrana authored
68 <?php if ($type == "PROCEDURE") { ?><td><?php echo lang('IN-OUT'); ?><?php } ?>
69 <th><?php echo ($type == "TABLE" ? lang('Column name') : lang('Parameter name')); ?>
70 <td><?php echo lang('Type'); ?>
71 <td><?php echo lang('Length'); ?>
72 <td><?php echo lang('Options'); ?>
a7096dd Separate editing functions
jakubvrana authored
73 <?php if ($type == "TABLE") { ?>
ace55ed HTML instead of XHTML
jakubvrana authored
74 <td><?php echo lang('NULL'); ?>
75 <td><input type="radio" name="auto_increment_col" value=""><?php echo lang('Auto Increment'); ?>
76 <td<?php echo ($column_comments ? "" : " class='hidden'"); ?>><?php echo lang('Comment'); ?>
a7096dd Separate editing functions
jakubvrana authored
77 <?php } ?>
ace55ed HTML instead of XHTML
jakubvrana authored
78 <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>
79 </thead>
a7096dd Separate editing functions
jakubvrana authored
80 <?php
93c27c9 Separate type_change()
jakubvrana authored
81 foreach ($fields as $i => $field) {
82 $i++;
3cd113f Procedures test
jakubvrana authored
83 $display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !$_POST["drop_col"][$i]));
93c27c9 Separate type_change()
jakubvrana authored
84 ?>
a76c555 Remove odd() from create
jakubvrana authored
85 <tr<?php echo ($display ? "" : " style='display: none;'"); ?>>
ace55ed HTML instead of XHTML
jakubvrana authored
86 <?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
87 <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
88 <?php edit_type("fields[$i]", $field, $collations, $foreign_keys); ?>
a7096dd Separate editing functions
jakubvrana authored
89 <?php if ($type == "TABLE") { ?>
cb6d36c HTML instead of XHTML
jakubvrana authored
90 <td><input type="checkbox" name="fields[<?php echo $i; ?>][null]" value="1"<?php if ($field["null"]) { ?> checked<?php } ?>>
91 <td><input type="radio" name="auto_increment_col" value="<?php echo $i; ?>"<?php if ($field["auto_increment"]) { ?> checked<?php } ?>>
689699a Shortcut for htmlspecialchars
jakubvrana authored
92 <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
93 <?php } ?>
94 <?php
ace55ed HTML instead of XHTML
jakubvrana authored
95 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;'>";
96 echo "&nbsp;<input type='image' name='drop_col[$i]' src='../adminer/cross.gif' alt='x' title='" . lang('Remove') . "' onclick='return !editing_remove_row(this);'>";
97 echo "&nbsp;<input type='image' name='up[$i]' src='../adminer/up.gif' alt='^' title='" . lang('Move up') . "'>";
98 echo "&nbsp;<input type='image' name='down[$i]' src='../adminer/down.gif' alt='v' title='" . lang('Move down') . "'>";
99 echo "\n\n";
a7096dd Separate editing functions
jakubvrana authored
100 }
93c27c9 Separate type_change()
jakubvrana authored
101 return $column_comments;
a7096dd Separate editing functions
jakubvrana authored
102 }
93c27c9 Separate type_change()
jakubvrana authored
103
fd8948f Order of columns in table
jakubvrana authored
104 function process_fields(&$fields) {
105 ksort($fields);
106 $offset = 0;
107 if ($_POST["up"]) {
108 $last = 0;
109 foreach ($fields as $key => $field) {
110 if (key($_POST["up"]) == $key) {
111 unset($fields[$key]);
112 array_splice($fields, $last, 0, array($field));
113 break;
114 }
115 if (isset($field["field"])) {
116 $last = $offset;
117 }
118 $offset++;
119 }
120 }
121 if ($_POST["down"]) {
122 $found = false;
123 foreach ($fields as $key => $field) {
124 if (isset($field["field"]) && $found) {
125 unset($fields[key($_POST["down"])]);
126 array_splice($fields, $offset, 0, array($found));
127 break;
128 }
129 if (key($_POST["down"]) == $key) {
130 $found = $field;
131 }
132 $offset++;
133 }
134 }
135 $fields = array_values($fields);
136 if ($_POST["add"]) {
137 array_splice($fields, key($_POST["add"]), 0, array(array()));
138 }
139 }
140
8060c5b Routines
jakubvrana authored
141 function normalize_enum($match) {
142 return "'" . str_replace("'", "''", addcslashes(stripcslashes(str_replace($match[0]{0} . $match[0]{0}, $match[0]{0}, substr($match[0], 1, -1))), '\\')) . "'";
143 }
144
a7096dd Separate editing functions
jakubvrana authored
145 function routine($name, $type) {
5f16c75 Prepare for version 2
jakubvrana authored
146 global $dbh, $enum_length, $inout;
9fa3b9d Type aliases in routine
jakubvrana authored
147 $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
148 $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
149 $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]+|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern";
5f16c75 Prepare for version 2
jakubvrana authored
150 $create = $dbh->result($dbh->query("SHOW CREATE $type " . idf_escape($name)), 2);
8060c5b Routines
jakubvrana authored
151 preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match);
a7096dd Separate editing functions
jakubvrana authored
152 $fields = array();
8060c5b Routines
jakubvrana authored
153 preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER);
2184679 Use field name in routine
jakubvrana authored
154 foreach ($matches as $param) {
155 $name = str_replace("``", "`", $param[2]) . $param[3];
9fa3b9d Type aliases in routine
jakubvrana authored
156 $data_type = strtolower($param[4]);
2184679 Use field name in routine
jakubvrana authored
157 $fields[$name] = array(
158 "field" => $name,
9fa3b9d Type aliases in routine
jakubvrana authored
159 "type" => (isset($aliases[$data_type]) ? $aliases[$data_type] : $data_type),
a7096dd Separate editing functions
jakubvrana authored
160 "length" => preg_replace_callback("~$enum_length~s", 'normalize_enum', $param[5]),
161 "unsigned" => strtolower(preg_replace('~\\s+~', ' ', trim("$param[7] $param[6]"))),
162 "inout" => strtoupper($param[1]),
8060c5b Routines
jakubvrana authored
163 "collation" => strtolower($param[8]),
a7096dd Separate editing functions
jakubvrana authored
164 );
165 }
166 if ($type != "FUNCTION") {
8060c5b Routines
jakubvrana authored
167 return array("fields" => $fields, "definition" => $match[10]);
a7096dd Separate editing functions
jakubvrana authored
168 }
8060c5b Routines
jakubvrana authored
169 $returns = array("type" => $match[10], "length" => $match[11], "unsigned" => $match[13], "collation" => $match[14]);
170 return array("fields" => $fields, "returns" => $returns, "definition" => $match[15]);
a7096dd Separate editing functions
jakubvrana authored
171 }
Something went wrong with that request. Please try again.