Skip to content

Commit

Permalink
Fix deletions using joins; fixes glpi-project#5478
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric-anne authored and nettalhi_cgi committed Aug 26, 2019
1 parent 27fb02b commit f5bf685
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion inc/dbmysql.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1018,7 +1018,7 @@ public function buildDelete($table, $where, array $joins = []) {
throw new \RuntimeException('Cannot run an DELETE query without WHERE clause!');
}

$query = "DELETE FROM ". self::quoteName($table);
$query = "DELETE " . self::quoteName($table) . " FROM ". self::quoteName($table);

$it = new DBmysqlIterator($this);
$query .= $it->analyzeJoins($joins);
Expand Down
8 changes: 4 additions & 4 deletions tests/database/DBmysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public function testBuildUpdate() {
$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'], [
$expected .= " SET `glpi_computers`.`name` = '_join_computer1' WHERE `glpi_locations`.`name` = 'test' AND `glpi_computertypes`.`name` = 'laptop'";
$built = $DB->buildUpdate('glpi_computers', ['glpi_computers.name' => '_join_computer1'], [
'glpi_locations.name' => 'test',
'glpi_computertypes.name' => 'laptop'
], [
Expand All @@ -118,11 +118,11 @@ public function testBuildUpdate() {
public function testBuildDelete() {
global $DB;

$expected = "DELETE FROM `glpi_tickets` WHERE `id` = '1'";
$expected = "DELETE `glpi_tickets` FROM `glpi_tickets` WHERE `id` = '1'";
$built = $DB->buildDelete('glpi_tickets', ['id' => 1]);
$this->string($built)->isIdenticalTo($expected);

$expected = "DELETE FROM `glpi_computers`";
$expected = "DELETE `glpi_computers` 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'";
Expand Down
10 changes: 5 additions & 5 deletions tests/units/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,27 +225,27 @@ protected function dataDelete() {
'table', [
'id' => 1
],
'DELETE FROM `table` WHERE `id` = \'1\''
'DELETE `table` FROM `table` WHERE `id` = \'1\''
], [
'table', [
'id' => [1, 2]
],
'DELETE FROM `table` WHERE `id` IN (\'1\', \'2\')'
'DELETE `table` FROM `table` WHERE `id` IN (\'1\', \'2\')'
], [
'table', [
'NOT' => ['id' => [1, 2]]
],
'DELETE FROM `table` WHERE NOT (`id` IN (\'1\', \'2\'))'
'DELETE `table` FROM `table` WHERE NOT (`id` IN (\'1\', \'2\'))'
], [
'table', [
'NOT' => ['id' => [new \QueryParam(), new \QueryParam()]]
],
'DELETE FROM `table` WHERE NOT (`id` IN (?, ?))'
'DELETE `table` FROM `table` WHERE NOT (`id` IN (?, ?))'
], [
'table', [
'NOT' => ['id' => [new \QueryParam('idone'), new \QueryParam('idtwo')]]
],
'DELETE FROM `table` WHERE NOT (`id` IN (:idone, :idtwo))'
'DELETE `table` FROM `table` WHERE NOT (`id` IN (:idone, :idtwo))'
]
];
}
Expand Down

0 comments on commit f5bf685

Please sign in to comment.