1
0

PartitionSplit.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\DB\QueryBuilder\Partitioned;
  8. /**
  9. * Information about a database partition, containing the tables in the partition and any active alias
  10. */
  11. class PartitionSplit {
  12. /** @var array<string, string> */
  13. public array $aliases = [];
  14. /**
  15. * @param string[] $tables
  16. */
  17. public function __construct(
  18. public string $name,
  19. public array $tables,
  20. ) {
  21. }
  22. public function addAlias(string $table, string $alias): void {
  23. if ($this->containsTable($table)) {
  24. $this->aliases[$alias] = $table;
  25. }
  26. }
  27. public function addTable(string $table): void {
  28. if (!$this->containsTable($table)) {
  29. $this->tables[] = $table;
  30. }
  31. }
  32. public function containsTable(string $table): bool {
  33. return in_array($table, $this->tables);
  34. }
  35. public function containsAlias(string $alias): bool {
  36. return array_key_exists($alias, $this->aliases);
  37. }
  38. private function getTablesAndAliases(): array {
  39. return array_keys($this->aliases) + $this->tables;
  40. }
  41. /**
  42. * Check if a query predicate mentions a table or alias from this partition
  43. *
  44. * @param string $predicate
  45. * @return bool
  46. */
  47. public function checkPredicateForTable(string $predicate): bool {
  48. foreach ($this->getTablesAndAliases() as $name) {
  49. if (str_contains($predicate, "`$name`.`")) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. public function isColumnInPartition(string $column): bool {
  56. foreach ($this->getTablesAndAliases() as $name) {
  57. if (str_starts_with($column, "$name.")) {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. }