package model

import (
	"database/sql"
	"fmt"
	"reflect"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/pkg/errors"
	"github.com/volatiletech/null"
	"github.com/volatiletech/sqlboiler/boil"
	"github.com/volatiletech/sqlboiler/queries"
	"github.com/volatiletech/sqlboiler/queries/qm"
	"github.com/volatiletech/sqlboiler/queries/qmhelper"
	"github.com/volatiletech/sqlboiler/strmangle"
)

type JobStatus struct {
	JobName      string      `boil:"job_name" json:"job_name" toml:"job_name" yaml:"job_name"`
	LastSync     time.Time   `boil:"last_sync" json:"last_sync" toml:"last_sync" yaml:"last_sync"`
	IsSuccess    bool        `boil:"is_success" json:"is_success" toml:"is_success" yaml:"is_success"`
	ErrorMessage null.String `boil:"error_message" json:"error_message,omitempty" toml:"error_message" yaml:"error_message,omitempty"`
	State        null.JSON   `boil:"state" json:"state,omitempty" toml:"state" yaml:"state,omitempty"`

	R *jobStatusR `boil:"-" json:"-" toml:"-" yaml:"-"`
	L jobStatusL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

var JobStatusColumns = struct {
	JobName      string
	LastSync     string
	IsSuccess    string
	ErrorMessage string
	State        string
}{
	JobName:      "job_name",
	LastSync:     "last_sync",
	IsSuccess:    "is_success",
	ErrorMessage: "error_message",
	State:        "state",
}

var JobStatusWhere = struct {
	JobName      whereHelperstring
	LastSync     whereHelpertime_Time
	IsSuccess    whereHelperbool
	ErrorMessage whereHelpernull_String
	State        whereHelpernull_JSON
}{
	JobName:      whereHelperstring{field: "`job_status`.`job_name`"},
	LastSync:     whereHelpertime_Time{field: "`job_status`.`last_sync`"},
	IsSuccess:    whereHelperbool{field: "`job_status`.`is_success`"},
	ErrorMessage: whereHelpernull_String{field: "`job_status`.`error_message`"},
	State:        whereHelpernull_JSON{field: "`job_status`.`state`"},
}

var JobStatusRels = struct {
}{}

type jobStatusR struct {
}

func (*jobStatusR) NewStruct() *jobStatusR {
	return &jobStatusR{}
}

type jobStatusL struct{}

var (
	jobStatusAllColumns            = []string{"job_name", "last_sync", "is_success", "error_message", "state"}
	jobStatusColumnsWithoutDefault = []string{"job_name", "error_message", "state"}
	jobStatusColumnsWithDefault    = []string{"last_sync", "is_success"}
	jobStatusPrimaryKeyColumns     = []string{"job_name"}
)

type (

	JobStatusSlice []*JobStatus

	jobStatusQuery struct {
		*queries.Query
	}
)

var (
	jobStatusType                 = reflect.TypeOf(&JobStatus{})
	jobStatusMapping              = queries.MakeStructMapping(jobStatusType)
	jobStatusPrimaryKeyMapping, _ = queries.BindMapping(jobStatusType, jobStatusMapping, jobStatusPrimaryKeyColumns)
	jobStatusInsertCacheMut       sync.RWMutex
	jobStatusInsertCache          = make(map[string]insertCache)
	jobStatusUpdateCacheMut       sync.RWMutex
	jobStatusUpdateCache          = make(map[string]updateCache)
	jobStatusUpsertCacheMut       sync.RWMutex
	jobStatusUpsertCache          = make(map[string]insertCache)
)

var (

	_ = time.Second

	_ = qmhelper.Where
)

func (q jobStatusQuery) OneG() (*JobStatus, error) {
	return q.One(boil.GetDB())
}

func (q jobStatusQuery) OneGP() *JobStatus {
	o, err := q.One(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return o
}

func (q jobStatusQuery) OneP(exec boil.Executor) *JobStatus {
	o, err := q.One(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return o
}

func (q jobStatusQuery) One(exec boil.Executor) (*JobStatus, error) {
	o := &JobStatus{}

	queries.SetLimit(q.Query, 1)

	err := q.Bind(nil, exec, o)
	if err != nil {
		if errors.Cause(err) == sql.ErrNoRows {
			return nil, sql.ErrNoRows
		}
		return nil, errors.Wrap(err, "model: failed to execute a one query for job_status")
	}

	return o, nil
}

func (q jobStatusQuery) AllG() (JobStatusSlice, error) {
	return q.All(boil.GetDB())
}

func (q jobStatusQuery) AllGP() JobStatusSlice {
	o, err := q.All(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return o
}

func (q jobStatusQuery) AllP(exec boil.Executor) JobStatusSlice {
	o, err := q.All(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return o
}

func (q jobStatusQuery) All(exec boil.Executor) (JobStatusSlice, error) {
	var o []*JobStatus

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

	return o, nil
}

func (q jobStatusQuery) CountG() (int64, error) {
	return q.Count(boil.GetDB())
}

func (q jobStatusQuery) CountGP() int64 {
	c, err := q.Count(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return c
}

func (q jobStatusQuery) CountP(exec boil.Executor) int64 {
	c, err := q.Count(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return c
}

func (q jobStatusQuery) Count(exec boil.Executor) (int64, error) {
	var count int64

	queries.SetSelect(q.Query, nil)
	queries.SetCount(q.Query)

	err := q.Query.QueryRow(exec).Scan(&count)
	if err != nil {
		return 0, errors.Wrap(err, "model: failed to count job_status rows")
	}

	return count, nil
}

func (q jobStatusQuery) ExistsG() (bool, error) {
	return q.Exists(boil.GetDB())
}

func (q jobStatusQuery) ExistsGP() bool {
	e, err := q.Exists(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return e
}

func (q jobStatusQuery) ExistsP(exec boil.Executor) bool {
	e, err := q.Exists(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return e
}

func (q jobStatusQuery) Exists(exec boil.Executor) (bool, error) {
	var count int64

	queries.SetSelect(q.Query, nil)
	queries.SetCount(q.Query)
	queries.SetLimit(q.Query, 1)

	err := q.Query.QueryRow(exec).Scan(&count)
	if err != nil {
		return false, errors.Wrap(err, "model: failed to check if job_status exists")
	}

	return count > 0, nil
}

func JobStatuses(mods ...qm.QueryMod) jobStatusQuery {
	mods = append(mods, qm.From("`job_status`"))
	return jobStatusQuery{NewQuery(mods...)}
}

func FindJobStatusG(jobName string, selectCols ...string) (*JobStatus, error) {
	return FindJobStatus(boil.GetDB(), jobName, selectCols...)
}

func FindJobStatusP(exec boil.Executor, jobName string, selectCols ...string) *JobStatus {
	retobj, err := FindJobStatus(exec, jobName, selectCols...)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return retobj
}

func FindJobStatusGP(jobName string, selectCols ...string) *JobStatus {
	retobj, err := FindJobStatus(boil.GetDB(), jobName, selectCols...)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return retobj
}

func FindJobStatus(exec boil.Executor, jobName string, selectCols ...string) (*JobStatus, error) {
	jobStatusObj := &JobStatus{}

	sel := "*"
	if len(selectCols) > 0 {
		sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
	}
	query := fmt.Sprintf(
		"select %s from `job_status` where `job_name`=?", sel,
	)

	q := queries.Raw(query, jobName)

	err := q.Bind(nil, exec, jobStatusObj)
	if err != nil {
		if errors.Cause(err) == sql.ErrNoRows {
			return nil, sql.ErrNoRows
		}
		return nil, errors.Wrap(err, "model: unable to select from job_status")
	}

	return jobStatusObj, nil
}

func (o *JobStatus) InsertG(columns boil.Columns) error {
	return o.Insert(boil.GetDB(), columns)
}

func (o *JobStatus) InsertP(exec boil.Executor, columns boil.Columns) {
	if err := o.Insert(exec, columns); err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) InsertGP(columns boil.Columns) {
	if err := o.Insert(boil.GetDB(), columns); err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) Insert(exec boil.Executor, columns boil.Columns) error {
	if o == nil {
		return errors.New("model: no job_status provided for insertion")
	}

	var err error

	nzDefaults := queries.NonZeroDefaultSet(jobStatusColumnsWithDefault, o)

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

	if !cached {
		wl, returnColumns := columns.InsertColumnSet(
			jobStatusAllColumns,
			jobStatusColumnsWithDefault,
			jobStatusColumnsWithoutDefault,
			nzDefaults,
		)

		cache.valueMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, wl)
		if err != nil {
			return err
		}
		cache.retMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, returnColumns)
		if err != nil {
			return err
		}
		if len(wl) != 0 {
			cache.query = fmt.Sprintf("INSERT INTO `job_status` (`%s`) %%sVALUES (%s)%%s", strings.Join(wl, "`,`"), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
		} else {
			cache.query = "INSERT INTO `job_status` () VALUES ()%s%s"
		}

		var queryOutput, queryReturning string

		if len(cache.retMapping) != 0 {
			cache.retQuery = fmt.Sprintf("SELECT `%s` FROM `job_status` WHERE %s", strings.Join(returnColumns, "`,`"), strmangle.WhereClause("`", "`", 0, jobStatusPrimaryKeyColumns))
		}

		cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning)
	}

	value := reflect.Indirect(reflect.ValueOf(o))
	vals := queries.ValuesFromMapping(value, cache.valueMapping)

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.query)
		fmt.Fprintln(boil.DebugWriter, vals)
	}

	_, err = exec.Exec(cache.query, vals...)

	if err != nil {
		return errors.Wrap(err, "model: unable to insert into job_status")
	}

	var identifierCols []interface{}

	if len(cache.retMapping) == 0 {
		goto CacheNoHooks
	}

	identifierCols = []interface{}{
		o.JobName,
	}

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.retQuery)
		fmt.Fprintln(boil.DebugWriter, identifierCols...)
	}

	err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
	if err != nil {
		return errors.Wrap(err, "model: unable to populate default values for job_status")
	}

CacheNoHooks:
	if !cached {
		jobStatusInsertCacheMut.Lock()
		jobStatusInsertCache[key] = cache
		jobStatusInsertCacheMut.Unlock()
	}

	return nil
}

func (o *JobStatus) UpdateG(columns boil.Columns) error {
	return o.Update(boil.GetDB(), columns)
}

func (o *JobStatus) UpdateP(exec boil.Executor, columns boil.Columns) {
	err := o.Update(exec, columns)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) UpdateGP(columns boil.Columns) {
	err := o.Update(boil.GetDB(), columns)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) Update(exec boil.Executor, columns boil.Columns) error {
	var err error
	key := makeCacheKey(columns, nil)
	jobStatusUpdateCacheMut.RLock()
	cache, cached := jobStatusUpdateCache[key]
	jobStatusUpdateCacheMut.RUnlock()

	if !cached {
		wl := columns.UpdateColumnSet(
			jobStatusAllColumns,
			jobStatusPrimaryKeyColumns,
		)

		if len(wl) == 0 {
			return errors.New("model: unable to update job_status, could not build whitelist")
		}

		cache.query = fmt.Sprintf("UPDATE `job_status` SET %s WHERE %s",
			strmangle.SetParamNames("`", "`", 0, wl),
			strmangle.WhereClause("`", "`", 0, jobStatusPrimaryKeyColumns),
		)
		cache.valueMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, append(wl, jobStatusPrimaryKeyColumns...))
		if err != nil {
			return err
		}
	}

	values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.query)
		fmt.Fprintln(boil.DebugWriter, values)
	}

	_, err = exec.Exec(cache.query, values...)
	if err != nil {
		return errors.Wrap(err, "model: unable to update job_status row")
	}

	if !cached {
		jobStatusUpdateCacheMut.Lock()
		jobStatusUpdateCache[key] = cache
		jobStatusUpdateCacheMut.Unlock()
	}

	return nil
}

