2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions related to table indexes
8 if (! defined('PHPMYADMIN')) {
13 * Function to get the name and type of the columns of a table
15 * @param string $db current database
16 * @param string $table current table
20 function PMA_getNameAndTypeOfTheColumns($db, $table)
23 foreach ($GLOBALS['dbi']->getColumnsFull($db, $table) as $row) {
24 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
25 $tmp[2] = /*overload*/mb_substr(
26 preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1
28 $columns[$row['Field']] = $tmp[1] . '('
29 . str_replace(',', ', ', $tmp[2]) . ')';
31 $columns[$row['Field']] = $row['Type'];
39 * Function to handle the creation or edit of an index
41 * @param string $db current db
42 * @param string $table current table
43 * @param PMA_Index $index current index
47 function PMA_handleCreateOrEditIndex($db, $table, $index)
51 $sql_query = PMA_getSqlQueryForIndexCreateOrEdit($db, $table, $index, $error);
53 // If there is a request for SQL previewing.
54 if (isset($_REQUEST['preview_sql'])) {
55 PMA_previewSQL($sql_query);
59 $GLOBALS['dbi']->query($sql_query);
60 $message = PMA_Message
::success(
61 __('Table %1$s has been altered successfully.')
63 $message->addParam($table);
65 if ($GLOBALS['is_ajax_request'] == true) {
66 $response = PMA_Response
::getInstance();
68 'message', PMA_Util
::getMessage($message, $sql_query, 'success')
70 $response->addJSON('index_table', PMA_Index
::getView($table, $db));
72 include 'tbl_structure.php';
76 $response = PMA_Response
::getInstance();
77 $response->isSuccess(false);
78 $response->addJSON('message', $error);
84 * Function to get the sql query for index creation or edit
86 * @param string $db current db
87 * @param string $table current table
88 * @param PMA_Index $index current index
89 * @param bool &$error whether error occurred or not
93 function PMA_getSqlQueryForIndexCreateOrEdit($db, $table, $index, &$error)
95 // $sql_query is the one displayed in the query box
96 $sql_query = 'ALTER TABLE ' . PMA_Util
::backquote($db)
97 . '.' . PMA_Util
::backquote($table);
99 // Drops the old index
100 if (! empty($_REQUEST['old_index'])) {
101 if ($_REQUEST['old_index'] == 'PRIMARY') {
102 $sql_query .= ' DROP PRIMARY KEY,';
104 $sql_query .= ' DROP INDEX '
105 . PMA_Util
::backquote($_REQUEST['old_index']) . ',';
109 // Builds the new one
110 switch ($index->getChoice()) {
112 if ($index->getName() == '') {
113 $index->setName('PRIMARY');
114 } elseif ($index->getName() != 'PRIMARY') {
115 $error = PMA_Message
::error(
116 __('The name of the primary key must be "PRIMARY"!')
119 $sql_query .= ' ADD PRIMARY KEY';
125 if ($index->getName() == 'PRIMARY') {
126 $error = PMA_Message
::error(__('Can\'t rename index to PRIMARY!'));
128 $sql_query .= ' ADD ' . $index->getChoice() . ' '
129 . ($index->getName() ? PMA_Util
::backquote($index->getName()) : '');
133 $index_fields = array();
134 foreach ($index->getColumns() as $key => $column) {
135 $index_fields[$key] = PMA_Util
::backquote($column->getName());
136 if ($column->getSubPart()) {
137 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
141 if (empty($index_fields)) {
142 $error = PMA_Message
::error(__('No index parts defined!'));
144 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
147 $keyBlockSizes = $index->getKeyBlockSize();
148 if (! empty($keyBlockSizes)) {
149 $sql_query .= " KEY_BLOCK_SIZE = "
150 . PMA_Util
::sqlAddSlashes($keyBlockSizes);
153 // specifying index type is allowed only for primary, unique and index only
154 $type = $index->getType();
155 if ($index->getChoice() != 'SPATIAL'
156 && $index->getChoice() != 'FULLTEXT'
157 && in_array($type, PMA_Index
::getIndexTypes())
159 $sql_query .= ' USING ' . $type;
162 $parser = $index->getParser();
163 if ($index->getChoice() == 'FULLTEXT' && ! empty($parser)) {
164 $sql_query .= " WITH PARSER " . PMA_Util
::sqlAddSlashes($parser);
167 $comment = $index->getComment();
168 if (! empty($comment)) {
169 $sql_query .= " COMMENT '" . PMA_Util
::sqlAddSlashes($comment) . "'";
178 * Function to prepare the form values for index
180 * @param string $db current database
181 * @param string $table current table
185 function PMA_prepareFormValues($db, $table)
187 if (isset($_REQUEST['index'])) {
188 if (is_array($_REQUEST['index'])) {
189 // coming already from form
190 $index = new PMA_Index($_REQUEST['index']);
192 $index = PMA_Index
::singleton($db, $table, $_REQUEST['index']);
195 $index = new PMA_Index
;
202 * Function to get the number of fields for the form
204 * @param PMA_Index $index index
208 function PMA_getNumberOfFieldsForForm($index)
210 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
211 // coming already from form
213 = isset($_REQUEST['index']['columns']['names'])?
214 count($_REQUEST['index']['columns']['names'])
215 - $index->getColumnCount():0;
216 if (isset($_REQUEST['add_fields'])) {
217 $add_fields +
= $_REQUEST['added_fields'];
219 } elseif (isset($_REQUEST['create_index'])) {
220 $add_fields = $_REQUEST['added_fields'];
223 }// end preparing form values
229 * Function to get form parameters
231 * @param string $db current db
232 * @param string $table current table
236 function PMA_getFormParameters($db, $table)
238 $form_params = array(
243 if (isset($_REQUEST['create_index'])) {
244 $form_params['create_index'] = 1;
245 } elseif (isset($_REQUEST['old_index'])) {
246 $form_params['old_index'] = $_REQUEST['old_index'];
247 } elseif (isset($_REQUEST['index'])) {
248 $form_params['old_index'] = $_REQUEST['index'];
255 * Function to get html for displaying the index form
257 * @param array $fields fields
258 * @param PMA_Index $index index
259 * @param array $form_params form parameters
260 * @param int $add_fields number of fields in the form
264 function PMA_getHtmlForIndexForm($fields, $index, $form_params, $add_fields)
267 $html .= '<form action="tbl_indexes.php" method="post" name="index_frm" id="'
268 . 'index_frm" class="ajax"'
269 . 'onsubmit="if (typeof(this.elements[\'index[Key_name]\'].disabled) !='
270 . ' \'undefined\') {'
271 . 'this.elements[\'index[Key_name]\'].disabled = false}">';
273 $html .= PMA_URL_getHiddenInputs($form_params);
275 $html .= '<fieldset id="index_edit_fields">';
277 $html .= '<div class="index_info">';
280 . '<div class="label">'
282 . '<label for="input_index_name">'
284 . PMA_Util
::showHint(
287 '"PRIMARY" <b>must</b> be the name of'
288 . ' and <b>only of</b> a primary key!'
295 . '<input type="text" name="index[Key_name]" id="input_index_name"'
297 . 'value="' . htmlspecialchars($index->getName()) . '"'
298 . 'onfocus="this.select()" />'
302 . '<div class="label">'
304 . '<label for="select_index_choice">'
305 . __('Index choice:')
306 . PMA_Util
::showMySQLDocu('ALTER_TABLE')
310 . $index->generateIndexChoiceSelector(isset($_REQUEST['create_edit_table']))
313 $html .= PMA_Util
::getDivForSliderEffect(
314 'indexoptions', __('Options')
318 . '<div class="label">'
320 . '<label for="input_key_block_size">'
321 . __('Key block size:')
325 . '<input type="text" name="index[Key_block_size]" '
326 . 'id="input_key_block_size" size="30" value="'
327 . htmlspecialchars($index->getKeyBlockSize()) . '" />'
331 . '<div class="label">'
333 . '<label for="select_index_type">'
335 . PMA_Util
::showMySQLDocu('ALTER_TABLE')
339 . $index->generateIndexTypeSelector()
343 . '<div class="label">'
345 . '<label for="input_parser">'
350 . '<input type="text" name="index[Parser]" '
351 . 'id="input_parse" size="30" value="' . htmlspecialchars($index->getParser()) . '" />'
355 . '<div class="label">'
357 . '<label for="input_index_comment">'
362 . '<input type="text" name="index[Index_comment]" '
363 . 'id="input_index_comment" size="30"'
364 . 'value="' . htmlspecialchars($index->getComment()) . '" />'
367 $html .= '</div>'; // end of indexoptions div
369 $html .= '<div class="clearfloat"></div>';
373 $html .= '<table id="index_columns">';
377 . '<th>' . __('Column') . '</th>'
378 . '<th>' . __('Size') . '</th>'
383 $spatial_types = array(
384 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
385 'multilinestring', 'multipolygon', 'geomtrycollection'
388 /* @var $column PMA_Index_Column */
389 foreach ($index->getColumns() as $column) {
390 $html .= '<tr class="';
391 $html .= $odd_row ?
'odd' : 'even';
392 $html .= 'noclick">';
393 $html .= '<td><span class="drag_icon" title="' . __('Drag to reorder') . '"'
395 $html .= '<select name="index[columns][names][]">';
396 $html .= '<option value="">-- ' . __('Ignore') . ' --</option>';
397 foreach ($fields as $field_name => $field_type) {
398 if (($index->getChoice() != 'FULLTEXT'
399 ||
preg_match('/(char|text)/i', $field_type))
400 && ($index->getChoice() != 'SPATIAL'
401 ||
in_array($field_type, $spatial_types))
403 $html .= '<option value="' . htmlspecialchars($field_name) . '"'
404 . (($field_name == $column->getName())
405 ?
' selected="selected"'
407 . htmlspecialchars($field_name) . ' ['
408 . htmlspecialchars($field_type) . ']'
409 . '</option>' . "\n";
411 } // end foreach $fields
412 $html .= '</select>';
415 $html .= '<input type="text" size="5" onfocus="this.select()"'
416 . 'name="index[columns][sub_parts][]" value="';
417 if ($index->getChoice() != 'SPATIAL') {
418 $html .= $column->getSubPart();
423 $odd_row = !$odd_row;
424 } // end foreach $edited_index_info['Sequences']
426 for ($i = 0; $i < $add_fields; $i++
) {
427 $html .= '<tr class="';
428 $html .= $odd_row ?
'odd' : 'even';
429 $html .= 'noclick">';
430 $html .= '<td><span class="drag_icon" title="' . __('Drag to reorder') . '"'
432 $html .= '<select name="index[columns][names][]">';
433 $html .= '<option value="">-- ' . __('Ignore') . ' --</option>';
435 foreach ($fields as $field_name => $field_type) {
436 if (isset($_REQUEST['create_edit_table'])) {
437 $col_index = $field_type[1];
438 $field_type = $field_type[0];
440 $html .= '<option value="'
441 . htmlspecialchars((isset($col_index)) ?
$col_index : $field_name)
442 . '" ' . ($j++
== $i ?
'selected="selected"' : '') . '>'
443 . htmlspecialchars($field_name) . ' ['
444 . htmlspecialchars($field_type) . ']'
445 . '</option>' . "\n";
446 } // end foreach $fields
447 $html .= '</select>';
450 . '<input type="text" size="5" onfocus="this.select()"'
451 . 'name="index[columns][sub_parts][]" value="" />'
454 $odd_row = !$odd_row;
455 } // end foreach $edited_index_info['Sequences']
461 $html .= '<div class="add_more">';
462 $btn_value = sprintf(__('Add %s column(s) to index'), 1);
463 $html .= '<div class="slider"></div>';
464 $html .= '<div class="add_fields hide">';
465 $html .= '<input type="submit" id="add_fields" value="' . $btn_value . '" />';
469 $html .= '</fieldset>';
471 $html .= '<fieldset class="tblFooters">';
472 $html .= '<button type="submit" id="preview_index_frm">' . __('Preview SQL') . '</button>';
473 $html .= '<input type="submit" id="save_index_frm" value="' . __('Go') . '" />';
474 $html .= '</fieldset>';