// Code generated by ent, DO NOT EDIT. package ent import ( "context" "database/sql/driver" "fmt" "math" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "git.dotya.ml/mirre-mt/pcmt/ent/localbreach" "git.dotya.ml/mirre-mt/pcmt/ent/predicate" "git.dotya.ml/mirre-mt/pcmt/ent/trackedbreaches" "github.com/google/uuid" ) // LocalBreachQuery is the builder for querying LocalBreach entities. type LocalBreachQuery struct { config ctx *QueryContext order []localbreach.OrderOption inters []Interceptor predicates []predicate.LocalBreach withTrackedBreaches *TrackedBreachesQuery // intermediate query (i.e. traversal path). sql *sql.Selector path func(context.Context) (*sql.Selector, error) } // Where adds a new predicate for the LocalBreachQuery builder. func (lbq *LocalBreachQuery) Where(ps ...predicate.LocalBreach) *LocalBreachQuery { lbq.predicates = append(lbq.predicates, ps...) return lbq } // Limit the number of records to be returned by this query. func (lbq *LocalBreachQuery) Limit(limit int) *LocalBreachQuery { lbq.ctx.Limit = &limit return lbq } // Offset to start from. func (lbq *LocalBreachQuery) Offset(offset int) *LocalBreachQuery { lbq.ctx.Offset = &offset return lbq } // 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 (lbq *LocalBreachQuery) Unique(unique bool) *LocalBreachQuery { lbq.ctx.Unique = &unique return lbq } // Order specifies how the records should be ordered. func (lbq *LocalBreachQuery) Order(o ...localbreach.OrderOption) *LocalBreachQuery { lbq.order = append(lbq.order, o...) return lbq } // QueryTrackedBreaches chains the current query on the "tracked_breaches" edge. func (lbq *LocalBreachQuery) QueryTrackedBreaches() *TrackedBreachesQuery { query := (&TrackedBreachesClient{config: lbq.config}).Query() query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { if err := lbq.prepareQuery(ctx); err != nil { return nil, err } selector := lbq.sqlQuery(ctx) if err := selector.Err(); err != nil { return nil, err } step := sqlgraph.NewStep( sqlgraph.From(localbreach.Table, localbreach.FieldID, selector), sqlgraph.To(trackedbreaches.Table, trackedbreaches.FieldID), sqlgraph.Edge(sqlgraph.M2M, true, localbreach.TrackedBreachesTable, localbreach.TrackedBreachesPrimaryKey...), ) fromU = sqlgraph.SetNeighbors(lbq.driver.Dialect(), step) return fromU, nil } return query } // First returns the first LocalBreach entity from the query. // Returns a *NotFoundError when no LocalBreach was found. func (lbq *LocalBreachQuery) First(ctx context.Context) (*LocalBreach, error) { nodes, err := lbq.Limit(1).All(setContextOp(ctx, lbq.ctx, "First")) if err != nil { return nil, err } if len(nodes) == 0 { return nil, &NotFoundError{localbreach.Label} } return nodes[0], nil } // FirstX is like First, but panics if an error occurs. func (lbq *LocalBreachQuery) FirstX(ctx context.Context) *LocalBreach { node, err := lbq.First(ctx) if err != nil && !IsNotFound(err) { panic(err) } return node } // FirstID returns the first LocalBreach ID from the query. // Returns a *NotFoundError when no LocalBreach ID was found. func (lbq *LocalBreachQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) { var ids []uuid.UUID if ids, err = lbq.Limit(1).IDs(setContextOp(ctx, lbq.ctx, "FirstID")); err != nil { return } if len(ids) == 0 { err = &NotFoundError{localbreach.Label} return } return ids[0], nil } // FirstIDX is like FirstID, but panics if an error occurs. func (lbq *LocalBreachQuery) FirstIDX(ctx context.Context) uuid.UUID { id, err := lbq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) } return id } // Only returns a single LocalBreach entity found by the query, ensuring it only returns one. // Returns a *NotSingularError when more than one LocalBreach entity is found. // Returns a *NotFoundError when no LocalBreach entities are found. func (lbq *LocalBreachQuery) Only(ctx context.Context) (*LocalBreach, error) { nodes, err := lbq.Limit(2).All(setContextOp(ctx, lbq.ctx, "Only")) if err != nil { return nil, err } switch len(nodes) { case 1: return nodes[0], nil case 0: return nil, &NotFoundError{localbreach.Label} default: return nil, &NotSingularError{localbreach.Label} } } // OnlyX is like Only, but panics if an error occurs. func (lbq *LocalBreachQuery) OnlyX(ctx context.Context) *LocalBreach { node, err := lbq.Only(ctx) if err != nil { panic(err) } return node } // OnlyID is like Only, but returns the only LocalBreach ID in the query. // Returns a *NotSingularError when more than one LocalBreach ID is found. // Returns a *NotFoundError when no entities are found. func (lbq *LocalBreachQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) { var ids []uuid.UUID if ids, err = lbq.Limit(2).IDs(setContextOp(ctx, lbq.ctx, "OnlyID")); err != nil { return } switch len(ids) { case 1: id = ids[0] case 0: err = &NotFoundError{localbreach.Label} default: err = &NotSingularError{localbreach.Label} } return } // OnlyIDX is like OnlyID, but panics if an error occurs. func (lbq *LocalBreachQuery) OnlyIDX(ctx context.Context) uuid.UUID { id, err := lbq.OnlyID(ctx) if err != nil { panic(err) } return id } // All executes the query and returns a list of LocalBreaches. func (lbq *LocalBreachQuery) All(ctx context.Context) ([]*LocalBreach, error) { ctx = setContextOp(ctx, lbq.ctx, "All") if err := lbq.prepareQuery(ctx); err != nil { return nil, err } qr := querierAll[[]*LocalBreach, *LocalBreachQuery]() return withInterceptors[[]*LocalBreach](ctx, lbq, qr, lbq.inters) } // AllX is like All, but panics if an error occurs. func (lbq *LocalBreachQuery) AllX(ctx context.Context) []*LocalBreach { nodes, err := lbq.All(ctx) if err != nil { panic(err) } return nodes } // IDs executes the query and returns a list of LocalBreach IDs. func (lbq *LocalBreachQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) { if lbq.ctx.Unique == nil && lbq.path != nil { lbq.Unique(true) } ctx = setContextOp(ctx, lbq.ctx, "IDs") if err = lbq.Select(localbreach.FieldID).Scan(ctx, &ids); err != nil { return nil, err } return ids, nil } // IDsX is like IDs, but panics if an error occurs. func (lbq *LocalBreachQuery) IDsX(ctx context.Context) []uuid.UUID { ids, err := lbq.IDs(ctx) if err != nil { panic(err) } return ids } // Count returns the count of the given query. func (lbq *LocalBreachQuery) Count(ctx context.Context) (int, error) { ctx = setContextOp(ctx, lbq.ctx, "Count") if err := lbq.prepareQuery(ctx); err != nil { return 0, err } return withInterceptors[int](ctx, lbq, querierCount[*LocalBreachQuery](), lbq.inters) } // CountX is like Count, but panics if an error occurs. func (lbq *LocalBreachQuery) CountX(ctx context.Context) int { count, err := lbq.Count(ctx) if err != nil { panic(err) } return count } // Exist returns true if the query has elements in the graph. func (lbq *LocalBreachQuery) Exist(ctx context.Context) (bool, error) { ctx = setContextOp(ctx, lbq.ctx, "Exist") switch _, err := lbq.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 (lbq *LocalBreachQuery) ExistX(ctx context.Context) bool { exist, err := lbq.Exist(ctx) if err != nil { panic(err) } return exist } // Clone returns a duplicate of the LocalBreachQuery builder, including all associated steps. It can be // used to prepare common query builders and use them differently after the clone is made. func (lbq *LocalBreachQuery) Clone() *LocalBreachQuery { if lbq == nil { return nil } return &LocalBreachQuery{ config: lbq.config, ctx: lbq.ctx.Clone(), order: append([]localbreach.OrderOption{}, lbq.order...), inters: append([]Interceptor{}, lbq.inters...), predicates: append([]predicate.LocalBreach{}, lbq.predicates...), withTrackedBreaches: lbq.withTrackedBreaches.Clone(), // clone intermediate query. sql: lbq.sql.Clone(), path: lbq.path, } } // WithTrackedBreaches tells the query-builder to eager-load the nodes that are connected to // the "tracked_breaches" edge. The optional arguments are used to configure the query builder of the edge. func (lbq *LocalBreachQuery) WithTrackedBreaches(opts ...func(*TrackedBreachesQuery)) *LocalBreachQuery { query := (&TrackedBreachesClient{config: lbq.config}).Query() for _, opt := range opts { opt(query) } lbq.withTrackedBreaches = query return lbq } // 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 { // Name string `json:"name,omitempty"` // Count int `json:"count,omitempty"` // } // // client.LocalBreach.Query(). // GroupBy(localbreach.FieldName). // Aggregate(ent.Count()). // Scan(ctx, &v) func (lbq *LocalBreachQuery) GroupBy(field string, fields ...string) *LocalBreachGroupBy { lbq.ctx.Fields = append([]string{field}, fields...) grbuild := &LocalBreachGroupBy{build: lbq} grbuild.flds = &lbq.ctx.Fields grbuild.label = localbreach.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 { // Name string `json:"name,omitempty"` // } // // client.LocalBreach.Query(). // Select(localbreach.FieldName). // Scan(ctx, &v) func (lbq *LocalBreachQuery) Select(fields ...string) *LocalBreachSelect { lbq.ctx.Fields = append(lbq.ctx.Fields, fields...) sbuild := &LocalBreachSelect{LocalBreachQuery: lbq} sbuild.label = localbreach.Label sbuild.flds, sbuild.scan = &lbq.ctx.Fields, sbuild.Scan return sbuild } // Aggregate returns a LocalBreachSelect configured with the given aggregations. func (lbq *LocalBreachQuery) Aggregate(fns ...AggregateFunc) *LocalBreachSelect { return lbq.Select().Aggregate(fns...) } func (lbq *LocalBreachQuery) prepareQuery(ctx context.Context) error { for _, inter := range lbq.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, lbq); err != nil { return err } } } for _, f := range lbq.ctx.Fields { if !localbreach.ValidColumn(f) { return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} } } if lbq.path != nil { prev, err := lbq.path(ctx) if err != nil { return err } lbq.sql = prev } return nil } func (lbq *LocalBreachQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*LocalBreach, error) { var ( nodes = []*LocalBreach{} _spec = lbq.querySpec() loadedTypes = [1]bool{ lbq.withTrackedBreaches != nil, } ) _spec.ScanValues = func(columns []string) ([]any, error) { return (*LocalBreach).scanValues(nil, columns) } _spec.Assign = func(columns []string, values []any) error { node := &LocalBreach{config: lbq.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, lbq.driver, _spec); err != nil { return nil, err } if len(nodes) == 0 { return nodes, nil } if query := lbq.withTrackedBreaches; query != nil { if err := lbq.loadTrackedBreaches(ctx, query, nodes, func(n *LocalBreach) { n.Edges.TrackedBreaches = []*TrackedBreaches{} }, func(n *LocalBreach, e *TrackedBreaches) { n.Edges.TrackedBreaches = append(n.Edges.TrackedBreaches, e) }); err != nil { return nil, err } } return nodes, nil } func (lbq *LocalBreachQuery) loadTrackedBreaches(ctx context.Context, query *TrackedBreachesQuery, nodes []*LocalBreach, init func(*LocalBreach), assign func(*LocalBreach, *TrackedBreaches)) error { edgeIDs := make([]driver.Value, len(nodes)) byID := make(map[uuid.UUID]*LocalBreach) nids := make(map[uuid.UUID]map[*LocalBreach]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(localbreach.TrackedBreachesTable) s.Join(joinT).On(s.C(trackedbreaches.FieldID), joinT.C(localbreach.TrackedBreachesPrimaryKey[0])) s.Where(sql.InValues(joinT.C(localbreach.TrackedBreachesPrimaryKey[1]), edgeIDs...)) columns := s.SelectedColumns() s.Select(joinT.C(localbreach.TrackedBreachesPrimaryKey[1])) 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(uuid.UUID)}, values...), nil } spec.Assign = func(columns []string, values []any) error { outValue := *values[0].(*uuid.UUID) inValue := *values[1].(*uuid.UUID) if nids[inValue] == nil { nids[inValue] = map[*LocalBreach]struct{}{byID[outValue]: {}} return assign(columns[1:], values[1:]) } nids[inValue][byID[outValue]] = struct{}{} return nil } }) }) neighbors, err := withInterceptors[[]*TrackedBreaches](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 "tracked_breaches" node returned %v`, n.ID) } for kn := range nodes { assign(kn, n) } } return nil } func (lbq *LocalBreachQuery) sqlCount(ctx context.Context) (int, error) { _spec := lbq.querySpec() _spec.Node.Columns = lbq.ctx.Fields if len(lbq.ctx.Fields) > 0 { _spec.Unique = lbq.ctx.Unique != nil && *lbq.ctx.Unique } return sqlgraph.CountNodes(ctx, lbq.driver, _spec) } func (lbq *LocalBreachQuery) querySpec() *sqlgraph.QuerySpec { _spec := sqlgraph.NewQuerySpec(localbreach.Table, localbreach.Columns, sqlgraph.NewFieldSpec(localbreach.FieldID, field.TypeUUID)) _spec.From = lbq.sql if unique := lbq.ctx.Unique; unique != nil { _spec.Unique = *unique } else if lbq.path != nil { _spec.Unique = true } if fields := lbq.ctx.Fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) _spec.Node.Columns = append(_spec.Node.Columns, localbreach.FieldID) for i := range fields { if fields[i] != localbreach.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) } } } if ps := lbq.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if limit := lbq.ctx.Limit; limit != nil { _spec.Limit = *limit } if offset := lbq.ctx.Offset; offset != nil { _spec.Offset = *offset } if ps := lbq.order; len(ps) > 0 { _spec.Order = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } return _spec } func (lbq *LocalBreachQuery) sqlQuery(ctx context.Context) *sql.Selector { builder := sql.Dialect(lbq.driver.Dialect()) t1 := builder.Table(localbreach.Table) columns := lbq.ctx.Fields if len(columns) == 0 { columns = localbreach.Columns } selector := builder.Select(t1.Columns(columns...)...).From(t1) if lbq.sql != nil { selector = lbq.sql selector.Select(selector.Columns(columns...)...) } if lbq.ctx.Unique != nil && *lbq.ctx.Unique { selector.Distinct() } for _, p := range lbq.predicates { p(selector) } for _, p := range lbq.order { p(selector) } if offset := lbq.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 := lbq.ctx.Limit; limit != nil { selector.Limit(*limit) } return selector } // LocalBreachGroupBy is the group-by builder for LocalBreach entities. type LocalBreachGroupBy struct { selector build *LocalBreachQuery } // Aggregate adds the given aggregation functions to the group-by query. func (lbgb *LocalBreachGroupBy) Aggregate(fns ...AggregateFunc) *LocalBreachGroupBy { lbgb.fns = append(lbgb.fns, fns...) return lbgb } // Scan applies the selector query and scans the result into the given value. func (lbgb *LocalBreachGroupBy) Scan(ctx context.Context, v any) error { ctx = setContextOp(ctx, lbgb.build.ctx, "GroupBy") if err := lbgb.build.prepareQuery(ctx); err != nil { return err } return scanWithInterceptors[*LocalBreachQuery, *LocalBreachGroupBy](ctx, lbgb.build, lbgb, lbgb.build.inters, v) } func (lbgb *LocalBreachGroupBy) sqlScan(ctx context.Context, root *LocalBreachQuery, v any) error { selector := root.sqlQuery(ctx).Select() aggregation := make([]string, 0, len(lbgb.fns)) for _, fn := range lbgb.fns { aggregation = append(aggregation, fn(selector)) } if len(selector.SelectedColumns()) == 0 { columns := make([]string, 0, len(*lbgb.flds)+len(lbgb.fns)) for _, f := range *lbgb.flds { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } selector.GroupBy(selector.Columns(*lbgb.flds...)...) if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() if err := lbgb.build.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() return sql.ScanSlice(rows, v) } // LocalBreachSelect is the builder for selecting fields of LocalBreach entities. type LocalBreachSelect struct { *LocalBreachQuery selector } // Aggregate adds the given aggregation functions to the selector query. func (lbs *LocalBreachSelect) Aggregate(fns ...AggregateFunc) *LocalBreachSelect { lbs.fns = append(lbs.fns, fns...) return lbs } // Scan applies the selector query and scans the result into the given value. func (lbs *LocalBreachSelect) Scan(ctx context.Context, v any) error { ctx = setContextOp(ctx, lbs.ctx, "Select") if err := lbs.prepareQuery(ctx); err != nil { return err } return scanWithInterceptors[*LocalBreachQuery, *LocalBreachSelect](ctx, lbs.LocalBreachQuery, lbs, lbs.inters, v) } func (lbs *LocalBreachSelect) sqlScan(ctx context.Context, root *LocalBreachQuery, v any) error { selector := root.sqlQuery(ctx) aggregation := make([]string, 0, len(lbs.fns)) for _, fn := range lbs.fns { aggregation = append(aggregation, fn(selector)) } switch n := len(*lbs.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 := lbs.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() return sql.ScanSlice(rows, v) }