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

46 lines
859 B
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
"regexp"
"strings"
)
func (filter *Filter) FormatSQL() (string, error) {
if filter == nil {
return "", nil
}
result := ""
if filter.OrderBy != nil {
order_by := filter.OrderBy
match, err := regexp.MatchString("^(\\w+(,\\s*))*\\w+$", strings.TrimSpace(*filter.OrderBy))
if err != nil {
return "", err
}
if match {
direction := "ASC"
if filter.OrderDirection != nil && filter.OrderDirection.IsValid() {
direction = filter.OrderDirection.String()
}
result += fmt.Sprintf(" ORDER BY %s %s", *order_by, direction)
}
}
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
}