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