* Search: fix PostgreSQL query
* Apply suggestion from code review
(cherry picked from commit 5a1d8cbea7)
Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
This commit is contained in:
co-authored by
Sofia Papagiannaki
parent
f86e30f492
commit
17354bfbe6
@@ -115,15 +115,31 @@ func (b *Builder) applyFilters() (ordering string) {
|
||||
b.params = append(b.params, whereParams...)
|
||||
}
|
||||
|
||||
if len(groups) > 0 {
|
||||
b.sql.WriteString(fmt.Sprintf(" GROUP BY %s", strings.Join(groups, ", ")))
|
||||
b.params = append(b.params, groupParams...)
|
||||
}
|
||||
|
||||
if len(orders) < 1 {
|
||||
orders = append(orders, TitleSorter{}.OrderBy())
|
||||
}
|
||||
|
||||
if len(groups) > 0 {
|
||||
cols := make([]string, 0, len(orders)+len(groups))
|
||||
for _, o := range orders {
|
||||
o := strings.TrimSuffix(o, " DESC")
|
||||
o = strings.TrimSuffix(o, " ASC")
|
||||
exists := false
|
||||
for _, g := range groups {
|
||||
if g == o {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
cols = append(cols, o)
|
||||
}
|
||||
}
|
||||
cols = append(cols, groups...)
|
||||
b.sql.WriteString(fmt.Sprintf(" GROUP BY %s", strings.Join(cols, ", ")))
|
||||
b.params = append(b.params, groupParams...)
|
||||
}
|
||||
|
||||
orderBy := fmt.Sprintf(" ORDER BY %s", strings.Join(orders, ", "))
|
||||
b.sql.WriteString(orderBy)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user