class Merge extends Query implements ConditionInterface (View source)

General class for an abstracted MERGE query operation.

An ANSI SQL:2003 compatible database would run the following query:

Traits

Provides an implementation of ConditionInterface.

Constants

STATUS_INSERT

Returned by execute() if an INSERT query has been executed.

STATUS_UPDATE

Returned by execute() if an UPDATE query has been executed.

Properties

protected Connection $connection

The connection object on which to run this query.

from  Query
protected string $connectionTarget

The target of the connection object.

from  Query
protected string $connectionKey

The key of the connection object.

from  Query
protected array $queryOptions

The query options to pass on to the connection object.

from  Query
protected string $uniqueIdentifier

A unique identifier for this query object.

from  Query
protected int $nextPlaceholder

The placeholder counter.

from  Query
protected array $comments

An array of comments that can be prepended to a query.

from  Query
protected Condition $condition

The condition object for this query.

from  QueryConditionTrait
protected string $table

The table to be used for INSERT and UPDATE.

protected string $conditionTable

The table or subquery to be used for the condition.

protected array $insertFields

An array of fields on which to insert.

protected array $defaultFields

An array of fields which should be set to their database-defined defaults.

protected string $insertValues

An array of values to be inserted.

protected array $updateFields

An array of fields that will be updated.

protected array $expressionFields

Array of fields to update to an expression in case of a duplicate record.

protected bool $needsUpdate

Flag indicating whether an UPDATE is necessary.

Methods

__construct(Connection $connection, string $table, array $options = [])

Constructs a Merge object.

__sleep()

Implements the magic __sleep function to disconnect from the database.

from  Query
__wakeup()

Implements the magic __wakeup function to reconnect to the database.

from  Query
__clone()

Implements the magic __clone function.

from  Query
execute()

Executes the merge database query.

string
__toString()

Implements PHP magic __toString method to convert the query to a string.

uniqueIdentifier()

Returns a unique identifier for this object.

from  Query
The
nextPlaceholder()

Gets the next placeholder value for this query object.

from  Query
$this
comment($comment)

Adds a comment to the query.

from  Query
array
getComments()

Returns a reference to the comments array for the query.

from  Query
getConnection()

Gets the database connection to be used for the query.

from  Query
condition($field, $value = NULL, $operator = '=')

{@inheritdoc}

isNull($field)

{@inheritdoc}

isNotNull($field)

{@inheritdoc}

exists(SelectInterface $select)

{@inheritdoc}

notExists(SelectInterface $select)

{@inheritdoc}

alwaysFalse()

{@inheritdoc}

conditions()

{@inheritdoc}

arguments()

{@inheritdoc}

where($snippet, $args = [])

{@inheritdoc}

compile(Connection $connection, PlaceholderInterface $queryPlaceholder)

{@inheritdoc}

compiled()

{@inheritdoc}

conditionGroupFactory($conjunction = 'AND')

{@inheritdoc}

andConditionGroup()

{@inheritdoc}

orConditionGroup()

{@inheritdoc}

$this
conditionTable($table)

Sets the table or subquery to be used for the condition.

$this
updateFields(array $fields)

Adds a set of field->value pairs to be updated.

$this
expression($field, $expression, array $arguments = NULL)

Specifies fields to be updated as an expression.

$this
insertFields(array $fields, array $values = [])

Adds a set of field->value pairs to be inserted.

$this
useDefaults(array $fields)

Specifies fields for which the database-defaults should be used.

$this
fields(array $fields, array $values = [])

Sets common field-value pairs in the INSERT and UPDATE query parts.

$this
keys(array $fields, array $values = [])

Sets the key fields to be used as conditions for this query.

$this
key(string $field, mixed $value = NULL)

Sets a single key field to be used as condition for this query.

Details

__construct(Connection $connection, string $table, array $options = [])

Constructs a Merge object.

Parameters

Connection $connection

Database connection object.

string $table

Name of the table to associate with this query.

array $options

Array of query options.

__sleep()

Implements the magic __sleep function to disconnect from the database.

__wakeup()

Implements the magic __wakeup function to reconnect to the database.

__clone()

Implements the magic __clone function.

StatementInterface|null execute()

Executes the merge database query.

Return Value

StatementInterface|null

A prepared statement, or NULL if the query is not valid.

Exceptions

InvalidMergeQueryException

string __toString()

Implements PHP magic __toString method to convert the query to a string.

In the degenerate case, there is no string-able query as this operation is potentially two queries.

Return Value

string

A prepared statement query string for this object.

uniqueIdentifier()

Returns a unique identifier for this object.

The nextPlaceholder()

Gets the next placeholder value for this query object.

Return Value

The

next available placeholder ID as an integer.

$this comment($comment)

Adds a comment to the query.

By adding a comment to a query, you can more easily find it in your query log or the list of active queries on an SQL server. This allows for easier debugging and allows you to more easily find where a query with a performance problem is being generated.

The comment string will be sanitized to remove * / and other characters that may terminate the string early so as to avoid SQL injection attacks.

