github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/models/strategy__generated.go (about) 1 // This is a generated source file. DO NOT EDIT 2 // Source: models/strategy__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 StrategyTable *builder.Table 16 17 func init() { 18 StrategyTable = DB.Register(&Strategy{}) 19 } 20 21 type StrategyIterator struct { 22 } 23 24 func (*StrategyIterator) New() interface{} { 25 return &Strategy{} 26 } 27 28 func (*StrategyIterator) Resolve(v interface{}) *Strategy { 29 return v.(*Strategy) 30 } 31 32 func (*Strategy) TableName() string { 33 return "t_strategy" 34 } 35 36 func (*Strategy) TableDesc() []string { 37 return []string{ 38 "Strategy event route strategy", 39 } 40 } 41 42 func (*Strategy) Comments() map[string]string { 43 return map[string]string{ 44 "AutoCollectMetric": "AutoCollectMetric if allow host collect event data for metering", 45 "EventType": "EventType user defined event type", 46 "Handler": "Handler wasm handler fn name", 47 } 48 } 49 50 func (*Strategy) ColDesc() map[string][]string { 51 return map[string][]string{ 52 "AutoCollectMetric": []string{ 53 "AutoCollectMetric if allow host collect event data for metering", 54 }, 55 "EventType": []string{ 56 "EventType user defined event type", 57 }, 58 "Handler": []string{ 59 "Handler wasm handler fn name", 60 }, 61 } 62 } 63 64 func (*Strategy) ColRel() map[string][]string { 65 return map[string][]string{} 66 } 67 68 func (*Strategy) PrimaryKey() []string { 69 return []string{ 70 "ID", 71 } 72 } 73 74 func (m *Strategy) IndexFieldNames() []string { 75 return []string{ 76 "AppletID", 77 "EventType", 78 "ID", 79 "ProjectID", 80 "StrategyID", 81 } 82 } 83 84 func (*Strategy) UniqueIndexes() builder.Indexes { 85 return builder.Indexes{ 86 "ui_prj_app_event": []string{ 87 "ProjectID", 88 "AppletID", 89 "EventType", 90 "DeletedAt", 91 }, 92 "ui_strategy_id": []string{ 93 "StrategyID", 94 "DeletedAt", 95 }, 96 } 97 } 98 99 func (*Strategy) UniqueIndexUIPrjAppEvent() string { 100 return "ui_prj_app_event" 101 } 102 103 func (*Strategy) UniqueIndexUIStrategyID() string { 104 return "ui_strategy_id" 105 } 106 107 func (m *Strategy) ColID() *builder.Column { 108 return StrategyTable.ColByFieldName(m.FieldID()) 109 } 110 111 func (*Strategy) FieldID() string { 112 return "ID" 113 } 114 115 func (m *Strategy) ColStrategyID() *builder.Column { 116 return StrategyTable.ColByFieldName(m.FieldStrategyID()) 117 } 118 119 func (*Strategy) FieldStrategyID() string { 120 return "StrategyID" 121 } 122 123 func (m *Strategy) ColProjectID() *builder.Column { 124 return StrategyTable.ColByFieldName(m.FieldProjectID()) 125 } 126 127 func (*Strategy) FieldProjectID() string { 128 return "ProjectID" 129 } 130 131 func (m *Strategy) ColAppletID() *builder.Column { 132 return StrategyTable.ColByFieldName(m.FieldAppletID()) 133 } 134 135 func (*Strategy) FieldAppletID() string { 136 return "AppletID" 137 } 138 139 func (m *Strategy) ColEventType() *builder.Column { 140 return StrategyTable.ColByFieldName(m.FieldEventType()) 141 } 142 143 func (*Strategy) FieldEventType() string { 144 return "EventType" 145 } 146 147 func (m *Strategy) ColHandler() *builder.Column { 148 return StrategyTable.ColByFieldName(m.FieldHandler()) 149 } 150 151 func (*Strategy) FieldHandler() string { 152 return "Handler" 153 } 154 155 func (m *Strategy) ColAutoCollectMetric() *builder.Column { 156 return StrategyTable.ColByFieldName(m.FieldAutoCollectMetric()) 157 } 158 159 func (*Strategy) FieldAutoCollectMetric() string { 160 return "AutoCollectMetric" 161 } 162 163 func (m *Strategy) ColCreatedAt() *builder.Column { 164 return StrategyTable.ColByFieldName(m.FieldCreatedAt()) 165 } 166 167 func (*Strategy) FieldCreatedAt() string { 168 return "CreatedAt" 169 } 170 171 func (m *Strategy) ColUpdatedAt() *builder.Column { 172 return StrategyTable.ColByFieldName(m.FieldUpdatedAt()) 173 } 174 175 func (*Strategy) FieldUpdatedAt() string { 176 return "UpdatedAt" 177 } 178 179 func (m *Strategy) ColDeletedAt() *builder.Column { 180 return StrategyTable.ColByFieldName(m.FieldDeletedAt()) 181 } 182 183 func (*Strategy) FieldDeletedAt() string { 184 return "DeletedAt" 185 } 186 187 func (m *Strategy) CondByValue(db sqlx.DBExecutor) builder.SqlCondition { 188 var ( 189 tbl = db.T(m) 190 fvs = builder.FieldValueFromStructByNoneZero(m) 191 cond = []builder.SqlCondition{tbl.ColByFieldName("DeletedAt").Eq(0)} 192 ) 193 194 for _, fn := range m.IndexFieldNames() { 195 if v, ok := fvs[fn]; ok { 196 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 197 delete(fvs, fn) 198 } 199 } 200 if len(cond) == 0 { 201 panic(fmt.Errorf("no field for indexes has value")) 202 } 203 for fn, v := range fvs { 204 cond = append(cond, tbl.ColByFieldName(fn).Eq(v)) 205 } 206 return builder.And(cond...) 207 } 208 209 func (m *Strategy) Create(db sqlx.DBExecutor) error { 210 211 if m.CreatedAt.IsZero() { 212 m.CreatedAt.Set(time.Now()) 213 } 214 215 if m.UpdatedAt.IsZero() { 216 m.UpdatedAt.Set(time.Now()) 217 } 218 219 _, err := db.Exec(sqlx.InsertToDB(db, m, nil)) 220 return err 221 } 222 223 func (m *Strategy) List(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) ([]Strategy, error) { 224 var ( 225 tbl = db.T(m) 226 lst = make([]Strategy, 0) 227 ) 228 cond = builder.And(tbl.ColByFieldName("DeletedAt").Eq(0), cond) 229 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Strategy.List")}, adds...) 230 err := db.QueryAndScan(builder.Select(nil).From(tbl, adds...), &lst) 231 return lst, err 232 } 233 234 func (m *Strategy) Count(db sqlx.DBExecutor, cond builder.SqlCondition, adds ...builder.Addition) (cnt int64, err error) { 235 tbl := db.T(m) 236 cond = builder.And(tbl.ColByFieldName("DeletedAt").Eq(0), cond) 237 adds = append([]builder.Addition{builder.Where(cond), builder.Comment("Strategy.List")}, adds...) 238 err = db.QueryAndScan(builder.Select(builder.Count()).From(tbl, adds...), &cnt) 239 return 240 } 241 242 func (m *Strategy) FetchByID(db sqlx.DBExecutor) error { 243 tbl := db.T(m) 244 err := db.QueryAndScan( 245 builder.Select(nil). 246 From( 247 tbl, 248 builder.Where( 249 builder.And( 250 tbl.ColByFieldName("ID").Eq(m.ID), 251 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 252 ), 253 ), 254 builder.Comment("Strategy.FetchByID"), 255 ), 256 m, 257 ) 258 return err 259 } 260 261 func (m *Strategy) FetchByProjectIDAndAppletIDAndEventType(db sqlx.DBExecutor) error { 262 tbl := db.T(m) 263 err := db.QueryAndScan( 264 builder.Select(nil). 265 From( 266 tbl, 267 builder.Where( 268 builder.And( 269 tbl.ColByFieldName("ProjectID").Eq(m.ProjectID), 270 tbl.ColByFieldName("AppletID").Eq(m.AppletID), 271 tbl.ColByFieldName("EventType").Eq(m.EventType), 272 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 273 ), 274 ), 275 builder.Comment("Strategy.FetchByProjectIDAndAppletIDAndEventType"), 276 ), 277 m, 278 ) 279 return err 280 } 281 282 func (m *Strategy) FetchByStrategyID(db sqlx.DBExecutor) error { 283 tbl := db.T(m) 284 err := db.QueryAndScan( 285 builder.Select(nil). 286 From( 287 tbl, 288 builder.Where( 289 builder.And( 290 tbl.ColByFieldName("StrategyID").Eq(m.StrategyID), 291 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 292 ), 293 ), 294 builder.Comment("Strategy.FetchByStrategyID"), 295 ), 296 m, 297 ) 298 return err 299 } 300 301 func (m *Strategy) UpdateByIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 302 303 if _, ok := fvs["UpdatedAt"]; !ok { 304 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 305 } 306 tbl := db.T(m) 307 res, err := db.Exec( 308 builder.Update(tbl). 309 Where( 310 builder.And( 311 tbl.ColByFieldName("ID").Eq(m.ID), 312 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 313 ), 314 builder.Comment("Strategy.UpdateByIDWithFVs"), 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.FetchByID(db) 323 } 324 return nil 325 } 326 327 func (m *Strategy) UpdateByID(db sqlx.DBExecutor, zeros ...string) error { 328 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 329 return m.UpdateByIDWithFVs(db, fvs) 330 } 331 332 func (m *Strategy) UpdateByProjectIDAndAppletIDAndEventTypeWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 333 334 if _, ok := fvs["UpdatedAt"]; !ok { 335 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 336 } 337 tbl := db.T(m) 338 res, err := db.Exec( 339 builder.Update(tbl). 340 Where( 341 builder.And( 342 tbl.ColByFieldName("ProjectID").Eq(m.ProjectID), 343 tbl.ColByFieldName("AppletID").Eq(m.AppletID), 344 tbl.ColByFieldName("EventType").Eq(m.EventType), 345 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 346 ), 347 builder.Comment("Strategy.UpdateByProjectIDAndAppletIDAndEventTypeWithFVs"), 348 ). 349 Set(tbl.AssignmentsByFieldValues(fvs)...), 350 ) 351 if err != nil { 352 return err 353 } 354 if affected, _ := res.RowsAffected(); affected == 0 { 355 return m.FetchByProjectIDAndAppletIDAndEventType(db) 356 } 357 return nil 358 } 359 360 func (m *Strategy) UpdateByProjectIDAndAppletIDAndEventType(db sqlx.DBExecutor, zeros ...string) error { 361 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 362 return m.UpdateByProjectIDAndAppletIDAndEventTypeWithFVs(db, fvs) 363 } 364 365 func (m *Strategy) UpdateByStrategyIDWithFVs(db sqlx.DBExecutor, fvs builder.FieldValues) error { 366 367 if _, ok := fvs["UpdatedAt"]; !ok { 368 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 369 } 370 tbl := db.T(m) 371 res, err := db.Exec( 372 builder.Update(tbl). 373 Where( 374 builder.And( 375 tbl.ColByFieldName("StrategyID").Eq(m.StrategyID), 376 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 377 ), 378 builder.Comment("Strategy.UpdateByStrategyIDWithFVs"), 379 ). 380 Set(tbl.AssignmentsByFieldValues(fvs)...), 381 ) 382 if err != nil { 383 return err 384 } 385 if affected, _ := res.RowsAffected(); affected == 0 { 386 return m.FetchByStrategyID(db) 387 } 388 return nil 389 } 390 391 func (m *Strategy) UpdateByStrategyID(db sqlx.DBExecutor, zeros ...string) error { 392 fvs := builder.FieldValueFromStructByNoneZero(m, zeros...) 393 return m.UpdateByStrategyIDWithFVs(db, fvs) 394 } 395 396 func (m *Strategy) Delete(db sqlx.DBExecutor) error { 397 _, err := db.Exec( 398 builder.Delete(). 399 From( 400 db.T(m), 401 builder.Where(m.CondByValue(db)), 402 builder.Comment("Strategy.Delete"), 403 ), 404 ) 405 return err 406 } 407 408 func (m *Strategy) DeleteByID(db sqlx.DBExecutor) error { 409 tbl := db.T(m) 410 _, err := db.Exec( 411 builder.Delete(). 412 From( 413 tbl, 414 builder.Where( 415 builder.And( 416 tbl.ColByFieldName("ID").Eq(m.ID), 417 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 418 ), 419 ), 420 builder.Comment("Strategy.DeleteByID"), 421 ), 422 ) 423 return err 424 } 425 426 func (m *Strategy) SoftDeleteByID(db sqlx.DBExecutor) error { 427 tbl := db.T(m) 428 fvs := builder.FieldValues{} 429 430 if _, ok := fvs["DeletedAt"]; !ok { 431 fvs["DeletedAt"] = types.Timestamp{Time: time.Now()} 432 } 433 434 if _, ok := fvs["UpdatedAt"]; !ok { 435 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 436 } 437 _, err := db.Exec( 438 builder.Update(db.T(m)). 439 Where( 440 builder.And( 441 tbl.ColByFieldName("ID").Eq(m.ID), 442 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 443 ), 444 builder.Comment("Strategy.SoftDeleteByID"), 445 ). 446 Set(tbl.AssignmentsByFieldValues(fvs)...), 447 ) 448 return err 449 } 450 451 func (m *Strategy) DeleteByProjectIDAndAppletIDAndEventType(db sqlx.DBExecutor) error { 452 tbl := db.T(m) 453 _, err := db.Exec( 454 builder.Delete(). 455 From( 456 tbl, 457 builder.Where( 458 builder.And( 459 tbl.ColByFieldName("ProjectID").Eq(m.ProjectID), 460 tbl.ColByFieldName("AppletID").Eq(m.AppletID), 461 tbl.ColByFieldName("EventType").Eq(m.EventType), 462 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 463 ), 464 ), 465 builder.Comment("Strategy.DeleteByProjectIDAndAppletIDAndEventType"), 466 ), 467 ) 468 return err 469 } 470 471 func (m *Strategy) SoftDeleteByProjectIDAndAppletIDAndEventType(db sqlx.DBExecutor) error { 472 tbl := db.T(m) 473 fvs := builder.FieldValues{} 474 475 if _, ok := fvs["DeletedAt"]; !ok { 476 fvs["DeletedAt"] = types.Timestamp{Time: time.Now()} 477 } 478 479 if _, ok := fvs["UpdatedAt"]; !ok { 480 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 481 } 482 _, err := db.Exec( 483 builder.Update(db.T(m)). 484 Where( 485 builder.And( 486 tbl.ColByFieldName("ProjectID").Eq(m.ProjectID), 487 tbl.ColByFieldName("AppletID").Eq(m.AppletID), 488 tbl.ColByFieldName("EventType").Eq(m.EventType), 489 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 490 ), 491 builder.Comment("Strategy.SoftDeleteByProjectIDAndAppletIDAndEventType"), 492 ). 493 Set(tbl.AssignmentsByFieldValues(fvs)...), 494 ) 495 return err 496 } 497 498 func (m *Strategy) DeleteByStrategyID(db sqlx.DBExecutor) error { 499 tbl := db.T(m) 500 _, err := db.Exec( 501 builder.Delete(). 502 From( 503 tbl, 504 builder.Where( 505 builder.And( 506 tbl.ColByFieldName("StrategyID").Eq(m.StrategyID), 507 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 508 ), 509 ), 510 builder.Comment("Strategy.DeleteByStrategyID"), 511 ), 512 ) 513 return err 514 } 515 516 func (m *Strategy) SoftDeleteByStrategyID(db sqlx.DBExecutor) error { 517 tbl := db.T(m) 518 fvs := builder.FieldValues{} 519 520 if _, ok := fvs["DeletedAt"]; !ok { 521 fvs["DeletedAt"] = types.Timestamp{Time: time.Now()} 522 } 523 524 if _, ok := fvs["UpdatedAt"]; !ok { 525 fvs["UpdatedAt"] = types.Timestamp{Time: time.Now()} 526 } 527 _, err := db.Exec( 528 builder.Update(db.T(m)). 529 Where( 530 builder.And( 531 tbl.ColByFieldName("StrategyID").Eq(m.StrategyID), 532 tbl.ColByFieldName("DeletedAt").Eq(m.DeletedAt), 533 ), 534 builder.Comment("Strategy.SoftDeleteByStrategyID"), 535 ). 536 Set(tbl.AssignmentsByFieldValues(fvs)...), 537 ) 538 return err 539 }