1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
<?php
namespace Wikimedia\Rdbms;
use InvalidArgumentException;
use LogicException;
use Wikimedia\Rdbms\Database\DbQuoter;
/**
* A composite leaf representing an expression.
*
* @since 1.42
*/
class Expression implements IExpression {
private string $field;
private string $op;
/** @var ?scalar|RawSQLValue|Blob|LikeValue|non-empty-list<scalar|Blob> */
private $value;
/**
* Store an expression
*
* @param string $field
* @param-taint $field exec_sql
* @param string $op One of '>', '<', '!=', '=', '>=', '<=', IExpression::LIKE, IExpression::NOT_LIKE
* @phan-param '\x3E'|'\x3C'|'!='|'='|'\x3E='|'\x3C='|'LIKE'|'NOT LIKE' $op
* @param-taint $op exec_sql
* @param ?scalar|RawSQLValue|Blob|LikeValue|non-empty-list<scalar|Blob> $value
* @param-taint $value escapes_sql
* @internal Outside of rdbms, Use IReadableDatabase::expr() to create an expression object.
*/
public function __construct( string $field, string $op, $value ) {
if ( !in_array( $op, IExpression::ACCEPTABLE_OPERATORS ) ) {
throw new InvalidArgumentException( "Operator $op is not supported" );
}
if (
( is_array( $value ) || $value === null ) &&
!in_array( $op, [ '!=', '=' ] )
) {
throw new InvalidArgumentException( "Operator $op can't take array or null as value" );
}
if ( is_array( $value ) ) {
if ( !$value ) {
throw new InvalidArgumentException( "The array of values can't be empty" );
} elseif ( !array_is_list( $value ) ) {
throw new InvalidArgumentException( "The array of values must be a list" );
} elseif ( in_array( null, $value, true ) ) {
throw new InvalidArgumentException( "NULL can't be in the array of values" );
}
}
if ( in_array( $op, [ IExpression::LIKE, IExpression::NOT_LIKE ] ) && !( $value instanceof LikeValue ) ) {
throw new InvalidArgumentException( "Value for 'LIKE' expression must be of LikeValue type" );
}
if ( !in_array( $op, [ IExpression::LIKE, IExpression::NOT_LIKE ] ) && ( $value instanceof LikeValue ) ) {
throw new InvalidArgumentException( "LikeValue may only be used with 'LIKE' expression" );
}
$field = trim( $field );
if ( !preg_match( '/^[A-Za-z\d\._]+$/', $field ) ) {
throw new InvalidArgumentException( "$field might contain SQL injection" );
}
$this->field = $field;
$this->op = $op;
$this->value = $value;
}
/**
* @param string $field
* @param-taint $field exec_sql
* @param string $op One of '>', '<', '!=', '=', '>=', '<=', IExpression::LIKE, IExpression::NOT_LIKE
* @phan-param '\x3E'|'\x3C'|'!='|'='|'\x3E='|'\x3C='|'LIKE'|'NOT LIKE' $op
* @param-taint $op exec_sql
* @param ?scalar|RawSQLValue|Blob|LikeValue|non-empty-list<scalar|Blob> $value
* @param-taint $value escapes_sql
* @phan-side-effect-free
*/
public function and( string $field, string $op, $value ): AndExpressionGroup {
$exprGroup = new AndExpressionGroup( $this );
return $exprGroup->and( $field, $op, $value );
}
/**
* @param string $field
* @param-taint $field exec_sql
* @param string $op One of '>', '<', '!=', '=', '>=', '<=', IExpression::LIKE, IExpression::NOT_LIKE
* @phan-param '\x3E'|'\x3C'|'!='|'='|'\x3E='|'\x3C='|'LIKE'|'NOT LIKE' $op
* @param-taint $op exec_sql
* @param ?scalar|RawSQLValue|Blob|LikeValue|non-empty-list<scalar|Blob> $value
* @param-taint $value escapes_sql
* @phan-side-effect-free
*/
public function or( string $field, string $op, $value ): OrExpressionGroup {
$exprGroup = new OrExpressionGroup( $this );
return $exprGroup->or( $field, $op, $value );
}
/**
* @param IExpression $expr
* @return AndExpressionGroup
* @phan-side-effect-free
*/
public function andExpr( IExpression $expr ): AndExpressionGroup {
$exprGroup = new AndExpressionGroup( $this );
return $exprGroup->andExpr( $expr );
}
/**
* @param IExpression $expr
* @return OrExpressionGroup
* @phan-side-effect-free
*/
public function orExpr( IExpression $expr ): OrExpressionGroup {
$exprGroup = new OrExpressionGroup( $this );
return $exprGroup->orExpr( $expr );
}
/**
* @internal to be used by rdbms library only
* @return-taint none
*/
public function toSql( DbQuoter $dbQuoter ): string {
if ( is_array( $this->value ) ) {
if ( count( $this->value ) === 1 ) {
$value = $this->value[0];
if ( $this->op === '=' ) {
return $this->field . ' = ' . $dbQuoter->addQuotes( $value );
} elseif ( $this->op === '!=' ) {
return $this->field . ' != ' . $dbQuoter->addQuotes( $value );
} else {
throw new LogicException( "Operator $this->op can't take array as value" );
}
}
$list = implode( ',', array_map( static fn ( $value ) => $dbQuoter->addQuotes( $value ), $this->value ) );
if ( $this->op === '=' ) {
return $this->field . " IN ($list)";
} elseif ( $this->op === '!=' ) {
return $this->field . " NOT IN ($list)";
} else {
throw new LogicException( "Operator $this->op can't take array as value" );
}
}
if ( $this->value === null ) {
if ( $this->op === '=' ) {
return $this->field . " IS NULL";
} elseif ( $this->op === '!=' ) {
return $this->field . " IS NOT NULL";
} else {
throw new LogicException( "Operator $this->op can't take null as value" );
}
}
if ( $this->value instanceof LikeValue ) {
// implies that `op` is LIKE or NOT_LIKE, checked in constructor
return $this->field . ' ' . $this->op . ' ' . $this->value->toSql( $dbQuoter );
}
return $this->field . ' ' . $this->op . ' ' . $dbQuoter->addQuotes( $this->value );
}
/**
* @internal to be used by rdbms library only
*/
public function toGeneralizedSql(): string {
return $this->field . ' ' . $this->op . ' ?';
}
}
|