2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Library for extracting information about the partitions
8 namespace PMA\libraries
;
11 * base Partition Class
15 class Partition
extends SubPartition
18 * @var string partition description
20 protected $description;
22 * @var SubPartition[] sub partitions
24 protected $subPartitions = array();
27 * Loads data from the fetched row from information_schema.PARTITIONS
29 * @param array $row fetched row
33 protected function loadData($row)
35 $this->name
= $row['PARTITION_NAME'];
36 $this->ordinal
= $row['PARTITION_ORDINAL_POSITION'];
37 $this->method
= $row['PARTITION_METHOD'];
38 $this->expression
= $row['PARTITION_EXPRESSION'];
39 $this->description
= $row['PARTITION_DESCRIPTION'];
40 // no sub partitions, load all data to this object
41 if (empty($row['SUBPARTITION_NAME'])) {
42 $this->loadCommonData($row);
47 * Returns the partiotion description
49 * @return string partition description
51 public function getDescription()
53 return $this->description
;
59 * @param SubPartition $partition Sub partition
63 public function addSubPartition(SubPartition
$partition)
65 $this->subPartitions
[] = $partition;
69 * Whether there are sub partitions
73 public function hasSubPartitions()
75 return ! empty($this->subPartitions
);
79 * Returns the number of data rows
81 * @return integer number of rows
83 public function getRows()
85 if (empty($this->subPartitions
)) {
89 foreach ($this->subPartitions
as $subPartition) {
90 $rows +
= $subPartition->rows
;
97 * Returns the total data length
99 * @return integer data length
101 public function getDataLength()
103 if (empty($this->subPartitions
)) {
104 return $this->dataLength
;
107 foreach ($this->subPartitions
as $subPartition) {
108 $dataLength +
= $subPartition->dataLength
;
115 * Returns the tatal index length
117 * @return integer index length
119 public function getIndexLength()
121 if (empty($this->subPartitions
)) {
122 return $this->indexLength
;
125 foreach ($this->subPartitions
as $subPartition) {
126 $indexLength +
= $subPartition->indexLength
;
133 * Returns the list of sub partitions
135 * @return SubPartition[]
137 public function getSubPartitions()
139 return $this->subPartitions
;
143 * Returns array of partitions for a specific db/table
145 * @param string $db database name
146 * @param string $table table name
149 * @return Partition[]
151 static public function getPartitions($db, $table)
153 if (Partition
::havePartitioning()) {
154 $result = $GLOBALS['dbi']->fetchResult(
155 "SELECT * FROM `information_schema`.`PARTITIONS`"
156 . " WHERE `TABLE_SCHEMA` = '" . $GLOBALS['dbi']->escapeString($db)
157 . "' AND `TABLE_NAME` = '" . $GLOBALS['dbi']->escapeString($table) . "'"
160 $partitionMap = array();
161 foreach ($result as $row) {
162 if (isset($partitionMap[$row['PARTITION_NAME']])) {
163 $partition = $partitionMap[$row['PARTITION_NAME']];
165 $partition = new Partition($row);
166 $partitionMap[$row['PARTITION_NAME']] = $partition;
169 if (! empty($row['SUBPARTITION_NAME'])) {
170 $parentPartition = $partition;
171 $partition = new SubPartition($row);
172 $parentPartition->addSubPartition($partition);
175 return array_values($partitionMap);
184 * returns array of partition names for a specific db/table
186 * @param string $db database name
187 * @param string $table table name
190 * @return array of partition names
192 static public function getPartitionNames($db, $table)
194 if (Partition
::havePartitioning()) {
195 return $GLOBALS['dbi']->fetchResult(
196 "SELECT DISTINCT `PARTITION_NAME` FROM `information_schema`.`PARTITIONS`"
197 . " WHERE `TABLE_SCHEMA` = '" . $GLOBALS['dbi']->escapeString($db)
198 . "' AND `TABLE_NAME` = '" . $GLOBALS['dbi']->escapeString($table) . "'"
206 * returns the partition method used by the table.
208 * @param string $db database name
209 * @param string $table table name
211 * @return string partition method
213 static public function getPartitionMethod($db, $table)
215 if (Partition
::havePartitioning()) {
216 $partition_method = $GLOBALS['dbi']->fetchResult(
217 "SELECT `PARTITION_METHOD` FROM `information_schema`.`PARTITIONS`"
218 . " WHERE `TABLE_SCHEMA` = '" . $GLOBALS['dbi']->escapeString($db) . "'"
219 . " AND `TABLE_NAME` = '" . $GLOBALS['dbi']->escapeString($table) . "'"
222 if (! empty($partition_method)) {
223 return $partition_method[0];
230 * checks if MySQL server supports partitioning
233 * @staticvar boolean $have_partitioning
234 * @staticvar boolean $already_checked
238 static public function havePartitioning()
240 static $have_partitioning = false;
241 static $already_checked = false;
243 if (! $already_checked) {
244 if (PMA_MYSQL_INT_VERSION
< 50600) {
245 if ($GLOBALS['dbi']->fetchValue(
246 "SELECT @@have_partitioning;"
248 $have_partitioning = true;
251 // see https://dev.mysql.com/doc/refman/5.6/en/partitioning.html
252 $plugins = $GLOBALS['dbi']->fetchResult("SHOW PLUGINS");
253 foreach ($plugins as $value) {
254 if ($value['Name'] == 'partition') {
255 $have_partitioning = true;
260 $already_checked = true;
262 return $have_partitioning;