limit.go 780 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.Write("LIMIT ")
  16. builder.Write(strconv.Itoa(limit.Limit))
  17. if limit.Offset > 0 {
  18. builder.Write(" OFFSET ")
  19. builder.Write(strconv.Itoa(limit.Offset))
  20. }
  21. }
  22. }
  23. // MergeClause merge order by clauses
  24. func (limit Limit) MergeClause(clause *Clause) {
  25. clause.Name = ""
  26. if v, ok := clause.Expression.(Limit); ok {
  27. if limit.Limit == 0 && v.Limit > 0 {
  28. limit.Limit = v.Limit
  29. }
  30. if limit.Offset == 0 && v.Offset > 0 {
  31. limit.Offset = v.Offset
  32. }
  33. }
  34. clause.Expression = limit
  35. }