MCPcopy Index your code
hub / github.com/CovenantSQL/CovenantSQL / ResolveOrderBy

Function ResolveOrderBy

cmd/cql-proxy/resolver/resolver.go:437–477  ·  view source on GitHub ↗

ResolveOrderBy resolves order by statement.

(q map[string]interface{}, availFields FieldMap)

Source from the content-addressed store, hash-verified

435
436// ResolveOrderBy resolves order by statement.
437func ResolveOrderBy(q map[string]interface{}, availFields FieldMap) (fields FieldMap, statement string, err error) {
438 fields = FieldMap{}
439
440 if len(q) == 0 {
441 // no order by config
442 return
443 }
444
445 var colStatements []string
446
447 for k, v := range q {
448 if !availFields[k] {
449 err = errors.Errorf("unknown field: %s", k)
450 return
451 }
452
453 if !isLiteral(v) {
454 err = errors.Errorf("invalid sort direction: %v", v)
455 return
456 }
457
458 var direction string
459
460 if asInt(v) > 0 {
461 direction = "ASC"
462 } else if asInt(v) < 0 {
463 direction = "DESC"
464 } else {
465 err = errors.Errorf("invalid sort direction: %v", v)
466 return
467 }
468
469 fields[k] = true
470
471 colStatements = append(colStatements, fmt.Sprintf(`"%s" %s`, k, direction))
472 }
473
474 statement = strings.Join(colStatements, ",")
475
476 return
477}
478
479func asInt(v interface{}) int64 {
480 switch d := v.(type) {

Callers 1

FindFunction · 0.85

Calls 3

isLiteralFunction · 0.85
asIntFunction · 0.85
ErrorfMethod · 0.80

Tested by

no test coverage detected