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

// StatisticDump is an object representing the database table.
type StatisticDump struct {
	StatisticDumpID int32     `boil:"statistic_dump_id" json:"statistic_dump_id" toml:"statistic_dump_id" yaml:"statistic_dump_id"`
	CreatedAt       time.Time `boil:"created_at" json:"created_at" toml:"created_at" yaml:"created_at"`

	R *statisticDumpR `boil:"-" json:"-" toml:"-" yaml:"-"`
	L statisticDumpL  `boil:"-" json:"-" toml:"-" yaml:"-"`
}

var StatisticDumpColumns = struct {
	StatisticDumpID string
	CreatedAt       string
}{
	StatisticDumpID: "statistic_dump_id",
	CreatedAt:       "created_at",
}

var StatisticDumpTableColumns = struct {
	StatisticDumpID string
	CreatedAt       string
}{
	StatisticDumpID: "statistic_dump.statistic_dump_id",
	CreatedAt:       "statistic_dump.created_at",
}

// Generated where

var StatisticDumpWhere = struct {
	StatisticDumpID whereHelperint32
	CreatedAt       whereHelpertime_Time
}{
	StatisticDumpID: whereHelperint32{field: "\"statistic_dump\".\"statistic_dump_id\""},
	CreatedAt:       whereHelpertime_Time{field: "\"statistic_dump\".\"created_at\""},
}

// StatisticDumpRels is where relationship names are stored.
var StatisticDumpRels = struct {
	Statistics string
}{
	Statistics: "Statistics",
}

// statisticDumpR is where relationships are stored.
type statisticDumpR struct {
	Statistics StatisticSlice `boil:"Statistics" json:"Statistics" toml:"Statistics" yaml:"Statistics"`
}

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

func (o *StatisticDump) GetStatistics() StatisticSlice {
	if o == nil {
		return nil
	}

	return o.R.GetStatistics()
}

func (r *statisticDumpR) GetStatistics() StatisticSlice {
	if r == nil {
		return nil
	}

	return r.Statistics
}

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

var (
	statisticDumpAllColumns            = []string{"statistic_dump_id", "created_at"}
	statisticDumpColumnsWithoutDefault = []string{}
	statisticDumpColumnsWithDefault    = []string{"statistic_dump_id", "created_at"}
	statisticDumpPrimaryKeyColumns     = []string{"statistic_dump_id"}
	statisticDumpGeneratedColumns      = []string{}
)

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

	statisticDumpQuery struct {
		*queries.Query
	}
)

// Cache for insert, update and upsert
var (
	statisticDumpType                 = reflect.TypeOf(&StatisticDump{})
	statisticDumpMapping              = queries.MakeStructMapping(statisticDumpType)
	statisticDumpPrimaryKeyMapping, _ = queries.BindMapping(statisticDumpType, statisticDumpMapping, statisticDumpPrimaryKeyColumns)
	statisticDumpInsertCacheMut       sync.RWMutex
	statisticDumpInsertCache          = make(map[string]insertCache)
	statisticDumpUpdateCacheMut       sync.RWMutex
	statisticDumpUpdateCache          = make(map[string]updateCache)
	statisticDumpUpsertCacheMut       sync.RWMutex
	statisticDumpUpsertCache          = 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 statisticDumpAfterSelectMu sync.Mutex
var statisticDumpAfterSelectHooks []StatisticDumpHook

var statisticDumpBeforeInsertMu sync.Mutex
var statisticDumpBeforeInsertHooks []StatisticDumpHook
var statisticDumpAfterInsertMu sync.Mutex
var statisticDumpAfterInsertHooks []StatisticDumpHook

var statisticDumpBeforeUpdateMu sync.Mutex
var statisticDumpBeforeUpdateHooks []StatisticDumpHook
var statisticDumpAfterUpdateMu sync.Mutex
var statisticDumpAfterUpdateHooks []StatisticDumpHook

var statisticDumpBeforeDeleteMu sync.Mutex
var statisticDumpBeforeDeleteHooks []StatisticDumpHook
var statisticDumpAfterDeleteMu sync.Mutex
var statisticDumpAfterDeleteHooks []StatisticDumpHook

var statisticDumpBeforeUpsertMu sync.Mutex
var statisticDumpBeforeUpsertHooks []StatisticDumpHook
var statisticDumpAfterUpsertMu sync.Mutex
var statisticDumpAfterUpsertHooks []StatisticDumpHook

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

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

	return nil
}

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

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

	return nil
}

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

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

	return nil
}

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

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

	return nil
}

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

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

	return nil
}

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

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

	return nil
}

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

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

	return nil
}

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

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

	return nil
}

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

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

	return nil
}

