github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/sqlbuilder/def_column_collection.go (about)

     1  package sqlbuilder
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  )
     7  
     8  type ColumnCollectionManger interface {
     9  	AddCol(cols ...Column)
    10  }
    11  
    12  func ColumnCollectionFromList(list []Column) ColumnCollection {
    13  	return &columns{l: list}
    14  }
    15  
    16  type ColumnCollection interface {
    17  	SqlExpr
    18  
    19  	Of(t Table) ColumnCollection
    20  
    21  	F(name string) Column
    22  	Col(name string) Column
    23  	Cols(names ...string) ColumnCollection
    24  	RangeCol(cb func(col Column, idx int) bool)
    25  	Len() int
    26  }
    27  
    28  func Cols(names ...string) ColumnCollection {
    29  	cols := &columns{}
    30  	for _, name := range names {
    31  		cols.AddCol(Col(name))
    32  	}
    33  	return cols
    34  }
    35  
    36  type columns struct {
    37  	l []Column
    38  }
    39  
    40  func (cols *columns) IsNil() bool {
    41  	return cols == nil || cols.Len() == 0
    42  }
    43  
    44  func (cols *columns) F(name string) (col Column) {
    45  	for i := range cols.l {
    46  		c := cols.l[i]
    47  		if c.MatchName(name) {
    48  			return c
    49  		}
    50  	}
    51  	return nil
    52  }
    53  
    54  func (cols *columns) Col(name string) (col Column) {
    55  	for i := range cols.l {
    56  		c := cols.l[i]
    57  		if c.MatchName(name) {
    58  			return c
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  func (cols *columns) Ex(ctx context.Context) *Ex {
    65  	e := Expr("")
    66  	e.Grow(cols.Len())
    67  
    68  	cols.RangeCol(func(col Column, idx int) bool {
    69  		if idx > 0 {
    70  			e.WriteQueryByte(',')
    71  		}
    72  		e.WriteExpr(col)
    73  		return true
    74  	})
    75  
    76  	return e.Ex(ctx)
    77  }
    78  
    79  func (cols *columns) Len() int {
    80  	if cols == nil || cols.l == nil {
    81  		return 0
    82  	}
    83  	return len(cols.l)
    84  }
    85  
    86  func (cols *columns) RangeCol(cb func(col Column, idx int) bool) {
    87  	for i := range cols.l {
    88  		if !cb(cols.l[i], i) {
    89  			break
    90  		}
    91  	}
    92  }
    93  
    94  func (cols *columns) Cols(names ...string) ColumnCollection {
    95  	if len(names) == 0 {
    96  		return &columns{
    97  			l: cols.l,
    98  		}
    99  	}
   100  
   101  	newCols := &columns{}
   102  	for _, colName := range names {
   103  		col := cols.F(colName)
   104  		if col == nil {
   105  			panic(fmt.Errorf("unknown column %s, %v", colName, names))
   106  		}
   107  		newCols.AddCol(col)
   108  	}
   109  	return newCols
   110  }
   111  
   112  func (cols *columns) Of(newTable Table) ColumnCollection {
   113  	newCols := &columns{}
   114  	for i := range cols.l {
   115  		newCols.AddCol(cols.l[i].Of(newTable))
   116  	}
   117  	return newCols
   118  }
   119  
   120  func (cols *columns) AddCol(columns ...Column) {
   121  	for i := range columns {
   122  		col := columns[i]
   123  		if col == nil {
   124  			continue
   125  		}
   126  		cols.l = append(cols.l, col)
   127  	}
   128  }