IndexColumn.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * holds the database index columns class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. declare(strict_types=1);
  9. namespace PhpMyAdmin;
  10. /**
  11. * Index column wrapper
  12. *
  13. * @package PhpMyAdmin
  14. */
  15. class IndexColumn
  16. {
  17. /**
  18. * @var string The column name
  19. */
  20. private $_name = '';
  21. /**
  22. * @var integer The column sequence number in the index, starting with 1.
  23. */
  24. private $_seq_in_index = 1;
  25. /**
  26. * @var string How the column is sorted in the index. “A” (Ascending) or
  27. * NULL (Not sorted)
  28. */
  29. private $_collation = null;
  30. /**
  31. * The number of indexed characters if the column is only partly indexed,
  32. * NULL if the entire column is indexed.
  33. *
  34. * @var integer
  35. */
  36. private $_sub_part = null;
  37. /**
  38. * Contains YES if the column may contain NULL.
  39. * If not, the column contains NO.
  40. *
  41. * @var string
  42. */
  43. private $_null = '';
  44. /**
  45. * An estimate of the number of unique values in the index. This is updated
  46. * by running ANALYZE TABLE or myisamchk -a. Cardinality is counted based on
  47. * statistics stored as integers, so the value is not necessarily exact even
  48. * for small tables. The higher the cardinality, the greater the chance that
  49. * MySQL uses the index when doing joins.
  50. *
  51. * @var integer
  52. */
  53. private $_cardinality = null;
  54. /**
  55. * Constructor
  56. *
  57. * @param array $params an array containing the parameters of the index column
  58. */
  59. public function __construct(array $params = [])
  60. {
  61. $this->set($params);
  62. }
  63. /**
  64. * Sets parameters of the index column
  65. *
  66. * @param array $params an array containing the parameters of the index column
  67. *
  68. * @return void
  69. */
  70. public function set(array $params)
  71. {
  72. if (isset($params['Column_name'])) {
  73. $this->_name = $params['Column_name'];
  74. }
  75. if (isset($params['Seq_in_index'])) {
  76. $this->_seq_in_index = $params['Seq_in_index'];
  77. }
  78. if (isset($params['Collation'])) {
  79. $this->_collation = $params['Collation'];
  80. }
  81. if (isset($params['Cardinality'])) {
  82. $this->_cardinality = $params['Cardinality'];
  83. }
  84. if (isset($params['Sub_part'])) {
  85. $this->_sub_part = $params['Sub_part'];
  86. }
  87. if (isset($params['Null'])) {
  88. $this->_null = $params['Null'];
  89. }
  90. }
  91. /**
  92. * Returns the column name
  93. *
  94. * @return string column name
  95. */
  96. public function getName()
  97. {
  98. return $this->_name;
  99. }
  100. /**
  101. * Return the column collation
  102. *
  103. * @return string column collation
  104. */
  105. public function getCollation()
  106. {
  107. return $this->_collation;
  108. }
  109. /**
  110. * Returns the cardinality of the column
  111. *
  112. * @return int cardinality of the column
  113. */
  114. public function getCardinality()
  115. {
  116. return $this->_cardinality;
  117. }
  118. /**
  119. * Returns whether the column is nullable
  120. *
  121. * @param boolean $as_text whether to returned the string representation
  122. *
  123. * @return mixed nullability of the column. True/false or Yes/No depending
  124. * on the value of the $as_text parameter
  125. */
  126. public function getNull($as_text = false)
  127. {
  128. if ($as_text) {
  129. if (! $this->_null || $this->_null == 'NO') {
  130. return __('No');
  131. }
  132. return __('Yes');
  133. }
  134. return $this->_null;
  135. }
  136. /**
  137. * Returns the sequence number of the column in the index
  138. *
  139. * @return int sequence number of the column in the index
  140. */
  141. public function getSeqInIndex()
  142. {
  143. return $this->_seq_in_index;
  144. }
  145. /**
  146. * Returns the number of indexed characters if the column is only
  147. * partly indexed
  148. *
  149. * @return int the number of indexed characters
  150. */
  151. public function getSubPart()
  152. {
  153. return $this->_sub_part;
  154. }
  155. /**
  156. * Gets the properties in an array for comparison purposes
  157. *
  158. * @return array an array containing the properties of the index column
  159. */
  160. public function getCompareData()
  161. {
  162. return [
  163. 'Column_name' => $this->_name,
  164. 'Seq_in_index' => $this->_seq_in_index,
  165. 'Collation' => $this->_collation,
  166. 'Sub_part' => $this->_sub_part,
  167. 'Null' => $this->_null,
  168. ];
  169. }
  170. }