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