Translated using Weblate (Portuguese (Brazil))
[phpmyadmin.git] / libraries / Partition.php
blob141dcb7b33de0003e498699b2c4e4cf5266d741b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Library for extracting information about the partitions
6 * @package PhpMyAdmin
7 */
8 namespace PMA\libraries;
10 /**
11 * base Partition Class
13 * @package PhpMyAdmin
15 class Partition extends SubPartition
17 /**
18 * @var string partition description
20 protected $description;
21 /**
22 * @var SubPartition[] sub partitions
24 protected $subPartitions = array();
26 /**
27 * Loads data from the fetched row from information_schema.PARTITIONS
29 * @param array $row fetched row
31 * @return void
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);
46 /**
47 * Returns the partiotion description
49 * @return string partition description
51 public function getDescription()
53 return $this->description;
56 /**
57 * Add a sub partition
59 * @param SubPartition $partition Sub partition
61 * @return void
63 public function addSubPartition(SubPartition $partition)
65 $this->subPartitions[] = $partition;
68 /**
69 * Whether there are sub partitions
71 * @return boolean
73 public function hasSubPartitions()
75 return ! empty($this->subPartitions);
78 /**
79 * Returns the number of data rows
81 * @return integer number of rows
83 public function getRows()
85 if (empty($this->subPartitions)) {
86 return $this->rows;
87 } else {
88 $rows = 0;
89 foreach ($this->subPartitions as $subPartition) {
90 $rows += $subPartition->rows;
92 return $rows;
96 /**
97 * Returns the total data length
99 * @return integer data length
101 public function getDataLength()
103 if (empty($this->subPartitions)) {
104 return $this->dataLength;
105 } else {
106 $dataLength = 0;
107 foreach ($this->subPartitions as $subPartition) {
108 $dataLength += $subPartition->dataLength;
110 return $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;
123 } else {
124 $indexLength = 0;
125 foreach ($this->subPartitions as $subPartition) {
126 $indexLength += $subPartition->indexLength;
128 return $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
148 * @access public
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) . "'"
159 if ($result) {
160 $partitionMap = array();
161 foreach ($result as $row) {
162 if (isset($partitionMap[$row['PARTITION_NAME']])) {
163 $partition = $partitionMap[$row['PARTITION_NAME']];
164 } else {
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);
177 return array();
178 } else {
179 return array();
184 * returns array of partition names for a specific db/table
186 * @param string $db database name
187 * @param string $table table name
189 * @access public
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) . "'"
200 } else {
201 return array();
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) . "'"
220 . " LIMIT 1"
222 if (! empty($partition_method)) {
223 return $partition_method[0];
226 return null;
230 * checks if MySQL server supports partitioning
232 * @static
233 * @staticvar boolean $have_partitioning
234 * @staticvar boolean $already_checked
235 * @access public
236 * @return boolean
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;"
247 )) {
248 $have_partitioning = true;
250 } else {
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;
256 break;
260 $already_checked = true;
262 return $have_partitioning;