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