Skip navigation
Help

UpdateQuery_sqlite

  1. drupal
    1. 7 drupal/includes/database/sqlite/query.inc

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::$nextPlaceholderThe placeholder counter.
Query::$queryOptionsThe query options to pass on to the connection object.
UpdateQuery::$argumentsAn array of values to update to.
UpdateQuery::$conditionThe condition object for this query.
UpdateQuery::$expressionFieldsArray of fields to update to an expression in case of a duplicate record.
UpdateQuery::$fieldsAn array of fields that will be updated.
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::__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::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.

File

drupal/includes/database/sqlite/query.inc, line 64
Query code for SQLite embedded database engine.

View source
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