github.com/kunlun-qilian/sqlx/v3@v3.0.0/builder/addition_limit.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  )
     7  
     8  type LimitAddition struct {
     9  }
    10  
    11  func (LimitAddition) AdditionType() AdditionType {
    12  	return AdditionLimit
    13  }
    14  
    15  func Limit(rowCount int64) *limit {
    16  	return &limit{rowCount: rowCount}
    17  }
    18  
    19  var _ Addition = (*limit)(nil)
    20  
    21  type limit struct {
    22  	LimitAddition
    23  
    24  	// LIMIT
    25  	rowCount int64
    26  	// OFFSET
    27  	offsetCount int64
    28  }
    29  
    30  func (l limit) Offset(offset int64) *limit {
    31  	l.offsetCount = offset
    32  	return &l
    33  }
    34  
    35  func (l *limit) IsNil() bool {
    36  	return l == nil || l.rowCount <= 0
    37  }
    38  
    39  func (l *limit) Ex(ctx context.Context) *Ex {
    40  	e := ExactlyExpr("LIMIT ")
    41  
    42  	e.WriteQuery(strconv.FormatInt(l.rowCount, 10))
    43  
    44  	if l.offsetCount > 0 {
    45  		e.WriteQuery(" OFFSET ")
    46  		e.WriteQuery(strconv.FormatInt(l.offsetCount, 10))
    47  	}
    48  
    49  	return e.Ex(ctx)
    50  }