// Code generated by ent, DO NOT EDIT. package ent import ( "context" "fmt" "math" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/cloudreve/Cloudreve/v4/ent/giftcode" "github.com/cloudreve/Cloudreve/v4/ent/predicate" "github.com/cloudreve/Cloudreve/v4/ent/user" ) // GiftCodeQuery is the builder for querying GiftCode entities. type GiftCodeQuery struct { config ctx *QueryContext order []giftcode.OrderOption inters []Interceptor predicates []predicate.GiftCode withRedeemer *UserQuery // intermediate query (i.e. traversal path). sql *sql.Selector path func(context.Context) (*sql.Selector, error) } // Where adds a new predicate for the GiftCodeQuery builder. func (gcq *GiftCodeQuery) Where(ps ...predicate.GiftCode) *GiftCodeQuery { gcq.predicates = append(gcq.predicates, ps...) return gcq } // Limit the number of records to be returned by this query. func (gcq *GiftCodeQuery) Limit(limit int) *GiftCodeQuery { gcq.ctx.Limit = &limit return gcq } // Offset to start from. func (gcq *GiftCodeQuery) Offset(offset int) *GiftCodeQuery { gcq.ctx.Offset = &offset return gcq } // 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 (gcq *GiftCodeQuery) Unique(unique bool) *GiftCodeQuery { gcq.ctx.Unique = &unique return gcq } // Order specifies how the records should be ordered. func (gcq *GiftCodeQuery) Order(o ...giftcode.OrderOption) *GiftCodeQuery { gcq.order = append(gcq.order, o...) return gcq } // QueryRedeemer chains the current query on the "redeemer" edge. func (gcq *GiftCodeQuery) QueryRedeemer() *UserQuery { query := (&UserClient{config: gcq.config}).Query() query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { if err := gcq.prepareQuery(ctx); err != nil { return nil, err } selector := gcq.sqlQuery(ctx) if err := selector.Err(); err != nil { return nil, err } step := sqlgraph.NewStep( sqlgraph.From(giftcode.Table, giftcode.FieldID, selector), sqlgraph.To(user.Table, user.FieldID), sqlgraph.Edge(sqlgraph.M2O, true, giftcode.RedeemerTable, giftcode.RedeemerColumn), ) fromU = sqlgraph.SetNeighbors(gcq.driver.Dialect(), step) return fromU, nil } return query } // First returns the first GiftCode entity from the query. // Returns a *NotFoundError when no GiftCode was found. func (gcq *GiftCodeQuery) First(ctx context.Context) (*GiftCode, error) { nodes, err := gcq.Limit(1).All(setContextOp(ctx, gcq.ctx, "First")) if err != nil { return nil, err } if len(nodes) == 0 { return nil, &NotFoundError{giftcode.Label} } return nodes[0], nil } // FirstX is like First, but panics if an error occurs. func (gcq *GiftCodeQuery) FirstX(ctx context.Context) *GiftCode { node, err := gcq.First(ctx) if err != nil && !IsNotFound(err) { panic(err) } return node } // FirstID returns the first GiftCode ID from the query. // Returns a *NotFoundError when no GiftCode ID was found. func (gcq *GiftCodeQuery) FirstID(ctx context.Context) (id int, err error) { var ids []int if ids, err = gcq.Limit(1).IDs(setContextOp(ctx, gcq.ctx, "FirstID")); err != nil { return } if len(ids) == 0 { err = &NotFoundError{giftcode.Label} return } return ids[0], nil } // FirstIDX is like FirstID, but panics if an error occurs. func (gcq *GiftCodeQuery) FirstIDX(ctx context.Context) int { id, err := gcq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) } return id } // Only returns a single GiftCode entity found by the query, ensuring it only returns one. // Returns a *NotSingularError when more than one GiftCode entity is found. // Returns a *NotFoundError when no GiftCode entities are found. func (gcq *GiftCodeQuery) Only(ctx context.Context) (*GiftCode, error) { nodes, err := gcq.Limit(2).All(setContextOp(ctx, gcq.ctx, "Only")) if err != nil { return nil, err } switch len(nodes) { case 1: return nodes[0], nil case 0: return nil, &NotFoundError{giftcode.Label} default: return nil, &NotSingularError{giftcode.Label} } } // OnlyX is like Only, but panics if an error occurs. func (gcq *GiftCodeQuery) OnlyX(ctx context.Context) *GiftCode { node, err := gcq.Only(ctx) if err != nil { panic(err) } return node } // OnlyID is like Only, but returns the only GiftCode ID in the query. // Returns a *NotSingularError when more than one GiftCode ID is found. // Returns a *NotFoundError when no entities are found. func (gcq *GiftCodeQuery) OnlyID(ctx context.Context) (id int, err error) { var ids []int if ids, err = gcq.Limit(2).IDs(setContextOp(ctx, gcq.ctx, "OnlyID")); err != nil { return } switch len(ids) { case 1: id = ids[0] case 0: err = &NotFoundError{giftcode.Label} default: err = &NotSingularError{giftcode.Label} } return } // OnlyIDX is like OnlyID, but panics if an error occurs. func (gcq *GiftCodeQuery) OnlyIDX(ctx context.Context) int { id, err := gcq.OnlyID(ctx) if err != nil { panic(err) } return id } // All executes the query and returns a list of GiftCodes. func (gcq *GiftCodeQuery) All(ctx context.Context) ([]*GiftCode, error) { ctx = setContextOp(ctx, gcq.ctx, "All") if err := gcq.prepareQuery(ctx); err != nil { return nil, err } qr := querierAll[[]*GiftCode, *GiftCodeQuery]() return withInterceptors[[]*GiftCode](ctx, gcq, qr, gcq.inters) } // AllX is like All, but panics if an error occurs. func (gcq *GiftCodeQuery) AllX(ctx context.Context) []*GiftCode { nodes, err := gcq.All(ctx) if err != nil { panic(err) } return nodes } // IDs executes the query and returns a list of GiftCode IDs. func (gcq *GiftCodeQuery) IDs(ctx context.Context) (ids []int, err error) { if gcq.ctx.Unique == nil && gcq.path != nil { gcq.Unique(true) } ctx = setContextOp(ctx, gcq.ctx, "IDs") if err = gcq.Select(giftcode.FieldID).Scan(ctx, &ids); err != nil { return nil, err } return ids, nil } // IDsX is like IDs, but panics if an error occurs. func (gcq *GiftCodeQuery) IDsX(ctx context.Context) []int { ids, err := gcq.IDs(ctx) if err != nil { panic(err) } return ids } // Count returns the count of the given query. func (gcq *GiftCodeQuery) Count(ctx context.Context) (int, error) { ctx = setContextOp(ctx, gcq.ctx, "Count") if err := gcq.prepareQuery(ctx); err != nil { return 0, err } return withInterceptors[int](ctx, gcq, querierCount[*GiftCodeQuery](), gcq.inters) } // CountX is like Count, but panics if an error occurs. func (gcq *GiftCodeQuery) CountX(ctx context.Context) int { count, err := gcq.Count(ctx) if err != nil { panic(err) } return count } // Exist returns true if the query has elements in the graph. func (gcq *GiftCodeQuery) Exist(ctx context.Context) (bool, error) { ctx = setContextOp(ctx, gcq.ctx, "Exist") switch _, err := gcq.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 (gcq *GiftCodeQuery) ExistX(ctx context.Context) bool { exist, err := gcq.Exist(ctx) if err != nil { panic(err) } return exist } // Clone returns a duplicate of the GiftCodeQuery builder, including all associated steps. It can be // used to prepare common query builders and use them differently after the clone is made. func (gcq *GiftCodeQuery) Clone() *GiftCodeQuery { if gcq == nil { return nil } return &GiftCodeQuery{ config: gcq.config, ctx: gcq.ctx.Clone(), order: append([]giftcode.OrderOption{}, gcq.order...), inters: append([]Interceptor{}, gcq.inters...), predicates: append([]predicate.GiftCode{}, gcq.predicates...), withRedeemer: gcq.withRedeemer.Clone(), // clone intermediate query. sql: gcq.sql.Clone(), path: gcq.path, } } // WithRedeemer tells the query-builder to eager-load the nodes that are connected to // the "redeemer" edge. The optional arguments are used to configure the query builder of the edge. func (gcq *GiftCodeQuery) WithRedeemer(opts ...func(*UserQuery)) *GiftCodeQuery { query := (&UserClient{config: gcq.config}).Query() for _, opt := range opts { opt(query) } gcq.withRedeemer = query return gcq } // 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.GiftCode.Query(). // GroupBy(giftcode.FieldCreatedAt). // Aggregate(ent.Count()). // Scan(ctx, &v) func (gcq *GiftCodeQuery) GroupBy(field string, fields ...string) *GiftCodeGroupBy { gcq.ctx.Fields = append([]string{field}, fields...) grbuild := &GiftCodeGroupBy{build: gcq} grbuild.flds = &gcq.ctx.Fields grbuild.label = giftcode.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.GiftCode.Query(). // Select(giftcode.FieldCreatedAt). // Scan(ctx, &v) func (gcq *GiftCodeQuery) Select(fields ...string) *GiftCodeSelect { gcq.ctx.Fields = append(gcq.ctx.Fields, fields...) sbuild := &GiftCodeSelect{GiftCodeQuery: gcq} sbuild.label = giftcode.Label sbuild.flds, sbuild.scan = &gcq.ctx.Fields, sbuild.Scan return sbuild } // Aggregate returns a GiftCodeSelect configured with the given aggregations. func (gcq *GiftCodeQuery) Aggregate(fns ...AggregateFunc) *GiftCodeSelect { return gcq.Select().Aggregate(fns...) } func (gcq *GiftCodeQuery) prepareQuery(ctx context.Context) error { for _, inter := range gcq.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, gcq); err != nil { return err } } } for _, f := range gcq.ctx.Fields { if !giftcode.ValidColumn(f) { return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } } if gcq.path != nil { prev, err := gcq.path(ctx) if err != nil { return err } gcq.sql = prev } return nil } func (gcq *GiftCodeQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*GiftCode, error) { var ( nodes = []*GiftCode{} _spec = gcq.querySpec() loadedTypes = [1]bool{ gcq.withRedeemer != nil, } ) _spec.ScanValues = func(columns []string) ([]any, error) { return (*GiftCode).scanValues(nil, columns) } _spec.Assign = func(columns []string, values []any) error { node := &GiftCode{config: gcq.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, gcq.driver, _spec); err != nil { return nil, err } if len(nodes) == 0 { return nodes, nil } if query := gcq.withRedeemer; query != nil { if err := gcq.loadRedeemer(ctx, query, nodes, nil, func(n *GiftCode, e *User) { n.Edges.Redeemer = e }); err != nil { return nil, err } } return nodes, nil } func (gcq *GiftCodeQuery) loadRedeemer(ctx context.Context, query *UserQuery, nodes []*GiftCode, init func(*GiftCode), assign func(*GiftCode, *User)) error { ids := make([]int, 0, len(nodes)) nodeids := make(map[int][]*GiftCode) for i := range nodes { fk := nodes[i].UsedByID if _, ok := nodeids[fk]; !ok { ids = append(ids, fk) } nodeids[fk] = append(nodeids[fk], nodes[i]) } if len(ids) == 0 { return nil } query.Where(user.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 "used_by_id" returned %v`, n.ID) } for i := range nodes { assign(nodes[i], n) } } return nil } func (gcq *GiftCodeQuery) sqlCount(ctx context.Context) (int, error) { _spec := gcq.querySpec() _spec.Node.Columns = gcq.ctx.Fields if len(gcq.ctx.Fields) > 0 { _spec.Unique = gcq.ctx.Unique != nil && *gcq.ctx.Unique } return sqlgraph.CountNodes(ctx, gcq.driver, _spec) } func (gcq *GiftCodeQuery) querySpec() *sqlgraph.QuerySpec { _spec := sqlgraph.NewQuerySpec(giftcode.Table, giftcode.Columns, sqlgraph.NewFieldSpec(giftcode.FieldID, field.TypeInt)) _spec.From = gcq.sql if unique := gcq.ctx.Unique; unique != nil { _spec.Unique = *unique } else if gcq.path != nil { _spec.Unique = true } if fields := gcq.ctx.Fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) _spec.Node.Columns = append(_spec.Node.Columns, giftcode.FieldID) for i := range fields { if fields[i] != giftcode.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) } } if gcq.withRedeemer != nil { _spec.Node.AddColumnOnce(giftcode.FieldUsedByID) } } if ps := gcq.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if limit := gcq.ctx.Limit; limit != nil { _spec.Limit = *limit } if offset := gcq.ctx.Offset; offset != nil { _spec.Offset = *offset } if ps := gcq.order; len(ps) > 0 { _spec.Order = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } return _spec } func (gcq *GiftCodeQuery) sqlQuery(ctx context.Context) *sql.Selector { builder := sql.Dialect(gcq.driver.Dialect()) t1 := builder.Table(giftcode.Table) columns := gcq.ctx.Fields if len(columns) == 0 { columns = giftcode.Columns } selector := builder.Select(t1.Columns(columns...)...).From(t1) if gcq.sql != nil { selector = gcq.sql selector.Select(selector.Columns(columns...)...) } if gcq.ctx.Unique != nil && *gcq.ctx.Unique { selector.Distinct() } for _, p := range gcq.predicates { p(selector) } for _, p := range gcq.order { p(selector) } if offset := gcq.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 := gcq.ctx.Limit; limit != nil { selector.Limit(*limit) } return selector } // GiftCodeGroupBy is the group-by builder for GiftCode entities. type GiftCodeGroupBy struct { selector build *GiftCodeQuery } // Aggregate adds the given aggregation functions to the group-by query. func (gcgb *GiftCodeGroupBy) Aggregate(fns ...AggregateFunc) *GiftCodeGroupBy { gcgb.fns = append(gcgb.fns, fns...) return gcgb } // Scan applies the selector query and scans the result into the given value. func (gcgb *GiftCodeGroupBy) Scan(ctx context.Context, v any) error { ctx = setContextOp(ctx, gcgb.build.ctx, "GroupBy") if err := gcgb.build.prepareQuery(ctx); err != nil { return err } return scanWithInterceptors[*GiftCodeQuery, *GiftCodeGroupBy](ctx, gcgb.build, gcgb, gcgb.build.inters, v) } func (gcgb *GiftCodeGroupBy) sqlScan(ctx context.Context, root *GiftCodeQuery, v any) error { selector := root.sqlQuery(ctx).Select() aggregation := make([]string, 0, len(gcgb.fns)) for _, fn := range gcgb.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { columns := make([]string, 0, len(*gcgb.flds)+len(gcgb.fns)) for _, f := range *gcgb.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } selector.GroupBy(selector.Columns(*gcgb.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() if err := gcgb.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() return sql.ScanSlice(rows, v) } // GiftCodeSelect is the builder for selecting fields of GiftCode entities. type GiftCodeSelect struct { *GiftCodeQuery selector } // Aggregate adds the given aggregation functions to the selector query. func (gcs *GiftCodeSelect) Aggregate(fns ...AggregateFunc) *GiftCodeSelect { gcs.fns = append(gcs.fns, fns...) return gcs } // Scan applies the selector query and scans the result into the given value. func (gcs *GiftCodeSelect) Scan(ctx context.Context, v any) error { ctx = setContextOp(ctx, gcs.ctx, "Select") if err := gcs.prepareQuery(ctx); err != nil { return err } return scanWithInterceptors[*GiftCodeQuery, *GiftCodeSelect](ctx, gcs.GiftCodeQuery, gcs, gcs.inters, v) } func (gcs *GiftCodeSelect) sqlScan(ctx context.Context, root *GiftCodeQuery, v any) error { selector := root.sqlQuery(ctx) aggregation := make([]string, 0, len(gcs.fns)) for _, fn := range gcs.fns { aggregation = append(aggregation, fn(selector)) } switch n := len(*gcs.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 := gcs.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() return sql.ScanSlice(rows, v) }