diff options
Diffstat (limited to 'src/models/votes.go')
| -rw-r--r-- | src/models/votes.go | 1439 |
1 files changed, 1439 insertions, 0 deletions
diff --git a/src/models/votes.go b/src/models/votes.go new file mode 100644 index 0000000..4ce7fa9 --- /dev/null +++ b/src/models/votes.go @@ -0,0 +1,1439 @@ +// 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/null/v8" + "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" +) + +// Vote is an object representing the database table. +type Vote struct { + ID int `boil:"id" json:"id" toml:"id" yaml:"id"` + PostID null.Int `boil:"post_id" json:"post_id,omitempty" toml:"post_id" yaml:"post_id,omitempty"` + CommentID null.Int `boil:"comment_id" json:"comment_id,omitempty" toml:"comment_id" yaml:"comment_id,omitempty"` + UserID int `boil:"user_id" json:"user_id" toml:"user_id" yaml:"user_id"` + Inc int `boil:"inc" json:"inc" toml:"inc" yaml:"inc"` + 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 *voteR `boil:"-" json:"-" toml:"-" yaml:"-"` + L voteL `boil:"-" json:"-" toml:"-" yaml:"-"` +} + +var VoteColumns = struct { + ID string + PostID string + CommentID string + UserID string + Inc string + CreatedAt string + UpdatedAt string +}{ + ID: "id", + PostID: "post_id", + CommentID: "comment_id", + UserID: "user_id", + Inc: "inc", + CreatedAt: "created_at", + UpdatedAt: "updated_at", +} + +var VoteTableColumns = struct { + ID string + PostID string + CommentID string + UserID string + Inc string + CreatedAt string + UpdatedAt string +}{ + ID: "votes.id", + PostID: "votes.post_id", + CommentID: "votes.comment_id", + UserID: "votes.user_id", + Inc: "votes.inc", + CreatedAt: "votes.created_at", + UpdatedAt: "votes.updated_at", +} + +// Generated where + +var VoteWhere = struct { + ID whereHelperint + PostID whereHelpernull_Int + CommentID whereHelpernull_Int + UserID whereHelperint + Inc whereHelperint + CreatedAt whereHelpertime_Time + UpdatedAt whereHelpertime_Time +}{ + ID: whereHelperint{field: "\"votes\".\"id\""}, + PostID: whereHelpernull_Int{field: "\"votes\".\"post_id\""}, + CommentID: whereHelpernull_Int{field: "\"votes\".\"comment_id\""}, + UserID: whereHelperint{field: "\"votes\".\"user_id\""}, + Inc: whereHelperint{field: "\"votes\".\"inc\""}, + CreatedAt: whereHelpertime_Time{field: "\"votes\".\"created_at\""}, + UpdatedAt: whereHelpertime_Time{field: "\"votes\".\"updated_at\""}, +} + +// VoteRels is where relationship names are stored. +var VoteRels = struct { + Comment string + Post string +}{ + Comment: "Comment", + Post: "Post", +} + +// voteR is where relationships are stored. +type voteR struct { + Comment *Comment `boil:"Comment" json:"Comment" toml:"Comment" yaml:"Comment"` + Post *Post `boil:"Post" json:"Post" toml:"Post" yaml:"Post"` +} + +// NewStruct creates a new relationship struct +func (*voteR) NewStruct() *voteR { + return &voteR{} +} + +func (r *voteR) GetComment() *Comment { + if r == nil { + return nil + } + return r.Comment +} + +func (r *voteR) GetPost() *Post { + if r == nil { + return nil + } + return r.Post +} + +// voteL is where Load methods for each relationship are stored. +type voteL struct{} + +var ( + voteAllColumns = []string{"id", "post_id", "comment_id", "user_id", "inc", "created_at", "updated_at"} + voteColumnsWithoutDefault = []string{"user_id", "inc"} + voteColumnsWithDefault = []string{"id", "post_id", "comment_id", "created_at", "updated_at"} + votePrimaryKeyColumns = []string{"id"} + voteGeneratedColumns = []string{} +) + +type ( + // VoteSlice is an alias for a slice of pointers to Vote. + // This should almost always be used instead of []Vote. + VoteSlice []*Vote + // VoteHook is the signature for custom Vote hook methods + VoteHook func(context.Context, boil.ContextExecutor, *Vote) error + + voteQuery struct { + *queries.Query + } +) + +// Cache for insert, update and upsert +var ( + voteType = reflect.TypeOf(&Vote{}) + voteMapping = queries.MakeStructMapping(voteType) + votePrimaryKeyMapping, _ = queries.BindMapping(voteType, voteMapping, votePrimaryKeyColumns) + voteInsertCacheMut sync.RWMutex + voteInsertCache = make(map[string]insertCache) + voteUpdateCacheMut sync.RWMutex + voteUpdateCache = make(map[string]updateCache) + voteUpsertCacheMut sync.RWMutex + voteUpsertCache = 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 voteAfterSelectMu sync.Mutex +var voteAfterSelectHooks []VoteHook + +var voteBeforeInsertMu sync.Mutex +var voteBeforeInsertHooks []VoteHook +var voteAfterInsertMu sync.Mutex +var voteAfterInsertHooks []VoteHook + +var voteBeforeUpdateMu sync.Mutex +var voteBeforeUpdateHooks []VoteHook +var voteAfterUpdateMu sync.Mutex +var voteAfterUpdateHooks []VoteHook + +var voteBeforeDeleteMu sync.Mutex +var voteBeforeDeleteHooks []VoteHook +var voteAfterDeleteMu sync.Mutex +var voteAfterDeleteHooks []VoteHook + +var voteBeforeUpsertMu sync.Mutex +var voteBeforeUpsertHooks []VoteHook +var voteAfterUpsertMu sync.Mutex +var voteAfterUpsertHooks []VoteHook + +// doAfterSelectHooks executes all "after Select" hooks. +func (o *Vote) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteAfterSelectHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeInsertHooks executes all "before insert" hooks. +func (o *Vote) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteBeforeInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterInsertHooks executes all "after Insert" hooks. +func (o *Vote) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteAfterInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpdateHooks executes all "before Update" hooks. +func (o *Vote) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteBeforeUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpdateHooks executes all "after Update" hooks. +func (o *Vote) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteAfterUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeDeleteHooks executes all "before Delete" hooks. +func (o *Vote) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteBeforeDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterDeleteHooks executes all "after Delete" hooks. +func (o *Vote) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteAfterDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpsertHooks executes all "before Upsert" hooks. +func (o *Vote) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteBeforeUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpsertHooks executes all "after Upsert" hooks. +func (o *Vote) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range voteAfterUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// AddVoteHook registers your hook function for all future operations. +func AddVoteHook(hookPoint boil.HookPoint, voteHook VoteHook) { + switch hookPoint { + case boil.AfterSelectHook: + voteAfterSelectMu.Lock() + voteAfterSelectHooks = append(voteAfterSelectHooks, voteHook) + voteAfterSelectMu.Unlock() + case boil.BeforeInsertHook: + voteBeforeInsertMu.Lock() + voteBeforeInsertHooks = append(voteBeforeInsertHooks, voteHook) + voteBeforeInsertMu.Unlock() + case boil.AfterInsertHook: + voteAfterInsertMu.Lock() + voteAfterInsertHooks = append(voteAfterInsertHooks, voteHook) + voteAfterInsertMu.Unlock() + case boil.BeforeUpdateHook: + voteBeforeUpdateMu.Lock() + voteBeforeUpdateHooks = append(voteBeforeUpdateHooks, voteHook) + voteBeforeUpdateMu.Unlock() + case boil.AfterUpdateHook: + voteAfterUpdateMu.Lock() + voteAfterUpdateHooks = append(voteAfterUpdateHooks, voteHook) + voteAfterUpdateMu.Unlock() + case boil.BeforeDeleteHook: + voteBeforeDeleteMu.Lock() + voteBeforeDeleteHooks = append(voteBeforeDeleteHooks, voteHook) + voteBeforeDeleteMu.Unlock() + case boil.AfterDeleteHook: + voteAfterDeleteMu.Lock() + voteAfterDeleteHooks = append(voteAfterDeleteHooks, voteHook) + voteAfterDeleteMu.Unlock() + case boil.BeforeUpsertHook: + voteBeforeUpsertMu.Lock() + voteBeforeUpsertHooks = append(voteBeforeUpsertHooks, voteHook) + voteBeforeUpsertMu.Unlock() + case boil.AfterUpsertHook: + voteAfterUpsertMu.Lock() + voteAfterUpsertHooks = append(voteAfterUpsertHooks, voteHook) + voteAfterUpsertMu.Unlock() + } +} + +// One returns a single vote record from the query. +func (q voteQuery) One(ctx context.Context, exec boil.ContextExecutor) (*Vote, error) { + o := &Vote{} + + 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 votes") + } + + if err := o.doAfterSelectHooks(ctx, exec); err != nil { + return o, err + } + + return o, nil +} + +// All returns all Vote records from the query. +func (q voteQuery) All(ctx context.Context, exec boil.ContextExecutor) (VoteSlice, error) { + var o []*Vote + + err := q.Bind(ctx, exec, &o) + if err != nil { + return nil, errors.Wrap(err, "models: failed to assign all query results to Vote slice") + } + + if len(voteAfterSelectHooks) != 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 Vote records in the query. +func (q voteQuery) 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 votes rows") + } + + return count, nil +} + +// Exists checks if the row exists in the table. +func (q voteQuery) 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 votes exists") + } + + return count > 0, nil +} + +// Comment pointed to by the foreign key. +func (o *Vote) Comment(mods ...qm.QueryMod) commentQuery { + queryMods := []qm.QueryMod{ + qm.Where("\"id\" = ?", o.CommentID), + } + + queryMods = append(queryMods, mods...) + + return Comments(queryMods...) +} + +// Post pointed to by the foreign key. +func (o *Vote) Post(mods ...qm.QueryMod) postQuery { + queryMods := []qm.QueryMod{ + qm.Where("\"id\" = ?", o.PostID), + } + + queryMods = append(queryMods, mods...) + + return Posts(queryMods...) +} + +// LoadComment allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for an N-1 relationship. +func (voteL) LoadComment(ctx context.Context, e boil.ContextExecutor, singular bool, maybeVote interface{}, mods queries.Applicator) error { + var slice []*Vote + var object *Vote + + if singular { + var ok bool + object, ok = maybeVote.(*Vote) + if !ok { + object = new(Vote) + ok = queries.SetFromEmbeddedStruct(&object, &maybeVote) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeVote)) + } + } + } else { + s, ok := maybeVote.(*[]*Vote) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeVote) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeVote)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &voteR{} + } + if !queries.IsNil(object.CommentID) { + args[object.CommentID] = struct{}{} + } + + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &voteR{} + } + + if !queries.IsNil(obj.CommentID) { + args[obj.CommentID] = 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(`comments`), + qm.WhereIn(`comments.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 Comment") + } + + var resultSlice []*Comment + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice Comment") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results of eager load for comments") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for comments") + } + + if len(commentAfterSelectHooks) != 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.Comment = foreign + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.Votes = append(foreign.R.Votes, object) + return nil + } + + for _, local := range slice { + for _, foreign := range resultSlice { + if queries.Equal(local.CommentID, foreign.ID) { + local.R.Comment = foreign + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.Votes = append(foreign.R.Votes, local) + break + } + } + } + + return nil +} + +// LoadPost allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for an N-1 relationship. +func (voteL) LoadPost(ctx context.Context, e boil.ContextExecutor, singular bool, maybeVote interface{}, mods queries.Applicator) error { + var slice []*Vote + var object *Vote + + if singular { + var ok bool + object, ok = maybeVote.(*Vote) + if !ok { + object = new(Vote) + ok = queries.SetFromEmbeddedStruct(&object, &maybeVote) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeVote)) + } + } + } else { + s, ok := maybeVote.(*[]*Vote) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeVote) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeVote)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &voteR{} + } + if !queries.IsNil(object.PostID) { + args[object.PostID] = struct{}{} + } + + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &voteR{} + } + + if !queries.IsNil(obj.PostID) { + args[obj.PostID] = 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(`posts`), + qm.WhereIn(`posts.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 Post") + } + + var resultSlice []*Post + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice Post") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results of eager load for 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 len(resultSlice) == 0 { + return nil + } + + if singular { + foreign := resultSlice[0] + object.R.Post = foreign + if foreign.R == nil { + foreign.R = &postR{} + } + foreign.R.Votes = append(foreign.R.Votes, object) + return nil + } + + for _, local := range slice { + for _, foreign := range resultSlice { + if queries.Equal(local.PostID, foreign.ID) { + local.R.Post = foreign + if foreign.R == nil { + foreign.R = &postR{} + } + foreign.R.Votes = append(foreign.R.Votes, local) + break + } + } + } + + return nil +} + +// SetComment of the vote to the related item. +// Sets o.R.Comment to related. +// Adds o to related.R.Votes. +func (o *Vote) SetComment(ctx context.Context, exec boil.ContextExecutor, insert bool, related *Comment) 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 \"votes\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"comment_id"}), + strmangle.WhereClause("\"", "\"", 2, votePrimaryKeyColumns), + ) + 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") + } + + queries.Assign(&o.CommentID, related.ID) + if o.R == nil { + o.R = &voteR{ + Comment: related, + } + } else { + o.R.Comment = related + } + + if related.R == nil { + related.R = &commentR{ + Votes: VoteSlice{o}, + } + } else { + related.R.Votes = append(related.R.Votes, o) + } + + return nil +} + +// RemoveComment relationship. +// Sets o.R.Comment to nil. +// Removes o from all passed in related items' relationships struct. +func (o *Vote) RemoveComment(ctx context.Context, exec boil.ContextExecutor, related *Comment) error { + var err error + + queries.SetScanner(&o.CommentID, nil) + if _, err = o.Update(ctx, exec, boil.Whitelist("comment_id")); err != nil { + return errors.Wrap(err, "failed to update local table") + } + + if o.R != nil { + o.R.Comment = nil + } + if related == nil || related.R == nil { + return nil + } + + for i, ri := range related.R.Votes { + if queries.Equal(o.CommentID, ri.CommentID) { + continue + } + + ln := len(related.R.Votes) + if ln > 1 && i < ln-1 { + related.R.Votes[i] = related.R.Votes[ln-1] + } + related.R.Votes = related.R.Votes[:ln-1] + break + } + return nil +} + +// SetPost of the vote to the related item. +// Sets o.R.Post to related. +// Adds o to related.R.Votes. +func (o *Vote) SetPost(ctx context.Context, exec boil.ContextExecutor, insert bool, related *Post) 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 \"votes\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"post_id"}), + strmangle.WhereClause("\"", "\"", 2, votePrimaryKeyColumns), + ) + 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") + } + + queries.Assign(&o.PostID, related.ID) + if o.R == nil { + o.R = &voteR{ + Post: related, + } + } else { + o.R.Post = related + } + + if related.R == nil { + related.R = &postR{ + Votes: VoteSlice{o}, + } + } else { + related.R.Votes = append(related.R.Votes, o) + } + + return nil +} + +// RemovePost relationship. +// Sets o.R.Post to nil. +// Removes o from all passed in related items' relationships struct. +func (o *Vote) RemovePost(ctx context.Context, exec boil.ContextExecutor, related *Post) error { + var err error + + queries.SetScanner(&o.PostID, nil) + if _, err = o.Update(ctx, exec, boil.Whitelist("post_id")); err != nil { + return errors.Wrap(err, "failed to update local table") + } + + if o.R != nil { + o.R.Post = nil + } + if related == nil || related.R == nil { + return nil + } + + for i, ri := range related.R.Votes { + if queries.Equal(o.PostID, ri.PostID) { + continue + } + + ln := len(related.R.Votes) + if ln > 1 && i < ln-1 { + related.R.Votes[i] = related.R.Votes[ln-1] + } + related.R.Votes = related.R.Votes[:ln-1] + break + } + return nil +} + +// Votes retrieves all the records using an executor. +func Votes(mods ...qm.QueryMod) voteQuery { + mods = append(mods, qm.From("\"votes\"")) + q := NewQuery(mods...) + if len(queries.GetSelect(q)) == 0 { + queries.SetSelect(q, []string{"\"votes\".*"}) + } + + return voteQuery{q} +} + +// FindVote retrieves a single record by ID with an executor. +// If selectCols is empty Find will return all columns. +func FindVote(ctx context.Context, exec boil.ContextExecutor, iD int, selectCols ...string) (*Vote, error) { + voteObj := &Vote{} + + sel := "*" + if len(selectCols) > 0 { + sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") + } + query := fmt.Sprintf( + "select %s from \"votes\" where \"id\"=$1", sel, + ) + + q := queries.Raw(query, iD) + + err := q.Bind(ctx, exec, voteObj) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, sql.ErrNoRows + } + return nil, errors.Wrap(err, "models: unable to select from votes") + } + + if err = voteObj.doAfterSelectHooks(ctx, exec); err != nil { + return voteObj, err + } + + return voteObj, nil +} + +// Insert a single record using an executor. +// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. +func (o *Vote) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { + if o == nil { + return errors.New("models: no votes 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(voteColumnsWithDefault, o) + + key := makeCacheKey(columns, nzDefaults) + voteInsertCacheMut.RLock() + cache, cached := voteInsertCache[key] + voteInsertCacheMut.RUnlock() + + if !cached { + wl, returnColumns := columns.InsertColumnSet( + voteAllColumns, + voteColumnsWithDefault, + voteColumnsWithoutDefault, + nzDefaults, + ) + + cache.valueMapping, err = queries.BindMapping(voteType, voteMapping, wl) + if err != nil { + return err + } + cache.retMapping, err = queries.BindMapping(voteType, voteMapping, returnColumns) + if err != nil { + return err + } + if len(wl) != 0 { + cache.query = fmt.Sprintf("INSERT INTO \"votes\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) + } else { + cache.query = "INSERT INTO \"votes\" %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 votes") + } + + if !cached { + voteInsertCacheMut.Lock() + voteInsertCache[key] = cache + voteInsertCacheMut.Unlock() + } + + return o.doAfterInsertHooks(ctx, exec) +} + +// Update uses an executor to update the Vote. +// 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 *Vote) 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) + voteUpdateCacheMut.RLock() + cache, cached := voteUpdateCache[key] + voteUpdateCacheMut.RUnlock() + + if !cached { + wl := columns.UpdateColumnSet( + voteAllColumns, + votePrimaryKeyColumns, + ) + + if !columns.IsWhitelist() { + wl = strmangle.SetComplement(wl, []string{"created_at"}) + } + if len(wl) == 0 { + return 0, errors.New("models: unable to update votes, could not build whitelist") + } + + cache.query = fmt.Sprintf("UPDATE \"votes\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, wl), + strmangle.WhereClause("\"", "\"", len(wl)+1, votePrimaryKeyColumns), + ) + cache.valueMapping, err = queries.BindMapping(voteType, voteMapping, append(wl, votePrimaryKeyColumns...)) + 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 votes row") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by update for votes") + } + + if !cached { + voteUpdateCacheMut.Lock() + voteUpdateCache[key] = cache + voteUpdateCacheMut.Unlock() + } + + return rowsAff, o.doAfterUpdateHooks(ctx, exec) +} + +// UpdateAll updates all rows with the specified column values. +func (q voteQuery) 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 votes") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected for votes") + } + + return rowsAff, nil +} + +// UpdateAll updates all rows with the specified column values, using an executor. +func (o VoteSlice) 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)), votePrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := fmt.Sprintf("UPDATE \"votes\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, colNames), + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), len(colNames)+1, votePrimaryKeyColumns, 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 vote slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all vote") + } + 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 *Vote) 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 votes 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(voteColumnsWithDefault, 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) + + voteUpsertCacheMut.RLock() + cache, cached := voteUpsertCache[key] + voteUpsertCacheMut.RUnlock() + + var err error + + if !cached { + insert, _ := insertColumns.InsertColumnSet( + voteAllColumns, + voteColumnsWithDefault, + voteColumnsWithoutDefault, + nzDefaults, + ) + + update := updateColumns.UpdateColumnSet( + voteAllColumns, + votePrimaryKeyColumns, + ) + + if updateOnConflict && len(update) == 0 { + return errors.New("models: unable to upsert votes, could not build update column list") + } + + ret := strmangle.SetComplement(voteAllColumns, strmangle.SetIntersect(insert, update)) + + conflict := conflictColumns + if len(conflict) == 0 && updateOnConflict && len(update) != 0 { + if len(votePrimaryKeyColumns) == 0 { + return errors.New("models: unable to upsert votes, could not build conflict column list") + } + + conflict = make([]string, len(votePrimaryKeyColumns)) + copy(conflict, votePrimaryKeyColumns) + } + cache.query = buildUpsertQueryPostgres(dialect, "\"votes\"", updateOnConflict, ret, update, conflict, insert, opts...) + + cache.valueMapping, err = queries.BindMapping(voteType, voteMapping, insert) + if err != nil { + return err + } + if len(ret) != 0 { + cache.retMapping, err = queries.BindMapping(voteType, voteMapping, 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 votes") + } + + if !cached { + voteUpsertCacheMut.Lock() + voteUpsertCache[key] = cache + voteUpsertCacheMut.Unlock() + } + + return o.doAfterUpsertHooks(ctx, exec) +} + +// Delete deletes a single Vote record with an executor. +// Delete will match against the primary key column to find the record to delete. +func (o *Vote) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if o == nil { + return 0, errors.New("models: no Vote provided for delete") + } + + if err := o.doBeforeDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), votePrimaryKeyMapping) + sql := "DELETE FROM \"votes\" 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 votes") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by delete for votes") + } + + if err := o.doAfterDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + return rowsAff, nil +} + +// DeleteAll deletes all matching rows. +func (q voteQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if q.Query == nil { + return 0, errors.New("models: no voteQuery 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 votes") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for votes") + } + + return rowsAff, nil +} + +// DeleteAll deletes all rows in the slice, using an executor. +func (o VoteSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if len(o) == 0 { + return 0, nil + } + + if len(voteBeforeDeleteHooks) != 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)), votePrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "DELETE FROM \"votes\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, votePrimaryKeyColumns, 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 vote slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for votes") + } + + if len(voteAfterDeleteHooks) != 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 *Vote) Reload(ctx context.Context, exec boil.ContextExecutor) error { + ret, err := FindVote(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 *VoteSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error { + if o == nil || len(*o) == 0 { + return nil + } + + slice := VoteSlice{} + var args []interface{} + for _, obj := range *o { + pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), votePrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "SELECT \"votes\".* FROM \"votes\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, votePrimaryKeyColumns, 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 VoteSlice") + } + + *o = slice + + return nil +} + +// VoteExists checks if the Vote row exists. +func VoteExists(ctx context.Context, exec boil.ContextExecutor, iD int) (bool, error) { + var exists bool + sql := "select exists(select 1 from \"votes\" 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 votes exists") + } + + return exists, nil +} + +// Exists checks if the Vote row exists. +func (o *Vote) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) { + return VoteExists(ctx, exec, o.ID) +} |