func (q jobStatusQuery) UpdateAllP(exec boil.Executor, cols M) {
	err := q.UpdateAll(exec, cols)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (q jobStatusQuery) UpdateAllG(cols M) error {
	return q.UpdateAll(boil.GetDB(), cols)
}

func (q jobStatusQuery) UpdateAll(exec boil.Executor, cols M) error {
	queries.SetUpdate(q.Query, cols)

	_, err := q.Query.Exec(exec)
	if err != nil {
		return errors.Wrap(err, "model: unable to update all for job_status")
	}

	return nil
}

func (o JobStatusSlice) UpdateAllG(cols M) error {
	return o.UpdateAll(boil.GetDB(), cols)
}

func (o JobStatusSlice) UpdateAllGP(cols M) {
	err := o.UpdateAll(boil.GetDB(), cols)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o JobStatusSlice) UpdateAllP(exec boil.Executor, cols M) {
	err := o.UpdateAll(exec, cols)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o JobStatusSlice) UpdateAll(exec boil.Executor, cols M) error {
	ln := int64(len(o))
	if ln == 0 {
		return nil
	}

	if len(cols) == 0 {
		return errors.New("model: 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++
	}

	for _, obj := range o {
		pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), jobStatusPrimaryKeyMapping)
		args = append(args, pkeyArgs...)
	}

	sql := fmt.Sprintf("UPDATE `job_status` SET %s WHERE %s",
		strmangle.SetParamNames("`", "`", 0, colNames),
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, jobStatusPrimaryKeyColumns, len(o)))

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, sql)
		fmt.Fprintln(boil.DebugWriter, args...)
	}

	_, err := exec.Exec(sql, args...)
	if err != nil {
		return errors.Wrap(err, "model: unable to update all in jobStatus slice")
	}

	return nil
}

