1
Fork 0
photoview/api/graphql/models/utils.go

49 lines
1.1 KiB
Go
Raw Normal View History

2020-02-09 15:26:59 +01:00
package models
import (
"fmt"
"log"
2020-02-09 15:26:59 +01:00
)
func (filter *Filter) FormatSQL(context string) (string, error) {
2020-02-09 15:26:59 +01:00
if filter == nil {
return "", nil
}
orderByMap := make(map[string]string)
orderByMap["media_date_shot"] = "media.date_shot"
orderByMap["media_date_imported"] = "media.date_imported"
orderByMap["media_title"] = "media.title"
orderByMap["media_kind"] = "media.media_type, SUBSTRING_INDEX(media.path, '.', -1)"
orderByMap["album_title"] = "album.title"
2020-02-09 15:26:59 +01:00
result := ""
if filter.OrderBy != nil {
order_by, ok := orderByMap[context+"_"+*filter.OrderBy]
if !ok {
log.Printf("Invalid order column: '%s'\n", *filter.OrderBy)
return "", nil
2020-02-09 15:26:59 +01:00
}
direction := "ASC"
if filter.OrderDirection != nil && filter.OrderDirection.IsValid() {
direction = filter.OrderDirection.String()
2020-02-09 15:26:59 +01:00
}
result += fmt.Sprintf(" ORDER BY %s %s", order_by, direction)
2020-02-09 15:26:59 +01:00
}
if filter.Limit != nil {
offset := 0
if filter.Offset != nil && *filter.Offset >= 0 {
offset = *filter.Offset
}
result += fmt.Sprintf(" LIMIT %d OFFSET %d", *filter.Limit, offset)
}
log.Printf("SQL Filter: '%s'\n", result)
2020-02-09 15:26:59 +01:00
return result, nil
}