// AddStatisticDumpHook registers your hook function for all future operations.
func AddStatisticDumpHook(hookPoint boil.HookPoint, statisticDumpHook StatisticDumpHook) {
	switch hookPoint {
	case boil.AfterSelectHook:
		statisticDumpAfterSelectMu.Lock()
		statisticDumpAfterSelectHooks = append(statisticDumpAfterSelectHooks, statisticDumpHook)
		statisticDumpAfterSelectMu.Unlock()
	case boil.BeforeInsertHook:
		statisticDumpBeforeInsertMu.Lock()
		statisticDumpBeforeInsertHooks = append(statisticDumpBeforeInsertHooks, statisticDumpHook)
		statisticDumpBeforeInsertMu.Unlock()
	case boil.AfterInsertHook:
		statisticDumpAfterInsertMu.Lock()
		statisticDumpAfterInsertHooks = append(statisticDumpAfterInsertHooks, statisticDumpHook)
		statisticDumpAfterInsertMu.Unlock()
	case boil.BeforeUpdateHook:
		statisticDumpBeforeUpdateMu.Lock()
		statisticDumpBeforeUpdateHooks = append(statisticDumpBeforeUpdateHooks, statisticDumpHook)
		statisticDumpBeforeUpdateMu.Unlock()
	case boil.AfterUpdateHook:
		statisticDumpAfterUpdateMu.Lock()
		statisticDumpAfterUpdateHooks = append(statisticDumpAfterUpdateHooks, statisticDumpHook)
		statisticDumpAfterUpdateMu.Unlock()
	case boil.BeforeDeleteHook:
		statisticDumpBeforeDeleteMu.Lock()
		statisticDumpBeforeDeleteHooks = append(statisticDumpBeforeDeleteHooks, statisticDumpHook)
		statisticDumpBeforeDeleteMu.Unlock()
	case boil.AfterDeleteHook:
		statisticDumpAfterDeleteMu.Lock()
		statisticDumpAfterDeleteHooks = append(statisticDumpAfterDeleteHooks, statisticDumpHook)
		statisticDumpAfterDeleteMu.Unlock()
	case boil.BeforeUpsertHook:
		statisticDumpBeforeUpsertMu.Lock()
		statisticDumpBeforeUpsertHooks = append(statisticDumpBeforeUpsertHooks, statisticDumpHook)
		statisticDumpBeforeUpsertMu.Unlock()
	case boil.AfterUpsertHook:
		statisticDumpAfterUpsertMu.Lock()
		statisticDumpAfterUpsertHooks = append(statisticDumpAfterUpsertHooks, statisticDumpHook)
		statisticDumpAfterUpsertMu.Unlock()
	}
}

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

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

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

	return o, nil
}

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

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

	if len(statisticDumpAfterSelectHooks) != 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 StatisticDump records in the query.
func (q statisticDumpQuery) 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 statistic_dump rows")
	}

	return count, nil
}

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

	return count > 0, nil
}

// Statistics retrieves all the statistic's Statistics with an executor.
func (o *StatisticDump) Statistics(mods ...qm.QueryMod) statisticQuery {
	var queryMods []qm.QueryMod
	if len(mods) != 0 {
		queryMods = append(queryMods, mods...)
	}

	queryMods = append(queryMods,
		qm.Where("\"statistic\".\"statistic_dump_id\"=?", o.StatisticDumpID),
	)

	return Statistics(queryMods...)
}

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

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

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

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

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

	if len(statisticAfterSelectHooks) != 0 {
		for _, obj := range resultSlice {
			if err := obj.doAfterSelectHooks(ctx, e); err != nil {
				return err
			}
		}
	}
	if singular {
		object.R.Statistics = resultSlice
		for _, foreign := range resultSlice {
			if foreign.R == nil {
				foreign.R = &statisticR{}
			}
			foreign.R.StatisticDump = object
		}
		return nil
	}

	for _, foreign := range resultSlice {
		for _, local := range slice {
			if local.StatisticDumpID == foreign.StatisticDumpID {
				local.R.Statistics = append(local.R.Statistics, foreign)
				if foreign.R == nil {
					foreign.R = &statisticR{}
				}
				foreign.R.StatisticDump = local
				break
			}
		}
	}

	return nil
}

// AddStatistics adds the given related objects to the existing relationships
// of the statistic_dump, optionally inserting them as new records.
// Appends related to o.R.Statistics.
// Sets related.R.StatisticDump appropriately.
func (o *StatisticDump) AddStatistics(ctx context.Context, exec boil.ContextExecutor, insert bool, related ...*Statistic) error {
	var err error
	for _, rel := range related {
		if insert {
			rel.StatisticDumpID = o.StatisticDumpID
			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 \"statistic\" SET %s WHERE %s",
				strmangle.SetParamNames("\"", "\"", 1, []string{"statistic_dump_id"}),
				strmangle.WhereClause("\"", "\"", 2, statisticPrimaryKeyColumns),
			)
			values := []interface{}{o.StatisticDumpID, rel.StatisticID}

			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.StatisticDumpID = o.StatisticDumpID
		}
	}

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

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

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

	return statisticDumpQuery{q}
}

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

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

	q := queries.Raw(query, statisticDumpID)

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

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

	return statisticDumpObj, nil
}