func (o *JobStatus) UpsertG(updateColumns, insertColumns boil.Columns) error {
	return o.Upsert(boil.GetDB(), updateColumns, insertColumns)
}

func (o *JobStatus) UpsertGP(updateColumns, insertColumns boil.Columns) {
	if err := o.Upsert(boil.GetDB(), updateColumns, insertColumns); err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) UpsertP(exec boil.Executor, updateColumns, insertColumns boil.Columns) {
	if err := o.Upsert(exec, updateColumns, insertColumns); err != nil {
		panic(boil.WrapErr(err))
	}
}

var mySQLJobStatusUniqueColumns = []string{
	"job_name",
}

func (o *JobStatus) Upsert(exec boil.Executor, updateColumns, insertColumns boil.Columns) error {
	if o == nil {
		return errors.New("model: no job_status provided for upsert")
	}

	nzDefaults := queries.NonZeroDefaultSet(jobStatusColumnsWithDefault, o)
	nzUniques := queries.NonZeroDefaultSet(mySQLJobStatusUniqueColumns, o)

	if len(nzUniques) == 0 {
		return errors.New("cannot upsert with a table that cannot conflict on a unique column")
	}

	buf := strmangle.GetBuffer()
	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)
	}
	buf.WriteByte('.')
	for _, c := range nzUniques {
		buf.WriteString(c)
	}
	key := buf.String()
	strmangle.PutBuffer(buf)

	jobStatusUpsertCacheMut.RLock()
	cache, cached := jobStatusUpsertCache[key]
	jobStatusUpsertCacheMut.RUnlock()

	var err error

	if !cached {
		insert, ret := insertColumns.InsertColumnSet(
			jobStatusAllColumns,
			jobStatusColumnsWithDefault,
			jobStatusColumnsWithoutDefault,
			nzDefaults,
		)
		update := updateColumns.UpdateColumnSet(
			jobStatusAllColumns,
			jobStatusPrimaryKeyColumns,
		)

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

		ret = strmangle.SetComplement(ret, nzUniques)
		cache.query = buildUpsertQueryMySQL(dialect, "job_status", update, insert)
		cache.retQuery = fmt.Sprintf(
			"SELECT %s FROM `job_status` WHERE %s",
			strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, ret), ","),
			strmangle.WhereClause("`", "`", 0, nzUniques),
		)

		cache.valueMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, insert)
		if err != nil {
			return err
		}
		if len(ret) != 0 {
			cache.retMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, 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.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.query)
		fmt.Fprintln(boil.DebugWriter, vals)
	}

	_, err = exec.Exec(cache.query, vals...)

	if err != nil {
		return errors.Wrap(err, "model: unable to upsert for job_status")
	}

	var uniqueMap []uint64
	var nzUniqueCols []interface{}

	if len(cache.retMapping) == 0 {
		goto CacheNoHooks
	}

	uniqueMap, err = queries.BindMapping(jobStatusType, jobStatusMapping, nzUniques)
	if err != nil {
		return errors.Wrap(err, "model: unable to retrieve unique values for job_status")
	}
	nzUniqueCols = queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), uniqueMap)

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.retQuery)
		fmt.Fprintln(boil.DebugWriter, nzUniqueCols...)
	}

	err = exec.QueryRow(cache.retQuery, nzUniqueCols...).Scan(returns...)
	if err != nil {
		return errors.Wrap(err, "model: unable to populate default values for job_status")
	}

CacheNoHooks:
	if !cached {
		jobStatusUpsertCacheMut.Lock()
		jobStatusUpsertCache[key] = cache
		jobStatusUpsertCacheMut.Unlock()
	}

	return nil
}

func (o *JobStatus) DeleteG() error {
	return o.Delete(boil.GetDB())
}

func (o *JobStatus) DeleteP(exec boil.Executor) {
	err := o.Delete(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) DeleteGP() {
	err := o.Delete(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) Delete(exec boil.Executor) error {
	if o == nil {
		return errors.New("model: no JobStatus provided for delete")
	}

	args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), jobStatusPrimaryKeyMapping)
	sql := "DELETE FROM `job_status` WHERE `job_name`=?"

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, sql)
		fmt.Fprintln(boil.DebugWriter, args...)
	}

	_, err := exec.Exec(sql, args...)
	if err != nil {
		return errors.Wrap(err, "model: unable to delete from job_status")
	}

	return nil
}

