// Code generated by ent, DO NOT EDIT. package ent import ( "context" "database/sql/driver" "fmt" "math" "tel-commerce/ent/business" "tel-commerce/ent/businesscategory" "tel-commerce/ent/predicate" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // BusinessCategoryQuery is the builder for querying BusinessCategory entities. type BusinessCategoryQuery struct { config ctx *QueryContext order []businesscategory.OrderOption inters []Interceptor predicates []predicate.BusinessCategory 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 BusinessCategoryQuery builder. func (bcq *BusinessCategoryQuery) Where(ps ...predicate.BusinessCategory) *BusinessCategoryQuery { bcq.predicates = append(bcq.predicates, ps...) return bcq } // Limit the number of records to be returned by this query. func (bcq *BusinessCategoryQuery) Limit(limit int) *BusinessCategoryQuery { bcq.ctx.Limit = &limit return bcq } // Offset to start from. func (bcq *BusinessCategoryQuery) Offset(offset int) *BusinessCategoryQuery { 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 *BusinessCategoryQuery) Unique(unique bool) *BusinessCategoryQuery { bcq.ctx.Unique = &unique return bcq } // Order specifies how the records should be ordered. func (bcq *BusinessCategoryQuery) Order(o ...businesscategory.OrderOption) *BusinessCategoryQuery { bcq.order = append(bcq.order, o...) return bcq } // QueryBusinesses chains the current query on the "businesses" edge. func (bcq *BusinessCategoryQuery) 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(businesscategory.Table, businesscategory.FieldID, selector), sqlgraph.To(business.Table, business.FieldID), sqlgraph.Edge(sqlgraph.M2M, false, businesscategory.BusinessesTable, businesscategory.BusinessesPrimaryKey...), ) fromU = sqlgraph.SetNeighbors(bcq.driver.Dialect(), step) return fromU, nil } return query } // First returns the first BusinessCategory entity from the query. // Returns a *NotFoundError when no BusinessCategory was found. func (bcq *BusinessCategoryQuery) First(ctx context.Context) (*BusinessCategory, 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{businesscategory.Label} } return nodes[0], nil } // FirstX is like First, but panics if an error occurs. func (bcq *BusinessCategoryQuery) FirstX(ctx context.Context) *BusinessCategory { node, err := bcq.First(ctx) if err != nil && !IsNotFound(err) { panic(err) } return node } // FirstID returns the first BusinessCategory ID from the query. // Returns a *NotFoundError when no BusinessCategory ID was found. func (bcq *BusinessCategoryQuery) 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{businesscategory.Label} return } return ids[0], nil } // FirstIDX is like FirstID, but panics if an error occurs. func (bcq *BusinessCategoryQuery) FirstIDX(ctx context.Context) int { id, err := bcq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) } return id } // Only returns a single BusinessCategory entity found by the query, ensuring it only returns one. // Returns a *NotSingularError when more than one BusinessCategory entity is found. // Returns a *NotFoundError when no BusinessCategory entities are found. func (bcq *BusinessCategoryQuery) Only(ctx context.Context) (*BusinessCategory, 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{businesscategory.Label} default: return nil, &NotSingularError{businesscategory.Label} } } // OnlyX is like Only, but panics if an error occurs. func (bcq *BusinessCategoryQuery) OnlyX(ctx context.Context) *BusinessCategory { node, err := bcq.Only(ctx) if err != nil { panic(err) } return node } // OnlyID is like Only, but returns the only BusinessCategory ID in the query. // Returns a *NotSingularError when more than one BusinessCategory ID is found. // Returns a *NotFoundError when no entities are found. func (bcq *BusinessCategoryQuery) 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{businesscategory.Label} default: err = &NotSingularError{businesscategory.Label} } return } // OnlyIDX is like OnlyID, but panics if an error occurs. func (bcq *BusinessCategoryQuery) 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 BusinessCategories. func (bcq *BusinessCategoryQuery) All(ctx context.Context) ([]*BusinessCategory, error) { ctx = setContextOp(ctx, bcq.ctx, "All") if err := bcq.prepareQuery(ctx); err != nil { return nil, err } qr := querierAll[[]*BusinessCategory, *BusinessCategoryQuery]() return withInterceptors[[]*BusinessCategory](ctx, bcq, qr, bcq.inters) } // AllX is like All, but panics if an error occurs. func (bcq *BusinessCategoryQuery) AllX(ctx context.Context) []*BusinessCategory { nodes, err := bcq.All(ctx) if err != nil { panic(err) } return nodes } // IDs executes the query and returns a list of BusinessCategory IDs. func (bcq *BusinessCategoryQuery) 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(businesscategory.FieldID).Scan(ctx, &ids); err != nil { return nil, err } return ids, nil } // IDsX is like IDs, but panics if an error occurs. func (bcq *BusinessCategoryQuery) 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 *BusinessCategoryQuery) 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[*BusinessCategoryQuery](), bcq.inters) } // CountX is like Count, but panics if an error occurs. func (bcq *BusinessCategoryQuery) 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 *BusinessCategoryQuery) 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 *BusinessCategoryQuery) ExistX(ctx context.Context) bool { exist, err := bcq.Exist(ctx) if err != nil { panic(err) } return exist } // Clone returns a duplicate of the BusinessCategoryQuery 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 *BusinessCategoryQuery) Clone() *BusinessCategoryQuery { if bcq == nil { return nil } return &BusinessCategoryQuery{ config: bcq.config, ctx: bcq.ctx.Clone(), order: append([]businesscategory.OrderOption{}, bcq.order...), inters: append([]Interceptor{}, bcq.inters...), predicates: append([]predicate.BusinessCategory{}, 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 *BusinessCategoryQuery) WithBusinesses(opts ...func(*BusinessQuery)) *BusinessCategoryQuery { 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 { // Slug string `json:"slug,omitempty"` // Count int `json:"count,omitempty"` // } // // client.BusinessCategory.Query(). // GroupBy(businesscategory.FieldSlug). // Aggregate(ent.Count()). // Scan(ctx, &v) func (bcq *BusinessCategoryQuery) GroupBy(field string, fields ...string) *BusinessCategoryGroupBy { bcq.ctx.Fields = append([]string{field}, fields...) grbuild := &BusinessCategoryGroupBy{build: bcq} grbuild.flds = &bcq.ctx.Fields grbuild.label = businesscategory.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 { // Slug string `json:"slug,omitempty"` // } // // client.BusinessCategory.Query(). // Select(businesscategory.FieldSlug). // Scan(ctx, &v) func (bcq *BusinessCategoryQuery) Select(fields ...string) *BusinessCategorySelect { bcq.ctx.Fields = append(bcq.ctx.Fields, fields...) sbuild := &BusinessCategorySelect{BusinessCategoryQuery: bcq} sbuild.label = businesscategory.Label sbuild.flds, sbuild.scan = &bcq.ctx.Fields, sbuild.Scan return sbuild } // Aggregate returns a BusinessCategorySelect configured with the given aggregations. func (bcq *BusinessCategoryQuery) Aggregate(fns ...AggregateFunc) *BusinessCategorySelect { return bcq.Select().Aggregate(fns...) } func (bcq *BusinessCategoryQuery) 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 !businesscategory.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 *BusinessCategoryQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*BusinessCategory, error) { var ( nodes = []*BusinessCategory{} _spec = bcq.querySpec() loadedTypes = [1]bool{ bcq.withBusinesses != nil, } ) _spec.ScanValues = func(columns []string) ([]any, error) { return (*BusinessCategory).scanValues(nil, columns) } _spec.Assign = func(columns []string, values []any) error { node := &BusinessCategory{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, func(n *BusinessCategory) { n.Edges.Businesses = []*Business{} }, func(n *BusinessCategory, e *Business) { n.Edges.Businesses = append(n.Edges.Businesses, e) }); err != nil { return nil, err } } return nodes, nil } func (bcq *BusinessCategoryQuery) loadBusinesses(ctx context.Context, query *BusinessQuery, nodes []*BusinessCategory, init func(*BusinessCategory), assign func(*BusinessCategory, *Business)) error { edgeIDs := make([]driver.Value, len(nodes)) byID := make(map[int]*BusinessCategory) nids := make(map[int]map[*BusinessCategory]struct{}) for i, node := range nodes { edgeIDs[i] = node.ID byID[node.ID] = node if init != nil { init(node) } } query.Where(func(s *sql.Selector) { joinT := sql.Table(businesscategory.BusinessesTable) s.Join(joinT).On(s.C(business.FieldID), joinT.C(businesscategory.BusinessesPrimaryKey[1])) s.Where(sql.InValues(joinT.C(businesscategory.BusinessesPrimaryKey[0]), edgeIDs...)) columns := s.SelectedColumns() s.Select(joinT.C(businesscategory.BusinessesPrimaryKey[0])) s.AppendSelect(columns...) s.SetDistinct(false) }) if err := query.prepareQuery(ctx); err != nil { return err } qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) { return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) { assign := spec.Assign values := spec.ScanValues spec.ScanValues = func(columns []string) ([]any, error) { values, err := values(columns[1:]) if err != nil { return nil, err } return append([]any{new(sql.NullInt64)}, values...), nil } spec.Assign = func(columns []string, values []any) error { outValue := int(values[0].(*sql.NullInt64).Int64) inValue := int(values[1].(*sql.NullInt64).Int64) if nids[inValue] == nil { nids[inValue] = map[*BusinessCategory]struct{}{byID[outValue]: {}} return assign(columns[1:], values[1:]) } nids[inValue][byID[outValue]] = struct{}{} return nil } }) }) neighbors, err := withInterceptors[[]*Business](ctx, query, qr, query.inters) if err != nil { return err } for _, n := range neighbors { nodes, ok := nids[n.ID] if !ok { return fmt.Errorf(`unexpected "businesses" node returned %v`, n.ID) } for kn := range nodes { assign(kn, n) } } return nil } func (bcq *BusinessCategoryQuery) 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 *BusinessCategoryQuery) querySpec() *sqlgraph.QuerySpec { _spec := sqlgraph.NewQuerySpec(businesscategory.Table, businesscategory.Columns, sqlgraph.NewFieldSpec(businesscategory.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, businesscategory.FieldID) for i := range fields { if fields[i] != businesscategory.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) } } } 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 *BusinessCategoryQuery) sqlQuery(ctx context.Context) *sql.Selector { builder := sql.Dialect(bcq.driver.Dialect()) t1 := builder.Table(businesscategory.Table) columns := bcq.ctx.Fields if len(columns) == 0 { columns = businesscategory.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 } // BusinessCategoryGroupBy is the group-by builder for BusinessCategory entities. type BusinessCategoryGroupBy struct { selector build *BusinessCategoryQuery } // Aggregate adds the given aggregation functions to the group-by query. func (bcgb *BusinessCategoryGroupBy) Aggregate(fns ...AggregateFunc) *BusinessCategoryGroupBy { bcgb.fns = append(bcgb.fns, fns...) return bcgb } // Scan applies the selector query and scans the result into the given value. func (bcgb *BusinessCategoryGroupBy) 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[*BusinessCategoryQuery, *BusinessCategoryGroupBy](ctx, bcgb.build, bcgb, bcgb.build.inters, v) } func (bcgb *BusinessCategoryGroupBy) sqlScan(ctx context.Context, root *BusinessCategoryQuery, 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) } // BusinessCategorySelect is the builder for selecting fields of BusinessCategory entities. type BusinessCategorySelect struct { *BusinessCategoryQuery selector } // Aggregate adds the given aggregation functions to the selector query. func (bcs *BusinessCategorySelect) Aggregate(fns ...AggregateFunc) *BusinessCategorySelect { bcs.fns = append(bcs.fns, fns...) return bcs } // Scan applies the selector query and scans the result into the given value. func (bcs *BusinessCategorySelect) Scan(ctx context.Context, v any) error { ctx = setContextOp(ctx, bcs.ctx, "Select") if err := bcs.prepareQuery(ctx); err != nil { return err } return scanWithInterceptors[*BusinessCategoryQuery, *BusinessCategorySelect](ctx, bcs.BusinessCategoryQuery, bcs, bcs.inters, v) } func (bcs *BusinessCategorySelect) sqlScan(ctx context.Context, root *BusinessCategoryQuery, 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) }