Skip to content

Commit

Permalink
Fix JOINs in delete and update queries building; fixes #5478 (#5482)
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne authored and trasher committed Feb 28, 2019
1 parent 683965d commit 4c9d46c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 7 additions & 6 deletions inc/dbmysql.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -892,19 +892,20 @@ public function buildUpdate($table, $params, $clauses, array $joins = []) {
throw new \RuntimeException('Cannot run an UPDATE query without WHERE clause!');
}

$query = "UPDATE ". self::quoteName($table) ." SET ";
$query = "UPDATE ". self::quoteName($table);

//JOINS
$it = new DBmysqlIterator($this);
$query .= $it->analyzeJoins($joins);

$query .= " SET ";
foreach ($params as $field => $value) {
$query .= self::quoteName($field) . " = ".$this->quoteValue($value).", ";
}
$query = rtrim($query, ', ');

$it = new DBmysqlIterator($this);
$query .= " WHERE " . $it->analyseCrit($clauses['WHERE']);

//JOINS
$query .= $it->analyzeJoins($joins);

// ORDER BY
if (isset($clauses['ORDER']) && !empty($clauses['ORDER'])) {
$query .= $it->handleOrderClause($clauses['ORDER']);
Expand Down Expand Up @@ -1020,8 +1021,8 @@ public function buildDelete($table, $where, array $joins = []) {
$query = "DELETE FROM ". self::quoteName($table);

$it = new DBmysqlIterator($this);
$query .= " WHERE " . $it->analyseCrit($where);
$query .= $it->analyzeJoins($joins);
$query .= " WHERE " . $it->analyseCrit($where);

return $query;
}
Expand Down
6 changes: 4 additions & 2 deletions tests/database/DBmysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ public function testBuildUpdate() {
], ['id' => 1]);
$this->string($built)->isIdenticalTo($expected);

$expected = "UPDATE `glpi_computers` SET `name` = '_join_computer1' WHERE `glpi_locations`.`name` = 'test' AND `glpi_computertypes`.`name` = 'laptop'";
$expected = "UPDATE `glpi_computers`";
$expected .= " LEFT JOIN `glpi_locations` ON (`glpi_computers`.`locations_id` = `glpi_locations`.`id`)";
$expected .= " LEFT JOIN `glpi_computertypes` ON (`glpi_computers`.`computertypes_id` = `glpi_computertypes`.`id`)";
$expected .= " SET `name` = '_join_computer1' WHERE `glpi_locations`.`name` = 'test' AND `glpi_computertypes`.`name` = 'laptop'";
$built = $DB->buildUpdate('glpi_computers', ['name' => '_join_computer1'], [
'glpi_locations.name' => 'test',
'glpi_computertypes.name' => 'laptop'
Expand Down Expand Up @@ -121,9 +122,10 @@ public function testBuildDelete() {
$built = $DB->buildDelete('glpi_tickets', ['id' => 1]);
$this->string($built)->isIdenticalTo($expected);

$expected = "DELETE FROM `glpi_computers` WHERE `glpi_locations`.`name` = 'test' AND `glpi_computertypes`.`name` = 'laptop'";
$expected = "DELETE FROM `glpi_computers`";
$expected .= " LEFT JOIN `glpi_locations` ON (`glpi_computers`.`locations_id` = `glpi_locations`.`id`)";
$expected .= " LEFT JOIN `glpi_computertypes` ON (`glpi_computers`.`computertypes_id` = `glpi_computertypes`.`id`)";
$expected .= " WHERE `glpi_locations`.`name` = 'test' AND `glpi_computertypes`.`name` = 'laptop'";
$built = $DB->buildDelete('glpi_computers', [
'glpi_locations.name' => 'test',
'glpi_computertypes.name' => 'laptop'
Expand Down

0 comments on commit 4c9d46c

Please sign in to comment.