github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_addition.go (about)

     1  package builder
     2  
     3  import "sort"
     4  
     5  type Addition interface {
     6  	SqlExpr
     7  	AdditionKind() AdditionType
     8  }
     9  
    10  type AdditionType int
    11  
    12  const (
    13  	AdditionJoin AdditionType = iota
    14  	AdditionWhere
    15  	AdditionGroupBy
    16  	AdditionCombination
    17  	AdditionOrderBy
    18  	AdditionLimit
    19  	AdditionOnConflict
    20  	AdditionOther
    21  	AdditionComment
    22  )
    23  
    24  func (t AdditionType) AdditionKind() AdditionType { return t }
    25  
    26  var (
    27  	_ Addition = (*join)(nil)
    28  	_ Addition = (*where)(nil)
    29  	_ Addition = (*groupby)(nil)
    30  	_ Addition = (*orderby)(nil)
    31  	_ Addition = (*addition)(nil)
    32  	_ Addition = (*limit)(nil)
    33  	_ Addition = (*comment)(nil)
    34  )
    35  
    36  type Additions []Addition
    37  
    38  func (adds Additions) Len() int { return len(adds) }
    39  
    40  func (adds Additions) Less(i, j int) bool {
    41  	return adds[i].AdditionKind() < adds[j].AdditionKind()
    42  }
    43  
    44  func (adds Additions) Swap(i, j int) { adds[i], adds[j] = adds[j], adds[i] }
    45  
    46  func WriteAdditions(e *Ex, adds ...Addition) {
    47  	final := make(Additions, 0, len(adds))
    48  	for i := range adds {
    49  		if IsNilExpr(adds[i]) {
    50  			continue
    51  		}
    52  		final = append(final, adds[i])
    53  	}
    54  	if len(final) == 0 {
    55  		return
    56  	}
    57  	sort.Sort(final)
    58  	for i := range final {
    59  		e.WriteQueryByte('\n')
    60  		e.WriteExpr(final[i])
    61  	}
    62  }