MediaWiki master
InsertQueryBuilder.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 $rows = [];
31
35 private $caller = __CLASS__;
36
40 private $upsert = false;
41
45 private $set = [];
46
50 private $uniqueIndexFields = [];
51
55 protected $options = [];
56
57 protected IDatabase $db;
58
63 public function __construct( IDatabase $db ) {
64 $this->db = $db;
65 }
66
74 public function connection( IDatabase $db ) {
75 if ( $this->db->getType() !== $db->getType() ) {
76 throw new InvalidArgumentException(
77 __METHOD__ . ' cannot switch to a database of a different type.'
78 );
79 }
80 $this->db = $db;
81 return $this;
82 }
83
102 public function queryInfo( $info ) {
103 if ( isset( $info['table'] ) ) {
104 $this->table( $info['table'] );
105 }
106 if ( isset( $info['rows'] ) ) {
107 $this->rows( $info['rows'] );
108 }
109 if ( isset( $info['options'] ) ) {
110 $this->options( (array)$info['options'] );
111 }
112 if ( isset( $info['upsert'] ) ) {
113 $this->onDuplicateKeyUpdate();
114 }
115 if ( isset( $info['uniqueIndexFields'] ) ) {
116 $this->uniqueIndexFields( (array)$info['uniqueIndexFields'] );
117 }
118 if ( isset( $info['set'] ) ) {
119 $this->set( (array)$info['set'] );
120 }
121 if ( isset( $info['caller'] ) ) {
122 $this->caller( $info['caller'] );
123 }
124 return $this;
125 }
126
134 public function table( $table ) {
135 $this->table = $table;
136 return $this;
137 }
138
146 public function insertInto( string $table ) {
147 return $this->table( $table );
148 }
149
157 public function insert( string $table ) {
158 return $this->table( $table );
159 }
160
169 public function option( $name, $value = null ) {
170 if ( $value === null ) {
171 $this->options[] = $name;
172 } else {
173 $this->options[$name] = $value;
174 }
175 return $this;
176 }
177
185 public function options( array $options ) {
186 $this->options = array_merge( $this->options, $options );
187 return $this;
188 }
189
200 public function rows( array $rows ) {
201 $this->rows = array_merge( $this->rows, $rows );
202 return $this;
203 }
204
214 public function row( array $row ) {
215 $this->rows[] = $row;
216 return $this;
217 }
218
227 public function ignore() {
228 $this->options[] = 'IGNORE';
229 return $this;
230 }
231
237 public function onDuplicateKeyUpdate() {
238 $this->upsert = true;
239 return $this;
240 }
241
248 public function uniqueIndexFields( $uniqueIndexFields ) {
249 if ( is_string( $uniqueIndexFields ) ) {
250 $uniqueIndexFields = [ $uniqueIndexFields ];
251 }
252 $this->uniqueIndexFields = $uniqueIndexFields;
253 return $this;
254 }
255
278 public function set( $set ) {
279 if ( is_array( $set ) ) {
280 foreach ( $set as $key => $value ) {
281 if ( is_int( $key ) ) {
282 $this->set[] = $value;
283 } else {
284 $this->set[$key] = $value;
285 }
286 }
287 } else {
288 $this->set[] = $set;
289 }
290 return $this;
291 }
292
300 public function andSet( $set ) {
301 return $this->set( $set );
302 }
303
311 public function caller( $fname ) {
312 $this->caller = $fname;
313 return $this;
314 }
315
319 public function execute(): void {
320 if ( !$this->rows ) {
321 throw new UnexpectedValueException(
322 __METHOD__ . ' can\'t have empty $rows value' );
323 }
324 if ( $this->table === '' ) {
325 throw new UnexpectedValueException(
326 __METHOD__ . ' expects table not to be empty' );
327 }
328 if ( $this->upsert && ( !$this->set || !$this->uniqueIndexFields ) ) {
329 throw new UnexpectedValueException(
330 __METHOD__ . ' called with upsert but no set value or unique key has been provided' );
331 }
332 if ( !$this->upsert && ( $this->set || $this->uniqueIndexFields ) ) {
333 throw new UnexpectedValueException(
334 __METHOD__ . ' is not called with upsert but set value or unique key has been provided' );
335 }
336 if ( $this->upsert ) {
337 $this->db->upsert( $this->table, $this->rows, [ $this->uniqueIndexFields ], $this->set, $this->caller );
338 return;
339 }
340 $this->db->insert( $this->table, $this->rows, $this->caller, $this->options );
341 }
342
356 public function getQueryInfo() {
357 $info = [
358 'table' => $this->table,
359 'rows' => $this->rows,
360 'upsert' => $this->upsert,
361 'set' => $this->set,
362 'uniqueIndexFields' => $this->uniqueIndexFields,
363 'options' => $this->options,
364 ];
365 if ( $this->caller !== __CLASS__ ) {
366 $info['caller'] = $this->caller;
367 }
368 return $info;
369 }
370}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Build INSERT queries with a fluent interface.
insert(string $table)
Set table for the query.
uniqueIndexFields( $uniqueIndexFields)
Set the unique index fields.
option( $name, $value=null)
Manually set an option in the $options array to be passed to IDatabase::insert()
onDuplicateKeyUpdate()
Do an update instead of insert.
getQueryInfo()
Get an associative array describing the query in terms of its raw parameters to Database::insert().
connection(IDatabase $db)
Change the IDatabase object the query builder is bound to.
insertInto(string $table)
Set table for the query.
andSet( $set)
Add set values to the query.
queryInfo( $info)
Set the query parameters to the given values, appending to the values which were already set.
table( $table)
Manually set the table name to be passed to IDatabase::insert()
__construct(IDatabase $db)
Only for use in subclasses.
array $options
The options to be passed to IDatabase::insert()
row(array $row)
Add one row to be inserted.
rows(array $rows)
Add rows to be inserted.
options(array $options)
Manually set multiple options in the $options array to be passed to IDatabase::insert().
execute()
Run the constructed INSERT query.
caller( $fname)
Set the method name to be included in an SQL comment.
Interface to a relational database.
Definition IDatabase.php:45
getType()
Get the RDBMS type of the server (e.g.