// Code generated by SQLBoiler 4.19.5 (https://github.com/aarondl/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/aarondl/null/v8"
	"github.com/aarondl/sqlboiler/v4/boil"
	"github.com/aarondl/sqlboiler/v4/queries"
	"github.com/aarondl/sqlboiler/v4/queries/qm"
	"github.com/aarondl/sqlboiler/v4/queries/qmhelper"
	"github.com/aarondl/strmangle"
	"github.com/friendsofgo/errors"
)

// Run is an object representing the database table.
type Run struct {
	RunID              int32       `boil:"run_id" json:"run_id" toml:"run_id" yaml:"run_id"`
	Title              string      `boil:"title" json:"title" toml:"title" yaml:"title"`
	ProjectID          int32       `boil:"project_id" json:"project_id" toml:"project_id" yaml:"project_id"`
	ScenarioID         int32       `boil:"scenario_id" json:"scenario_id" toml:"scenario_id" yaml:"scenario_id"`
	Status             string      `boil:"status" json:"status" toml:"status" yaml:"status"`
	ScriptRuns         string      `boil:"script_runs" json:"script_runs" toml:"script_runs" yaml:"script_runs"`
	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"`
	DeletedAt          null.Time   `boil:"deleted_at" json:"deleted_at,omitempty" toml:"deleted_at" yaml:"deleted_at,omitempty"`
	Info               string      `boil:"info" json:"info" toml:"info" yaml:"info"`
	PercentageOfTarget int32       `boil:"percentage_of_target" json:"percentage_of_target" toml:"percentage_of_target" yaml:"percentage_of_target"`
	UserName           string      `boil:"user_name" json:"user_name" toml:"user_name" yaml:"user_name"`
	PreferredUserName  string      `boil:"preferred_user_name" json:"preferred_user_name" toml:"preferred_user_name" yaml:"preferred_user_name"`
	ReportLink         null.String `boil:"report_link" json:"report_link,omitempty" toml:"report_link" yaml:"report_link,omitempty"`

	R *runR `boil:"-" json:"-" toml:"-" yaml:"-"`
	L runL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

var RunColumns = struct {
	RunID              string
	Title              string
	ProjectID          string
	ScenarioID         string
	Status             string
	ScriptRuns         string
	CreatedAt          string
	UpdatedAt          string
	DeletedAt          string
	Info               string
	PercentageOfTarget string
	UserName           string
	PreferredUserName  string
	ReportLink         string
}{
	RunID:              "run_id",
	Title:              "title",
	ProjectID:          "project_id",
	ScenarioID:         "scenario_id",
	Status:             "status",
	ScriptRuns:         "script_runs",
	CreatedAt:          "created_at",
	UpdatedAt:          "updated_at",
	DeletedAt:          "deleted_at",
	Info:               "info",
	PercentageOfTarget: "percentage_of_target",
	UserName:           "user_name",
	PreferredUserName:  "preferred_user_name",
	ReportLink:         "report_link",
}

var RunTableColumns = struct {
	RunID              string
	Title              string
	ProjectID          string
	ScenarioID         string
	Status             string
	ScriptRuns         string
	CreatedAt          string
	UpdatedAt          string
	DeletedAt          string
	Info               string
	PercentageOfTarget string
	UserName           string
	PreferredUserName  string
	ReportLink         string
}{
	RunID:              "runs.run_id",
	Title:              "runs.title",
	ProjectID:          "runs.project_id",
	ScenarioID:         "runs.scenario_id",
	Status:             "runs.status",
	ScriptRuns:         "runs.script_runs",
	CreatedAt:          "runs.created_at",
	UpdatedAt:          "runs.updated_at",
	DeletedAt:          "runs.deleted_at",
	Info:               "runs.info",
	PercentageOfTarget: "runs.percentage_of_target",
	UserName:           "runs.user_name",
	PreferredUserName:  "runs.preferred_user_name",
	ReportLink:         "runs.report_link",
}

// Generated where

var RunWhere = struct {
	RunID              whereHelperint32
	Title              whereHelperstring
	ProjectID          whereHelperint32
	ScenarioID         whereHelperint32
	Status             whereHelperstring
	ScriptRuns         whereHelperstring
	CreatedAt          whereHelpertime_Time
	UpdatedAt          whereHelpertime_Time
	DeletedAt          whereHelpernull_Time
	Info               whereHelperstring
	PercentageOfTarget whereHelperint32
	UserName           whereHelperstring
	PreferredUserName  whereHelperstring
	ReportLink         whereHelpernull_String
}{
	RunID:              whereHelperint32{field: "\"runs\".\"run_id\""},
	Title:              whereHelperstring{field: "\"runs\".\"title\""},
	ProjectID:          whereHelperint32{field: "\"runs\".\"project_id\""},
	ScenarioID:         whereHelperint32{field: "\"runs\".\"scenario_id\""},
	Status:             whereHelperstring{field: "\"runs\".\"status\""},
	ScriptRuns:         whereHelperstring{field: "\"runs\".\"script_runs\""},
	CreatedAt:          whereHelpertime_Time{field: "\"runs\".\"created_at\""},
	UpdatedAt:          whereHelpertime_Time{field: "\"runs\".\"updated_at\""},
	DeletedAt:          whereHelpernull_Time{field: "\"runs\".\"deleted_at\""},
	Info:               whereHelperstring{field: "\"runs\".\"info\""},
	PercentageOfTarget: whereHelperint32{field: "\"runs\".\"percentage_of_target\""},
	UserName:           whereHelperstring{field: "\"runs\".\"user_name\""},
	PreferredUserName:  whereHelperstring{field: "\"runs\".\"preferred_user_name\""},
	ReportLink:         whereHelpernull_String{field: "\"runs\".\"report_link\""},
}

// RunRels is where relationship names are stored.
var RunRels = struct {
	Commands   string
	RunReports string
}{
	Commands:   "Commands",
	RunReports: "RunReports",
}

// runR is where relationships are stored.
type runR struct {
	Commands   CommandSlice   `boil:"Commands" json:"Commands" toml:"Commands" yaml:"Commands"`
	RunReports RunReportSlice `boil:"RunReports" json:"RunReports" toml:"RunReports" yaml:"RunReports"`
}

// NewStruct creates a new relationship struct
func (*runR) NewStruct() *runR {
	return &runR{}
}

func (o *Run) GetCommands() CommandSlice {
	if o == nil {
		return nil
	}

	return o.R.GetCommands()
}

func (r *runR) GetCommands() CommandSlice {
	if r == nil {
		return nil
	}

	return r.Commands
}

func (o *Run) GetRunReports() RunReportSlice {
	if o == nil {
		return nil
	}

	return o.R.GetRunReports()
}

func (r *runR) GetRunReports() RunReportSlice {
	if r == nil {
		return nil
	}

	return r.RunReports
}

// runL is where Load methods for each relationship are stored.
type runL struct{}

var (
	runAllColumns            = []string{"run_id", "title", "project_id", "scenario_id", "status", "script_runs", "created_at", "updated_at", "deleted_at", "info", "percentage_of_target", "user_name", "preferred_user_name", "report_link"}
	runColumnsWithoutDefault = []string{"title", "project_id", "scenario_id", "status", "script_runs"}
	runColumnsWithDefault    = []string{"run_id", "created_at", "updated_at", "deleted_at", "info", "percentage_of_target", "user_name", "preferred_user_name", "report_link"}
	runPrimaryKeyColumns     = []string{"run_id"}
	runGeneratedColumns      = []string{}
)

type (
	// RunSlice is an alias for a slice of pointers to Run.
	// This should almost always be used instead of []Run.
	RunSlice []*Run
	// RunHook is the signature for custom Run hook methods
	RunHook func(context.Context, boil.ContextExecutor, *Run) error

	runQuery struct {
		*queries.Query
	}
)

// Cache for insert, update and upsert
var (
	runType                 = reflect.TypeOf(&Run{})
	runMapping              = queries.MakeStructMapping(runType)
	runPrimaryKeyMapping, _ = queries.BindMapping(runType, runMapping, runPrimaryKeyColumns)
	runInsertCacheMut       sync.RWMutex
	runInsertCache          = make(map[string]insertCache)
	runUpdateCacheMut       sync.RWMutex
	runUpdateCache          = make(map[string]updateCache)
	runUpsertCacheMut       sync.RWMutex
	runUpsertCache          = 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 runAfterSelectMu sync.Mutex
var runAfterSelectHooks []RunHook

var runBeforeInsertMu sync.Mutex
var runBeforeInsertHooks []RunHook
var runAfterInsertMu sync.Mutex
var runAfterInsertHooks []RunHook

var runBeforeUpdateMu sync.Mutex
var runBeforeUpdateHooks []RunHook
var runAfterUpdateMu sync.Mutex
var runAfterUpdateHooks []RunHook

var runBeforeDeleteMu sync.Mutex
var runBeforeDeleteHooks []RunHook
var runAfterDeleteMu sync.Mutex
var runAfterDeleteHooks []RunHook

var runBeforeUpsertMu sync.Mutex
var runBeforeUpsertHooks []RunHook
var runAfterUpsertMu sync.Mutex
var runAfterUpsertHooks []RunHook

// doAfterSelectHooks executes all "after Select" hooks.
func (o *Run) doAfterSelectHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runAfterSelectHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// doBeforeInsertHooks executes all "before insert" hooks.
func (o *Run) doBeforeInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runBeforeInsertHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// doAfterInsertHooks executes all "after Insert" hooks.
func (o *Run) doAfterInsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runAfterInsertHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// doBeforeUpdateHooks executes all "before Update" hooks.
func (o *Run) doBeforeUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runBeforeUpdateHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// doAfterUpdateHooks executes all "after Update" hooks.
func (o *Run) doAfterUpdateHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runAfterUpdateHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// doBeforeDeleteHooks executes all "before Delete" hooks.
func (o *Run) doBeforeDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runBeforeDeleteHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// doAfterDeleteHooks executes all "after Delete" hooks.
func (o *Run) doAfterDeleteHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runAfterDeleteHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// doBeforeUpsertHooks executes all "before Upsert" hooks.
func (o *Run) doBeforeUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runBeforeUpsertHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// doAfterUpsertHooks executes all "after Upsert" hooks.
func (o *Run) doAfterUpsertHooks(ctx context.Context, exec boil.ContextExecutor) (err error) {
	if boil.HooksAreSkipped(ctx) {
		return nil
	}

	for _, hook := range runAfterUpsertHooks {
		if err := hook(ctx, exec, o); err != nil {
			return err
		}
	}

	return nil
}

// AddRunHook registers your hook function for all future operations.
func AddRunHook(hookPoint boil.HookPoint, runHook RunHook) {
	switch hookPoint {
	case boil.AfterSelectHook:
		runAfterSelectMu.Lock()
		runAfterSelectHooks = append(runAfterSelectHooks, runHook)
		runAfterSelectMu.Unlock()
	case boil.BeforeInsertHook:
		runBeforeInsertMu.Lock()
		runBeforeInsertHooks = append(runBeforeInsertHooks, runHook)
		runBeforeInsertMu.Unlock()
	case boil.AfterInsertHook:
		runAfterInsertMu.Lock()
		runAfterInsertHooks = append(runAfterInsertHooks, runHook)
		runAfterInsertMu.Unlock()
	case boil.BeforeUpdateHook:
		runBeforeUpdateMu.Lock()
		runBeforeUpdateHooks = append(runBeforeUpdateHooks, runHook)
		runBeforeUpdateMu.Unlock()
	case boil.AfterUpdateHook:
		runAfterUpdateMu.Lock()
		runAfterUpdateHooks = append(runAfterUpdateHooks, runHook)
		runAfterUpdateMu.Unlock()
	case boil.BeforeDeleteHook:
		runBeforeDeleteMu.Lock()
		runBeforeDeleteHooks = append(runBeforeDeleteHooks, runHook)
		runBeforeDeleteMu.Unlock()
	case boil.AfterDeleteHook:
		runAfterDeleteMu.Lock()
		runAfterDeleteHooks = append(runAfterDeleteHooks, runHook)
		runAfterDeleteMu.Unlock()
	case boil.BeforeUpsertHook:
		runBeforeUpsertMu.Lock()
		runBeforeUpsertHooks = append(runBeforeUpsertHooks, runHook)
		runBeforeUpsertMu.Unlock()
	case boil.AfterUpsertHook:
		runAfterUpsertMu.Lock()
		runAfterUpsertHooks = append(runAfterUpsertHooks, runHook)
		runAfterUpsertMu.Unlock()
	}
}

// One returns a single run record from the query.
func (q runQuery) One(ctx context.Context, exec boil.ContextExecutor) (*Run, error) {
	o := &Run{}

	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 runs")
	}

	if err := o.doAfterSelectHooks(ctx, exec); err != nil {
		return o, err
	}

	return o, nil
}

// All returns all Run records from the query.
func (q runQuery) All(ctx context.Context, exec boil.ContextExecutor) (RunSlice, error) {
	var o []*Run

	err := q.Bind(ctx, exec, &o)
	if err != nil {
		return nil, errors.Wrap(err, "models: failed to assign all query results to Run slice")
	}

	if len(runAfterSelectHooks) != 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 Run records in the query.
func (q runQuery) 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 runs rows")
	}

	return count, nil
}

