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