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

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  type Columns struct {
    11  	lst     []*Column
    12  	autoInc *Column
    13  }
    14  
    15  func Cols(names ...string) *Columns {
    16  	cols := &Columns{}
    17  	for _, name := range names {
    18  		cols.Add(Col(name))
    19  	}
    20  	return cols
    21  }
    22  
    23  func (c *Columns) IsNil() bool { return c == nil || c.Len() == 0 }
    24  
    25  func (c *Columns) Ex(ctx context.Context) *Ex {
    26  	e := Expr("")
    27  	e.Grow(c.Len())
    28  
    29  	c.Range(func(col *Column, idx int) {
    30  		if idx > 0 {
    31  			e.WriteQueryByte(',')
    32  		}
    33  		e.WriteExpr(col)
    34  	})
    35  	return e.Ex(ctx)
    36  }
    37  
    38  func (c *Columns) AutoIncrement() *Column { return c.autoInc }
    39  
    40  func (c *Columns) Len() int {
    41  	if c == nil || c.lst == nil {
    42  		return 0
    43  	}
    44  	return len(c.lst)
    45  }
    46  
    47  func (c *Columns) Clone() *Columns {
    48  	clone := &Columns{
    49  		lst: make([]*Column, len(c.lst)),
    50  	}
    51  	copy(clone.lst, c.lst)
    52  	return clone
    53  }
    54  
    55  func (c *Columns) Col(name string) *Column {
    56  	name = strings.ToLower(name)
    57  	for i := range c.lst {
    58  		if c.lst[i].Name == name {
    59  			return c.lst[i]
    60  		}
    61  	}
    62  	return nil
    63  }
    64  
    65  func (c *Columns) ColByFieldName(name string) *Column {
    66  	for i := range c.lst {
    67  		if c.lst[i].FieldName == name {
    68  			return c.lst[i]
    69  		}
    70  	}
    71  	return nil
    72  }
    73  
    74  func (c *Columns) Cols(names ...string) (*Columns, error) {
    75  	if len(names) == 0 {
    76  		return c.Clone(), nil
    77  	}
    78  	cols := &Columns{}
    79  	for _, name := range names {
    80  		col := c.Col(name)
    81  		if col == nil {
    82  			return nil, errors.Errorf("unknown struct column %s", name)
    83  		}
    84  		cols.Add(col)
    85  	}
    86  	return cols, nil
    87  }
    88  
    89  func (c *Columns) ColsByFieldNames(names ...string) (*Columns, error) {
    90  	if len(names) == 0 {
    91  		return c.Clone(), nil
    92  	}
    93  	cols := &Columns{lst: make([]*Column, 0, len(names))}
    94  	for _, name := range names {
    95  		col := c.ColByFieldName(name)
    96  		if col == nil {
    97  			return nil, errors.Errorf("unknonw struct field %s", name)
    98  		}
    99  		cols.lst = append(cols.lst, col)
   100  	}
   101  	return cols, nil
   102  }
   103  
   104  func (c *Columns) MustCols(names ...string) *Columns {
   105  	cols, err := c.Cols(names...)
   106  	if err != nil {
   107  		panic(err)
   108  	}
   109  	return cols
   110  }
   111  
   112  func (c *Columns) MustColsByFieldNames(names ...string) *Columns {
   113  	cols, err := c.ColsByFieldNames(names...)
   114  	if err != nil {
   115  		panic(err)
   116  	}
   117  	return cols
   118  }
   119  
   120  func (c *Columns) ColNames() []string {
   121  	names := make([]string, 0, c.Len())
   122  	c.Range(func(col *Column, idx int) {
   123  		if col.Name != "" {
   124  			names = append(names, col.Name)
   125  		}
   126  	})
   127  	return names
   128  }
   129  
   130  func (c *Columns) FieldNames() []string {
   131  	names := make([]string, 0, c.Len())
   132  	c.Range(func(col *Column, idx int) {
   133  		if col.FieldName != "" {
   134  			names = append(names, col.FieldName)
   135  		}
   136  	})
   137  	return names
   138  }
   139  
   140  func (c *Columns) Add(cols ...*Column) {
   141  	for i := range cols {
   142  		if cols[i] == nil {
   143  			continue
   144  		}
   145  		col := cols[i]
   146  		if col.ColumnType != nil && col.ColumnType.AutoIncrement {
   147  			if c.autoInc != nil {
   148  				panic("auto increment field can only have one")
   149  			}
   150  			c.autoInc = col
   151  		}
   152  		c.lst = append(c.lst, col)
   153  	}
   154  }
   155  
   156  func (c *Columns) Range(f func(*Column, int)) {
   157  	for i := range c.lst {
   158  		f(c.lst[i], i)
   159  	}
   160  }
   161  
   162  func (c *Columns) List() []*Column {
   163  	if c == nil {
   164  		return nil
   165  	}
   166  	return c.lst
   167  }