cache table information
[phpmyadmin.git] / tbl_indexes.php
blob48435d6fa4b5d477bd3cd0dc56f6c82e8486e9d4
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays index edit/creation form and handles it
6 * @version $Id$
7 */
9 /**
10 * Gets some core libraries
12 require_once './libraries/common.inc.php';
13 require_once './libraries/Index.class.php';
14 require_once './libraries/tbl_common.php';
16 // Get fields and stores their name/type
17 $fields = array();
18 foreach (PMA_DBI_get_fields($db, $table) as $row) {
19 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
20 $tmp[2] = substr(preg_replace('@([^,])\'\'@', '\\1\\\'',
21 ',' . $tmp[2]), 1);
22 $fields[$row['Field']] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
23 } else {
24 $fields[$row['Field']] = $row['Type'];
26 } // end while
28 // Prepares the form values
29 if (isset($_REQUEST['index'])) {
30 if (is_array($_REQUEST['index'])) {
31 // coming already from form
32 $index = new PMA_Index($_REQUEST['index']);
33 } else {
34 $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
36 } else {
37 $index = new PMA_Index;
40 /**
41 * Process the data from the edit/create index form,
42 * run the query to build the new index
43 * and moves back to "tbl_sql.php"
45 if (isset($_REQUEST['do_save_data'])) {
46 $error = false;
48 // $sql_query is the one displayed in the query box
49 $sql_query = 'ALTER TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table);
51 // Drops the old index
52 if (! empty($_REQUEST['old_index'])) {
53 if ($_REQUEST['old_index'] == 'PRIMARY') {
54 $sql_query .= ' DROP PRIMARY KEY,';
55 } else {
56 $sql_query .= ' DROP INDEX ' . PMA_backquote($_REQUEST['old_index']) . ',';
58 } // end if
60 // Builds the new one
61 switch ($index->getType()) {
62 case 'PRIMARY':
63 if ($index->getName() == '') {
64 $index->setName('PRIMARY');
65 } elseif ($index->getName() != 'PRIMARY') {
66 $error = PMA_Message::error('strPrimaryKeyName');
68 $sql_query .= ' ADD PRIMARY KEY';
69 break;
70 case 'FULLTEXT':
71 case 'UNIQUE':
72 case 'INDEX':
73 if ($index->getName() == 'PRIMARY') {
74 $error = PMA_Message::error('strCantRenameIdxToPrimary');
76 $sql_query .= ' ADD ' . $index->getType() . ' '
77 . ($index->getName() ? PMA_backquote($index->getName()) : '');
78 break;
79 } // end switch
81 $index_fields = array();
82 foreach ($index->getColumns() as $key => $column) {
83 $index_fields[$key] = PMA_backquote($column->getName());
84 if ($column->getSubPart()) {
85 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
87 } // end while
89 if (empty($index_fields)){
90 $error = PMA_Message::error('strNoIndexPartsDefined');
91 } else {
92 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
95 if (! $error) {
96 PMA_DBI_query($sql_query);
97 $message = PMA_Message::success('strTableAlteredSuccessfully');
98 $message->addParam($table);
100 $active_page = 'tbl_structure.php';
101 require './tbl_structure.php';
102 exit;
103 } else {
104 $error->display();
106 } // end builds the new index
110 * Display the form to edit/create an index
113 // Displays headers (if needed)
114 $GLOBALS['js_include'][] = 'functions.js';
115 $GLOBALS['js_include'][] = 'indexes.js';
117 require_once './libraries/tbl_info.inc.php';
118 require_once './libraries/tbl_links.inc.php';
120 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
121 // coming already from form
122 $add_fields =
123 count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
124 if (isset($_REQUEST['add_fields'])) {
125 $add_fields += $_REQUEST['added_fields'];
127 } elseif (isset($_REQUEST['create_index'])) {
128 $add_fields = $_REQUEST['added_fields'];
129 } else {
130 $add_fields = 1;
133 // end preparing form values
136 <form action="./tbl_indexes.php" method="post" name="index_frm"
137 onsubmit="if (typeof(this.elements['index'].disabled) != 'undefined') {
138 this.elements['index'].disabled = false}">
139 <?php
140 $form_params = array(
141 'db' => $db,
142 'table' => $table,
145 if (isset($_REQUEST['create_index'])) {
146 $form_params['create_index'] = 1;
147 } elseif (isset($_REQUEST['old_index'])) {
148 $form_params['old_index'] = $_REQUEST['old_index'];
149 } elseif (isset($_REQUEST['index'])) {
150 $form_params['old_index'] = $_REQUEST['index'];
153 echo PMA_generate_common_hidden_inputs($form_params);
155 <fieldset>
156 <legend>
157 <?php
158 echo (isset($_REQUEST['create_index'])
159 ? $strCreateIndexTopic
160 : $strModifyIndexTopic);
162 </legend>
164 <div class="formelement">
165 <label for="input_index_name"><?php echo $strIndexName; ?></label>
166 <input type="text" name="index[Key_name]" id="input_index_name" size="25"
167 value="<?php echo htmlspecialchars($index->getName()); ?>" onfocus="this.select()" />
168 </div>
170 <div class="formelement">
171 <label for="select_index_type"><?php echo $strIndexType; ?></label>
172 <select name="index[Index_type]" id="select_index_type" onchange="return checkIndexName()">
173 <?php echo $index->getTypeSelector(); ?>
174 </select>
175 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
176 </div>
179 <br class="clearfloat" />
180 <?php
181 PMA_Message::warning('strPrimaryKeyWarning')->display();
184 <table>
185 <thead>
186 <tr><th><?php echo $strField; ?></th>
187 <th><?php echo $strSize; ?></th>
188 </tr>
189 </thead>
190 <tbody>
191 <?php
192 $odd_row = true;
193 foreach ($index->getColumns() as $column) {
195 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
196 <td><select name="index[columns][names][]">
197 <option value="">-- <?php echo $strIgnore; ?> --</option>
198 <?php
199 foreach ($fields as $field_name => $field_type) {
200 if ($index->getType() != 'FULLTEXT'
201 || preg_match('/(char|text)/i', $field_type)) {
202 echo '<option value="' . htmlspecialchars($field_name) . '"'
203 . (($field_name == $column->getName()) ? ' selected="selected"' : '') . '>'
204 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
205 . '</option>' . "\n";
207 } // end foreach $fields
209 </select>
210 </td>
211 <td><input type="text" size="5" onfocus="this.select()"
212 name="index[columns][sub_parts][]" value="<?php echo $column->getSubPart(); ?>" />
213 </td>
214 </tr>
215 <?php
216 $odd_row = !$odd_row;
217 } // end foreach $edited_index_info['Sequences']
218 for ($i = 0; $i < $add_fields; $i++) {
220 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?>">
221 <td><select name="index[columns][names][]">
222 <option value="">-- <?php echo $strIgnore; ?> --</option>
223 <?php
224 foreach ($fields as $field_name => $field_type) {
225 echo '<option value="' . htmlspecialchars($field_name) . '">'
226 . htmlspecialchars($field_name) . ' [' . $field_type . ']'
227 . '</option>' . "\n";
228 } // end foreach $fields
230 </select>
231 </td>
232 <td><input type="text" size="5" onfocus="this.select()"
233 name="index[columns][sub_parts][]" value="" />
234 </td>
235 </tr>
236 <?php
237 $odd_row = !$odd_row;
238 } // end foreach $edited_index_info['Sequences']
240 </tbody>
241 </table>
242 </fieldset>
244 <fieldset class="tblFooters">
245 <input type="submit" name="do_save_data" value="<?php echo $strSave; ?>" />
246 <?php
247 echo $strOr . ' ';
248 echo sprintf($strAddToIndex,
249 '<input type="text" name="added_fields" size="2" value="1"'
250 .' onfocus="this.select()" />') . "\n";
251 echo '<input type="submit" name="add_fields" value="' . $strGo . '"'
252 .' onclick="return checkFormElementInRange(this.form,'
253 ." 'added_fields', '" . PMA_jsFormat($GLOBALS['strInvalidColumnCount']) . "', 1"
254 .')" />' . "\n";
256 </fieldset>
257 </form>
258 <?php
261 * Displays the footer
263 require_once './libraries/footer.inc.php';