|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +// SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + |
| 8 | +namespace OC\AppFramework\ORM; |
| 9 | + |
| 10 | +use OCP\AppFramework\ORM\IEntityExpressionBuilder; |
| 11 | +use OCP\DB\QueryBuilder\ICompositeExpression; |
| 12 | +use OCP\DB\QueryBuilder\IExpressionBuilder; |
| 13 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 14 | + |
| 15 | +final readonly class EntityExpressionBuilder implements IEntityExpressionBuilder { |
| 16 | + private IExpressionBuilder $expr; |
| 17 | + |
| 18 | + /** |
| 19 | + * @param EntityInfo<object> $entityInfo |
| 20 | + * @param string $columnPrefix Prepended to every resolved column, e.g. the `e.` table |
| 21 | + * alias used for joined selects; pass '' for unaliased |
| 22 | + * queries such as a bulk delete. |
| 23 | + */ |
| 24 | + public function __construct( |
| 25 | + private IQueryBuilder $qb, |
| 26 | + private EntityManager $entityManager, |
| 27 | + private EntityInfo $entityInfo, |
| 28 | + private string $columnPrefix = 'e.', |
| 29 | + ) { |
| 30 | + $this->expr = $this->qb->expr(); |
| 31 | + } |
| 32 | + |
| 33 | + #[\Override] |
| 34 | + public function andX(string|ICompositeExpression ...$x): ICompositeExpression { |
| 35 | + return $this->expr->andX(...$x); |
| 36 | + } |
| 37 | + |
| 38 | + #[\Override] |
| 39 | + public function orX(string|ICompositeExpression ...$x): ICompositeExpression { |
| 40 | + return $this->expr->orX(...$x); |
| 41 | + } |
| 42 | + |
| 43 | + #[\Override] |
| 44 | + public function eq(string $property, mixed $value): string { |
| 45 | + return $this->expr->eq($this->column($property), $this->parameter($property, $value)); |
| 46 | + } |
| 47 | + |
| 48 | + #[\Override] |
| 49 | + public function neq(string $property, mixed $value): string { |
| 50 | + return $this->expr->neq($this->column($property), $this->parameter($property, $value)); |
| 51 | + } |
| 52 | + |
| 53 | + #[\Override] |
| 54 | + public function lt(string $property, mixed $value): string { |
| 55 | + return $this->expr->lt($this->column($property), $this->parameter($property, $value)); |
| 56 | + } |
| 57 | + |
| 58 | + #[\Override] |
| 59 | + public function lte(string $property, mixed $value): string { |
| 60 | + return $this->expr->lte($this->column($property), $this->parameter($property, $value)); |
| 61 | + } |
| 62 | + |
| 63 | + #[\Override] |
| 64 | + public function gt(string $property, mixed $value): string { |
| 65 | + return $this->expr->gt($this->column($property), $this->parameter($property, $value)); |
| 66 | + } |
| 67 | + |
| 68 | + #[\Override] |
| 69 | + public function gte(string $property, mixed $value): string { |
| 70 | + return $this->expr->gte($this->column($property), $this->parameter($property, $value)); |
| 71 | + } |
| 72 | + |
| 73 | + #[\Override] |
| 74 | + public function like(string $property, string $pattern): string { |
| 75 | + return $this->expr->like($this->column($property), $this->parameter($property, $pattern)); |
| 76 | + } |
| 77 | + |
| 78 | + #[\Override] |
| 79 | + public function notLike(string $property, string $pattern): string { |
| 80 | + return $this->expr->notLike($this->column($property), $this->parameter($property, $pattern)); |
| 81 | + } |
| 82 | + |
| 83 | + #[\Override] |
| 84 | + public function in(string $property, array $values): string { |
| 85 | + return $this->expr->in($this->column($property), $this->parameter($property, $values, true)); |
| 86 | + } |
| 87 | + |
| 88 | + #[\Override] |
| 89 | + public function notIn(string $property, array $values): string { |
| 90 | + return $this->expr->notIn($this->column($property), $this->parameter($property, $values, true)); |
| 91 | + } |
| 92 | + |
| 93 | + #[\Override] |
| 94 | + public function isNull(string $property): string { |
| 95 | + return $this->expr->isNull($this->column($property)); |
| 96 | + } |
| 97 | + |
| 98 | + #[\Override] |
| 99 | + public function isNotNull(string $property): string { |
| 100 | + return $this->expr->isNotNull($this->column($property)); |
| 101 | + } |
| 102 | + |
| 103 | + private function column(string $property): string { |
| 104 | + return $this->columnPrefix . $this->resolveColumn($property); |
| 105 | + } |
| 106 | + |
| 107 | + private function parameter(string $property, mixed $value, bool $isArray = false): string { |
| 108 | + $column = $this->resolveColumn($property); |
| 109 | + /** @psalm-suppress MixedAssignment $value is caller-supplied, unwrapped of any \BackedEnum case. */ |
| 110 | + $value = $this->entityManager->toParameterValue($value); |
| 111 | + $type = $this->entityManager->getParameterType($this->entityInfo->mappingColumnToTypes[$column], $isArray); |
| 112 | + |
| 113 | + return (string)$this->qb->createNamedParameter($value, $type); |
| 114 | + } |
| 115 | + |
| 116 | + private function resolveColumn(string $property): string { |
| 117 | + return $this->entityInfo->mappingPropertyToColumn[$property] |
| 118 | + ?? throw new \InvalidArgumentException($this->entityInfo->entityClass . ' has no property named ' . $property); |
| 119 | + } |
| 120 | +} |
0 commit comments