github.com/oinume/lekcije@v0.0.0-20231017100347-5b4c5eb6ab24/backend/model2/user.go (about) 1 // Code generated by SQLBoiler 4.14.2 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT. 2 // This file is meant to be re-generated in place and/or deleted at any time. 3 4 package model2 5 6 import ( 7 "context" 8 "database/sql" 9 "fmt" 10 "reflect" 11 "strconv" 12 "strings" 13 "sync" 14 "time" 15 16 "github.com/friendsofgo/errors" 17 "github.com/volatiletech/null/v8" 18 "github.com/volatiletech/sqlboiler/v4/boil" 19 "github.com/volatiletech/sqlboiler/v4/queries" 20 "github.com/volatiletech/sqlboiler/v4/queries/qm" 21 "github.com/volatiletech/sqlboiler/v4/queries/qmhelper" 22 "github.com/volatiletech/strmangle" 23 ) 24 25 // User is an object representing the database table. 26 type User struct { 27 ID uint `boil:"id" json:"id" toml:"id" yaml:"id"` 28 Name string `boil:"name" json:"name" toml:"name" yaml:"name"` 29 Email string `boil:"email" json:"email" toml:"email" yaml:"email"` 30 EmailVerified uint8 `boil:"email_verified" json:"email_verified" toml:"email_verified" yaml:"email_verified"` 31 PlanID uint8 `boil:"plan_id" json:"plan_id" toml:"plan_id" yaml:"plan_id"` 32 FollowedTeacherAt null.Time `boil:"followed_teacher_at" json:"followed_teacher_at,omitempty" toml:"followed_teacher_at" yaml:"followed_teacher_at,omitempty"` 33 OpenNotificationAt null.Time `boil:"open_notification_at" json:"open_notification_at,omitempty" toml:"open_notification_at" yaml:"open_notification_at,omitempty"` 34 CreatedAt time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"` 35 UpdatedAt time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"` 36 37 R *userR `boil:"-" json:"-" toml:"-" yaml:"-"` 38 L userL `boil:"-" json:"-" toml:"-" yaml:"-"` 39 } 40 41 var UserColumns = struct { 42 ID string 43 Name string 44 Email string 45 EmailVerified string 46 PlanID string 47 FollowedTeacherAt string 48 OpenNotificationAt string 49 CreatedAt string 50 UpdatedAt string 51 }{ 52 ID: "id", 53 Name: "name", 54 Email: "email", 55 EmailVerified: "email_verified", 56 PlanID: "plan_id", 57 FollowedTeacherAt: "followed_teacher_at", 58 OpenNotificationAt: "open_notification_at", 59 CreatedAt: "created_at", 60 UpdatedAt: "updated_at", 61 } 62 63 var UserTableColumns = struct { 64 ID string 65 Name string 66 Email string 67 EmailVerified string 68 PlanID string 69 FollowedTeacherAt string 70 OpenNotificationAt string 71 CreatedAt string 72 UpdatedAt string 73 }{ 74 ID: "user.id", 75 Name: "user.name", 76 Email: "user.email", 77 EmailVerified: "user.email_verified", 78 PlanID: "user.plan_id", 79 FollowedTeacherAt: "user.followed_teacher_at", 80 OpenNotificationAt: "user.open_notification_at", 81 CreatedAt: "user.created_at", 82 UpdatedAt: "user.updated_at", 83 } 84 85 // Generated where 86 87 type whereHelpernull_Time struct{ field string } 88 89 func (w whereHelpernull_Time) EQ(x null.Time) qm.QueryMod { 90 return qmhelper.WhereNullEQ(w.field, false, x) 91 } 92 func (w whereHelpernull_Time) NEQ(x null.Time) qm.QueryMod { 93 return qmhelper.WhereNullEQ(w.field, true, x) 94 } 95 func (w whereHelpernull_Time) LT(x null.Time) qm.QueryMod { 96 return qmhelper.Where(w.field, qmhelper.LT, x) 97 } 98 func (w whereHelpernull_Time) LTE(x null.Time) qm.QueryMod { 99 return qmhelper.Where(w.field, qmhelper.LTE, x) 100 } 101 func (w whereHelpernull_Time) GT(x null.Time) qm.QueryMod { 102 return qmhelper.Where(w.field, qmhelper.GT, x) 103 } 104 func (w whereHelpernull_Time) GTE(x null.Time) qm.QueryMod { 105 return qmhelper.Where(w.field, qmhelper.GTE, x) 106 } 107 108 func (w whereHelpernull_Time) IsNull() qm.QueryMod { return qmhelper.WhereIsNull(w.field) } 109 func (w whereHelpernull_Time) IsNotNull() qm.QueryMod { return qmhelper.WhereIsNotNull(w.field) } 110 111 var UserWhere = struct { 112 ID whereHelperuint 113 Name whereHelperstring 114 Email whereHelperstring 115 EmailVerified whereHelperuint8 116 PlanID whereHelperuint8 117 FollowedTeacherAt whereHelpernull_Time 118 OpenNotificationAt whereHelpernull_Time 119 CreatedAt whereHelpertime_Time 120 UpdatedAt whereHelpertime_Time 121 }{ 122 ID: whereHelperuint{field: "`user`.`id`"}, 123 Name: whereHelperstring{field: "`user`.`name`"}, 124 Email: whereHelperstring{field: "`user`.`email`"}, 125 EmailVerified: whereHelperuint8{field: "`user`.`email_verified`"}, 126 PlanID: whereHelperuint8{field: "`user`.`plan_id`"}, 127 FollowedTeacherAt: whereHelpernull_Time{field: "`user`.`followed_teacher_at`"}, 128 OpenNotificationAt: whereHelpernull_Time{field: "`user`.`open_notification_at`"}, 129 CreatedAt: whereHelpertime_Time{field: "`user`.`created_at`"}, 130 UpdatedAt: whereHelpertime_Time{field: "`user`.`updated_at`"}, 131 } 132 133 // UserRels is where relationship names are stored. 134 var UserRels = struct { 135 }{} 136 137 // userR is where relationships are stored. 138 type userR struct { 139 } 140 141 // NewStruct creates a new relationship struct 142 func (*userR) NewStruct() *userR { 143 return &userR{} 144 } 145 146 // userL is where Load methods for each relationship are stored. 147 type userL struct{} 148 149 var ( 150 userAllColumns = []string{"id", "name", "email", "email_verified", "plan_id", "followed_teacher_at", "open_notification_at", "created_at", "updated_at"} 151 userColumnsWithoutDefault = []string{"name", "email", "followed_teacher_at", "open_notification_at", "created_at", "updated_at"} 152 userColumnsWithDefault = []string{"id", "email_verified", "plan_id"} 153 userPrimaryKeyColumns = []string{"id"} 154 userGeneratedColumns = []string{} 155 ) 156 157 type ( 158 // UserSlice is an alias for a slice of pointers to User. 159 // This should almost always be used instead of []User. 160 UserSlice []*User 161 // UserHook is the signature for custom User hook methods 162 UserHook func(context.Context, boil.ContextExecutor, *User) error 163 164 userQuery struct { 165 *queries.Query 166 } 167 ) 168 169 // Cache for insert, update and upsert 170 var ( 171 userType = reflect.TypeOf(&User{}) 172 userMapping = queries.MakeStructMapping(userType) 173 userPrimaryKeyMapping, _ = queries.BindMapping(userType, userMapping, userPrimaryKeyColumns) 174 userInsertCacheMut sync.RWMutex 175 userInsertCache = make(map[string]insertCache) 176 userUpdateCacheMut sync.RWMutex 177 userUpdateCache = make(map[string]updateCache) 178 userUpsertCacheMut sync.RWMutex 179 userUpsertCache = make(map[string]insertCache) 180 ) 181 182 var ( 183 // Force time package dependency for automated UpdatedAt/CreatedAt. 184 _ = time.Second 185 // Force qmhelper dependency for where clause generation (which doesn't 186 // always happen) 187 _ = qmhelper.Where 188 ) 189 190 var userAfterSelectHooks []UserHook 191 192 var userBeforeInsertHooks []UserHook 193 var userAfterInsertHooks []UserHook 194 195 var userBeforeUpdateHooks []UserHook 196 var userAfterUpdateHooks []UserHook 197 198 var userBeforeDeleteHooks []UserHook 199 var userAfterDeleteHooks []UserHook 200 201 var userBeforeUpsertHooks []UserHook 202 var userAfterUpsertHooks []UserHook 203 204 // doAfterSelectHooks executes all "after Select" hooks. 205 func (o *User) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 206 if boil.HooksAreSkipped(ctx) { 207 return nil 208 } 209 210 for _, hook := range userAfterSelectHooks { 211 if err := hook(ctx, exec, o); err != nil { 212 return err 213 } 214 } 215 216 return nil 217 } 218 219 // doBeforeInsertHooks executes all "before insert" hooks. 220 func (o *User) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 221 if boil.HooksAreSkipped(ctx) { 222 return nil 223 } 224 225 for _, hook := range userBeforeInsertHooks { 226 if err := hook(ctx, exec, o); err != nil { 227 return err 228 } 229 } 230 231 return nil 232 } 233 234 // doAfterInsertHooks executes all "after Insert" hooks. 235 func (o *User) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 236 if boil.HooksAreSkipped(ctx) { 237 return nil 238 } 239 240 for _, hook := range userAfterInsertHooks { 241 if err := hook(ctx, exec, o); err != nil { 242 return err 243 } 244 } 245 246 return nil 247 } 248 249 // doBeforeUpdateHooks executes all "before Update" hooks. 250 func (o *User) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 251 if boil.HooksAreSkipped(ctx) { 252 return nil 253 } 254 255 for _, hook := range userBeforeUpdateHooks { 256 if err := hook(ctx, exec, o); err != nil { 257 return err 258 } 259 } 260 261 return nil 262 } 263 264 // doAfterUpdateHooks executes all "after Update" hooks. 265 func (o *User) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 266 if boil.HooksAreSkipped(ctx) { 267 return nil 268 } 269 270 for _, hook := range userAfterUpdateHooks { 271 if err := hook(ctx, exec, o); err != nil { 272 return err 273 } 274 } 275 276 return nil 277 } 278 279 // doBeforeDeleteHooks executes all "before Delete" hooks. 280 func (o *User) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 281 if boil.HooksAreSkipped(ctx) { 282 return nil 283 } 284 285 for _, hook := range userBeforeDeleteHooks { 286 if err := hook(ctx, exec, o); err != nil { 287 return err 288 } 289 } 290 291 return nil 292 } 293 294 // doAfterDeleteHooks executes all "after Delete" hooks. 295 func (o *User) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 296 if boil.HooksAreSkipped(ctx) { 297 return nil 298 } 299 300 for _, hook := range userAfterDeleteHooks { 301 if err := hook(ctx, exec, o); err != nil { 302 return err 303 } 304 } 305 306 return nil 307 } 308 309 // doBeforeUpsertHooks executes all "before Upsert" hooks. 310 func (o *User) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 311 if boil.HooksAreSkipped(ctx) { 312 return nil 313 } 314 315 for _, hook := range userBeforeUpsertHooks { 316 if err := hook(ctx, exec, o); err != nil { 317 return err 318 } 319 } 320 321 return nil 322 } 323 324 // doAfterUpsertHooks executes all "after Upsert" hooks. 325 func (o *User) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { 326 if boil.HooksAreSkipped(ctx) { 327 return nil 328 } 329 330 for _, hook := range userAfterUpsertHooks { 331 if err := hook(ctx, exec, o); err != nil { 332 return err 333 } 334 } 335 336 return nil 337 } 338 339 // AddUserHook registers your hook function for all future operations. 340 func AddUserHook(hookPoint boil.HookPoint, userHook UserHook) { 341 switch hookPoint { 342 case boil.AfterSelectHook: 343 userAfterSelectHooks = append(userAfterSelectHooks, userHook) 344 case boil.BeforeInsertHook: 345 userBeforeInsertHooks = append(userBeforeInsertHooks, userHook) 346 case boil.AfterInsertHook: 347 userAfterInsertHooks = append(userAfterInsertHooks, userHook) 348 case boil.BeforeUpdateHook: 349 userBeforeUpdateHooks = append(userBeforeUpdateHooks, userHook) 350 case boil.AfterUpdateHook: 351 userAfterUpdateHooks = append(userAfterUpdateHooks, userHook) 352 case boil.BeforeDeleteHook: 353 userBeforeDeleteHooks = append(userBeforeDeleteHooks, userHook) 354 case boil.AfterDeleteHook: 355 userAfterDeleteHooks = append(userAfterDeleteHooks, userHook) 356 case boil.BeforeUpsertHook: 357 userBeforeUpsertHooks = append(userBeforeUpsertHooks, userHook) 358 case boil.AfterUpsertHook: 359 userAfterUpsertHooks = append(userAfterUpsertHooks, userHook) 360 } 361 } 362 363 // One returns a single user record from the query. 364 func (q userQuery) One(ctx context.Context, exec boil.ContextExecutor) (*User, error) { 365 o := &User{} 366 367 queries.SetLimit(q.Query, 1) 368 369 err := q.Bind(ctx, exec, o) 370 if err != nil { 371 if errors.Is(err, sql.ErrNoRows) { 372 return nil, sql.ErrNoRows 373 } 374 return nil, errors.Wrap(err, "model2: failed to execute a one query for user") 375 } 376 377 if err := o.doAfterSelectHooks(ctx, exec); err != nil { 378 return o, err 379 } 380 381 return o, nil 382 } 383 384 // All returns all User records from the query. 385 func (q userQuery) All(ctx context.Context, exec boil.ContextExecutor) (UserSlice, error) { 386 var o []*User 387 388 err := q.Bind(ctx, exec, &o) 389 if err != nil { 390 return nil, errors.Wrap(err, "model2: failed to assign all query results to User slice") 391 } 392 393 if len(userAfterSelectHooks) != 0 { 394 for _, obj := range o { 395 if err := obj.doAfterSelectHooks(ctx, exec); err != nil { 396 return o, err 397 } 398 } 399 } 400 401 return o, nil 402 } 403 404 // Count returns the count of all User records in the query. 405 func (q userQuery) Count(ctx context.Context, exec boil.ContextExecutor) (int64, error) { 406 var count int64 407 408 queries.SetSelect(q.Query, nil) 409 queries.SetCount(q.Query) 410 411 err := q.Query.QueryRowContext(ctx, exec).Scan(&count) 412 if err != nil { 413 return 0, errors.Wrap(err, "model2: failed to count user rows") 414 } 415 416 return count, nil 417 } 418 419 // Exists checks if the row exists in the table. 420 func (q userQuery) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) { 421 var count int64 422 423 queries.SetSelect(q.Query, nil) 424 queries.SetCount(q.Query) 425 queries.SetLimit(q.Query, 1) 426 427 err := q.Query.QueryRowContext(ctx, exec).Scan(&count) 428 if err != nil { 429 return false, errors.Wrap(err, "model2: failed to check if user exists") 430 } 431 432 return count > 0, nil 433 } 434 435 // Users retrieves all the records using an executor. 436 func Users(mods ...qm.QueryMod) userQuery { 437 mods = append(mods, qm.From("`user`")) 438 q := NewQuery(mods...) 439 if len(queries.GetSelect(q)) == 0 { 440 queries.SetSelect(q, []string{"`user`.*"}) 441 } 442 443 return userQuery{q} 444 } 445 446 // FindUser retrieves a single record by ID with an executor. 447 // If selectCols is empty Find will return all columns. 448 func FindUser(ctx context.Context, exec boil.ContextExecutor, iD uint, selectCols ...string) (*User, error) { 449 userObj := &User{} 450 451 sel := "*" 452 if len(selectCols) > 0 { 453 sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") 454 } 455 query := fmt.Sprintf( 456 "select %s from `user` where `id`=?", sel, 457 ) 458 459 q := queries.Raw(query, iD) 460 461 err := q.Bind(ctx, exec, userObj) 462 if err != nil { 463 if errors.Is(err, sql.ErrNoRows) { 464 return nil, sql.ErrNoRows 465 } 466 return nil, errors.Wrap(err, "model2: unable to select from user") 467 } 468 469 if err = userObj.doAfterSelectHooks(ctx, exec); err != nil { 470 return userObj, err 471 } 472 473 return userObj, nil 474 } 475 476 // Insert a single record using an executor. 477 // See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. 478 func (o *User) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { 479 if o == nil { 480 return errors.New("model2: no user provided for insertion") 481 } 482 483 var err error 484 if !boil.TimestampsAreSkipped(ctx) { 485 currTime := time.Now().In(boil.GetLocation()) 486 487 if o.CreatedAt.IsZero() { 488 o.CreatedAt = currTime 489 } 490 if o.UpdatedAt.IsZero() { 491 o.UpdatedAt = currTime 492 } 493 } 494 495 if err := o.doBeforeInsertHooks(ctx, exec); err != nil { 496 return err 497 } 498 499 nzDefaults := queries.NonZeroDefaultSet(userColumnsWithDefault, o) 500 501 key := makeCacheKey(columns, nzDefaults) 502 userInsertCacheMut.RLock() 503 cache, cached := userInsertCache[key] 504 userInsertCacheMut.RUnlock() 505 506 if !cached { 507 wl, returnColumns := columns.InsertColumnSet( 508 userAllColumns, 509 userColumnsWithDefault, 510 userColumnsWithoutDefault, 511 nzDefaults, 512 ) 513 514 cache.valueMapping, err = queries.BindMapping(userType, userMapping, wl) 515 if err != nil { 516 return err 517 } 518 cache.retMapping, err = queries.BindMapping(userType, userMapping, returnColumns) 519 if err != nil { 520 return err 521 } 522 if len(wl) != 0 { 523 cache.query = fmt.Sprintf("INSERT INTO `user` (`%s`) %%sVALUES (%s)%%s", strings.Join(wl, "`,`"), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) 524 } else { 525 cache.query = "INSERT INTO `user` () VALUES ()%s%s" 526 } 527 528 var queryOutput, queryReturning string 529 530 if len(cache.retMapping) != 0 { 531 cache.retQuery = fmt.Sprintf("SELECT `%s` FROM `user` WHERE %s", strings.Join(returnColumns, "`,`"), strmangle.WhereClause("`", "`", 0, userPrimaryKeyColumns)) 532 } 533 534 cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning) 535 } 536 537 value := reflect.Indirect(reflect.ValueOf(o)) 538 vals := queries.ValuesFromMapping(value, cache.valueMapping) 539 540 if boil.IsDebug(ctx) { 541 writer := boil.DebugWriterFrom(ctx) 542 fmt.Fprintln(writer, cache.query) 543 fmt.Fprintln(writer, vals) 544 } 545 result, err := exec.ExecContext(ctx, cache.query, vals...) 546 547 if err != nil { 548 return errors.Wrap(err, "model2: unable to insert into user") 549 } 550 551 var lastID int64 552 var identifierCols []interface{} 553 554 if len(cache.retMapping) == 0 { 555 goto CacheNoHooks 556 } 557 558 lastID, err = result.LastInsertId() 559 if err != nil { 560 return ErrSyncFail 561 } 562 563 o.ID = uint(lastID) 564 if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == userMapping["id"] { 565 goto CacheNoHooks 566 } 567 568 identifierCols = []interface{}{ 569 o.ID, 570 } 571 572 if boil.IsDebug(ctx) { 573 writer := boil.DebugWriterFrom(ctx) 574 fmt.Fprintln(writer, cache.retQuery) 575 fmt.Fprintln(writer, identifierCols...) 576 } 577 err = exec.QueryRowContext(ctx, cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) 578 if err != nil { 579 return errors.Wrap(err, "model2: unable to populate default values for user") 580 } 581 582 CacheNoHooks: 583 if !cached { 584 userInsertCacheMut.Lock() 585 userInsertCache[key] = cache 586 userInsertCacheMut.Unlock() 587 } 588 589 return o.doAfterInsertHooks(ctx, exec) 590 } 591 592 // Update uses an executor to update the User. 593 // See boil.Columns.UpdateColumnSet documentation to understand column list inference for updates. 594 // Update does not automatically update the record in case of default values. Use .Reload() to refresh the records. 595 func (o *User) Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error) { 596 if !boil.TimestampsAreSkipped(ctx) { 597 currTime := time.Now().In(boil.GetLocation()) 598 599 o.UpdatedAt = currTime 600 } 601 602 var err error 603 if err = o.doBeforeUpdateHooks(ctx, exec); err != nil { 604 return 0, err 605 } 606 key := makeCacheKey(columns, nil) 607 userUpdateCacheMut.RLock() 608 cache, cached := userUpdateCache[key] 609 userUpdateCacheMut.RUnlock() 610 611 if !cached { 612 wl := columns.UpdateColumnSet( 613 userAllColumns, 614 userPrimaryKeyColumns, 615 ) 616 617 if !columns.IsWhitelist() { 618 wl = strmangle.SetComplement(wl, []string{"created_at"}) 619 } 620 if len(wl) == 0 { 621 return 0, errors.New("model2: unable to update user, could not build whitelist") 622 } 623 624 cache.query = fmt.Sprintf("UPDATE `user` SET %s WHERE %s", 625 strmangle.SetParamNames("`", "`", 0, wl), 626 strmangle.WhereClause("`", "`", 0, userPrimaryKeyColumns), 627 ) 628 cache.valueMapping, err = queries.BindMapping(userType, userMapping, append(wl, userPrimaryKeyColumns...)) 629 if err != nil { 630 return 0, err 631 } 632 } 633 634 values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping) 635 636 if boil.IsDebug(ctx) { 637 writer := boil.DebugWriterFrom(ctx) 638 fmt.Fprintln(writer, cache.query) 639 fmt.Fprintln(writer, values) 640 } 641 var result sql.Result 642 result, err = exec.ExecContext(ctx, cache.query, values...) 643 if err != nil { 644 return 0, errors.Wrap(err, "model2: unable to update user row") 645 } 646 647 rowsAff, err := result.RowsAffected() 648 if err != nil { 649 return 0, errors.Wrap(err, "model2: failed to get rows affected by update for user") 650 } 651 652 if !cached { 653 userUpdateCacheMut.Lock() 654 userUpdateCache[key] = cache 655 userUpdateCacheMut.Unlock() 656 } 657 658 return rowsAff, o.doAfterUpdateHooks(ctx, exec) 659 } 660 661 // UpdateAll updates all rows with the specified column values. 662 func (q userQuery) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) { 663 queries.SetUpdate(q.Query, cols) 664 665 result, err := q.Query.ExecContext(ctx, exec) 666 if err != nil { 667 return 0, errors.Wrap(err, "model2: unable to update all for user") 668 } 669 670 rowsAff, err := result.RowsAffected() 671 if err != nil { 672 return 0, errors.Wrap(err, "model2: unable to retrieve rows affected for user") 673 } 674 675 return rowsAff, nil 676 } 677 678 // UpdateAll updates all rows with the specified column values, using an executor. 679 func (o UserSlice) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) { 680 ln := int64(len(o)) 681 if ln == 0 { 682 return 0, nil 683 } 684 685 if len(cols) == 0 { 686 return 0, errors.New("model2: update all requires at least one column argument") 687 } 688 689 colNames := make([]string, len(cols)) 690 args := make([]interface{}, len(cols)) 691 692 i := 0 693 for name, value := range cols { 694 colNames[i] = name 695 args[i] = value 696 i++ 697 } 698 699 // Append all of the primary key values for each column 700 for _, obj := range o { 701 pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), userPrimaryKeyMapping) 702 args = append(args, pkeyArgs...) 703 } 704 705 sql := fmt.Sprintf("UPDATE `user` SET %s WHERE %s", 706 strmangle.SetParamNames("`", "`", 0, colNames), 707 strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, userPrimaryKeyColumns, len(o))) 708 709 if boil.IsDebug(ctx) { 710 writer := boil.DebugWriterFrom(ctx) 711 fmt.Fprintln(writer, sql) 712 fmt.Fprintln(writer, args...) 713 } 714 result, err := exec.ExecContext(ctx, sql, args...) 715 if err != nil { 716 return 0, errors.Wrap(err, "model2: unable to update all in user slice") 717 } 718 719 rowsAff, err := result.RowsAffected() 720 if err != nil { 721 return 0, errors.Wrap(err, "model2: unable to retrieve rows affected all in update all user") 722 } 723 return rowsAff, nil 724 } 725 726 var mySQLUserUniqueColumns = []string{ 727 "id", 728 "email", 729 } 730 731 // Upsert attempts an insert using an executor, and does an update or ignore on conflict. 732 // See boil.Columns documentation for how to properly use updateColumns and insertColumns. 733 func (o *User) Upsert(ctx context.Context, exec boil.ContextExecutor, updateColumns, insertColumns boil.Columns) error { 734 if o == nil { 735 return errors.New("model2: no user provided for upsert") 736 } 737 if !boil.TimestampsAreSkipped(ctx) { 738 currTime := time.Now().In(boil.GetLocation()) 739 740 if o.CreatedAt.IsZero() { 741 o.CreatedAt = currTime 742 } 743 o.UpdatedAt = currTime 744 } 745 746 if err := o.doBeforeUpsertHooks(ctx, exec); err != nil { 747 return err 748 } 749 750 nzDefaults := queries.NonZeroDefaultSet(userColumnsWithDefault, o) 751 nzUniques := queries.NonZeroDefaultSet(mySQLUserUniqueColumns, o) 752 753 if len(nzUniques) == 0 { 754 return errors.New("cannot upsert with a table that cannot conflict on a unique column") 755 } 756 757 // Build cache key in-line uglily - mysql vs psql problems 758 buf := strmangle.GetBuffer() 759 buf.WriteString(strconv.Itoa(updateColumns.Kind)) 760 for _, c := range updateColumns.Cols { 761 buf.WriteString(c) 762 } 763 buf.WriteByte('.') 764 buf.WriteString(strconv.Itoa(insertColumns.Kind)) 765 for _, c := range insertColumns.Cols { 766 buf.WriteString(c) 767 } 768 buf.WriteByte('.') 769 for _, c := range nzDefaults { 770 buf.WriteString(c) 771 } 772 buf.WriteByte('.') 773 for _, c := range nzUniques { 774 buf.WriteString(c) 775 } 776 key := buf.String() 777 strmangle.PutBuffer(buf) 778 779 userUpsertCacheMut.RLock() 780 cache, cached := userUpsertCache[key] 781 userUpsertCacheMut.RUnlock() 782 783 var err error 784 785 if !cached { 786 insert, ret := insertColumns.InsertColumnSet( 787 userAllColumns, 788 userColumnsWithDefault, 789 userColumnsWithoutDefault, 790 nzDefaults, 791 ) 792 793 update := updateColumns.UpdateColumnSet( 794 userAllColumns, 795 userPrimaryKeyColumns, 796 ) 797 798 if !updateColumns.IsNone() && len(update) == 0 { 799 return errors.New("model2: unable to upsert user, could not build update column list") 800 } 801 802 ret = strmangle.SetComplement(ret, nzUniques) 803 cache.query = buildUpsertQueryMySQL(dialect, "`user`", update, insert) 804 cache.retQuery = fmt.Sprintf( 805 "SELECT %s FROM `user` WHERE %s", 806 strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, ret), ","), 807 strmangle.WhereClause("`", "`", 0, nzUniques), 808 ) 809 810 cache.valueMapping, err = queries.BindMapping(userType, userMapping, insert) 811 if err != nil { 812 return err 813 } 814 if len(ret) != 0 { 815 cache.retMapping, err = queries.BindMapping(userType, userMapping, ret) 816 if err != nil { 817 return err 818 } 819 } 820 } 821 822 value := reflect.Indirect(reflect.ValueOf(o)) 823 vals := queries.ValuesFromMapping(value, cache.valueMapping) 824 var returns []interface{} 825 if len(cache.retMapping) != 0 { 826 returns = queries.PtrsFromMapping(value, cache.retMapping) 827 } 828 829 if boil.IsDebug(ctx) { 830 writer := boil.DebugWriterFrom(ctx) 831 fmt.Fprintln(writer, cache.query) 832 fmt.Fprintln(writer, vals) 833 } 834 result, err := exec.ExecContext(ctx, cache.query, vals...) 835 836 if err != nil { 837 return errors.Wrap(err, "model2: unable to upsert for user") 838 } 839 840 var lastID int64 841 var uniqueMap []uint64 842 var nzUniqueCols []interface{} 843 844 if len(cache.retMapping) == 0 { 845 goto CacheNoHooks 846 } 847 848 lastID, err = result.LastInsertId() 849 if err != nil { 850 return ErrSyncFail 851 } 852 853 o.ID = uint(lastID) 854 if lastID != 0 && len(cache.retMapping) == 1 && cache.retMapping[0] == userMapping["id"] { 855 goto CacheNoHooks 856 } 857 858 uniqueMap, err = queries.BindMapping(userType, userMapping, nzUniques) 859 if err != nil { 860 return errors.Wrap(err, "model2: unable to retrieve unique values for user") 861 } 862 nzUniqueCols = queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), uniqueMap) 863 864 if boil.IsDebug(ctx) { 865 writer := boil.DebugWriterFrom(ctx) 866 fmt.Fprintln(writer, cache.retQuery) 867 fmt.Fprintln(writer, nzUniqueCols...) 868 } 869 err = exec.QueryRowContext(ctx, cache.retQuery, nzUniqueCols...).Scan(returns...) 870 if err != nil { 871 return errors.Wrap(err, "model2: unable to populate default values for user") 872 } 873 874 CacheNoHooks: 875 if !cached { 876 userUpsertCacheMut.Lock() 877 userUpsertCache[key] = cache 878 userUpsertCacheMut.Unlock() 879 } 880 881 return o.doAfterUpsertHooks(ctx, exec) 882 } 883 884 // Delete deletes a single User record with an executor. 885 // Delete will match against the primary key column to find the record to delete. 886 func (o *User) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) { 887 if o == nil { 888 return 0, errors.New("model2: no User provided for delete") 889 } 890 891 if err := o.doBeforeDeleteHooks(ctx, exec); err != nil { 892 return 0, err 893 } 894 895 args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), userPrimaryKeyMapping) 896 sql := "DELETE FROM `user` WHERE `id`=?" 897 898 if boil.IsDebug(ctx) { 899 writer := boil.DebugWriterFrom(ctx) 900 fmt.Fprintln(writer, sql) 901 fmt.Fprintln(writer, args...) 902 } 903 result, err := exec.ExecContext(ctx, sql, args...) 904 if err != nil { 905 return 0, errors.Wrap(err, "model2: unable to delete from user") 906 } 907 908 rowsAff, err := result.RowsAffected() 909 if err != nil { 910 return 0, errors.Wrap(err, "model2: failed to get rows affected by delete for user") 911 } 912 913 if err := o.doAfterDeleteHooks(ctx, exec); err != nil { 914 return 0, err 915 } 916 917 return rowsAff, nil 918 } 919 920 // DeleteAll deletes all matching rows. 921 func (q userQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { 922 if q.Query == nil { 923 return 0, errors.New("model2: no userQuery provided for delete all") 924 } 925 926 queries.SetDelete(q.Query) 927 928 result, err := q.Query.ExecContext(ctx, exec) 929 if err != nil { 930 return 0, errors.Wrap(err, "model2: unable to delete all from user") 931 } 932 933 rowsAff, err := result.RowsAffected() 934 if err != nil { 935 return 0, errors.Wrap(err, "model2: failed to get rows affected by deleteall for user") 936 } 937 938 return rowsAff, nil 939 } 940 941 // DeleteAll deletes all rows in the slice, using an executor. 942 func (o UserSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { 943 if len(o) == 0 { 944 return 0, nil 945 } 946 947 if len(userBeforeDeleteHooks) != 0 { 948 for _, obj := range o { 949 if err := obj.doBeforeDeleteHooks(ctx, exec); err != nil { 950 return 0, err 951 } 952 } 953 } 954 955 var args []interface{} 956 for _, obj := range o { 957 pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), userPrimaryKeyMapping) 958 args = append(args, pkeyArgs...) 959 } 960 961 sql := "DELETE FROM `user` WHERE " + 962 strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, userPrimaryKeyColumns, len(o)) 963 964 if boil.IsDebug(ctx) { 965 writer := boil.DebugWriterFrom(ctx) 966 fmt.Fprintln(writer, sql) 967 fmt.Fprintln(writer, args) 968 } 969 result, err := exec.ExecContext(ctx, sql, args...) 970 if err != nil { 971 return 0, errors.Wrap(err, "model2: unable to delete all from user slice") 972 } 973 974 rowsAff, err := result.RowsAffected() 975 if err != nil { 976 return 0, errors.Wrap(err, "model2: failed to get rows affected by deleteall for user") 977 } 978 979 if len(userAfterDeleteHooks) != 0 { 980 for _, obj := range o { 981 if err := obj.doAfterDeleteHooks(ctx, exec); err != nil { 982 return 0, err 983 } 984 } 985 } 986 987 return rowsAff, nil 988 } 989 990 // Reload refetches the object from the database 991 // using the primary keys with an executor. 992 func (o *User) Reload(ctx context.Context, exec boil.ContextExecutor) error { 993 ret, err := FindUser(ctx, exec, o.ID) 994 if err != nil { 995 return err 996 } 997 998 *o = *ret 999 return nil 1000 } 1001 1002 // ReloadAll refetches every row with matching primary key column values 1003 // and overwrites the original object slice with the newly updated slice. 1004 func (o *UserSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error { 1005 if o == nil || len(*o) == 0 { 1006 return nil 1007 } 1008 1009 slice := UserSlice{} 1010 var args []interface{} 1011 for _, obj := range *o { 1012 pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), userPrimaryKeyMapping) 1013 args = append(args, pkeyArgs...) 1014 } 1015 1016 sql := "SELECT `user`.* FROM `user` WHERE " + 1017 strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, userPrimaryKeyColumns, len(*o)) 1018 1019 q := queries.Raw(sql, args...) 1020 1021 err := q.Bind(ctx, exec, &slice) 1022 if err != nil { 1023 return errors.Wrap(err, "model2: unable to reload all in UserSlice") 1024 } 1025 1026 *o = slice 1027 1028 return nil 1029 } 1030 1031 // UserExists checks if the User row exists. 1032 func UserExists(ctx context.Context, exec boil.ContextExecutor, iD uint) (bool, error) { 1033 var exists bool 1034 sql := "select exists(select 1 from `user` where `id`=? limit 1)" 1035 1036 if boil.IsDebug(ctx) { 1037 writer := boil.DebugWriterFrom(ctx) 1038 fmt.Fprintln(writer, sql) 1039 fmt.Fprintln(writer, iD) 1040 } 1041 row := exec.QueryRowContext(ctx, sql, iD) 1042 1043 err := row.Scan(&exists) 1044 if err != nil { 1045 return false, errors.Wrap(err, "model2: unable to check if user exists") 1046 } 1047 1048 return exists, nil 1049 } 1050 1051 // Exists checks if the User row exists. 1052 func (o *User) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) { 1053 return UserExists(ctx, exec, o.ID) 1054 }