github.com/kunlun-qilian/sqlx/v3@v3.0.0/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 "github.com/kunlun-qilian/sqlx/v3" 8 github_com_go_courier_sqlx_v2_builder "github.com/kunlun-qilian/sqlx/v3/builder" 9 github_com_go_courier_sqlx_v2_datatypes "github.com/kunlun-qilian/sqlx/v3/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 "ID", 167 "Name", 168 "Nickname", 169 "Username", 170 } 171 } 172 173 func (m *User) ConditionByStruct(db github_com_go_courier_sqlx_v2.DBExecutor) github_com_go_courier_sqlx_v2_builder.SqlCondition { 174 table := db.T(m) 175 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m) 176 177 conditions := make([]github_com_go_courier_sqlx_v2_builder.SqlCondition, 0) 178 179 for _, fieldName := range m.IndexFieldNames() { 180 if v, exists := fieldValues[fieldName]; exists { 181 conditions = append(conditions, table.F(fieldName).Eq(v)) 182 delete(fieldValues, fieldName) 183 } 184 } 185 186 if len(conditions) == 0 { 187 panic(fmt.Errorf("at least one of field for indexes has value")) 188 } 189 190 for fieldName, v := range fieldValues { 191 conditions = append(conditions, table.F(fieldName).Eq(v)) 192 } 193 194 condition := github_com_go_courier_sqlx_v2_builder.And(conditions...) 195 196 condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0)) 197 return condition 198 } 199 200 func (m *User) Create(db github_com_go_courier_sqlx_v2.DBExecutor) error { 201 202 if m.CreatedAt.IsZero() { 203 m.CreatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 204 } 205 206 if m.UpdatedAt.IsZero() { 207 m.UpdatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 208 } 209 210 _, err := db.ExecExpr(github_com_go_courier_sqlx_v2.InsertToDB(db, m, nil)) 211 return err 212 213 } 214 215 func (m *User) CreateOnDuplicateWithUpdateFields(db github_com_go_courier_sqlx_v2.DBExecutor, updateFields []string) error { 216 217 if len(updateFields) == 0 { 218 panic(fmt.Errorf("must have update fields")) 219 } 220 221 if m.CreatedAt.IsZero() { 222 m.CreatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 223 } 224 225 if m.UpdatedAt.IsZero() { 226 m.UpdatedAt = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 227 } 228 229 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, updateFields...) 230 231 delete(fieldValues, "ID") 232 233 table := db.T(m) 234 235 cols, vals := table.ColumnsAndValuesByFieldValues(fieldValues) 236 237 fields := make(map[string]bool, len(updateFields)) 238 for _, field := range updateFields { 239 fields[field] = true 240 } 241 242 for _, fieldNames := range m.UniqueIndexes() { 243 for _, field := range fieldNames { 244 delete(fields, field) 245 } 246 } 247 248 if len(fields) == 0 { 249 panic(fmt.Errorf("no fields for updates")) 250 } 251 252 for field := range fieldValues { 253 if !fields[field] { 254 delete(fieldValues, field) 255 } 256 } 257 258 additions := github_com_go_courier_sqlx_v2_builder.Additions{} 259 260 switch db.Dialect().DriverName() { 261 case "mysql": 262 additions = append(additions, github_com_go_courier_sqlx_v2_builder.OnDuplicateKeyUpdate(table.AssignmentsByFieldValues(fieldValues)...)) 263 case "postgres": 264 indexes := m.UniqueIndexes() 265 fields := make([]string, 0) 266 for _, fs := range indexes { 267 fields = append(fields, fs...) 268 } 269 indexFields, _ := db.T(m).Fields(fields...) 270 271 additions = append(additions, 272 github_com_go_courier_sqlx_v2_builder.OnConflict(indexFields). 273 DoUpdateSet(table.AssignmentsByFieldValues(fieldValues)...)) 274 } 275 276 additions = append(additions, github_com_go_courier_sqlx_v2_builder.Comment("User.CreateOnDuplicateWithUpdateFields")) 277 278 expr := github_com_go_courier_sqlx_v2_builder.Insert().Into(table, additions...).Values(cols, vals...) 279 280 _, err := db.ExecExpr(expr) 281 return err 282 283 } 284 285 func (m *User) DeleteByStruct(db github_com_go_courier_sqlx_v2.DBExecutor) error { 286 287 _, err := db.ExecExpr( 288 github_com_go_courier_sqlx_v2_builder.Delete(). 289 From( 290 db.T(m), 291 github_com_go_courier_sqlx_v2_builder.Where(m.ConditionByStruct(db)), 292 github_com_go_courier_sqlx_v2_builder.Comment("User.DeleteByStruct"), 293 ), 294 ) 295 296 return err 297 } 298 299 func (m *User) FetchByID(db github_com_go_courier_sqlx_v2.DBExecutor) error { 300 301 table := db.T(m) 302 303 err := db.QueryExprAndScan( 304 github_com_go_courier_sqlx_v2_builder.Select(nil). 305 From( 306 db.T(m), 307 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 308 table.F("ID").Eq(m.ID), 309 table.F("DeletedAt").Eq(m.DeletedAt), 310 )), 311 github_com_go_courier_sqlx_v2_builder.Comment("User.FetchByID"), 312 ), 313 m, 314 ) 315 316 return err 317 } 318 319 func (m *User) UpdateByIDWithMap(db github_com_go_courier_sqlx_v2.DBExecutor, fieldValues github_com_go_courier_sqlx_v2_builder.FieldValues) error { 320 321 if _, ok := fieldValues["UpdatedAt"]; !ok { 322 fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 323 } 324 325 table := db.T(m) 326 327 result, err := db.ExecExpr( 328 github_com_go_courier_sqlx_v2_builder.Update(db.T(m)). 329 Where( 330 github_com_go_courier_sqlx_v2_builder.And( 331 table.F("ID").Eq(m.ID), 332 table.F("DeletedAt").Eq(m.DeletedAt), 333 ), 334 github_com_go_courier_sqlx_v2_builder.Comment("User.UpdateByIDWithMap"), 335 ). 336 Set(table.AssignmentsByFieldValues(fieldValues)...), 337 ) 338 339 if err != nil { 340 return err 341 } 342 343 rowsAffected, _ := result.RowsAffected() 344 if rowsAffected == 0 { 345 return m.FetchByID(db) 346 } 347 348 return nil 349 350 } 351 352 func (m *User) UpdateByIDWithStruct(db github_com_go_courier_sqlx_v2.DBExecutor, zeroFields ...string) error { 353 354 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, zeroFields...) 355 return m.UpdateByIDWithMap(db, fieldValues) 356 357 } 358 359 func (m *User) FetchByIDForUpdate(db github_com_go_courier_sqlx_v2.DBExecutor) error { 360 361 table := db.T(m) 362 363 err := db.QueryExprAndScan( 364 github_com_go_courier_sqlx_v2_builder.Select(nil). 365 From( 366 db.T(m), 367 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 368 table.F("ID").Eq(m.ID), 369 table.F("DeletedAt").Eq(m.DeletedAt), 370 )), 371 github_com_go_courier_sqlx_v2_builder.ForUpdate(), 372 github_com_go_courier_sqlx_v2_builder.Comment("User.FetchByIDForUpdate"), 373 ), 374 m, 375 ) 376 377 return err 378 } 379 380 func (m *User) DeleteByID(db github_com_go_courier_sqlx_v2.DBExecutor) error { 381 382 table := db.T(m) 383 384 _, err := db.ExecExpr( 385 github_com_go_courier_sqlx_v2_builder.Delete(). 386 From(db.T(m), 387 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 388 table.F("ID").Eq(m.ID), 389 table.F("DeletedAt").Eq(m.DeletedAt), 390 )), 391 github_com_go_courier_sqlx_v2_builder.Comment("User.DeleteByID"), 392 )) 393 394 return err 395 } 396 397 func (m *User) SoftDeleteByID(db github_com_go_courier_sqlx_v2.DBExecutor) error { 398 399 table := db.T(m) 400 401 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValues{} 402 if _, ok := fieldValues["DeletedAt"]; !ok { 403 fieldValues["DeletedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 404 } 405 406 if _, ok := fieldValues["UpdatedAt"]; !ok { 407 fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 408 } 409 410 _, err := db.ExecExpr( 411 github_com_go_courier_sqlx_v2_builder.Update(db.T(m)). 412 Where( 413 github_com_go_courier_sqlx_v2_builder.And( 414 table.F("ID").Eq(m.ID), 415 table.F("DeletedAt").Eq(m.DeletedAt), 416 ), 417 github_com_go_courier_sqlx_v2_builder.Comment("User.SoftDeleteByID"), 418 ). 419 Set(table.AssignmentsByFieldValues(fieldValues)...), 420 ) 421 422 return err 423 424 } 425 426 func (m *User) FetchByName(db github_com_go_courier_sqlx_v2.DBExecutor) error { 427 428 table := db.T(m) 429 430 err := db.QueryExprAndScan( 431 github_com_go_courier_sqlx_v2_builder.Select(nil). 432 From( 433 db.T(m), 434 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 435 table.F("Name").Eq(m.Name), 436 table.F("DeletedAt").Eq(m.DeletedAt), 437 )), 438 github_com_go_courier_sqlx_v2_builder.Comment("User.FetchByName"), 439 ), 440 m, 441 ) 442 443 return err 444 } 445 446 func (m *User) UpdateByNameWithMap(db github_com_go_courier_sqlx_v2.DBExecutor, fieldValues github_com_go_courier_sqlx_v2_builder.FieldValues) error { 447 448 if _, ok := fieldValues["UpdatedAt"]; !ok { 449 fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 450 } 451 452 table := db.T(m) 453 454 result, err := db.ExecExpr( 455 github_com_go_courier_sqlx_v2_builder.Update(db.T(m)). 456 Where( 457 github_com_go_courier_sqlx_v2_builder.And( 458 table.F("Name").Eq(m.Name), 459 table.F("DeletedAt").Eq(m.DeletedAt), 460 ), 461 github_com_go_courier_sqlx_v2_builder.Comment("User.UpdateByNameWithMap"), 462 ). 463 Set(table.AssignmentsByFieldValues(fieldValues)...), 464 ) 465 466 if err != nil { 467 return err 468 } 469 470 rowsAffected, _ := result.RowsAffected() 471 if rowsAffected == 0 { 472 return m.FetchByName(db) 473 } 474 475 return nil 476 477 } 478 479 func (m *User) UpdateByNameWithStruct(db github_com_go_courier_sqlx_v2.DBExecutor, zeroFields ...string) error { 480 481 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValuesFromStructByNonZero(m, zeroFields...) 482 return m.UpdateByNameWithMap(db, fieldValues) 483 484 } 485 486 func (m *User) FetchByNameForUpdate(db github_com_go_courier_sqlx_v2.DBExecutor) error { 487 488 table := db.T(m) 489 490 err := db.QueryExprAndScan( 491 github_com_go_courier_sqlx_v2_builder.Select(nil). 492 From( 493 db.T(m), 494 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 495 table.F("Name").Eq(m.Name), 496 table.F("DeletedAt").Eq(m.DeletedAt), 497 )), 498 github_com_go_courier_sqlx_v2_builder.ForUpdate(), 499 github_com_go_courier_sqlx_v2_builder.Comment("User.FetchByNameForUpdate"), 500 ), 501 m, 502 ) 503 504 return err 505 } 506 507 func (m *User) DeleteByName(db github_com_go_courier_sqlx_v2.DBExecutor) error { 508 509 table := db.T(m) 510 511 _, err := db.ExecExpr( 512 github_com_go_courier_sqlx_v2_builder.Delete(). 513 From(db.T(m), 514 github_com_go_courier_sqlx_v2_builder.Where(github_com_go_courier_sqlx_v2_builder.And( 515 table.F("Name").Eq(m.Name), 516 table.F("DeletedAt").Eq(m.DeletedAt), 517 )), 518 github_com_go_courier_sqlx_v2_builder.Comment("User.DeleteByName"), 519 )) 520 521 return err 522 } 523 524 func (m *User) SoftDeleteByName(db github_com_go_courier_sqlx_v2.DBExecutor) error { 525 526 table := db.T(m) 527 528 fieldValues := github_com_go_courier_sqlx_v2_builder.FieldValues{} 529 if _, ok := fieldValues["DeletedAt"]; !ok { 530 fieldValues["DeletedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 531 } 532 533 if _, ok := fieldValues["UpdatedAt"]; !ok { 534 fieldValues["UpdatedAt"] = github_com_go_courier_sqlx_v2_datatypes.Timestamp(time.Now()) 535 } 536 537 _, err := db.ExecExpr( 538 github_com_go_courier_sqlx_v2_builder.Update(db.T(m)). 539 Where( 540 github_com_go_courier_sqlx_v2_builder.And( 541 table.F("Name").Eq(m.Name), 542 table.F("DeletedAt").Eq(m.DeletedAt), 543 ), 544 github_com_go_courier_sqlx_v2_builder.Comment("User.SoftDeleteByName"), 545 ). 546 Set(table.AssignmentsByFieldValues(fieldValues)...), 547 ) 548 549 return err 550 551 } 552 553 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) { 554 555 list := make([]User, 0) 556 557 table := db.T(m) 558 _ = table 559 560 condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0)) 561 562 finalAdditions := []github_com_go_courier_sqlx_v2_builder.Addition{ 563 github_com_go_courier_sqlx_v2_builder.Where(condition), 564 github_com_go_courier_sqlx_v2_builder.Comment("User.List"), 565 } 566 567 if len(additions) > 0 { 568 finalAdditions = append(finalAdditions, additions...) 569 } 570 571 err := db.QueryExprAndScan( 572 github_com_go_courier_sqlx_v2_builder.Select(nil). 573 From(db.T(m), finalAdditions...), 574 &list, 575 ) 576 577 return list, err 578 579 } 580 581 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) { 582 583 count := -1 584 585 table := db.T(m) 586 _ = table 587 588 condition = github_com_go_courier_sqlx_v2_builder.And(condition, table.F("DeletedAt").Eq(0)) 589 590 finalAdditions := []github_com_go_courier_sqlx_v2_builder.Addition{ 591 github_com_go_courier_sqlx_v2_builder.Where(condition), 592 github_com_go_courier_sqlx_v2_builder.Comment("User.Count"), 593 } 594 595 if len(additions) > 0 { 596 finalAdditions = append(finalAdditions, additions...) 597 } 598 599 err := db.QueryExprAndScan( 600 github_com_go_courier_sqlx_v2_builder.Select( 601 github_com_go_courier_sqlx_v2_builder.Count(), 602 ). 603 From(db.T(m), finalAdditions...), 604 &count, 605 ) 606 607 return count, err 608 609 } 610 611 func (m *User) BatchFetchByIDList(db github_com_go_courier_sqlx_v2.DBExecutor, values []uint64) ([]User, error) { 612 613 if len(values) == 0 { 614 return nil, nil 615 } 616 617 table := db.T(m) 618 619 condition := table.F("ID").In(values) 620 621 return m.List(db, condition) 622 623 } 624 625 func (m *User) BatchFetchByNameList(db github_com_go_courier_sqlx_v2.DBExecutor, values []string) ([]User, error) { 626 627 if len(values) == 0 { 628 return nil, nil 629 } 630 631 table := db.T(m) 632 633 condition := table.F("Name").In(values) 634 635 return m.List(db, condition) 636 637 } 638 639 func (m *User) BatchFetchByNicknameList(db github_com_go_courier_sqlx_v2.DBExecutor, values []string) ([]User, error) { 640 641 if len(values) == 0 { 642 return nil, nil 643 } 644 645 table := db.T(m) 646 647 condition := table.F("Nickname").In(values) 648 649 return m.List(db, condition) 650 651 } 652 653 func (m *User) BatchFetchByUsernameList(db github_com_go_courier_sqlx_v2.DBExecutor, values []string) ([]User, error) { 654 655 if len(values) == 0 { 656 return nil, nil 657 } 658 659 table := db.T(m) 660 661 condition := table.F("Username").In(values) 662 663 return m.List(db, condition) 664 665 }