// Code generated by ent, DO NOT EDIT. package ent import ( "context" "fmt" "math" "tss-rocks-be/ent/daily" "tss-rocks-be/ent/dailycontent" "tss-rocks-be/ent/predicate" "entgo.io/ent" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" ) // DailyContentQuery is the builder for querying DailyContent entities. type DailyContentQuery struct { config ctx *QueryContext order []dailycontent.OrderOption inters []Interceptor predicates []predicate.DailyContent withDaily *DailyQuery withFKs bool // intermediate query (i.e. traversal path). sql *sql.Selector path func(context.Context) (*sql.Selector, error) } // Where adds a new predicate for the DailyContentQuery builder. func (dcq *DailyContentQuery) Where(ps ...predicate.DailyContent) *DailyContentQuery { dcq.predicates = append(dcq.predicates, ps...) return dcq } // Limit the number of records to be returned by this query. func (dcq *DailyContentQuery) Limit(limit int) *DailyContentQuery { dcq.ctx.Limit = &limit return dcq } // Offset to start from. func (dcq *DailyContentQuery) Offset(offset int) *DailyContentQuery { 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 *DailyContentQuery) Unique(unique bool) *DailyContentQuery { dcq.ctx.Unique = &unique return dcq } // Order specifies how the records should be ordered. func (dcq *DailyContentQuery) Order(o ...dailycontent.OrderOption) *DailyContentQuery { dcq.order = append(dcq.order, o...) return dcq } // QueryDaily chains the current query on the "daily" edge. func (dcq *DailyContentQuery) QueryDaily() *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(dailycontent.Table, dailycontent.FieldID, selector), sqlgraph.To(daily.Table, daily.FieldID), sqlgraph.Edge(sqlgraph.M2O, true, dailycontent.DailyTable, dailycontent.DailyColumn), ) fromU = sqlgraph.SetNeighbors(dcq.driver.Dialect(), step) return fromU, nil } return query } // First returns the first DailyContent entity from the query. // Returns a *NotFoundError when no DailyContent was found. func (dcq *DailyContentQuery) First(ctx context.Context) (*DailyContent, 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{dailycontent.Label} } return nodes[0], nil } // FirstX is like First, but panics if an error occurs. func (dcq *DailyContentQuery) FirstX(ctx context.Context) *DailyContent { node, err := dcq.First(ctx) if err != nil && !IsNotFound(err) { panic(err) } return node } // FirstID returns the first DailyContent ID from the query. // Returns a *NotFoundError when no DailyContent ID was found. func (dcq *DailyContentQuery) 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{dailycontent.Label} return } return ids[0], nil } // FirstIDX is like FirstID, but panics if an error occurs. func (dcq *DailyContentQuery) FirstIDX(ctx context.Context) int { id, err := dcq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) } return id } // Only returns a single DailyContent entity found by the query, ensuring it only returns one. // Returns a *NotSingularError when more than one DailyContent entity is found. // Returns a *NotFoundError when no DailyContent entities are found. func (dcq *DailyContentQuery) Only(ctx context.Context) (*DailyContent, 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{dailycontent.Label} default: return nil, &NotSingularError{dailycontent.Label} } } // OnlyX is like Only, but panics if an error occurs. func (dcq *DailyContentQuery) OnlyX(ctx context.Context) *DailyContent { node, err := dcq.Only(ctx) if err != nil { panic(err) } return node } // OnlyID is like Only, but returns the only DailyContent ID in the query. // Returns a *NotSingularError when more than one DailyContent ID is found. // Returns a *NotFoundError when no entities are found. func (dcq *DailyContentQuery) 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{dailycontent.Label} default: err = &NotSingularError{dailycontent.Label} } return } // OnlyIDX is like OnlyID, but panics if an error occurs. func (dcq *DailyContentQuery) 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 DailyContents. func (dcq *DailyContentQuery) All(ctx context.Context) ([]*DailyContent, error) { ctx = setContextOp(ctx, dcq.ctx, ent.OpQueryAll) if err := dcq.prepareQuery(ctx); err != nil { return nil, err } qr := querierAll[[]*DailyContent, *DailyContentQuery]() return withInterceptors[[]*DailyContent](ctx, dcq, qr, dcq.inters) } // AllX is like All, but panics if an error occurs. func (dcq *DailyContentQuery) AllX(ctx context.Context) []*DailyContent { nodes, err := dcq.All(ctx) if err != nil { panic(err) } return nodes } // IDs executes the query and returns a list of DailyContent IDs. func (dcq *DailyContentQuery) 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(dailycontent.FieldID).Scan(ctx, &ids); err != nil { return nil, err } return ids, nil } // IDsX is like IDs, but panics if an error occurs. func (dcq *DailyContentQuery) 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 *DailyContentQuery) 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[*DailyContentQuery](), dcq.inters) } // CountX is like Count, but panics if an error occurs. func (dcq *DailyContentQuery) 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 *DailyContentQuery) 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 *DailyContentQuery) ExistX(ctx context.Context) bool { exist, err := dcq.Exist(ctx) if err != nil { panic(err) } return exist } // Clone returns a duplicate of the DailyContentQuery 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 *DailyContentQuery) Clone() *DailyContentQuery { if dcq == nil { return nil } return &DailyContentQuery{ config: dcq.config, ctx: dcq.ctx.Clone(), order: append([]dailycontent.OrderOption{}, dcq.order...), inters: append([]Interceptor{}, dcq.inters...), predicates: append([]predicate.DailyContent{}, dcq.predicates...), withDaily: dcq.withDaily.Clone(), // clone intermediate query. sql: dcq.sql.Clone(), path: dcq.path, } } // WithDaily tells the query-builder to eager-load the nodes that are connected to // the "daily" edge. The optional arguments are used to configure the query builder of the edge. func (dcq *DailyContentQuery) WithDaily(opts ...func(*DailyQuery)) *DailyContentQuery { query := (&DailyClient{config: dcq.config}).Query() for _, opt := range opts { opt(query) } dcq.withDaily = 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 { // LanguageCode dailycontent.LanguageCode `json:"language_code,omitempty"` // Count int `json:"count,omitempty"` // } // // client.DailyContent.Query(). // GroupBy(dailycontent.FieldLanguageCode). // Aggregate(ent.Count()). // Scan(ctx, &v) func (dcq *DailyContentQuery) GroupBy(field string, fields ...string) *DailyContentGroupBy { dcq.ctx.Fields = append([]string{field}, fields...) grbuild := &DailyContentGroupBy{build: dcq} grbuild.flds = &dcq.ctx.Fields grbuild.label = dailycontent.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 { // LanguageCode dailycontent.LanguageCode `json:"language_code,omitempty"` // } // // client.DailyContent.Query(). // Select(dailycontent.FieldLanguageCode). // Scan(ctx, &v) func (dcq *DailyContentQuery) Select(fields ...string) *DailyContentSelect { dcq.ctx.Fields = append(dcq.ctx.Fields, fields...) sbuild := &DailyContentSelect{DailyContentQuery: dcq} sbuild.label = dailycontent.Label sbuild.flds, sbuild.scan = &dcq.ctx.Fields, sbuild.Scan return sbuild } // Aggregate returns a DailyContentSelect configured with the given aggregations. func (dcq *DailyContentQuery) Aggregate(fns ...AggregateFunc) *DailyContentSelect { return dcq.Select().Aggregate(fns...) } func (dcq *DailyContentQuery) 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 !dailycontent.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 *DailyContentQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*DailyContent, error) { var ( nodes = []*DailyContent{} withFKs = dcq.withFKs _spec = dcq.querySpec() loadedTypes = [1]bool{ dcq.withDaily != nil, } ) if dcq.withDaily != nil { withFKs = true } if withFKs { _spec.Node.Columns = append(_spec.Node.Columns, dailycontent.ForeignKeys...) } _spec.ScanValues = func(columns []string) ([]any, error) { return (*DailyContent).scanValues(nil, columns) } _spec.Assign = func(columns []string, values []any) error { node := &DailyContent{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.withDaily; query != nil { if err := dcq.loadDaily(ctx, query, nodes, nil, func(n *DailyContent, e *Daily) { n.Edges.Daily = e }); err != nil { return nil, err } } return nodes, nil } func (dcq *DailyContentQuery) loadDaily(ctx context.Context, query *DailyQuery, nodes []*DailyContent, init func(*DailyContent), assign func(*DailyContent, *Daily)) error { ids := make([]string, 0, len(nodes)) nodeids := make(map[string][]*DailyContent) for i := range nodes { if nodes[i].daily_contents == nil { continue } fk := *nodes[i].daily_contents if _, ok := nodeids[fk]; !ok { ids = append(ids, fk) } nodeids[fk] = append(nodeids[fk], nodes[i]) } if len(ids) == 0 { return nil } query.Where(daily.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 "daily_contents" returned %v`, n.ID) } for i := range nodes { assign(nodes[i], n) } } return nil } func (dcq *DailyContentQuery) 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 *DailyContentQuery) querySpec() *sqlgraph.QuerySpec { _spec := sqlgraph.NewQuerySpec(dailycontent.Table, dailycontent.Columns, sqlgraph.NewFieldSpec(dailycontent.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, dailycontent.FieldID) for i := range fields { if fields[i] != dailycontent.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 *DailyContentQuery) sqlQuery(ctx context.Context) *sql.Selector { builder := sql.Dialect(dcq.driver.Dialect()) t1 := builder.Table(dailycontent.Table) columns := dcq.ctx.Fields if len(columns) == 0 { columns = dailycontent.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 } // DailyContentGroupBy is the group-by builder for DailyContent entities. type DailyContentGroupBy struct { selector build *DailyContentQuery } // Aggregate adds the given aggregation functions to the group-by query. func (dcgb *DailyContentGroupBy) Aggregate(fns ...AggregateFunc) *DailyContentGroupBy { dcgb.fns = append(dcgb.fns, fns...) return dcgb } // Scan applies the selector query and scans the result into the given value. func (dcgb *DailyContentGroupBy) 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[*DailyContentQuery, *DailyContentGroupBy](ctx, dcgb.build, dcgb, dcgb.build.inters, v) } func (dcgb *DailyContentGroupBy) sqlScan(ctx context.Context, root *DailyContentQuery, 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) } // DailyContentSelect is the builder for selecting fields of DailyContent entities. type DailyContentSelect struct { *DailyContentQuery selector } // Aggregate adds the given aggregation functions to the selector query. func (dcs *DailyContentSelect) Aggregate(fns ...AggregateFunc) *DailyContentSelect { dcs.fns = append(dcs.fns, fns...) return dcs } // Scan applies the selector query and scans the result into the given value. func (dcs *DailyContentSelect) 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[*DailyContentQuery, *DailyContentSelect](ctx, dcs.DailyContentQuery, dcs, dcs.inters, v) } func (dcs *DailyContentSelect) sqlScan(ctx context.Context, root *DailyContentQuery, 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) }