// Insert a single record using an executor.
// See boil.Columns.InsertColumnSet documentation to understand column list inference for inserts.
func (o *StatisticDump) Insert(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) error {
	if o == nil {
		return errors.New("models: no statistic_dump provided for insertion")
	}

	var err error
	if !boil.TimestampsAreSkipped(ctx) {
		currTime := time.Now().In(boil.GetLocation())

		if o.CreatedAt.IsZero() {
			o.CreatedAt = currTime
		}
	}

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

	nzDefaults := queries.NonZeroDefaultSet(statisticDumpColumnsWithDefault, o)

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

	if !cached {
		wl, returnColumns := columns.InsertColumnSet(
			statisticDumpAllColumns,
			statisticDumpColumnsWithDefault,
			statisticDumpColumnsWithoutDefault,
			nzDefaults,
		)

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

	if !cached {
		statisticDumpInsertCacheMut.Lock()
		statisticDumpInsertCache[key] = cache
		statisticDumpInsertCacheMut.Unlock()
	}

	return o.doAfterInsertHooks(ctx, exec)
}

// Update uses an executor to update the StatisticDump.
// 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 *StatisticDump) Update(ctx context.Context, exec boil.ContextExecutor, columns boil.Columns) (int64, error) {
	var err error
	if err = o.doBeforeUpdateHooks(ctx, exec); err != nil {
		return 0, err
	}
	key := makeCacheKey(columns, nil)
	statisticDumpUpdateCacheMut.RLock()
	cache, cached := statisticDumpUpdateCache[key]
	statisticDumpUpdateCacheMut.RUnlock()

	if !cached {
		wl := columns.UpdateColumnSet(
			statisticDumpAllColumns,
			statisticDumpPrimaryKeyColumns,
		)

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

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

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

	if !cached {
		statisticDumpUpdateCacheMut.Lock()
		statisticDumpUpdateCache[key] = cache
		statisticDumpUpdateCacheMut.Unlock()
	}

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

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

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

	return rowsAff, nil
}

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

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

	rowsAff, err := result.RowsAffected()
	if err != nil {
		return 0, errors.Wrap(err, "models: unable to retrieve rows affected all in update all statisticDump")
	}
	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 *StatisticDump) 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 statistic_dump provided for upsert")
	}
	if !boil.TimestampsAreSkipped(ctx) {
		currTime := time.Now().In(boil.GetLocation())

		if o.CreatedAt.IsZero() {
			o.CreatedAt = currTime
		}
	}

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

	nzDefaults := queries.NonZeroDefaultSet(statisticDumpColumnsWithDefault, 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)

	statisticDumpUpsertCacheMut.RLock()
	cache, cached := statisticDumpUpsertCache[key]
	statisticDumpUpsertCacheMut.RUnlock()

	var err error

	if !cached {
		insert, _ := insertColumns.InsertColumnSet(
			statisticDumpAllColumns,
			statisticDumpColumnsWithDefault,
			statisticDumpColumnsWithoutDefault,
			nzDefaults,
		)

		update := updateColumns.UpdateColumnSet(
			statisticDumpAllColumns,
			statisticDumpPrimaryKeyColumns,
		)

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

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

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

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

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

	if !cached {
		statisticDumpUpsertCacheMut.Lock()
		statisticDumpUpsertCache[key] = cache
		statisticDumpUpsertCacheMut.Unlock()
	}

	return o.doAfterUpsertHooks(ctx, exec)
}

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

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

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

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

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

	return rowsAff, nil
}

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

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

	return rowsAff, nil
}

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

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

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

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

	if len(statisticDumpAfterDeleteHooks) != 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 *StatisticDump) Reload(ctx context.Context, exec boil.ContextExecutor) error {
	ret, err := FindStatisticDump(ctx, exec, o.StatisticDumpID)
	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 *StatisticDumpSlice) ReloadAll(ctx context.Context, exec boil.ContextExecutor) error {
	if o == nil || len(*o) == 0 {
		return nil
	}

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

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

	*o = slice

	return nil
}

// StatisticDumpExists checks if the StatisticDump row exists.
func StatisticDumpExists(ctx context.Context, exec boil.ContextExecutor, statisticDumpID int32) (bool, error) {
	var exists bool
	sql := "select exists(select 1 from \"statistic_dump\" where \"statistic_dump_id\"=$1 limit 1)"

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

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

	return exists, nil
}

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