gitee.com/h79/goutils@v1.22.10/dao/wrapper/select.go (about)

     1  package wrapper
     2  
     3  import (
     4  	commonoption "gitee.com/h79/goutils/common/option"
     5  	"gitee.com/h79/goutils/dao/option"
     6  	"strings"
     7  )
     8  
     9  var _ ISelect = (*Select)(nil)
    10  
    11  type Select struct {
    12  	query []string
    13  }
    14  
    15  func (sel *Select) Clone() *Select {
    16  	return &Select{query: sel.query}
    17  }
    18  
    19  func (sel *Select) Delete(column string) *Select {
    20  	for i := range sel.query {
    21  		if sel.query[i] == column {
    22  			sel.query = append(sel.query[:i], sel.query[i+1:]...)
    23  			break
    24  		}
    25  	}
    26  	return sel
    27  }
    28  
    29  func (sel *Select) Add(column string) *Select {
    30  	sel.query = append(sel.query, column)
    31  	return sel
    32  }
    33  
    34  func (sel *Select) Query() []string {
    35  	return sel.query
    36  }
    37  
    38  func (sel *Select) Is() bool {
    39  	if sel == nil {
    40  		return false
    41  	}
    42  	return len(sel.query) > 0
    43  }
    44  
    45  func (sel *Select) Value() []interface{} {
    46  	return nil
    47  }
    48  
    49  func (sel *Select) Build(opts ...commonoption.Option) string {
    50  	return sel.build(option.FullSqlExist(opts...))
    51  }
    52  
    53  func (sel *Select) build(full bool) string {
    54  	count := 0
    55  	builder := strings.Builder{}
    56  	for i := range sel.query {
    57  		if len(sel.query[i]) > 0 {
    58  			if count == 0 {
    59  				if full {
    60  					builder.WriteString("SELECT ")
    61  				}
    62  			} else if count > 0 {
    63  				builder.WriteByte(',')
    64  			}
    65  			builder.WriteString(sel.query[i])
    66  			count++
    67  		}
    68  	}
    69  	return builder.String()
    70  }