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