From 4c9d46ce6653da2ea211704a2d8d2e1adc268683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Anne?= Date: Thu, 28 Feb 2019 07:59:21 +0100 Subject: [PATCH] Fix JOINs in delete and update queries building; fixes #5478 (#5482) --- inc/dbmysql.class.php | 13 +++++++------ tests/database/DBmysql.php | 6 ++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/inc/dbmysql.class.php b/inc/dbmysql.class.php index c9430fa5dbb..a1c2fae6905 100644 --- a/inc/dbmysql.class.php +++ b/inc/dbmysql.class.php @@ -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']); @@ -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; } diff --git a/tests/database/DBmysql.php b/tests/database/DBmysql.php index 24b5dfaa62b..558d0335fae 100644 --- a/tests/database/DBmysql.php +++ b/tests/database/DBmysql.php @@ -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' @@ -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'