func (q jobStatusQuery) DeleteAllP(exec boil.Executor) {
	err := q.DeleteAll(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (q jobStatusQuery) DeleteAll(exec boil.Executor) error {
	if q.Query == nil {
		return errors.New("model: no jobStatusQuery provided for delete all")
	}

	queries.SetDelete(q.Query)

	_, err := q.Query.Exec(exec)
	if err != nil {
		return errors.Wrap(err, "model: unable to delete all from job_status")
	}

	return nil
}

func (o JobStatusSlice) DeleteAllG() error {
	return o.DeleteAll(boil.GetDB())
}

func (o JobStatusSlice) DeleteAllP(exec boil.Executor) {
	err := o.DeleteAll(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o JobStatusSlice) DeleteAllGP() {
	err := o.DeleteAll(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o JobStatusSlice) DeleteAll(exec boil.Executor) error {
	if len(o) == 0 {
		return nil
	}

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

	sql := "DELETE FROM `job_status` WHERE " +
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, jobStatusPrimaryKeyColumns, len(o))

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, sql)
		fmt.Fprintln(boil.DebugWriter, args)
	}

	_, err := exec.Exec(sql, args...)
	if err != nil {
		return errors.Wrap(err, "model: unable to delete all from jobStatus slice")
	}

	return nil
}

func (o *JobStatus) ReloadG() error {
	if o == nil {
		return errors.New("model: no JobStatus provided for reload")
	}

	return o.Reload(boil.GetDB())
}

func (o *JobStatus) ReloadP(exec boil.Executor) {
	if err := o.Reload(exec); err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) ReloadGP() {
	if err := o.Reload(boil.GetDB()); err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatus) Reload(exec boil.Executor) error {
	ret, err := FindJobStatus(exec, o.JobName)
	if err != nil {
		return err
	}

	*o = *ret
	return nil
}

func (o *JobStatusSlice) ReloadAllG() error {
	if o == nil {
		return errors.New("model: empty JobStatusSlice provided for reload all")
	}

	return o.ReloadAll(boil.GetDB())
}

func (o *JobStatusSlice) ReloadAllP(exec boil.Executor) {
	if err := o.ReloadAll(exec); err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatusSlice) ReloadAllGP() {
	if err := o.ReloadAll(boil.GetDB()); err != nil {
		panic(boil.WrapErr(err))
	}
}

func (o *JobStatusSlice) ReloadAll(exec boil.Executor) error {
	if o == nil || len(*o) == 0 {
		return nil
	}

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

	sql := "SELECT `job_status`.* FROM `job_status` WHERE " +
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, jobStatusPrimaryKeyColumns, len(*o))

	q := queries.Raw(sql, args...)

	err := q.Bind(nil, exec, &slice)
	if err != nil {
		return errors.Wrap(err, "model: unable to reload all in JobStatusSlice")
	}

	*o = slice

	return nil
}

func JobStatusExistsG(jobName string) (bool, error) {
	return JobStatusExists(boil.GetDB(), jobName)
}

func JobStatusExistsP(exec boil.Executor, jobName string) bool {
	e, err := JobStatusExists(exec, jobName)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return e
}

func JobStatusExistsGP(jobName string) bool {
	e, err := JobStatusExists(boil.GetDB(), jobName)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return e
}

func JobStatusExists(exec boil.Executor, jobName string) (bool, error) {
	var exists bool
	sql := "select exists(select 1 from `job_status` where `job_name`=? limit 1)"

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, sql)
		fmt.Fprintln(boil.DebugWriter, jobName)
	}

	row := exec.QueryRow(sql, jobName)

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

	return exists, nil
}
// Code generated by SQLBoiler (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 model

import (
	"database/sql"
	"fmt"
	"reflect"
	"strconv"
	"strings"
	"sync"
	"time"

	"github.com/pkg/errors"
	"github.com/volatiletech/null"
	"github.com/volatiletech/sqlboiler/boil"
	"github.com/volatiletech/sqlboiler/queries"
	"github.com/volatiletech/sqlboiler/queries/qm"
	"github.com/volatiletech/sqlboiler/queries/qmhelper"
	"github.com/volatiletech/sqlboiler/strmangle"
)

