Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / tbl_indexes.php
blob94d337d819436e2813c87515810576621b605e71
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.inc.php';
16 // Get fields and stores their name/type
17 $fields = array();
18 foreach ($GLOBALS['dbi']->getColumnsFull($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] . '('
24 . str_replace(',', ', ', $tmp[2]) . ')';
25 } else {
26 $fields[$row['Field']] = $row['Type'];
28 } // end while
30 // Prepares the form values
31 if (isset($_REQUEST['index'])) {
32 if (is_array($_REQUEST['index'])) {
33 // coming already from form
34 $index = new PMA_Index($_REQUEST['index']);
35 } else {
36 $index = PMA_Index::singleton($db, $table, $_REQUEST['index']);
38 } else {
39 $index = new PMA_Index;
42 /**
43 * Process the data from the edit/create index form,
44 * run the query to build the new index
45 * and moves back to "tbl_sql.php"
47 if (isset($_REQUEST['do_save_data'])) {
48 $error = false;
50 // $sql_query is the one displayed in the query box
51 $sql_query = 'ALTER TABLE ' . PMA_Util::backquote($db)
52 . '.' . PMA_Util::backquote($table);
54 // Drops the old index
55 if (! empty($_REQUEST['old_index'])) {
56 if ($_REQUEST['old_index'] == 'PRIMARY') {
57 $sql_query .= ' DROP PRIMARY KEY,';
58 } else {
59 $sql_query .= ' DROP INDEX '
60 . PMA_Util::backquote($_REQUEST['old_index']) . ',';
62 } // end if
64 // Builds the new one
65 switch ($index->getType()) {
66 case 'PRIMARY':
67 if ($index->getName() == '') {
68 $index->setName('PRIMARY');
69 } elseif ($index->getName() != 'PRIMARY') {
70 $error = PMA_Message::error(
71 __('The name of the primary key must be "PRIMARY"!')
74 $sql_query .= ' ADD PRIMARY KEY';
75 break;
76 case 'FULLTEXT':
77 case 'UNIQUE':
78 case 'INDEX':
79 case 'SPATIAL':
80 if ($index->getName() == 'PRIMARY') {
81 $error = PMA_Message::error(__('Can\'t rename index to PRIMARY!'));
83 $sql_query .= ' ADD ' . $index->getType() . ' '
84 . ($index->getName() ? PMA_Util::backquote($index->getName()) : '');
85 break;
86 } // end switch
88 $index_fields = array();
89 foreach ($index->getColumns() as $key => $column) {
90 $index_fields[$key] = PMA_Util::backquote($column->getName());
91 if ($column->getSubPart()) {
92 $index_fields[$key] .= '(' . $column->getSubPart() . ')';
94 } // end while
96 if (empty($index_fields)) {
97 $error = PMA_Message::error(__('No index parts defined!'));
98 } else {
99 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
102 if (PMA_MYSQL_INT_VERSION > 50500) {
103 $sql_query .= "COMMENT '"
104 . PMA_Util::sqlAddSlashes($index->getComment())
105 . "'";
107 $sql_query .= ';';
109 if (! $error) {
110 $GLOBALS['dbi']->query($sql_query);
111 $message = PMA_Message::success(
112 __('Table %1$s has been altered successfully')
114 $message->addParam($table);
116 if ($GLOBALS['is_ajax_request'] == true) {
117 $response = PMA_Response::getInstance();
118 $response->addJSON('message', $message);
119 $response->addJSON('index_table', PMA_Index::getView($table, $db));
120 $response->addJSON(
121 'sql_query',
122 PMA_Util::getMessage(null, $sql_query)
124 } else {
125 $active_page = 'tbl_structure.php';
126 include 'tbl_structure.php';
128 exit;
129 } else {
130 if ($GLOBALS['is_ajax_request'] == true) {
131 $response = PMA_Response::getInstance();
132 $response->isSuccess(false);
133 $response->addJSON('message', $error);
134 exit;
135 } else {
136 $error->display();
139 } // end builds the new index
143 * Display the form to edit/create an index
145 require_once 'libraries/tbl_info.inc.php';
146 require_once 'libraries/display_indexes.lib.php';