2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * holds the datasbe index class
10 * @since phpMyAdmin 3.0.0
16 * Class-wide storage container for indexes (caching, singleton)
20 protected static $_registry = array();
23 * @var string The name of the schema
25 protected $_schema = '';
28 * @var string The name of the table
30 protected $_table = '';
33 * @var string The name of the index
35 protected $_name = '';
42 protected $_columns = array();
45 * The index method used (BTREE, FULLTEXT, HASH, RTREE).
49 protected $_type = '';
56 protected $_remarks = '';
59 * Any comment provided for the index with a COMMENT attribute when the
64 protected $_comment = '';
67 * @var integer 0 if the index cannot contain duplicates, 1 if it can.
69 protected $_non_unique = 0;
72 * Indicates how the key is packed. NULL if it is not.
76 protected $_packed = null;
82 * @param array $params
84 public function __construct($params = array())
89 static public function singleton($schema, $table, $index_name = '')
91 PMA_Index
::_loadIndexes($table, $schema);
92 if (! isset(PMA_Index
::$_registry[$schema][$table][$index_name])) {
93 $index = new PMA_Index
;
94 if (strlen($index_name)) {
95 $index->setName($index_name);
96 PMA_Index
::$_registry[$schema][$table][$index->getName()] = $index;
100 return PMA_Index
::$_registry[$schema][$table][$index_name];
105 * returns an array with all indexes from the given table
107 * @uses PMA_Index::_loadIndexes()
108 * @uses PMA_Index::$_registry
109 * @param string $table
110 * @param string $schema
113 static public function getFromTable($table, $schema)
115 PMA_Index
::_loadIndexes($table, $schema);
117 if (isset(PMA_Index
::$_registry[$schema][$table])) {
118 return PMA_Index
::$_registry[$schema][$table];
125 * return primary if set, false otherwise
127 * @uses PMA_Index::_loadIndexes()
128 * @uses PMA_Index::$_registry
129 * @param string $table
130 * @param string $schema
131 * @return mixed primary index or false if no one exists
133 static public function getPrimary($table, $schema)
135 PMA_Index
::_loadIndexes($table, $schema);
137 if (isset(PMA_Index
::$_registry[$schema][$table]['PRIMARY'])) {
138 return PMA_Index
::$_registry[$schema][$table]['PRIMARY'];
145 * Load index data for table
147 * @uses PMA_Index::$_registry
148 * @uses PMA_DBI_fetch_result()
149 * @uses PMA_backquote()
151 * @uses PMA_Index->addColumn()
152 * @param string $table
153 * @param string $schema
156 static protected function _loadIndexes($table, $schema)
158 if (isset(PMA_Index
::$_registry[$schema][$table])) {
162 $_raw_indexes = PMA_DBI_fetch_result('SHOW INDEX FROM ' . PMA_backquote($schema) . '.' . PMA_backquote($table));
163 foreach ($_raw_indexes as $_each_index) {
164 $_each_index['Schema'] = $schema;
165 if (! isset(PMA_Index
::$_registry[$schema][$table][$_each_index['Key_name']])) {
166 $key = new PMA_Index($_each_index);
167 PMA_Index
::$_registry[$schema][$table][$_each_index['Key_name']] = $key;
169 $key = PMA_Index
::$_registry[$schema][$table][$_each_index['Key_name']];
172 $key->addColumn($_each_index);
179 * Add column to index
181 * @uses $this->_columns
182 * @uses PMA_Index_Column
183 * @param array $params column params
185 public function addColumn($params)
187 if (strlen($params['Column_name'])) {
188 $this->_columns
[$params['Column_name']] = new PMA_Index_Column($params);
192 public function addColumns($columns)
196 if (isset($columns['names'])) {
199 // $columns[sub_parts][]
200 foreach ($columns['names'] as $key => $name) {
202 'Column_name' => $name,
203 'Sub_part' => $columns['sub_parts'][$key],
207 // coming from SHOW INDEXES
209 // $columns[][sub_part]
211 $_columns = $columns;
214 foreach ($_columns as $column) {
215 $this->addColumn($column);
220 * Returns true if $column indexed in this index
222 * @uses $this->_columns
223 * @param string $column
226 public function hasColumn($column)
228 return isset($this->_columns
[$column]);
231 public function set($params)
233 if (isset($params['columns'])) {
234 $this->addColumns($params['columns']);
236 if (isset($params['Schema'])) {
237 $this->_schema
= $params['Schema'];
239 if (isset($params['Table'])) {
240 $this->_table
= $params['Table'];
242 if (isset($params['Key_name'])) {
243 $this->_name
= $params['Key_name'];
245 if (isset($params['Index_type'])) {
246 $this->_type
= $params['Index_type'];
248 if (isset($params['Comment'])) {
249 $this->_remarks
= $params['Comment'];
251 if (isset($params['Index_comment'])) {
252 $this->_comment
= $params['Index_comment'];
254 if (isset($params['Non_unique'])) {
255 $this->_non_unique
= $params['Non_unique'];
257 if (isset($params['Packed'])) {
258 $this->_packed
= $params['Packed'];
262 public function getColumnCount()
264 return count($this->_columns
);
267 public function getComment()
269 return $this->_comment
;
272 public function getRemarks()
274 return $this->_remarks
;
277 public function getComments()
279 $comments = $this->getRemarks();
280 if (strlen($comments)) {
283 $comments .= $this->getComment();
288 public function getType()
294 * Return a list of all index types
296 * @return array index types
298 static public function getTypes()
308 public function getTypeSelector()
312 foreach (PMA_Index
::getTypes() as $each_index_type) {
313 if ($each_index_type === 'PRIMARY'
314 && $this->_name
!== 'PRIMARY'
315 && PMA_Index
::getPrimary($this->_table
, $this->_schema
)) {
316 // skip PRIMARY if there is already one in the table
319 $html_options .= '<option value="' . $each_index_type . '"'
320 . (($this->_type
== $each_index_type) ?
' selected="selected"' : '')
321 . '>'. $each_index_type . '</option>' . "\n";
324 return $html_options;
327 public function getPacked()
329 return $this->_packed
;
332 public function isPacked($as_text = false)
336 '0' => $GLOBALS['strNo'],
337 '1' => $GLOBALS['strYes'],
346 if (null === $this->_packed
) {
350 return $this->_packed
;
353 public function getNonUnique()
355 return $this->_non_unique
;
358 public function isUnique($as_text = false)
362 '0' => $GLOBALS['strYes'],
363 '1' => $GLOBALS['strNo'],
372 return $r[$this->_non_unique
];
375 public function getName()
380 public function setName($name)
382 $this->_name
= (string) $name;
385 public function getColumns()
387 return $this->_columns
;
393 * @param string $table The tablename
394 * @param array $indexes_info Referenced info array
395 * @param array $indexes_data Referenced data array
396 * @param boolean $print_mode
398 * @return array Index collection array
399 * @author Garvin Hicking (pma@supergarv.de)
401 static public function getView($table, $schema, $print_mode = false)
403 $indexes = PMA_Index
::getFromTable($table, $schema);
405 if (count($indexes) < 1) {
406 return PMA_Message
::warning('strNoIndex')->getDisplay();
411 $r .= '<h2>' . $GLOBALS['strIndexes'] . ': ';
412 $r .= PMA_showMySQLDocu('optimization', 'optimizing-database-structure');
418 $r .= '<th colspan="2">' . $GLOBALS['strAction'] . '</th>';
420 $r .= '<th>' . $GLOBALS['strKeyname'] . '</th>';
421 $r .= '<th>' . $GLOBALS['strType'] . '</th>';
422 $r .= '<th>' . $GLOBALS['strUnique'] . '</th>';
423 $r .= '<th>' . $GLOBALS['strPacked'] . '</th>';
424 $r .= '<th>' . $GLOBALS['strField'] . '</th>';
425 $r .= '<th>' . $GLOBALS['strCardinality'] . '</th>';
426 $r .= '<th>' . $GLOBALS['strCollation'] . '</th>';
427 $r .= '<th>' . $GLOBALS['strNull'] . '</th>';
428 $r .= '<th>' . $GLOBALS['strComment'] . '</th>';
434 foreach ($indexes as $index) {
435 $row_span = ' rowspan="' . $index->getColumnCount() . '" ';
437 $r .= '<tr class="' . ($odd_row ?
'odd' : 'even') . '">';
440 $this_params = $GLOBALS['url_params'];
441 $this_params['index'] = $index->getName();
442 $r .= '<td ' . $row_span . '>'
443 . ' <a href="tbl_indexes.php' . PMA_generate_common_url($this_params)
444 . '">' . PMA_getIcon('b_edit.png', $GLOBALS['strEdit']) . '</a>'
447 $this_params = $GLOBALS['url_params'];
448 if ($index->getName() == 'PRIMARY') {
449 $this_params['sql_query'] = 'ALTER TABLE ' . PMA_backquote($table) . ' DROP PRIMARY KEY';
450 $this_params['zero_rows'] = $GLOBALS['strPrimaryKeyHasBeenDropped'];
451 $js_msg = PMA_jsFormat('ALTER TABLE ' . $table . ' DROP PRIMARY KEY');
453 $this_params['sql_query'] = 'ALTER TABLE ' . PMA_backquote($table) . ' DROP INDEX ' . PMA_backquote($index->getName());
454 $this_params['zero_rows'] = sprintf($GLOBALS['strIndexHasBeenDropped'], $index->getName());
455 $js_msg = PMA_jsFormat('ALTER TABLE ' . $table . ' DROP INDEX ' . $index->getName());
458 $r .= '<td ' . $row_span . '>'
459 . ' <a href="sql.php' . PMA_generate_common_url($this_params)
460 . '" onclick="return confirmLink(this, \'' . $js_msg . '\')">'
461 . PMA_getIcon('b_drop.png', $GLOBALS['strDrop']) . '</a>'
465 $r .= '<th ' . $row_span . '>' . htmlspecialchars($index->getName()) . '</th>';
466 $r .= '<td ' . $row_span . '>' . htmlspecialchars($index->getType()) . '</td>';
467 $r .= '<td ' . $row_span . '>' . $index->isUnique(true) . '</td>';
468 $r .= '<td ' . $row_span . '>' . $index->isPacked(true) . '</td>';
470 foreach ($index->getColumns() as $column) {
471 if ($column->getSeqInIndex() > 1) {
472 $r .= '<tr class="' . ($odd_row ?
'odd' : 'even') . '">';
474 $r .= '<td>' . htmlspecialchars($column->getName());
475 if ($column->getSubPart()) {
476 $r .= ' (' . $column->getSubPart() . ')';
479 $r .= '<td>' . htmlspecialchars($column->getCardinality()) . '</td>';
480 $r .= '<td>' . htmlspecialchars($column->getCollation()) . '</td>';
481 $r .= '<td>' . htmlspecialchars($column->getNull()) . '</td>';
483 if ($column->getSeqInIndex() == 1) {
484 $r .= '<td ' . $row_span . '>'
485 . htmlspecialchars($index->getComments()) . '</td>';
488 } // end foreach $index['Sequences']
490 $odd_row = ! $odd_row;
496 $r .= PMA_Index
::findDuplicates($table, $schema);
502 public function getCompareData()
505 // 'Non_unique' => $this->_non_unique,
506 'Packed' => $this->_packed
,
507 'Index_type' => $this->_type
,
510 foreach ($this->_columns
as $column) {
511 $data['columns'][] = $column->getCompareData();
518 * Function to check over array of indexes and look for common problems
520 * @uses $GLOBALS['strIndexesSeemEqual']
528 * @param string name of table
529 * @return string Output HTML
531 static public function findDuplicates($table, $schema)
533 $indexes = PMA_Index
::getFromTable($table, $schema);
537 // count($indexes) < 2:
538 // there is no need to check if there less than two indexes
539 if (count($indexes) < 2) {
543 // remove last index from stack and ...
544 while ($while_index = array_pop($indexes)) {
545 // ... compare with every remaining index in stack
546 foreach ($indexes as $each_index) {
547 if ($each_index->getCompareData() !== $while_index->getCompareData()) {
551 // did not find any difference
552 // so it makes no sense to have this two equal indexes
554 $message = PMA_Message
::warning('strIndexesSeemEqual');
555 $message->addParam($each_index->getName());
556 $message->addParam($while_index->getName());
557 $output .= $message->getDisplay();
559 // there is no need to check any further indexes if we have already
560 // found that this one has a duplicate
568 class PMA_Index_Column
571 * @var string The column name
573 protected $_name = '';
576 * @var integer The column sequence number in the index, starting with 1.
578 protected $_seq_in_index = 1;
581 * @var string How the column is sorted in the index. “A” (Ascending) or NULL (Not sorted)
583 protected $_collation = null;
586 * The number of indexed characters if the column is only partly indexed,
587 * NULL if the entire column is indexed.
591 protected $_sub_part = null;
594 * Contains YES if the column may contain NULL.
595 * If not, the column contains NO.
599 protected $_null = '';
602 * An estimate of the number of unique values in the index. This is updated
603 * by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on
604 * statistics stored as integers, so the value is not necessarily exact even
605 * for small tables. The higher the cardinality, the greater the chance that
606 * MySQL uses the index when doing joins.
610 protected $_cardinality = 0;
612 public function __construct($params = array())
617 public function set($params)
619 if (isset($params['Column_name'])) {
620 $this->_name
= $params['Column_name'];
622 if (isset($params['Seq_in_index'])) {
623 $this->_seq_in_index
= $params['Seq_in_index'];
625 if (isset($params['Collation'])) {
626 $this->_collation
= $params['Collation'];
628 if (isset($params['Cardinality'])) {
629 $this->_cardinality
= $params['Cardinality'];
631 if (isset($params['Sub_part'])) {
632 $this->_sub_part
= $params['Sub_part'];
634 if (isset($params['Null'])) {
635 $this->_null
= $params['Null'];
639 public function getName()
644 public function getCollation()
646 return $this->_collation
;
649 public function getCardinality()
651 return $this->_cardinality
;
654 public function getNull()
659 public function getSeqInIndex()
661 return $this->_seq_in_index
;
664 public function getSubPart()
666 return $this->_sub_part
;
669 public function getCompareData()
672 'Column_name' => $this->_name
,
673 'Seq_in_index' => $this->_seq_in_index
,
674 'Collation' => $this->_collation
,
675 'Sub_part' => $this->_sub_part
,
676 'Null' => $this->_null
,