Fix where clause building with right hosts alias (#2875)

This commit is contained in:
Martin Angers 2021-11-10 12:24:19 -05:00 committed by GitHub
parent 700d78278b
commit b7ed3589a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 3 additions and 2 deletions

View file

@ -737,7 +737,7 @@ func (d *Datastore) SearchHosts(ctx context.Context, filter fleet.TeamFilter, qu
}
args = append(args, in)
sqlb.WriteString(" id NOT IN (?) AND ")
sqlb.WriteString(d.whereFilterHostsByTeams(filter, "hosts"))
sqlb.WriteString(d.whereFilterHostsByTeams(filter, "h"))
sqlb.WriteString(` ORDER BY hst.seen_time DESC LIMIT 10`)
sql, args, err := sqlx.In(sqlb.String(), args...)

View file

@ -443,6 +443,7 @@ func (d *Datastore) ListHostsInLabel(ctx context.Context, filter fleet.TeamFilte
return hosts, nil
}
// NOTE: the hosts table must be aliased to `h` in the query passed to this function.
func (d *Datastore) applyHostLabelFilters(filter fleet.TeamFilter, lid uint, query string, opt fleet.HostListOptions) (string, []interface{}) {
params := []interface{}{lid}
@ -456,7 +457,7 @@ func (d *Datastore) applyHostLabelFilters(filter fleet.TeamFilter, lid uint, que
}
func (d *Datastore) CountHostsInLabel(ctx context.Context, filter fleet.TeamFilter, lid uint, opt fleet.HostListOptions) (int, error) {
query := `SELECT count(*) FROM label_membership lm
query := `SELECT count(*) FROM label_membership lm
JOIN hosts h ON (lm.host_id = h.id)
LEFT JOIN host_seen_times hst ON (h.id=hst.host_id)
WHERE lm.label_id = ?`