// Code generated by SQLBoiler 4.16.1 (https://github.com/volatiletech/sqlboiler). DO NOT EDIT. // This file is meant to be re-generated in place and/or deleted at any time. package models import ( "context" "database/sql" "fmt" "reflect" "strconv" "strings" "sync" "time" "github.com/friendsofgo/errors" "github.com/volatiletech/sqlboiler/v4/boil" "github.com/volatiletech/sqlboiler/v4/queries" "github.com/volatiletech/sqlboiler/v4/queries/qm" "github.com/volatiletech/sqlboiler/v4/queries/qmhelper" "github.com/volatiletech/strmangle" ) // Tag is an object representing the database table. type Tag struct { ID int `boil:"id" json:"id" toml:"id" yaml:"id"` Tag string `boil:"tag" json:"tag" toml:"tag" yaml:"tag"` Description string `boil:"description" json:"description" toml:"description" yaml:"description"` UserID int `boil:"user_id" json:"user_id" toml:"user_id" yaml:"user_id"` CreatedAt time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"` UpdatedAt time.Time `boil:"updated_at" json:"updated_at" toml:"updated_at" yaml:"updated_at"` R *tagR `boil:"-" json:"-" toml:"-" yaml:"-"` L tagL `boil:"-" json:"-" toml:"-" yaml:"-"` } var TagColumns = struct { ID string Tag string Description string UserID string CreatedAt string UpdatedAt string }{ ID: "id", Tag: "tag", Description: "description", UserID: "user_id", CreatedAt: "created_at", UpdatedAt: "updated_at", } var TagTableColumns = struct { ID string Tag string Description string UserID string CreatedAt string UpdatedAt string }{ ID: "tags.id", Tag: "tags.tag", Description: "tags.description", UserID: "tags.user_id", CreatedAt: "tags.created_at", UpdatedAt: "tags.updated_at", } // Generated where var TagWhere = struct { ID whereHelperint Tag whereHelperstring Description whereHelperstring UserID whereHelperint CreatedAt whereHelpertime_Time UpdatedAt whereHelpertime_Time }{ ID: whereHelperint{field: "\"tags\".\"id\""}, Tag: whereHelperstring{field: "\"tags\".\"tag\""}, Description: whereHelperstring{field: "\"tags\".\"description\""}, UserID: whereHelperint{field: "\"tags\".\"user_id\""}, CreatedAt: whereHelpertime_Time{field: "\"tags\".\"created_at\""}, UpdatedAt: whereHelpertime_Time{field: "\"tags\".\"updated_at\""}, } // TagRels is where relationship names are stored. var TagRels = struct { User string Posts string }{ User: "User", Posts: "Posts", } // tagR is where relationships are stored. type tagR struct { User *User `boil:"User" json:"User" toml:"User" yaml:"User"` Posts PostSlice `boil:"Posts" json:"Posts" toml:"Posts" yaml:"Posts"` } // NewStruct creates a new relationship struct func (*tagR) NewStruct() *tagR { return &tagR{} } func (r *tagR) GetUser() *User { if r == nil { return nil } return r.User } func (r *tagR) GetPosts() PostSlice { if r == nil { return nil } return r.Posts } // tagL is where Load methods for each relationship are stored. type tagL struct{} var ( tagAllColumns = []string{"id", "tag", "description", "user_id", "created_at", "updated_at"} tagColumnsWithoutDefault = []string{"tag", "description", "user_id"} tagColumnsWithDefault = []string{"id", "created_at", "updated_at"} tagPrimaryKeyColumns = []string{"id"} tagGeneratedColumns = []string{} ) type ( // TagSlice is an alias for a slice of pointers to Tag. // This should almost always be used instead of []Tag. TagSlice []*Tag // TagHook is the signature for custom Tag hook methods TagHook func(context.Context, boil.ContextExecutor, *Tag) error tagQuery struct { *queries.Query } ) // Cache for insert, update and upsert var ( tagType = reflect.TypeOf(&Tag{}) tagMapping = queries.MakeStructMapping(tagType) tagPrimaryKeyMapping, _ = queries.BindMapping(tagType, tagMapping, tagPrimaryKeyColumns) tagInsertCacheMut sync.RWMutex tagInsertCache = make(map[string]insertCache) tagUpdateCacheMut sync.RWMutex tagUpdateCache = make(map[string]updateCache) tagUpsertCacheMut sync.RWMutex tagUpsertCache = make(map[string]insertCache) ) var ( // Force time package dependency for automated UpdatedAt/CreatedAt. _ = time.Second // Force qmhelper dependency for where clause generation (which doesn't // always happen) _ = qmhelper.Where ) var tagAfterSelectMu sync.Mutex var tagAfterSelectHooks []TagHook var tagBeforeInsertMu sync.Mutex var tagBeforeInsertHooks []TagHook var tagAfterInsertMu sync.Mutex var tagAfterInsertHooks []TagHook var tagBeforeUpdateMu sync.Mutex var tagBeforeUpdateHooks []TagHook var tagAfterUpdateMu sync.Mutex var tagAfterUpdateHooks []TagHook var tagBeforeDeleteMu sync.Mutex var tagBeforeDeleteHooks []TagHook var tagAfterDeleteMu sync.Mutex var tagAfterDeleteHooks []TagHook var tagBeforeUpsertMu sync.Mutex var tagBeforeUpsertHooks []TagHook var tagAfterUpsertMu sync.Mutex var tagAfterUpsertHooks []TagHook // doAfterSelectHooks executes all "after Select" hooks. func (o *Tag) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagAfterSelectHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doBeforeInsertHooks executes all "before insert" hooks. func (o *Tag) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagBeforeInsertHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterInsertHooks executes all "after Insert" hooks. func (o *Tag) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagAfterInsertHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doBeforeUpdateHooks executes all "before Update" hooks. func (o *Tag) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagBeforeUpdateHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterUpdateHooks executes all "after Update" hooks. func (o *Tag) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagAfterUpdateHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doBeforeDeleteHooks executes all "before Delete" hooks. func (o *Tag) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagBeforeDeleteHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterDeleteHooks executes all "after Delete" hooks. func (o *Tag) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagAfterDeleteHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doBeforeUpsertHooks executes all "before Upsert" hooks. func (o *Tag) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagBeforeUpsertHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // doAfterUpsertHooks executes all "after Upsert" hooks. func (o *Tag) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { if boil.HooksAreSkipped(ctx) { return nil } for _, hook := range tagAfterUpsertHooks { if err := hook(ctx, exec, o); err != nil { return err } } return nil } // AddTagHook registers your hook function for all future operations. func AddTagHook(hookPoint boil.HookPoint, tagHook TagHook) { switch hookPoint { case boil.AfterSelectHook: tagAfterSelectMu.Lock() tagAfterSelectHooks = append(tagAfterSelectHooks, tagHook) tagAfterSelectMu.Unlock() case boil.BeforeInsertHook: tagBeforeInsertMu.Lock() tagBeforeInsertHooks = append(tagBeforeInsertHooks, tagHook) tagBeforeInsertMu.Unlock() case boil.AfterInsertHook: tagAfterInsertMu.Lock() tagAfterInsertHooks = append(tagAfterInsertHooks, tagHook) tagAfterInsertMu.Unlock() case boil.BeforeUpdateHook: tagBeforeUpdateMu.Lock() tagBeforeUpdateHooks = append(tagBeforeUpdateHooks, tagHook) tagBeforeUpdateMu.Unlock() case boil.AfterUpdateHook: tagAfterUpdateMu.Lock() tagAfterUpdateHooks = append(tagAfterUpdateHooks, tagHook) tagAfterUpdateMu.Unlock() case boil.BeforeDeleteHook: tagBeforeDeleteMu.Lock() tagBeforeDeleteHooks = append(tagBeforeDeleteHooks, tagHook) tagBeforeDeleteMu.Unlock() case boil.AfterDeleteHook: tagAfterDeleteMu.Lock() tagAfterDeleteHooks = append(tagAfterDeleteHooks, tagHook) tagAfterDeleteMu.Unlock() case boil.BeforeUpsertHook: tagBeforeUpsertMu.Lock() tagBeforeUpsertHooks = append(tagBeforeUpsertHooks, tagHook) tagBeforeUpsertMu.Unlock() case boil.AfterUpsertHook: tagAfterUpsertMu.Lock() tagAfterUpsertHooks = append(tagAfterUpsertHooks, tagHook) tagAfterUpsertMu.Unlock() } } // One returns a single tag record from the query. func (q tagQuery) One(ctx context.Context, exec boil.ContextExecutor) (*Tag, error) { o := &Tag{} queries.SetLimit(q.Query, 1) err := q.Bind(ctx, exec, o) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, sql.ErrNoRows } return nil, errors.Wrap(err, "models: failed to execute a one query for tags") } if err := o.doAfterSelectHooks(ctx, exec); err != nil { return o, err } return o, nil } // All returns all Tag records from the query. func (q tagQuery) All(ctx context.Context, exec boil.ContextExecutor) (TagSlice, error) { var o []*Tag err := q.Bind(ctx, exec, &o) if err != nil { return nil, errors.Wrap(err, "models: failed to assign all query results to Tag slice") } if len(tagAfterSelectHooks) != 0 { for _, obj := range o { if err := obj.doAfterSelectHooks(ctx, exec); err != nil { return o, err } } } return o, nil } // Count returns the count of all Tag records in the query. func (q tagQuery) Count(ctx context.Context, exec boil.ContextExecutor) (int64, error) { var count int64 queries.SetSelect(q.Query, nil) queries.SetCount(q.Query) err := q.Query.QueryRowContext(ctx, exec).Scan(&count) if err != nil { return 0, errors.Wrap(err, "models: failed to count tags rows") } return count, nil } // Exists checks if the row exists in the table. func (q tagQuery) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) { var count int64 queries.SetSelect(q.Query, nil) queries.SetCount(q.Query) queries.SetLimit(q.Query, 1) err := q.Query.QueryRowContext(ctx, exec).Scan(&count) if err != nil { return false, errors.Wrap(err, "models: failed to check if tags exists") } return count > 0, nil } // User pointed to by the foreign key. func (o *Tag) User(mods ...qm.QueryMod) userQuery { queryMods := []qm.QueryMod{ qm.Where("\"id\" = ?", o.UserID), } queryMods = append(queryMods, mods...) return Users(queryMods...) } // Posts retrieves all the post's Posts with an executor. func (o *Tag) Posts(mods ...qm.QueryMod) postQuery { var queryMods []qm.QueryMod if len(mods) != 0 { queryMods = append(queryMods, mods...) } queryMods = append(queryMods, qm.InnerJoin("\"post_tags\" on \"posts\".\"id\" = \"post_tags\".\"post_id\""), qm.Where("\"post_tags\".\"tag_id\"=?", o.ID), ) return Posts(queryMods...) } // LoadUser allows an eager lookup of values, cached into the // loaded structs of the objects. This is for an N-1 relationship. func (tagL) LoadUser(ctx context.Context, e boil.ContextExecutor, singular bool, maybeTag interface{}, mods queries.Applicator) error { var slice []*Tag var object *Tag if singular { var ok bool object, ok = maybeTag.(*Tag) if !ok { object = new(Tag) ok = queries.SetFromEmbeddedStruct(&object, &maybeTag) if !ok { return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeTag)) } } } else { s, ok := maybeTag.(*[]*Tag) if ok { slice = *s } else { ok = queries.SetFromEmbeddedStruct(&slice, maybeTag) if !ok { return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeTag)) } } } args := make(map[interface{}]struct{}) if singular { if object.R == nil { object.R = &tagR{} } args[object.UserID] = struct{}{} } else { for _, obj := range slice { if obj.R == nil { obj.R = &tagR{} } args[obj.UserID] = struct{}{} } } if len(args) == 0 { return nil } argsSlice := make([]interface{}, len(args)) i := 0 for arg := range args { argsSlice[i] = arg i++ } query := NewQuery( qm.From(`users`), qm.WhereIn(`users.id in ?`, argsSlice...), ) if mods != nil { mods.Apply(query) } results, err := query.QueryContext(ctx, e) if err != nil { return errors.Wrap(err, "failed to eager load User") } var resultSlice []*User if err = queries.Bind(results, &resultSlice); err != nil { return errors.Wrap(err, "failed to bind eager loaded slice User") } if err = results.Close(); err != nil { return errors.Wrap(err, "failed to close results of eager load for users") } if err = results.Err(); err != nil { return errors.Wrap(err, "error occurred during iteration of eager loaded relations for users") } if len(userAfterSelectHooks) != 0 { for _, obj := range resultSlice { if err := obj.doAfterSelectHooks(ctx, e); err != nil { return err } } } if len(resultSlice) == 0 { return nil } if singular { foreign := resultSlice[0] object.R.User = foreign if foreign.R == nil { foreign.R = &userR{} } foreign.R.Tags = append(foreign.R.Tags, object) return nil } for _, local := range slice { for _, foreign := range resultSlice { if local.UserID == foreign.ID { local.R.User = foreign if foreign.R == nil { foreign.R = &userR{} } foreign.R.Tags = append(foreign.R.Tags, local) break } } } return nil } // LoadPosts allows an eager lookup of values, cached into the // loaded structs of the objects. This is for a 1-M or N-M relationship. func (tagL) LoadPosts(ctx context.Context, e boil.ContextExecutor, singular bool, maybeTag interface{}, mods queries.Applicator) error { var slice []*Tag var object *Tag if singular { var ok bool object, ok = maybeTag.(*Tag) if !ok { object = new(Tag) ok = queries.SetFromEmbeddedStruct(&object, &maybeTag) if !ok { return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeTag)) } } } else { s, ok := maybeTag.(*[]*Tag) if ok { slice = *s } else { ok = queries.SetFromEmbeddedStruct(&slice, maybeTag) if !ok { return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeTag)) } } } args := make(map[interface{}]struct{}) if singular { if object.R == nil { object.R = &tagR{} } args[object.ID] = struct{}{} } else { for _, obj := range slice { if obj.R == nil { obj.R = &tagR{} } args[obj.ID] = struct{}{} } } if len(args) == 0 { return nil } argsSlice := make([]interface{}, len(args)) i := 0 for arg := range args { argsSlice[i] = arg i++ } query := NewQuery( qm.Select("\"posts\".\"id\", \"posts\".\"title\", \"posts\".\"description\", \"posts\".\"url\", \"posts\".\"user_id\", \"posts\".\"state\", \"posts\".\"created_at\", \"posts\".\"updated_at\", \"a\".\"tag_id\""), qm.From("\"posts\""), qm.InnerJoin("\"post_tags\" as \"a\" on \"posts\".\"id\" = \"a\".\"post_id\""), qm.WhereIn("\"a\".\"tag_id\" in ?", argsSlice...), ) if mods != nil { mods.Apply(query) } results, err := query.QueryContext(ctx, e) if err != nil { return errors.Wrap(err, "failed to eager load posts") } var resultSlice []*Post var localJoinCols []int for results.Next() { one := new(Post) var localJoinCol int err = results.Scan(&one.ID, &one.Title, &one.Description, &one.URL, &one.UserID, &one.State, &one.CreatedAt, &one.UpdatedAt, &localJoinCol) if err != nil { return errors.Wrap(err, "failed to scan eager loaded results for posts") } if err = results.Err(); err != nil { return errors.Wrap(err, "failed to plebian-bind eager loaded slice posts") } resultSlice = append(resultSlice, one) localJoinCols = append(localJoinCols, localJoinCol) } if err = results.Close(); err != nil { return errors.Wrap(err, "failed to close results in eager load on posts") } if err = results.Err(); err != nil { return errors.Wrap(err, "error occurred during iteration of eager loaded relations for posts") } if len(postAfterSelectHooks) != 0 { for _, obj := range resultSlice { if err := obj.doAfterSelectHooks(ctx, e); err != nil { return err } } } if singular { object.R.Posts = resultSlice for _, foreign := range resultSlice { if foreign.R == nil { foreign.R = &postR{} } foreign.R.Tags = append(foreign.R.Tags, object) } return nil } for i, foreign := range resultSlice { localJoinCol := localJoinCols[i] for _, local := range slice { if local.ID == localJoinCol { local.R.Posts = append(local.R.Posts, foreign) if foreign.R == nil { foreign.R = &postR{} } foreign.R.Tags = append(foreign.R.Tags, local) break } } } return nil } // SetUser of the tag to the related item. // Sets o.R.User to related. // Adds o to related.R.Tags. func (o *Tag) SetUser(ctx context.Context, exec boil.ContextExecutor, insert bool, related *User) error { var err error if insert { if err = related.Insert(ctx, exec, boil.Infer()); err != nil { return errors.Wrap(err, "failed to insert into foreign table") } } updateQuery := fmt.Sprintf( "UPDATE \"tags\" SET %s WHERE %s", strmangle.SetParamNames("\"", "\"", 1, []string{"user_id"}), strmangle.WhereClause("\"", "\"", 2, tagPrimaryKeyColumns), ) values := []interface{}{related.ID, o.ID} if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, updateQuery) fmt.Fprintln(writer, values) } if _, err = exec.ExecContext(ctx, updateQuery, values...); err != nil { return errors.Wrap(err, "failed to update local table") } o.UserID = related.ID if o.R == nil { o.R = &tagR{ User: related, } } else { o.R.User = related } if related.R == nil { related.R = &userR{ Tags: TagSlice{o}, } } else { related.R.Tags = append(related.R.Tags, o) } return nil } // AddPosts adds the given related objects to the existing relationships // of the tag, optionally inserting them as new records. // Appends related to o.R.Posts. // Sets related.R.Tags appropriately. func (o *Tag) AddPosts(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Post) error { var err error for _, rel := range related { if insert { if err = rel.Insert(ctx, exec, boil.Infer()); err != nil { return errors.Wrap(err, "failed to insert into foreign table") } } } for _, rel := range related { query := "insert into \"post_tags\" (\"tag_id\", \"post_id\") values ($1, $2)" values := []interface{}{o.ID, rel.ID} if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, query) fmt.Fprintln(writer, values) } _, err = exec.ExecContext(ctx, query, values...) if err != nil { return errors.Wrap(err, "failed to insert into join table") } } if o.R == nil { o.R = &tagR{ Posts: related, } } else { o.R.Posts = append(o.R.Posts, related...) } for _, rel := range related { if rel.R == nil { rel.R = &postR{ Tags: TagSlice{o}, } } else { rel.R.Tags = append(rel.R.Tags, o) } } return nil } // SetPosts removes all previously related items of the // tag replacing them completely with the passed // in related items, optionally inserting them as new records. // Sets o.R.Tags's Posts accordingly. // Replaces o.R.Posts with related. // Sets related.R.Tags's Posts accordingly. func (o *Tag) SetPosts(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Post) error { query := "delete from \"post_tags\" where \"tag_id\" = $1" values := []interface{}{o.ID} if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, query) fmt.Fprintln(writer, values) } _, err := exec.ExecContext(ctx, query, values...) if err != nil { return errors.Wrap(err, "failed to remove relationships before set") } removePostsFromTagsSlice(o, related) if o.R != nil { o.R.Posts = nil } return o.AddPosts(ctx, exec, insert, related...) } // RemovePosts relationships from objects passed in. // Removes related items from R.Posts (uses pointer comparison, removal does not keep order) // Sets related.R.Tags. func (o *Tag) RemovePosts(ctx context.Context, exec boil.ContextExecutor, related ...*Post) error { if len(related) == 0 { return nil } var err error query := fmt.Sprintf( "delete from \"post_tags\" where \"tag_id\" = $1 and \"post_id\" in (%s)", strmangle.Placeholders(dialect.UseIndexPlaceholders, len(related), 2, 1), ) values := []interface{}{o.ID} for _, rel := range related { values = append(values, rel.ID) } if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, query) fmt.Fprintln(writer, values) } _, err = exec.ExecContext(ctx, query, values...) if err != nil { return errors.Wrap(err, "failed to remove relationships before set") } removePostsFromTagsSlice(o, related) if o.R == nil { return nil } for _, rel := range related { for i, ri := range o.R.Posts { if rel != ri { continue } ln := len(o.R.Posts) if ln > 1 && i < ln-1 { o.R.Posts[i] = o.R.Posts[ln-1] } o.R.Posts = o.R.Posts[:ln-1] break } } return nil } func removePostsFromTagsSlice(o *Tag, related []*Post) { for _, rel := range related { if rel.R == nil { continue } for i, ri := range rel.R.Tags { if o.ID != ri.ID { continue } ln := len(rel.R.Tags) if ln > 1 && i < ln-1 { rel.R.Tags[i] = rel.R.Tags[ln-1] } rel.R.Tags = rel.R.Tags[:ln-1] break } } } // Tags retrieves all the records using an executor. func Tags(mods ...qm.QueryMod) tagQuery { mods = append(mods, qm.From("\"tags\"")) q := NewQuery(mods...) if len(queries.GetSelect(q)) == 0 { queries.SetSelect(q, []string{"\"tags\".*"}) } return tagQuery{q} } // FindTag retrieves a single record by ID with an executor. // If selectCols is empty Find will return all columns. func FindTag(ctx context.Context, exec boil.ContextExecutor, iD int, selectCols ...string) (*Tag, error) { tagObj := &Tag{} sel := "*" if len(selectCols) > 0 { sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") } query := fmt.Sprintf( "select %s from \"tags\" where \"id\"=$1", sel, ) q := queries.Raw(query, iD) err := q.Bind(ctx, exec, tagObj) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, sql.ErrNoRows } return nil, errors.Wrap(err, "models: unable to select from tags") } if err = tagObj.doAfterSelectHooks(ctx, exec); err != nil { return tagObj, err } return tagObj, nil } // Insert a single record using an executor. // See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. func (o *Tag) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { if o == nil { return errors.New("models: no tags provided for insertion") } var err error if !boil.TimestampsAreSkipped(ctx) { currTime := time.Now().In(boil.GetLocation()) if o.CreatedAt.IsZero() { o.CreatedAt = currTime } if o.UpdatedAt.IsZero() { o.UpdatedAt = currTime } } if err := o.doBeforeInsertHooks(ctx, exec); err != nil { return err } nzDefaults := queries.NonZeroDefaultSet(tagColumnsWithDefault, o) key := makeCacheKey(columns, nzDefaults) tagInsertCacheMut.RLock() cache, cached := tagInsertCache[key] tagInsertCacheMut.RUnlock() if !cached { wl, returnColumns := columns.InsertColumnSet( tagAllColumns, tagColumnsWithDefault, tagColumnsWithoutDefault, nzDefaults, ) cache.valueMapping, err = queries.BindMapping(tagType, tagMapping, wl) if err != nil { return err } cache.retMapping, err = queries.BindMapping(tagType, tagMapping, returnColumns) if err != nil { return err } if len(wl) != 0 { cache.query = fmt.Sprintf("INSERT INTO \"tags\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) } else { cache.query = "INSERT INTO \"tags\" %sDEFAULT VALUES%s" } var queryOutput, queryReturning string if len(cache.retMapping) != 0 { queryReturning = fmt.Sprintf(" RETURNING \"%s\"", strings.Join(returnColumns, "\",\"")) } cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning) } value := reflect.Indirect(reflect.ValueOf(o)) vals := queries.ValuesFromMapping(value, cache.valueMapping) if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, cache.query) fmt.Fprintln(writer, vals) } if len(cache.retMapping) != 0 { err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...) } else { _, err = exec.ExecContext(ctx, cache.query, vals...) } if err != nil { return errors.Wrap(err, "models: unable to insert into tags") } if !cached { tagInsertCacheMut.Lock() tagInsertCache[key] = cache tagInsertCacheMut.Unlock() } return o.doAfterInsertHooks(ctx, exec) } // Update uses an executor to update the Tag. // See boil.Columns.UpdateColumnSet documentation to understand column list inference for updates. // Update does not automatically update the record in case of default values. Use .Reload() to refresh the records. func (o *Tag) Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error) { if !boil.TimestampsAreSkipped(ctx) { currTime := time.Now().In(boil.GetLocation()) o.UpdatedAt = currTime } var err error if err = o.doBeforeUpdateHooks(ctx, exec); err != nil { return 0, err } key := makeCacheKey(columns, nil) tagUpdateCacheMut.RLock() cache, cached := tagUpdateCache[key] tagUpdateCacheMut.RUnlock() if !cached { wl := columns.UpdateColumnSet( tagAllColumns, tagPrimaryKeyColumns, ) if !columns.IsWhitelist() { wl = strmangle.SetComplement(wl, []string{"created_at"}) } if len(wl) == 0 { return 0, errors.New("models: unable to update tags, could not build whitelist") } cache.query = fmt.Sprintf("UPDATE \"tags\" SET %s WHERE %s", strmangle.SetParamNames("\"", "\"", 1, wl), strmangle.WhereClause("\"", "\"", len(wl)+1, tagPrimaryKeyColumns), ) cache.valueMapping, err = queries.BindMapping(tagType, tagMapping, append(wl, tagPrimaryKeyColumns...)) if err != nil { return 0, err } } values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping) if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, cache.query) fmt.Fprintln(writer, values) } var result sql.Result result, err = exec.ExecContext(ctx, cache.query, values...) if err != nil { return 0, errors.Wrap(err, "models: unable to update tags row") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: failed to get rows affected by update for tags") } if !cached { tagUpdateCacheMut.Lock() tagUpdateCache[key] = cache tagUpdateCacheMut.Unlock() } return rowsAff, o.doAfterUpdateHooks(ctx, exec) } // UpdateAll updates all rows with the specified column values. func (q tagQuery) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) { queries.SetUpdate(q.Query, cols) result, err := q.Query.ExecContext(ctx, exec) if err != nil { return 0, errors.Wrap(err, "models: unable to update all for tags") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: unable to retrieve rows affected for tags") } return rowsAff, nil } // UpdateAll updates all rows with the specified column values, using an executor. func (o TagSlice) UpdateAll(ctx context.Context, exec boil.ContextExecutor, cols M) (int64, error) { ln := int64(len(o)) if ln == 0 { return 0, nil } if len(cols) == 0 { return 0, errors.New("models: update all requires at least one column argument") } colNames := make([]string, len(cols)) args := make([]interface{}, len(cols)) i := 0 for name, value := range cols { colNames[i] = name args[i] = value i++ } // Append all of the primary key values for each column for _, obj := range o { pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), tagPrimaryKeyMapping) args = append(args, pkeyArgs...) } sql := fmt.Sprintf("UPDATE \"tags\" SET %s WHERE %s", strmangle.SetParamNames("\"", "\"", 1, colNames), strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), len(colNames)+1, tagPrimaryKeyColumns, len(o))) if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, sql) fmt.Fprintln(writer, args...) } result, err := exec.ExecContext(ctx, sql, args...) if err != nil { return 0, errors.Wrap(err, "models: unable to update all in tag slice") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all tag") } return rowsAff, nil } // Upsert attempts an insert using an executor, and does an update or ignore on conflict. // See boil.Columns documentation for how to properly use updateColumns and insertColumns. func (o *Tag) Upsert(ctx context.Context, exec boil.ContextExecutor, updateOnConflict bool, conflictColumns []string, updateColumns, insertColumns boil.Columns, opts ...UpsertOptionFunc) error { if o == nil { return errors.New("models: no tags provided for upsert") } if !boil.TimestampsAreSkipped(ctx) { currTime := time.Now().In(boil.GetLocation()) if o.CreatedAt.IsZero() { o.CreatedAt = currTime } o.UpdatedAt = currTime } if err := o.doBeforeUpsertHooks(ctx, exec); err != nil { return err } nzDefaults := queries.NonZeroDefaultSet(tagColumnsWithDefault, o) // Build cache key in-line uglily - mysql vs psql problems buf := strmangle.GetBuffer() if updateOnConflict { buf.WriteByte('t') } else { buf.WriteByte('f') } buf.WriteByte('.') for _, c := range conflictColumns { buf.WriteString(c) } buf.WriteByte('.') buf.WriteString(strconv.Itoa(updateColumns.Kind)) for _, c := range updateColumns.Cols { buf.WriteString(c) } buf.WriteByte('.') buf.WriteString(strconv.Itoa(insertColumns.Kind)) for _, c := range insertColumns.Cols { buf.WriteString(c) } buf.WriteByte('.') for _, c := range nzDefaults { buf.WriteString(c) } key := buf.String() strmangle.PutBuffer(buf) tagUpsertCacheMut.RLock() cache, cached := tagUpsertCache[key] tagUpsertCacheMut.RUnlock() var err error if !cached { insert, _ := insertColumns.InsertColumnSet( tagAllColumns, tagColumnsWithDefault, tagColumnsWithoutDefault, nzDefaults, ) update := updateColumns.UpdateColumnSet( tagAllColumns, tagPrimaryKeyColumns, ) if updateOnConflict && len(update) == 0 { return errors.New("models: unable to upsert tags, could not build update column list") } ret := strmangle.SetComplement(tagAllColumns, strmangle.SetIntersect(insert, update)) conflict := conflictColumns if len(conflict) == 0 && updateOnConflict && len(update) != 0 { if len(tagPrimaryKeyColumns) == 0 { return errors.New("models: unable to upsert tags, could not build conflict column list") } conflict = make([]string, len(tagPrimaryKeyColumns)) copy(conflict, tagPrimaryKeyColumns) } cache.query = buildUpsertQueryPostgres(dialect, "\"tags\"", updateOnConflict, ret, update, conflict, insert, opts...) cache.valueMapping, err = queries.BindMapping(tagType, tagMapping, insert) if err != nil { return err } if len(ret) != 0 { cache.retMapping, err = queries.BindMapping(tagType, tagMapping, ret) if err != nil { return err } } } value := reflect.Indirect(reflect.ValueOf(o)) vals := queries.ValuesFromMapping(value, cache.valueMapping) var returns []interface{} if len(cache.retMapping) != 0 { returns = queries.PtrsFromMapping(value, cache.retMapping) } if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, cache.query) fmt.Fprintln(writer, vals) } if len(cache.retMapping) != 0 { err = exec.QueryRowContext(ctx, cache.query, vals...).Scan(returns...) if errors.Is(err, sql.ErrNoRows) { err = nil // Postgres doesn't return anything when there's no update } } else { _, err = exec.ExecContext(ctx, cache.query, vals...) } if err != nil { return errors.Wrap(err, "models: unable to upsert tags") } if !cached { tagUpsertCacheMut.Lock() tagUpsertCache[key] = cache tagUpsertCacheMut.Unlock() } return o.doAfterUpsertHooks(ctx, exec) } // Delete deletes a single Tag record with an executor. // Delete will match against the primary key column to find the record to delete. func (o *Tag) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) { if o == nil { return 0, errors.New("models: no Tag provided for delete") } if err := o.doBeforeDeleteHooks(ctx, exec); err != nil { return 0, err } args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), tagPrimaryKeyMapping) sql := "DELETE FROM \"tags\" WHERE \"id\"=$1" if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, sql) fmt.Fprintln(writer, args...) } result, err := exec.ExecContext(ctx, sql, args...) if err != nil { return 0, errors.Wrap(err, "models: unable to delete from tags") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: failed to get rows affected by delete for tags") } if err := o.doAfterDeleteHooks(ctx, exec); err != nil { return 0, err } return rowsAff, nil } // DeleteAll deletes all matching rows. func (q tagQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { if q.Query == nil { return 0, errors.New("models: no tagQuery provided for delete all") } queries.SetDelete(q.Query) result, err := q.Query.ExecContext(ctx, exec) if err != nil { return 0, errors.Wrap(err, "models: unable to delete all from tags") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for tags") } return rowsAff, nil } // DeleteAll deletes all rows in the slice, using an executor. func (o TagSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { if len(o) == 0 { return 0, nil } if len(tagBeforeDeleteHooks) != 0 { for _, obj := range o { if err := obj.doBeforeDeleteHooks(ctx, exec); err != nil { return 0, err } } } var args []interface{} for _, obj := range o { pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), tagPrimaryKeyMapping) args = append(args, pkeyArgs...) } sql := "DELETE FROM \"tags\" WHERE " + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, tagPrimaryKeyColumns, len(o)) if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, sql) fmt.Fprintln(writer, args) } result, err := exec.ExecContext(ctx, sql, args...) if err != nil { return 0, errors.Wrap(err, "models: unable to delete all from tag slice") } rowsAff, err := result.RowsAffected() if err != nil { return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for tags") } if len(tagAfterDeleteHooks) != 0 { for _, obj := range o { if err := obj.doAfterDeleteHooks(ctx, exec); err != nil { return 0, err } } } return rowsAff, nil } // Reload refetches the object from the database // using the primary keys with an executor. func (o *Tag) Reload(ctx context.Context, exec boil.ContextExecutor) error { ret, err := FindTag(ctx, exec, o.ID) if err != nil { return err } *o = *ret return nil } // ReloadAll refetches every row with matching primary key column values // and overwrites the original object slice with the newly updated slice. func (o *TagSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error { if o == nil || len(*o) == 0 { return nil } slice := TagSlice{} var args []interface{} for _, obj := range *o { pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), tagPrimaryKeyMapping) args = append(args, pkeyArgs...) } sql := "SELECT \"tags\".* FROM \"tags\" WHERE " + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, tagPrimaryKeyColumns, len(*o)) q := queries.Raw(sql, args...) err := q.Bind(ctx, exec, &slice) if err != nil { return errors.Wrap(err, "models: unable to reload all in TagSlice") } *o = slice return nil } // TagExists checks if the Tag row exists. func TagExists(ctx context.Context, exec boil.ContextExecutor, iD int) (bool, error) { var exists bool sql := "select exists(select 1 from \"tags\" where \"id\"=$1 limit 1)" if boil.IsDebug(ctx) { writer := boil.DebugWriterFrom(ctx) fmt.Fprintln(writer, sql) fmt.Fprintln(writer, iD) } row := exec.QueryRowContext(ctx, sql, iD) err := row.Scan(&exists) if err != nil { return false, errors.Wrap(err, "models: unable to check if tags exists") } return exists, nil } // Exists checks if the Tag row exists. func (o *Tag) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) { return TagExists(ctx, exec, o.ID) }