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

// ViewSearch is an object representing the database table.
type ViewSearch struct {
	ID         null.Int32  `boil:"id" json:"id,omitempty" toml:"id" yaml:"id,omitempty"`
	Type       null.String `boil:"type" json:"type,omitempty" toml:"type" yaml:"type,omitempty"`
	ProjectID  null.Int32  `boil:"project_id" json:"project_id,omitempty" toml:"project_id" yaml:"project_id,omitempty"`
	ScenarioID null.Int32  `boil:"scenario_id" json:"scenario_id,omitempty" toml:"scenario_id" yaml:"scenario_id,omitempty"`
	Data       null.String `boil:"data" json:"data,omitempty" toml:"data" yaml:"data,omitempty"`
}

var ViewSearchColumns = struct {
	ID         string
	Type       string
	ProjectID  string
	ScenarioID string
	Data       string
}{
	ID:         "id",
	Type:       "type",
	ProjectID:  "project_id",
	ScenarioID: "scenario_id",
	Data:       "data",
}

var ViewSearchTableColumns = struct {
	ID         string
	Type       string
	ProjectID  string
	ScenarioID string
	Data       string
}{
	ID:         "view_search.id",
	Type:       "view_search.type",
	ProjectID:  "view_search.project_id",
	ScenarioID: "view_search.scenario_id",
	Data:       "view_search.data",
}

// Generated where

var ViewSearchWhere = struct {
	ID         whereHelpernull_Int32
	Type       whereHelpernull_String
	ProjectID  whereHelpernull_Int32
	ScenarioID whereHelpernull_Int32
	Data       whereHelpernull_String
}{
	ID:         whereHelpernull_Int32{field: "\"view_search\".\"id\""},
	Type:       whereHelpernull_String{field: "\"view_search\".\"type\""},
	ProjectID:  whereHelpernull_Int32{field: "\"view_search\".\"project_id\""},
	ScenarioID: whereHelpernull_Int32{field: "\"view_search\".\"scenario_id\""},
	Data:       whereHelpernull_String{field: "\"view_search\".\"data\""},
}

var (
	viewSearchAllColumns            = []string{"id", "type", "project_id", "scenario_id", "data"}
	viewSearchColumnsWithoutDefault = []string{}
	viewSearchColumnsWithDefault    = []string{"id", "type", "project_id", "scenario_id", "data"}
	viewSearchPrimaryKeyColumns     = []string{}
	viewSearchGeneratedColumns      = []string{}
)

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

	viewSearchQuery struct {
		*queries.Query
	}
)

// Cache for insert, update and upsert
var (
	viewSearchType           = reflect.TypeOf(&ViewSearch{})
	viewSearchMapping        = queries.MakeStructMapping(viewSearchType)
	viewSearchInsertCacheMut sync.RWMutex
	viewSearchInsertCache    = make(map[string]insertCache)
	viewSearchUpdateCacheMut sync.RWMutex
	viewSearchUpdateCache    = make(map[string]updateCache)
	viewSearchUpsertCacheMut sync.RWMutex
	viewSearchUpsertCache    = 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
	// These are used in some views
	_ = fmt.Sprintln("")
	_ = reflect.Int
	_ = strings.Builder{}
	_ = sync.Mutex{}
	_ = strmangle.Plural("")
	_ = strconv.IntSize
)

var viewSearchAfterSelectMu sync.Mutex
var viewSearchAfterSelectHooks []ViewSearchHook

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

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

	return nil
}

// AddViewSearchHook registers your hook function for all future operations.
func AddViewSearchHook(hookPoint boil.HookPoint, viewSearchHook ViewSearchHook) {
	switch hookPoint {
	case boil.AfterSelectHook:
		viewSearchAfterSelectMu.Lock()
		viewSearchAfterSelectHooks = append(viewSearchAfterSelectHooks, viewSearchHook)
		viewSearchAfterSelectMu.Unlock()
	}
}

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

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

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

	return o, nil
}

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

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

	if len(viewSearchAfterSelectHooks) != 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 ViewSearch records in the query.
func (q viewSearchQuery) 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 view_search rows")
	}

	return count, nil
}

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

	return count > 0, nil
}

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

	return viewSearchQuery{q}
}