// JobStatus is an object representing the database table.
type JobStatus struct {
	JobName      string      `boil:"job_name" json:"job_name" toml:"job_name" yaml:"job_name"`
	LastSync     time.Time   `boil:"last_sync" json:"last_sync" toml:"last_sync" yaml:"last_sync"`
	IsSuccess    bool        `boil:"is_success" json:"is_success" toml:"is_success" yaml:"is_success"`
	ErrorMessage null.String `boil:"error_message" json:"error_message,omitempty" toml:"error_message" yaml:"error_message,omitempty"`
	State        null.JSON   `boil:"state" json:"state,omitempty" toml:"state" yaml:"state,omitempty"`

	R *jobStatusR `boil:"-" json:"-" toml:"-" yaml:"-"`
	L jobStatusL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

var JobStatusColumns = struct {
	JobName      string
	LastSync     string
	IsSuccess    string
	ErrorMessage string
	State        string
}{
	JobName:      "job_name",
	LastSync:     "last_sync",
	IsSuccess:    "is_success",
	ErrorMessage: "error_message",
	State:        "state",
}

// Generated where

var JobStatusWhere = struct {
	JobName      whereHelperstring
	LastSync     whereHelpertime_Time
	IsSuccess    whereHelperbool
	ErrorMessage whereHelpernull_String
	State        whereHelpernull_JSON
}{
	JobName:      whereHelperstring{field: "`job_status`.`job_name`"},
	LastSync:     whereHelpertime_Time{field: "`job_status`.`last_sync`"},
	IsSuccess:    whereHelperbool{field: "`job_status`.`is_success`"},
	ErrorMessage: whereHelpernull_String{field: "`job_status`.`error_message`"},
	State:        whereHelpernull_JSON{field: "`job_status`.`state`"},
}

// JobStatusRels is where relationship names are stored.
var JobStatusRels = struct {
}{}

// jobStatusR is where relationships are stored.
type jobStatusR struct {
}

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

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

var (
	jobStatusAllColumns            = []string{"job_name", "last_sync", "is_success", "error_message", "state"}
	jobStatusColumnsWithoutDefault = []string{"job_name", "error_message", "state"}
	jobStatusColumnsWithDefault    = []string{"last_sync", "is_success"}
	jobStatusPrimaryKeyColumns     = []string{"job_name"}
)

type (
	// JobStatusSlice is an alias for a slice of pointers to JobStatus.
	// This should generally be used opposed to []JobStatus.
	JobStatusSlice []*JobStatus

	jobStatusQuery struct {
		*queries.Query
	}
)

// Cache for insert, update and upsert
var (
	jobStatusType                 = reflect.TypeOf(&JobStatus{})
	jobStatusMapping              = queries.MakeStructMapping(jobStatusType)
	jobStatusPrimaryKeyMapping, _ = queries.BindMapping(jobStatusType, jobStatusMapping, jobStatusPrimaryKeyColumns)
	jobStatusInsertCacheMut       sync.RWMutex
	jobStatusInsertCache          = make(map[string]insertCache)
	jobStatusUpdateCacheMut       sync.RWMutex
	jobStatusUpdateCache          = make(map[string]updateCache)
	jobStatusUpsertCacheMut       sync.RWMutex
	jobStatusUpsertCache          = 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
)

// OneG returns a single jobStatus record from the query using the global executor.
func (q jobStatusQuery) OneG() (*JobStatus, error) {
	return q.One(boil.GetDB())
}

// OneGP returns a single jobStatus record from the query using the global executor, and panics on error.
func (q jobStatusQuery) OneGP() *JobStatus {
	o, err := q.One(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return o
}

// OneP returns a single jobStatus record from the query, and panics on error.
func (q jobStatusQuery) OneP(exec boil.Executor) *JobStatus {
	o, err := q.One(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return o
}

// One returns a single jobStatus record from the query.
func (q jobStatusQuery) One(exec boil.Executor) (*JobStatus, error) {
	o := &JobStatus{}

	queries.SetLimit(q.Query, 1)

	err := q.Bind(nil, exec, o)
	if err != nil {
		if errors.Cause(err) == sql.ErrNoRows {
			return nil, sql.ErrNoRows
		}
		return nil, errors.Wrap(err, "model: failed to execute a one query for job_status")
	}

	return o, nil
}

// AllG returns all JobStatus records from the query using the global executor.
func (q jobStatusQuery) AllG() (JobStatusSlice, error) {
	return q.All(boil.GetDB())
}

// AllGP returns all JobStatus records from the query using the global executor, and panics on error.
func (q jobStatusQuery) AllGP() JobStatusSlice {
	o, err := q.All(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return o
}

// AllP returns all JobStatus records from the query, and panics on error.
func (q jobStatusQuery) AllP(exec boil.Executor) JobStatusSlice {
	o, err := q.All(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return o
}

// All returns all JobStatus records from the query.
func (q jobStatusQuery) All(exec boil.Executor) (JobStatusSlice, error) {
	var o []*JobStatus

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

	return o, nil
}

// CountG returns the count of all JobStatus records in the query, and panics on error.
func (q jobStatusQuery) CountG() (int64, error) {
	return q.Count(boil.GetDB())
}

// CountGP returns the count of all JobStatus records in the query using the global executor, and panics on error.
func (q jobStatusQuery) CountGP() int64 {
	c, err := q.Count(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return c
}

// CountP returns the count of all JobStatus records in the query, and panics on error.
func (q jobStatusQuery) CountP(exec boil.Executor) int64 {
	c, err := q.Count(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return c
}

// Count returns the count of all JobStatus records in the query.
func (q jobStatusQuery) Count(exec boil.Executor) (int64, error) {
	var count int64

	queries.SetSelect(q.Query, nil)
	queries.SetCount(q.Query)

	err := q.Query.QueryRow(exec).Scan(&count)
	if err != nil {
		return 0, errors.Wrap(err, "model: failed to count job_status rows")
	}

	return count, nil
}

// ExistsG checks if the row exists in the table, and panics on error.
func (q jobStatusQuery) ExistsG() (bool, error) {
	return q.Exists(boil.GetDB())
}

// ExistsGP checks if the row exists in the table using the global executor, and panics on error.
func (q jobStatusQuery) ExistsGP() bool {
	e, err := q.Exists(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return e
}

// ExistsP checks if the row exists in the table, and panics on error.
func (q jobStatusQuery) ExistsP(exec boil.Executor) bool {
	e, err := q.Exists(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return e
}

// Exists checks if the row exists in the table.
func (q jobStatusQuery) Exists(exec boil.Executor) (bool, error) {
	var count int64

	queries.SetSelect(q.Query, nil)
	queries.SetCount(q.Query)
	queries.SetLimit(q.Query, 1)

	err := q.Query.QueryRow(exec).Scan(&count)
	if err != nil {
		return false, errors.Wrap(err, "model: failed to check if job_status exists")
	}

	return count > 0, nil
}

// JobStatuses retrieves all the records using an executor.
func JobStatuses(mods ...qm.QueryMod) jobStatusQuery {
	mods = append(mods, qm.From("`job_status`"))
	return jobStatusQuery{NewQuery(mods...)}
}

// FindJobStatusG retrieves a single record by ID.
func FindJobStatusG(jobName string, selectCols ...string) (*JobStatus, error) {
	return FindJobStatus(boil.GetDB(), jobName, selectCols...)
}

// FindJobStatusP retrieves a single record by ID with an executor, and panics on error.
func FindJobStatusP(exec boil.Executor, jobName string, selectCols ...string) *JobStatus {
	retobj, err := FindJobStatus(exec, jobName, selectCols...)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return retobj
}

// FindJobStatusGP retrieves a single record by ID, and panics on error.
func FindJobStatusGP(jobName string, selectCols ...string) *JobStatus {
	retobj, err := FindJobStatus(boil.GetDB(), jobName, selectCols...)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return retobj
}

// FindJobStatus retrieves a single record by ID with an executor.
// If selectCols is empty Find will return all columns.
func FindJobStatus(exec boil.Executor, jobName string, selectCols ...string) (*JobStatus, error) {
	jobStatusObj := &JobStatus{}

	sel := "*"
	if len(selectCols) > 0 {
		sel = strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, selectCols), ",")
	}
	query := fmt.Sprintf(
		"select %s from `job_status` where `job_name`=?", sel,
	)

	q := queries.Raw(query, jobName)

	err := q.Bind(nil, exec, jobStatusObj)
	if err != nil {
		if errors.Cause(err) == sql.ErrNoRows {
			return nil, sql.ErrNoRows
		}
		return nil, errors.Wrap(err, "model: unable to select from job_status")
	}

	return jobStatusObj, nil
}

// InsertG a single record. See Insert for whitelist behavior description.
func (o *JobStatus) InsertG(columns boil.Columns) error {
	return o.Insert(boil.GetDB(), columns)
}

// InsertP a single record using an executor, and panics on error. See Insert
// for whitelist behavior description.
func (o *JobStatus) InsertP(exec boil.Executor, columns boil.Columns) {
	if err := o.Insert(exec, columns); err != nil {
		panic(boil.WrapErr(err))
	}
}

// InsertGP a single record, and panics on error. See Insert for whitelist
// behavior description.
func (o *JobStatus) InsertGP(columns boil.Columns) {
	if err := o.Insert(boil.GetDB(), columns); err != nil {
		panic(boil.WrapErr(err))
	}
}

// Insert a single record using an executor.
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
func (o *JobStatus) Insert(exec boil.Executor, columns boil.Columns) error {
	if o == nil {
		return errors.New("model: no job_status provided for insertion")
	}

	var err error

	nzDefaults := queries.NonZeroDefaultSet(jobStatusColumnsWithDefault, o)

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

	if !cached {
		wl, returnColumns := columns.InsertColumnSet(
			jobStatusAllColumns,
			jobStatusColumnsWithDefault,
			jobStatusColumnsWithoutDefault,
			nzDefaults,
		)

		cache.valueMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, wl)
		if err != nil {
			return err
		}
		cache.retMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, returnColumns)
		if err != nil {
			return err
		}
		if len(wl) != 0 {
			cache.query = fmt.Sprintf("INSERT INTO `job_status` (`%s`) %%sVALUES (%s)%%s", strings.Join(wl, "`,`"), strmangle.Placeholders(dialect.UseIndexPlaceholders, len(wl), 1, 1))
		} else {
			cache.query = "INSERT INTO `job_status` () VALUES ()%s%s"
		}

		var queryOutput, queryReturning string

		if len(cache.retMapping) != 0 {
			cache.retQuery = fmt.Sprintf("SELECT `%s` FROM `job_status` WHERE %s", strings.Join(returnColumns, "`,`"), strmangle.WhereClause("`", "`", 0, jobStatusPrimaryKeyColumns))
		}

		cache.query = fmt.Sprintf(cache.query, queryOutput, queryReturning)
	}

	value := reflect.Indirect(reflect.ValueOf(o))
	vals := queries.ValuesFromMapping(value, cache.valueMapping)

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.query)
		fmt.Fprintln(boil.DebugWriter, vals)
	}

	_, err = exec.Exec(cache.query, vals...)

	if err != nil {
		return errors.Wrap(err, "model: unable to insert into job_status")
	}

	var identifierCols []interface{}

	if len(cache.retMapping) == 0 {
		goto CacheNoHooks
	}

	identifierCols = []interface{}{
		o.JobName,
	}

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.retQuery)
		fmt.Fprintln(boil.DebugWriter, identifierCols...)
	}

	err = exec.QueryRow(cache.retQuery, identifierCols...).Scan(queries.PtrsFromMapping(value, cache.retMapping)...)
	if err != nil {
		return errors.Wrap(err, "model: unable to populate default values for job_status")
	}

CacheNoHooks:
	if !cached {
		jobStatusInsertCacheMut.Lock()
		jobStatusInsertCache[key] = cache
		jobStatusInsertCacheMut.Unlock()
	}

	return nil
}

// UpdateG a single JobStatus record using the global executor.
// See Update for more documentation.
func (o *JobStatus) UpdateG(columns boil.Columns) error {
	return o.Update(boil.GetDB(), columns)
}

// UpdateP uses an executor to update the JobStatus, and panics on error.
// See Update for more documentation.
func (o *JobStatus) UpdateP(exec boil.Executor, columns boil.Columns) {
	err := o.Update(exec, columns)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// UpdateGP a single JobStatus record using the global executor. Panics on error.
// See Update for more documentation.
func (o *JobStatus) UpdateGP(columns boil.Columns) {
	err := o.Update(boil.GetDB(), columns)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// Update uses an executor to update the JobStatus.
// 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 *JobStatus) Update(exec boil.Executor, columns boil.Columns) error {
	var err error
	key := makeCacheKey(columns, nil)
	jobStatusUpdateCacheMut.RLock()
	cache, cached := jobStatusUpdateCache[key]
	jobStatusUpdateCacheMut.RUnlock()

	if !cached {
		wl := columns.UpdateColumnSet(
			jobStatusAllColumns,
			jobStatusPrimaryKeyColumns,
		)

		if len(wl) == 0 {
			return errors.New("model: unable to update job_status, could not build whitelist")
		}

		cache.query = fmt.Sprintf("UPDATE `job_status` SET %s WHERE %s",
			strmangle.SetParamNames("`", "`", 0, wl),
			strmangle.WhereClause("`", "`", 0, jobStatusPrimaryKeyColumns),
		)
		cache.valueMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, append(wl, jobStatusPrimaryKeyColumns...))
		if err != nil {
			return err
		}
	}

	values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping)

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.query)
		fmt.Fprintln(boil.DebugWriter, values)
	}

	_, err = exec.Exec(cache.query, values...)
	if err != nil {
		return errors.Wrap(err, "model: unable to update job_status row")
	}

	if !cached {
		jobStatusUpdateCacheMut.Lock()
		jobStatusUpdateCache[key] = cache
		jobStatusUpdateCacheMut.Unlock()
	}

	return nil
}

