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