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'
| Name | Description |
|---|---|
| Query::$comments | An array of comments that can be prepended to a query. |
| Query::$connection | The connection object on which to run this query. |
| Query::$connectionKey | The key of the connection object. |
| Query::$connectionTarget | The target of the connection object. |
| Query::$queryOptions | The query options to pass on to the connection object. |
| UpdateQuery::$expressionFields | Array of fields to update to an expression in case of a duplicate record. |
| UpdateQuery::$table | The table to update. |
drupal/
<?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->isNull($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();
}
}
?>