Skip to content

Commit

Permalink
feat: allow inspecting the parts of the composite expression builder
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Jul 10, 2024
1 parent e31f474 commit b6c33a6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
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

0 comments on commit b6c33a6

Please sign in to comment.