// UpdateAllP updates all rows with matching column names, and panics on error.
func (q jobStatusQuery) UpdateAllP(exec boil.Executor, cols M) {
	err := q.UpdateAll(exec, cols)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// UpdateAllG updates all rows with the specified column values.
func (q jobStatusQuery) UpdateAllG(cols M) error {
	return q.UpdateAll(boil.GetDB(), cols)
}

// UpdateAll updates all rows with the specified column values.
func (q jobStatusQuery) UpdateAll(exec boil.Executor, cols M) error {
	queries.SetUpdate(q.Query, cols)

	_, err := q.Query.Exec(exec)
	if err != nil {
		return errors.Wrap(err, "model: unable to update all for job_status")
	}

	return nil
}

// UpdateAllG updates all rows with the specified column values.
func (o JobStatusSlice) UpdateAllG(cols M) error {
	return o.UpdateAll(boil.GetDB(), cols)
}

// UpdateAllGP updates all rows with the specified column values, and panics on error.
func (o JobStatusSlice) UpdateAllGP(cols M) {
	err := o.UpdateAll(boil.GetDB(), cols)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// UpdateAllP updates all rows with the specified column values, and panics on error.
func (o JobStatusSlice) UpdateAllP(exec boil.Executor, cols M) {
	err := o.UpdateAll(exec, cols)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// UpdateAll updates all rows with the specified column values, using an executor.
func (o JobStatusSlice) UpdateAll(exec boil.Executor, cols M) error {
	ln := int64(len(o))
	if ln == 0 {
		return nil
	}

	if len(cols) == 0 {
		return errors.New("model: 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)), jobStatusPrimaryKeyMapping)
		args = append(args, pkeyArgs...)
	}

	sql := fmt.Sprintf("UPDATE `job_status` SET %s WHERE %s",
		strmangle.SetParamNames("`", "`", 0, colNames),
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, jobStatusPrimaryKeyColumns, len(o)))

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, sql)
		fmt.Fprintln(boil.DebugWriter, args...)
	}

	_, err := exec.Exec(sql, args...)
	if err != nil {
		return errors.Wrap(err, "model: unable to update all in jobStatus slice")
	}

	return nil
}

// UpsertG attempts an insert, and does an update or ignore on conflict.
func (o *JobStatus) UpsertG(updateColumns, insertColumns boil.Columns) error {
	return o.Upsert(boil.GetDB(), updateColumns, insertColumns)
}

// UpsertGP attempts an insert, and does an update or ignore on conflict. Panics on error.
func (o *JobStatus) UpsertGP(updateColumns, insertColumns boil.Columns) {
	if err := o.Upsert(boil.GetDB(), updateColumns, insertColumns); err != nil {
		panic(boil.WrapErr(err))
	}
}

// UpsertP attempts an insert using an executor, and does an update or ignore on conflict.
// UpsertP panics on error.
func (o *JobStatus) UpsertP(exec boil.Executor, updateColumns, insertColumns boil.Columns) {
	if err := o.Upsert(exec, updateColumns, insertColumns); err != nil {
		panic(boil.WrapErr(err))
	}
}

var mySQLJobStatusUniqueColumns = []string{
	"job_name",
}

// 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 *JobStatus) Upsert(exec boil.Executor, updateColumns, insertColumns boil.Columns) error {
	if o == nil {
		return errors.New("model: no job_status provided for upsert")
	}

	nzDefaults := queries.NonZeroDefaultSet(jobStatusColumnsWithDefault, o)
	nzUniques := queries.NonZeroDefaultSet(mySQLJobStatusUniqueColumns, o)

	if len(nzUniques) == 0 {
		return errors.New("cannot upsert with a table that cannot conflict on a unique column")
	}

	// Build cache key in-line uglily - mysql vs psql problems
	buf := strmangle.GetBuffer()
	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)
	}
	buf.WriteByte('.')
	for _, c := range nzUniques {
		buf.WriteString(c)
	}
	key := buf.String()
	strmangle.PutBuffer(buf)

	jobStatusUpsertCacheMut.RLock()
	cache, cached := jobStatusUpsertCache[key]
	jobStatusUpsertCacheMut.RUnlock()

	var err error

	if !cached {
		insert, ret := insertColumns.InsertColumnSet(
			jobStatusAllColumns,
			jobStatusColumnsWithDefault,
			jobStatusColumnsWithoutDefault,
			nzDefaults,
		)
		update := updateColumns.UpdateColumnSet(
			jobStatusAllColumns,
			jobStatusPrimaryKeyColumns,
		)

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

		ret = strmangle.SetComplement(ret, nzUniques)
		cache.query = buildUpsertQueryMySQL(dialect, "job_status", update, insert)
		cache.retQuery = fmt.Sprintf(
			"SELECT %s FROM `job_status` WHERE %s",
			strings.Join(strmangle.IdentQuoteSlice(dialect.LQ, dialect.RQ, ret), ","),
			strmangle.WhereClause("`", "`", 0, nzUniques),
		)

		cache.valueMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, insert)
		if err != nil {
			return err
		}
		if len(ret) != 0 {
			cache.retMapping, err = queries.BindMapping(jobStatusType, jobStatusMapping, 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.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.query)
		fmt.Fprintln(boil.DebugWriter, vals)
	}

	_, err = exec.Exec(cache.query, vals...)

	if err != nil {
		return errors.Wrap(err, "model: unable to upsert for job_status")
	}

	var uniqueMap []uint64
	var nzUniqueCols []interface{}

	if len(cache.retMapping) == 0 {
		goto CacheNoHooks
	}

	uniqueMap, err = queries.BindMapping(jobStatusType, jobStatusMapping, nzUniques)
	if err != nil {
		return errors.Wrap(err, "model: unable to retrieve unique values for job_status")
	}
	nzUniqueCols = queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), uniqueMap)

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, cache.retQuery)
		fmt.Fprintln(boil.DebugWriter, nzUniqueCols...)
	}

	err = exec.QueryRow(cache.retQuery, nzUniqueCols...).Scan(returns...)
	if err != nil {
		return errors.Wrap(err, "model: unable to populate default values for job_status")
	}

