limit.go 905 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package clause
  2. import "strconv"
  3. // Limit limit clause
  4. type Limit struct {
  5. Limit int
  6. Offset int
  7. }
  8. // Name where clause name
  9. func (limit Limit) Name() string {
  10. return "LIMIT"
  11. }
  12. // Build build where clause
  13. func (limit Limit) Build(builder Builder) {
  14. if limit.Limit > 0 {
  15. builder.WriteString("LIMIT ")
  16. builder.WriteString(strconv.Itoa(limit.Limit))
  17. }
  18. if limit.Offset > 0 {
  19. if limit.Limit > 0 {
  20. builder.WriteString(" ")
  21. }
  22. builder.WriteString("OFFSET ")
  23. builder.WriteString(strconv.Itoa(limit.Offset))
  24. }
  25. }
  26. // MergeClause merge order by clauses
  27. func (limit Limit) MergeClause(clause *Clause) {
  28. clause.Name = ""
  29. if v, ok := clause.Expression.(Limit); ok {
  30. if limit.Limit == 0 && v.Limit != 0 {
  31. limit.Limit = v.Limit
  32. }
  33. if limit.Offset == 0 && v.Offset > 0 {
  34. limit.Offset = v.Offset
  35. } else if limit.Offset < 0 {
  36. limit.Offset = 0
  37. }
  38. }
  39. clause.Expression = limit
  40. }