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:
Leonard Gram 2022-03-15 15:08:40 +01:00 committed by GitHub
parent 16cf179df1
commit f46038ed3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 1 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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, "")