Index.class.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * holds the database index class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Index manipulation class
  13. *
  14. * @package PhpMyAdmin
  15. * @since phpMyAdmin 3.0.0
  16. */
  17. class PMA_Index
  18. {
  19. /**
  20. * Class-wide storage container for indexes (caching, singleton)
  21. *
  22. * @var array
  23. */
  24. private static $_registry = array();
  25. /**
  26. * @var string The name of the schema
  27. */
  28. private $_schema = '';
  29. /**
  30. * @var string The name of the table
  31. */
  32. private $_table = '';
  33. /**
  34. * @var string The name of the index
  35. */
  36. private $_name = '';
  37. /**
  38. * Columns in index
  39. *
  40. * @var array
  41. */
  42. private $_columns = array();
  43. /**
  44. * The index method used (BTREE, SPATIAL, FULLTEXT, HASH, RTREE).
  45. *
  46. * @var string
  47. */
  48. private $_type = '';
  49. /**
  50. * The index choice (PRIMARY, UNIQUE, INDEX, SPATIAL, FULLTEXT)
  51. *
  52. * @var string
  53. */
  54. private $_choice = '';
  55. /**
  56. * Various remarks.
  57. *
  58. * @var string
  59. */
  60. private $_remarks = '';
  61. /**
  62. * Any comment provided for the index with a COMMENT attribute when the
  63. * index was created.
  64. *
  65. * @var string
  66. */
  67. private $_comment = '';
  68. /**
  69. * @var integer 0 if the index cannot contain duplicates, 1 if it can.
  70. */
  71. private $_non_unique = 0;
  72. /**
  73. * Indicates how the key is packed. NULL if it is not.
  74. *
  75. * @var string
  76. */
  77. private $_packed = null;
  78. /**
  79. * Constructor
  80. *
  81. * @param array $params parameters
  82. */
  83. public function __construct($params = array())
  84. {
  85. $this->set($params);
  86. }
  87. /**
  88. * Creates(if not already created) and returns the corresponding Index object
  89. *
  90. * @param string $schema database name
  91. * @param string $table table name
  92. * @param string $index_name index name
  93. *
  94. * @return PMA_Index corresponding Index object
  95. */
  96. static public function singleton($schema, $table, $index_name = '')
  97. {
  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;
  104. }
  105. return $index;
  106. } else {
  107. return PMA_Index::$_registry[$schema][$table][$index_name];
  108. }
  109. }
  110. /**
  111. * returns an array with all indexes from the given table
  112. *
  113. * @param string $table table
  114. * @param string $schema schema
  115. *
  116. * @return array array of indexes
  117. */
  118. static public function getFromTable($table, $schema)
  119. {
  120. PMA_Index::_loadIndexes($table, $schema);
  121. if (isset(PMA_Index::$_registry[$schema][$table])) {
  122. return PMA_Index::$_registry[$schema][$table];
  123. } else {
  124. return array();
  125. }
  126. }
  127. /**
  128. * return primary if set, false otherwise
  129. *
  130. * @param string $table table
  131. * @param string $schema schema
  132. *
  133. * @return mixed primary index or false if no one exists
  134. */
  135. static public function getPrimary($table, $schema)
  136. {
  137. PMA_Index::_loadIndexes($table, $schema);
  138. if (isset(PMA_Index::$_registry[$schema][$table]['PRIMARY'])) {
  139. return PMA_Index::$_registry[$schema][$table]['PRIMARY'];
  140. } else {
  141. return false;
  142. }
  143. }
  144. /**
  145. * Load index data for table
  146. *
  147. * @param string $table table
  148. * @param string $schema schema
  149. *
  150. * @return boolean whether loading was successful
  151. */
  152. static private function _loadIndexes($table, $schema)
  153. {
  154. if (isset(PMA_Index::$_registry[$schema][$table])) {
  155. return true;
  156. }
  157. $_raw_indexes = $GLOBALS['dbi']->getTableIndexes($schema, $table);
  158. foreach ($_raw_indexes as $_each_index) {
  159. $_each_index['Schema'] = $schema;
  160. $keyName = $_each_index['Key_name'];
  161. if (! isset(PMA_Index::$_registry[$schema][$table][$keyName])) {
  162. $key = new PMA_Index($_each_index);
  163. PMA_Index::$_registry[$schema][$table][$keyName] = $key;
  164. } else {
  165. $key = PMA_Index::$_registry[$schema][$table][$keyName];
  166. }
  167. $key->addColumn($_each_index);
  168. }
  169. return true;
  170. }
  171. /**
  172. * Add column to index
  173. *
  174. * @param array $params column params
  175. *
  176. * @return void
  177. */
  178. public function addColumn($params)
  179. {
  180. if (strlen($params['Column_name'])) {
  181. $this->_columns[$params['Column_name']] = new PMA_Index_Column($params);
  182. }
  183. }
  184. /**
  185. * Adds a list of columns to the index
  186. *
  187. * @param array $columns array containing details about the columns
  188. *
  189. * @return void
  190. */
  191. public function addColumns($columns)
  192. {
  193. $_columns = array();
  194. if (isset($columns['names'])) {
  195. // coming from form
  196. // $columns[names][]
  197. // $columns[sub_parts][]
  198. foreach ($columns['names'] as $key => $name) {
  199. $sub_part = isset($columns['sub_parts'][$key])
  200. ? $columns['sub_parts'][$key] : '';
  201. $_columns[] = array(
  202. 'Column_name' => $name,
  203. 'Sub_part' => $sub_part,
  204. );
  205. }
  206. } else {
  207. // coming from SHOW INDEXES
  208. // $columns[][name]
  209. // $columns[][sub_part]
  210. // ...
  211. $_columns = $columns;
  212. }
  213. foreach ($_columns as $column) {
  214. $this->addColumn($column);
  215. }
  216. }
  217. /**
  218. * Returns true if $column indexed in this index
  219. *
  220. * @param string $column the column
  221. *
  222. * @return boolean true if $column indexed in this index
  223. */
  224. public function hasColumn($column)
  225. {
  226. return isset($this->_columns[$column]);
  227. }
  228. /**
  229. * Sets index details
  230. *
  231. * @param array $params index details
  232. *
  233. * @return void
  234. */
  235. public function set($params)
  236. {
  237. if (isset($params['columns'])) {
  238. $this->addColumns($params['columns']);
  239. }
  240. if (isset($params['Schema'])) {
  241. $this->_schema = $params['Schema'];
  242. }
  243. if (isset($params['Table'])) {
  244. $this->_table = $params['Table'];
  245. }
  246. if (isset($params['Key_name'])) {
  247. $this->_name = $params['Key_name'];
  248. }
  249. if (isset($params['Index_type'])) {
  250. $this->_type = $params['Index_type'];
  251. }
  252. if (isset($params['Comment'])) {
  253. $this->_remarks = $params['Comment'];
  254. }
  255. if (isset($params['Index_comment'])) {
  256. $this->_comment = $params['Index_comment'];
  257. }
  258. if (isset($params['Non_unique'])) {
  259. $this->_non_unique = $params['Non_unique'];
  260. }
  261. if (isset($params['Packed'])) {
  262. $this->_packed = $params['Packed'];
  263. }
  264. if ('PRIMARY' == $this->_name) {
  265. $this->_choice = 'PRIMARY';
  266. } elseif ('FULLTEXT' == $this->_type) {
  267. $this->_choice = 'FULLTEXT';
  268. } elseif ('SPATIAL' == $this->_type) {
  269. $this->_choice = 'SPATIAL';
  270. } elseif ('0' == $this->_non_unique) {
  271. $this->_choice = 'UNIQUE';
  272. } else {
  273. $this->_choice = 'INDEX';
  274. }
  275. }
  276. /**
  277. * Returns the number of columns of the index
  278. *
  279. * @return integer the number of the columns
  280. */
  281. public function getColumnCount()
  282. {
  283. return count($this->_columns);
  284. }
  285. /**
  286. * Returns the index comment
  287. *
  288. * @return string index comment
  289. */
  290. public function getComment()
  291. {
  292. return $this->_comment;
  293. }
  294. /**
  295. * Returns index remarks
  296. *
  297. * @return string index remarks
  298. */
  299. public function getRemarks()
  300. {
  301. return $this->_remarks;
  302. }
  303. /**
  304. * Returns concatenated remarks and comment
  305. *
  306. * @return string concatenated remarks and comment
  307. */
  308. public function getComments()
  309. {
  310. $comments = $this->getRemarks();
  311. if (strlen($comments)) {
  312. $comments .= "\n";
  313. }
  314. $comments .= $this->getComment();
  315. return $comments;
  316. }
  317. /**
  318. * Returns index type ((BTREE, SPATIAL, FULLTEXT, HASH, RTREE)
  319. *
  320. * @return string index type
  321. */
  322. public function getType()
  323. {
  324. return $this->_type;
  325. }
  326. /**
  327. * Returns index choice (PRIMARY, UNIQUE, INDEX, SPATIAL, FULLTEXT)
  328. *
  329. * @return string index choice
  330. */
  331. public function getChoice()
  332. {
  333. return $this->_choice;
  334. }
  335. /**
  336. * Return a list of all index choices
  337. *
  338. * @return array index choices
  339. */
  340. static public function getIndexChoices()
  341. {
  342. return array(
  343. 'PRIMARY',
  344. 'INDEX',
  345. 'UNIQUE',
  346. 'SPATIAL',
  347. 'FULLTEXT',
  348. );
  349. }
  350. /**
  351. * Returns HTML for the index choice selector
  352. *
  353. * @return string HTML for the index choice selector
  354. */
  355. public function generateIndexSelector()
  356. {
  357. $html_options = '';
  358. foreach (PMA_Index::getIndexChoices() as $each_index_choice) {
  359. if ($each_index_choice === 'PRIMARY'
  360. && $this->_choice !== 'PRIMARY'
  361. && PMA_Index::getPrimary($this->_table, $this->_schema)
  362. ) {
  363. // skip PRIMARY if there is already one in the table
  364. continue;
  365. }
  366. $html_options .= '<option value="' . $each_index_choice . '"'
  367. . (($this->_choice == $each_index_choice)
  368. ? ' selected="selected"'
  369. : '')
  370. . '>' . $each_index_choice . '</option>' . "\n";
  371. }
  372. return $html_options;
  373. }
  374. /**
  375. * Returns how the index is packed
  376. *
  377. * @return string how the index is packed
  378. */
  379. public function getPacked()
  380. {
  381. return $this->_packed;
  382. }
  383. /**
  384. * Returns 'No'/false if the index is not packed,
  385. * how the index is packed if packed
  386. *
  387. * @param boolean $as_text whether to output should be in text
  388. *
  389. * @return mixed how index is paked
  390. */
  391. public function isPacked($as_text = false)
  392. {
  393. if ($as_text) {
  394. $r = array(
  395. '0' => __('No'),
  396. '1' => __('Yes'),
  397. );
  398. } else {
  399. $r = array(
  400. '0' => false,
  401. '1' => true,
  402. );
  403. }
  404. if (null === $this->_packed) {
  405. return $r[0];
  406. }
  407. return $this->_packed;
  408. }
  409. /**
  410. * Returns integer 0 if the index cannot contain duplicates, 1 if it can
  411. *
  412. * @return integer 0 if the index cannot contain duplicates, 1 if it can
  413. */
  414. public function getNonUnique()
  415. {
  416. return $this->_non_unique;
  417. }
  418. /**
  419. * Returns whether the index is a 'Unique' index
  420. *
  421. * @param boolean $as_text whether to output should be in text
  422. *
  423. * @return mixed whether the index is a 'Unique' index
  424. */
  425. public function isUnique($as_text = false)
  426. {
  427. if ($as_text) {
  428. $r = array(
  429. '0' => __('Yes'),
  430. '1' => __('No'),
  431. );
  432. } else {
  433. $r = array(
  434. '0' => true,
  435. '1' => false,
  436. );
  437. }
  438. return $r[$this->_non_unique];
  439. }
  440. /**
  441. * Returns the name of the index
  442. *
  443. * @return string the name of the index
  444. */
  445. public function getName()
  446. {
  447. return $this->_name;
  448. }
  449. /**
  450. * Sets the name of the index
  451. *
  452. * @param string $name index name
  453. *
  454. * @return void
  455. */
  456. public function setName($name)
  457. {
  458. $this->_name = (string) $name;
  459. }
  460. /**
  461. * Returns the columns of the index
  462. *
  463. * @return PMA_Index_Column[] the columns of the index
  464. */
  465. public function getColumns()
  466. {
  467. return $this->_columns;
  468. }
  469. /**
  470. * Show index data
  471. *
  472. * @param string $table The table name
  473. * @param string $schema The schema name
  474. * @param boolean $print_mode Whether the output is for the print mode
  475. *
  476. * @return array Index collection array
  477. *
  478. * @access public
  479. */
  480. static public function getView($table, $schema, $print_mode = false)
  481. {
  482. $indexes = PMA_Index::getFromTable($table, $schema);
  483. $no_indexes_class = count($indexes) > 0 ? ' hide' : '';
  484. $no_indexes = "<div class='no_indexes_defined$no_indexes_class'>";
  485. $no_indexes .= PMA_Message::notice(__('No index defined!'))->getDisplay();
  486. $no_indexes .= '</div>';
  487. if (! $print_mode) {
  488. $r = '<fieldset class="index_info">';
  489. $r .= '<legend id="index_header">' . __('Indexes');
  490. $r .= PMA_Util::showMySQLDocu('optimizing-database-structure');
  491. $r .= '</legend>';
  492. $r .= $no_indexes;
  493. if (count($indexes) < 1) {
  494. $r .= '</fieldset>';
  495. return $r;
  496. }
  497. $r .= PMA_Index::findDuplicates($table, $schema);
  498. } else {
  499. $r = '<h3>' . __('Indexes') . '</h3>';
  500. $r .= $no_indexes;
  501. if (count($indexes) < 1) {
  502. return $r;
  503. }
  504. }
  505. $r .= '<table id="table_index">';
  506. $r .= '<thead>';
  507. $r .= '<tr>';
  508. if (! $print_mode) {
  509. $r .= '<th colspan="2">' . __('Action') . '</th>';
  510. }
  511. $r .= '<th>' . __('Keyname') . '</th>';
  512. $r .= '<th>' . __('Type') . '</th>';
  513. $r .= '<th>' . __('Unique') . '</th>';
  514. $r .= '<th>' . __('Packed') . '</th>';
  515. $r .= '<th>' . __('Column') . '</th>';
  516. $r .= '<th>' . __('Cardinality') . '</th>';
  517. $r .= '<th>' . __('Collation') . '</th>';
  518. $r .= '<th>' . __('Null') . '</th>';
  519. if (PMA_MYSQL_INT_VERSION > 50500) {
  520. $r .= '<th>' . __('Comment') . '</th>';
  521. }
  522. $r .= '</tr>';
  523. $r .= '</thead>';
  524. $r .= '<tbody>';
  525. $odd_row = true;
  526. foreach ($indexes as $index) {
  527. $row_span = ' rowspan="' . $index->getColumnCount() . '" ';
  528. $r .= '<tr class="noclick ' . ($odd_row ? 'odd' : 'even') . '">';
  529. if (! $print_mode) {
  530. $this_params = $GLOBALS['url_params'];
  531. $this_params['index'] = $index->getName();
  532. $r .= '<td class="edit_index';
  533. $r .= ' ajax';
  534. $r .= '" ' . $row_span . '>'
  535. . ' <a class="';
  536. $r .= 'ajax';
  537. $r .= '" href="tbl_indexes.php' . PMA_URL_getCommon($this_params)
  538. . '">' . PMA_Util::getIcon('b_edit.png', __('Edit')) . '</a>'
  539. . '</td>' . "\n";
  540. $this_params = $GLOBALS['url_params'];
  541. if ($index->getName() == 'PRIMARY') {
  542. $this_params['sql_query'] = 'ALTER TABLE '
  543. . PMA_Util::backquote($table)
  544. . ' DROP PRIMARY KEY;';
  545. $this_params['message_to_show']
  546. = __('The primary key has been dropped.');
  547. $js_msg = PMA_jsFormat(
  548. 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY'
  549. );
  550. } else {
  551. $this_params['sql_query'] = 'ALTER TABLE '
  552. . PMA_Util::backquote($table) . ' DROP INDEX '
  553. . PMA_Util::backquote($index->getName()) . ';';
  554. $this_params['message_to_show'] = sprintf(
  555. __('Index %s has been dropped.'), $index->getName()
  556. );
  557. $js_msg = PMA_jsFormat(
  558. 'ALTER TABLE ' . $table . ' DROP INDEX '
  559. . $index->getName() . ';'
  560. );
  561. }
  562. $r .= '<td ' . $row_span . '>';
  563. $r .= '<input type="hidden" class="drop_primary_key_index_msg"'
  564. . ' value="' . $js_msg . '" />';
  565. $r .= ' <a class="drop_primary_key_index_anchor';
  566. $r .= ' ajax';
  567. $r .= '" href="sql.php' . PMA_URL_getCommon($this_params)
  568. . '" >'
  569. . PMA_Util::getIcon('b_drop.png', __('Drop')) . '</a>'
  570. . '</td>' . "\n";
  571. }
  572. if (! $print_mode) {
  573. $r .= '<th ' . $row_span . '>'
  574. . htmlspecialchars($index->getName())
  575. . '</th>';
  576. } else {
  577. $r .= '<td ' . $row_span . '>'
  578. . htmlspecialchars($index->getName())
  579. . '</td>';
  580. }
  581. $r .= '<td ' . $row_span . '>'
  582. . htmlspecialchars($index->getType())
  583. . '</td>';
  584. $r .= '<td ' . $row_span . '>' . $index->isUnique(true) . '</td>';
  585. $r .= '<td ' . $row_span . '>' . $index->isPacked(true) . '</td>';
  586. foreach ($index->getColumns() as $column) {
  587. if ($column->getSeqInIndex() > 1) {
  588. $r .= '<tr class="noclick ' . ($odd_row ? 'odd' : 'even') . '">';
  589. }
  590. $r .= '<td>' . htmlspecialchars($column->getName());
  591. if ($column->getSubPart()) {
  592. $r .= ' (' . $column->getSubPart() . ')';
  593. }
  594. $r .= '</td>';
  595. $r .= '<td>'
  596. . htmlspecialchars($column->getCardinality())
  597. . '</td>';
  598. $r .= '<td>'
  599. . htmlspecialchars($column->getCollation())
  600. . '</td>';
  601. $r .= '<td>'
  602. . htmlspecialchars($column->getNull(true))
  603. . '</td>';
  604. if (PMA_MYSQL_INT_VERSION > 50500
  605. && $column->getSeqInIndex() == 1
  606. ) {
  607. $r .= '<td ' . $row_span . '>'
  608. . htmlspecialchars($index->getComments()) . '</td>';
  609. }
  610. $r .= '</tr>';
  611. } // end foreach $index['Sequences']
  612. $odd_row = ! $odd_row;
  613. } // end while
  614. $r .= '</tbody>';
  615. $r .= '</table>';
  616. if (! $print_mode) {
  617. $r .= '</fieldset>';
  618. }
  619. return $r;
  620. }
  621. /**
  622. * Gets the properties in an array for comparison purposes
  623. *
  624. * @return array an array containing the properties of the index
  625. */
  626. public function getCompareData()
  627. {
  628. $data = array(
  629. // 'Non_unique' => $this->_non_unique,
  630. 'Packed' => $this->_packed,
  631. 'Index_type' => $this->_type,
  632. );
  633. foreach ($this->_columns as $column) {
  634. $data['columns'][] = $column->getCompareData();
  635. }
  636. return $data;
  637. }
  638. /**
  639. * Function to check over array of indexes and look for common problems
  640. *
  641. * @param string $table table name
  642. * @param string $schema schema name
  643. *
  644. * @return string Output HTML
  645. * @access public
  646. */
  647. static public function findDuplicates($table, $schema)
  648. {
  649. $indexes = PMA_Index::getFromTable($table, $schema);
  650. $output = '';
  651. // count($indexes) < 2:
  652. // there is no need to check if there less than two indexes
  653. if (count($indexes) < 2) {
  654. return $output;
  655. }
  656. // remove last index from stack and ...
  657. while ($while_index = array_pop($indexes)) {
  658. // ... compare with every remaining index in stack
  659. foreach ($indexes as $each_index) {
  660. if ($each_index->getCompareData() !== $while_index->getCompareData()
  661. ) {
  662. continue;
  663. }
  664. // did not find any difference
  665. // so it makes no sense to have this two equal indexes
  666. $message = PMA_Message::notice(
  667. __('The indexes %1$s and %2$s seem to be equal and one of them could possibly be removed.')
  668. );
  669. $message->addParam($each_index->getName());
  670. $message->addParam($while_index->getName());
  671. $output .= $message->getDisplay();
  672. // there is no need to check any further indexes if we have already
  673. // found that this one has a duplicate
  674. continue 2;
  675. }
  676. }
  677. return $output;
  678. }
  679. }
  680. /**
  681. * Index column wrapper
  682. *
  683. * @package PhpMyAdmin
  684. */
  685. class PMA_Index_Column
  686. {
  687. /**
  688. * @var string The column name
  689. */
  690. private $_name = '';
  691. /**
  692. * @var integer The column sequence number in the index, starting with 1.
  693. */
  694. private $_seq_in_index = 1;
  695. /**
  696. * @var string How the column is sorted in the index. “A” (Ascending) or
  697. * NULL (Not sorted)
  698. */
  699. private $_collation = null;
  700. /**
  701. * The number of indexed characters if the column is only partly indexed,
  702. * NULL if the entire column is indexed.
  703. *
  704. * @var integer
  705. */
  706. private $_sub_part = null;
  707. /**
  708. * Contains YES if the column may contain NULL.
  709. * If not, the column contains NO.
  710. *
  711. * @var string
  712. */
  713. private $_null = '';
  714. /**
  715. * An estimate of the number of unique values in the index. This is updated
  716. * by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on
  717. * statistics stored as integers, so the value is not necessarily exact even
  718. * for small tables. The higher the cardinality, the greater the chance that
  719. * MySQL uses the index when doing joins.
  720. *
  721. * @var integer
  722. */
  723. private $_cardinality = null;
  724. /**
  725. * Constructor
  726. *
  727. * @param array $params an array containing the parameters of the index column
  728. */
  729. public function __construct($params = array())
  730. {
  731. $this->set($params);
  732. }
  733. /**
  734. * Sets parameters of the index column
  735. *
  736. * @param array $params an array containing the parameters of the index column
  737. *
  738. * @return void
  739. */
  740. public function set($params)
  741. {
  742. if (isset($params['Column_name'])) {
  743. $this->_name = $params['Column_name'];
  744. }
  745. if (isset($params['Seq_in_index'])) {
  746. $this->_seq_in_index = $params['Seq_in_index'];
  747. }
  748. if (isset($params['Collation'])) {
  749. $this->_collation = $params['Collation'];
  750. }
  751. if (isset($params['Cardinality'])) {
  752. $this->_cardinality = $params['Cardinality'];
  753. }
  754. if (isset($params['Sub_part'])) {
  755. $this->_sub_part = $params['Sub_part'];
  756. }
  757. if (isset($params['Null'])) {
  758. $this->_null = $params['Null'];
  759. }
  760. }
  761. /**
  762. * Returns the column name
  763. *
  764. * @return string column name
  765. */
  766. public function getName()
  767. {
  768. return $this->_name;
  769. }
  770. /**
  771. * Return the column collation
  772. *
  773. * @return string column collation
  774. */
  775. public function getCollation()
  776. {
  777. return $this->_collation;
  778. }
  779. /**
  780. * Returns the cardinality of the column
  781. *
  782. * @return int cardinality of the column
  783. */
  784. public function getCardinality()
  785. {
  786. return $this->_cardinality;
  787. }
  788. /**
  789. * Returns whether the column is nullable
  790. *
  791. * @param boolean $as_text whether to returned the string representation
  792. *
  793. * @return mixed nullability of the column. True/false or Yes/No depending
  794. * on the value of the $as_text parameter
  795. */
  796. public function getNull($as_text = false)
  797. {
  798. return $as_text
  799. ? (!$this->_null || $this->_null == 'NO' ? __('No') : __('Yes'))
  800. : $this->_null;
  801. }
  802. /**
  803. * Returns the sequence number of the column in the index
  804. *
  805. * @return int sequence number of the column in the index
  806. */
  807. public function getSeqInIndex()
  808. {
  809. return $this->_seq_in_index;
  810. }
  811. /**
  812. * Returns the number of indexed characters if the column is only
  813. * partly indexed
  814. *
  815. * @return int the number of indexed characters
  816. */
  817. public function getSubPart()
  818. {
  819. return $this->_sub_part;
  820. }
  821. /**
  822. * Gets the properties in an array for comparison purposes
  823. *
  824. * @return array an array containing the properties of the index column
  825. */
  826. public function getCompareData()
  827. {
  828. return array(
  829. 'Column_name' => $this->_name,
  830. 'Seq_in_index' => $this->_seq_in_index,
  831. 'Collation' => $this->_collation,
  832. 'Sub_part' => $this->_sub_part,
  833. 'Null' => $this->_null,
  834. );
  835. }
  836. }
  837. ?>