/** * Get HTML for display indexes * * @return string $html_output */ function PMA_getHtmlForDisplayIndexes() { $html_output = '<div id="index_div" class="ajax" >'; $html_output .= PMA\libraries\Index::getHtmlForIndexes($GLOBALS['table'], $GLOBALS['db']); $html_output .= '<fieldset class="tblFooters print_ignore" style="text-align: ' . 'left;"><form action="tbl_indexes.php" method="post">'; $html_output .= URL::getHiddenInputs($GLOBALS['db'], $GLOBALS['table']); $html_output .= sprintf(__('Create an index on %s columns'), '<input type="number" name="added_fields" value="1" ' . 'min="1" required="required" />'); $html_output .= '<input type="hidden" name="create_index" value="1" />' . '<input class="add_index ajax"' . ' type="submit" value="' . __('Go') . '" />'; $html_output .= '</form>' . '</fieldset>' . '</div>'; return $html_output; }
* Get columns names */ $columns = $GLOBALS['dbi']->getColumns($GLOBALS['db'], $GLOBALS['table']); /** * Displays the page */ $response->addHTML('<div id="boxContainer" data-box-width="300">'); /** * Order the table */ $hideOrderTable = false; // `ALTER TABLE ORDER BY` does not make sense for InnoDB tables that contain // a user-defined clustered index (PRIMARY KEY or NOT NULL UNIQUE index). // InnoDB always orders table rows according to such an index if one is present. if ($tbl_storage_engine == 'INNODB') { $indexes = PMA\libraries\Index::getFromTable($GLOBALS['table'], $GLOBALS['db']); foreach ($indexes as $name => $idx) { if ($name == 'PRIMARY') { $hideOrderTable = true; break; } elseif (!$idx->getNonUnique()) { $notNull = true; foreach ($idx->getColumns() as $column) { if ($column->getNull()) { $notNull = false; break; } } if ($notNull) { $hideOrderTable = true; break;
echo '</td>', "\n"; } echo ' <td>'; if (isset($comments[$column_name])) { echo htmlspecialchars($comments[$column_name]); } echo '</td>', "\n"; if ($cfgRelation['mimework']) { $mime_map = PMA_getMIME($db, $table, true); echo ' <td>'; if (isset($mime_map[$column_name])) { echo htmlspecialchars(str_replace('_', '/', $mime_map[$column_name]['mimetype'])); } echo '</td>', "\n"; } echo '</tr>'; } // end foreach $count++; echo '</table>'; // display indexes information if (count(PMA\libraries\Index::getFromTable($table, $db)) > 0) { echo PMA\libraries\Index::getHtmlForIndexes($table, $db, true); } echo '</div>'; } //ends main while /** * Displays the footer */ echo PMA\libraries\Util::getButton();
/** * Function to get html to display problems in indexes * * @param string $query_type query type * @param array|null $selectedTables array of table names selected from the * database structure page, for an action * like check table, optimize table, * analyze table or repair table * @param string $db current database * * @return string */ function PMA_getHtmlForIndexesProblems($query_type, $selectedTables, $db) { // BEGIN INDEX CHECK See if indexes should be checked. if (isset($query_type) && $query_type == 'check_tbl' && isset($selectedTables) && is_array($selectedTables)) { $indexes_problems_html = ''; foreach ($selectedTables as $tbl_name) { $check = PMA\libraries\Index::findDuplicates($tbl_name, $db); if (!empty($check)) { $indexes_problems_html .= sprintf(__('Problems with indexes of table `%s`'), $tbl_name); $indexes_problems_html .= $check; } } } else { $indexes_problems_html = null; } return $indexes_problems_html; }
/** * Create relevant index statements * * @param array $index an array of index columns * @param string $index_choice index choice that which represents * the index type of $indexed_fields * @param boolean $is_create_tbl true if requirement is to get the statement * for table creation * * @return array an array of sql statements for indexes */ function PMA_buildIndexStatements($index, $index_choice, $is_create_tbl = true) { $statement = array(); if (!count($index)) { return $statement; } $sql_query = PMA_getStatementPrefix($is_create_tbl) . ' ' . $index_choice; if (!empty($index['Key_name']) && $index['Key_name'] != 'PRIMARY') { $sql_query .= ' ' . PMA\libraries\Util::backquote($index['Key_name']); } $index_fields = array(); foreach ($index['columns'] as $key => $column) { $index_fields[$key] = PMA\libraries\Util::backquote($_REQUEST['field_name'][$column['col_index']]); if ($column['size']) { $index_fields[$key] .= '(' . $column['size'] . ')'; } } // end while $sql_query .= ' (' . implode(', ', $index_fields) . ')'; $keyBlockSizes = $index['Key_block_size']; if (!empty($keyBlockSizes)) { $sql_query .= " KEY_BLOCK_SIZE = " . PMA\libraries\Util::sqlAddSlashes($keyBlockSizes); } // specifying index type is allowed only for primary, unique and index only $type = $index['Index_type']; if ($index['Index_choice'] != 'SPATIAL' && $index['Index_choice'] != 'FULLTEXT' && in_array($type, PMA\libraries\Index::getIndexTypes())) { $sql_query .= ' USING ' . $type; } $parser = $index['Parser']; if ($index['Index_choice'] == 'FULLTEXT' && !empty($parser)) { $sql_query .= " WITH PARSER " . PMA\libraries\Util::sqlAddSlashes($parser); } $comment = $index['Index_comment']; if (!empty($comment)) { $sql_query .= " COMMENT '" . PMA\libraries\Util::sqlAddSlashes($comment) . "'"; } $statement[] = $sql_query; return $statement; }
/** * Tests for displayFormAction() * * @return void * @test */ public function testDisplayFormAction() { $table = $this->getMockBuilder('PMA\\libraries\\Table')->disableOriginalConstructor()->getMock(); $table->expects($this->any())->method('getStatusInfo')->will($this->returnValue("")); $table->expects($this->any())->method('isView')->will($this->returnValue(false)); $table->expects($this->any())->method('getNameAndTypeOfTheColumns')->will($this->returnValue(array("field_name" => "field_type"))); $GLOBALS['dbi']->expects($this->any())->method('getTable')->will($this->returnValue($table)); $container = Container::getDefaultContainer(); $container->set('db', 'db'); $container->set('table', 'table'); $container->set('dbi', $GLOBALS['dbi']); $response = new \PMA\Test\Stubs\Response(); $container->set('PMA\\libraries\\Response', $response); $container->alias('response', 'PMA\\libraries\\Response'); $index = new PMA\libraries\Index(); $ctrl = new TableIndexesController($index); $_REQUEST['create_index'] = true; $_REQUEST['added_fields'] = 3; $ctrl->displayFormAction(); $html = $response->getHTMLResult(); //PMA_URL_getHiddenInputs $this->assertContains(PMA_URL_getHiddenInputs(array('db' => 'db', 'table' => 'table', 'create_index' => 1)), $html); $doc_html = PMA\libraries\Util::showHint(PMA\libraries\Message::notice(__('"PRIMARY" <b>must</b> be the name of' . ' and <b>only of</b> a primary key!'))); $this->assertContains($doc_html, $html); $this->assertContains(PMA\libraries\Util::showMySQLDocu('ALTER_TABLE'), $html); // generateIndexSelector $this->assertContains(PMA\libraries\Template::trim($index->generateIndexChoiceSelector(false)), $html); $this->assertContains(sprintf(__('Add %s column(s) to index'), 1), $html); //$field_name & $field_type $this->assertContains("field_name", $html); $this->assertContains("field_type", $html); }
/** * find all the possible partial dependencies based on data in the table. * * @param string $table current table * @param string $db current database * * @return string HTML containing the list of all the possible partial dependencies */ function PMA_findPartialDependencies($table, $db) { $dependencyList = array(); $GLOBALS['dbi']->selectDb($db, $GLOBALS['userlink']); $columns = (array) $GLOBALS['dbi']->getColumnNames($db, $table, $GLOBALS['userlink']); $columns = (array) Util::backquote($columns); $totalRowsRes = $GLOBALS['dbi']->fetchResult('SELECT COUNT(*) FROM (SELECT * FROM ' . Util::backquote($table) . ' LIMIT 500) as dt;'); $totalRows = $totalRowsRes[0]; $primary = PMA\libraries\Index::getPrimary($table, $db); $primarycols = $primary->getColumns(); $pk = array(); foreach ($primarycols as $col) { $pk[] = Util::backquote($col->getName()); } $partialKeys = PMA_getAllCombinationPartialKeys($pk); $distinctValCount = PMA_findDistinctValuesCount(array_unique(array_merge($columns, $partialKeys)), $table); foreach ($columns as $column) { if (!in_array($column, $pk)) { foreach ($partialKeys as $partialKey) { if ($partialKey && PMA_checkPartialDependency($partialKey, $column, $table, $distinctValCount[$partialKey], $distinctValCount[$column], $totalRows)) { $dependencyList[$partialKey][] = $column; } } } } $html = __('This list is based on a subset of the table\'s data ' . 'and is not necessarily accurate. ') . '<div class="dependencies_box">'; foreach ($dependencyList as $dependon => $colList) { $html .= '<span class="displayblock">' . '<input type="button" class="pickPd" value="' . __('Pick') . '"/>' . '<span class="determinants">' . htmlspecialchars(str_replace('`', '', $dependon)) . '</span> -> ' . '<span class="dependents">' . htmlspecialchars(str_replace('`', '', implode(', ', $colList))) . '</span>' . '</span>'; } if (empty($dependencyList)) { $html .= '<p class="displayblock desc">' . __('No partial dependencies found!') . '</p>'; } $html .= '</div>'; return $html; }
/** * Returns all indices * * @param bool $unique_only whether to include only unique ones * * @return array indices */ function PMA_getAllKeys($unique_only = false) { $keys = array(); foreach ($GLOBALS['PMD']['TABLE_NAME_SMALL'] as $I => $table) { $schema = $GLOBALS['PMD']['OWNER'][$I]; // for now, take into account only the first index segment foreach (PMA\libraries\Index::getFromTable($table, $schema) as $index) { if ($unique_only && !$index->isUnique()) { continue; } $columns = $index->getColumns(); foreach ($columns as $column_name => $dummy) { $keys[$schema . '.' . $table . '.' . $column_name] = 1; } } } return $keys; }
/** * Test for PMA\libraries\Index_Column * * @return void */ public function testColumns() { $index = new PMA\libraries\Index(); $index->addColumns($this->_params['columns']); $index_columns = $index->getColumns(); $index_column = $index_columns['column1']; $this->assertEquals('column1', $index_column->getName()); $this->assertEquals('index1', $index_column->getSeqInIndex()); $this->assertEquals('Collation1', $index_column->getCollation()); $this->assertEquals('Cardinality1', $index_column->getCardinality()); }