order_by.go 898 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package clause
  2. type OrderByColumn struct {
  3. Column Column
  4. Desc bool
  5. Reorder bool
  6. }
  7. type OrderBy struct {
  8. Columns []OrderByColumn
  9. }
  10. // Name where clause name
  11. func (orderBy OrderBy) Name() string {
  12. return "ORDER BY"
  13. }
  14. // Build build where clause
  15. func (orderBy OrderBy) Build(builder Builder) {
  16. for idx, column := range orderBy.Columns {
  17. if idx > 0 {
  18. builder.WriteByte(',')
  19. }
  20. builder.WriteQuoted(column.Column)
  21. if column.Desc {
  22. builder.Write(" DESC")
  23. }
  24. }
  25. }
  26. // MergeClause merge order by clauses
  27. func (orderBy OrderBy) MergeClause(clause *Clause) {
  28. if v, ok := clause.Expression.(OrderBy); ok {
  29. for i := len(orderBy.Columns) - 1; i >= 0; i-- {
  30. if orderBy.Columns[i].Reorder {
  31. orderBy.Columns = orderBy.Columns[i:]
  32. clause.Expression = orderBy
  33. return
  34. }
  35. }
  36. orderBy.Columns = append(v.Columns, orderBy.Columns...)
  37. }
  38. clause.Expression = orderBy
  39. }