// Exists checks if the row exists in the table.
func (q runQuery) 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 runs exists")
	}

	return count > 0, nil
}

// Commands retrieves all the command's Commands with an executor.
func (o *Run) Commands(mods ...qm.QueryMod) commandQuery {
	var queryMods []qm.QueryMod
	if len(mods) != 0 {
		queryMods = append(queryMods, mods...)
	}

	queryMods = append(queryMods,
		qm.Where("\"command\".\"run_id\"=?", o.RunID),
	)

	return Commands(queryMods...)
}

// RunReports retrieves all the run_report's RunReports with an executor.
func (o *Run) RunReports(mods ...qm.QueryMod) runReportQuery {
	var queryMods []qm.QueryMod
	if len(mods) != 0 {
		queryMods = append(queryMods, mods...)
	}

	queryMods = append(queryMods,
		qm.Where("\"run_report\".\"run_id\"=?", o.RunID),
	)

	return RunReports(queryMods...)
}

// LoadCommands 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 (runL) LoadCommands(ctx context.Context, e boil.ContextExecutor, singular bool, maybeRun interface{}, mods queries.Applicator) error {
	var slice []*Run
	var object *Run

	if singular {
		var ok bool
		object, ok = maybeRun.(*Run)
		if !ok {
			object = new(Run)
			ok = queries.SetFromEmbeddedStruct(&object, &maybeRun)
			if !ok {
				return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeRun))
			}
		}
	} else {
		s, ok := maybeRun.(*[]*Run)
		if ok {
			slice = *s
		} else {
			ok = queries.SetFromEmbeddedStruct(&slice, maybeRun)
			if !ok {
				return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeRun))
			}
		}
	}

	args := make(map[interface{}]struct{})
	if singular {
		if object.R == nil {
			object.R = &runR{}
		}
		args[object.RunID] = struct{}{}
	} else {
		for _, obj := range slice {
			if obj.R == nil {
				obj.R = &runR{}
			}
			args[obj.RunID] = 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(`command`),
		qm.WhereIn(`command.run_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 command")
	}

	var resultSlice []*Command
	if err = queries.Bind(results, &resultSlice); err != nil {
		return errors.Wrap(err, "failed to bind eager loaded slice command")
	}

	if err = results.Close(); err != nil {
		return errors.Wrap(err, "failed to close results in eager load on command")
	}
	if err = results.Err(); err != nil {
		return errors.Wrap(err, "error occurred during iteration of eager loaded relations for command")
	}

	if len(commandAfterSelectHooks) != 0 {
		for _, obj := range resultSlice {
			if err := obj.doAfterSelectHooks(ctx, e); err != nil {
				return err
			}
		}
	}
	if singular {
		object.R.Commands = resultSlice
		for _, foreign := range resultSlice {
			if foreign.R == nil {
				foreign.R = &commandR{}
			}
			foreign.R.Run = object
		}
		return nil
	}

	for _, foreign := range resultSlice {
		for _, local := range slice {
			if local.RunID == foreign.RunID {
				local.R.Commands = append(local.R.Commands, foreign)
				if foreign.R == nil {
					foreign.R = &commandR{}
				}
				foreign.R.Run = local
				break
			}
		}
	}

	return nil
}

// LoadRunReports 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 (runL) LoadRunReports(ctx context.Context, e boil.ContextExecutor, singular bool, maybeRun interface{}, mods queries.Applicator) error {
	var slice []*Run
	var object *Run

	if singular {
		var ok bool
		object, ok = maybeRun.(*Run)
		if !ok {
			object = new(Run)
			ok = queries.SetFromEmbeddedStruct(&object, &maybeRun)
			if !ok {
				return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", object, maybeRun))
			}
		}
	} else {
		s, ok := maybeRun.(*[]*Run)
		if ok {
			slice = *s
		} else {
			ok = queries.SetFromEmbeddedStruct(&slice, maybeRun)
			if !ok {
				return errors.New(fmt.Sprintf("failed to set %T from embedded struct %T", slice, maybeRun))
			}
		}
	}

	args := make(map[interface{}]struct{})
	if singular {
		if object.R == nil {
			object.R = &runR{}
		}
		args[object.RunID] = struct{}{}
	} else {
		for _, obj := range slice {
			if obj.R == nil {
				obj.R = &runR{}
			}
			args[obj.RunID] = 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(`run_report`),
		qm.WhereIn(`run_report.run_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 run_report")
	}

	var resultSlice []*RunReport
	if err = queries.Bind(results, &resultSlice); err != nil {
		return errors.Wrap(err, "failed to bind eager loaded slice run_report")
	}

	if err = results.Close(); err != nil {
		return errors.Wrap(err, "failed to close results in eager load on run_report")
	}
	if err = results.Err(); err != nil {
		return errors.Wrap(err, "error occurred during iteration of eager loaded relations for run_report")
	}

	if len(runReportAfterSelectHooks) != 0 {
		for _, obj := range resultSlice {
			if err := obj.doAfterSelectHooks(ctx, e); err != nil {
				return err
			}
		}
	}
	if singular {
		object.R.RunReports = resultSlice
		for _, foreign := range resultSlice {
			if foreign.R == nil {
				foreign.R = &runReportR{}
			}
			foreign.R.Run = object
		}
		return nil
	}

	for _, foreign := range resultSlice {
		for _, local := range slice {
			if local.RunID == foreign.RunID {
				local.R.RunReports = append(local.R.RunReports, foreign)
				if foreign.R == nil {
					foreign.R = &runReportR{}
				}
				foreign.R.Run = local
				break
			}
		}
	}

	return nil
}

// AddCommands adds the given related objects to the existing relationships
// of the run, optionally inserting them as new records.
// Appends related to o.R.Commands.
// Sets related.R.Run appropriately.
func (o *Run) AddCommands(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Command) error {
	var err error
	for _, rel := range related {
		if insert {
			rel.RunID = o.RunID
			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 \"command\" SET %s WHERE %s",
				strmangle.SetParamNames("\"", "\"", 1, []string{"run_id"}),
				strmangle.WhereClause("\"", "\"", 2, commandPrimaryKeyColumns),
			)
			values := []interface{}{o.RunID, rel.CommandID}

			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")
			}

			rel.RunID = o.RunID
		}
	}

	if o.R == nil {
		o.R = &runR{
			Commands: related,
		}
	} else {
		o.R.Commands = append(o.R.Commands, related...)
	}

	for _, rel := range related {
		if rel.R == nil {
			rel.R = &commandR{
				Run: o,
			}
		} else {
			rel.R.Run = o
		}
	}
	return nil
}

