Better descriptions for Drizzle column types
[phpmyadmin.git] / tbl_indexes.php
blob6a47419fd029b347468922aa382e065d077eaf8f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Displays index edit/creation form and handles it
6 * @package PhpMyAdmin
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_columns_full($db, $table) as $row) {
19 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
20 $tmp[2] = substr(
21 preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1
23 $fields[$row['Field']] = $tmp[1] . '(' . str_replace(',', ', ', $tmp[2]) . ')';
24 } else {
25 $fields[$row['Field']] = $row['Type'];
27 } // end while
29 // Prepares the form values
30 if (isset($_REQUEST['index'])) {
31 if (is_array($_REQUEST['index'])) {
32 // coming already from form
33 $index = new PMA_Index($_REQUEST['index']);
34 } else {
35 $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
37 } else {
38 $index = new PMA_Index;
41 /**
42 * Process the data from the edit/create index form,
43 * run the query to build the new index
44 * and moves back to "tbl_sql.php"
46 if (isset($_REQUEST['do_save_data'])) {
47 $error = false;
49 // $sql_query is the one displayed in the query box
50 $sql_query = 'ALTER TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ';';
52 // Drops the old index
53 if (! empty($_REQUEST['old_index'])) {
54 if ($_REQUEST['old_index'] == 'PRIMARY') {
55 $sql_query .= ' DROP PRIMARY KEY,';
56 } else {
57 $sql_query .= ' DROP INDEX ' . PMA_backquote($_REQUEST['old_index']) . ',';
59 } // end if
61 // Builds the new one
62 switch ($index->getType()) {
63 case 'PRIMARY':
64 if ($index->getName() == '') {
65 $index->setName('PRIMARY');
66 } elseif ($index->getName() != 'PRIMARY') {
67 $error = PMA_Message::error(__('The name of the primary key must be "PRIMARY"!'));
69 $sql_query .= ' ADD PRIMARY KEY';
70 break;
71 case 'FULLTEXT':
72 case 'UNIQUE':
73 case 'INDEX':
74 case 'SPATIAL':
75 if ($index->getName() == 'PRIMARY') {
76 $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!'));
78 $sql_query .= ' ADD ' . $index->getType() . ' '
79 . ($index->getName() ? PMA_backquote($index->getName()) : '');
80 break;
81 } // end switch
83 $index_fields = array();
84 foreach ($index->getColumns() as $key => $column) {
85 $index_fields[$key] = PMA_backquote($column->getName());
86 if ($column->getSubPart()) {
87 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
89 } // end while
91 if (empty($index_fields)) {
92 $error = PMA_Message::error(__('No index parts defined!'));
93 } else {
94 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
97 if (! $error) {
98 PMA_DBI_query($sql_query);
99 $message = PMA_Message::success(__('Table %1$s has been altered successfully'));
100 $message->addParam($table);
102 if ( $GLOBALS['is_ajax_request'] == true) {
103 $extra_data['index_table'] = PMA_Index::getView($table, $db);
104 $extra_data['sql_query'] = PMA_showMessage(null, $sql_query);
105 PMA_ajaxResponse($message, $message->isSuccess(), $extra_data);
108 $active_page = 'tbl_structure.php';
109 include 'tbl_structure.php';
110 exit;
111 } else {
112 if ( $GLOBALS['is_ajax_request'] == true) {
113 $extra_data['error'] = $error;
114 PMA_ajaxResponse($error, false);
116 $error->display();
118 } // end builds the new index
122 * Display the form to edit/create an index
125 // Displays headers (if needed)
126 $GLOBALS['js_include'][] = 'indexes.js';
127 require_once 'libraries/tbl_info.inc.php';
128 if ($GLOBALS['is_ajax_request'] != true) {
129 include_once 'libraries/tbl_links.inc.php';
132 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
133 // coming already from form
134 $add_fields
135 = count($_REQUEST['index']['columns']['names']) - $index->getColumnCount();
136 if (isset($_REQUEST['add_fields'])) {
137 $add_fields += $_REQUEST['added_fields'];
139 } elseif (isset($_REQUEST['create_index'])) {
140 $add_fields = $_REQUEST['added_fields'];
141 } else {
142 $add_fields = 1;
145 // end preparing form values
148 <form action="tbl_indexes.php" method="post" name="index_frm" id="index_frm" <?php echo ($GLOBALS['cfg']['AjaxEnable'] ? ' class="ajax"' : ''); ?>
149 onsubmit="if (typeof(this.elements['index[Key_name]'].disabled) != 'undefined') {
150 this.elements['index[Key_name]'].disabled = false}">
151 <?php
152 $form_params = array(
153 'db' => $db,
154 'table' => $table,
157 if (isset($_REQUEST['create_index'])) {
158 $form_params['create_index'] = 1;
159 } elseif (isset($_REQUEST['old_index'])) {
160 $form_params['old_index'] = $_REQUEST['old_index'];
161 } elseif (isset($_REQUEST['index'])) {
162 $form_params['old_index'] = $_REQUEST['index'];
165 echo PMA_generate_common_hidden_inputs($form_params);
167 <fieldset id="index_edit_fields">
168 <?php
169 if ($GLOBALS['is_ajax_request'] != true) {
171 <legend>
172 <?php
173 if (isset($_REQUEST['create_index'])) {
174 echo __('Add index');
175 } else {
176 echo __('Edit index');
179 </legend>
180 <?php
183 <div class='index_info'>
184 <div>
185 <div class="label">
186 <strong>
187 <label for="input_index_name">
188 <?php echo __('Index name:'); ?>
189 <?php echo PMA_showhint(PMA_Message::notice(__('("PRIMARY" <b>must</b> be the name of and <b>only of</b> a primary key!)'))); ?>
190 </label>
191 </strong>
192 </div>
193 <input type="text" name="index[Key_name]" id="input_index_name" size="25"
194 value="<?php echo htmlspecialchars($index->getName()); ?>" onfocus="this.select()" />
195 </div>
196 <div>
197 <div class="label">
198 <strong>
199 <label for="select_index_type">
200 <?php echo __('Index type:'); ?>
201 <?php echo PMA_showMySQLDocu('SQL-Syntax', 'ALTER_TABLE'); ?>
202 </label>
203 </strong>
204 </div>
205 <select name="index[Index_type]" id="select_index_type" >
206 <?php echo $index->generateIndexSelector(); ?>
207 </select>
208 </div>
209 <div class="clearfloat"></div>
210 </div>
212 <table id="index_columns">
213 <thead>
214 <tr><th><?php echo __('Column'); ?></th>
215 <th><?php echo __('Size'); ?></th>
216 </tr>
217 </thead>
218 <tbody>
219 <?php
220 $odd_row = true;
221 $spatial_types = array(
222 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
223 'multilinestring', 'multipolygon', 'geomtrycollection'
225 foreach ($index->getColumns() as $column) {
227 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
228 <td><select name="index[columns][names][]">
229 <option value="">-- <?php echo __('Ignore'); ?> --</option>
230 <?php
231 foreach ($fields as $field_name => $field_type) {
232 if (($index->getType() != 'FULLTEXT' || preg_match('/(char|text)/i', $field_type))
233 && ($index->getType() != 'SPATIAL' || in_array($field_type, $spatial_types))
235 echo '<option value="' . htmlspecialchars($field_name) . '"'
236 . (($field_name == $column->getName()) ? ' selected="selected"' : '') . '>'
237 . htmlspecialchars($field_name) . ' [' . htmlspecialchars($field_type) . ']'
238 . '</option>' . "\n";
240 } // end foreach $fields
242 </select>
243 </td>
244 <td><input type="text" size="5" onfocus="this.select()"
245 name="index[columns][sub_parts][]"
246 value="<?php if ($index->getType() != 'SPATIAL') { echo $column->getSubPart(); } ?>" />
247 </td>
248 </tr>
249 <?php
250 $odd_row = !$odd_row;
251 } // end foreach $edited_index_info['Sequences']
252 for ($i = 0; $i < $add_fields; $i++) {
254 <tr class="<?php echo $odd_row ? 'odd' : 'even'; ?> noclick">
255 <td><select name="index[columns][names][]">
256 <option value="">-- <?php echo __('Ignore'); ?> --</option>
257 <?php
258 foreach ($fields as $field_name => $field_type) {
259 echo '<option value="' . htmlspecialchars($field_name) . '">'
260 . htmlspecialchars($field_name) . ' [' . htmlspecialchars($field_type) . ']'
261 . '</option>' . "\n";
262 } // end foreach $fields
264 </select>
265 </td>
266 <td><input type="text" size="5" onfocus="this.select()"
267 name="index[columns][sub_parts][]" value="" />
268 </td>
269 </tr>
270 <?php
271 $odd_row = !$odd_row;
272 } // end foreach $edited_index_info['Sequences']
274 </tbody>
275 </table>
276 </fieldset>
277 <fieldset class="tblFooters">
278 <?php
279 if ($GLOBALS['is_ajax_request'] != true) {
281 <input type="submit" name="do_save_data" value="<?php echo __('Save'); ?>" />
282 <span id="addMoreColumns">
283 <?php
284 echo __('Or') . ' ';
285 printf(
286 __('Add to index &nbsp;%s&nbsp;column(s)') . "\n",
287 '<input type="text" name="added_fields" size="2" value="1" />'
289 echo '<input type="submit" name="add_fields" value="' . __('Go') . '" />' . "\n";
291 </span>
292 <?php
293 } else {
294 $btn_value = sprintf(__('Add %d column(s) to index'), 1);
295 echo '<div class="slider"></div>';
296 echo '<div class="add_fields">';
297 echo '<input type="submit" value="' . $btn_value . '" />';
298 echo '</div>';
301 </fieldset>
302 </form>
303 <?php
306 * Displays the footer
308 require 'libraries/footer.inc.php';