2020-02-02 09:40:44 +03:00
|
|
|
package clause
|
|
|
|
|
2020-02-07 18:45:35 +03:00
|
|
|
import "strconv"
|
|
|
|
|
2020-02-02 09:40:44 +03:00
|
|
|
// Limit limit clause
|
|
|
|
type Limit struct {
|
2022-10-07 15:14:14 +03:00
|
|
|
Limit *int
|
2020-02-07 18:45:35 +03:00
|
|
|
Offset int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name where clause name
|
|
|
|
func (limit Limit) Name() string {
|
|
|
|
return "LIMIT"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build build where clause
|
|
|
|
func (limit Limit) Build(builder Builder) {
|
2022-10-07 15:14:14 +03:00
|
|
|
if limit.Limit != nil && *limit.Limit >= 0 {
|
2020-03-09 12:07:00 +03:00
|
|
|
builder.WriteString("LIMIT ")
|
2022-10-07 15:14:14 +03:00
|
|
|
builder.WriteString(strconv.Itoa(*limit.Limit))
|
2020-07-02 13:14:33 +03:00
|
|
|
}
|
|
|
|
if limit.Offset > 0 {
|
2022-10-07 15:14:14 +03:00
|
|
|
if limit.Limit != nil && *limit.Limit >= 0 {
|
2022-03-17 17:54:30 +03:00
|
|
|
builder.WriteByte(' ')
|
2020-03-07 08:43:20 +03:00
|
|
|
}
|
2020-07-02 13:14:33 +03:00
|
|
|
builder.WriteString("OFFSET ")
|
|
|
|
builder.WriteString(strconv.Itoa(limit.Offset))
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeClause merge order by clauses
|
|
|
|
func (limit Limit) MergeClause(clause *Clause) {
|
|
|
|
clause.Name = ""
|
|
|
|
|
|
|
|
if v, ok := clause.Expression.(Limit); ok {
|
2023-04-11 05:25:47 +03:00
|
|
|
if (limit.Limit == nil || *limit.Limit == 0) && v.Limit != nil {
|
2020-02-07 18:45:35 +03:00
|
|
|
limit.Limit = v.Limit
|
|
|
|
}
|
|
|
|
|
|
|
|
if limit.Offset == 0 && v.Offset > 0 {
|
|
|
|
limit.Offset = v.Offset
|
2020-03-04 18:56:42 +03:00
|
|
|
} else if limit.Offset < 0 {
|
|
|
|
limit.Offset = 0
|
2020-02-07 18:45:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clause.Expression = limit
|
2020-02-02 09:40:44 +03:00
|
|
|
}
|