// Code generated by ent, DO NOT EDIT.

package ent

import (
	"context"
	"database/sql/driver"
	"fmt"
	"math"

	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/predicate"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/project"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/projectversion"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/workflowrun"
	"github.com/google/uuid"
)

// ProjectVersionQuery is the builder for querying ProjectVersion entities.
type ProjectVersionQuery struct {
	config
	ctx         *QueryContext
	order       []projectversion.OrderOption
	inters      []Interceptor
	predicates  []predicate.ProjectVersion
	withProject *ProjectQuery
	withRuns    *WorkflowRunQuery
	modifiers   []func(*sql.Selector)
	// intermediate query (i.e. traversal path).
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}

// Where adds a new predicate for the ProjectVersionQuery builder.
func (pvq *ProjectVersionQuery) Where(ps ...predicate.ProjectVersion) *ProjectVersionQuery {
	pvq.predicates = append(pvq.predicates, ps...)
	return pvq
}

// Limit the number of records to be returned by this query.
func (pvq *ProjectVersionQuery) Limit(limit int) *ProjectVersionQuery {
	pvq.ctx.Limit = &limit
	return pvq
}

// Offset to start from.
func (pvq *ProjectVersionQuery) Offset(offset int) *ProjectVersionQuery {
	pvq.ctx.Offset = &offset
	return pvq
}

// 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 (pvq *ProjectVersionQuery) Unique(unique bool) *ProjectVersionQuery {
	pvq.ctx.Unique = &unique
	return pvq
}

// Order specifies how the records should be ordered.
func (pvq *ProjectVersionQuery) Order(o ...projectversion.OrderOption) *ProjectVersionQuery {
	pvq.order = append(pvq.order, o...)
	return pvq
}

// QueryProject chains the current query on the "project" edge.
func (pvq *ProjectVersionQuery) QueryProject() *ProjectQuery {
	query := (&ProjectClient{config: pvq.config}).Query()
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := pvq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := pvq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(projectversion.Table, projectversion.FieldID, selector),
			sqlgraph.To(project.Table, project.FieldID),
			sqlgraph.Edge(sqlgraph.M2O, true, projectversion.ProjectTable, projectversion.ProjectColumn),
		)
		fromU = sqlgraph.SetNeighbors(pvq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

// QueryRuns chains the current query on the "runs" edge.
func (pvq *ProjectVersionQuery) QueryRuns() *WorkflowRunQuery {
	query := (&WorkflowRunClient{config: pvq.config}).Query()
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := pvq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := pvq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(projectversion.Table, projectversion.FieldID, selector),
			sqlgraph.To(workflowrun.Table, workflowrun.FieldID),
			sqlgraph.Edge(sqlgraph.O2M, false, projectversion.RunsTable, projectversion.RunsColumn),
		)
		fromU = sqlgraph.SetNeighbors(pvq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}

// First returns the first ProjectVersion entity from the query.
// Returns a *NotFoundError when no ProjectVersion was found.
func (pvq *ProjectVersionQuery) First(ctx context.Context) (*ProjectVersion, error) {
	nodes, err := pvq.Limit(1).All(setContextOp(ctx, pvq.ctx, ent.OpQueryFirst))
	if err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nil, &NotFoundError{projectversion.Label}
	}
	return nodes[0], nil
}