Parameters

$comment

The comment string to be inserted into the query.

Return Value

$this

array getComments()

Returns a reference to the comments array for the query.

Because this method returns by reference, alter hooks may edit the comments array directly to make their changes. If just adding comments, however, the use of comment() is preferred.

Note that this method must be called by reference as well:

Return Value

array

A reference to the comments array structure.

Connection getConnection()

Gets the database connection to be used for the query.

Return Value

Connection

The database connection to be used for the query.

condition($field, $value = NULL, $operator = '=')

{@inheritdoc}

Parameters

$field
$value
$operator

isNull($field)

{@inheritdoc}

Parameters

$field

isNotNull($field)

{@inheritdoc}

Parameters

$field

exists(SelectInterface $select)

{@inheritdoc}

Parameters

SelectInterface $select

notExists(SelectInterface $select)

{@inheritdoc}

Parameters

SelectInterface $select

alwaysFalse()

{@inheritdoc}

conditions()

{@inheritdoc}

arguments()

{@inheritdoc}

where($snippet, $args = [])

{@inheritdoc}

Parameters

$snippet
$args

compile(Connection $connection, PlaceholderInterface $queryPlaceholder)

{@inheritdoc}

Parameters

Connection $connection
PlaceholderInterface $queryPlaceholder

compiled()

{@inheritdoc}

conditionGroupFactory($conjunction = 'AND')

{@inheritdoc}

Parameters

$conjunction

andConditionGroup()

{@inheritdoc}

orConditionGroup()

{@inheritdoc}

protected $this conditionTable($table)

Sets the table or subquery to be used for the condition.

Parameters

$table

The table name or the subquery to be used. Use a Select query object to pass in a subquery.

Return Value

$this

The called object.

$this updateFields(array $fields)

Adds a set of field->value pairs to be updated.

Parameters

array $fields

An associative array of fields to write into the database. The array keys are the field names and the values are the values to which to set them.

Return Value

$this

The called object.

$this expression($field, $expression, array $arguments = NULL)

Specifies fields to be updated as an expression.

Expression fields are cases such as counter = counter + 1. This method takes precedence over MergeQuery::updateFields() and its wrappers, MergeQuery::key() and MergeQuery::fields().

Parameters

$field

The field to set.

$expression

The field will be set to the value of this expression. This parameter may include named placeholders.

array $arguments

If specified, this is an array of key/value pairs for named placeholders corresponding to the expression.

Return Value

$this

The called object.

$this insertFields(array $fields, array $values = [])

Adds a set of field->value pairs to be inserted.

Parameters

array $fields

An array of fields on which to insert. This array may be indexed or associative. If indexed, the array is taken to be the list of fields. If associative, the keys of the array are taken to be the fields and the values are taken to be corresponding values to insert. If a $values argument is provided, $fields must be indexed.

array $values

An array of fields to insert into the database. The values must be specified in the same order as the $fields array.

Return Value

$this

The called object.

$this useDefaults(array $fields)

Specifies fields for which the database-defaults should be used.

If you want to force a given field to use the database-defined default, not NULL or undefined, use this method to instruct the database to use default values explicitly. In most cases this will not be necessary unless you are inserting a row that is all default values, as you cannot specify no values in an INSERT query.

Specifying a field both in fields() and in useDefaults() is an error and will not execute.

Parameters

array $fields

An array of values for which to use the default values specified in the table definition.

Return Value

$this

The called object.

$this fields(array $fields, array $values = [])

Sets common field-value pairs in the INSERT and UPDATE query parts.

This method should only be called once. It may be called either with a single associative array or two indexed arrays. If called with an associative array, the keys are taken to be the fields and the values are taken to be the corresponding values to set. If called with two arrays, the first array is taken as the fields and the second array is taken as the corresponding values.

Parameters

array $fields

An array of fields to insert, or an associative array of fields and values. The keys of the array are taken to be the fields and the values are taken to be corresponding values to insert.

array $values

An array of values to set into the database. The values must be specified in the same order as the $fields array.

Return Value

$this

The called object.

$this keys(array $fields, array $values = [])

Sets the key fields to be used as conditions for this query.

This method should only be called once. It may be called either with a single associative array or two indexed arrays. If called with an associative array, the keys are taken to be the fields and the values are taken to be the corresponding values to set. If called with two arrays, the first array is taken as the fields and the second array is taken as the corresponding values.

The fields are copied to the condition of the query and the INSERT part. If no other method is called, the UPDATE will become a no-op.

Parameters

array $fields

An array of fields to set, or an associative array of fields and values.

array $values

An array of values to set into the database. The values must be specified in the same order as the $fields array.

Return Value

$this

$this key(string $field, mixed $value = NULL)

Sets a single key field to be used as condition for this query.

Same as \Drupal\Core\Database\Query\Merge::keys() but offering a signature that is more natural for the case of a single key.

Parameters

string $field

The name of the field to set.

mixed $value

The value to set into the database.

Return Value

$this

See also

Merge::keys