Partition.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * Library for extracting information about the partitions
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin;
  7. use function array_values;
  8. /**
  9. * base Partition Class
  10. */
  11. class Partition extends SubPartition
  12. {
  13. /** @var string partition description */
  14. protected $description;
  15. /** @var SubPartition[] sub partitions */
  16. protected $subPartitions = [];
  17. /**
  18. * Loads data from the fetched row from information_schema.PARTITIONS
  19. *
  20. * @param array $row fetched row
  21. *
  22. * @return void
  23. */
  24. protected function loadData(array $row)
  25. {
  26. $this->name = $row['PARTITION_NAME'];
  27. $this->ordinal = $row['PARTITION_ORDINAL_POSITION'];
  28. $this->method = $row['PARTITION_METHOD'];
  29. $this->expression = $row['PARTITION_EXPRESSION'];
  30. $this->description = $row['PARTITION_DESCRIPTION'];
  31. // no sub partitions, load all data to this object
  32. if (! empty($row['SUBPARTITION_NAME'])) {
  33. return;
  34. }
  35. $this->loadCommonData($row);
  36. }
  37. /**
  38. * Returns the partition description
  39. *
  40. * @return string partition description
  41. */
  42. public function getDescription()
  43. {
  44. return $this->description;
  45. }
  46. /**
  47. * Add a sub partition
  48. *
  49. * @param SubPartition $partition Sub partition
  50. *
  51. * @return void
  52. */
  53. public function addSubPartition(SubPartition $partition)
  54. {
  55. $this->subPartitions[] = $partition;
  56. }
  57. /**
  58. * Whether there are sub partitions
  59. *
  60. * @return bool
  61. */
  62. public function hasSubPartitions()
  63. {
  64. return ! empty($this->subPartitions);
  65. }
  66. /**
  67. * Returns the number of data rows
  68. *
  69. * @return int number of rows
  70. */
  71. public function getRows()
  72. {
  73. if (empty($this->subPartitions)) {
  74. return $this->rows;
  75. }
  76. $rows = 0;
  77. foreach ($this->subPartitions as $subPartition) {
  78. $rows += $subPartition->rows;
  79. }
  80. return $rows;
  81. }
  82. /**
  83. * Returns the total data length
  84. *
  85. * @return int data length
  86. */
  87. public function getDataLength()
  88. {
  89. if (empty($this->subPartitions)) {
  90. return $this->dataLength;
  91. }
  92. $dataLength = 0;
  93. foreach ($this->subPartitions as $subPartition) {
  94. $dataLength += $subPartition->dataLength;
  95. }
  96. return $dataLength;
  97. }
  98. /**
  99. * Returns the total index length
  100. *
  101. * @return int index length
  102. */
  103. public function getIndexLength()
  104. {
  105. if (empty($this->subPartitions)) {
  106. return $this->indexLength;
  107. }
  108. $indexLength = 0;
  109. foreach ($this->subPartitions as $subPartition) {
  110. $indexLength += $subPartition->indexLength;
  111. }
  112. return $indexLength;
  113. }
  114. /**
  115. * Returns the list of sub partitions
  116. *
  117. * @return SubPartition[]
  118. */
  119. public function getSubPartitions()
  120. {
  121. return $this->subPartitions;
  122. }
  123. /**
  124. * Returns array of partitions for a specific db/table
  125. *
  126. * @param string $db database name
  127. * @param string $table table name
  128. *
  129. * @return Partition[]
  130. *
  131. * @access public
  132. */
  133. public static function getPartitions($db, $table)
  134. {
  135. global $dbi;
  136. if (self::havePartitioning()) {
  137. $result = $dbi->fetchResult(
  138. 'SELECT * FROM `information_schema`.`PARTITIONS`'
  139. . " WHERE `TABLE_SCHEMA` = '" . $dbi->escapeString($db)
  140. . "' AND `TABLE_NAME` = '" . $dbi->escapeString($table) . "'"
  141. );
  142. if ($result) {
  143. $partitionMap = [];
  144. /** @var array $row */
  145. foreach ($result as $row) {
  146. if (isset($partitionMap[$row['PARTITION_NAME']])) {
  147. $partition = $partitionMap[$row['PARTITION_NAME']];
  148. } else {
  149. $partition = new Partition($row);
  150. $partitionMap[$row['PARTITION_NAME']] = $partition;
  151. }
  152. if (empty($row['SUBPARTITION_NAME'])) {
  153. continue;
  154. }
  155. $parentPartition = $partition;
  156. $partition = new SubPartition($row);
  157. $parentPartition->addSubPartition($partition);
  158. }
  159. return array_values($partitionMap);
  160. }
  161. return [];
  162. }
  163. return [];
  164. }
  165. /**
  166. * returns array of partition names for a specific db/table
  167. *
  168. * @param string $db database name
  169. * @param string $table table name
  170. *
  171. * @return array of partition names
  172. *
  173. * @access public
  174. */
  175. public static function getPartitionNames($db, $table)
  176. {
  177. global $dbi;
  178. if (self::havePartitioning()) {
  179. return $dbi->fetchResult(
  180. 'SELECT DISTINCT `PARTITION_NAME` FROM `information_schema`.`PARTITIONS`'
  181. . " WHERE `TABLE_SCHEMA` = '" . $dbi->escapeString($db)
  182. . "' AND `TABLE_NAME` = '" . $dbi->escapeString($table) . "'"
  183. );
  184. }
  185. return [];
  186. }
  187. /**
  188. * returns the partition method used by the table.
  189. *
  190. * @param string $db database name
  191. * @param string $table table name
  192. *
  193. * @return string|null partition method
  194. */
  195. public static function getPartitionMethod($db, $table)
  196. {
  197. global $dbi;
  198. if (self::havePartitioning()) {
  199. $partition_method = $dbi->fetchResult(
  200. 'SELECT `PARTITION_METHOD` FROM `information_schema`.`PARTITIONS`'
  201. . " WHERE `TABLE_SCHEMA` = '" . $dbi->escapeString($db) . "'"
  202. . " AND `TABLE_NAME` = '" . $dbi->escapeString($table) . "'"
  203. . ' LIMIT 1'
  204. );
  205. if (! empty($partition_method)) {
  206. return $partition_method[0];
  207. }
  208. }
  209. return null;
  210. }
  211. /**
  212. * checks if MySQL server supports partitioning
  213. *
  214. * @return bool
  215. *
  216. * @static
  217. * @staticvar boolean $have_partitioning
  218. * @staticvar boolean $already_checked
  219. * @access public
  220. */
  221. public static function havePartitioning()
  222. {
  223. global $dbi;
  224. static $have_partitioning = false;
  225. static $already_checked = false;
  226. if (! $already_checked) {
  227. if ($dbi->getVersion() < 50600) {
  228. if ($dbi->fetchValue(
  229. 'SELECT @@have_partitioning;'
  230. )) {
  231. $have_partitioning = true;
  232. }
  233. } elseif ($dbi->getVersion() >= 80000) {
  234. $have_partitioning = true;
  235. } else {
  236. // see https://dev.mysql.com/doc/refman/5.6/en/partitioning.html
  237. $plugins = $dbi->fetchResult('SHOW PLUGINS');
  238. foreach ($plugins as $value) {
  239. if ($value['Name'] === 'partition') {
  240. $have_partitioning = true;
  241. break;
  242. }
  243. }
  244. }
  245. $already_checked = true;
  246. }
  247. return $have_partitioning;
  248. }
  249. }