gitee.com/eden-framework/sqlx@v0.0.3/generator/__examples__/database/user__generated.go (about) 1 package database 2 3 import ( 4 fmt "fmt" 5 time "time" 6 7 github_com_go_courier_sqlx_v2 "gitee.com/eden-framework/sqlx" 8 github_com_go_courier_sqlx_v2_builder "gitee.com/eden-framework/sqlx/builder" 9 github_com_go_courier_sqlx_v2_datatypes "gitee.com/eden-framework/sqlx/datatypes" 10 ) 11 12 func (User) PrimaryKey() []string { 13 return []string{ 14 "ID", 15 } 16 } 17 18 func (User) Indexes() github_com_go_courier_sqlx_v2_builder.Indexes { 19 return github_com_go_courier_sqlx_v2_builder.Indexes{ 20 "I_geom/SPATIAL": []string{ 21 "Geom", 22 }, 23 "I_nickname/BTREE": []string{ 24 "Nickname", 25 }, 26 "I_username": []string{ 27 "Username", 28 }, 29 } 30 } 31 32 func (User) UniqueIndexIName() string { 33 return "I_name" 34 } 35 36 func (User) UniqueIndexes() github_com_go_courier_sqlx_v2_builder.Indexes { 37 return github_com_go_courier_sqlx_v2_builder.Indexes{ 38 "I_name": []string{ 39 "Name", 40 "DeletedAt", 41 }, 42 } 43 } 44 45 func (User) Comments() map[string]string { 46 return map[string]string{ 47 "Name": "姓名", 48 } 49 } 50 51 var UserTable *github_com_go_courier_sqlx_v2_builder.Table 52 53 func init() { 54 UserTable = DBTest.Register(&User{}) 55 } 56 57 type UserIterator struct { 58 } 59 60 func (UserIterator) New() interface{} { 61 return &User{} 62 } 63 64 func (UserIterator) Resolve(v interface{}) *User { 65 return v.(*User) 66 } 67 68 func (User) TableName() string { 69 return "t_user" 70 } 71 72 func (User) ColDescriptions() map[string][]string { 73 return map[string][]string{ 74 "Name": []string{ 75 "姓名", 76 }, 77 } 78 } 79 80 func (User) FieldKeyID() string { 81 return "ID" 82 } 83 84 func (m *User) FieldID() *github_com_go_courier_sqlx_v2_builder.Column { 85 return UserTable.F(m.FieldKeyID()) 86 } 87 88 func (User) FieldKeyName() string { 89 return "Name" 90 } 91 92 func (m *User) FieldName() *github_com_go_courier_sqlx_v2_builder.Column { 93 return UserTable.F(m.FieldKeyName()) 94 } 95 96 func (User) FieldKeyUsername() string { 97 return "Username" 98 } 99 100 func (m *User) FieldUsername() *github_com_go_courier_sqlx_v2_builder.Column { 101 return UserTable.F(m.FieldKeyUsername()) 102 } 103 104 func (User) FieldKeyNickname() string { 105 return "Nickname" 106 } 107 108 func (m *User) FieldNickname() *github_com_go_courier_sqlx_v2_builder.Column { 109 return UserTable.F(m.FieldKeyNickname()) 110 } 111 112 func (User) FieldKeyGender() string { 113 return "Gender" 114 } 115 116 func (m *User) FieldGender() *github_com_go_courier_sqlx_v2_builder.Column { 117 return UserTable.F(m.FieldKeyGender()) 118 } 119 120 func (User) FieldKeyBoolean() string { 121 return "Boolean" 122 } 123 124 func (m *User) FieldBoolean() *github_com_go_courier_sqlx_v2_builder.Column { 125 return UserTable.F(m.FieldKeyBoolean()) 126 } 127 128 func (User) FieldKeyGeom() string { 129 return "Geom" 130 } 131 132 func (m *User) FieldGeom() *github_com_go_courier_sqlx_v2_builder.Column { 133 return UserTable.F(m.FieldKeyGeom()) 134 } 135 136 func (User) FieldKeyCreatedAt() string { 137 return "CreatedAt" 138 } 139 140 func (m *User) FieldCreatedAt() *github_com_go_courier_sqlx_v2_builder.Column { 141 return UserTable.F(m.FieldKeyCreatedAt()) 142 } 143 144 func (User) FieldKeyUpdatedAt() string { 145 return "UpdatedAt" 146 } 147 148 func (m *User) FieldUpdatedAt() *github_com_go_courier_sqlx_v2_builder.Column { 149 return UserTable.F(m.FieldKeyUpdatedAt()) 150 } 151 152 func (User) FieldKeyDeletedAt() string { 153 return "DeletedAt" 154 } 155 156 func (m *User) FieldDeletedAt() *github_com_go_courier_sqlx_v2_builder.Column { 157 return UserTable.F(m.FieldKeyDeletedAt()) 158 } 159 160 func (User) ColRelations() map[string][]string { 161 return map[string][]string{} 162 } 163 164 func (m *User) IndexFieldNames() []string { 165 return []string{ 166 "Geom", 167 "ID", 168 "Name", 169 "Nickname", 170 "Username", 171 } 172 } 173 174 func (m *User) ConditionByStruct(db github_com_go_courier_sqlx_v2.DBExecutor) github_com_go_courier_sqlx_v2_builder.SqlCondition { 175 table := db.T(m) 176 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m) 177 178 conditions := make([]github_com_go_courier_sqlx_v2_builder.SqlCondition, 0) 179 180 for _, fieldName := range m.IndexFieldNames() { 181 if v, exists := fieldValues[fieldName]; exists { 182 conditions = append(conditions, table.F(fieldName).Eq(v)) 183 delete(fieldValues, fieldName) 184 } 185 } 186 187 if len(conditions) == 0 { 188 panic(fmt.Errorf("at least one of field for indexes has value")) 189 } 190 191 for fieldName, v := range fieldValues { 192 conditions = append(conditions, table.F(fieldName).Eq(v)) 193 } 194 195 condition := github_com_go_courier_sqlx_v2_builder.And(conditions...) 196 197 condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0)) 198 return condition 199 } 200 201 func (m *User) Create(db github_com_go_courier_sqlx_v2.DBExecutor) error { 202 203 if m.CreatedAt.IsZero() { 204 m.CreatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 205 } 206 207 if m.UpdatedAt.IsZero() { 208 m.UpdatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 209 } 210 211 _, err := db.ExecExpr(github_com_go_courier_sqlx_v2.InsertToDB(db, m, nil)) 212 return err 213 214 } 215 216 func (m *User) CreateOnDuplicateWithUpdateFields(db github_com_go_courier_sqlx_v2.DBExecutor, updateFields []string) error { 217 218 if len(updateFields) == 0 { 219 panic(fmt.Errorf("must have update fields")) 220 } 221 222 if m.CreatedAt.IsZero() { 223 m.CreatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 224 } 225 226 if m.UpdatedAt.IsZero() { 227 m.UpdatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 228 } 229 230 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, updateFields...) 231 232 delete(fieldValues, "ID") 233 234 table := db.T(m) 235 236 cols, vals := table.ColumnsAndValuesByFieldValues(fieldValues) 237 238 fields := make(map[string]bool, len(updateFields)) 239 for _, field := range updateFields { 240 fields[field] = true 241 } 242 243 for _, fieldNames := range m.UniqueIndexes() { 244 for _, field := range fieldNames { 245 delete(fields, field) 246 } 247 } 248 249 if len(fields) == 0 { 250 panic(fmt.Errorf("no fields for updates")) 251 } 252 253 for field := range fieldValues { 254 if !fields[field] { 255 delete(fieldValues, field) 256 } 257 } 258 259 additions := github_com_go_courier_sqlx_v2_builder.Additions{} 260 261 switch db.Dialect().DriverName() { 262 case "mysql": 263 additions = append(additions, github_com_go_courier_sqlx_v2_builder.OnDuplicateKeyUpdate(table.AssignmentsByFieldValues(fieldValues)...)) 264 case "postgres": 265 indexes := m.UniqueIndexes() 266 fields := make([]string, 0) 267 for _, fs := range indexes { 268 fields = append(fields, fs...) 269 } 270 indexFields, _ := db.T(m).Fields(fields...) 271 272 additions = append(additions, 273 github_com_go_courier_sqlx_v2_builder.OnConflict(indexFields). 274 DoUpdateSet(table.AssignmentsByFieldValues(fieldValues)...)) 275 } 276 277 additions = append(additions, github_com_go_courier_sqlx_v2_builder.Comment("User.CreateOnDuplicateWithUpdateFields")) 278 279 expr := github_com_go_courier_sqlx_v2_builder.Insert().Into(table, additions...).Values(cols, vals...) 280 281 _, err := db.ExecExpr(expr) 282 return err 283 284 } 285 286 func (m *User) DeleteByStruct(db github_com_go_courier_sqlx_v2.DBExecutor) error { 287 288 _, err := db.ExecExpr( 289 github_com_go_courier_sqlx_v2_builder.Delete(). 290 From( 291 db.T(m), 292 github_com_go_courier_sqlx_v2_builder.Where(m.ConditionByStruct(db)), 293 github_com_go_courier_sqlx_v2_builder.Comment("User.DeleteByStruct"), 294 ), 295 ) 296 297 return err 298 } 299 300 func (m *User) FetchByID(db github_com_go_courier_sqlx_v2.DBExecutor) error { 301 302 table := db.T(m) 303 304 err := db.QueryExprAndScan( 305 github_com_go_courier_sqlx_v2_builder.Select(nil). 306 From( 307 db.T(m), 308 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 309 table.F("ID").Eq(m.ID), 310 table.F("DeletedAt").Eq(m.DeletedAt), 311 )), 312 github_com_go_courier_sqlx_v2_builder.Comment("User.FetchByID"), 313 ), 314 m, 315 ) 316 317 return err 318 } 319 320 func (m *User) UpdateByIDWithMap(db github_com_go_courier_sqlx_v2.DBExecutor, fieldValues github_com_go_courier_sqlx_v2_builder.FieldValues) error { 321 322 if _, ok := fieldValues["UpdatedAt"]; !ok { 323 fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 324 } 325 326 table := db.T(m) 327 328 result, err := db.ExecExpr( 329 github_com_go_courier_sqlx_v2_builder.Update(db.T(m)). 330 Where( 331 github_com_go_courier_sqlx_v2_builder.And( 332 table.F("ID").Eq(m.ID), 333 table.F("DeletedAt").Eq(m.DeletedAt), 334 ), 335 github_com_go_courier_sqlx_v2_builder.Comment("User.UpdateByIDWithMap"), 336 ). 337 Set(table.AssignmentsByFieldValues(fieldValues)...), 338 ) 339 340 if err != nil { 341 return err 342 } 343 344 rowsAffected, _ := result.RowsAffected() 345 if rowsAffected == 0 { 346 return m.FetchByID(db) 347 } 348 349 return nil 350 351 } 352 353 func (m *User) UpdateByIDWithStruct(db github_com_go_courier_sqlx_v2.DBExecutor, zeroFields ...string) error { 354 355 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, zeroFields...) 356 return m.UpdateByIDWithMap(db, fieldValues) 357 358 } 359 360 func (m *User) FetchByIDForUpdate(db github_com_go_courier_sqlx_v2.DBExecutor) error { 361 362 table := db.T(m) 363 364 err := db.QueryExprAndScan( 365 github_com_go_courier_sqlx_v2_builder.Select(nil). 366 From( 367 db.T(m), 368 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 369 table.F("ID").Eq(m.ID), 370 table.F("DeletedAt").Eq(m.DeletedAt), 371 )), 372 github_com_go_courier_sqlx_v2_builder.ForUpdate(), 373 github_com_go_courier_sqlx_v2_builder.Comment("User.FetchByIDForUpdate"), 374 ), 375 m, 376 ) 377 378 return err 379 } 380 381 func (m *User) DeleteByID(db github_com_go_courier_sqlx_v2.DBExecutor) error { 382 383 table := db.T(m) 384 385 _, err := db.ExecExpr( 386 github_com_go_courier_sqlx_v2_builder.Delete(). 387 From(db.T(m), 388 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 389 table.F("ID").Eq(m.ID), 390 table.F("DeletedAt").Eq(m.DeletedAt), 391 )), 392 github_com_go_courier_sqlx_v2_builder.Comment("User.DeleteByID"), 393 )) 394 395 return err 396 } 397 398 func (m *User) SoftDeleteByID(db github_com_go_courier_sqlx_v2.DBExecutor) error { 399 400 table := db.T(m) 401 402 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValues{} 403 if _, ok := fieldValues["DeletedAt"]; !ok { 404 fieldValues["DeletedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 405 } 406 407 if _, ok := fieldValues["UpdatedAt"]; !ok { 408 fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 409 } 410 411 _, err := db.ExecExpr( 412 github_com_go_courier_sqlx_v2_builder.Update(db.T(m)). 413 Where( 414 github_com_go_courier_sqlx_v2_builder.And( 415 table.F("ID").Eq(m.ID), 416 table.F("DeletedAt").Eq(m.DeletedAt), 417 ), 418 github_com_go_courier_sqlx_v2_builder.Comment("User.SoftDeleteByID"), 419 ). 420 Set(table.AssignmentsByFieldValues(fieldValues)...), 421 ) 422 423 return err 424 425 } 426 427 func (m *User) FetchByName(db github_com_go_courier_sqlx_v2.DBExecutor) error { 428 429 table := db.T(m) 430 431 err := db.QueryExprAndScan( 432 github_com_go_courier_sqlx_v2_builder.Select(nil). 433 From( 434 db.T(m), 435 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 436 table.F("Name").Eq(m.Name), 437 table.F("DeletedAt").Eq(m.DeletedAt), 438 )), 439 github_com_go_courier_sqlx_v2_builder.Comment("User.FetchByName"), 440 ), 441 m, 442 ) 443 444 return err 445 } 446 447 func (m *User) UpdateByNameWithMap(db github_com_go_courier_sqlx_v2.DBExecutor, fieldValues github_com_go_courier_sqlx_v2_builder.FieldValues) error { 448 449 if _, ok := fieldValues["UpdatedAt"]; !ok { 450 fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 451 } 452 453 table := db.T(m) 454 455 result, err := db.ExecExpr( 456 github_com_go_courier_sqlx_v2_builder.Update(db.T(m)). 457 Where( 458 github_com_go_courier_sqlx_v2_builder.And( 459 table.F("Name").Eq(m.Name), 460 table.F("DeletedAt").Eq(m.DeletedAt), 461 ), 462 github_com_go_courier_sqlx_v2_builder.Comment("User.UpdateByNameWithMap"), 463 ). 464 Set(table.AssignmentsByFieldValues(fieldValues)...), 465 ) 466 467 if err != nil { 468 return err 469 } 470 471 rowsAffected, _ := result.RowsAffected() 472 if rowsAffected == 0 { 473 return m.FetchByName(db) 474 } 475 476 return nil 477 478 } 479 480 func (m *User) UpdateByNameWithStruct(db github_com_go_courier_sqlx_v2.DBExecutor, zeroFields ...string) error { 481 482 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, zeroFields...) 483 return m.UpdateByNameWithMap(db, fieldValues) 484 485 } 486 487 func (m *User) FetchByNameForUpdate(db github_com_go_courier_sqlx_v2.DBExecutor) error { 488 489 table := db.T(m) 490 491 err := db.QueryExprAndScan( 492 github_com_go_courier_sqlx_v2_builder.Select(nil). 493 From( 494 db.T(m), 495 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 496 table.F("Name").Eq(m.Name), 497 table.F("DeletedAt").Eq(m.DeletedAt), 498 )), 499 github_com_go_courier_sqlx_v2_builder.ForUpdate(), 500 github_com_go_courier_sqlx_v2_builder.Comment("User.FetchByNameForUpdate"), 501 ), 502 m, 503 ) 504 505 return err 506 } 507 508 func (m *User) DeleteByName(db github_com_go_courier_sqlx_v2.DBExecutor) error { 509 510 table := db.T(m) 511 512 _, err := db.ExecExpr( 513 github_com_go_courier_sqlx_v2_builder.Delete(). 514 From(db.T(m), 515 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 516 table.F("Name").Eq(m.Name), 517 table.F("DeletedAt").Eq(m.DeletedAt), 518 )), 519 github_com_go_courier_sqlx_v2_builder.Comment("User.DeleteByName"), 520 )) 521 522 return err 523 } 524 525 func (m *User) SoftDeleteByName(db github_com_go_courier_sqlx_v2.DBExecutor) error { 526 527 table := db.T(m) 528 529 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValues{} 530 if _, ok := fieldValues["DeletedAt"]; !ok { 531 fieldValues["DeletedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 532 } 533 534 if _, ok := fieldValues["UpdatedAt"]; !ok { 535 fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 536 } 537 538 _, err := db.ExecExpr( 539 github_com_go_courier_sqlx_v2_builder.Update(db.T(m)). 540 Where( 541 github_com_go_courier_sqlx_v2_builder.And( 542 table.F("Name").Eq(m.Name), 543 table.F("DeletedAt").Eq(m.DeletedAt), 544 ), 545 github_com_go_courier_sqlx_v2_builder.Comment("User.SoftDeleteByName"), 546 ). 547 Set(table.AssignmentsByFieldValues(fieldValues)...), 548 ) 549 550 return err 551 552 } 553 554 func (m *User) List(db github_com_go_courier_sqlx_v2.DBExecutor, condition github_com_go_courier_sqlx_v2_builder.SqlCondition, additions ...github_com_go_courier_sqlx_v2_builder.Addition) ([]User, error) { 555 556 list := make([]User, 0) 557 558 table := db.T(m) 559 _ = table 560 561 condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0)) 562 563 finalAdditions := []github_com_go_courier_sqlx_v2_builder.Addition{ 564 github_com_go_courier_sqlx_v2_builder.Where(condition), 565 github_com_go_courier_sqlx_v2_builder.Comment("User.List"), 566 } 567 568 if len(additions) > 0 { 569 finalAdditions = append(finalAdditions, additions...) 570 } 571 572 err := db.QueryExprAndScan( 573 github_com_go_courier_sqlx_v2_builder.Select(nil). 574 From(db.T(m), finalAdditions...), 575 &list, 576 ) 577 578 return list, err 579 580 } 581 582 func (m *User) Count(db github_com_go_courier_sqlx_v2.DBExecutor, condition github_com_go_courier_sqlx_v2_builder.SqlCondition, additions ...github_com_go_courier_sqlx_v2_builder.Addition) (int, error) { 583 584 count := -1 585 586 table := db.T(m) 587 _ = table 588 589 condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0)) 590 591 finalAdditions := []github_com_go_courier_sqlx_v2_builder.Addition{ 592 github_com_go_courier_sqlx_v2_builder.Where(condition), 593 github_com_go_courier_sqlx_v2_builder.Comment("User.Count"), 594 } 595 596 if len(additions) > 0 { 597 finalAdditions = append(finalAdditions, additions...) 598 } 599 600 err := db.QueryExprAndScan( 601 github_com_go_courier_sqlx_v2_builder.Select( 602 github_com_go_courier_sqlx_v2_builder.Count(), 603 ). 604 From(db.T(m), finalAdditions...), 605 &count, 606 ) 607 608 return count, err 609 610 } 611 612 func (m *User) BatchFetchByGeomList(db github_com_go_courier_sqlx_v2.DBExecutor, values []GeomString) ([]User, error) { 613 614 if len(values) == 0 { 615 return nil, nil 616 } 617 618 table := db.T(m) 619 620 condition := table.F("Geom").In(values) 621 622 return m.List(db, condition) 623 624 } 625 626 func (m *User) BatchFetchByIDList(db github_com_go_courier_sqlx_v2.DBExecutor, values []uint64) ([]User, error) { 627 628 if len(values) == 0 { 629 return nil, nil 630 } 631 632 table := db.T(m) 633 634 condition := table.F("ID").In(values) 635 636 return m.List(db, condition) 637 638 } 639 640 func (m *User) BatchFetchByNameList(db github_com_go_courier_sqlx_v2.DBExecutor, values []string) ([]User, error) { 641 642 if len(values) == 0 { 643 return nil, nil 644 } 645 646 table := db.T(m) 647 648 condition := table.F("Name").In(values) 649 650 return m.List(db, condition) 651 652 } 653 654 func (m *User) BatchFetchByNicknameList(db github_com_go_courier_sqlx_v2.DBExecutor, values []string) ([]User, error) { 655 656 if len(values) == 0 { 657 return nil, nil 658 } 659 660 table := db.T(m) 661 662 condition := table.F("Nickname").In(values) 663 664 return m.List(db, condition) 665 666 } 667 668 func (m *User) BatchFetchByUsernameList(db github_com_go_courier_sqlx_v2.DBExecutor, values []string) ([]User, error) { 669 670 if len(values) == 0 { 671 return nil, nil 672 } 673 674 table := db.T(m) 675 676 condition := table.F("Username").In(values) 677 678 return m.List(db, condition) 679 680 }