Translated using Weblate (Catalan)
[phpmyadmin.git] / libraries / tbl_indexes.lib.php
blob7b11e1e27f8b116dc07820f9c04c4cb708bd2d1c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions related to table indexes
6 * @package PhpMyAdmin
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
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
18 * @return array
20 function PMA_getNameAndTypeOfTheColumns($db, $table)
22 $columns = array();
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]) . ')';
30 } else {
31 $columns[$row['Field']] = $row['Type'];
33 } // end while
35 return $columns;
38 /**
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
45 * @return void
47 function PMA_handleCreateOrEditIndex($db, $table, $index)
49 $error = false;
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);
58 if (! $error) {
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));
69 $response->addJSON(
70 'sql_query',
71 PMA_Util::getMessage(null, $sql_query)
73 } else {
74 include 'tbl_structure.php';
76 exit;
77 } else {
78 $response = PMA_Response::getInstance();
79 $response->isSuccess(false);
80 $response->addJSON('message', $error);
81 exit;
85 /**
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
93 * @return string
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,';
105 } else {
106 $sql_query .= ' DROP INDEX '
107 . PMA_Util::backquote($_REQUEST['old_index']) . ',';
109 } // end if
111 // Builds the new one
112 switch ($index->getType()) {
113 case 'PRIMARY':
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';
122 break;
123 case 'FULLTEXT':
124 case 'UNIQUE':
125 case 'INDEX':
126 case 'SPATIAL':
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()) : '');
132 break;
133 } // end switch
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() . ')';
141 } // end while
143 if (empty($index_fields)) {
144 $error = PMA_Message::error(__('No index parts defined!'));
145 } else {
146 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
149 $sql_query .= " COMMENT '"
150 . PMA_Util::sqlAddSlashes($index->getComment())
151 . "'";
152 $sql_query .= ';';
154 return $sql_query;
158 * Function to prepare the form values for index
160 * @param string $db current database
161 * @param string $table current table
163 * @return PMA_Index
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']);
171 } else {
172 $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
174 } else {
175 $index = new PMA_Index;
178 return $index;
182 * Function to get the number of fields for the form
184 * @param PMA_Index $index index
186 * @return int
188 function PMA_getNumberOfFieldsForForm($index)
190 if (isset($_REQUEST['index']) && is_array($_REQUEST['index'])) {
191 // coming already from form
192 $add_fields
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'];
201 } else {
202 $add_fields = 0;
203 }// end preparing form values
205 return $add_fields;
209 * Function to get form parameters
211 * @param string $db current db
212 * @param string $table current table
214 * @return array
216 function PMA_getFormParameters($db, $table)
218 $form_params = array(
219 'db' => $db,
220 'table' => $table,
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'];
231 return $form_params;
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
242 * @return string
244 function PMA_getHtmlForIndexForm($fields, $index, $form_params, $add_fields)
246 $html = "";
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">';
259 $html .= '<div>'
260 . '<div class="label">'
261 . '<strong>'
262 . '<label for="input_index_name">'
263 . __('Index name:')
264 . PMA_Util::showHint(
265 PMA_Message::notice(
267 '"PRIMARY" <b>must</b> be the name of'
268 . ' and <b>only of</b> a primary key!'
272 . '</label>'
273 . '</strong>'
274 . '</div>'
275 . '<input type="text" name="index[Key_name]" id="input_index_name"'
276 . ' size="25"'
277 . 'value="' . htmlspecialchars($index->getName()) . '"'
278 . 'onfocus="this.select()" />'
279 . '</div>';
281 $html .= '<div>'
282 . '<div class="label">'
283 . '<strong>'
284 . '<label for="input_index_comment">'
285 . __('Comment:')
286 . '</label>'
287 . '</strong>'
288 . '</div>'
289 . '<input type="text" name="index[Index_comment]" '
290 . 'id="input_index_comment" size="30"'
291 . 'value="' . htmlspecialchars($index->getComment()) . '"'
292 . 'onfocus="this.select()" />'
293 . '</div>';
295 $html .= '<div>'
296 . '<div class="label">'
297 . '<strong>'
298 . '<label for="select_index_type">'
299 . __('Index type:')
300 . PMA_Util::showMySQLDocu('ALTER_TABLE')
301 . '</label>'
302 . '</strong>'
303 . '</div>'
304 . '<select name="index[Index_type]" id="select_index_type" '
305 . (isset($_REQUEST['create_edit_table']) ? 'disabled="disabled"' : '') . '>'
306 . $index->generateIndexSelector()
307 . '</select>'
308 . '</div>';
310 $html .= '<div class="clearfloat"></div>';
312 $html .= '</div>';
314 $html .= '<table id="index_columns">';
316 $html .= '<thead>'
317 . '<tr>'
318 . '<th>' . __('Column') . '</th>'
319 . '<th>' . __('Size') . '</th>'
320 . '</tr>'
321 . '</thead>';
323 $odd_row = true;
324 $spatial_types = array(
325 'geometry', 'point', 'linestring', 'polygon', 'multipoint',
326 'multilinestring', 'multipolygon', 'geomtrycollection'
328 $html .= '<tbody>';
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') . '"'
335 . '></span>';
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"'
347 : '') . '>'
348 . htmlspecialchars($field_name) . ' ['
349 . htmlspecialchars($field_type) . ']'
350 . '</option>' . "\n";
352 } // end foreach $fields
353 $html .= '</select>';
354 $html .= '</td>';
355 $html .= '<td>';
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();
361 $html .= '"/>';
362 $html .= '</td>';
363 $html .= '</tr>';
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') . '"'
372 . '></span>';
373 $html .= '<select name="index[columns][names][]">';
374 $html .= '<option value="">-- ' . __('Ignore') . ' --</option>';
375 $j = 0;
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>';
389 $html .= '</td>';
390 $html .= '<td>'
391 . '<input type="text" size="5" onfocus="this.select()"'
392 . 'name="index[columns][sub_parts][]" value="" />'
393 . '</td>';
394 $html .= '</tr>';
395 $odd_row = !$odd_row;
396 } // end foreach $edited_index_info['Sequences']
398 $html .= '</tbody>';
400 $html .= '</table>';
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 . '" />';
410 $html .= '</div>';
412 $html .= '</fieldset>';
414 $html .= '</form>';
416 return $html;