github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/models/applet__generated.go (about) 1 // This is a generated source file. DO NOT EDIT 2 // Source: models/applet__generated.go 3 4 package models 5 6 import ( 7 "fmt" 8 "time" 9 10 "github.com/machinefi/w3bstream/pkg/depends/base/types" 11 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx" 12 "github.com/machinefi/w3bstream/pkg/depends/kit/sqlx/builder" 13 ) 14 15 var AppletTable *builder.Table 16 17 func init() { 18 AppletTable = DB.Register(&Applet{}) 19 } 20 21 type AppletIterator struct { 22 } 23 24 func (*AppletIterator) New() interface{} { 25 return &Applet{} 26 } 27 28 func (*AppletIterator) Resolve(v interface{}) *Applet { 29 return v.(*Applet) 30 } 31 32 func (*Applet) TableName() string { 33 return "t_applet" 34 } 35 36 func (*Applet) TableDesc() []string { 37 return []string{ 38 "Applet database model applet", 39 } 40 } 41 42 func (*Applet) Comments() map[string]string { 43 return map[string]string{} 44 } 45 46 func (*Applet) ColDesc() map[string][]string { 47 return map[string][]string{} 48 } 49 50 func (*Applet) ColRel() map[string][]string { 51 return map[string][]string{} 52 } 53 54 func (*Applet) PrimaryKey() []string { 55 return []string{ 56 "ID", 57 } 58 } 59 60 func (m *Applet) IndexFieldNames() []string { 61 return []string{ 62 "AppletID", 63 "ID", 64 "Name", 65 "ProjectID", 66 } 67 } 68 69 func (*Applet) UniqueIndexes() builder.Indexes { 70 return builder.Indexes{ 71 "ui_applet_id": []string{ 72 "AppletID", 73 }, 74 "ui_project_name": []string{ 75 "ProjectID", 76 "Name", 77 }, 78 } 79 } 80 81 func (*Applet) UniqueIndexUIAppletID() string { 82 return "ui_applet_id" 83 } 84 85 func (*Applet) UniqueIndexUIProjectName() string { 86 return "ui_project_name" 87 } 88 89 func (m *Applet) ColID() *builder.Column { 90 return AppletTable.ColByFieldName(m.FieldID()) 91 } 92 93 func (*Applet) FieldID() string { 94 return "ID" 95 } 96 97 func (m *Applet) ColProjectID() *builder.Column { 98 return AppletTable.ColByFieldName(m.FieldProjectID()) 99 } 100 101 func (*Applet) FieldProjectID() string { 102 return "ProjectID" 103 } 104 105 func (m *Applet) ColAppletID() *builder.Column { 106 return AppletTable.ColByFieldName(m.FieldAppletID()) 107 } 108 109 func (*Applet) FieldAppletID() string { 110 return "AppletID" 111 } 112 113 func (m *Applet) ColResourceID() *builder.Column { 114 return AppletTable.ColByFieldName(m.FieldResourceID()) 115 } 116 117 func (*Applet) FieldResourceID() string { 118 return "ResourceID" 119 } 120 121 func (m *Applet) ColName() *builder.Column { 122 return AppletTable.ColByFieldName(m.FieldName()) 123 } 124 125 func (*Applet) FieldName() string { 126 return "Name" 127 } 128 129 func (m *Applet) ColCreatedAt() *builder.Column { 130 return AppletTable.ColByFieldName(m.FieldCreatedAt()) 131 } 132 133 func (*Applet) FieldCreatedAt() string { 134 return "CreatedAt" 135 } 136 137 func (m *Applet) ColUpdatedAt() *builder.Column { 138 return AppletTable.ColByFieldName(m.FieldUpdatedAt()) 139 } 140 141 func (*Applet) FieldUpdatedAt() string { 142 return "UpdatedAt" 143 } 144 145 func (m *Applet) CondByValue(db sqlx.DBExecutor) builder.SqlCondition { 146 var ( 147 tbl = db.T(m) 148 fvs = builder.FieldValueFromStructByNoneZero(m) 149 cond = make([]builder.SqlCondition, 0) 150 ) 151 152 for _, fn := range m.IndexFieldNames() { 153 if v, ok := fvs[fn]; ok { 154 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 155 delete(fvs, fn) 156 } 157 } 158 if len(cond) == 0 { 159 panic(fmt.Errorf("no field for indexes has value")) 160 } 161 for fn, v := range fvs { 162 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 163 } 164 return builder.And(cond...) 165 } 166 167 func (m *Applet) Create(db sqlx.DBExecutor) error { 168 169 if m.CreatedAt.IsZero() { 170 m.CreatedAt.Set(time.Now()) 171 } 172 173 if m.UpdatedAt.IsZero() { 174 m.UpdatedAt.Set(time.Now()) 175 } 176 177 _, err := db.Exec(sqlx.InsertToDB(db, m, nil)) 178 return err 179 } 180 181 func (m *Applet) List(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) ([]Applet, error) { 182 var ( 183 tbl = db.T(m) 184 lst = make([]Applet, 0) 185 ) 186 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Applet.List")}, adds...) 187 err := db.QueryAndScan(builder.Select(nil).From(tbl, adds...), &lst) 188 return lst, err 189 } 190 191 func (m *Applet) Count(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) (cnt int64, err error) { 192 tbl := db.T(m) 193 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Applet.List")}, adds...) 194 err = db.QueryAndScan(builder.Select(builder.Count()).From(tbl, adds...), &cnt) 195 return 196 } 197 198 func (m *Applet) FetchByID(db sqlx.DBExecutor) error { 199 tbl := db.T(m) 200 err := db.QueryAndScan( 201 builder.Select(nil). 202 From( 203 tbl, 204 builder.Where( 205 builder.And( 206 tbl.ColByFieldName("ID").Eq(m.ID), 207 ), 208 ), 209 builder.Comment("Applet.FetchByID"), 210 ), 211 m, 212 ) 213 return err 214 } 215 216 func (m *Applet) FetchByAppletID(db sqlx.DBExecutor) error { 217 tbl := db.T(m) 218 err := db.QueryAndScan( 219 builder.Select(nil). 220 From( 221 tbl, 222 builder.Where( 223 builder.And( 224 tbl.ColByFieldName("AppletID").Eq(m.AppletID), 225 ), 226 ), 227 builder.Comment("Applet.FetchByAppletID"), 228 ), 229 m, 230 ) 231 return err 232 } 233 234 func (m *Applet) FetchByProjectIDAndName(db sqlx.DBExecutor) error { 235 tbl := db.T(m) 236 err := db.QueryAndScan( 237 builder.Select(nil). 238 From( 239 tbl, 240 builder.Where( 241 builder.And( 242 tbl.ColByFieldName("ProjectID").Eq(m.ProjectID), 243 tbl.ColByFieldName("Name").Eq(m.Name), 244 ), 245 ), 246 builder.Comment("Applet.FetchByProjectIDAndName"), 247 ), 248 m, 249 ) 250 return err 251 } 252 253 func (m *Applet) UpdateByIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 254 255 if _, ok := fvs["UpdatedAt"]; !ok { 256 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 257 } 258 tbl := db.T(m) 259 res, err := db.Exec( 260 builder.Update(tbl). 261 Where( 262 builder.And( 263 tbl.ColByFieldName("ID").Eq(m.ID), 264 ), 265 builder.Comment("Applet.UpdateByIDWithFVs"), 266 ). 267 Set(tbl.AssignmentsByFieldValues(fvs)...), 268 ) 269 if err != nil { 270 return err 271 } 272 if affected, _ := res.RowsAffected(); affected == 0 { 273 return m.FetchByID(db) 274 } 275 return nil 276 } 277 278 func (m *Applet) UpdateByID(db sqlx.DBExecutor, zeros ...string) error { 279 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 280 return m.UpdateByIDWithFVs(db, fvs) 281 } 282 283 func (m *Applet) UpdateByAppletIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 284 285 if _, ok := fvs["UpdatedAt"]; !ok { 286 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 287 } 288 tbl := db.T(m) 289 res, err := db.Exec( 290 builder.Update(tbl). 291 Where( 292 builder.And( 293 tbl.ColByFieldName("AppletID").Eq(m.AppletID), 294 ), 295 builder.Comment("Applet.UpdateByAppletIDWithFVs"), 296 ). 297 Set(tbl.AssignmentsByFieldValues(fvs)...), 298 ) 299 if err != nil { 300 return err 301 } 302 if affected, _ := res.RowsAffected(); affected == 0 { 303 return m.FetchByAppletID(db) 304 } 305 return nil 306 } 307 308 func (m *Applet) UpdateByAppletID(db sqlx.DBExecutor, zeros ...string) error { 309 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 310 return m.UpdateByAppletIDWithFVs(db, fvs) 311 } 312 313 func (m *Applet) UpdateByProjectIDAndNameWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 314 315 if _, ok := fvs["UpdatedAt"]; !ok { 316 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 317 } 318 tbl := db.T(m) 319 res, err := db.Exec( 320 builder.Update(tbl). 321 Where( 322 builder.And( 323 tbl.ColByFieldName("ProjectID").Eq(m.ProjectID), 324 tbl.ColByFieldName("Name").Eq(m.Name), 325 ), 326 builder.Comment("Applet.UpdateByProjectIDAndNameWithFVs"), 327 ). 328 Set(tbl.AssignmentsByFieldValues(fvs)...), 329 ) 330 if err != nil { 331 return err 332 } 333 if affected, _ := res.RowsAffected(); affected == 0 { 334 return m.FetchByProjectIDAndName(db) 335 } 336 return nil 337 } 338 339 func (m *Applet) UpdateByProjectIDAndName(db sqlx.DBExecutor, zeros ...string) error { 340 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 341 return m.UpdateByProjectIDAndNameWithFVs(db, fvs) 342 } 343 344 func (m *Applet) Delete(db sqlx.DBExecutor) error { 345 _, err := db.Exec( 346 builder.Delete(). 347 From( 348 db.T(m), 349 builder.Where(m.CondByValue(db)), 350 builder.Comment("Applet.Delete"), 351 ), 352 ) 353 return err 354 } 355 356 func (m *Applet) DeleteByID(db sqlx.DBExecutor) error { 357 tbl := db.T(m) 358 _, err := db.Exec( 359 builder.Delete(). 360 From( 361 tbl, 362 builder.Where( 363 builder.And( 364 tbl.ColByFieldName("ID").Eq(m.ID), 365 ), 366 ), 367 builder.Comment("Applet.DeleteByID"), 368 ), 369 ) 370 return err 371 } 372 373 func (m *Applet) DeleteByAppletID(db sqlx.DBExecutor) error { 374 tbl := db.T(m) 375 _, err := db.Exec( 376 builder.Delete(). 377 From( 378 tbl, 379 builder.Where( 380 builder.And( 381 tbl.ColByFieldName("AppletID").Eq(m.AppletID), 382 ), 383 ), 384 builder.Comment("Applet.DeleteByAppletID"), 385 ), 386 ) 387 return err 388 } 389 390 func (m *Applet) DeleteByProjectIDAndName(db sqlx.DBExecutor) error { 391 tbl := db.T(m) 392 _, err := db.Exec( 393 builder.Delete(). 394 From( 395 tbl, 396 builder.Where( 397 builder.And( 398 tbl.ColByFieldName("ProjectID").Eq(m.ProjectID), 399 tbl.ColByFieldName("Name").Eq(m.Name), 400 ), 401 ), 402 builder.Comment("Applet.DeleteByProjectIDAndName"), 403 ), 404 ) 405 return err 406 }