MediaWiki master
Wikimedia\Rdbms\InsertQueryBuilder Class Reference

Build INSERT queries with a fluent interface. More...

Collaboration diagram for Wikimedia\Rdbms\InsertQueryBuilder:

Public Member Functions

 __construct (IDatabase $db)
 Only for use in subclasses.
 
 andSet ( $set)
 Add set values to the query.
 
 caller ( $fname)
 Set the method name to be included in an SQL comment.
 
 connection (IDatabase $db)
 Change the IDatabase object the query builder is bound to.
 
 execute ()
 Run the constructed INSERT query.
 
 getQueryInfo ()
 Get an associative array describing the query in terms of its raw parameters to Database::insert().
 
 ignore ()
 Enable the IGNORE option.
 
 insert (string $table)
 Set table for the query.
 
 insertInto (string $table)
 Set table for the query.
 
 onDuplicateKeyUpdate ()
 Do an update instead of insert.
 
 option ( $name, $value=null)
 Manually set an option in the $options array to be passed to IDatabase::insert()
 
 options (array $options)
 Manually set multiple options in the $options array to be passed to IDatabase::insert().
 
 queryInfo ( $info)
 Set the query parameters to the given values, appending to the values which were already set.
 
 row (array $row)
 Add one row to be inserted.
 
 rows (array $rows)
 Add rows to be inserted.
 
 set ( $set)
 Add SET part to the query.
 
 table ( $table)
 Manually set the table name to be passed to IDatabase::insert()
 
 uniqueIndexFields ( $uniqueIndexFields)
 Set the unique index fields.
 

Protected Attributes

IDatabase $db
 
array $options = []
 The options to be passed to IDatabase::insert()
 

Detailed Description

Build INSERT queries with a fluent interface.

Each query builder object must be used for a single database query only, and not be reused afterwards. To run multiple similar queries, you can create a query builder to set up most of your query, which you can use as a "template" to clone. You can then modify the cloned object for each individual query.

Since
1.41
Stability: stable
to extend

Definition at line 21 of file InsertQueryBuilder.php.

Constructor & Destructor Documentation

◆ __construct()

Wikimedia\Rdbms\InsertQueryBuilder::__construct ( IDatabase $db)

Only for use in subclasses.

To create a InsertQueryBuilder instance, use $db->newInsertQueryBuilder() instead.

Parameters
IDatabase$db

Definition at line 66 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\$db.

Member Function Documentation

◆ andSet()

Wikimedia\Rdbms\InsertQueryBuilder::andSet ( $set)

Add set values to the query.

Alias for set().

Parameters
string|array<string,?scalar>|array<int,string>$set
Returns
$this

Definition at line 303 of file InsertQueryBuilder.php.

◆ caller()

Wikimedia\Rdbms\InsertQueryBuilder::caller ( $fname)

Set the method name to be included in an SQL comment.

Parameters
string$fname
Returns
$this

Definition at line 314 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\caller().

Referenced by Wikimedia\Rdbms\InsertQueryBuilder\caller(), and Wikimedia\Rdbms\InsertQueryBuilder\queryInfo().

◆ connection()

Wikimedia\Rdbms\InsertQueryBuilder::connection ( IDatabase $db)

Change the IDatabase object the query builder is bound to.

The specified IDatabase will subsequently be used to execute the query.

Parameters
IDatabase$db
Returns
$this

Definition at line 77 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\$db, and Wikimedia\Rdbms\IReadableDatabase\getType().

◆ execute()

Wikimedia\Rdbms\InsertQueryBuilder::execute ( )

Run the constructed INSERT query.

Definition at line 322 of file InsertQueryBuilder.php.

◆ getQueryInfo()

Wikimedia\Rdbms\InsertQueryBuilder::getQueryInfo ( )

Get an associative array describing the query in terms of its raw parameters to Database::insert().

This can be used to interface with legacy code.

Returns
array The query info array, with keys:
  • table: The table name
  • rows: The rows array
  • options: The query options
  • upsert: Whether it's insert or upsert
  • uniqueIndexFields: Fields of the unique index
  • set: The set array
  • caller: The caller signature

Definition at line 359 of file InsertQueryBuilder.php.

◆ ignore()

Wikimedia\Rdbms\InsertQueryBuilder::ignore ( )

Enable the IGNORE option.

Skip insertion of rows that would cause unique key conflicts. IDatabase::affectedRows() can be used to determine how many rows were inserted.

Returns
$this

Definition at line 230 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\options().

◆ insert()

Wikimedia\Rdbms\InsertQueryBuilder::insert ( string $table)

Set table for the query.

Alias for table().

Parameters
string$tableUnqualified name of table
Returns
$this

Definition at line 160 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\table().

◆ insertInto()

Wikimedia\Rdbms\InsertQueryBuilder::insertInto ( string $table)

Set table for the query.

Alias for table().

Parameters
string$tableUnqualified name of table
Returns
$this

Definition at line 149 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\table().

◆ onDuplicateKeyUpdate()

Wikimedia\Rdbms\InsertQueryBuilder::onDuplicateKeyUpdate ( )

Do an update instead of insert.

Returns
$this

Definition at line 240 of file InsertQueryBuilder.php.

Referenced by Wikimedia\Rdbms\InsertQueryBuilder\queryInfo().

