Skip to content

Commit f2e2a8d

Browse files
ozgengreenbonebot
authored andcommitted
refactor: simplify report host lookup for filtered OS results
Use host and hostname values from the result iterator to resolve the related report host, instead of querying through the result id.
1 parent 2d345ac commit f2e2a8d

1 file changed

Lines changed: 38 additions & 25 deletions

File tree

src/manage_sql_report_operating_systems.c

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,47 @@
2222
#include "sql.h"
2323

2424
/**
25-
* @brief Get the report host for a result.
25+
* @brief Get the report host matching result host information.
2626
*
27-
* @param[in] result Result whose related report host to return.
27+
* @param[in] report Report containing the result.
28+
* @param[in] host Host value from the result.
29+
* @param[in] hostname Hostname value from the result.
2830
*
2931
* @return Related report host on success, or 0 if not found.
3032
*/
3133
static report_host_t
32-
result_report_host (result_t result)
34+
result_report_host (report_t report, const char *host, const char *hostname)
3335
{
34-
return sql_int (
35-
"SELECT rh.id"
36-
" FROM results r"
37-
" JOIN report_hosts rh"
38-
" ON rh.report = r.report"
39-
" AND ("
40-
" (COALESCE (r.host, '') <> ''"
41-
" AND rh.host = r.host"
42-
" AND COALESCE (rh.hostname, '') = COALESCE (r.hostname, ''))"
43-
" OR"
44-
" (COALESCE (r.host, '') = ''"
45-
" AND COALESCE (r.hostname, '') <> ''"
46-
" AND COALESCE (rh.hostname, '') = COALESCE (r.hostname, ''))"
47-
" )"
48-
" WHERE r.id = $1"
49-
" LIMIT 1;",
50-
SQL_INT_PARAM (result),
51-
NULL);
36+
if (report == 0)
37+
return 0;
38+
39+
if (host && *host)
40+
{
41+
return sql_int_ps (
42+
"SELECT id"
43+
" FROM report_hosts"
44+
" WHERE report = $1"
45+
" AND host = $2"
46+
" LIMIT 1;",
47+
SQL_RESOURCE_PARAM (report),
48+
SQL_STR_PARAM (host),
49+
NULL);
50+
}
51+
52+
if (hostname && *hostname)
53+
{
54+
return sql_int_ps (
55+
"SELECT id"
56+
" FROM report_hosts"
57+
" WHERE report = $1"
58+
" AND hostname = $2"
59+
" LIMIT 1;",
60+
SQL_RESOURCE_PARAM (report),
61+
SQL_STR_PARAM (hostname),
62+
NULL);
63+
}
64+
65+
return 0;
5266
}
5367

5468
/**
@@ -157,11 +171,10 @@ fill_filtered_report_host_ids (GHashTable **report_host_ids,
157171

158172
while (next (results))
159173
{
160-
result_t result;
161174
report_host_t report_host;
162-
163-
result = result_iterator_result (results);
164-
report_host = result_report_host (result);
175+
report_host = result_report_host (report,
176+
result_iterator_host (results),
177+
result_iterator_hostname (results));
165178

166179
if (report_host)
167180
g_hash_table_add (*report_host_ids, GSIZE_TO_POINTER (report_host));

0 commit comments

Comments
 (0)