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