github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/modelgen/__examples__/org__generated.go (about) 1 // This is a generated source file. DO NOT EDIT 2 // Source: example/org__generated.go 3 4 package example 5 6 import ( 7 "fmt" 8 9 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx" 10 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 11 ) 12 13 var OrgTable *builder.Table 14 15 func init() { 16 OrgTable = DB.Register(&Org{}) 17 } 18 19 type OrgIterator struct { 20 } 21 22 func (*OrgIterator) New() interface{} { 23 return &Org{} 24 } 25 26 func (*OrgIterator) Resolve(v interface{}) *Org { 27 return v.(*Org) 28 } 29 30 func (*Org) TableName() string { 31 return "t_org" 32 } 33 34 func (*Org) TableDesc() []string { 35 return []string{ 36 "Org describe organization information", 37 } 38 } 39 40 func (*Org) Comments() map[string]string { 41 return map[string]string{ 42 "UserID": "User relation...", 43 } 44 } 45 46 func (*Org) ColDesc() map[string][]string { 47 return map[string][]string{ 48 "UserID": []string{ 49 "User relation...", 50 }, 51 } 52 } 53 54 func (*Org) ColRel() map[string][]string { 55 return map[string][]string{ 56 "UserID": []string{ 57 "User", 58 "ID", 59 }, 60 } 61 } 62 63 func (*Org) PrimaryKey() []string { 64 return []string{ 65 "ID", 66 } 67 } 68 69 func (m *Org) IndexFieldNames() []string { 70 return []string{ 71 "ID", 72 } 73 } 74 75 func (m *Org) ColID() *builder.Column { 76 return OrgTable.ColByFieldName(m.FieldID()) 77 } 78 79 func (*Org) FieldID() string { 80 return "ID" 81 } 82 83 func (m *Org) ColName() *builder.Column { 84 return OrgTable.ColByFieldName(m.FieldName()) 85 } 86 87 func (*Org) FieldName() string { 88 return "Name" 89 } 90 91 func (m *Org) ColUserID() *builder.Column { 92 return OrgTable.ColByFieldName(m.FieldUserID()) 93 } 94 95 func (*Org) FieldUserID() string { 96 return "UserID" 97 } 98 99 func (m *Org) CondByValue(db sqlx.DBExecutor) builder.SqlCondition { 100 var ( 101 tbl = db.T(m) 102 fvs = builder.FieldValueFromStructByNoneZero(m) 103 cond = make([]builder.SqlCondition, 0) 104 ) 105 106 for _, fn := range m.IndexFieldNames() { 107 if v, ok := fvs[fn]; ok { 108 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 109 delete(fvs, fn) 110 } 111 } 112 if len(cond) == 0 { 113 panic(fmt.Errorf("no field for indexes has value")) 114 } 115 for fn, v := range fvs { 116 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 117 } 118 return builder.And(cond...) 119 } 120 121 func (m *Org) Create(db sqlx.DBExecutor) error { 122 123 _, err := db.Exec(sqlx.InsertToDB(db, m, nil)) 124 return err 125 } 126 127 func (m *Org) List(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) ([]Org, error) { 128 var ( 129 tbl = db.T(m) 130 lst = make([]Org, 0) 131 ) 132 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Org.List")}, adds...) 133 err := db.QueryAndScan(builder.Select(nil).From(tbl, adds...), &lst) 134 return lst, err 135 } 136 137 func (m *Org) Count(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) (cnt int64, err error) { 138 tbl := db.T(m) 139 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Org.List")}, adds...) 140 err = db.QueryAndScan(builder.Select(builder.Count()).From(tbl, adds...), &cnt) 141 return 142 } 143 144 func (m *Org) FetchByID(db sqlx.DBExecutor) error { 145 tbl := db.T(m) 146 err := db.QueryAndScan( 147 builder.Select(nil). 148 From( 149 tbl, 150 builder.Where( 151 builder.And( 152 tbl.ColByFieldName("ID").Eq(m.ID), 153 ), 154 ), 155 builder.Comment("Org.FetchByID"), 156 ), 157 m, 158 ) 159 return err 160 } 161 162 func (m *Org) UpdateByIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 163 tbl := db.T(m) 164 res, err := db.Exec( 165 builder.Update(tbl). 166 Where( 167 builder.And( 168 tbl.ColByFieldName("ID").Eq(m.ID), 169 ), 170 builder.Comment("Org.UpdateByIDWithFVs"), 171 ). 172 Set(tbl.AssignmentsByFieldValues(fvs)...), 173 ) 174 if err != nil { 175 return err 176 } 177 if affected, _ := res.RowsAffected(); affected == 0 { 178 return m.FetchByID(db) 179 } 180 return nil 181 } 182 183 func (m *Org) UpdateByID(db sqlx.DBExecutor, zeros ...string) error { 184 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 185 return m.UpdateByIDWithFVs(db, fvs) 186 } 187 188 func (m *Org) Delete(db sqlx.DBExecutor) error { 189 _, err := db.Exec( 190 builder.Delete(). 191 From( 192 db.T(m), 193 builder.Where(m.CondByValue(db)), 194 builder.Comment("Org.Delete"), 195 ), 196 ) 197 return err 198 } 199 200 func (m *Org) DeleteByID(db sqlx.DBExecutor) error { 201 tbl := db.T(m) 202 _, err := db.Exec( 203 builder.Delete(). 204 From( 205 tbl, 206 builder.Where( 207 builder.And( 208 tbl.ColByFieldName("ID").Eq(m.ID), 209 ), 210 ), 211 builder.Comment("Org.DeleteByID"), 212 ), 213 ) 214 return err 215 }