UpdateQuery_sqlite

  1. drupal
    1. 8
    2. 7

SQLite specific implementation of UpdateQuery.

SQLite counts all the rows that match the conditions as modified, even if they will not be affected by the query. We workaround this by ensuring that we don't select those rows.

A query like this one: UPDATE test SET name = 'newname' WHERE tid = 1 will become: UPDATE test SET name = 'newname' WHERE tid = 1 AND name <> 'newname'

Hierarchy

Properties

NameDescription
Query::$commentsAn array of comments that can be prepended to a query.
Query::$connectionThe connection object on which to run this query.
Query::$connectionKeyThe key of the connection object.
Query::$connectionTargetThe target of the connection object.
Query::$queryOptionsThe query options to pass on to the connection object.
UpdateQuery::$expressionFieldsArray of fields to update to an expression in case of a duplicate record.
UpdateQuery::$tableThe table to update.

Functions & methods

NameDescription
Query::commentAdds a comment to the query.
Query::getCommentsReturns a reference to the comments array for the query.
Query::nextPlaceholderGets the next placeholder value for this query object. Overrides QueryPlaceholderInterface::nextPlaceholder
Query::uniqueIdentifierReturns a unique identifier for this object. Overrides QueryPlaceholderInterface::uniqueIdentifier
Query::__cloneImplements the magic __clone function.
Query::__sleepImplements the magic __sleep function to disconnect from the database.
Query::__wakeupImplements the magic __wakeup function to reconnect to the database.
UpdateQuery::argumentsImplements QueryConditionInterface::arguments(). Overrides QueryConditionInterface::arguments
UpdateQuery::compileImplements QueryConditionInterface::compile(). Overrides QueryConditionInterface::compile
UpdateQuery::compiledImplements QueryConditionInterface::compiled(). Overrides QueryConditionInterface::compiled
UpdateQuery::conditionImplements QueryConditionInterface::condition(). Overrides QueryConditionInterface::condition
UpdateQuery::conditionsImplements QueryConditionInterface::conditions(). Overrides QueryConditionInterface::conditions
UpdateQuery::existsImplements QueryConditionInterface::exists(). Overrides QueryConditionInterface::exists
UpdateQuery::expressionSpecifies fields to be updated as an expression.
UpdateQuery::fieldsAdds a set of field->value pairs to be updated.
UpdateQuery::isNotNullImplements QueryConditionInterface::isNotNull(). Overrides QueryConditionInterface::isNotNull
UpdateQuery::isNullImplements QueryConditionInterface::isNull(). Overrides QueryConditionInterface::isNull
UpdateQuery::notExistsImplements QueryConditionInterface::notExists(). Overrides QueryConditionInterface::notExists
UpdateQuery::whereImplements QueryConditionInterface::where(). Overrides QueryConditionInterface::where
UpdateQuery::__constructConstructs an UpdateQuery object. Overrides Query::__construct
UpdateQuery::__toStringImplements PHP magic __toString method to convert the query to a string. Overrides Query::__toString
UpdateQuery_sqlite::executeExecutes the UPDATE query. Overrides UpdateQuery::execute
UpdateQuery_sqlite::removeFieldsInConditionHelper function that removes the fields that are already in a condition.

includes/database/sqlite/query.inc, line 64

View source
<?php
class UpdateQuery_sqlite extends UpdateQuery {
  /**
   * Helper function that removes the fields that are already in a condition.
   *
   * @param $fields
   *   The fields.
   * @param QueryConditionInterface $condition
   *   A database condition.
   */
  protected function removeFieldsInCondition(&$fields, QueryConditionInterface $condition) {
    foreach ($condition->conditions() as $child_condition) {
      if ($child_condition['field'] instanceof QueryConditionInterface) {
        $this->removeFieldsInCondition($fields, $child_condition['field']);
      }
      else {
        unset($fields[$child_condition['field']]);
      }
    }
  }

  public function execute() {
    if (!empty($this->queryOptions['sqlite_return_matched_rows'])) {
      return parent::execute();
    }

    // Get the fields used in the update query, and remove those that are already
    // in the condition.
    $fields = $this->expressionFields + $this->fields;
    $this->removeFieldsInCondition($fields, $this->condition);

    // Add the inverse of the fields to the condition.
    $condition = new DatabaseCondition('OR');
    foreach ($fields as $field => $data) {
      if (is_array($data)) {
        // The field is an expression.
        $condition->where($field . ' <> ' . $data['expression']);
        $condition->isNull($field);
      }
      elseif (!isset($data)) {
        // The field will be set to NULL.
        $condition->isNotNull($field);
      }
      else {
        $condition->condition($field, $data, '<>');
        $condition->isNull($field);
      }
    }
    if (count($condition)) {
      $condition->compile($this->connection, $this);
      $this->condition->where((string) $condition, $condition->arguments());
    }
    return parent::execute();
  }

}
?>

Related topics