2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * holds the database index class
10 * @since phpMyAdmin 3.0.0
17 * Class-wide storage container for indexes (caching, singleton)
21 protected static $_registry = array();
24 * @var string The name of the schema
26 protected $_schema = '';
29 * @var string The name of the table
31 protected $_table = '';
34 * @var string The name of the index
36 protected $_name = '';
43 protected $_columns = array();
46 * The index method used (BTREE, SPATIAL, FULLTEXT, HASH, RTREE).
50 protected $_type = '';
53 * The index choice (PRIMARY, UNIQUE, INDEX, SPATIAL, FULLTEXT)
57 protected $_choice = '';
64 protected $_remarks = '';
67 * Any comment provided for the index with a COMMENT attribute when the
72 protected $_comment = '';
75 * @var integer 0 if the index cannot contain duplicates, 1 if it can.
77 protected $_non_unique = 0;
80 * Indicates how the key is packed. NULL if it is not.
84 protected $_packed = null;
89 * @param array $params
91 public function __construct($params = array())
96 static public function singleton($schema, $table, $index_name = '')
98 PMA_Index
::_loadIndexes($table, $schema);
99 if (! isset(PMA_Index
::$_registry[$schema][$table][$index_name])) {
100 $index = new PMA_Index
;
101 if (strlen($index_name)) {
102 $index->setName($index_name);
103 PMA_Index
::$_registry[$schema][$table][$index->getName()] = $index;
107 return PMA_Index
::$_registry[$schema][$table][$index_name];
112 * returns an array with all indexes from the given table
114 * @param string $table
115 * @param string $schema
118 static public function getFromTable($table, $schema)
120 PMA_Index
::_loadIndexes($table, $schema);
122 if (isset(PMA_Index
::$_registry[$schema][$table])) {
123 return PMA_Index
::$_registry[$schema][$table];
130 * return primary if set, false otherwise
132 * @param string $table
133 * @param string $schema
134 * @return mixed primary index or false if no one exists
136 static public function getPrimary($table, $schema)
138 PMA_Index
::_loadIndexes($table, $schema);
140 if (isset(PMA_Index
::$_registry[$schema][$table]['PRIMARY'])) {
141 return PMA_Index
::$_registry[$schema][$table]['PRIMARY'];
148 * Load index data for table
150 * @param string $table
151 * @param string $schema
154 static protected function _loadIndexes($table, $schema)
156 if (isset(PMA_Index
::$_registry[$schema][$table])) {
160 $_raw_indexes = PMA_DBI_fetch_result(PMA_DBI_get_table_indexes_sql($schema, $table));
161 foreach ($_raw_indexes as $_each_index) {
162 $_each_index['Schema'] = $schema;
163 if (! isset(PMA_Index
::$_registry[$schema][$table][$_each_index['Key_name']])) {
164 $key = new PMA_Index($_each_index);
165 PMA_Index
::$_registry[$schema][$table][$_each_index['Key_name']] = $key;
167 $key = PMA_Index
::$_registry[$schema][$table][$_each_index['Key_name']];
170 $key->addColumn($_each_index);
177 * Add column to index
179 * @param array $params column params
181 public function addColumn($params)
183 if (strlen($params['Column_name'])) {
184 $this->_columns
[$params['Column_name']] = new PMA_Index_Column($params);
188 public function addColumns($columns)
192 if (isset($columns['names'])) {
195 // $columns[sub_parts][]
196 foreach ($columns['names'] as $key => $name) {
197 $sub_part = isset($columns['sub_parts'][$key]) ?
$columns['sub_parts'][$key] : '';
199 'Column_name' => $name,
200 'Sub_part' => $sub_part,
204 // coming from SHOW INDEXES
206 // $columns[][sub_part]
208 $_columns = $columns;
211 foreach ($_columns as $column) {
212 $this->addColumn($column);
217 * Returns true if $column indexed in this index
219 * @param string $column
222 public function hasColumn($column)
224 return isset($this->_columns
[$column]);
227 public function set($params)
229 if (isset($params['columns'])) {
230 $this->addColumns($params['columns']);
232 if (isset($params['Schema'])) {
233 $this->_schema
= $params['Schema'];
235 if (isset($params['Table'])) {
236 $this->_table
= $params['Table'];
238 if (isset($params['Key_name'])) {
239 $this->_name
= $params['Key_name'];
241 if (isset($params['Index_type'])) {
242 $this->_type
= $params['Index_type'];
244 if (isset($params['Comment'])) {
245 $this->_remarks
= $params['Comment'];
247 if (isset($params['Index_comment'])) {
248 $this->_comment
= $params['Index_comment'];
250 if (isset($params['Non_unique'])) {
251 $this->_non_unique
= $params['Non_unique'];
253 if (isset($params['Packed'])) {
254 $this->_packed
= $params['Packed'];
256 if ('PRIMARY' == $this->_name
) {
257 $this->_choice
= 'PRIMARY';
258 } elseif ('FULLTEXT' == $this->_type
) {
259 $this->_choice
= 'FULLTEXT';
260 } elseif ('SPATIAL' == $this->_type
) {
261 $this->_choice
= 'SPATIAL';
262 } elseif ('0' == $this->_non_unique
) {
263 $this->_choice
= 'UNIQUE';
265 $this->_choice
= 'INDEX';
269 public function getColumnCount()
271 return count($this->_columns
);
274 public function getComment()
276 return $this->_comment
;
279 public function getRemarks()
281 return $this->_remarks
;
284 public function getComments()
286 $comments = $this->getRemarks();
287 if (strlen($comments)) {
290 $comments .= $this->getComment();
295 public function getType()
300 public function getChoice()
302 return $this->_choice
;
306 * Return a list of all index choices
308 * @return array index choices
310 static public function getIndexChoices()
321 public function generateIndexSelector()
325 foreach (PMA_Index
::getIndexChoices() as $each_index_choice) {
326 if ($each_index_choice === 'PRIMARY'
327 && $this->_choice
!== 'PRIMARY'
328 && PMA_Index
::getPrimary($this->_table
, $this->_schema
)) {
329 // skip PRIMARY if there is already one in the table
332 $html_options .= '<option value="' . $each_index_choice . '"'
333 . (($this->_choice
== $each_index_choice) ?
' selected="selected"' : '')
334 . '>'. $each_index_choice . '</option>' . "\n";
337 return $html_options;
340 public function getPacked()
342 return $this->_packed
;
345 public function isPacked($as_text = false)
359 if (null === $this->_packed
) {
363 return $this->_packed
;
366 public function getNonUnique()
368 return $this->_non_unique
;
371 public function isUnique($as_text = false)
385 return $r[$this->_non_unique
];
388 public function getName()
393 public function setName($name)
395 $this->_name
= (string) $name;
398 public function getColumns()
400 return $this->_columns
;
406 * @param string $table The tablename
407 * @param array $indexes_info Referenced info array
408 * @param array $indexes_data Referenced data array
409 * @param boolean $print_mode
411 * @return array Index collection array
413 static public function getView($table, $schema, $print_mode = false)
415 $indexes = PMA_Index
::getFromTable($table, $schema);
417 if (count($indexes) < 1) {
418 return PMA_Message
::error(__('No index defined!'))->getDisplay();
423 $r .= '<h2 id="index_header">' . __('Indexes') . ': ';
424 $r .= PMA_showMySQLDocu('optimization', 'optimizing-database-structure');
426 $r .= '<table id="table_index">';
430 $r .= '<th colspan="2">' . __('Action') . '</th>';
432 $r .= '<th>' . __('Keyname') . '</th>';
433 $r .= '<th>' . __('Type') . '</th>';
434 $r .= '<th>' . __('Unique') . '</th>';
435 $r .= '<th>' . __('Packed') . '</th>';
436 $r .= '<th>' . __('Column') . '</th>';
437 $r .= '<th>' . __('Cardinality') . '</th>';
438 $r .= '<th>' . __('Collation') . '</th>';
439 $r .= '<th>' . __('Null') . '</th>';
440 $r .= '<th>' . __('Comment') . '</th>';
446 foreach ($indexes as $index) {
447 $row_span = ' rowspan="' . $index->getColumnCount() . '" ';
449 $r .= '<tr class="' . ($odd_row ?
'odd' : 'even') . '">';
452 $this_params = $GLOBALS['url_params'];
453 $this_params['index'] = $index->getName();
454 $r .= '<td class="edit_index ';
455 if ($GLOBALS['cfg']['AjaxEnable']) {
458 $r .= '" ' . $row_span . '>'
459 . ' <a href="tbl_indexes.php' . PMA_generate_common_url($this_params)
460 . '">' . PMA_getIcon('b_edit.png', __('Edit')) . '</a>'
463 $this_params = $GLOBALS['url_params'];
464 if ($index->getName() == 'PRIMARY') {
465 $this_params['sql_query'] = 'ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY';
466 $this_params['message_to_show'] = __('The primary key has been dropped');
467 $js_msg = PMA_jsFormat('ALTER TABLE ' . $table . ' DROP PRIMARY KEY');
469 $this_params['sql_query'] = 'ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index->getName());
470 $this_params['message_to_show'] = sprintf(__('Index %s has been dropped'), $index->getName());
471 $js_msg = PMA_jsFormat('ALTER TABLE ' . $table . ' DROP INDEX ' . $index->getName());
474 $r .= '<td ' . $row_span . '>';
475 $r .= '<input type="hidden" class="drop_primary_key_index_msg" value="' . $js_msg . '" />';
477 if ($GLOBALS['cfg']['AjaxEnable']) {
478 $r .= 'class="drop_primary_key_index_anchor" ';
480 $r .= ' href="sql.php' . PMA_generate_common_url($this_params)
482 . PMA_getIcon('b_drop.png', __('Drop')) . '</a>'
486 $r .= '<th ' . $row_span . '>' . htmlspecialchars($index->getName()) . '</th>';
487 $r .= '<td ' . $row_span . '>' . htmlspecialchars($index->getType()) . '</td>';
488 $r .= '<td ' . $row_span . '>' . $index->isUnique(true) . '</td>';
489 $r .= '<td ' . $row_span . '>' . $index->isPacked(true) . '</td>';
491 foreach ($index->getColumns() as $column) {
492 if ($column->getSeqInIndex() > 1) {
493 $r .= '<tr class="' . ($odd_row ?
'odd' : 'even') . '">';
495 $r .= '<td>' . htmlspecialchars($column->getName());
496 if ($column->getSubPart()) {
497 $r .= ' (' . $column->getSubPart() . ')';
500 $r .= '<td>' . htmlspecialchars($column->getCardinality()) . '</td>';
501 $r .= '<td>' . htmlspecialchars($column->getCollation()) . '</td>';
502 $r .= '<td>' . htmlspecialchars($column->getNull(true)) . '</td>';
504 if ($column->getSeqInIndex() == 1) {
505 $r .= '<td ' . $row_span . '>'
506 . htmlspecialchars($index->getComments()) . '</td>';
509 } // end foreach $index['Sequences']
511 $odd_row = ! $odd_row;
517 $r .= PMA_Index
::findDuplicates($table, $schema);
523 public function getCompareData()
526 // 'Non_unique' => $this->_non_unique,
527 'Packed' => $this->_packed
,
528 'Index_type' => $this->_type
,
531 foreach ($this->_columns
as $column) {
532 $data['columns'][] = $column->getCompareData();
539 * Function to check over array of indexes and look for common problems
542 * @param string name of table
543 * @return string Output HTML
545 static public function findDuplicates($table, $schema)
547 $indexes = PMA_Index
::getFromTable($table, $schema);
551 // count($indexes) < 2:
552 // there is no need to check if there less than two indexes
553 if (count($indexes) < 2) {
557 // remove last index from stack and ...
558 while ($while_index = array_pop($indexes)) {
559 // ... compare with every remaining index in stack
560 foreach ($indexes as $each_index) {
561 if ($each_index->getCompareData() !== $while_index->getCompareData()) {
565 // did not find any difference
566 // so it makes no sense to have this two equal indexes
568 $message = PMA_Message
::error(__('The indexes %1$s and %2$s seem to be equal and one of them could possibly be removed.'));
569 $message->addParam($each_index->getName());
570 $message->addParam($while_index->getName());
571 $output .= $message->getDisplay();
573 // there is no need to check any further indexes if we have already
574 // found that this one has a duplicate
583 * @package phpMyAdmin
585 class PMA_Index_Column
588 * @var string The column name
590 protected $_name = '';
593 * @var integer The column sequence number in the index, starting with 1.
595 protected $_seq_in_index = 1;
598 * @var string How the column is sorted in the index. “A” (Ascending) or NULL (Not sorted)
600 protected $_collation = null;
603 * The number of indexed characters if the column is only partly indexed,
604 * NULL if the entire column is indexed.
608 protected $_sub_part = null;
611 * Contains YES if the column may contain NULL.
612 * If not, the column contains NO.
616 protected $_null = '';
619 * An estimate of the number of unique values in the index. This is updated
620 * by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on
621 * statistics stored as integers, so the value is not necessarily exact even
622 * for small tables. The higher the cardinality, the greater the chance that
623 * MySQL uses the index when doing joins.
627 protected $_cardinality = null;
629 public function __construct($params = array())
634 public function set($params)
636 if (isset($params['Column_name'])) {
637 $this->_name
= $params['Column_name'];
639 if (isset($params['Seq_in_index'])) {
640 $this->_seq_in_index
= $params['Seq_in_index'];
642 if (isset($params['Collation'])) {
643 $this->_collation
= $params['Collation'];
645 if (isset($params['Cardinality'])) {
646 $this->_cardinality
= $params['Cardinality'];
648 if (isset($params['Sub_part'])) {
649 $this->_sub_part
= $params['Sub_part'];
651 if (isset($params['Null'])) {
652 $this->_null
= $params['Null'];
656 public function getName()
661 public function getCollation()
663 return $this->_collation
;
666 public function getCardinality()
668 return $this->_cardinality
;
671 public function getNull($as_text = false)
674 ?
(!$this->_null ||
$this->_null
== 'NO' ?
__('No') : __('Yes'))
678 public function getSeqInIndex()
680 return $this->_seq_in_index
;
683 public function getSubPart()
685 return $this->_sub_part
;
688 public function getCompareData()
691 'Column_name' => $this->_name
,
692 'Seq_in_index' => $this->_seq_in_index
,
693 'Collation' => $this->_collation
,
694 'Sub_part' => $this->_sub_part
,
695 'Null' => $this->_null
,