CacheNoHooks:
	if !cached {
		jobStatusUpsertCacheMut.Lock()
		jobStatusUpsertCache[key] = cache
		jobStatusUpsertCacheMut.Unlock()
	}

	return nil
}

// DeleteG deletes a single JobStatus record.
// DeleteG will match against the primary key column to find the record to delete.
func (o *JobStatus) DeleteG() error {
	return o.Delete(boil.GetDB())
}

// DeleteP deletes a single JobStatus record with an executor.
// DeleteP will match against the primary key column to find the record to delete.
// Panics on error.
func (o *JobStatus) DeleteP(exec boil.Executor) {
	err := o.Delete(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// DeleteGP deletes a single JobStatus record.
// DeleteGP will match against the primary key column to find the record to delete.
// Panics on error.
func (o *JobStatus) DeleteGP() {
	err := o.Delete(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// Delete deletes a single JobStatus record with an executor.
// Delete will match against the primary key column to find the record to delete.
func (o *JobStatus) Delete(exec boil.Executor) error {
	if o == nil {
		return errors.New("model: no JobStatus provided for delete")
	}

	args := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), jobStatusPrimaryKeyMapping)
	sql := "DELETE FROM `job_status` WHERE `job_name`=?"

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, sql)
		fmt.Fprintln(boil.DebugWriter, args...)
	}

	_, err := exec.Exec(sql, args...)
	if err != nil {
		return errors.Wrap(err, "model: unable to delete from job_status")
	}

	return nil
}

