limit.go 942 B

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