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
58 protected $db;
59
66 public function __construct( IDatabase $db ) {
67 $this->db = $db;
68 }
69
77 public function connection( IDatabase $db ) {
78 if ( $this->db->getType() !== $db->getType() ) {
79 throw new InvalidArgumentException(
80 __METHOD__ . ' cannot switch to a database of a different type.'
81 );
82 }
83 $this->db = $db;
84 return $this;
85 }
86
105 public function queryInfo( $info ) {
106 if ( isset( $info['table'] ) ) {
107 $this->table( $info['table'] );
108 }
109 if ( isset( $info['rows'] ) ) {
110 $this->rows( $info['rows'] );
111 }
112 if ( isset( $info['options'] ) ) {
113 $this->options( (array)$info['options'] );
114 }
115 if ( isset( $info['upsert'] ) ) {
116 $this->onDuplicateKeyUpdate();
117 }
118 if ( isset( $info['uniqueIndexFields'] ) ) {
119 $this->uniqueIndexFields( (array)$info['uniqueIndexFields'] );
120 }
121 if ( isset( $info['set'] ) ) {
122 $this->set( (array)$info['set'] );
123 }
124 if ( isset( $info['caller'] ) ) {
125 $this->caller( $info['caller'] );
126 }
127 return $this;
128 }
129
137 public function table( $table ) {
138 $this->table = $table;
139 return $this;
140 }
141
149 public function insertInto( string $table ) {
150 return $this->table( $table );
151 }
152
160 public function insert( string $table ) {
161 return $this->table( $table );
162 }
163
172 public function option( $name, $value = null ) {
173 if ( $value === null ) {
174 $this->options[] = $name;
175 } else {
176 $this->options[$name] = $value;
177 }
178 return $this;
179 }
180
188 public function options( array $options ) {
189 $this->options = array_merge( $this->options, $options );
190 return $this;
191 }
192
203 public function rows( array $rows ) {
204 $this->rows = array_merge( $this->rows, $rows );
205 return $this;
206 }
207
217 public function row( array $row ) {
218 $this->rows[] = $row;
219 return $this;
220 }
221
230 public function ignore() {
231 $this->options[] = 'IGNORE';
232 return $this;
233 }
234
240 public function onDuplicateKeyUpdate() {
241 $this->upsert = true;
242 return $this;
243 }
244
251 public function uniqueIndexFields( $uniqueIndexFields ) {
252 if ( is_string( $uniqueIndexFields ) ) {
253 $uniqueIndexFields = [ $uniqueIndexFields ];
254 }
255 $this->uniqueIndexFields = $uniqueIndexFields;
256 return $this;
257 }
258
281 public function set( $set ) {
282 if ( is_array( $set ) ) {
283 foreach ( $set as $key => $value ) {
284 if ( is_int( $key ) ) {
285 $this->set[] = $value;
286 } else {
287 $this->set[$key] = $value;
288 }
289 }
290 } else {
291 $this->set[] = $set;
292 }
293 return $this;
294 }
295
303 public function andSet( $set ) {
304 return $this->set( $set );
305 }
306
314 public function caller( $fname ) {
315 $this->caller = $fname;
316 return $this;
317 }
318
322 public function execute(): void {
323 if ( !$this->rows ) {
324 throw new UnexpectedValueException(
325 __METHOD__ . ' can\'t have empty $rows value' );
326 }
327 if ( $this->table === '' ) {
328 throw new UnexpectedValueException(
329 __METHOD__ . ' expects table not to be empty' );
330 }
331 if ( $this->upsert && ( !$this->set || !$this->uniqueIndexFields ) ) {
332 throw new UnexpectedValueException(
333 __METHOD__ . ' called with upsert but no set value or unique key has been provided' );
334 }
335 if ( !$this->upsert && ( $this->set || $this->uniqueIndexFields ) ) {
336 throw new UnexpectedValueException(
337 __METHOD__ . ' is not called with upsert but set value or unique key has been provided' );
338 }
339 if ( $this->upsert ) {
340 $this->db->upsert( $this->table, $this->rows, [ $this->uniqueIndexFields ], $this->set, $this->caller );
341 return;
342 }
343 $this->db->insert( $this->table, $this->rows, $this->caller, $this->options );
344 }
345
359 public function getQueryInfo() {
360 $info = [
361 'table' => $this->table,
362 'rows' => $this->rows,
363 'upsert' => $this->upsert,
364 'set' => $this->set,
365 'uniqueIndexFields' => $this->uniqueIndexFields,
366 'options' => $this->options,
367 ];
368 if ( $this->caller !== __CLASS__ ) {
369 $info['caller'] = $this->caller;
370 }
371 return $info;
372 }
373}
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:48
getType()
Get the RDBMS type of the server (e.g.