BuildExpr resolves the sort field into a valid db sort expression.
(fieldResolver FieldResolver)
| 24 | |
| 25 | // BuildExpr resolves the sort field into a valid db sort expression. |
| 26 | func (s *SortField) BuildExpr(fieldResolver FieldResolver) (string, error) { |
| 27 | // special case for random sort |
| 28 | if s.Name == randomSortKey { |
| 29 | return "RANDOM()", nil |
| 30 | } |
| 31 | |
| 32 | // special case for the builtin SQLite rowid column |
| 33 | if s.Name == rowidSortKey { |
| 34 | return fmt.Sprintf("[[_rowid_]] %s", s.Direction), nil |
| 35 | } |
| 36 | |
| 37 | result, err := fieldResolver.Resolve(s.Name) |
| 38 | |
| 39 | // invalidate empty fields and non-column identifiers |
| 40 | if err != nil || len(result.Params) > 0 || result.Identifier == "" || strings.ToLower(result.Identifier) == "null" { |
| 41 | return "", fmt.Errorf("invalid sort field %q", s.Name) |
| 42 | } |
| 43 | |
| 44 | return fmt.Sprintf("%s %s", result.Identifier, s.Direction), nil |
| 45 | } |
| 46 | |
| 47 | // ParseSortFromString parses the provided string expression |
| 48 | // into a slice of SortFields. |