2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Library for extracting information about the partitions
8 if (! defined('PHPMYADMIN')) {
13 * Represents a sub partition of a table
17 class PMA_SubPartition
20 * @var string the database
24 * @var string the table
28 * @var string partition name
32 * @var integer ordinal
36 * @var string partition method
40 * @var string partition expression
42 protected $expression;
44 * @var integer no of table rows in the partition
48 * @var integer data length
50 protected $dataLength;
52 * @var integer index length
54 protected $indexLength;
56 * @var string partition comment
61 * Constructs a partition
63 * @param array $row fetched row from information_schema.PARTITIONS
65 public function __construct($row)
67 $this->db
= $row['TABLE_SCHEMA'];
68 $this->table
= $row['TABLE_NAME'];
69 $this->loadData($row);
73 * Loads data from the fetched row from information_schema.PARTITIONS
75 * @param array $row fetched row
79 protected function loadData($row)
81 $this->name
= $row['SUBPARTITION_NAME'];
82 $this->ordinal
= $row['SUBPARTITION_ORDINAL_POSITION'];
83 $this->method
= $row['SUBPARTITION_METHOD'];
84 $this->expression
= $row['SUBPARTITION_EXPRESSION'];
85 $this->loadCommonData($row);
89 * Loads some data that is common to both partitions and sub partitions
91 * @param array $row fetched row
95 protected function loadCommonData($row)
97 $this->rows
= $row['TABLE_ROWS'];
98 $this->dataLength
= $row['DATA_LENGTH'];
99 $this->indexLength
= $row['INDEX_LENGTH'];
100 $this->comment
= $row['PARTITION_COMMENT'];
104 * Return the partition name
106 * @return string partition name
108 public function getName()
114 * Return the ordinal of the partition
116 * @return number the ordinal
118 public function getOrdinal()
120 return $this->ordinal
;
124 * Returns the partition method
126 * @return string partition method
128 public function getMethod()
130 return $this->method
;
134 * Returns the partition expression
136 * @return string partition expression
138 public function getExpression()
140 return $this->expression
;
144 * Returns the number of data rows
146 * @return integer number of rows
148 public function getRows()
154 * Returns the data length
156 * @return integer data length
158 public function getDataLength()
160 return $this->dataLength
;
164 * Returns the index length
166 * @return integer index length
168 public function getIndexLength()
170 return $this->indexLength
;
174 * Returns the partition comment
176 * @return string partition comment
178 public function getComment()
180 return $this->comment
;
185 * base Partition Class
187 * @package PhpMyAdmin
189 class PMA_Partition
extends PMA_SubPartition
192 * @var string partition description
194 protected $description;
196 * @var PMA_SubPartition[] sub partitions
198 protected $subPartitions = array();
201 * Loads data from the fetched row from information_schema.PARTITIONS
203 * @param array $row fetched row
207 protected function loadData($row)
209 $this->name
= $row['PARTITION_NAME'];
210 $this->ordinal
= $row['PARTITION_ORDINAL_POSITION'];
211 $this->method
= $row['PARTITION_METHOD'];
212 $this->expression
= $row['PARTITION_EXPRESSION'];
213 $this->description
= $row['PARTITION_DESCRIPTION'];
214 // no sub partitions, load all data to this object
215 if (empty($row['SUBPARTITION_NAME'])) {
216 $this->loadCommonData($row);
221 * Returns the partiotion description
223 * @return string partition description
225 public function getDescription()
227 return $this->description
;
231 * Add a sub partition
233 * @param PMA_SubPartition $partition Sub partition
237 public function addSubPartition(PMA_SubPartition
$partition)
239 $this->subPartitions
[] = $partition;
243 * Whether there are sub partitions
247 public function hasSubPartitions()
249 return ! empty($this->subPartitions
);
253 * Returns the number of data rows
255 * @return integer number of rows
257 public function getRows()
259 if (empty($this->subPartitions
)) {
263 foreach ($this->subPartitions
as $subPartition) {
264 $rows +
= $subPartition->rows
;
271 * Returns the total data length
273 * @return integer data length
275 public function getDataLength()
277 if (empty($this->subPartitions
)) {
278 return $this->dataLength
;
281 foreach ($this->subPartitions
as $subPartition) {
282 $dataLength +
= $subPartition->dataLength
;
289 * Returns the tatal index length
291 * @return integer index length
293 public function getIndexLength()
295 if (empty($this->subPartitions
)) {
296 return $this->indexLength
;
299 foreach ($this->subPartitions
as $subPartition) {
300 $indexLength +
= $subPartition->indexLength
;
307 * Returns the list of sub partitions
309 * @return PMA_SubPartition[]
311 public function getSubPartitions()
313 return $this->subPartitions
;
317 * Returns array of partitions for a specific db/table
319 * @param string $db database name
320 * @param string $table table name
323 * @return PMA_Partition[]
325 static public function getPartitions($db, $table)
327 if (PMA_Partition
::havePartitioning()) {
328 $result = $GLOBALS['dbi']->fetchResult(
329 "SELECT * FROM `information_schema`.`PARTITIONS`"
330 . " WHERE `TABLE_SCHEMA` = '" . PMA_Util
::sqlAddSlashes($db)
331 . "' AND `TABLE_NAME` = '" . PMA_Util
::sqlAddSlashes($table) . "'"
334 $partitionMap = array();
335 foreach ($result as $row) {
336 if (isset($partitionMap[$row['PARTITION_NAME']])) {
337 $partition = $partitionMap[$row['PARTITION_NAME']];
339 $partition = new PMA_Partition($row);
340 $partitionMap[$row['PARTITION_NAME']] = $partition;
343 if (! empty($row['SUBPARTITION_NAME'])) {
344 $parentPartition = $partition;
345 $partition = new PMA_SubPartition($row);
346 $parentPartition->addSubPartition($partition);
349 return array_values($partitionMap);
358 * returns array of partition names for a specific db/table
360 * @param string $db database name
361 * @param string $table table name
364 * @return array of partition names
366 static public function getPartitionNames($db, $table)
368 if (PMA_Partition
::havePartitioning()) {
369 return $GLOBALS['dbi']->fetchResult(
370 "SELECT `PARTITION_NAME` FROM `information_schema`.`PARTITIONS`"
371 . " WHERE `TABLE_SCHEMA` = '" . PMA_Util
::sqlAddSlashes($db)
372 . "' AND `TABLE_NAME` = '" . PMA_Util
::sqlAddSlashes($table) . "'"
380 * returns the partition method used by the table.
382 * @param string $db database name
383 * @param string $table table name
385 * @return string partition method
387 static public function getPartitionMethod($db, $table)
389 if (PMA_Partition
::havePartitioning()) {
390 $partition_method = $GLOBALS['dbi']->fetchResult(
391 "SELECT `PARTITION_METHOD` FROM `information_schema`.`PARTITIONS`"
392 . " WHERE `TABLE_SCHEMA` = '" . PMA_Util
::sqlAddSlashes($db) . "'"
393 . " AND `TABLE_NAME` = '" . PMA_Util
::sqlAddSlashes($table) . "'"
395 if (! empty($partition_method)) {
396 return $partition_method[0];
403 * checks if MySQL server supports partitioning
406 * @staticvar boolean $have_partitioning
407 * @staticvar boolean $already_checked
411 static public function havePartitioning()
413 static $have_partitioning = false;
414 static $already_checked = false;
416 if (! $already_checked) {
417 if (PMA_MYSQL_INT_VERSION
< 50600) {
418 if ($GLOBALS['dbi']->fetchValue(
419 "SELECT @@have_partitioning;"
421 $have_partitioning = true;
424 // see http://dev.mysql.com/doc/refman/5.6/en/partitioning.html
425 $plugins = $GLOBALS['dbi']->fetchResult("SHOW PLUGINS");
426 foreach ($plugins as $value) {
427 if ($value['Name'] == 'partition') {
428 $have_partitioning = true;
433 $already_checked = true;
435 return $have_partitioning;