// AddRunReports adds the given related objects to the existing relationships
// of the run, optionally inserting them as new records.
// Appends related to o.R.RunReports.
// Sets related.R.Run appropriately.
func (o *Run) AddRunReports(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*RunReport) error {
	var err error
	for _, rel := range related {
		if insert {
			rel.RunID = o.RunID
			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 \"run_report\" SET %s WHERE %s",
				strmangle.SetParamNames("\"", "\"", 1, []string{"run_id"}),
				strmangle.WhereClause("\"", "\"", 2, runReportPrimaryKeyColumns),
			)
			values := []interface{}{o.RunID, 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")
			}

			rel.RunID = o.RunID
		}
	}

	if o.R == nil {
		o.R = &runR{
			RunReports: related,
		}
	} else {
		o.R.RunReports = append(o.R.RunReports, related...)
	}

	for _, rel := range related {
		if rel.R == nil {
			rel.R = &runReportR{
				Run: o,
			}
		} else {
			rel.R.Run = o
		}
	}
	return nil
}

// Runs retrieves all the records using an executor.
func Runs(mods ...qm.QueryMod) runQuery {
	mods = append(mods, qm.From("\"runs\""))
	q := NewQuery(mods...)
	if len(queries.GetSelect(q)) == 0 {
		queries.SetSelect(q, []string{"\"runs\".*"})
	}

	return runQuery{q}
}

