MediaWiki master
UpdateQueryBuilder.php
Go to the documentation of this file.
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use InvalidArgumentException;
6use UnexpectedValueException;
7
25 private $table = '';
26
30 private $set = [];
31
35 private $conds = [];
36
40 private $caller = __CLASS__;
41
45 protected $options = [];
46
48 protected $db;
49
56 public function __construct( IDatabase $db ) {
57 $this->db = $db;
58 }
59
67 public function connection( IDatabase $db ) {
68 if ( $this->db->getType() !== $db->getType() ) {
69 throw new InvalidArgumentException(
70 __METHOD__ . ' cannot switch to a database of a different type.'
71 );
72 }
73 $this->db = $db;
74 return $this;
75 }
76
93 public function queryInfo( $info ) {
94 if ( isset( $info['table'] ) ) {
95 $this->table( $info['table'] );
96 }
97 if ( isset( $info['set'] ) ) {
98 $this->set( $info['set'] );
99 }
100 if ( isset( $info['conds'] ) ) {
101 $this->where( $info['conds'] );
102 }
103 if ( isset( $info['options'] ) ) {
104 $this->options( (array)$info['options'] );
105 }
106 if ( isset( $info['caller'] ) ) {
107 $this->caller( $info['caller'] );
108 }
109 return $this;
110 }
111
119 public function table( $table ) {
120 $this->table = $table;
121 return $this;
122 }
123
131 public function update( string $table ) {
132 return $this->table( $table );
133 }
134
143 public function option( $name, $value = null ) {
144 if ( $value === null ) {
145 $this->options[] = $name;
146 } else {
147 $this->options[$name] = $value;
148 }
149 return $this;
150 }
151
159 public function options( array $options ) {
160 $this->options = array_merge( $this->options, $options );
161 return $this;
162 }
163
201 public function where( $conds ) {
202 if ( is_array( $conds ) ) {
203 foreach ( $conds as $key => $cond ) {
204 if ( is_int( $key ) ) {
205 $this->conds[] = $cond;
206 } elseif ( isset( $this->conds[$key] ) ) {
207 // @phan-suppress-previous-line PhanTypeMismatchDimFetch
208 // T288882
209 $this->conds[] = $this->db->makeList(
210 [ $key => $cond ], IDatabase::LIST_AND );
211 } else {
212 $this->conds[$key] = $cond;
213 }
214 }
215 } else {
216 $this->conds[] = $conds;
217 }
218 return $this;
219 }
220
228 public function andWhere( $conds ) {
229 return $this->where( $conds );
230 }
231
239 public function conds( $conds ) {
240 return $this->where( $conds );
241 }
242
265 public function set( $set ) {
266 if ( is_array( $set ) ) {
267 foreach ( $set as $key => $value ) {
268 if ( is_int( $key ) ) {
269 $this->set[] = $value;
270 } else {
271 $this->set[$key] = $value;
272 }
273 }
274 } else {
275 $this->set[] = $set;
276 }
277 return $this;
278 }
279
287 public function andSet( $set ) {
288 return $this->set( $set );
289 }
290
299 public function ignore() {
300 $this->options[] = 'IGNORE';
301 return $this;
302 }
303
311 public function caller( $fname ) {
312 $this->caller = $fname;
313 return $this;
314 }
315
319 public function execute(): void {
320 if ( !$this->conds ) {
321 throw new UnexpectedValueException(
322 __METHOD__ . ' expects at least one condition to be set' );
323 }
324 if ( !$this->set ) {
325 throw new UnexpectedValueException(
326 __METHOD__ . ' can\t have empty $set value' );
327 }
328 if ( $this->table === '' ) {
329 throw new UnexpectedValueException(
330 __METHOD__ . ' expects table not to be empty' );
331 }
332 $this->db->update( $this->table, $this->set, $this->conds, $this->caller, $this->options );
333 }
334
346 public function getQueryInfo() {
347 $info = [
348 'table' => $this->table,
349 'set' => $this->set,
350 'conds' => $this->conds,
351 'options' => $this->options,
352 ];
353 if ( $this->caller !== __CLASS__ ) {
354 $info['caller'] = $this->caller;
355 }
356 return $info;
357 }
358}
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:36
getType()
Get the RDBMS type of the server (e.g.