MediaWiki master
ExpressionGroup.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use InvalidArgumentException;
7
13abstract class ExpressionGroup implements IExpression {
17 protected array $children = [];
18
23 public function __construct( IExpression ...$children ) {
24 $this->children = $children;
25 }
26
27 final protected function add( IExpression $expression ) {
28 $this->children[] = $expression;
29 }
30
31 abstract protected function getType(): string;
32
40 public static function newFromArray( array $conds ) {
41 if ( !$conds ) {
42 throw new InvalidArgumentException( "The array of conditions can't be empty." );
43 }
44 $exprs = [];
45 foreach ( $conds as $field => $cond ) {
46 if ( is_numeric( $field ) ) {
47 if ( !$cond instanceof IExpression ) {
48 throw new InvalidArgumentException(
49 __METHOD__ . ": Only IExpression are allowed with numeric key." );
50 }
51 $exprs[] = $cond;
52 } else {
53 if ( $cond instanceof IExpression ) {
54 throw new InvalidArgumentException( __METHOD__ . ": unexpected key $field for IExpression value" );
55 }
56 $exprs[] = new Expression( $field, '=', $cond );
57 }
58 }
59 // @phan-suppress-next-line PhanTypeInstantiateAbstractStatic
60 return new static( ...$exprs );
61 }
62
68 final public function toSql( DbQuoter $dbQuoter ): string {
69 if ( !$this->children ) {
70 throw new InvalidArgumentException( "The array of values can't be empty." );
71 }
72 $sqls = array_map( static fn ( $value ) => $value->toSql( $dbQuoter ), $this->children );
73 return '(' . implode( ' ' . $this->getType() . ' ', $sqls ) . ')';
74 }
75
76 final public function toGeneralizedSql(): string {
77 if ( !$this->children ) {
78 throw new InvalidArgumentException( "The array of values can't be empty." );
79 }
80 $sqls = array_map( static fn ( $value ) => $value->toGeneralizedSql(), $this->children );
81 return '(' . implode( ' ' . $this->getType() . ' ', $sqls ) . ')';
82 }
83}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
A composite node representing a group of expressions.
add(IExpression $expression)
static newFromArray(array $conds)
__construct(IExpression ... $children)
toGeneralizedSql()
Return SQL for aggregated logging.
A composite leaf representing an expression.