MediaWiki master
UpdateQueryBuilder.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use InvalidArgumentException;
6use UnexpectedValueException;
7
8// Very long type annotations :(
9// phpcs:disable Generic.Files.LineLength
10
28 private $table = '';
29
33 private $set = [];
34
38 private $conds = [];
39
43 private $caller = __CLASS__;
44
48 protected $options = [];
49
51 protected $db;
52
59 public function __construct( IDatabase $db ) {
60 $this->db = $db;
61 }
62
70 public function connection( IDatabase $db ) {
71 if ( $this->db->getType() !== $db->getType() ) {
72 throw new InvalidArgumentException(
73 __METHOD__ . ' cannot switch to a database of a different type.'
74 );
75 }
76 $this->db = $db;
77 return $this;
78 }
79
96 public function queryInfo( $info ) {
97 if ( isset( $info['table'] ) ) {
98 $this->table( $info['table'] );
99 }
100 if ( isset( $info['set'] ) ) {
101 $this->set( $info['set'] );
102 }
103 if ( isset( $info['conds'] ) ) {
104 $this->where( $info['conds'] );
105 }
106 if ( isset( $info['options'] ) ) {
107 $this->options( (array)$info['options'] );
108 }
109 if ( isset( $info['caller'] ) ) {
110 $this->caller( $info['caller'] );
111 }
112 return $this;
113 }
114
122 public function table( $table ) {
123 $this->table = $table;
124 return $this;
125 }
126
134 public function update( string $table ) {
135 return $this->table( $table );
136 }
137
146 public function option( $name, $value = null ) {
147 if ( $value === null ) {
148 $this->options[] = $name;
149 } else {
150 $this->options[$name] = $value;
151 }
152 return $this;
153 }
154
162 public function options( array $options ) {
163 $this->options = array_merge( $this->options, $options );
164 return $this;
165 }
166
204 public function where( $conds ) {
205 if ( is_array( $conds ) ) {
206 foreach ( $conds as $key => $cond ) {
207 if ( is_int( $key ) ) {
208 $this->conds[] = $cond;
209 } elseif ( isset( $this->conds[$key] ) ) {
210 // @phan-suppress-previous-line PhanTypeMismatchDimFetch
211 // T288882
212 $this->conds[] = $this->db->makeList(
213 [ $key => $cond ], IDatabase::LIST_AND );
214 } else {
215 $this->conds[$key] = $cond;
216 }
217 }
218 } else {
219 $this->conds[] = $conds;
220 }
221 return $this;
222 }
223
231 public function andWhere( $conds ) {
232 return $this->where( $conds );
233 }
234
242 public function conds( $conds ) {
243 return $this->where( $conds );
244 }
245
268 public function set( $set ) {
269 if ( is_array( $set ) ) {
270 foreach ( $set as $key => $value ) {
271 if ( is_int( $key ) ) {
272 $this->set[] = $value;
273 } else {
274 $this->set[$key] = $value;
275 }
276 }
277 } else {
278 $this->set[] = $set;
279 }
280 return $this;
281 }
282
290 public function andSet( $set ) {
291 return $this->set( $set );
292 }
293
302 public function ignore() {
303 $this->options[] = 'IGNORE';
304 return $this;
305 }
306
314 public function caller( $fname ) {
315 $this->caller = $fname;
316 return $this;
317 }
318
322 public function execute(): void {
323 if ( !$this->conds ) {
324 throw new UnexpectedValueException(
325 __METHOD__ . ' expects at least one condition to be set' );
326 }
327 if ( !$this->set ) {
328 throw new UnexpectedValueException(
329 __METHOD__ . ' can\t have empty $set value' );
330 }
331 if ( $this->table === '' ) {
332 throw new UnexpectedValueException(
333 __METHOD__ . ' expects table not to be empty' );
334 }
335 $this->db->update( $this->table, $this->set, $this->conds, $this->caller, $this->options );
336 }
337
349 public function getQueryInfo() {
350 $info = [
351 'table' => $this->table,
352 'set' => $this->set,
353 'conds' => $this->conds,
354 'options' => $this->options,
355 ];
356 if ( $this->caller !== __CLASS__ ) {
357 $info['caller'] = $this->caller;
358 }
359 return $info;
360 }
361}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Build UPDATE queries with a fluent interface.
execute()
Run the constructed UPDATE query.
options(array $options)
Manually set multiple options in the $options array to be passed to IDatabase::update().
caller( $fname)
Set the method name to be included in an SQL comment.
array $options
The options to be passed to IDatabase::update()
where( $conds)
Add conditions to the query.
connection(IDatabase $db)
Change the IDatabase object the query builder is bound to.
queryInfo( $info)
Set the query parameters to the given values, appending to the values which were already set.
andWhere( $conds)
Add conditions to the query.
getQueryInfo()
Get an associative array describing the query in terms of its raw parameters to Database::update().
table( $table)
Manually set the table name to be passed to IDatabase::update()
conds( $conds)
Add conditions to the query.
option( $name, $value=null)
Manually set an option in the $options array to be passed to IDatabase::update()
update(string $table)
Set table for the query.
andSet( $set)
Add set values to the query.
__construct(IDatabase $db)
Only for use in subclasses.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
getType()
Get the RDBMS type of the server (e.g.
const LIST_AND
Combine list with AND clauses.