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 implements IWriteQueryBuilder {
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     * @inheritDoc
69     */
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
80    /**
81     * @inheritDoc
82     */
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
108    /**
109     * Manually set the table name to be passed to IDatabase::insert()
110     *
111     * @param string $table The unqualified name of a table
112     * @param-taint $table exec_sql
113     * @return $this
114     */
115    public function table( $table ) {
116        $this->table = $table;
117        return $this;
118    }
119
120    /**
121     * Set table for the query. Alias for table().
122     *
123     * @param string $table The unqualified name of a table
124     * @param-taint $table exec_sql
125     * @return $this
126     */
127    public function insertInto( string $table ) {
128        return $this->table( $table );
129    }
130
131    /**
132     * Set table for the query. Alias for table().
133     *
134     * @param string $table The unqualified name of a table
135     * @param-taint $table exec_sql
136     * @return $this
137     */
138    public function insert( string $table ) {
139        return $this->table( $table );
140    }
141
142    /**
143     * Manually set an option in the $options array to be passed to
144     * IDatabase::insert()
145     *
146     * @param string $name The option name
147     * @param mixed $value The option value, or null for a boolean option
148     * @return $this
149     */
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
159    /**
160     * Manually set multiple options in the $options array to be passed to
161     * IDatabase::insert().
162     *
163     * @param array $options
164     * @return $this
165     */
166    public function options( array $options ) {
167        $this->options = array_merge( $this->options, $options );
168        return $this;
169    }
170
171    /**
172     * Add rows to be inserted.
173     *
174     * @param list<array> $rows
175     * $rows should be an integer-keyed list of such string-keyed maps, defining a list of new rows.
176     * The keys in each map must be identical to each other and in the same order.
177     * The rows must not collide with each other.
178     *
179     * @return $this
180     */
181    public function rows( array $rows ) {
182        $this->rows = array_merge( $this->rows, $rows );
183        return $this;
184    }
185
186    /**
187     * Add one row to be inserted.
188     *
189     * @param array $row
190     * $row must be a string-keyed map of (column name => value) defining a new row. Values are
191     * treated as literals and quoted appropriately; null is interpreted as NULL.
192     *
193     * @return $this
194     */
195    public function row( array $row ) {
196        $this->rows[] = $row;
197        return $this;
198    }
199
200    /**
201     * Enable the IGNORE option.
202     *
203     * Skip insertion of rows that would cause unique key conflicts.
204     * IDatabase::affectedRows() can be used to determine how many rows were inserted.
205     *
206     * @return $this
207     */
208    public function ignore() {
209        $this->options[] = 'IGNORE';
210        return $this;
211    }
212
213    /**
214     * Do an update instead of insert
215     *
216     * @return $this
217     */
218    public function onDuplicateKeyUpdate() {
219        $this->upsert = true;
220        return $this;
221    }
222
223    /**
224     * Set the unique index fields
225     *
226     * @param string|string[] $uniqueIndexFields
227     * @return $this
228     */
229    public function uniqueIndexFields( $uniqueIndexFields ) {
230        if ( is_string( $uniqueIndexFields ) ) {
231            $uniqueIndexFields = [ $uniqueIndexFields ];
232        }
233        $this->uniqueIndexFields = $uniqueIndexFields;
234        return $this;
235    }
236
237    /**
238     * Add SET part to the query. It takes an array containing arrays of column names map to
239     * the set values.
240     *
241     * @param string|array<string,?scalar|RawSQLValue|Blob>|array<int,string> $set
242     * @param-taint $set exec_sql_numkey
243     *
244     * Combination map/list where each string-keyed entry maps a column
245     * to a literal assigned value and each integer-keyed value is a SQL expression in the
246     * format of a column assignment within UPDATE...SET. The (column => value) entries are
247     * convenient due to automatic value quoting and conversion of null to NULL. The SQL
248     * assignment format is useful for updates like "column = column + X". All assignments
249     * have no defined execution order, so they should not depend on each other. Do not
250     * modify AUTOINCREMENT or UUID columns in assignments.
251     *
252     * Untrusted user input is safe in the values of string keys, however untrusted
253     * input must not be used in the array key names or in the values of numeric keys.
254     * Escaping of untrusted input used in values of numeric keys should be done via
255     * IDatabase::addQuotes()
256     *
257     * @return $this
258     */
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
274    /**
275     * Add set values to the query. Alias for set().
276     *
277     * @param string|array<string,?scalar|RawSQLValue|Blob>|array<int,string> $set
278     * @param-taint $set exec_sql_numkey
279     * @return $this
280     */
281    public function andSet( $set ) {
282        return $this->set( $set );
283    }
284
285    /**
286     * @inheritDoc
287     */
288    public function caller( $fname ) {
289        $this->caller = $fname;
290        return $this;
291    }
292
293    /**
294     * @inheritDoc
295     */
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
320    /**
321     * @inheritDoc
322     */
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}