|
22 | 22 | #include "sql.h" |
23 | 23 |
|
24 | 24 | /** |
25 | | - * @brief Get the report host for a result. |
| 25 | + * @brief Get the report host matching result host information. |
26 | 26 | * |
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. |
28 | 30 | * |
29 | 31 | * @return Related report host on success, or 0 if not found. |
30 | 32 | */ |
31 | 33 | static report_host_t |
32 | | -result_report_host (result_t result) |
| 34 | +result_report_host (report_t report, const char *host, const char *hostname) |
33 | 35 | { |
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; |
52 | 66 | } |
53 | 67 |
|
54 | 68 | /** |
@@ -157,11 +171,10 @@ fill_filtered_report_host_ids (GHashTable **report_host_ids, |
157 | 171 |
|
158 | 172 | while (next (results)) |
159 | 173 | { |
160 | | - result_t result; |
161 | 174 | 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)); |
165 | 178 |
|
166 | 179 | if (report_host) |
167 | 180 | g_hash_table_add (*report_host_ids, GSIZE_TO_POINTER (report_host)); |
|
0 commit comments