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
70 public function connection( IDatabase $db ) {
71 if ( $this->db->getType() !== $db->getType() ) {
72 throw new InvalidArgumentException(
73 __METHOD__ . ' cannot switch to a database of a different type.'
74 );
75 }
76 $this->db = $db;
77 return $this;
78 }
79
83 public function queryInfo( $info ) {
84 if ( isset( $info['table'] ) ) {
85 $this->table( $info['table'] );
86 }
87 if ( isset( $info['rows'] ) ) {
88 $this->rows( $info['rows'] );
89 }
90 if ( isset( $info['options'] ) ) {
91 $this->options( (array)$info['options'] );
92 }
93 if ( isset( $info['upsert'] ) ) {
94 $this->onDuplicateKeyUpdate();
95 }
96 if ( isset( $info['uniqueIndexFields'] ) ) {
97 $this->uniqueIndexFields( (array)$info['uniqueIndexFields'] );
98 }
99 if ( isset( $info['set'] ) ) {
100 $this->set( (array)$info['set'] );
101 }
102 if ( isset( $info['caller'] ) ) {
103 $this->caller( $info['caller'] );
104 }
105 return $this;
106 }
107
115 public function table( $table ) {
116 $this->table = $table;
117 return $this;
118 }
119
127 public function insertInto( string $table ) {
128 return $this->table( $table );
129 }
130
138 public function insert( string $table ) {
139 return $this->table( $table );
140 }
141
150 public function option( $name, $value = null ) {
151 if ( $value === null ) {
152 $this->options[] = $name;
153 } else {
154 $this->options[$name] = $value;
155 }
156 return $this;
157 }
158
166 public function options( array $options ) {
167 $this->options = array_merge( $this->options, $options );
168 return $this;
169 }
170
181 public function rows( array $rows ) {
182 $this->rows = array_merge( $this->rows, $rows );
183 return $this;
184 }
185
195 public function row( array $row ) {
196 $this->rows[] = $row;
197 return $this;
198 }
199
208 public function ignore() {
209 $this->options[] = 'IGNORE';
210 return $this;
211 }
212
218 public function onDuplicateKeyUpdate() {
219 $this->upsert = true;
220 return $this;
221 }
222
229 public function uniqueIndexFields( $uniqueIndexFields ) {
230 if ( is_string( $uniqueIndexFields ) ) {
231 $uniqueIndexFields = [ $uniqueIndexFields ];
232 }
233 $this->uniqueIndexFields = $uniqueIndexFields;
234 return $this;
235 }
236
259 public function set( $set ) {
260 if ( is_array( $set ) ) {
261 foreach ( $set as $key => $value ) {
262 if ( is_int( $key ) ) {
263 $this->set[] = $value;
264 } else {
265 $this->set[$key] = $value;
266 }
267 }
268 } else {
269 $this->set[] = $set;
270 }
271 return $this;
272 }
273
281 public function andSet( $set ) {
282 return $this->set( $set );
283 }
284
288 public function caller( $fname ) {
289 $this->caller = $fname;
290 return $this;
291 }
292
296 public function execute(): void {
297 if ( !$this->rows ) {
298 throw new UnexpectedValueException(
299 __METHOD__ . ' can\'t have empty $rows value' );
300 }
301 if ( $this->table === '' ) {
302 throw new UnexpectedValueException(
303 __METHOD__ . ' expects table not to be empty' );
304 }
305 if ( $this->upsert && ( !$this->set || !$this->uniqueIndexFields ) ) {
306 throw new UnexpectedValueException(
307 __METHOD__ . ' called with upsert but no set value or unique key has been provided' );
308 }
309 if ( !$this->upsert && ( $this->set || $this->uniqueIndexFields ) ) {
310 throw new UnexpectedValueException(
311 __METHOD__ . ' is not called with upsert but set value or unique key has been provided' );
312 }
313 if ( $this->upsert ) {
314 $this->db->upsert( $this->table, $this->rows, [ $this->uniqueIndexFields ], $this->set, $this->caller );
315 return;
316 }
317 $this->db->insert( $this->table, $this->rows, $this->caller, $this->options );
318 }
319
323 public function getQueryInfo() {
324 $info = [
325 'table' => $this->table,
326 'rows' => $this->rows,
327 'upsert' => $this->upsert,
328 'set' => $this->set,
329 'uniqueIndexFields' => $this->uniqueIndexFields,
330 'options' => $this->options,
331 ];
332 if ( $this->caller !== __CLASS__ ) {
333 $info['caller'] = $this->caller;
334 }
335 return $info;
336 }
337}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
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 the counter-part meth...
connection(IDatabase $db)
Change the IDatabase object the query builder is bound to.The specified IDatabase will subsequently b...
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 query.
caller( $fname)
Set the method name to be included in an SQL comment.$this
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.