online-order/ent/businessconfig_query.go
2023-10-29 02:42:07 +03:30

609 lines
18 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"math"
"online-order/ent/business"
"online-order/ent/businessconfig"
"online-order/ent/predicate"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// BusinessConfigQuery is the builder for querying BusinessConfig entities.
type BusinessConfigQuery struct {
config
ctx *QueryContext
order []businessconfig.OrderOption
inters []Interceptor
predicates []predicate.BusinessConfig
withBusinesses *BusinessQuery
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the BusinessConfigQuery builder.
func (bcq *BusinessConfigQuery) Where(ps ...predicate.BusinessConfig) *BusinessConfigQuery {
bcq.predicates = append(bcq.predicates, ps...)
return bcq
}
// Limit the number of records to be returned by this query.
func (bcq *BusinessConfigQuery) Limit(limit int) *BusinessConfigQuery {
bcq.ctx.Limit = &limit
return bcq
}
// Offset to start from.
func (bcq *BusinessConfigQuery) Offset(offset int) *BusinessConfigQuery {
bcq.ctx.Offset = &offset
return bcq
}
// Unique configures the query builder to filter duplicate records on query.
// By default, unique is set to true, and can be disabled using this method.
func (bcq *BusinessConfigQuery) Unique(unique bool) *BusinessConfigQuery {
bcq.ctx.Unique = &unique
return bcq
}
// Order specifies how the records should be ordered.
func (bcq *BusinessConfigQuery) Order(o ...businessconfig.OrderOption) *BusinessConfigQuery {
bcq.order = append(bcq.order, o...)
return bcq
}
// QueryBusinesses chains the current query on the "businesses" edge.
func (bcq *BusinessConfigQuery) QueryBusinesses() *BusinessQuery {
query := (&BusinessClient{config: bcq.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := bcq.prepareQuery(ctx); err != nil {
return nil, err
}
selector := bcq.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(businessconfig.Table, businessconfig.FieldID, selector),
sqlgraph.To(business.Table, business.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, businessconfig.BusinessesTable, businessconfig.BusinessesColumn),
)
fromU = sqlgraph.SetNeighbors(bcq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first BusinessConfig entity from the query.
// Returns a *NotFoundError when no BusinessConfig was found.
func (bcq *BusinessConfigQuery) First(ctx context.Context) (*BusinessConfig, error) {
nodes, err := bcq.Limit(1).All(setContextOp(ctx, bcq.ctx, "First"))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{businessconfig.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (bcq *BusinessConfigQuery) FirstX(ctx context.Context) *BusinessConfig {
node, err := bcq.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first BusinessConfig ID from the query.
// Returns a *NotFoundError when no BusinessConfig ID was found.
func (bcq *BusinessConfigQuery) FirstID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = bcq.Limit(1).IDs(setContextOp(ctx, bcq.ctx, "FirstID")); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{businessconfig.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (bcq *BusinessConfigQuery) FirstIDX(ctx context.Context) int {
id, err := bcq.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single BusinessConfig entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one BusinessConfig entity is found.
// Returns a *NotFoundError when no BusinessConfig entities are found.
func (bcq *BusinessConfigQuery) Only(ctx context.Context) (*BusinessConfig, error) {
nodes, err := bcq.Limit(2).All(setContextOp(ctx, bcq.ctx, "Only"))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{businessconfig.Label}
default:
return nil, &NotSingularError{businessconfig.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (bcq *BusinessConfigQuery) OnlyX(ctx context.Context) *BusinessConfig {
node, err := bcq.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only BusinessConfig ID in the query.
// Returns a *NotSingularError when more than one BusinessConfig ID is found.
// Returns a *NotFoundError when no entities are found.
func (bcq *BusinessConfigQuery) OnlyID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = bcq.Limit(2).IDs(setContextOp(ctx, bcq.ctx, "OnlyID")); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{businessconfig.Label}
default:
err = &NotSingularError{businessconfig.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (bcq *BusinessConfigQuery) OnlyIDX(ctx context.Context) int {
id, err := bcq.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of BusinessConfigs.
func (bcq *BusinessConfigQuery) All(ctx context.Context) ([]*BusinessConfig, error) {
ctx = setContextOp(ctx, bcq.ctx, "All")
if err := bcq.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*BusinessConfig, *BusinessConfigQuery]()
return withInterceptors[[]*BusinessConfig](ctx, bcq, qr, bcq.inters)
}
// AllX is like All, but panics if an error occurs.
func (bcq *BusinessConfigQuery) AllX(ctx context.Context) []*BusinessConfig {
nodes, err := bcq.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of BusinessConfig IDs.
func (bcq *BusinessConfigQuery) IDs(ctx context.Context) (ids []int, err error) {
if bcq.ctx.Unique == nil && bcq.path != nil {
bcq.Unique(true)
}
ctx = setContextOp(ctx, bcq.ctx, "IDs")
if err = bcq.Select(businessconfig.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (bcq *BusinessConfigQuery) IDsX(ctx context.Context) []int {
ids, err := bcq.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (bcq *BusinessConfigQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, bcq.ctx, "Count")
if err := bcq.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, bcq, querierCount[*BusinessConfigQuery](), bcq.inters)
}
// CountX is like Count, but panics if an error occurs.
func (bcq *BusinessConfigQuery) CountX(ctx context.Context) int {
count, err := bcq.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (bcq *BusinessConfigQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, bcq.ctx, "Exist")
switch _, err := bcq.FirstID(ctx); {
case IsNotFound(err):
return false, nil
case err != nil:
return false, fmt.Errorf("ent: check existence: %w", err)
default:
return true, nil
}
}
// ExistX is like Exist, but panics if an error occurs.
func (bcq *BusinessConfigQuery) ExistX(ctx context.Context) bool {
exist, err := bcq.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the BusinessConfigQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (bcq *BusinessConfigQuery) Clone() *BusinessConfigQuery {
if bcq == nil {
return nil
}
return &BusinessConfigQuery{
config: bcq.config,
ctx: bcq.ctx.Clone(),
order: append([]businessconfig.OrderOption{}, bcq.order...),
inters: append([]Interceptor{}, bcq.inters...),
predicates: append([]predicate.BusinessConfig{}, bcq.predicates...),
withBusinesses: bcq.withBusinesses.Clone(),
// clone intermediate query.
sql: bcq.sql.Clone(),
path: bcq.path,
}
}
// WithBusinesses tells the query-builder to eager-load the nodes that are connected to
// the "businesses" edge. The optional arguments are used to configure the query builder of the edge.
func (bcq *BusinessConfigQuery) WithBusinesses(opts ...func(*BusinessQuery)) *BusinessConfigQuery {
query := (&BusinessClient{config: bcq.config}).Query()
for _, opt := range opts {
opt(query)
}
bcq.withBusinesses = query
return bcq
}
// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: count, max, mean, min, sum.
//
// Example:
//
// var v []struct {
// Company string `json:"company,omitempty"`
// Count int `json:"count,omitempty"`
// }
//
// client.BusinessConfig.Query().
// GroupBy(businessconfig.FieldCompany).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (bcq *BusinessConfigQuery) GroupBy(field string, fields ...string) *BusinessConfigGroupBy {
bcq.ctx.Fields = append([]string{field}, fields...)
grbuild := &BusinessConfigGroupBy{build: bcq}
grbuild.flds = &bcq.ctx.Fields
grbuild.label = businessconfig.Label
grbuild.scan = grbuild.Scan
return grbuild
}
// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
//
// Example:
//
// var v []struct {
// Company string `json:"company,omitempty"`
// }
//
// client.BusinessConfig.Query().
// Select(businessconfig.FieldCompany).
// Scan(ctx, &v)
func (bcq *BusinessConfigQuery) Select(fields ...string) *BusinessConfigSelect {
bcq.ctx.Fields = append(bcq.ctx.Fields, fields...)
sbuild := &BusinessConfigSelect{BusinessConfigQuery: bcq}
sbuild.label = businessconfig.Label
sbuild.flds, sbuild.scan = &bcq.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a BusinessConfigSelect configured with the given aggregations.
func (bcq *BusinessConfigQuery) Aggregate(fns ...AggregateFunc) *BusinessConfigSelect {
return bcq.Select().Aggregate(fns...)
}
func (bcq *BusinessConfigQuery) prepareQuery(ctx context.Context) error {
for _, inter := range bcq.inters {
if inter == nil {
return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
}
if trv, ok := inter.(Traverser); ok {
if err := trv.Traverse(ctx, bcq); err != nil {
return err
}
}
}
for _, f := range bcq.ctx.Fields {
if !businessconfig.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if bcq.path != nil {
prev, err := bcq.path(ctx)
if err != nil {
return err
}
bcq.sql = prev
}
return nil
}
func (bcq *BusinessConfigQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*BusinessConfig, error) {
var (
nodes = []*BusinessConfig{}
_spec = bcq.querySpec()
loadedTypes = [1]bool{
bcq.withBusinesses != nil,
}
)
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*BusinessConfig).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &BusinessConfig{config: bcq.config}
nodes = append(nodes, node)
node.Edges.loadedTypes = loadedTypes
return node.assignValues(columns, values)
}
for i := range hooks {
hooks[i](ctx, _spec)
}
if err := sqlgraph.QueryNodes(ctx, bcq.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := bcq.withBusinesses; query != nil {
if err := bcq.loadBusinesses(ctx, query, nodes, nil,
func(n *BusinessConfig, e *Business) { n.Edges.Businesses = e }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (bcq *BusinessConfigQuery) loadBusinesses(ctx context.Context, query *BusinessQuery, nodes []*BusinessConfig, init func(*BusinessConfig), assign func(*BusinessConfig, *Business)) error {
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*BusinessConfig)
for i := range nodes {
if nodes[i].BusinessID == nil {
continue
}
fk := *nodes[i].BusinessID
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(business.IDIn(ids...))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
nodes, ok := nodeids[n.ID]
if !ok {
return fmt.Errorf(`unexpected foreign-key "business_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (bcq *BusinessConfigQuery) sqlCount(ctx context.Context) (int, error) {
_spec := bcq.querySpec()
_spec.Node.Columns = bcq.ctx.Fields
if len(bcq.ctx.Fields) > 0 {
_spec.Unique = bcq.ctx.Unique != nil && *bcq.ctx.Unique
}
return sqlgraph.CountNodes(ctx, bcq.driver, _spec)
}
func (bcq *BusinessConfigQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(businessconfig.Table, businessconfig.Columns, sqlgraph.NewFieldSpec(businessconfig.FieldID, field.TypeInt))
_spec.From = bcq.sql
if unique := bcq.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if bcq.path != nil {
_spec.Unique = true
}
if fields := bcq.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, businessconfig.FieldID)
for i := range fields {
if fields[i] != businessconfig.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
if bcq.withBusinesses != nil {
_spec.Node.AddColumnOnce(businessconfig.FieldBusinessID)
}
}
if ps := bcq.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := bcq.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := bcq.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := bcq.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (bcq *BusinessConfigQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(bcq.driver.Dialect())
t1 := builder.Table(businessconfig.Table)
columns := bcq.ctx.Fields
if len(columns) == 0 {
columns = businessconfig.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if bcq.sql != nil {
selector = bcq.sql
selector.Select(selector.Columns(columns...)...)
}
if bcq.ctx.Unique != nil && *bcq.ctx.Unique {
selector.Distinct()
}
for _, p := range bcq.predicates {
p(selector)
}
for _, p := range bcq.order {
p(selector)
}
if offset := bcq.ctx.Offset; offset != nil {
// limit is mandatory for offset clause. We start
// with default value, and override it below if needed.
selector.Offset(*offset).Limit(math.MaxInt32)
}
if limit := bcq.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// BusinessConfigGroupBy is the group-by builder for BusinessConfig entities.
type BusinessConfigGroupBy struct {
selector
build *BusinessConfigQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (bcgb *BusinessConfigGroupBy) Aggregate(fns ...AggregateFunc) *BusinessConfigGroupBy {
bcgb.fns = append(bcgb.fns, fns...)
return bcgb
}
// Scan applies the selector query and scans the result into the given value.
func (bcgb *BusinessConfigGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, bcgb.build.ctx, "GroupBy")
if err := bcgb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*BusinessConfigQuery, *BusinessConfigGroupBy](ctx, bcgb.build, bcgb, bcgb.build.inters, v)
}
func (bcgb *BusinessConfigGroupBy) sqlScan(ctx context.Context, root *BusinessConfigQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(bcgb.fns))
for _, fn := range bcgb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*bcgb.flds)+len(bcgb.fns))
for _, f := range *bcgb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*bcgb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := bcgb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// BusinessConfigSelect is the builder for selecting fields of BusinessConfig entities.
type BusinessConfigSelect struct {
*BusinessConfigQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (bcs *BusinessConfigSelect) Aggregate(fns ...AggregateFunc) *BusinessConfigSelect {
bcs.fns = append(bcs.fns, fns...)
return bcs
}
// Scan applies the selector query and scans the result into the given value.
func (bcs *BusinessConfigSelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, bcs.ctx, "Select")
if err := bcs.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*BusinessConfigQuery, *BusinessConfigSelect](ctx, bcs.BusinessConfigQuery, bcs, bcs.inters, v)
}
func (bcs *BusinessConfigSelect) sqlScan(ctx context.Context, root *BusinessConfigQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(bcs.fns))
for _, fn := range bcs.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*bcs.selector.flds); {
case n == 0 && len(aggregation) > 0:
selector.Select(aggregation...)
case n != 0 && len(aggregation) > 0:
selector.AppendSelect(aggregation...)
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := bcs.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}