// FindRun retrieves a single record by ID with an executor.
// If selectCols is empty Find will return all columns.
func FindRun(ctx context.Context, exec boil.ContextExecutor, runID int32, selectCols ...string) (*Run, error) {
	runObj := &Run{}

	sel := "*"
	if len(selectCols) > 0 {
		sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
	}
	query := fmt.Sprintf(
		"select %s from \"runs\" where \"run_id\"=$1", sel,
	)

	q := queries.Raw(query, runID)

	err := q.Bind(ctx, exec, runObj)
	if err != nil {
		if errors.Is(err, sql.ErrNoRows) {
			return nil, sql.ErrNoRows
		}
		return nil, errors.Wrap(err, "models: unable to select from runs")
	}

	if err = runObj.doAfterSelectHooks(ctx, exec); err != nil {
		return runObj, err
	}

	return runObj, nil
}

// Insert a single record using an executor.
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
func (o *Run) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error {
	if o == nil {
		return errors.New("models: no runs 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(runColumnsWithDefault, o)

	key := makeCacheKey(columns, nzDefaults)
	runInsertCacheMut.RLock()
	cache, cached := runInsertCache[key]
	runInsertCacheMut.RUnlock()

	if !cached {
		wl, returnColumns := columns.InsertColumnSet(
			runAllColumns,
			runColumnsWithDefault,
			runColumnsWithoutDefault,
			nzDefaults,
		)

		cache.valueMapping, err = queries.BindMapping(runType, runMapping, wl)
		if err != nil {
			return err
		}
		cache.retMapping, err = queries.BindMapping(runType, runMapping, returnColumns)
		if err != nil {
			return err
		}
		if len(wl) != 0 {
			cache.query = fmt.Sprintf("INSERT INTO \"runs\" (\"%s\") %%sVALUES (%s)%%s", strings.Join(wl, "\",\""), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
		} else {
			cache.query = "INSERT INTO \"runs\" %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 runs")
	}

	if !cached {
		runInsertCacheMut.Lock()
		runInsertCache[key] = cache
		runInsertCacheMut.Unlock()
	}

	return o.doAfterInsertHooks(ctx, exec)
}

// Update uses an executor to update the Run.
// 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 *Run) 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)
	runUpdateCacheMut.RLock()
	cache, cached := runUpdateCache[key]
	runUpdateCacheMut.RUnlock()

	if !cached {
		wl := columns.UpdateColumnSet(
			runAllColumns,
			runPrimaryKeyColumns,
		)

		if !columns.IsWhitelist() {
			wl = strmangle.SetComplement(wl, []string{"created_at"})
		}
		if len(wl) == 0 {
			return 0, errors.New("models: unable to update runs, could not build whitelist")
		}

		cache.query = fmt.Sprintf("UPDATE \"runs\" SET %s WHERE %s",
			strmangle.SetParamNames("\"", "\"", 1, wl),
			strmangle.WhereClause("\"", "\"", len(wl)+1, runPrimaryKeyColumns),
		)
		cache.valueMapping, err = queries.BindMapping(runType, runMapping, append(wl, runPrimaryKeyColumns...))
		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 runs row")
	}

	rowsAff, err := result.RowsAffected()
	if err != nil {
		return 0, errors.Wrap(err, "models: failed to get rows affected by update for runs")
	}

	if !cached {
		runUpdateCacheMut.Lock()
		runUpdateCache[key] = cache
		runUpdateCacheMut.Unlock()
	}

	return rowsAff, o.doAfterUpdateHooks(ctx, exec)
}

// UpdateAll updates all rows with the specified column values.
func (q runQuery) 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 runs")
	}

	rowsAff, err := result.RowsAffected()
	if err != nil {
		return 0, errors.Wrap(err, "models: unable to retrieve rows affected for runs")
	}

	return rowsAff, nil
}