// FirstX is like First, but panics if an error occurs.
func (pvq *ProjectVersionQuery) FirstX(ctx context.Context) *ProjectVersion {
	node, err := pvq.First(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return node
}

// FirstID returns the first ProjectVersion ID from the query.
// Returns a *NotFoundError when no ProjectVersion ID was found.
func (pvq *ProjectVersionQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) {
	var ids []uuid.UUID
	if ids, err = pvq.Limit(1).IDs(setContextOp(ctx, pvq.ctx, ent.OpQueryFirstID)); err != nil {
		return
	}
	if len(ids) == 0 {
		err = &NotFoundError{projectversion.Label}
		return
	}
	return ids[0], nil
}

// FirstIDX is like FirstID, but panics if an error occurs.
func (pvq *ProjectVersionQuery) FirstIDX(ctx context.Context) uuid.UUID {
	id, err := pvq.FirstID(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return id
}

// Only returns a single ProjectVersion entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one ProjectVersion entity is found.
// Returns a *NotFoundError when no ProjectVersion entities are found.
func (pvq *ProjectVersionQuery) Only(ctx context.Context) (*ProjectVersion, error) {
	nodes, err := pvq.Limit(2).All(setContextOp(ctx, pvq.ctx, ent.OpQueryOnly))
	if err != nil {
		return nil, err
	}
	switch len(nodes) {
	case 1:
		return nodes[0], nil
	case 0:
		return nil, &NotFoundError{projectversion.Label}
	default:
		return nil, &NotSingularError{projectversion.Label}
	}
}

// OnlyX is like Only, but panics if an error occurs.
func (pvq *ProjectVersionQuery) OnlyX(ctx context.Context) *ProjectVersion {
	node, err := pvq.Only(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// OnlyID is like Only, but returns the only ProjectVersion ID in the query.
// Returns a *NotSingularError when more than one ProjectVersion ID is found.
// Returns a *NotFoundError when no entities are found.
func (pvq *ProjectVersionQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) {
	var ids []uuid.UUID
	if ids, err = pvq.Limit(2).IDs(setContextOp(ctx, pvq.ctx, ent.OpQueryOnlyID)); err != nil {
		return
	}
	switch len(ids) {
	case 1:
		id = ids[0]
	case 0:
		err = &NotFoundError{projectversion.Label}
	default:
		err = &NotSingularError{projectversion.Label}
	}
	return
}

// OnlyIDX is like OnlyID, but panics if an error occurs.
func (pvq *ProjectVersionQuery) OnlyIDX(ctx context.Context) uuid.UUID {
	id, err := pvq.OnlyID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}

// All executes the query and returns a list of ProjectVersions.
func (pvq *ProjectVersionQuery) All(ctx context.Context) ([]*ProjectVersion, error) {
	ctx = setContextOp(ctx, pvq.ctx, ent.OpQueryAll)
	if err := pvq.prepareQuery(ctx); err != nil {
		return nil, err
	}
	qr := querierAll[[]*ProjectVersion, *ProjectVersionQuery]()
	return withInterceptors[[]*ProjectVersion](ctx, pvq, qr, pvq.inters)
}

// AllX is like All, but panics if an error occurs.
func (pvq *ProjectVersionQuery) AllX(ctx context.Context) []*ProjectVersion {
	nodes, err := pvq.All(ctx)
	if err != nil {
		panic(err)
	}
	return nodes
}

// IDs executes the query and returns a list of ProjectVersion IDs.
func (pvq *ProjectVersionQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) {
	if pvq.ctx.Unique == nil && pvq.path != nil {
		pvq.Unique(true)
	}
	ctx = setContextOp(ctx, pvq.ctx, ent.OpQueryIDs)
	if err = pvq.Select(projectversion.FieldID).Scan(ctx, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}

// IDsX is like IDs, but panics if an error occurs.
func (pvq *ProjectVersionQuery) IDsX(ctx context.Context) []uuid.UUID {
	ids, err := pvq.IDs(ctx)
	if err != nil {
		panic(err)
	}
	return ids
}

// Count returns the count of the given query.
func (pvq *ProjectVersionQuery) Count(ctx context.Context) (int, error) {
	ctx = setContextOp(ctx, pvq.ctx, ent.OpQueryCount)
	if err := pvq.prepareQuery(ctx); err != nil {
		return 0, err
	}
	return withInterceptors[int](ctx, pvq, querierCount[*ProjectVersionQuery](), pvq.inters)
}

// CountX is like Count, but panics if an error occurs.
func (pvq *ProjectVersionQuery) CountX(ctx context.Context) int {
	count, err := pvq.Count(ctx)
	if err != nil {
		panic(err)
	}
	return count
}

// Exist returns true if the query has elements in the graph.
func (pvq *ProjectVersionQuery) Exist(ctx context.Context) (bool, error) {
	ctx = setContextOp(ctx, pvq.ctx, ent.OpQueryExist)
	switch _, err := pvq.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 (pvq *ProjectVersionQuery) ExistX(ctx context.Context) bool {
	exist, err := pvq.Exist(ctx)
	if err != nil {
		panic(err)
	}
	return exist
}

// Clone returns a duplicate of the ProjectVersionQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (pvq *ProjectVersionQuery) Clone() *ProjectVersionQuery {
	if pvq == nil {
		return nil
	}
	return &ProjectVersionQuery{
		config:      pvq.config,
		ctx:         pvq.ctx.Clone(),
		order:       append([]projectversion.OrderOption{}, pvq.order...),
		inters:      append([]Interceptor{}, pvq.inters...),
		predicates:  append([]predicate.ProjectVersion{}, pvq.predicates...),
		withProject: pvq.withProject.Clone(),
		withRuns:    pvq.withRuns.Clone(),
		// clone intermediate query.
		sql:       pvq.sql.Clone(),
		path:      pvq.path,
		modifiers: append([]func(*sql.Selector){}, pvq.modifiers...),
	}
}

// WithProject tells the query-builder to eager-load the nodes that are connected to
// the "project" edge. The optional arguments are used to configure the query builder of the edge.
func (pvq *ProjectVersionQuery) WithProject(opts ...func(*ProjectQuery)) *ProjectVersionQuery {
	query := (&ProjectClient{config: pvq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	pvq.withProject = query
	return pvq
}

// WithRuns tells the query-builder to eager-load the nodes that are connected to
// the "runs" edge. The optional arguments are used to configure the query builder of the edge.
func (pvq *ProjectVersionQuery) WithRuns(opts ...func(*WorkflowRunQuery)) *ProjectVersionQuery {
	query := (&WorkflowRunClient{config: pvq.config}).Query()
	for _, opt := range opts {
		opt(query)
	}
	pvq.withRuns = query
	return pvq
}

// 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 {
//		Version string `json:"version,omitempty"`
//		Count int `json:"count,omitempty"`
//	}
//
//	client.ProjectVersion.Query().
//		GroupBy(projectversion.FieldVersion).
//		Aggregate(ent.Count()).
//		Scan(ctx, &v)
func (pvq *ProjectVersionQuery) GroupBy(field string, fields ...string) *ProjectVersionGroupBy {
	pvq.ctx.Fields = append([]string{field}, fields...)
	grbuild := &ProjectVersionGroupBy{build: pvq}
	grbuild.flds = &pvq.ctx.Fields
	grbuild.label = projectversion.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 {
//		Version string `json:"version,omitempty"`
//	}
//
//	client.ProjectVersion.Query().
//		Select(projectversion.FieldVersion).
//		Scan(ctx, &v)
func (pvq *ProjectVersionQuery) Select(fields ...string) *ProjectVersionSelect {
	pvq.ctx.Fields = append(pvq.ctx.Fields, fields...)
	sbuild := &ProjectVersionSelect{ProjectVersionQuery: pvq}
	sbuild.label = projectversion.Label
	sbuild.flds, sbuild.scan = &pvq.ctx.Fields, sbuild.Scan
	return sbuild
}

// Aggregate returns a ProjectVersionSelect configured with the given aggregations.
func (pvq *ProjectVersionQuery) Aggregate(fns ...AggregateFunc) *ProjectVersionSelect {
	return pvq.Select().Aggregate(fns...)
}

func (pvq *ProjectVersionQuery) prepareQuery(ctx context.Context) error {
	for _, inter := range pvq.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, pvq); err != nil {
				return err
			}
		}
	}
	for _, f := range pvq.ctx.Fields {
		if !projectversion.ValidColumn(f) {
			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
		}
	}
	if pvq.path != nil {
		prev, err := pvq.path(ctx)
		if err != nil {
			return err
		}
		pvq.sql = prev
	}
	return nil
}

func (pvq *ProjectVersionQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*ProjectVersion, error) {
	var (
		nodes       = []*ProjectVersion{}
		_spec       = pvq.querySpec()
		loadedTypes = [2]bool{
			pvq.withProject != nil,
			pvq.withRuns != nil,
		}
	)
	_spec.ScanValues = func(columns []string) ([]any, error) {
		return (*ProjectVersion).scanValues(nil, columns)
	}
	_spec.Assign = func(columns []string, values []any) error {
		node := &ProjectVersion{config: pvq.config}
		nodes = append(nodes, node)
		node.Edges.loadedTypes = loadedTypes
		return node.assignValues(columns, values)
	}
	if len(pvq.modifiers) > 0 {
		_spec.Modifiers = pvq.modifiers
	}
	for i := range hooks {
		hooks[i](ctx, _spec)
	}
	if err := sqlgraph.QueryNodes(ctx, pvq.driver, _spec); err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nodes, nil
	}
	if query := pvq.withProject; query != nil {
		if err := pvq.loadProject(ctx, query, nodes, nil,
			func(n *ProjectVersion, e *Project) { n.Edges.Project = e }); err != nil {
			return nil, err
		}
	}
	if query := pvq.withRuns; query != nil {
		if err := pvq.loadRuns(ctx, query, nodes,
			func(n *ProjectVersion) { n.Edges.Runs = []*WorkflowRun{} },
			func(n *ProjectVersion, e *WorkflowRun) { n.Edges.Runs = append(n.Edges.Runs, e) }); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

func (pvq *ProjectVersionQuery) loadProject(ctx context.Context, query *ProjectQuery, nodes []*ProjectVersion, init func(*ProjectVersion), assign func(*ProjectVersion, *Project)) error {
	ids := make([]uuid.UUID, 0, len(nodes))
	nodeids := make(map[uuid.UUID][]*ProjectVersion)
	for i := range nodes {
		fk := nodes[i].ProjectID
		if _, ok := nodeids[fk]; !ok {
			ids = append(ids, fk)
		}
		nodeids[fk] = append(nodeids[fk], nodes[i])
	}
	if len(ids) == 0 {
		return nil
	}
	query.Where(project.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 "project_id" returned %v`, n.ID)
		}
		for i := range nodes {
			assign(nodes[i], n)
		}
	}
	return nil
}
func (pvq *ProjectVersionQuery) loadRuns(ctx context.Context, query *WorkflowRunQuery, nodes []*ProjectVersion, init func(*ProjectVersion), assign func(*ProjectVersion, *WorkflowRun)) error {
	fks := make([]driver.Value, 0, len(nodes))
	nodeids := make(map[uuid.UUID]*ProjectVersion)
	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
	if len(query.ctx.Fields) > 0 {
		query.ctx.AppendFieldOnce(workflowrun.FieldVersionID)
	}
	query.Where(predicate.WorkflowRun(func(s *sql.Selector) {
		s.Where(sql.InValues(s.C(projectversion.RunsColumn), fks...))
	}))
	neighbors, err := query.All(ctx)
	if err != nil {
		return err
	}
	for _, n := range neighbors {
		fk := n.VersionID
		node, ok := nodeids[fk]
		if !ok {
			return fmt.Errorf(`unexpected referenced foreign-key "version_id" returned %v for node %v`, fk, n.ID)
		}
		assign(node, n)
	}
	return nil
}

func (pvq *ProjectVersionQuery) sqlCount(ctx context.Context) (int, error) {
	_spec := pvq.querySpec()
	if len(pvq.modifiers) > 0 {
		_spec.Modifiers = pvq.modifiers
	}
	_spec.Node.Columns = pvq.ctx.Fields
	if len(pvq.ctx.Fields) > 0 {
		_spec.Unique = pvq.ctx.Unique != nil && *pvq.ctx.Unique
	}
	return sqlgraph.CountNodes(ctx, pvq.driver, _spec)
}

func (pvq *ProjectVersionQuery) querySpec() *sqlgraph.QuerySpec {
	_spec := sqlgraph.NewQuerySpec(projectversion.Table, projectversion.Columns, sqlgraph.NewFieldSpec(projectversion.FieldID, field.TypeUUID))
	_spec.From = pvq.sql
	if unique := pvq.ctx.Unique; unique != nil {
		_spec.Unique = *unique
	} else if pvq.path != nil {
		_spec.Unique = true
	}
	if fields := pvq.ctx.Fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, projectversion.FieldID)
		for i := range fields {
			if fields[i] != projectversion.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
			}
		}
		if pvq.withProject != nil {
			_spec.Node.AddColumnOnce(projectversion.FieldProjectID)
		}
	}
	if ps := pvq.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if limit := pvq.ctx.Limit; limit != nil {
		_spec.Limit = *limit
	}
	if offset := pvq.ctx.Offset; offset != nil {
		_spec.Offset = *offset
	}
	if ps := pvq.order; len(ps) > 0 {
		_spec.Order = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return _spec
}

func (pvq *ProjectVersionQuery) sqlQuery(ctx context.Context) *sql.Selector {
	builder := sql.Dialect(pvq.driver.Dialect())
	t1 := builder.Table(projectversion.Table)
	columns := pvq.ctx.Fields
	if len(columns) == 0 {
		columns = projectversion.Columns
	}
	selector := builder.Select(t1.Columns(columns...)...).From(t1)
	if pvq.sql != nil {
		selector = pvq.sql
		selector.Select(selector.Columns(columns...)...)
	}
	if pvq.ctx.Unique != nil && *pvq.ctx.Unique {
		selector.Distinct()
	}
	for _, m := range pvq.modifiers {
		m(selector)
	}
	for _, p := range pvq.predicates {
		p(selector)
	}
	for _, p := range pvq.order {
		p(selector)
	}
	if offset := pvq.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 := pvq.ctx.Limit; limit != nil {
		selector.Limit(*limit)
	}
	return selector
}

// ForUpdate locks the selected rows against concurrent updates, and prevent them from being
// updated, deleted or "selected ... for update" by other sessions, until the transaction is
// either committed or rolled-back.
func (pvq *ProjectVersionQuery) ForUpdate(opts ...sql.LockOption) *ProjectVersionQuery {
	if pvq.driver.Dialect() == dialect.Postgres {
		pvq.Unique(false)
	}
	pvq.modifiers = append(pvq.modifiers, func(s *sql.Selector) {
		s.ForUpdate(opts...)
	})
	return pvq
}

// ForShare behaves similarly to ForUpdate, except that it acquires a shared mode lock
// on any rows that are read. Other sessions can read the rows, but cannot modify them
// until your transaction commits.
func (pvq *ProjectVersionQuery) ForShare(opts ...sql.LockOption) *ProjectVersionQuery {
	if pvq.driver.Dialect() == dialect.Postgres {
		pvq.Unique(false)
	}
	pvq.modifiers = append(pvq.modifiers, func(s *sql.Selector) {
		s.ForShare(opts...)
	})
	return pvq
}

// Modify adds a query modifier for attaching custom logic to queries.
func (pvq *ProjectVersionQuery) Modify(modifiers ...func(s *sql.Selector)) *ProjectVersionSelect {
	pvq.modifiers = append(pvq.modifiers, modifiers...)
	return pvq.Select()
}

// ProjectVersionGroupBy is the group-by builder for ProjectVersion entities.
type ProjectVersionGroupBy struct {
	selector
	build *ProjectVersionQuery
}

// Aggregate adds the given aggregation functions to the group-by query.
func (pvgb *ProjectVersionGroupBy) Aggregate(fns ...AggregateFunc) *ProjectVersionGroupBy {
	pvgb.fns = append(pvgb.fns, fns...)
	return pvgb
}

// Scan applies the selector query and scans the result into the given value.
func (pvgb *ProjectVersionGroupBy) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, pvgb.build.ctx, ent.OpQueryGroupBy)
	if err := pvgb.build.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*ProjectVersionQuery, *ProjectVersionGroupBy](ctx, pvgb.build, pvgb, pvgb.build.inters, v)
}

func (pvgb *ProjectVersionGroupBy) sqlScan(ctx context.Context, root *ProjectVersionQuery, v any) error {
	selector := root.sqlQuery(ctx).Select()
	aggregation := make([]string, 0, len(pvgb.fns))
	for _, fn := range pvgb.fns {
		aggregation = append(aggregation, fn(selector))
	}
	if len(selector.SelectedColumns()) == 0 {
		columns := make([]string, 0, len(*pvgb.flds)+len(pvgb.fns))
		for _, f := range *pvgb.flds {
			columns = append(columns, selector.C(f))
		}
		columns = append(columns, aggregation...)
		selector.Select(columns...)
	}
	selector.GroupBy(selector.Columns(*pvgb.flds...)...)
	if err := selector.Err(); err != nil {
		return err
	}
	rows := &sql.Rows{}
	query, args := selector.Query()
	if err := pvgb.build.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}

// ProjectVersionSelect is the builder for selecting fields of ProjectVersion entities.
type ProjectVersionSelect struct {
	*ProjectVersionQuery
	selector
}

// Aggregate adds the given aggregation functions to the selector query.
func (pvs *ProjectVersionSelect) Aggregate(fns ...AggregateFunc) *ProjectVersionSelect {
	pvs.fns = append(pvs.fns, fns...)
	return pvs
}

// Scan applies the selector query and scans the result into the given value.
func (pvs *ProjectVersionSelect) Scan(ctx context.Context, v any) error {
	ctx = setContextOp(ctx, pvs.ctx, ent.OpQuerySelect)
	if err := pvs.prepareQuery(ctx); err != nil {
		return err
	}
	return scanWithInterceptors[*ProjectVersionQuery, *ProjectVersionSelect](ctx, pvs.ProjectVersionQuery, pvs, pvs.inters, v)
}

func (pvs *ProjectVersionSelect) sqlScan(ctx context.Context, root *ProjectVersionQuery, v any) error {
	selector := root.sqlQuery(ctx)
	aggregation := make([]string, 0, len(pvs.fns))
	for _, fn := range pvs.fns {
		aggregation = append(aggregation, fn(selector))
	}
	switch n := len(*pvs.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 := pvs.driver.Query(ctx, query, args, rows); err != nil {
		return err
	}
	defer rows.Close()
	return sql.ScanSlice(rows, v)
}

// Modify adds a query modifier for attaching custom logic to queries.
func (pvs *ProjectVersionSelect) Modify(modifiers ...func(s *sql.Selector)) *ProjectVersionSelect {
	pvs.modifiers = append(pvs.modifiers, modifiers...)
	return pvs
}
