aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/DB/QueryBuilder/Partitioned/JoinCondition.php
blob: a08858d1d6bd63436d0c33b206fbc0819280ddce (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?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;

use OC\DB\QueryBuilder\CompositeExpression;
use OC\DB\QueryBuilder\QueryFunction;
use OCP\DB\QueryBuilder\IQueryFunction;

/**
 * Utility class for working with join conditions
 */
class JoinCondition {
	public function __construct(
		public string|IQueryFunction $fromColumn,
		public ?string $fromAlias,
		public string|IQueryFunction $toColumn,
		public ?string $toAlias,
		public array $fromConditions,
		public array $toConditions,
	) {
		if (is_string($this->fromColumn) && str_starts_with($this->fromColumn, '(')) {
			$this->fromColumn = new QueryFunction($this->fromColumn);
		}
		if (is_string($this->toColumn) && str_starts_with($this->toColumn, '(')) {
			$this->toColumn = new QueryFunction($this->toColumn);
		}
	}

	/**
	 * @param JoinCondition[] $conditions
	 * @return JoinCondition
	 */
	public static function merge(array $conditions): JoinCondition {
		$fromColumn = '';
		$toColumn = '';
		$fromAlias = null;
		$toAlias = null;
		$fromConditions = [];
		$toConditions = [];
		foreach ($conditions as $condition) {
			if (($condition->fromColumn && $fromColumn) || ($condition->toColumn && $toColumn)) {
				throw new InvalidPartitionedQueryException("Can't join from {$condition->fromColumn} to {$condition->toColumn} as it already join froms {$fromColumn} to {$toColumn}");
			}
			if ($condition->fromColumn) {
				$fromColumn = $condition->fromColumn;
			}
			if ($condition->toColumn) {
				$toColumn = $condition->toColumn;
			}
			if ($condition->fromAlias) {
				$fromAlias = $condition->fromAlias;
			}
			if ($condition->toAlias) {
				$toAlias = $condition->toAlias;
			}
			$fromConditions = array_merge($fromConditions, $condition->fromConditions);
			$toConditions = array_merge($toConditions, $condition->toConditions);
		}
		return new JoinCondition($fromColumn, $fromAlias, $toColumn, $toAlias, $fromConditions, $toConditions);
	}

	/**
	 * @param null|string|CompositeExpression $condition
	 * @param string $join
	 * @param string $alias
	 * @param string $fromAlias
	 * @return JoinCondition
	 * @throws InvalidPartitionedQueryException
	 */
	public static function parse($condition, string $join, string $alias, string $fromAlias): JoinCondition {
		if ($condition === null) {
			throw new InvalidPartitionedQueryException("Can't join on $join without a condition");
		}

		$result = self::parseSubCondition($condition, $join, $alias, $fromAlias);
		if (!$result->fromColumn || !$result->toColumn) {
			throw new InvalidPartitionedQueryException("No join condition found from $fromAlias to $alias");
		}
		return $result;
	}

	private static function parseSubCondition($condition, string $join, string $alias, string $fromAlias): JoinCondition {
		if ($condition instanceof CompositeExpression) {
			if ($condition->getType() === CompositeExpression::TYPE_OR) {
				throw new InvalidPartitionedQueryException("Cannot join on $join with an OR expression");
			}
			return self::merge(array_map(function ($subCondition) use ($join, $alias, $fromAlias) {
				return self::parseSubCondition($subCondition, $join, $alias, $fromAlias);
			}, $condition->getParts()));
		}

		$condition = (string)$condition;
		$isSubCondition = self::isExtraCondition($condition);
		if ($isSubCondition) {
			if (self::mentionsAlias($condition, $fromAlias)) {
				return new JoinCondition('', null, '', null, [$condition], []);
			} else {
				return new JoinCondition('', null, '', null, [], [$condition]);
			}
		}

		$condition = str_replace('`', '', $condition);

		// expect a condition in the form of 'alias1.column1 = alias2.column2'
		if (!str_contains($condition, ' = ')) {
			throw new InvalidPartitionedQueryException("Can only join on $join with an `eq` condition");
		}
		$parts = explode(' = ', $condition, 2);
		$parts = array_map(function (string $part) {
			return self::clearConditionPart($part);
		}, $parts);

		if (!self::isSingleCondition($parts[0]) || !self::isSingleCondition($parts[1])) {
			throw new InvalidPartitionedQueryException("Can only join on $join with a single condition");
		}


		if (self::mentionsAlias($parts[0], $fromAlias)) {
			return new JoinCondition($parts[0], self::getAliasForPart($parts[0]), $parts[1], self::getAliasForPart($parts[1]), [], []);
		} elseif (self::mentionsAlias($parts[1], $fromAlias)) {
			return new JoinCondition($parts[1], self::getAliasForPart($parts[1]), $parts[0], self::getAliasForPart($parts[0]), [], []);
		} else {
			throw new InvalidPartitionedQueryException("join condition for $join needs to explicitly refer to the table by alias");
		}
	}

	private static function isSingleCondition(string $condition): bool {
		return !(str_contains($condition, ' OR ') || str_contains($condition, ' AND '));
	}

	private static function getAliasForPart(string $part): ?string {
		if (str_contains($part, ' ')) {
			return uniqid('join_alias_');
		} else {
			return null;
		}
	}

	private static function clearConditionPart(string $part): string {
		if (str_starts_with($part, 'CAST(')) {
			// pgsql/mysql cast
			$part = substr($part, strlen('CAST('));
			[$part] = explode(' AS ', $part);
		} elseif (str_starts_with($part, 'to_number(to_char(')) {
			// oracle cast to int
			$part = substr($part, strlen('to_number(to_char('), -2);
		} elseif (str_starts_with($part, 'to_number(to_char(')) {
			// oracle cast to string
			$part = substr($part, strlen('to_char('), -1);
		}
		return $part;
	}

	/**
	 * Check that a condition is an extra limit on the from/to part, and not the join condition
	 *
	 * This is done by checking that only one of the halves of the condition references a column
	 */
	private static function isExtraCondition(string $condition): bool {
		$parts = explode(' ', $condition, 2);
		return str_contains($parts[0], '`') xor str_contains($parts[1], '`');
	}

	private static function mentionsAlias(string $condition, string $alias): bool {
		return str_contains($condition, "$alias.");
	}
}