// Code generated by ent, DO NOT EDIT. package ent import ( "context" "database/sql/driver" "fmt" "math" "tss-rocks-be/ent/daily" "tss-rocks-be/ent/dailycategory" "tss-rocks-be/ent/dailycategorycontent" "tss-rocks-be/ent/predicate" "entgo.io/ent" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // DailyCategoryQuery is the builder for querying DailyCategory entities. type DailyCategoryQuery struct { config ctx *QueryContext order []dailycategory.OrderOption inters []Interceptor predicates []predicate.DailyCategory withContents *DailyCategoryContentQuery withDailyItems *DailyQuery // intermediate query (i.e. traversal path). sql *sql.Selector path func(context.Context) (*sql.Selector, error) } // Where adds a new predicate for the DailyCategoryQuery builder. func (dcq *DailyCategoryQuery) Where(ps ...predicate.DailyCategory) *DailyCategoryQuery { dcq.predicates = append(dcq.predicates, ps...) return dcq } // Limit the number of records to be returned by this query. func (dcq *DailyCategoryQuery) Limit(limit int) *DailyCategoryQuery { dcq.ctx.Limit = &limit return dcq } // Offset to start from. func (dcq *DailyCategoryQuery) Offset(offset int) *DailyCategoryQuery { dcq.ctx.Offset = &offset return dcq } // 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 (dcq *DailyCategoryQuery) Unique(unique bool) *DailyCategoryQuery { dcq.ctx.Unique = &unique return dcq } // Order specifies how the records should be ordered. func (dcq *DailyCategoryQuery) Order(o ...dailycategory.OrderOption) *DailyCategoryQuery { dcq.order = append(dcq.order, o...) return dcq } // QueryContents chains the current query on the "contents" edge. func (dcq *DailyCategoryQuery) QueryContents() *DailyCategoryContentQuery { query := (&DailyCategoryContentClient{config: dcq.config}).Query() query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { if err := dcq.prepareQuery(ctx); err != nil { return nil, err } selector := dcq.sqlQuery(ctx) if err := selector.Err(); err != nil { return nil, err } step := sqlgraph.NewStep( sqlgraph.From(dailycategory.Table, dailycategory.FieldID, selector), sqlgraph.To(dailycategorycontent.Table, dailycategorycontent.FieldID), sqlgraph.Edge(sqlgraph.O2M, false, dailycategory.ContentsTable, dailycategory.ContentsColumn), ) fromU = sqlgraph.SetNeighbors(dcq.driver.Dialect(), step) return fromU, nil } return query } // QueryDailyItems chains the current query on the "daily_items" edge. func (dcq *DailyCategoryQuery) QueryDailyItems() *DailyQuery { query := (&DailyClient{config: dcq.config}).Query() query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { if err := dcq.prepareQuery(ctx); err != nil { return nil, err } selector := dcq.sqlQuery(ctx) if err := selector.Err(); err != nil { return nil, err } step := sqlgraph.NewStep( sqlgraph.From(dailycategory.Table, dailycategory.FieldID, selector), sqlgraph.To(daily.Table, daily.FieldID), sqlgraph.Edge(sqlgraph.O2M, false, dailycategory.DailyItemsTable, dailycategory.DailyItemsColumn), ) fromU = sqlgraph.SetNeighbors(dcq.driver.Dialect(), step) return fromU, nil } return query } // First returns the first DailyCategory entity from the query. // Returns a *NotFoundError when no DailyCategory was found. func (dcq *DailyCategoryQuery) First(ctx context.Context) (*DailyCategory, error) { nodes, err := dcq.Limit(1).All(setContextOp(ctx, dcq.ctx, ent.OpQueryFirst)) if err != nil { return nil, err } if len(nodes) == 0 { return nil, &NotFoundError{dailycategory.Label} } return nodes[0], nil } // FirstX is like First, but panics if an error occurs. func (dcq *DailyCategoryQuery) FirstX(ctx context.Context) *DailyCategory { node, err := dcq.First(ctx) if err != nil && !IsNotFound(err) { panic(err) } return node } // FirstID returns the first DailyCategory ID from the query. // Returns a *NotFoundError when no DailyCategory ID was found. func (dcq *DailyCategoryQuery) FirstID(ctx context.Context) (id int, err error) { var ids []int if ids, err = dcq.Limit(1).IDs(setContextOp(ctx, dcq.ctx, ent.OpQueryFirstID)); err != nil { return } if len(ids) == 0 { err = &NotFoundError{dailycategory.Label} return } return ids[0], nil } // FirstIDX is like FirstID, but panics if an error occurs. func (dcq *DailyCategoryQuery) FirstIDX(ctx context.Context) int { id, err := dcq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) } return id } // Only returns a single DailyCategory entity found by the query, ensuring it only returns one. // Returns a *NotSingularError when more than one DailyCategory entity is found. // Returns a *NotFoundError when no DailyCategory entities are found. func (dcq *DailyCategoryQuery) Only(ctx context.Context) (*DailyCategory, error) { nodes, err := dcq.Limit(2).All(setContextOp(ctx, dcq.ctx, ent.OpQueryOnly)) if err != nil { return nil, err } switch len(nodes) { case 1: return nodes[0], nil case 0: return nil, &NotFoundError{dailycategory.Label} default: return nil, &NotSingularError{dailycategory.Label} } } // OnlyX is like Only, but panics if an error occurs. func (dcq *DailyCategoryQuery) OnlyX(ctx context.Context) *DailyCategory { node, err := dcq.Only(ctx) if err != nil { panic(err) } return node } // OnlyID is like Only, but returns the only DailyCategory ID in the query. // Returns a *NotSingularError when more than one DailyCategory ID is found. // Returns a *NotFoundError when no entities are found. func (dcq *DailyCategoryQuery) OnlyID(ctx context.Context) (id int, err error) { var ids []int if ids, err = dcq.Limit(2).IDs(setContextOp(ctx, dcq.ctx, ent.OpQueryOnlyID)); err != nil { return } switch len(ids) { case 1: id = ids[0] case 0: err = &NotFoundError{dailycategory.Label} default: err = &NotSingularError{dailycategory.Label} } return } // OnlyIDX is like OnlyID, but panics if an error occurs. func (dcq *DailyCategoryQuery) OnlyIDX(ctx context.Context) int { id, err := dcq.OnlyID(ctx) if err != nil { panic(err) } return id } // All executes the query and returns a list of DailyCategories. func (dcq *DailyCategoryQuery) All(ctx context.Context) ([]*DailyCategory, error) { ctx = setContextOp(ctx, dcq.ctx, ent.OpQueryAll) if err := dcq.prepareQuery(ctx); err != nil { return nil, err } qr := querierAll[[]*DailyCategory, *DailyCategoryQuery]() return withInterceptors[[]*DailyCategory](ctx, dcq, qr, dcq.inters) } // AllX is like All, but panics if an error occurs. func (dcq *DailyCategoryQuery) AllX(ctx context.Context) []*DailyCategory { nodes, err := dcq.All(ctx) if err != nil { panic(err) } return nodes } // IDs executes the query and returns a list of DailyCategory IDs. func (dcq *DailyCategoryQuery) IDs(ctx context.Context) (ids []int, err error) { if dcq.ctx.Unique == nil && dcq.path != nil { dcq.Unique(true) } ctx = setContextOp(ctx, dcq.ctx, ent.OpQueryIDs) if err = dcq.Select(dailycategory.FieldID).Scan(ctx, &ids); err != nil { return nil, err } return ids, nil } // IDsX is like IDs, but panics if an error occurs. func (dcq *DailyCategoryQuery) IDsX(ctx context.Context) []int { ids, err := dcq.IDs(ctx) if err != nil { panic(err) } return ids } // Count returns the count of the given query. func (dcq *DailyCategoryQuery) Count(ctx context.Context) (int, error) { ctx = setContextOp(ctx, dcq.ctx, ent.OpQueryCount) if err := dcq.prepareQuery(ctx); err != nil { return 0, err } return withInterceptors[int](ctx, dcq, querierCount[*DailyCategoryQuery](), dcq.inters) } // CountX is like Count, but panics if an error occurs. func (dcq *DailyCategoryQuery) CountX(ctx context.Context) int { count, err := dcq.Count(ctx) if err != nil { panic(err) } return count } // Exist returns true if the query has elements in the graph. func (dcq *DailyCategoryQuery) Exist(ctx context.Context) (bool, error) { ctx = setContextOp(ctx, dcq.ctx, ent.OpQueryExist) switch _, err := dcq.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 (dcq *DailyCategoryQuery) ExistX(ctx context.Context) bool { exist, err := dcq.Exist(ctx) if err != nil { panic(err) } return exist } // Clone returns a duplicate of the DailyCategoryQuery builder, including all associated steps. It can be // used to prepare common query builders and use them differently after the clone is made. func (dcq *DailyCategoryQuery) Clone() *DailyCategoryQuery { if dcq == nil { return nil } return &DailyCategoryQuery{ config: dcq.config, ctx: dcq.ctx.Clone(), order: append([]dailycategory.OrderOption{}, dcq.order...), inters: append([]Interceptor{}, dcq.inters...), predicates: append([]predicate.DailyCategory{}, dcq.predicates...), withContents: dcq.withContents.Clone(), withDailyItems: dcq.withDailyItems.Clone(), // clone intermediate query. sql: dcq.sql.Clone(), path: dcq.path, } } // WithContents tells the query-builder to eager-load the nodes that are connected to // the "contents" edge. The optional arguments are used to configure the query builder of the edge. func (dcq *DailyCategoryQuery) WithContents(opts ...func(*DailyCategoryContentQuery)) *DailyCategoryQuery { query := (&DailyCategoryContentClient{config: dcq.config}).Query() for _, opt := range opts { opt(query) } dcq.withContents = query return dcq } // WithDailyItems tells the query-builder to eager-load the nodes that are connected to // the "daily_items" edge. The optional arguments are used to configure the query builder of the edge. func (dcq *DailyCategoryQuery) WithDailyItems(opts ...func(*DailyQuery)) *DailyCategoryQuery { query := (&DailyClient{config: dcq.config}).Query() for _, opt := range opts { opt(query) } dcq.withDailyItems = query return dcq } // 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 { // CreatedAt time.Time `json:"created_at,omitempty"` // Count int `json:"count,omitempty"` // } // // client.DailyCategory.Query(). // GroupBy(dailycategory.FieldCreatedAt). // Aggregate(ent.Count()). // Scan(ctx, &v) func (dcq *DailyCategoryQuery) GroupBy(field string, fields ...string) *DailyCategoryGroupBy { dcq.ctx.Fields = append([]string{field}, fields...) grbuild := &DailyCategoryGroupBy{build: dcq} grbuild.flds = &dcq.ctx.Fields grbuild.label = dailycategory.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 { // CreatedAt time.Time `json:"created_at,omitempty"` // } // // client.DailyCategory.Query(). // Select(dailycategory.FieldCreatedAt). // Scan(ctx, &v) func (dcq *DailyCategoryQuery) Select(fields ...string) *DailyCategorySelect { dcq.ctx.Fields = append(dcq.ctx.Fields, fields...) sbuild := &DailyCategorySelect{DailyCategoryQuery: dcq} sbuild.label = dailycategory.Label sbuild.flds, sbuild.scan = &dcq.ctx.Fields, sbuild.Scan return sbuild } // Aggregate returns a DailyCategorySelect configured with the given aggregations. func (dcq *DailyCategoryQuery) Aggregate(fns ...AggregateFunc) *DailyCategorySelect { return dcq.Select().Aggregate(fns...) } func (dcq *DailyCategoryQuery) prepareQuery(ctx context.Context) error { for _, inter := range dcq.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, dcq); err != nil { return err } } } for _, f := range dcq.ctx.Fields { if !dailycategory.ValidColumn(f) { return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } } if dcq.path != nil { prev, err := dcq.path(ctx) if err != nil { return err } dcq.sql = prev } return nil } func (dcq *DailyCategoryQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*DailyCategory, error) { var ( nodes = []*DailyCategory{} _spec = dcq.querySpec() loadedTypes = [2]bool{ dcq.withContents != nil, dcq.withDailyItems != nil, } ) _spec.ScanValues = func(columns []string) ([]any, error) { return (*DailyCategory).scanValues(nil, columns) } _spec.Assign = func(columns []string, values []any) error { node := &DailyCategory{config: dcq.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, dcq.driver, _spec); err != nil { return nil, err } if len(nodes) == 0 { return nodes, nil } if query := dcq.withContents; query != nil { if err := dcq.loadContents(ctx, query, nodes, func(n *DailyCategory) { n.Edges.Contents = []*DailyCategoryContent{} }, func(n *DailyCategory, e *DailyCategoryContent) { n.Edges.Contents = append(n.Edges.Contents, e) }); err != nil { return nil, err } } if query := dcq.withDailyItems; query != nil { if err := dcq.loadDailyItems(ctx, query, nodes, func(n *DailyCategory) { n.Edges.DailyItems = []*Daily{} }, func(n *DailyCategory, e *Daily) { n.Edges.DailyItems = append(n.Edges.DailyItems, e) }); err != nil { return nil, err } } return nodes, nil } func (dcq *DailyCategoryQuery) loadContents(ctx context.Context, query *DailyCategoryContentQuery, nodes []*DailyCategory, init func(*DailyCategory), assign func(*DailyCategory, *DailyCategoryContent)) error { fks := make([]driver.Value, 0, len(nodes)) nodeids := make(map[int]*DailyCategory) for i := range nodes { fks = append(fks, nodes[i].ID) nodeids[nodes[i].ID] = nodes[i] if init != nil { init(nodes[i]) } } query.withFKs = true query.Where(predicate.DailyCategoryContent(func(s *sql.Selector) { s.Where(sql.InValues(s.C(dailycategory.ContentsColumn), fks...)) })) neighbors, err := query.All(ctx) if err != nil { return err } for _, n := range neighbors { fk := n.daily_category_contents if fk == nil { return fmt.Errorf(`foreign-key "daily_category_contents" is nil for node %v`, n.ID) } node, ok := nodeids[*fk] if !ok { return fmt.Errorf(`unexpected referenced foreign-key "daily_category_contents" returned %v for node %v`, *fk, n.ID) } assign(node, n) } return nil } func (dcq *DailyCategoryQuery) loadDailyItems(ctx context.Context, query *DailyQuery, nodes []*DailyCategory, init func(*DailyCategory), assign func(*DailyCategory, *Daily)) error { fks := make([]driver.Value, 0, len(nodes)) nodeids := make(map[int]*DailyCategory) for i := range nodes { fks = append(fks, nodes[i].ID) nodeids[nodes[i].ID] = nodes[i] if init != nil { init(nodes[i]) } } query.withFKs = true query.Where(predicate.Daily(func(s *sql.Selector) { s.Where(sql.InValues(s.C(dailycategory.DailyItemsColumn), fks...)) })) neighbors, err := query.All(ctx) if err != nil { return err } for _, n := range neighbors { fk := n.daily_category_daily_items if fk == nil { return fmt.Errorf(`foreign-key "daily_category_daily_items" is nil for node %v`, n.ID) } node, ok := nodeids[*fk] if !ok { return fmt.Errorf(`unexpected referenced foreign-key "daily_category_daily_items" returned %v for node %v`, *fk, n.ID) } assign(node, n) } return nil } func (dcq *DailyCategoryQuery) sqlCount(ctx context.Context) (int, error) { _spec := dcq.querySpec() _spec.Node.Columns = dcq.ctx.Fields if len(dcq.ctx.Fields) > 0 { _spec.Unique = dcq.ctx.Unique != nil && *dcq.ctx.Unique } return sqlgraph.CountNodes(ctx, dcq.driver, _spec) } func (dcq *DailyCategoryQuery) querySpec() *sqlgraph.QuerySpec { _spec := sqlgraph.NewQuerySpec(dailycategory.Table, dailycategory.Columns, sqlgraph.NewFieldSpec(dailycategory.FieldID, field.TypeInt)) _spec.From = dcq.sql if unique := dcq.ctx.Unique; unique != nil { _spec.Unique = *unique } else if dcq.path != nil { _spec.Unique = true } if fields := dcq.ctx.Fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) _spec.Node.Columns = append(_spec.Node.Columns, dailycategory.FieldID) for i := range fields { if fields[i] != dailycategory.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) } } } if ps := dcq.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if limit := dcq.ctx.Limit; limit != nil { _spec.Limit = *limit } if offset := dcq.ctx.Offset; offset != nil { _spec.Offset = *offset } if ps := dcq.order; len(ps) > 0 { _spec.Order = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } return _spec } func (dcq *DailyCategoryQuery) sqlQuery(ctx context.Context) *sql.Selector { builder := sql.Dialect(dcq.driver.Dialect()) t1 := builder.Table(dailycategory.Table) columns := dcq.ctx.Fields if len(columns) == 0 { columns = dailycategory.Columns } selector := builder.Select(t1.Columns(columns...)...).From(t1) if dcq.sql != nil { selector = dcq.sql selector.Select(selector.Columns(columns...)...) } if dcq.ctx.Unique != nil && *dcq.ctx.Unique { selector.Distinct() } for _, p := range dcq.predicates { p(selector) } for _, p := range dcq.order { p(selector) } if offset := dcq.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 := dcq.ctx.Limit; limit != nil { selector.Limit(*limit) } return selector } // DailyCategoryGroupBy is the group-by builder for DailyCategory entities. type DailyCategoryGroupBy struct { selector build *DailyCategoryQuery } // Aggregate adds the given aggregation functions to the group-by query. func (dcgb *DailyCategoryGroupBy) Aggregate(fns ...AggregateFunc) *DailyCategoryGroupBy { dcgb.fns = append(dcgb.fns, fns...) return dcgb } // Scan applies the selector query and scans the result into the given value. func (dcgb *DailyCategoryGroupBy) Scan(ctx context.Context, v any) error { ctx = setContextOp(ctx, dcgb.build.ctx, ent.OpQueryGroupBy) if err := dcgb.build.prepareQuery(ctx); err != nil { return err } return scanWithInterceptors[*DailyCategoryQuery, *DailyCategoryGroupBy](ctx, dcgb.build, dcgb, dcgb.build.inters, v) } func (dcgb *DailyCategoryGroupBy) sqlScan(ctx context.Context, root *DailyCategoryQuery, v any) error { selector := root.sqlQuery(ctx).Select() aggregation := make([]string, 0, len(dcgb.fns)) for _, fn := range dcgb.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { columns := make([]string, 0, len(*dcgb.flds)+len(dcgb.fns)) for _, f := range *dcgb.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } selector.GroupBy(selector.Columns(*dcgb.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() if err := dcgb.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() return sql.ScanSlice(rows, v) } // DailyCategorySelect is the builder for selecting fields of DailyCategory entities. type DailyCategorySelect struct { *DailyCategoryQuery selector } // Aggregate adds the given aggregation functions to the selector query. func (dcs *DailyCategorySelect) Aggregate(fns ...AggregateFunc) *DailyCategorySelect { dcs.fns = append(dcs.fns, fns...) return dcs } // Scan applies the selector query and scans the result into the given value. func (dcs *DailyCategorySelect) Scan(ctx context.Context, v any) error { ctx = setContextOp(ctx, dcs.ctx, ent.OpQuerySelect) if err := dcs.prepareQuery(ctx); err != nil { return err } return scanWithInterceptors[*DailyCategoryQuery, *DailyCategorySelect](ctx, dcs.DailyCategoryQuery, dcs, dcs.inters, v) } func (dcs *DailyCategorySelect) sqlScan(ctx context.Context, root *DailyCategoryQuery, v any) error { selector := root.sqlQuery(ctx) aggregation := make([]string, 0, len(dcs.fns)) for _, fn := range dcs.fns { aggregation = append(aggregation, fn(selector)) } switch n := len(*dcs.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 := dcs.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() return sql.ScanSlice(rows, v) }