From ef4e6de867df2bf7d044203d3235b614de12747f Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Wed, 2 Oct 2019 13:54:54 +0000 Subject: [PATCH] Fix device_groups in alert/group builder (#10643) --- LibreNMS/Alerting/QueryBuilderParser.php | 3 ++- LibreNMS/DB/Schema.php | 9 ++------- tests/data/misc/querybuilder.json | 8 ++++++++ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/LibreNMS/Alerting/QueryBuilderParser.php b/LibreNMS/Alerting/QueryBuilderParser.php index ede26f9baf..eaa2f6e51e 100644 --- a/LibreNMS/Alerting/QueryBuilderParser.php +++ b/LibreNMS/Alerting/QueryBuilderParser.php @@ -430,7 +430,8 @@ class QueryBuilderParser implements \JsonSerializable } if (!$this->schema->columnExists($child, $child_key)) { - echo"FIXME: Could not make glue from $child to $parent\n"; + $child_key = $this->schema->getPrimaryKey($child); + \Log::warning("QueryBuilderParser: Warning, guessing glue from $child.$child_key to $parent.$parent_key"); } } diff --git a/LibreNMS/DB/Schema.php b/LibreNMS/DB/Schema.php index 486a10d0ba..55426440ab 100644 --- a/LibreNMS/DB/Schema.php +++ b/LibreNMS/DB/Schema.php @@ -89,19 +89,14 @@ class Schema * Get the primary key column(s) for a table * * @param string $table - * @return string|array if a single column just the name is returned, otherwise an array of column names + * @return string if a single column just the name is returned, otherwise the first column listed will be returned */ public function getPrimaryKey($table) { $schema = $this->getSchema(); - $columns = $schema[$table]['Indexes']['PRIMARY']['Columns']; - if (count($columns) == 1) { - return reset($columns); - } - - return $columns; + return reset($columns); } public function getSchema() diff --git a/tests/data/misc/querybuilder.json b/tests/data/misc/querybuilder.json index 1739f13d04..d56d68c042 100644 --- a/tests/data/misc/querybuilder.json +++ b/tests/data/misc/querybuilder.json @@ -180,4 +180,12 @@ "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"]] ] +, + [ + "%device_groups.id = 1", + {"condition":"AND","rules":[{"id":"device_groups.id","field":"device_groups.id","type":"string","input":"text","operator":"equal","value":"1"}],"valid":true}, + "device_groups.id = 1", + "SELECT * FROM devices,device_group_device,device_groups WHERE (devices.device_id = ? AND devices.device_id = device_group_device.device_id AND device_group_device.device_group_id = device_groups.id) AND device_groups.id = 1", + ["select * from `devices` left join `device_group_device` on `devices`.`device_id` = `device_group_device`.`device_id` left join `device_groups` on `device_group_device`.`device_group_id` = `device_groups`.`id` where (`device_groups`.`id` = ?)", ["1"]] + ] ]