MediaWiki master
Wikimedia\Rdbms\UpdateQueryBuilder Class Reference

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

Collaboration diagram for Wikimedia\Rdbms\UpdateQueryBuilder:

Public Member Functions

 __construct (IDatabase $db)
 Only for use in subclasses.
 
 andSet ( $set)
 Add set values to the query.
 
 andWhere ( $conds)
 Add conditions to the query.
 
 caller ( $fname)
 Set the method name to be included in an SQL comment.
 
 conds ( $conds)
 Add conditions to the query.
 
 connection (IDatabase $db)
 Change the IDatabase object the query builder is bound to.
 
 execute ()
 Run the constructed UPDATE query.
 
 getQueryInfo ()
 Get an associative array describing the query in terms of its raw parameters to Database::update().
 
 ignore ()
 Enable the IGNORE option.
 
 option ( $name, $value=null)
 Manually set an option in the $options array to be passed to IDatabase::update()
 
 options (array $options)
 Manually set multiple options in the $options array to be passed to IDatabase::update().
 
 queryInfo ( $info)
 Set the query parameters to the given values, appending to the values which were already set.
 
 set ( $set)
 Add SET part to the query.
 
 table ( $table)
 Manually set the table name to be passed to IDatabase::update()
 
 update (string $table)
 Set table for the query.
 
 where ( $conds)
 Add conditions to the query.
 

Protected Attributes

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

Detailed Description

Build UPDATE 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 24 of file UpdateQueryBuilder.php.

Constructor & Destructor Documentation

◆ __construct()

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

Only for use in subclasses.

To create a UpdateQueryBuilder instance, use $db->newUpdateQueryBuilder() instead.

Parameters
IDatabase$db

Definition at line 59 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\$db.

Member Function Documentation

◆ andSet()

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

Add set values to the query.

Alias for set().

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

Definition at line 290 of file UpdateQueryBuilder.php.

◆ andWhere()

Wikimedia\Rdbms\UpdateQueryBuilder::andWhere ( $conds)

Add conditions to the query.

Alias for where().

Parameters
string|IExpression|array<string,?scalar|non-empty-array<int,?scalar>|RawSQLValue>|array<int,string|IExpression>$conds
Returns
$this

Definition at line 231 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\where().

◆ caller()

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

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

Parameters
string$fname
Returns
$this

Definition at line 314 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\caller().

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

◆ conds()

Wikimedia\Rdbms\UpdateQueryBuilder::conds ( $conds)

Add conditions to the query.

Alias for where().

Parameters
string|IExpression|array<string,?scalar|non-empty-array<int,?scalar>|RawSQLValue>|array<int,string|IExpression>$conds
Returns
$this

Definition at line 242 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\where().

Referenced by Wikimedia\Rdbms\UpdateQueryBuilder\where().

◆ connection()

Wikimedia\Rdbms\UpdateQueryBuilder::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 70 of file UpdateQueryBuilder.php.

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

◆ execute()

Wikimedia\Rdbms\UpdateQueryBuilder::execute ( )

Run the constructed UPDATE query.

Definition at line 322 of file UpdateQueryBuilder.php.

◆ getQueryInfo()

Wikimedia\Rdbms\UpdateQueryBuilder::getQueryInfo ( )

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

This can be used to interface with legacy code.

Returns
array The query info array, with keys:
  • table: The table name
  • set: The set array
  • conds: The conditions
  • options: The query options
  • caller: The caller signature

Definition at line 349 of file UpdateQueryBuilder.php.

◆ ignore()

Wikimedia\Rdbms\UpdateQueryBuilder::ignore ( )

Enable the IGNORE option.

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

Returns
$this

Definition at line 302 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\options().

◆ option()

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

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

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

Definition at line 146 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\options().

◆ options()

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

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

Parameters
array$options
Returns
$this

Definition at line 162 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\options().

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

◆ queryInfo()

Wikimedia\Rdbms\UpdateQueryBuilder::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::update.

Parameters
array$infoAssociative array of query info, with keys:
  • table: The table name to be passed to Database::update()
  • set: The set conditions
  • conds: The conditions
  • options: The query options
  • caller: The caller signature.
Returns
$this

Definition at line 96 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\caller(), Wikimedia\Rdbms\UpdateQueryBuilder\options(), Wikimedia\Rdbms\UpdateQueryBuilder\table(), and Wikimedia\Rdbms\UpdateQueryBuilder\where().

◆ set()

Wikimedia\Rdbms\UpdateQueryBuilder::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|RawSQLValue>|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 268 of file UpdateQueryBuilder.php.

◆ table()

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

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

Parameters
string$tableThe unqualified name of a table
Returns
$this

Definition at line 122 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\table().

Referenced by Wikimedia\Rdbms\UpdateQueryBuilder\queryInfo(), Wikimedia\Rdbms\UpdateQueryBuilder\table(), and Wikimedia\Rdbms\UpdateQueryBuilder\update().

◆ update()

Wikimedia\Rdbms\UpdateQueryBuilder::update ( string $table)

Set table for the query.

Alias for table().

Parameters
string$tableThe unqualified name of a table
Returns
$this

Definition at line 134 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\table().

◆ where()

Wikimedia\Rdbms\UpdateQueryBuilder::where ( $conds)

Add conditions to the query.

The supplied conditions will be appended to the existing conditions, separated by AND.

Parameters
string|IExpression|array<string,?scalar|non-empty-array<int,?scalar>|RawSQLValue>|array<int,string|IExpression>$conds

May be either a string containing a single condition, or an array of conditions. If an array is given, the conditions constructed from each element are combined with AND.

Array elements may take one of two forms:

  • Elements with a numeric key are interpreted as raw SQL fragments.
  • Elements with a string key are interpreted as equality conditions, where the key is the field name.
    • If the value of such an array element is a scalar (such as a string), it will be treated as data and thus quoted appropriately. If it is null, an IS NULL clause will be added.
    • If the value is an array, an IN (...) clause will be constructed from its non-null elements, and an IS NULL clause will be added if null is present, such that the field may match any of the elements in the array. The non-null elements will be quoted.

Note that expressions are often DBMS-dependent in their syntax. DBMS-independent wrappers are provided for constructing several types of expression commonly used in condition queries. See:

  • IDatabase::buildLike()
  • IDatabase::conditional()

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 204 of file UpdateQueryBuilder.php.

References Wikimedia\Rdbms\UpdateQueryBuilder\conds(), and Wikimedia\Rdbms\Platform\ISQLPlatform\LIST_AND.

Referenced by Wikimedia\Rdbms\UpdateQueryBuilder\andWhere(), Wikimedia\Rdbms\UpdateQueryBuilder\conds(), and Wikimedia\Rdbms\UpdateQueryBuilder\queryInfo().

Member Data Documentation

◆ $db

IDatabase Wikimedia\Rdbms\UpdateQueryBuilder::$db
protected

◆ $options

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

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

Definition at line 48 of file UpdateQueryBuilder.php.


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