Partition.php 7.3 KB

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