gitee.com/eden-framework/sqlx@v0.0.3/builder/addition.go (about)

     1  package builder
     2  
     3  import (
     4  	"sort"
     5  )
     6  
     7  type Additions []Addition
     8  
     9  type Addition interface {
    10  	SqlExpr
    11  	AdditionType() AdditionType
    12  }
    13  
    14  type AdditionType int
    15  
    16  const (
    17  	AdditionJoin AdditionType = iota
    18  	AdditionWhere
    19  	AdditionGroupBy
    20  	AdditionCombination
    21  	AdditionOrderBy
    22  	AdditionLimit
    23  	AdditionOnConflict
    24  	AdditionOther
    25  	AdditionComment
    26  )
    27  
    28  func WriteAdditions(e *Ex, additions ...Addition) {
    29  	finalAdditions := Additions{}
    30  	for i := range additions {
    31  		if IsNilExpr(additions[i]) {
    32  			continue
    33  		}
    34  		finalAdditions = append(finalAdditions, additions[i])
    35  	}
    36  
    37  	if len(finalAdditions) == 0 {
    38  		return
    39  	}
    40  
    41  	sort.Sort(finalAdditions)
    42  
    43  	for i := range finalAdditions {
    44  		e.WriteByte('\n')
    45  		e.WriteExpr(finalAdditions[i])
    46  	}
    47  }
    48  
    49  func (additions Additions) Len() int {
    50  	return len(additions)
    51  }
    52  
    53  func (additions Additions) Less(i, j int) bool {
    54  	return additions[i].AdditionType() < additions[j].AdditionType()
    55  }
    56  
    57  func (additions Additions) Swap(i, j int) {
    58  	additions[i], additions[j] = additions[j], additions[i]
    59  }
    60  
    61  func AsAddition(expr SqlExpr) *OtherAddition {
    62  	return &OtherAddition{
    63  		SqlExpr: expr,
    64  	}
    65  }
    66  
    67  type OtherAddition struct {
    68  	SqlExpr
    69  }
    70  
    71  func (OtherAddition) AdditionType() AdditionType {
    72  	return AdditionOther
    73  }
    74  
    75  func (a *OtherAddition) IsNil() bool {
    76  	return a == nil || IsNilExpr(a.SqlExpr)
    77  }