◆ option()

Wikimedia\Rdbms\InsertQueryBuilder::option ( $name,
$value = null )

Manually set an option in the $options array to be passed to IDatabase::insert()

Parameters
string$nameThe option name
mixed$valueThe option value, or null for a boolean option
Returns
$this

Definition at line 172 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\options().

◆ options()

Wikimedia\Rdbms\InsertQueryBuilder::options ( array $options)

Manually set multiple options in the $options array to be passed to IDatabase::insert().

Parameters
array$options
Returns
$this

Definition at line 188 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\options().

Referenced by Wikimedia\Rdbms\InsertQueryBuilder\ignore(), Wikimedia\Rdbms\InsertQueryBuilder\option(), Wikimedia\Rdbms\InsertQueryBuilder\options(), and Wikimedia\Rdbms\InsertQueryBuilder\queryInfo().

◆ queryInfo()

Wikimedia\Rdbms\InsertQueryBuilder::queryInfo ( $info)

Set the query parameters to the given values, appending to the values which were already set.

This can be used to interface with legacy code. If a key is omitted, the previous value will be retained.

The parameters must be formatted as required by Database::insert.

Parameters
array$infoAssociative array of query info, with keys:
  • table: The table name to be passed to Database::insert()
  • rows: The rows to be inserted
  • options: The query options
  • upsert: Whether it's insert or upsert
  • uniqueIndexFields: Fields of the unique index
  • set: The set array
  • caller: The caller signature
Returns
$this

Definition at line 105 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\caller(), Wikimedia\Rdbms\InsertQueryBuilder\onDuplicateKeyUpdate(), Wikimedia\Rdbms\InsertQueryBuilder\options(), Wikimedia\Rdbms\InsertQueryBuilder\rows(), Wikimedia\Rdbms\InsertQueryBuilder\table(), and Wikimedia\Rdbms\InsertQueryBuilder\uniqueIndexFields().

◆ row()

Wikimedia\Rdbms\InsertQueryBuilder::row ( array $row)

Add one row to be inserted.

Parameters
array$row$row must be a string-keyed map of (column name => value) defining a new row. Values are treated as literals and quoted appropriately; null is interpreted as NULL.
Returns
$this

Definition at line 217 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\rows().

◆ rows()

Wikimedia\Rdbms\InsertQueryBuilder::rows ( array $rows)

Add rows to be inserted.

Parameters
list<array>$rows $rows should be an integer-keyed list of such string-keyed maps, defining a list of new rows. The keys in each map must be identical to each other and in the same order. The rows must not collide with each other.
Returns
$this

Definition at line 203 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\rows().

Referenced by Wikimedia\Rdbms\InsertQueryBuilder\queryInfo(), Wikimedia\Rdbms\InsertQueryBuilder\row(), and Wikimedia\Rdbms\InsertQueryBuilder\rows().

◆ set()

Wikimedia\Rdbms\InsertQueryBuilder::set ( $set)

Add SET part to the query.

It takes an array containing arrays of column names map to the set values.

Parameters
string|array<string,?scalar>|array<int,string>$set

Combination map/list where each string-keyed entry maps a column to a literal assigned value and each integer-keyed value is a SQL expression in the format of a column assignment within UPDATE...SET. The (column => value) entries are convenient due to automatic value quoting and conversion of null to NULL. The SQL assignment format is useful for updates like "column = column + X". All assignments have no defined execution order, so they should not depend on each other. Do not modify AUTOINCREMENT or UUID columns in assignments.

Untrusted user input is safe in the values of string keys, however untrusted input must not be used in the array key names or in the values of numeric keys. Escaping of untrusted input used in values of numeric keys should be done via IDatabase::addQuotes()

Returns
$this

Definition at line 281 of file InsertQueryBuilder.php.

◆ table()

Wikimedia\Rdbms\InsertQueryBuilder::table ( $table)

Manually set the table name to be passed to IDatabase::insert()

Parameters
string$tableUnqualified name of table
Returns
$this

Definition at line 137 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\table().

Referenced by Wikimedia\Rdbms\InsertQueryBuilder\insert(), Wikimedia\Rdbms\InsertQueryBuilder\insertInto(), Wikimedia\Rdbms\InsertQueryBuilder\queryInfo(), and Wikimedia\Rdbms\InsertQueryBuilder\table().

◆ uniqueIndexFields()

Wikimedia\Rdbms\InsertQueryBuilder::uniqueIndexFields ( $uniqueIndexFields)

Set the unique index fields.

Parameters
string | string[]$uniqueIndexFields
Returns
$this

Definition at line 251 of file InsertQueryBuilder.php.

References Wikimedia\Rdbms\InsertQueryBuilder\uniqueIndexFields().

Referenced by Wikimedia\Rdbms\InsertQueryBuilder\queryInfo(), and Wikimedia\Rdbms\InsertQueryBuilder\uniqueIndexFields().

Member Data Documentation

◆ $db

IDatabase Wikimedia\Rdbms\InsertQueryBuilder::$db
protected

◆ $options

array Wikimedia\Rdbms\InsertQueryBuilder::$options = []
protected

The options to be passed to IDatabase::insert()

Definition at line 55 of file InsertQueryBuilder.php.


The documentation for this class was generated from the following file: