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