diff options
Diffstat (limited to 'src/models/comments.go')
| -rw-r--r-- | src/models/comments.go | 2284 |
1 files changed, 2284 insertions, 0 deletions
diff --git a/src/models/comments.go b/src/models/comments.go new file mode 100644 index 0000000..f3c0f94 --- /dev/null +++ b/src/models/comments.go @@ -0,0 +1,2284 @@ +// 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" +) + +// Comment is an object representing the database table. +type Comment struct { + ID int `boil:"id" json:"id" toml:"id" yaml:"id"` + Comment string `boil:"comment" json:"comment" toml:"comment" yaml:"comment"` + UserID int `boil:"user_id" json:"user_id" toml:"user_id" yaml:"user_id"` + PostID int `boil:"post_id" json:"post_id" toml:"post_id" yaml:"post_id"` + ReplyID null.Int `boil:"reply_id" json:"reply_id,omitempty" toml:"reply_id" yaml:"reply_id,omitempty"` + State null.String `boil:"state" json:"state,omitempty" toml:"state" yaml:"state,omitempty"` + 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 *commentR `boil:"-" json:"-" toml:"-" yaml:"-"` + L commentL `boil:"-" json:"-" toml:"-" yaml:"-"` +} + +var CommentColumns = struct { + ID string + Comment string + UserID string + PostID string + ReplyID string + State string + CreatedAt string + UpdatedAt string +}{ + ID: "id", + Comment: "comment", + UserID: "user_id", + PostID: "post_id", + ReplyID: "reply_id", + State: "state", + CreatedAt: "created_at", + UpdatedAt: "updated_at", +} + +var CommentTableColumns = struct { + ID string + Comment string + UserID string + PostID string + ReplyID string + State string + CreatedAt string + UpdatedAt string +}{ + ID: "comments.id", + Comment: "comments.comment", + UserID: "comments.user_id", + PostID: "comments.post_id", + ReplyID: "comments.reply_id", + State: "comments.state", + CreatedAt: "comments.created_at", + UpdatedAt: "comments.updated_at", +} + +// Generated where + +type whereHelperint struct{ field string } + +func (w whereHelperint) EQ(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) } +func (w whereHelperint) NEQ(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.NEQ, x) } +func (w whereHelperint) LT(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) } +func (w whereHelperint) LTE(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LTE, x) } +func (w whereHelperint) GT(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) } +func (w whereHelperint) GTE(x int) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GTE, x) } +func (w whereHelperint) IN(slice []int) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) +} +func (w whereHelperint) NIN(slice []int) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) +} + +type whereHelperstring struct{ field string } + +func (w whereHelperstring) EQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.EQ, x) } +func (w whereHelperstring) NEQ(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.NEQ, x) } +func (w whereHelperstring) LT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LT, x) } +func (w whereHelperstring) LTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.LTE, x) } +func (w whereHelperstring) GT(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GT, x) } +func (w whereHelperstring) GTE(x string) qm.QueryMod { return qmhelper.Where(w.field, qmhelper.GTE, x) } +func (w whereHelperstring) LIKE(x string) qm.QueryMod { return qm.Where(w.field+" LIKE ?", x) } +func (w whereHelperstring) NLIKE(x string) qm.QueryMod { return qm.Where(w.field+" NOT LIKE ?", x) } +func (w whereHelperstring) ILIKE(x string) qm.QueryMod { return qm.Where(w.field+" ILIKE ?", x) } +func (w whereHelperstring) NILIKE(x string) qm.QueryMod { return qm.Where(w.field+" NOT ILIKE ?", x) } +func (w whereHelperstring) IN(slice []string) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) +} +func (w whereHelperstring) NIN(slice []string) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) +} + +type whereHelpernull_Int struct{ field string } + +func (w whereHelpernull_Int) EQ(x null.Int) qm.QueryMod { + return qmhelper.WhereNullEQ(w.field, false, x) +} +func (w whereHelpernull_Int) NEQ(x null.Int) qm.QueryMod { + return qmhelper.WhereNullEQ(w.field, true, x) +} +func (w whereHelpernull_Int) LT(x null.Int) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.LT, x) +} +func (w whereHelpernull_Int) LTE(x null.Int) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.LTE, x) +} +func (w whereHelpernull_Int) GT(x null.Int) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.GT, x) +} +func (w whereHelpernull_Int) GTE(x null.Int) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.GTE, x) +} +func (w whereHelpernull_Int) IN(slice []int) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) +} +func (w whereHelpernull_Int) NIN(slice []int) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) +} + +func (w whereHelpernull_Int) IsNull() qm.QueryMod { return qmhelper.WhereIsNull(w.field) } +func (w whereHelpernull_Int) IsNotNull() qm.QueryMod { return qmhelper.WhereIsNotNull(w.field) } + +type whereHelpernull_String struct{ field string } + +func (w whereHelpernull_String) EQ(x null.String) qm.QueryMod { + return qmhelper.WhereNullEQ(w.field, false, x) +} +func (w whereHelpernull_String) NEQ(x null.String) qm.QueryMod { + return qmhelper.WhereNullEQ(w.field, true, x) +} +func (w whereHelpernull_String) LT(x null.String) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.LT, x) +} +func (w whereHelpernull_String) LTE(x null.String) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.LTE, x) +} +func (w whereHelpernull_String) GT(x null.String) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.GT, x) +} +func (w whereHelpernull_String) GTE(x null.String) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.GTE, x) +} +func (w whereHelpernull_String) LIKE(x null.String) qm.QueryMod { + return qm.Where(w.field+" LIKE ?", x) +} +func (w whereHelpernull_String) NLIKE(x null.String) qm.QueryMod { + return qm.Where(w.field+" NOT LIKE ?", x) +} +func (w whereHelpernull_String) ILIKE(x null.String) qm.QueryMod { + return qm.Where(w.field+" ILIKE ?", x) +} +func (w whereHelpernull_String) NILIKE(x null.String) qm.QueryMod { + return qm.Where(w.field+" NOT ILIKE ?", x) +} +func (w whereHelpernull_String) IN(slice []string) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereIn(fmt.Sprintf("%s IN ?", w.field), values...) +} +func (w whereHelpernull_String) NIN(slice []string) qm.QueryMod { + values := make([]interface{}, 0, len(slice)) + for _, value := range slice { + values = append(values, value) + } + return qm.WhereNotIn(fmt.Sprintf("%s NOT IN ?", w.field), values...) +} + +func (w whereHelpernull_String) IsNull() qm.QueryMod { return qmhelper.WhereIsNull(w.field) } +func (w whereHelpernull_String) IsNotNull() qm.QueryMod { return qmhelper.WhereIsNotNull(w.field) } + +type whereHelpertime_Time struct{ field string } + +func (w whereHelpertime_Time) EQ(x time.Time) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.EQ, x) +} +func (w whereHelpertime_Time) NEQ(x time.Time) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.NEQ, x) +} +func (w whereHelpertime_Time) LT(x time.Time) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.LT, x) +} +func (w whereHelpertime_Time) LTE(x time.Time) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.LTE, x) +} +func (w whereHelpertime_Time) GT(x time.Time) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.GT, x) +} +func (w whereHelpertime_Time) GTE(x time.Time) qm.QueryMod { + return qmhelper.Where(w.field, qmhelper.GTE, x) +} + +var CommentWhere = struct { + ID whereHelperint + Comment whereHelperstring + UserID whereHelperint + PostID whereHelperint + ReplyID whereHelpernull_Int + State whereHelpernull_String + CreatedAt whereHelpertime_Time + UpdatedAt whereHelpertime_Time +}{ + ID: whereHelperint{field: "\"comments\".\"id\""}, + Comment: whereHelperstring{field: "\"comments\".\"comment\""}, + UserID: whereHelperint{field: "\"comments\".\"user_id\""}, + PostID: whereHelperint{field: "\"comments\".\"post_id\""}, + ReplyID: whereHelpernull_Int{field: "\"comments\".\"reply_id\""}, + State: whereHelpernull_String{field: "\"comments\".\"state\""}, + CreatedAt: whereHelpertime_Time{field: "\"comments\".\"created_at\""}, + UpdatedAt: whereHelpertime_Time{field: "\"comments\".\"updated_at\""}, +} + +// CommentRels is where relationship names are stored. +var CommentRels = struct { + Post string + Reply string + User string + ReplyComments string + Votes string +}{ + Post: "Post", + Reply: "Reply", + User: "User", + ReplyComments: "ReplyComments", + Votes: "Votes", +} + +// commentR is where relationships are stored. +type commentR struct { + Post *Post `boil:"Post" json:"Post" toml:"Post" yaml:"Post"` + Reply *Comment `boil:"Reply" json:"Reply" toml:"Reply" yaml:"Reply"` + User *User `boil:"User" json:"User" toml:"User" yaml:"User"` + ReplyComments CommentSlice `boil:"ReplyComments" json:"ReplyComments" toml:"ReplyComments" yaml:"ReplyComments"` + Votes VoteSlice `boil:"Votes" json:"Votes" toml:"Votes" yaml:"Votes"` +} + +// NewStruct creates a new relationship struct +func (*commentR) NewStruct() *commentR { + return &commentR{} +} + +func (r *commentR) GetPost() *Post { + if r == nil { + return nil + } + return r.Post +} + +func (r *commentR) GetReply() *Comment { + if r == nil { + return nil + } + return r.Reply +} + +func (r *commentR) GetUser() *User { + if r == nil { + return nil + } + return r.User +} + +func (r *commentR) GetReplyComments() CommentSlice { + if r == nil { + return nil + } + return r.ReplyComments +} + +func (r *commentR) GetVotes() VoteSlice { + if r == nil { + return nil + } + return r.Votes +} + +// commentL is where Load methods for each relationship are stored. +type commentL struct{} + +var ( + commentAllColumns = []string{"id", "comment", "user_id", "post_id", "reply_id", "state", "created_at", "updated_at"} + commentColumnsWithoutDefault = []string{"comment", "user_id", "post_id"} + commentColumnsWithDefault = []string{"id", "reply_id", "state", "created_at", "updated_at"} + commentPrimaryKeyColumns = []string{"id"} + commentGeneratedColumns = []string{} +) + +type ( + // CommentSlice is an alias for a slice of pointers to Comment. + // This should almost always be used instead of []Comment. + CommentSlice []*Comment + // CommentHook is the signature for custom Comment hook methods + CommentHook func(context.Context, boil.ContextExecutor, *Comment) error + + commentQuery struct { + *queries.Query + } +) + +// Cache for insert, update and upsert +var ( + commentType = reflect.TypeOf(&Comment{}) + commentMapping = queries.MakeStructMapping(commentType) + commentPrimaryKeyMapping, _ = queries.BindMapping(commentType, commentMapping, commentPrimaryKeyColumns) + commentInsertCacheMut sync.RWMutex + commentInsertCache = make(map[string]insertCache) + commentUpdateCacheMut sync.RWMutex + commentUpdateCache = make(map[string]updateCache) + commentUpsertCacheMut sync.RWMutex + commentUpsertCache = 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 commentAfterSelectMu sync.Mutex +var commentAfterSelectHooks []CommentHook + +var commentBeforeInsertMu sync.Mutex +var commentBeforeInsertHooks []CommentHook +var commentAfterInsertMu sync.Mutex +var commentAfterInsertHooks []CommentHook + +var commentBeforeUpdateMu sync.Mutex +var commentBeforeUpdateHooks []CommentHook +var commentAfterUpdateMu sync.Mutex +var commentAfterUpdateHooks []CommentHook + +var commentBeforeDeleteMu sync.Mutex +var commentBeforeDeleteHooks []CommentHook +var commentAfterDeleteMu sync.Mutex +var commentAfterDeleteHooks []CommentHook + +var commentBeforeUpsertMu sync.Mutex +var commentBeforeUpsertHooks []CommentHook +var commentAfterUpsertMu sync.Mutex +var commentAfterUpsertHooks []CommentHook + +// doAfterSelectHooks executes all "after Select" hooks. +func (o *Comment) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentAfterSelectHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeInsertHooks executes all "before insert" hooks. +func (o *Comment) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentBeforeInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterInsertHooks executes all "after Insert" hooks. +func (o *Comment) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentAfterInsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpdateHooks executes all "before Update" hooks. +func (o *Comment) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentBeforeUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpdateHooks executes all "after Update" hooks. +func (o *Comment) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentAfterUpdateHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeDeleteHooks executes all "before Delete" hooks. +func (o *Comment) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentBeforeDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterDeleteHooks executes all "after Delete" hooks. +func (o *Comment) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentAfterDeleteHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doBeforeUpsertHooks executes all "before Upsert" hooks. +func (o *Comment) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentBeforeUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// doAfterUpsertHooks executes all "after Upsert" hooks. +func (o *Comment) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) { + if boil.HooksAreSkipped(ctx) { + return nil + } + + for _, hook := range commentAfterUpsertHooks { + if err := hook(ctx, exec, o); err != nil { + return err + } + } + + return nil +} + +// AddCommentHook registers your hook function for all future operations. +func AddCommentHook(hookPoint boil.HookPoint, commentHook CommentHook) { + switch hookPoint { + case boil.AfterSelectHook: + commentAfterSelectMu.Lock() + commentAfterSelectHooks = append(commentAfterSelectHooks, commentHook) + commentAfterSelectMu.Unlock() + case boil.BeforeInsertHook: + commentBeforeInsertMu.Lock() + commentBeforeInsertHooks = append(commentBeforeInsertHooks, commentHook) + commentBeforeInsertMu.Unlock() + case boil.AfterInsertHook: + commentAfterInsertMu.Lock() + commentAfterInsertHooks = append(commentAfterInsertHooks, commentHook) + commentAfterInsertMu.Unlock() + case boil.BeforeUpdateHook: + commentBeforeUpdateMu.Lock() + commentBeforeUpdateHooks = append(commentBeforeUpdateHooks, commentHook) + commentBeforeUpdateMu.Unlock() + case boil.AfterUpdateHook: + commentAfterUpdateMu.Lock() + commentAfterUpdateHooks = append(commentAfterUpdateHooks, commentHook) + commentAfterUpdateMu.Unlock() + case boil.BeforeDeleteHook: + commentBeforeDeleteMu.Lock() + commentBeforeDeleteHooks = append(commentBeforeDeleteHooks, commentHook) + commentBeforeDeleteMu.Unlock() + case boil.AfterDeleteHook: + commentAfterDeleteMu.Lock() + commentAfterDeleteHooks = append(commentAfterDeleteHooks, commentHook) + commentAfterDeleteMu.Unlock() + case boil.BeforeUpsertHook: + commentBeforeUpsertMu.Lock() + commentBeforeUpsertHooks = append(commentBeforeUpsertHooks, commentHook) + commentBeforeUpsertMu.Unlock() + case boil.AfterUpsertHook: + commentAfterUpsertMu.Lock() + commentAfterUpsertHooks = append(commentAfterUpsertHooks, commentHook) + commentAfterUpsertMu.Unlock() + } +} + +// One returns a single comment record from the query. +func (q commentQuery) One(ctx context.Context, exec boil.ContextExecutor) (*Comment, error) { + o := &Comment{} + + 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 comments") + } + + if err := o.doAfterSelectHooks(ctx, exec); err != nil { + return o, err + } + + return o, nil +} + +// All returns all Comment records from the query. +func (q commentQuery) All(ctx context.Context, exec boil.ContextExecutor) (CommentSlice, error) { + var o []*Comment + + err := q.Bind(ctx, exec, &o) + if err != nil { + return nil, errors.Wrap(err, "models: failed to assign all query results to Comment slice") + } + + if len(commentAfterSelectHooks) != 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 Comment records in the query. +func (q commentQuery) 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 comments rows") + } + + return count, nil +} + +// Exists checks if the row exists in the table. +func (q commentQuery) 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 comments exists") + } + + return count > 0, nil +} + +// Post pointed to by the foreign key. +func (o *Comment) Post(mods ...qm.QueryMod) postQuery { + queryMods := []qm.QueryMod{ + qm.Where("\"id\" = ?", o.PostID), + } + + queryMods = append(queryMods, mods...) + + return Posts(queryMods...) +} + +// Reply pointed to by the foreign key. +func (o *Comment) Reply(mods ...qm.QueryMod) commentQuery { + queryMods := []qm.QueryMod{ + qm.Where("\"id\" = ?", o.ReplyID), + } + + queryMods = append(queryMods, mods...) + + return Comments(queryMods...) +} + +// User pointed to by the foreign key. +func (o *Comment) User(mods ...qm.QueryMod) userQuery { + queryMods := []qm.QueryMod{ + qm.Where("\"id\" = ?", o.UserID), + } + + queryMods = append(queryMods, mods...) + + return Users(queryMods...) +} + +// ReplyComments retrieves all the comment's Comments with an executor via reply_id column. +func (o *Comment) ReplyComments(mods ...qm.QueryMod) commentQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"comments\".\"reply_id\"=?", o.ID), + ) + + return Comments(queryMods...) +} + +// Votes retrieves all the vote's Votes with an executor. +func (o *Comment) Votes(mods ...qm.QueryMod) voteQuery { + var queryMods []qm.QueryMod + if len(mods) != 0 { + queryMods = append(queryMods, mods...) + } + + queryMods = append(queryMods, + qm.Where("\"votes\".\"comment_id\"=?", o.ID), + ) + + return Votes(queryMods...) +} + +// LoadPost allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for an N-1 relationship. +func (commentL) LoadPost(ctx context.Context, e boil.ContextExecutor, singular bool, maybeComment interface{}, mods queries.Applicator) error { + var slice []*Comment + var object *Comment + + if singular { + var ok bool + object, ok = maybeComment.(*Comment) + if !ok { + object = new(Comment) + ok = queries.SetFromEmbeddedStruct(&object, &maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeComment)) + } + } + } else { + s, ok := maybeComment.(*[]*Comment) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeComment)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &commentR{} + } + args[object.PostID] = struct{}{} + + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &commentR{} + } + + 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.Comments = append(foreign.R.Comments, object) + return nil + } + + for _, local := range slice { + for _, foreign := range resultSlice { + if local.PostID == foreign.ID { + local.R.Post = foreign + if foreign.R == nil { + foreign.R = &postR{} + } + foreign.R.Comments = append(foreign.R.Comments, local) + break + } + } + } + + return nil +} + +// LoadReply allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for an N-1 relationship. +func (commentL) LoadReply(ctx context.Context, e boil.ContextExecutor, singular bool, maybeComment interface{}, mods queries.Applicator) error { + var slice []*Comment + var object *Comment + + if singular { + var ok bool + object, ok = maybeComment.(*Comment) + if !ok { + object = new(Comment) + ok = queries.SetFromEmbeddedStruct(&object, &maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeComment)) + } + } + } else { + s, ok := maybeComment.(*[]*Comment) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeComment)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &commentR{} + } + if !queries.IsNil(object.ReplyID) { + args[object.ReplyID] = struct{}{} + } + + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &commentR{} + } + + if !queries.IsNil(obj.ReplyID) { + args[obj.ReplyID] = 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.Reply = foreign + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.ReplyComments = append(foreign.R.ReplyComments, object) + return nil + } + + for _, local := range slice { + for _, foreign := range resultSlice { + if queries.Equal(local.ReplyID, foreign.ID) { + local.R.Reply = foreign + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.ReplyComments = append(foreign.R.ReplyComments, local) + break + } + } + } + + return nil +} + +// LoadUser allows an eager lookup of values, cached into the +// loaded structs of the objects. This is for an N-1 relationship. +func (commentL) LoadUser(ctx context.Context, e boil.ContextExecutor, singular bool, maybeComment interface{}, mods queries.Applicator) error { + var slice []*Comment + var object *Comment + + if singular { + var ok bool + object, ok = maybeComment.(*Comment) + if !ok { + object = new(Comment) + ok = queries.SetFromEmbeddedStruct(&object, &maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeComment)) + } + } + } else { + s, ok := maybeComment.(*[]*Comment) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeComment)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &commentR{} + } + args[object.UserID] = struct{}{} + + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &commentR{} + } + + 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.Comments = append(foreign.R.Comments, 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.Comments = append(foreign.R.Comments, local) + break + } + } + } + + return nil +} + +// LoadReplyComments 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 (commentL) LoadReplyComments(ctx context.Context, e boil.ContextExecutor, singular bool, maybeComment interface{}, mods queries.Applicator) error { + var slice []*Comment + var object *Comment + + if singular { + var ok bool + object, ok = maybeComment.(*Comment) + if !ok { + object = new(Comment) + ok = queries.SetFromEmbeddedStruct(&object, &maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeComment)) + } + } + } else { + s, ok := maybeComment.(*[]*Comment) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeComment)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &commentR{} + } + args[object.ID] = struct{}{} + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &commentR{} + } + 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.From(`comments`), + qm.WhereIn(`comments.reply_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 comments") + } + + var resultSlice []*Comment + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice comments") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results in eager load on 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 singular { + object.R.ReplyComments = resultSlice + for _, foreign := range resultSlice { + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.Reply = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if queries.Equal(local.ID, foreign.ReplyID) { + local.R.ReplyComments = append(local.R.ReplyComments, foreign) + if foreign.R == nil { + foreign.R = &commentR{} + } + foreign.R.Reply = local + break + } + } + } + + return nil +} + +// LoadVotes 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 (commentL) LoadVotes(ctx context.Context, e boil.ContextExecutor, singular bool, maybeComment interface{}, mods queries.Applicator) error { + var slice []*Comment + var object *Comment + + if singular { + var ok bool + object, ok = maybeComment.(*Comment) + if !ok { + object = new(Comment) + ok = queries.SetFromEmbeddedStruct(&object, &maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeComment)) + } + } + } else { + s, ok := maybeComment.(*[]*Comment) + if ok { + slice = *s + } else { + ok = queries.SetFromEmbeddedStruct(&slice, maybeComment) + if !ok { + return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeComment)) + } + } + } + + args := make(map[interface{}]struct{}) + if singular { + if object.R == nil { + object.R = &commentR{} + } + args[object.ID] = struct{}{} + } else { + for _, obj := range slice { + if obj.R == nil { + obj.R = &commentR{} + } + 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.From(`votes`), + qm.WhereIn(`votes.comment_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 votes") + } + + var resultSlice []*Vote + if err = queries.Bind(results, &resultSlice); err != nil { + return errors.Wrap(err, "failed to bind eager loaded slice votes") + } + + if err = results.Close(); err != nil { + return errors.Wrap(err, "failed to close results in eager load on votes") + } + if err = results.Err(); err != nil { + return errors.Wrap(err, "error occurred during iteration of eager loaded relations for votes") + } + + if len(voteAfterSelectHooks) != 0 { + for _, obj := range resultSlice { + if err := obj.doAfterSelectHooks(ctx, e); err != nil { + return err + } + } + } + if singular { + object.R.Votes = resultSlice + for _, foreign := range resultSlice { + if foreign.R == nil { + foreign.R = &voteR{} + } + foreign.R.Comment = object + } + return nil + } + + for _, foreign := range resultSlice { + for _, local := range slice { + if queries.Equal(local.ID, foreign.CommentID) { + local.R.Votes = append(local.R.Votes, foreign) + if foreign.R == nil { + foreign.R = &voteR{} + } + foreign.R.Comment = local + break + } + } + } + + return nil +} + +// SetPost of the comment to the related item. +// Sets o.R.Post to related. +// Adds o to related.R.Comments. +func (o *Comment) 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 \"comments\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"post_id"}), + strmangle.WhereClause("\"", "\"", 2, commentPrimaryKeyColumns), + ) + 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.PostID = related.ID + if o.R == nil { + o.R = &commentR{ + Post: related, + } + } else { + o.R.Post = related + } + + if related.R == nil { + related.R = &postR{ + Comments: CommentSlice{o}, + } + } else { + related.R.Comments = append(related.R.Comments, o) + } + + return nil +} + +// SetReply of the comment to the related item. +// Sets o.R.Reply to related. +// Adds o to related.R.ReplyComments. +func (o *Comment) SetReply(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 \"comments\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"reply_id"}), + strmangle.WhereClause("\"", "\"", 2, commentPrimaryKeyColumns), + ) + 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.ReplyID, related.ID) + if o.R == nil { + o.R = &commentR{ + Reply: related, + } + } else { + o.R.Reply = related + } + + if related.R == nil { + related.R = &commentR{ + ReplyComments: CommentSlice{o}, + } + } else { + related.R.ReplyComments = append(related.R.ReplyComments, o) + } + + return nil +} + +// RemoveReply relationship. +// Sets o.R.Reply to nil. +// Removes o from all passed in related items' relationships struct. +func (o *Comment) RemoveReply(ctx context.Context, exec boil.ContextExecutor, related *Comment) error { + var err error + + queries.SetScanner(&o.ReplyID, nil) + if _, err = o.Update(ctx, exec, boil.Whitelist("reply_id")); err != nil { + return errors.Wrap(err, "failed to update local table") + } + + if o.R != nil { + o.R.Reply = nil + } + if related == nil || related.R == nil { + return nil + } + + for i, ri := range related.R.ReplyComments { + if queries.Equal(o.ReplyID, ri.ReplyID) { + continue + } + + ln := len(related.R.ReplyComments) + if ln > 1 && i < ln-1 { + related.R.ReplyComments[i] = related.R.ReplyComments[ln-1] + } + related.R.ReplyComments = related.R.ReplyComments[:ln-1] + break + } + return nil +} + +// SetUser of the comment to the related item. +// Sets o.R.User to related. +// Adds o to related.R.Comments. +func (o *Comment) 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 \"comments\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"user_id"}), + strmangle.WhereClause("\"", "\"", 2, commentPrimaryKeyColumns), + ) + 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 = &commentR{ + User: related, + } + } else { + o.R.User = related + } + + if related.R == nil { + related.R = &userR{ + Comments: CommentSlice{o}, + } + } else { + related.R.Comments = append(related.R.Comments, o) + } + + return nil +} + +// AddReplyComments adds the given related objects to the existing relationships +// of the comment, optionally inserting them as new records. +// Appends related to o.R.ReplyComments. +// Sets related.R.Reply appropriately. +func (o *Comment) AddReplyComments(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Comment) error { + var err error + for _, rel := range related { + if insert { + queries.Assign(&rel.ReplyID, o.ID) + if err = rel.Insert(ctx, exec, boil.Infer()); err != nil { + return errors.Wrap(err, "failed to insert into foreign table") + } + } else { + updateQuery := fmt.Sprintf( + "UPDATE \"comments\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"reply_id"}), + strmangle.WhereClause("\"", "\"", 2, commentPrimaryKeyColumns), + ) + values := []interface{}{o.ID, rel.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 foreign table") + } + + queries.Assign(&rel.ReplyID, o.ID) + } + } + + if o.R == nil { + o.R = &commentR{ + ReplyComments: related, + } + } else { + o.R.ReplyComments = append(o.R.ReplyComments, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &commentR{ + Reply: o, + } + } else { + rel.R.Reply = o + } + } + return nil +} + +// SetReplyComments removes all previously related items of the +// comment replacing them completely with the passed +// in related items, optionally inserting them as new records. +// Sets o.R.Reply's ReplyComments accordingly. +// Replaces o.R.ReplyComments with related. +// Sets related.R.Reply's ReplyComments accordingly. +func (o *Comment) SetReplyComments(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Comment) error { + query := "update \"comments\" set \"reply_id\" = null where \"reply_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") + } + + if o.R != nil { + for _, rel := range o.R.ReplyComments { + queries.SetScanner(&rel.ReplyID, nil) + if rel.R == nil { + continue + } + + rel.R.Reply = nil + } + o.R.ReplyComments = nil + } + + return o.AddReplyComments(ctx, exec, insert, related...) +} + +// RemoveReplyComments relationships from objects passed in. +// Removes related items from R.ReplyComments (uses pointer comparison, removal does not keep order) +// Sets related.R.Reply. +func (o *Comment) RemoveReplyComments(ctx context.Context, exec boil.ContextExecutor, related ...*Comment) error { + if len(related) == 0 { + return nil + } + + var err error + for _, rel := range related { + queries.SetScanner(&rel.ReplyID, nil) + if rel.R != nil { + rel.R.Reply = nil + } + if _, err = rel.Update(ctx, exec, boil.Whitelist("reply_id")); err != nil { + return err + } + } + if o.R == nil { + return nil + } + + for _, rel := range related { + for i, ri := range o.R.ReplyComments { + if rel != ri { + continue + } + + ln := len(o.R.ReplyComments) + if ln > 1 && i < ln-1 { + o.R.ReplyComments[i] = o.R.ReplyComments[ln-1] + } + o.R.ReplyComments = o.R.ReplyComments[:ln-1] + break + } + } + + return nil +} + +// AddVotes adds the given related objects to the existing relationships +// of the comment, optionally inserting them as new records. +// Appends related to o.R.Votes. +// Sets related.R.Comment appropriately. +func (o *Comment) AddVotes(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Vote) error { + var err error + for _, rel := range related { + if insert { + queries.Assign(&rel.CommentID, o.ID) + if err = rel.Insert(ctx, exec, boil.Infer()); err != nil { + return errors.Wrap(err, "failed to insert into foreign table") + } + } else { + updateQuery := fmt.Sprintf( + "UPDATE \"votes\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, []string{"comment_id"}), + strmangle.WhereClause("\"", "\"", 2, votePrimaryKeyColumns), + ) + values := []interface{}{o.ID, rel.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 foreign table") + } + + queries.Assign(&rel.CommentID, o.ID) + } + } + + if o.R == nil { + o.R = &commentR{ + Votes: related, + } + } else { + o.R.Votes = append(o.R.Votes, related...) + } + + for _, rel := range related { + if rel.R == nil { + rel.R = &voteR{ + Comment: o, + } + } else { + rel.R.Comment = o + } + } + return nil +} + +// SetVotes removes all previously related items of the +// comment replacing them completely with the passed +// in related items, optionally inserting them as new records. +// Sets o.R.Comment's Votes accordingly. +// Replaces o.R.Votes with related. +// Sets related.R.Comment's Votes accordingly. +func (o *Comment) SetVotes(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Vote) error { + query := "update \"votes\" set \"comment_id\" = null where \"comment_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") + } + + if o.R != nil { + for _, rel := range o.R.Votes { + queries.SetScanner(&rel.CommentID, nil) + if rel.R == nil { + continue + } + + rel.R.Comment = nil + } + o.R.Votes = nil + } + + return o.AddVotes(ctx, exec, insert, related...) +} + +// RemoveVotes relationships from objects passed in. +// Removes related items from R.Votes (uses pointer comparison, removal does not keep order) +// Sets related.R.Comment. +func (o *Comment) RemoveVotes(ctx context.Context, exec boil.ContextExecutor, related ...*Vote) error { + if len(related) == 0 { + return nil + } + + var err error + for _, rel := range related { + queries.SetScanner(&rel.CommentID, nil) + if rel.R != nil { + rel.R.Comment = nil + } + if _, err = rel.Update(ctx, exec, boil.Whitelist("comment_id")); err != nil { + return err + } + } + if o.R == nil { + return nil + } + + for _, rel := range related { + for i, ri := range o.R.Votes { + if rel != ri { + continue + } + + ln := len(o.R.Votes) + if ln > 1 && i < ln-1 { + o.R.Votes[i] = o.R.Votes[ln-1] + } + o.R.Votes = o.R.Votes[:ln-1] + break + } + } + + return nil +} + +// Comments retrieves all the records using an executor. +func Comments(mods ...qm.QueryMod) commentQuery { + mods = append(mods, qm.From("\"comments\"")) + q := NewQuery(mods...) + if len(queries.GetSelect(q)) == 0 { + queries.SetSelect(q, []string{"\"comments\".*"}) + } + + return commentQuery{q} +} + +// FindComment retrieves a single record by ID with an executor. +// If selectCols is empty Find will return all columns. +func FindComment(ctx context.Context, exec boil.ContextExecutor, iD int, selectCols ...string) (*Comment, error) { + commentObj := &Comment{} + + sel := "*" + if len(selectCols) > 0 { + sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",") + } + query := fmt.Sprintf( + "select %s from \"comments\" where \"id\"=$1", sel, + ) + + q := queries.Raw(query, iD) + + err := q.Bind(ctx, exec, commentObj) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, sql.ErrNoRows + } + return nil, errors.Wrap(err, "models: unable to select from comments") + } + + if err = commentObj.doAfterSelectHooks(ctx, exec); err != nil { + return commentObj, err + } + + return commentObj, nil +} + +// Insert a single record using an executor. +// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts. +func (o *Comment) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error { + if o == nil { + return errors.New("models: no comments 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(commentColumnsWithDefault, o) + + key := makeCacheKey(columns, nzDefaults) + commentInsertCacheMut.RLock() + cache, cached := commentInsertCache[key] + commentInsertCacheMut.RUnlock() + + if !cached { + wl, returnColumns := columns.InsertColumnSet( + commentAllColumns, + commentColumnsWithDefault, + commentColumnsWithoutDefault, + nzDefaults, + ) + + cache.valueMapping, err = queries.BindMapping(commentType, commentMapping, wl) + if err != nil { + return err + } + cache.retMapping, err = queries.BindMapping(commentType, commentMapping, returnColumns) + if err != nil { + return err + } + if len(wl) != 0 { + cache.query = fmt.Sprintf("INSERT INTO \"comments\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1)) + } else { + cache.query = "INSERT INTO \"comments\" %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 comments") + } + + if !cached { + commentInsertCacheMut.Lock() + commentInsertCache[key] = cache + commentInsertCacheMut.Unlock() + } + + return o.doAfterInsertHooks(ctx, exec) +} + +// Update uses an executor to update the Comment. +// 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 *Comment) 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) + commentUpdateCacheMut.RLock() + cache, cached := commentUpdateCache[key] + commentUpdateCacheMut.RUnlock() + + if !cached { + wl := columns.UpdateColumnSet( + commentAllColumns, + commentPrimaryKeyColumns, + ) + + if !columns.IsWhitelist() { + wl = strmangle.SetComplement(wl, []string{"created_at"}) + } + if len(wl) == 0 { + return 0, errors.New("models: unable to update comments, could not build whitelist") + } + + cache.query = fmt.Sprintf("UPDATE \"comments\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, wl), + strmangle.WhereClause("\"", "\"", len(wl)+1, commentPrimaryKeyColumns), + ) + cache.valueMapping, err = queries.BindMapping(commentType, commentMapping, append(wl, commentPrimaryKeyColumns...)) + 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 comments row") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by update for comments") + } + + if !cached { + commentUpdateCacheMut.Lock() + commentUpdateCache[key] = cache + commentUpdateCacheMut.Unlock() + } + + return rowsAff, o.doAfterUpdateHooks(ctx, exec) +} + +// UpdateAll updates all rows with the specified column values. +func (q commentQuery) 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 comments") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected for comments") + } + + return rowsAff, nil +} + +// UpdateAll updates all rows with the specified column values, using an executor. +func (o CommentSlice) 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)), commentPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := fmt.Sprintf("UPDATE \"comments\" SET %s WHERE %s", + strmangle.SetParamNames("\"", "\"", 1, colNames), + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), len(colNames)+1, commentPrimaryKeyColumns, 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 comment slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all comment") + } + 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 *Comment) 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 comments 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(commentColumnsWithDefault, 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) + + commentUpsertCacheMut.RLock() + cache, cached := commentUpsertCache[key] + commentUpsertCacheMut.RUnlock() + + var err error + + if !cached { + insert, _ := insertColumns.InsertColumnSet( + commentAllColumns, + commentColumnsWithDefault, + commentColumnsWithoutDefault, + nzDefaults, + ) + + update := updateColumns.UpdateColumnSet( + commentAllColumns, + commentPrimaryKeyColumns, + ) + + if updateOnConflict && len(update) == 0 { + return errors.New("models: unable to upsert comments, could not build update column list") + } + + ret := strmangle.SetComplement(commentAllColumns, strmangle.SetIntersect(insert, update)) + + conflict := conflictColumns + if len(conflict) == 0 && updateOnConflict && len(update) != 0 { + if len(commentPrimaryKeyColumns) == 0 { + return errors.New("models: unable to upsert comments, could not build conflict column list") + } + + conflict = make([]string, len(commentPrimaryKeyColumns)) + copy(conflict, commentPrimaryKeyColumns) + } + cache.query = buildUpsertQueryPostgres(dialect, "\"comments\"", updateOnConflict, ret, update, conflict, insert, opts...) + + cache.valueMapping, err = queries.BindMapping(commentType, commentMapping, insert) + if err != nil { + return err + } + if len(ret) != 0 { + cache.retMapping, err = queries.BindMapping(commentType, commentMapping, 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 comments") + } + + if !cached { + commentUpsertCacheMut.Lock() + commentUpsertCache[key] = cache + commentUpsertCacheMut.Unlock() + } + + return o.doAfterUpsertHooks(ctx, exec) +} + +// Delete deletes a single Comment record with an executor. +// Delete will match against the primary key column to find the record to delete. +func (o *Comment) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if o == nil { + return 0, errors.New("models: no Comment provided for delete") + } + + if err := o.doBeforeDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), commentPrimaryKeyMapping) + sql := "DELETE FROM \"comments\" 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 comments") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by delete for comments") + } + + if err := o.doAfterDeleteHooks(ctx, exec); err != nil { + return 0, err + } + + return rowsAff, nil +} + +// DeleteAll deletes all matching rows. +func (q commentQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if q.Query == nil { + return 0, errors.New("models: no commentQuery 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 comments") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for comments") + } + + return rowsAff, nil +} + +// DeleteAll deletes all rows in the slice, using an executor. +func (o CommentSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) { + if len(o) == 0 { + return 0, nil + } + + if len(commentBeforeDeleteHooks) != 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)), commentPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "DELETE FROM \"comments\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, commentPrimaryKeyColumns, 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 comment slice") + } + + rowsAff, err := result.RowsAffected() + if err != nil { + return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for comments") + } + + if len(commentAfterDeleteHooks) != 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 *Comment) Reload(ctx context.Context, exec boil.ContextExecutor) error { + ret, err := FindComment(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 *CommentSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error { + if o == nil || len(*o) == 0 { + return nil + } + + slice := CommentSlice{} + var args []interface{} + for _, obj := range *o { + pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), commentPrimaryKeyMapping) + args = append(args, pkeyArgs...) + } + + sql := "SELECT \"comments\".* FROM \"comments\" WHERE " + + strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, commentPrimaryKeyColumns, 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 CommentSlice") + } + + *o = slice + + return nil +} + +// CommentExists checks if the Comment row exists. +func CommentExists(ctx context.Context, exec boil.ContextExecutor, iD int) (bool, error) { + var exists bool + sql := "select exists(select 1 from \"comments\" 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 comments exists") + } + + return exists, nil +} + +// Exists checks if the Comment row exists. +func (o *Comment) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) { + return CommentExists(ctx, exec, o.ID) +} |
