Maintenance.php 4.0 KB

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