Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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