Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.31% covered (warning)
75.31%
61 / 81
61.11% covered (warning)
61.11%
11 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
InsertQueryBuilder
75.31% covered (warning)
75.31%
61 / 81
61.11% covered (warning)
61.11%
11 / 18
66.30
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 connection
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 queryInfo
93.33% covered (success)
93.33%
14 / 15
0.00% covered (danger)
0.00%
0 / 1
8.02
 table
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 insertInto
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 insert
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 option
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 options
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 rows
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 row
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 ignore
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 onDuplicateKeyUpdate
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 uniqueIndexFields
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 set
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
4.37
 andSet
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 caller
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
50.00% covered (danger)
50.00%
8 / 16
0.00% covered (danger)
0.00%
0 / 1
22.50
 getQueryInfo
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use InvalidArgumentException;
6use UnexpectedValueException;
7
8/**
9 * Build INSERT queries with a fluent interface.
10 *
11 * Each query builder object must be used for a single database query only,
12 * and not be reused afterwards. To run multiple similar queries, you can
13 * create a query builder to set up most of your query, which you can use
14 * as a "template" to clone. You can then modify the cloned object for
15 * each individual query.
16 *
17 * @since 1.41
18 * @stable to extend
19 * @ingroup Database
20 */
21class InsertQueryBuilder {
22    /**
23     * @var string The table name to be passed to IDatabase::insert()
24     */
25    private $table = '';
26
27    /**
28     * @var list<array> The rows to be passed to IDatabase::insert()
29     */
30    private $rows = [];
31
32    /**
33     * @var string The caller (function name) to be passed to IDatabase::insert()
34     */
35    private $caller = __CLASS__;
36
37    /**
38     * @var bool whether this is an upsert or not
39     */
40    private $upsert = false;
41
42    /**
43     * @var array The set values to be passed to IDatabase::upsert()
44     */
45    private $set = [];
46
47    /**
48     * @var string[] The unique keys to be passed to IDatabase::upsert()
49     */
50    private $uniqueIndexFields = [];
51
52    /**
53     * @var array The options to be passed to IDatabase::insert()
54     */
55    protected $options = [];
56
57    protected IDatabase $db;
58
59    /**
60     * Only for use in subclasses. To create a InsertQueryBuilder instance,
61     * use `$db->newInsertQueryBuilder()` instead.
62     */
63    public function __construct( IDatabase $db ) {
64        $this->db = $db;
65    }
66
67    /**
68     * Change the IDatabase object the query builder is bound to. The specified
69     * IDatabase will subsequently be used to execute the query.
70     *
71     * @param IDatabase $db
72     * @return $this
73     */
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
84    /**
85     * Set the query parameters to the given values, appending to the values
86     * which were already set. This can be used to interface with legacy code.
87     * If a key is omitted, the previous value will be retained.
88     *
89     * The parameters must be formatted as required by Database::insert.
90     *
91     * @param array $info Associative array of query info, with keys:
92     *   - table: The table name to be passed to Database::insert()
93     *   - rows: The rows to be inserted
94     *   - options: The query options
95     *   - upsert: Whether it's insert or upsert
96     *   - uniqueIndexFields: Fields of the unique index
97     *   - set: The set array
98     *   - caller: The caller signature
99     *
100     * @return $this
101     */
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
127    /**
128     * Manually set the table name to be passed to IDatabase::insert()
129     *
130     * @param string $table The unqualified name of a table
131     * @param-taint $table exec_sql
132     * @return $this
133     */
134    public function table( $table ) {
135        $this->table = $table;
136        return $this;
137    }
138
139    /**
140     * Set table for the query. Alias for table().
141     *
142     * @param string $table The unqualified name of a table
143     * @param-taint $table exec_sql
144     * @return $this
145     */
146    public function insertInto( string $table ) {
147        return $this->table( $table );
148    }
149
150    /**
151     * Set table for the query. Alias for table().
152     *
153     * @param string $table The unqualified name of a table
154     * @param-taint $table exec_sql
155     * @return $this
156     */
157    public function insert( string $table ) {
158        return $this->table( $table );
159    }
160
161    /**
162     * Manually set an option in the $options array to be passed to
163     * IDatabase::insert()
164     *
165     * @param string $name The option name
166     * @param mixed $value The option value, or null for a boolean option
167     * @return $this
168     */
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
178    /**
179     * Manually set multiple options in the $options array to be passed to
180     * IDatabase::insert().
181     *
182     * @param array $options
183     * @return $this
184     */
185    public function options( array $options ) {
186        $this->options = array_merge( $this->options, $options );
187        return $this;
188    }
189
190    /**
191     * Add rows to be inserted.
192     *
193     * @param list<array> $rows
194     * $rows should be an integer-keyed list of such string-keyed maps, defining a list of new rows.
195     * The keys in each map must be identical to each other and in the same order.
196     * The rows must not collide with each other.
197     *
198     * @return $this
199     */
200    public function rows( array $rows ) {
201        $this->rows = array_merge( $this->rows, $rows );
202        return $this;
203    }
204
205    /**
206     * Add one row to be inserted.
207     *
208     * @param array $row
209     * $row must be a string-keyed map of (column name => value) defining a new row. Values are
210     * treated as literals and quoted appropriately; null is interpreted as NULL.
211     *
212     * @return $this
213     */
214    public function row( array $row ) {
215        $this->rows[] = $row;
216        return $this;
217    }
218
219    /**
220     * Enable the IGNORE option.
221     *
222     * Skip insertion of rows that would cause unique key conflicts.
223     * IDatabase::affectedRows() can be used to determine how many rows were inserted.
224     *
225     * @return $this
226     */
227    public function ignore() {
228        $this->options[] = 'IGNORE';
229        return $this;
230    }
231
232    /**
233     * Do an update instead of insert
234     *
235     * @return $this
236     */
237    public function onDuplicateKeyUpdate() {
238        $this->upsert = true;
239        return $this;
240    }
241
242    /**
243     * Set the unique index fields
244     *
245     * @param string|string[] $uniqueIndexFields
246     * @return $this
247     */
248    public function uniqueIndexFields( $uniqueIndexFields ) {
249        if ( is_string( $uniqueIndexFields ) ) {
250            $uniqueIndexFields = [ $uniqueIndexFields ];
251        }
252        $this->uniqueIndexFields = $uniqueIndexFields;
253        return $this;
254    }
255
256    /**
257     * Add SET part to the query. It takes an array containing arrays of column names map to
258     * the set values.
259     *
260     * @param string|array<string,?scalar|RawSQLValue>|array<int,string> $set
261     * @param-taint $set exec_sql_numkey
262     *
263     * Combination map/list where each string-keyed entry maps a column
264     * to a literal assigned value and each integer-keyed value is a SQL expression in the
265     * format of a column assignment within UPDATE...SET. The (column => value) entries are
266     * convenient due to automatic value quoting and conversion of null to NULL. The SQL
267     * assignment format is useful for updates like "column = column + X". All assignments
268     * have no defined execution order, so they should not depend on each other. Do not
269     * modify AUTOINCREMENT or UUID columns in assignments.
270     *
271     * Untrusted user input is safe in the values of string keys, however untrusted
272     * input must not be used in the array key names or in the values of numeric keys.
273     * Escaping of untrusted input used in values of numeric keys should be done via
274     * IDatabase::addQuotes()
275     *
276     * @return $this
277     */
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
293    /**
294     * Add set values to the query. Alias for set().
295     *
296     * @param string|array<string,?scalar|RawSQLValue>|array<int,string> $set
297     * @param-taint $set exec_sql_numkey
298     * @return $this
299     */
300    public function andSet( $set ) {
301        return $this->set( $set );
302    }
303
304    /**
305     * Set the method name to be included in an SQL comment.
306     *
307     * @param string $fname
308     * @param-taint $fname exec_sql
309     * @return $this
310     */
311    public function caller( $fname ) {
312        $this->caller = $fname;
313        return $this;
314    }
315
316    /**
317     * Run the constructed INSERT query.
318     */
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
343    /**
344     * Get an associative array describing the query in terms of its raw parameters to
345     * Database::insert(). This can be used to interface with legacy code.
346     *
347     * @return array The query info array, with keys:
348     *   - table: The table name
349     *   - rows: The rows array
350     *   - options: The query options
351     *   - upsert: Whether it's insert or upsert
352     *   - uniqueIndexFields: Fields of the unique index
353     *   - set: The set array
354     *   - caller: The caller signature
355     */
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}