Partition.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Table;
  4. use PhpMyAdmin\DatabaseInterface;
  5. use PhpMyAdmin\Util;
  6. use function sprintf;
  7. final class Partition
  8. {
  9. /** @var DatabaseInterface */
  10. private $dbi;
  11. public function __construct(DatabaseInterface $dbi)
  12. {
  13. $this->dbi = $dbi;
  14. }
  15. public function analyze(string $db, string $table, string $partition): array
  16. {
  17. $query = sprintf(
  18. 'ALTER TABLE %s ANALYZE PARTITION %s;',
  19. Util::backquote($table),
  20. Util::backquote($partition)
  21. );
  22. $this->dbi->selectDb($db);
  23. $result = $this->dbi->fetchResult($query);
  24. $rows = [];
  25. foreach ($result as $row) {
  26. $rows[$row['Table']][] = $row;
  27. }
  28. return [$rows, $query];
  29. }
  30. public function check(string $db, string $table, string $partition): array
  31. {
  32. $query = sprintf(
  33. 'ALTER TABLE %s CHECK PARTITION %s;',
  34. Util::backquote($table),
  35. Util::backquote($partition)
  36. );
  37. $this->dbi->selectDb($db);
  38. $result = $this->dbi->fetchResult($query);
  39. $rows = [];
  40. foreach ($result as $row) {
  41. $rows[$row['Table']][] = $row;
  42. }
  43. return [$rows, $query];
  44. }
  45. public function drop(string $db, string $table, string $partition): array
  46. {
  47. $query = sprintf(
  48. 'ALTER TABLE %s DROP PARTITION %s;',
  49. Util::backquote($table),
  50. Util::backquote($partition)
  51. );
  52. $this->dbi->selectDb($db);
  53. $result = $this->dbi->tryQuery($query);
  54. return [(bool) $result, $query];
  55. }
  56. public function optimize(string $db, string $table, string $partition): array
  57. {
  58. $query = sprintf(
  59. 'ALTER TABLE %s OPTIMIZE PARTITION %s;',
  60. Util::backquote($table),
  61. Util::backquote($partition)
  62. );
  63. $this->dbi->selectDb($db);
  64. $result = $this->dbi->fetchResult($query);
  65. $rows = [];
  66. foreach ($result as $row) {
  67. $rows[$row['Table']][] = $row;
  68. }
  69. return [$rows, $query];
  70. }
  71. public function rebuild(string $db, string $table, string $partition): array
  72. {
  73. $query = sprintf(
  74. 'ALTER TABLE %s REBUILD PARTITION %s;',
  75. Util::backquote($table),
  76. Util::backquote($partition)
  77. );
  78. $this->dbi->selectDb($db);
  79. $result = $this->dbi->tryQuery($query);
  80. return [(bool) $result, $query];
  81. }
  82. public function repair(string $db, string $table, string $partition): array
  83. {
  84. $query = sprintf(
  85. 'ALTER TABLE %s REPAIR PARTITION %s;',
  86. Util::backquote($table),
  87. Util::backquote($partition)
  88. );
  89. $this->dbi->selectDb($db);
  90. $result = $this->dbi->fetchResult($query);
  91. $rows = [];
  92. foreach ($result as $row) {
  93. $rows[$row['Table']][] = $row;
  94. }
  95. return [$rows, $query];
  96. }
  97. public function truncate(string $db, string $table, string $partition): array
  98. {
  99. $query = sprintf(
  100. 'ALTER TABLE %s TRUNCATE PARTITION %s;',
  101. Util::backquote($table),
  102. Util::backquote($partition)
  103. );
  104. $this->dbi->selectDb($db);
  105. $result = $this->dbi->tryQuery($query);
  106. return [(bool) $result, $query];
  107. }
  108. }