Allow retrieving indexes by types

Signed-off-by: Madhura Jayaratne <madhura.cj@gmail.com>
This commit is contained in:
Madhura Jayaratne 2015-04-22 12:25:45 +05:30
parent fba28baa32
commit b5d75f952b
3 changed files with 48 additions and 15 deletions

View File

@ -17,6 +17,12 @@ if (! defined('PHPMYADMIN')) {
*/
class PMA_Index
{
const PRIMARY = 1;
const UNIQUE = 2;
const INDEX = 4;
const SPATIAL = 8;
const FULLTEXT = 16;
/**
* Class-wide storage container for indexes (caching, singleton)
*
@ -141,7 +147,7 @@ class PMA_Index
* @param string $table table
* @param string $schema schema
*
* @return array array of indexes
* @return PMA_Index[] array of indexes
*/
static public function getFromTable($table, $schema)
{
@ -154,6 +160,35 @@ class PMA_Index
}
}
/**
* Returns an array with all indexes from the given table of the requested types
*
* @param string $table table
* @param string $schema schema
* @param int $choices choices
*
* @return PMA_Index[] array of indexes
*/
static public function getFromTableByChoice($table, $schema, $choices = 31)
{
$indexes = array();
foreach (self::getFromTable($table, $schema) as $index) {
if (($types & PMA_Index::PRIMARY) && $index->getChoice() == 'PRIMARY') {
$indexes[] = $index;
}
if (($types & PMA_Index::UNIQUE) && $index->getChoice() == 'UNIQUE') {
$indexes[] = $index;
}
if (($types & PMA_Index::SPATIAL) && $index->getChoice() == 'SPATIAL') {
$indexes[] = $index;
}
if (($types & PMA_Index::FULLTEXT) && $index->getChoice() == 'FULLTEXT') {
$indexes[] = $index;
}
}
return $indexes;
}
/**
* return primary if set, false otherwise
*

View File

@ -2100,7 +2100,7 @@ function PMA_getHtmlForActionsInTableStructure($type, $tbl_storage_engine,
$html_output .= PMA_getHtmlForActionRowInStructureTable(
$type, $tbl_storage_engine,
'add_unique unique nowrap',
isset($columns_with_unique_index[$field_name]),
in_array($field_name, $columns_with_unique_index),
false, $url_query, $primary, 'ADD UNIQUE',
__('An index has been added on %s.'),
'Unique', $titles, $row, false
@ -2754,26 +2754,24 @@ function PMA_moveColumns($db, $table)
}
/**
* Get columns with unique index
* Get columns with indexes
*
* @param string $db database name
* @param string $table tablename
* @param int $types types bitmask
*
* @return array $columns_with_unique_index An array of columns with unique index,
* with $column name as the array key
* @return array an array of columns
*/
function PMA_getColumnsWithUniqueIndex($db ,$table)
function PMA_getColumnsWithIndex($db, $table, $types)
{
$columns_with_unique_index = array();
foreach (PMA_Index::getFromTable($table, $db) as $index) {
if ($index->isUnique() && $index->getChoice() == 'UNIQUE') {
$columns = $index->getColumns();
foreach ($columns as $column_name => $dummy) {
$columns_with_unique_index[$column_name] = 1;
}
$columns_with_index = array();
foreach (PMA_Index::getFromTableByChoice($table, $db, $types) as $index) {
$columns = $index->getColumns();
foreach ($columns as $column_name => $dummy) {
$columns_with_index[] = $column_name;
}
}
return $columns_with_unique_index;
return $columns_with_index;
}
/**

View File

@ -171,7 +171,7 @@ require_once 'libraries/Index.class.php';
// @todo should be: $server->db($db)->table($table)->primary()
$primary = PMA_Index::getPrimary($table, $db);
$columns_with_unique_index = PMA_getColumnsWithUniqueIndex($db, $table);
$columns_with_unique_index = PMA_getColumnsWithIndex($db, $table, PMA_Index::UNIQUE);
// 3. Get fields
$fields = (array) $GLOBALS['dbi']->getColumns($db, $table, null, true);