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();
67 $response->addJSON('message', $message);
68 $response->addJSON('index_table', PMA_Index
::getView($table, $db));
71 PMA_Util
::getMessage(null, $sql_query)
74 include 'tbl_structure.php';
78 $response = PMA_Response
::getInstance();
79 $response->isSuccess(false);
80 $response->addJSON('message', $error);
86 * Function to get the sql query for index creation or edit
88 * @param string $db current db
89 * @param string $table current table
90 * @param PMA_Index $index current index
91 * @param bool &$error whether error occurred or not
95 function PMA_getSqlQueryForIndexCreateOrEdit($db, $table, $index, &$error)
97 // $sql_query is the one displayed in the query box
98 $sql_query = 'ALTER TABLE ' . PMA_Util
::backquote($db)
99 . '.' . PMA_Util
::backquote($table);
101 // Drops the old index
102 if (! empty($_REQUEST['old_index'])) {
103 if ($_REQUEST['old_index'] == 'PRIMARY') {
104 $sql_query .= ' DROP PRIMARY KEY,';
106 $sql_query .= ' DROP INDEX '
107 . PMA_Util
::backquote($_REQUEST['old_index']) . ',';
111 // Builds the new one
112 switch ($index->getType()) {
114 if ($index->getName() == '') {
115 $index->setName('PRIMARY');
116 } elseif ($index->getName() != 'PRIMARY') {
117 $error = PMA_Message
::error(
118 __('The name of the primary key must be "PRIMARY"!')
121 $sql_query .= ' ADD PRIMARY KEY';
127 if ($index->getName() == 'PRIMARY') {
128 $error = PMA_Message
::error(__('Can\'t rename index to PRIMARY!'));
130 $sql_query .= ' ADD ' . $index->getType() . ' '
131 . ($index->getName() ? PMA_Util
::backquote($index->getName()) : '');
135 $index_fields = array();
136 foreach ($index->getColumns() as $key => $column) {
137 $index_fields[$key] = PMA_Util
::backquote($column->getName());
138 if ($column->getSubPart()) {
139 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
143 if (empty($index_fields)) {
144 $error = PMA_Message
::error(__('No index parts defined!'));
146 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
149 $sql_query .= " COMMENT '"
150 . PMA_Util
::sqlAddSlashes($index->getComment())
158 * Function to prepare the form values for index
160 * @param string $db current database
161 * @param string $table current table
165 function PMA_prepareFormValues($db, $table)
167 if (isset($_REQUEST['index'])) {
168 if (is_array($_REQUEST['index'])) {
169 // coming already from form
170 $index = new PMA_Index($_REQUEST['index']);
172 $index = PMA_Index
::singleton($db, $table, $_REQUEST['index']);
175 $index = new PMA_Index
;
182 * Function to get the number of fields for the form
184 * @param PMA_Index $index index
188 function PMA_getNumberOfFieldsForForm($index)
190 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
191 // coming already from form
193 = isset($_REQUEST['index']['columns']['names'])?
194 count($_REQUEST['index']['columns']['names'])
195 - $index->getColumnCount():0;
196 if (isset($_REQUEST['add_fields'])) {
197 $add_fields +
= $_REQUEST['added_fields'];
199 } elseif (isset($_REQUEST['create_index'])) {
200 $add_fields = $_REQUEST['added_fields'];
203 }// end preparing form values
209 * Function to get form parameters
211 * @param string $db current db
212 * @param string $table current table
216 function PMA_getFormParameters($db, $table)
218 $form_params = array(
223 if (isset($_REQUEST['create_index'])) {
224 $form_params['create_index'] = 1;
225 } elseif (isset($_REQUEST['old_index'])) {
226 $form_params['old_index'] = $_REQUEST['old_index'];
227 } elseif (isset($_REQUEST['index'])) {
228 $form_params['old_index'] = $_REQUEST['index'];
235 * Function to get html for displaying the index form
237 * @param array $fields fields
238 * @param PMA_Index $index index
239 * @param array $form_params form parameters
240 * @param int $add_fields number of fields in the form
244 function PMA_getHtmlForIndexForm($fields, $index, $form_params, $add_fields)
247 $html .= '<form action="tbl_indexes.php" method="post" name="index_frm" id="'
248 . 'index_frm" class="ajax"'
249 . 'onsubmit="if (typeof(this.elements[\'index[Key_name]\'].disabled) !='
250 . ' \'undefined\') {'
251 . 'this.elements[\'index[Key_name]\'].disabled = false}">';
253 $html .= PMA_URL_getHiddenInputs($form_params);
255 $html .= '<fieldset id="index_edit_fields">';
257 $html .= '<div class="index_info">';
260 . '<div class="label">'
262 . '<label for="input_index_name">'
264 . PMA_Util
::showHint(
267 '"PRIMARY" <b>must</b> be the name of'
268 . ' and <b>only of</b> a primary key!'
275 . '<input type="text" name="index[Key_name]" id="input_index_name"'
277 . 'value="' . htmlspecialchars($index->getName()) . '"'
278 . 'onfocus="this.select()" />'
282 . '<div class="label">'
284 . '<label for="input_index_comment">'
289 . '<input type="text" name="index[Index_comment]" '
290 . 'id="input_index_comment" size="30"'
291 . 'value="' . htmlspecialchars($index->getComment()) . '"'
292 . 'onfocus="this.select()" />'
296 . '<div class="label">'
298 . '<label for="select_index_type">'
300 . PMA_Util
::showMySQLDocu('ALTER_TABLE')
304 . '<select name="index[Index_type]" id="select_index_type" '
305 . (isset($_REQUEST['create_edit_table']) ?
'disabled="disabled"' : '') . '>'
306 . $index->generateIndexSelector()
310 $html .= '<div class="clearfloat"></div>';
314 $html .= '<table id="index_columns">';
318 . '<th>' . __('Column') . '</th>'
319 . '<th>' . __('Size') . '</th>'
324 $spatial_types = array(
325 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
326 'multilinestring', 'multipolygon', 'geomtrycollection'
329 /* @var $column PMA_Index_Column */
330 foreach ($index->getColumns() as $column) {
331 $html .= '<tr class="';
332 $html .= $odd_row ?
'odd' : 'even';
333 $html .= 'noclick">';
334 $html .= '<td><span class="drag_icon" title="' . __('Drag to reorder') . '"'
336 $html .= '<select name="index[columns][names][]">';
337 $html .= '<option value="">-- ' . __('Ignore') . ' --</option>';
338 foreach ($fields as $field_name => $field_type) {
339 if (($index->getType() != 'FULLTEXT'
340 ||
preg_match('/(char|text)/i', $field_type))
341 && ($index->getType() != 'SPATIAL'
342 ||
in_array($field_type, $spatial_types))
344 $html .= '<option value="' . htmlspecialchars($field_name) . '"'
345 . (($field_name == $column->getName())
346 ?
' selected="selected"'
348 . htmlspecialchars($field_name) . ' ['
349 . htmlspecialchars($field_type) . ']'
350 . '</option>' . "\n";
352 } // end foreach $fields
353 $html .= '</select>';
356 $html .= '<input type="text" size="5" onfocus="this.select()"'
357 . 'name="index[columns][sub_parts][]" value="';
358 if ($index->getType() != 'SPATIAL') {
359 $html .= $column->getSubPart();
364 $odd_row = !$odd_row;
365 } // end foreach $edited_index_info['Sequences']
367 for ($i = 0; $i < $add_fields; $i++
) {
368 $html .= '<tr class="';
369 $html .= $odd_row ?
'odd' : 'even';
370 $html .= 'noclick">';
371 $html .= '<td><span class="drag_icon" title="' . __('Drag to reorder') . '"'
373 $html .= '<select name="index[columns][names][]">';
374 $html .= '<option value="">-- ' . __('Ignore') . ' --</option>';
376 foreach ($fields as $field_name => $field_type) {
377 if (isset($_REQUEST['create_edit_table'])) {
378 $col_index = $field_type[1];
379 $field_type = $field_type[0];
381 $html .= '<option value="'
382 . htmlspecialchars((isset($col_index)) ?
$col_index : $field_name)
383 . '" ' . ($j++
== $i ?
'selected="selected"' : '') . '>'
384 . htmlspecialchars($field_name) . ' ['
385 . htmlspecialchars($field_type) . ']'
386 . '</option>' . "\n";
387 } // end foreach $fields
388 $html .= '</select>';
391 . '<input type="text" size="5" onfocus="this.select()"'
392 . 'name="index[columns][sub_parts][]" value="" />'
395 $odd_row = !$odd_row;
396 } // end foreach $edited_index_info['Sequences']
402 $html .= '</fieldset>';
404 $html .= '<fieldset class="tblFooters">';
406 $btn_value = sprintf(__('Add %s column(s) to index'), 1);
407 $html .= '<div class="slider"></div>';
408 $html .= '<div class="add_fields">';
409 $html .= '<input type="submit" value="' . $btn_value . '" />';
412 $html .= '</fieldset>';