// UpdateAll updates all rows with the specified column values, using an executor.
func (o RunSlice) 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)), runPrimaryKeyMapping)
		args = append(args, pkeyArgs...)
	}

	sql := fmt.Sprintf("UPDATE \"runs\" SET %s WHERE %s",
		strmangle.SetParamNames("\"", "\"", 1, colNames),
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), len(colNames)+1, runPrimaryKeyColumns, 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 run slice")
	}

	rowsAff, err := result.RowsAffected()
	if err != nil {
		return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all run")
	}
	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 *Run) 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 runs 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(runColumnsWithDefault, 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)

	runUpsertCacheMut.RLock()
	cache, cached := runUpsertCache[key]
	runUpsertCacheMut.RUnlock()

	var err error

	if !cached {
		insert, _ := insertColumns.InsertColumnSet(
			runAllColumns,
			runColumnsWithDefault,
			runColumnsWithoutDefault,
			nzDefaults,
		)

		update := updateColumns.UpdateColumnSet(
			runAllColumns,
			runPrimaryKeyColumns,
		)

		if updateOnConflict && len(update) == 0 {
			return errors.New("models: unable to upsert runs, could not build update column list")
		}

		ret := strmangle.SetComplement(runAllColumns, strmangle.SetIntersect(insert, update))

		conflict := conflictColumns
		if len(conflict) == 0 && updateOnConflict && len(update) != 0 {
			if len(runPrimaryKeyColumns) == 0 {
				return errors.New("models: unable to upsert runs, could not build conflict column list")
			}

			conflict = make([]string, len(runPrimaryKeyColumns))
			copy(conflict, runPrimaryKeyColumns)
		}
		cache.query = buildUpsertQueryPostgres(dialect, "\"runs\"", updateOnConflict, ret, update, conflict, insert, opts...)

		cache.valueMapping, err = queries.BindMapping(runType, runMapping, insert)
		if err != nil {
			return err
		}
		if len(ret) != 0 {
			cache.retMapping, err = queries.BindMapping(runType, runMapping, 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 runs")
	}

	if !cached {
		runUpsertCacheMut.Lock()
		runUpsertCache[key] = cache
		runUpsertCacheMut.Unlock()
	}

	return o.doAfterUpsertHooks(ctx, exec)
}

// Delete deletes a single Run record with an executor.
// Delete will match against the primary key column to find the record to delete.
func (o *Run) Delete(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
	if o == nil {
		return 0, errors.New("models: no Run provided for delete")
	}

	if err := o.doBeforeDeleteHooks(ctx, exec); err != nil {
		return 0, err
	}

	args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), runPrimaryKeyMapping)
	sql := "DELETE FROM \"runs\" WHERE \"run_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 runs")
	}

	rowsAff, err := result.RowsAffected()
	if err != nil {
		return 0, errors.Wrap(err, "models: failed to get rows affected by delete for runs")
	}

	if err := o.doAfterDeleteHooks(ctx, exec); err != nil {
		return 0, err
	}

	return rowsAff, nil
}

