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
47 protected IDatabase $db;
48
53 public function __construct( IDatabase $db ) {
54 $this->db = $db;
55 }
56
60 public function connection( IDatabase $db ) {
61 if ( $this->db->getType() !== $db->getType() ) {
62 throw new InvalidArgumentException(
63 __METHOD__ . ' cannot switch to a database of a different type.'
64 );
65 }
66 $this->db = $db;
67 return $this;
68 }
69
73 public function queryInfo( $info ) {
74 if ( isset( $info['table'] ) ) {
75 $this->table( $info['table'] );
76 }
77 if ( isset( $info['set'] ) ) {
78 $this->set( $info['set'] );
79 }
80 if ( isset( $info['conds'] ) ) {
81 $this->where( $info['conds'] );
82 }
83 if ( isset( $info['options'] ) ) {
84 $this->options( (array)$info['options'] );
85 }
86 if ( isset( $info['caller'] ) ) {
87 $this->caller( $info['caller'] );
88 }
89 return $this;
90 }
91
99 public function table( $table ) {
100 $this->table = $table;
101 return $this;
102 }
103
111 public function update( string $table ) {
112 return $this->table( $table );
113 }
114
123 public function option( $name, $value = null ) {
124 if ( $value === null ) {
125 $this->options[] = $name;
126 } else {
127 $this->options[$name] = $value;
128 }
129 return $this;
130 }
131
139 public function options( array $options ) {
140 $this->options = array_merge( $this->options, $options );
141 return $this;
142 }
143
182 public function where( $conds ) {
183 if ( is_array( $conds ) ) {
184 foreach ( $conds as $key => $cond ) {
185 if ( is_int( $key ) ) {
186 $this->conds[] = $cond;
187 } elseif ( isset( $this->conds[$key] ) ) {
188 // @phan-suppress-previous-line PhanTypeMismatchDimFetch
189 // T288882
190 $this->conds[] = $this->db->makeList(
191 [ $key => $cond ], IDatabase::LIST_AND );
192 } else {
193 $this->conds[$key] = $cond;
194 }
195 }
196 } else {
197 $this->conds[] = $conds;
198 }
199 return $this;
200 }
201
210 public function andWhere( $conds ) {
211 return $this->where( $conds );
212 }
213
222 public function conds( $conds ) {
223 return $this->where( $conds );
224 }
225
248 public function set( $set ) {
249 if ( is_array( $set ) ) {
250 foreach ( $set as $key => $value ) {
251 if ( is_int( $key ) ) {
252 $this->set[] = $value;
253 } else {
254 $this->set[$key] = $value;
255 }
256 }
257 } else {
258 $this->set[] = $set;
259 }
260 return $this;
261 }
262
270 public function andSet( $set ) {
271 return $this->set( $set );
272 }
273
282 public function ignore() {
283 $this->options[] = 'IGNORE';
284 return $this;
285 }
286
290 public function caller( $fname ) {
291 $this->caller = $fname;
292 return $this;
293 }
294
298 public function execute(): void {
299 if ( !$this->conds ) {
300 throw new UnexpectedValueException(
301 __METHOD__ . ' expects at least one condition to be set' );
302 }
303 if ( !$this->set ) {
304 throw new UnexpectedValueException(
305 __METHOD__ . ' can\t have empty $set value' );
306 }
307 if ( $this->table === '' ) {
308 throw new UnexpectedValueException(
309 __METHOD__ . ' expects table not to be empty' );
310 }
311 $this->db->update( $this->table, $this->set, $this->conds, $this->caller, $this->options );
312 }
313
317 public function getQueryInfo() {
318 $info = [
319 'table' => $this->table,
320 'set' => $this->set,
321 'conds' => $this->conds,
322 'options' => $this->options,
323 ];
324 if ( $this->caller !== __CLASS__ ) {
325 $info['caller'] = $this->caller;
326 }
327 return $info;
328 }
329}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
Build UPDATE queries with a fluent interface.
execute()
Run the constructed 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.$this
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.The specified IDatabase will subsequently b...
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 the counter-part meth...
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.
Interface to a relational database.
Definition IDatabase.php:31
getType()
Get the RDBMS type of the server (e.g.
Shared interface of all write query builders.
const LIST_AND
Combine list with AND clauses.