2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Displays index edit/creation form and handles it
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
18 foreach ($GLOBALS['dbi']->getColumnsFull($db, $table) as $row) {
19 if (preg_match('@^(set|enum)\((.+)\)$@i', $row['Type'], $tmp)) {
21 preg_replace('@([^,])\'\'@', '\\1\\\'', ',' . $tmp[2]), 1
23 $fields[$row['Field']] = $tmp[1] . '('
24 . str_replace(',', ', ', $tmp[2]) . ')';
26 $fields[$row['Field']] = $row['Type'];
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']);
36 $index = PMA_Index
::singleton($db, $table, $_REQUEST['index']);
39 $index = new PMA_Index
;
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'])) {
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,';
59 $sql_query .= ' DROP INDEX '
60 . PMA_Util
::backquote($_REQUEST['old_index']) . ',';
65 switch ($index->getType()) {
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';
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()) : '');
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() . ')';
96 if (empty($index_fields)) {
97 $error = PMA_Message
::error(__('No index parts defined!'));
99 $sql_query .= ' (' . implode(', ', $index_fields) . ')';
102 if (PMA_MYSQL_INT_VERSION
> 50500) {
103 $sql_query .= "COMMENT '"
104 . PMA_Util
::sqlAddSlashes($index->getComment())
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));
122 PMA_Util
::getMessage(null, $sql_query)
125 $active_page = 'tbl_structure.php';
126 include 'tbl_structure.php';
130 if ($GLOBALS['is_ajax_request'] == true) {
131 $response = PMA_Response
::getInstance();
132 $response->isSuccess(false);
133 $response->addJSON('message', $error);
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';