// DeleteAll deletes all matching rows.
func (q runQuery) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
	if q.Query == nil {
		return 0, errors.New("models: no runQuery 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 runs")
	}

	rowsAff, err := result.RowsAffected()
	if err != nil {
		return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for runs")
	}

	return rowsAff, nil
}

// DeleteAll deletes all rows in the slice, using an executor.
func (o RunSlice) DeleteAll(ctx context.Context, exec boil.ContextExecutor) (int64, error) {
	if len(o) == 0 {
		return 0, nil
	}

	if len(runBeforeDeleteHooks) != 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)), runPrimaryKeyMapping)
		args = append(args, pkeyArgs...)
	}

	sql := "DELETE FROM \"runs\" WHERE " +
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, runPrimaryKeyColumns, 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 run slice")
	}

	rowsAff, err := result.RowsAffected()
	if err != nil {
		return 0, errors.Wrap(err, "models: failed to get rows affected by deleteall for runs")
	}

	if len(runAfterDeleteHooks) != 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 *Run) Reload(ctx context.Context, exec boil.ContextExecutor) error {
	ret, err := FindRun(ctx, exec, o.RunID)
	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 *RunSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error {
	if o == nil || len(*o) == 0 {
		return nil
	}

	slice := RunSlice{}
	var args []interface{}
	for _, obj := range *o {
		pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), runPrimaryKeyMapping)
		args = append(args, pkeyArgs...)
	}

	sql := "SELECT \"runs\".* FROM \"runs\" WHERE " +
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 1, runPrimaryKeyColumns, 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 RunSlice")
	}

	*o = slice

	return nil
}

// RunExists checks if the Run row exists.
func RunExists(ctx context.Context, exec boil.ContextExecutor, runID int32) (bool, error) {
	var exists bool
	sql := "select exists(select 1 from \"runs\" where \"run_id\"=$1 limit 1)"

	if boil.IsDebug(ctx) {
		writer := boil.DebugWriterFrom(ctx)
		fmt.Fprintln(writer, sql)
		fmt.Fprintln(writer, runID)
	}
	row := exec.QueryRowContext(ctx, sql, runID)

	err := row.Scan(&exists)
	if err != nil {
		return false, errors.Wrap(err, "models: unable to check if runs exists")
	}

	return exists, nil
}

// Exists checks if the Run row exists.
func (o *Run) Exists(ctx context.Context, exec boil.ContextExecutor) (bool, error) {
	return RunExists(ctx, exec, o.RunID)
}
