mirror of
https://github.com/grafana/grafana.git
synced 2025-01-02 04:07:15 -06:00
Search: sort results correctly when using postgres (#46466)
* Search: sort results correctly when using postgres postgresql puts nulls first while both mysql and sqlite puts them last * linting
This commit is contained in:
parent
16cf179df1
commit
f46038ed3a
@ -27,6 +27,8 @@ type Dialect interface {
|
||||
BooleanStr(bool) string
|
||||
DateTimeFunc(string) string
|
||||
|
||||
OrderBy(order string) string
|
||||
|
||||
CreateIndexSQL(tableName string, index *Index) string
|
||||
CreateTableSQL(table *Table) string
|
||||
AddColumnSQL(tableName string, col *Column) string
|
||||
@ -308,3 +310,7 @@ func (b *BaseDialect) Lock(_ LockCfg) error {
|
||||
func (b *BaseDialect) Unlock(_ LockCfg) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BaseDialect) OrderBy(order string) string {
|
||||
return order
|
||||
}
|
||||
|
@ -284,6 +284,19 @@ func (db *PostgresDialect) Lock(cfg LockCfg) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// OrderBy normalizes ordering so that nulls end up last in sorting, which they do by default in both sqlite and mysql but not postgres
|
||||
// order should be a string like `dashboard.id ASC`
|
||||
func (db *PostgresDialect) OrderBy(order string) string {
|
||||
nullSort := "FIRST"
|
||||
normalizedOrder := strings.ToUpper(strings.TrimSpace(order))
|
||||
|
||||
if strings.HasSuffix(normalizedOrder, " DESC") {
|
||||
nullSort = "LAST"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s NULLS %s", order, nullSort)
|
||||
}
|
||||
|
||||
func (db *PostgresDialect) Unlock(cfg LockCfg) error {
|
||||
// trying to release a previously-acquired exclusive session level advisory lock.
|
||||
// it will either return true if the lock is successfully released or
|
||||
|
@ -140,7 +140,12 @@ func (b *Builder) applyFilters() (ordering string) {
|
||||
b.params = append(b.params, groupParams...)
|
||||
}
|
||||
|
||||
orderBy := fmt.Sprintf(" ORDER BY %s", strings.Join(orders, ", "))
|
||||
orderByCols := []string{}
|
||||
for _, o := range orders {
|
||||
orderByCols = append(orderByCols, b.Dialect.OrderBy(o))
|
||||
}
|
||||
|
||||
orderBy := fmt.Sprintf(" ORDER BY %s", strings.Join(orderByCols, ", "))
|
||||
b.sql.WriteString(orderBy)
|
||||
|
||||
order := strings.Join(orderJoins, "")
|
||||
|
Loading…
Reference in New Issue
Block a user