Fix device group queries that don't start with devices table (#10360)

This commit is contained in:
Tony Murray
2019-06-21 06:53:48 -05:00
committed by GitHub
parent 6239db7435
commit 7f6a342d71
2 changed files with 11 additions and 1 deletions

View File

@ -176,8 +176,11 @@ class QueryBuilderFluentParser extends QueryBuilderParser
foreach ($this->generateGlue() as $glue) {
list($left, $right) = explode(' = ', $glue, 2);
if (str_contains($right, '.')) { // last line is devices.device_id = ? for alerting... ignore it
list($leftTable, $leftKey) = explode('.', $left);
list($rightTable, $rightKey) = explode('.', $right);
$joins[] = [$rightTable, $left, $right];
$target_table = ($rightTable != 'devices' ? $rightTable : $leftTable); // don't try to join devices
$joins[] = [$target_table, $left, $right];
}
}

View File

@ -172,5 +172,12 @@
"devices.hostname LIKE '%one%' AND (devices.hostname LIKE '%two%' OR devices.hostname LIKE '%three%' OR (devices.hostname LIKE 'six%' AND devices.hostname = \"seven\")) AND (devices.hostname NOT LIKE '%four%' OR devices.hostname NOT LIKE '%five%')",
"SELECT * FROM devices WHERE (devices.device_id = ?) AND devices.hostname LIKE '%one%' AND (devices.hostname LIKE '%two%' OR devices.hostname LIKE '%three%' OR (devices.hostname LIKE 'six%' AND devices.hostname = \"seven\")) AND (devices.hostname NOT LIKE '%four%' OR devices.hostname NOT LIKE '%five%')",
["select * from `devices` where (`devices`.`hostname` LIKE ? AND (`devices`.`hostname` LIKE ? OR `devices`.`hostname` LIKE ? OR (`devices`.`hostname` LIKE ? AND `devices`.`hostname` = ?)) AND (`devices`.`hostname` NOT LIKE ? OR `devices`.`hostname` NOT LIKE ?))", ["%one%","%two%","%three%","six%","seven","%four%","%five%"]]
],
[
"%locations.location ~ somewhere",
{"condition":"AND","rules":[{"id":"locations.location","field":"locations.location","type":"string","input":"text","operator":"regex","value":"somewhere"}],"valid":true},
"locations.location REGEXP \"somewhere\"",
"SELECT * FROM locations,devices WHERE (devices.device_id = ? AND locations.id = devices.location_id) AND locations.location REGEXP \"somewhere\"",
["select * from `devices` left join `locations` on `locations`.`id` = `devices`.`location_id` where (`locations`.`location` REGEXP ?)", ["somewhere"]]
]
]