IndexColumn.php 4.2 KB

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