dal/pkg/builder/convert_sort.go
Anton Nesterov 1db30b92c2
[wip] builder
Signed-off-by: Anton Nesterov <anton@demiurg.io>
2024-08-09 21:14:28 +02:00

59 lines
1,007 B
Go

package builder
import (
"fmt"
"strings"
)
func ConvertSort(ctx Context, sort Map) (string, error) {
if sort == nil {
return "", nil
}
keys := aggregateSortedKeys([]Map{sort})
expressions := make([]string, 0)
for _, key := range keys {
name := ctx.GetColumnName(key)
order := normalize_order(sort[key])
if order != "" {
order = " " + order
}
expressions = append(expressions, name+order)
}
return fmt.Sprintf("ORDER BY %s", strings.Join(expressions, ", ")), nil
}
func normalize_order(order interface{}) string {
if order == nil {
return ""
}
orderInt, ok := order.(int)
if ok {
if orderInt == 1 {
return "ASC"
}
if orderInt == -1 {
return "DESC"
}
}
orderStr, ok := order.(string)
if !ok {
return ""
}
if orderStr == "" {
return ""
}
if orderStr == "1" {
return "ASC"
}
if orderStr == "-1" {
return "DESC"
}
if strings.ToUpper(orderStr) == "ASC" {
return "ASC"
}
if strings.ToUpper(orderStr) == "DESC" {
return "DESC"
}
return ""
}