MediaWiki  master
UpdateQueryBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Wikimedia\Rdbms;
4 
5 use InvalidArgumentException;
6 use 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 
92  public function queryInfo( $info ) {
93  if ( isset( $info['table'] ) ) {
94  $this->table( $info['table'] );
95  }
96  if ( isset( $info['set'] ) ) {
97  $this->set( $info['set'] );
98  }
99  if ( isset( $info['conds'] ) ) {
100  $this->where( $info['conds'] );
101  }
102  if ( isset( $info['options'] ) ) {
103  $this->options( (array)$info['options'] );
104  }
105  return $this;
106  }
107 
114  public function table( $table ) {
115  $this->table = $table;
116  return $this;
117  }
118 
125  public function update( string $table ) {
126  return $this->table( $table );
127  }
128 
137  public function option( $name, $value = null ) {
138  if ( $value === null ) {
139  $this->options[] = $name;
140  } else {
141  $this->options[$name] = $value;
142  }
143  return $this;
144  }
145 
153  public function options( array $options ) {
154  $this->options = array_merge( $this->options, $options );
155  return $this;
156  }
157 
194  public function where( $conds ) {
195  if ( is_array( $conds ) ) {
196  foreach ( $conds as $key => $cond ) {
197  if ( is_int( $key ) ) {
198  $this->conds[] = $cond;
199  } elseif ( isset( $this->conds[$key] ) ) {
200  // @phan-suppress-previous-line PhanTypeMismatchDimFetch
201  // T288882
202  $this->conds[] = $this->db->makeList(
203  [ $key => $cond ], IDatabase::LIST_AND );
204  } else {
205  $this->conds[$key] = $cond;
206  }
207  }
208  } else {
209  $this->conds[] = $conds;
210  }
211  return $this;
212  }
213 
220  public function andWhere( $conds ) {
221  return $this->where( $conds );
222  }
223 
230  public function conds( $conds ) {
231  return $this->where( $conds );
232  }
233 
255  public function set( $set ) {
256  if ( is_array( $set ) ) {
257  foreach ( $set as $key => $value ) {
258  if ( is_int( $key ) ) {
259  $this->set[] = $value;
260  } else {
261  $this->set[$key] = $value;
262  }
263  }
264  } else {
265  $this->set[] = $set;
266  }
267  return $this;
268  }
269 
276  public function andSet( $set ) {
277  return $this->set( $set );
278  }
279 
288  public function ignore() {
289  $this->options[] = 'IGNORE';
290  return $this;
291  }
292 
299  public function caller( $fname ) {
300  $this->caller = $fname;
301  return $this;
302  }
303 
309  public function execute() {
310  if ( !$this->conds ) {
311  throw new UnexpectedValueException(
312  __METHOD__ . ' expects at least one condition to be set' );
313  }
314  if ( !$this->set ) {
315  throw new UnexpectedValueException(
316  __METHOD__ . ' can\t have empty $set value' );
317  }
318  if ( $this->table === '' ) {
319  throw new UnexpectedValueException(
320  __METHOD__ . ' expects table not to be empty' );
321  }
322  return $this->db->update( $this->table, $this->set, $this->conds, $this->caller, $this->options );
323  }
324 
335  public function getQueryInfo() {
336  return [
337  'table' => $this->table,
338  'set' => $this->set,
339  'conds' => $this->conds,
340  'options' => $this->options,
341  ];
342  }
343 }
const LIST_AND
Definition: Defines.php:43
Build UPDATE queries with a fluent interface.
execute()
Run the constructed UPDATE query and return the result.
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.
ignore()
Enable the IGNORE option.
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.