Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow inspecting the parts of the composite expression builder #46396

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions lib/private/DB/QueryBuilder/CompositeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OC\DB\QueryBuilder;

use OCP\DB\QueryBuilder\ICompositeExpression;

class CompositeExpression implements ICompositeExpression, \Countable {
/** @var \Doctrine\DBAL\Query\Expression\CompositeExpression */
protected $compositeExpression;
public const TYPE_AND = 'AND';
public const TYPE_OR = 'OR';

/**
* Constructor.
*
* @param \Doctrine\DBAL\Query\Expression\CompositeExpression $compositeExpression
*/
public function __construct(\Doctrine\DBAL\Query\Expression\CompositeExpression $compositeExpression) {
$this->compositeExpression = $compositeExpression;
public function __construct(
private string $type,
private array $parts = []
) {
}

/**
Expand All @@ -30,7 +28,9 @@ public function __construct(\Doctrine\DBAL\Query\Expression\CompositeExpression
* @return \OCP\DB\QueryBuilder\ICompositeExpression
*/
public function addMultiple(array $parts = []): ICompositeExpression {
$this->compositeExpression->addMultiple($parts);
foreach ($parts as $part) {
$this->add($part);
}

return $this;
}
Expand All @@ -43,7 +43,15 @@ public function addMultiple(array $parts = []): ICompositeExpression {
* @return \OCP\DB\QueryBuilder\ICompositeExpression
*/
public function add($part): ICompositeExpression {
$this->compositeExpression->add($part);
if ($part === null) {
return $this;
}

if ($part instanceof self && count($part) === 0) {
return $this;
}

$this->parts[] = $part;

return $this;
}
Expand All @@ -54,7 +62,7 @@ public function add($part): ICompositeExpression {
* @return integer
*/
public function count(): int {
return $this->compositeExpression->count();
return count($this->parts);
}

/**
Expand All @@ -63,7 +71,7 @@ public function count(): int {
* @return string
*/
public function getType(): string {
return $this->compositeExpression->getType();
return $this->type;
}

/**
Expand All @@ -72,6 +80,13 @@ public function getType(): string {
* @return string
*/
public function __toString(): string {
return (string) $this->compositeExpression;
if ($this->count() === 1) {
return (string)$this->parts[0];
}
return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
}

public function getParts(): array {
return $this->parts;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ public function __construct(ConnectionAdapter $connection, IQueryBuilder $queryB
* @return \OCP\DB\QueryBuilder\ICompositeExpression
*/
public function andX(...$x): ICompositeExpression {
$compositeExpression = call_user_func_array([$this->expressionBuilder, 'andX'], $x);
return new CompositeExpression($compositeExpression);
return new CompositeExpression(CompositeExpression::TYPE_AND, $x);
}

/**
Expand All @@ -82,8 +81,7 @@ public function andX(...$x): ICompositeExpression {
* @return \OCP\DB\QueryBuilder\ICompositeExpression
*/
public function orX(...$x): ICompositeExpression {
$compositeExpression = call_user_func_array([$this->expressionBuilder, 'orX'], $x);
return new CompositeExpression($compositeExpression);
return new CompositeExpression(CompositeExpression::TYPE_OR, $x);
}

/**
Expand Down
Loading