aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/DB/QueryBuilder/Partitioned/PartitionSplit.php
blob: ad4c0fab0553364e5937d2c87f226897b2741c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace OC\DB\QueryBuilder\Partitioned;

/**
 * Information about a database partition, containing the tables in the partition and any active alias
 */
class PartitionSplit {
	/** @var array<string, string> */
	public array $aliases = [];

	/**
	 * @param string[] $tables
	 */
	public function __construct(
		public string $name,
		public array $tables,
	) {
	}

	public function addAlias(string $table, string $alias): void {
		if ($this->containsTable($table)) {
			$this->aliases[$alias] = $table;
		}
	}

	public function addTable(string $table): void {
		if (!$this->containsTable($table)) {
			$this->tables[] = $table;
		}
	}

	public function containsTable(string $table): bool {
		return in_array($table, $this->tables);
	}

	public function containsAlias(string $alias): bool {
		return array_key_exists($alias, $this->aliases);
	}

	private function getTablesAndAliases(): array {
		return array_keys($this->aliases) + $this->tables;
	}

	/**
	 * Check if a query predicate mentions a table or alias from this partition
	 *
	 * @param string $predicate
	 * @return bool
	 */
	public function checkPredicateForTable(string $predicate): bool {
		foreach ($this->getTablesAndAliases() as $name) {
			if (str_contains($predicate, "`$name`.`")) {
				return true;
			}
		}
		return false;
	}

	public function isColumnInPartition(string $column): bool {
		foreach ($this->getTablesAndAliases() as $name) {
			if (str_starts_with($column, "$name.")) {
				return true;
			}
		}
		return false;
	}
}