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