// DeleteAllP deletes all rows, and panics on error.
func (q jobStatusQuery) DeleteAllP(exec boil.Executor) {
	err := q.DeleteAll(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// DeleteAll deletes all matching rows.
func (q jobStatusQuery) DeleteAll(exec boil.Executor) error {
	if q.Query == nil {
		return errors.New("model: no jobStatusQuery provided for delete all")
	}

	queries.SetDelete(q.Query)

	_, err := q.Query.Exec(exec)
	if err != nil {
		return errors.Wrap(err, "model: unable to delete all from job_status")
	}

	return nil
}

// DeleteAllG deletes all rows in the slice.
func (o JobStatusSlice) DeleteAllG() error {
	return o.DeleteAll(boil.GetDB())
}

// DeleteAllP deletes all rows in the slice, using an executor, and panics on error.
func (o JobStatusSlice) DeleteAllP(exec boil.Executor) {
	err := o.DeleteAll(exec)
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// DeleteAllGP deletes all rows in the slice, and panics on error.
func (o JobStatusSlice) DeleteAllGP() {
	err := o.DeleteAll(boil.GetDB())
	if err != nil {
		panic(boil.WrapErr(err))
	}
}

// DeleteAll deletes all rows in the slice, using an executor.
func (o JobStatusSlice) DeleteAll(exec boil.Executor) error {
	if len(o) == 0 {
		return nil
	}

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

	sql := "DELETE FROM `job_status` WHERE " +
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, jobStatusPrimaryKeyColumns, len(o))

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, sql)
		fmt.Fprintln(boil.DebugWriter, args)
	}

	_, err := exec.Exec(sql, args...)
	if err != nil {
		return errors.Wrap(err, "model: unable to delete all from jobStatus slice")
	}

	return nil
}

// ReloadG refetches the object from the database using the primary keys.
func (o *JobStatus) ReloadG() error {
	if o == nil {
		return errors.New("model: no JobStatus provided for reload")
	}

	return o.Reload(boil.GetDB())
}

// ReloadP refetches the object from the database with an executor. Panics on error.
func (o *JobStatus) ReloadP(exec boil.Executor) {
	if err := o.Reload(exec); err != nil {
		panic(boil.WrapErr(err))
	}
}

// ReloadGP refetches the object from the database and panics on error.
func (o *JobStatus) ReloadGP() {
	if err := o.Reload(boil.GetDB()); err != nil {
		panic(boil.WrapErr(err))
	}
}

// Reload refetches the object from the database
// using the primary keys with an executor.
func (o *JobStatus) Reload(exec boil.Executor) error {
	ret, err := FindJobStatus(exec, o.JobName)
	if err != nil {
		return err
	}

	*o = *ret
	return nil
}

// ReloadAllG refetches every row with matching primary key column values
// and overwrites the original object slice with the newly updated slice.
func (o *JobStatusSlice) ReloadAllG() error {
	if o == nil {
		return errors.New("model: empty JobStatusSlice provided for reload all")
	}

	return o.ReloadAll(boil.GetDB())
}

// ReloadAllP refetches every row with matching primary key column values
// and overwrites the original object slice with the newly updated slice.
// Panics on error.
func (o *JobStatusSlice) ReloadAllP(exec boil.Executor) {
	if err := o.ReloadAll(exec); err != nil {
		panic(boil.WrapErr(err))
	}
}

// ReloadAllGP refetches every row with matching primary key column values
// and overwrites the original object slice with the newly updated slice.
// Panics on error.
func (o *JobStatusSlice) ReloadAllGP() {
	if err := o.ReloadAll(boil.GetDB()); err != nil {
		panic(boil.WrapErr(err))
	}
}

// ReloadAll refetches every row with matching primary key column values
// and overwrites the original object slice with the newly updated slice.
func (o *JobStatusSlice) ReloadAll(exec boil.Executor) error {
	if o == nil || len(*o) == 0 {
		return nil
	}

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

	sql := "SELECT `job_status`.* FROM `job_status` WHERE " +
		strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), 0, jobStatusPrimaryKeyColumns, len(*o))

	q := queries.Raw(sql, args...)

	err := q.Bind(nil, exec, &slice)
	if err != nil {
		return errors.Wrap(err, "model: unable to reload all in JobStatusSlice")
	}

	*o = slice

	return nil
}

// JobStatusExistsG checks if the JobStatus row exists.
func JobStatusExistsG(jobName string) (bool, error) {
	return JobStatusExists(boil.GetDB(), jobName)
}

// JobStatusExistsP checks if the JobStatus row exists. Panics on error.
func JobStatusExistsP(exec boil.Executor, jobName string) bool {
	e, err := JobStatusExists(exec, jobName)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return e
}

// JobStatusExistsGP checks if the JobStatus row exists. Panics on error.
func JobStatusExistsGP(jobName string) bool {
	e, err := JobStatusExists(boil.GetDB(), jobName)
	if err != nil {
		panic(boil.WrapErr(err))
	}

	return e
}

// JobStatusExists checks if the JobStatus row exists.
func JobStatusExists(exec boil.Executor, jobName string) (bool, error) {
	var exists bool
	sql := "select exists(select 1 from `job_status` where `job_name`=? limit 1)"

	if boil.DebugMode {
		fmt.Fprintln(boil.DebugWriter, sql)
		fmt.Fprintln(boil.DebugWriter, jobName)
	}

	row := exec.QueryRow(sql, jobName)

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

	return exists, nil
}
