Fix record sorting order (NULLS FIRST/LAST)

This commit is contained in:
Denis Arh
2020-09-15 08:34:48 +02:00
parent 683f5e2b23
commit 7af6218de9
5 changed files with 31 additions and 5 deletions
+1
View File
@@ -43,6 +43,7 @@ func Connect(ctx context.Context, dsn string) (store.Storer, error) {
cfg.ErrorHandler = errorHandler
cfg.UpsertBuilder = UpsertBuilder
cfg.CastModuleFieldToColumnType = fieldToColumnTypeCaster
cfg.SqlSortHandler = SqlSortHandler
if s.Store, err = rdbms.Connect(ctx, cfg); err != nil {
return nil, err
+11
View File
@@ -0,0 +1,11 @@
package mysql
import "fmt"
func SqlSortHandler(exp string, desc bool) string {
if desc {
return fmt.Sprintf("%s DESC", exp)
} else {
return fmt.Sprintf("%s ASC", exp)
}
}
+2 -5
View File
@@ -321,8 +321,6 @@ func (s Store) composeRecordsSorter(m *types.Module, q squirrel.SelectBuilder, s
if sortable[c.Column] {
sqlSort[i] = c.Column
} else if f := m.Fields.FindByName(c.Column); f != nil {
//sqlSort[i] = fmt.Sprintf("%s%s.value", composeRecordValueAliasPfx, sort[i].Column)
// sqlSort[i] = fmt.Sprintf("COALESCE(%s%s.value, '')", composeRecordValueAliasPfx + sort[i].Column)
sqlSort[i], err = s.config.CastModuleFieldToColumnType(f, c.Column)
} else {
err = fmt.Errorf("could not sort by unknown column: %s", c.Column)
@@ -332,9 +330,8 @@ func (s Store) composeRecordsSorter(m *types.Module, q squirrel.SelectBuilder, s
return q, err
}
if sort[i].Descending {
sqlSort[i] += " DESC"
}
// Apply proper sorting param
sqlSort[i] = s.config.SqlSortHandler(sqlSort[i], sort[i].Descending)
}
return q.OrderBy(sqlSort...), nil
+8
View File
@@ -445,6 +445,14 @@ func setOrderBy(q squirrel.SelectBuilder, sort filter.SortExprSet, ss ...string)
return q.OrderBy(sqlSort...), nil
}
func SqlSortHandler(exp string, desc bool) string {
if desc {
return fmt.Sprintf("%s DESC NULLS LAST", exp)
} else {
return fmt.Sprintf("%s ASC NULLS FIRST", exp)
}
}
// TxNoRetry - Transaction retry handler
//
// Only returns false so transactions will never retry
+9
View File
@@ -111,6 +111,11 @@ type (
// Functions are used in filters and aggregations
SqlFunctionHandler func(f ql.Function) (ql.ASTNode, error)
// SqlSortHandler handles construction of sorting expression
//
// MySQL does not support NULLS FIRST/LAST and without it sorts nulls differently then Postgres/SQLite
SqlSortHandler func(exp string, desc bool) string
CastModuleFieldToColumnType func(ModuleFieldTypeDetector, string) (string, error)
}
)
@@ -184,6 +189,10 @@ func (c *Config) SetDefaults() {
if c.TriggerHandlers == nil {
c.TriggerHandlers = TriggerHandlers{}
}
if c.SqlSortHandler == nil {
c.SqlSortHandler = SqlSortHandler
}
}
// ParseExtra parses extra params (params starting with *)