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