func (igb *ImageGroupBy) Scan(ctx context.Context, v interface{}) error {
	query, err := igb.path(ctx)
	if err != nil {
		return err
	}
	igb.sql = query
	return igb.sqlScan(ctx, v)
}
Scan applies the group-by query and scans the result into the given value.
package ent
import (
	"context"
	"database/sql/driver"
	"errors"
	"fmt"
	"math"
	"entgo.io/contrib/entproto/internal/entprototest/ent/image"
	"entgo.io/contrib/entproto/internal/entprototest/ent/predicate"
	"entgo.io/contrib/entproto/internal/entprototest/ent/user"
	"entgo.io/ent/dialect/sql"
	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/google/uuid"
)
type ImageQuery struct {
	config
	limit      *int
	offset     *int
	unique     *bool
	order      []OrderFunc
	fields     []string
	predicates []predicate.Image
	withUserProfilePic *UserQuery
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}
func (iq *ImageQuery) Where(ps ...predicate.Image) *ImageQuery {
	iq.predicates = append(iq.predicates, ps...)
	return iq
}
func (iq *ImageQuery) Limit(limit int) *ImageQuery {
	iq.limit = &limit
	return iq
}
func (iq *ImageQuery) Offset(offset int) *ImageQuery {
	iq.offset = &offset
	return iq
}
func (iq *ImageQuery) Unique(unique bool) *ImageQuery {
	iq.unique = &unique
	return iq
}
func (iq *ImageQuery) Order(o ...OrderFunc) *ImageQuery {
	iq.order = append(iq.order, o...)
	return iq
}
func (iq *ImageQuery) QueryUserProfilePic() *UserQuery {
	query := &UserQuery{config: iq.config}
	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
		if err := iq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		selector := iq.sqlQuery(ctx)
		if err := selector.Err(); err != nil {
			return nil, err
		}
		step := sqlgraph.NewStep(
			sqlgraph.From(image.Table, image.FieldID, selector),
			sqlgraph.To(user.Table, user.FieldID),
			sqlgraph.Edge(sqlgraph.O2M, true, image.UserProfilePicTable, image.UserProfilePicColumn),
		)
		fromU = sqlgraph.SetNeighbors(iq.driver.Dialect(), step)
		return fromU, nil
	}
	return query
}
func (iq *ImageQuery) First(ctx context.Context) (*Image, error) {
	nodes, err := iq.Limit(1).All(ctx)
	if err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nil, &NotFoundError{image.Label}
	}
	return nodes[0], nil
}
func (iq *ImageQuery) FirstX(ctx context.Context) *Image {
	node, err := iq.First(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return node
}
func (iq *ImageQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) {
	var ids []uuid.UUID
	if ids, err = iq.Limit(1).IDs(ctx); err != nil {
		return
	}
	if len(ids) == 0 {
		err = &NotFoundError{image.Label}
		return
	}
	return ids[0], nil
}
func (iq *ImageQuery) FirstIDX(ctx context.Context) uuid.UUID {
	id, err := iq.FirstID(ctx)
	if err != nil && !IsNotFound(err) {
		panic(err)
	}
	return id
}
func (iq *ImageQuery) Only(ctx context.Context) (*Image, error) {
	nodes, err := iq.Limit(2).All(ctx)
	if err != nil {
		return nil, err
	}
	switch len(nodes) {
	case 1:
		return nodes[0], nil
	case 0:
		return nil, &NotFoundError{image.Label}
	default:
		return nil, &NotSingularError{image.Label}
	}
}
func (iq *ImageQuery) OnlyX(ctx context.Context) *Image {
	node, err := iq.Only(ctx)
	if err != nil {
		panic(err)
	}
	return node
}
func (iq *ImageQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) {
	var ids []uuid.UUID
	if ids, err = iq.Limit(2).IDs(ctx); err != nil {
		return
	}
	switch len(ids) {
	case 1:
		id = ids[0]
	case 0:
		err = &NotFoundError{image.Label}
	default:
		err = &NotSingularError{image.Label}
	}
	return
}
func (iq *ImageQuery) OnlyIDX(ctx context.Context) uuid.UUID {
	id, err := iq.OnlyID(ctx)
	if err != nil {
		panic(err)
	}
	return id
}
func (iq *ImageQuery) All(ctx context.Context) ([]*Image, error) {
	if err := iq.prepareQuery(ctx); err != nil {
		return nil, err
	}
	return iq.sqlAll(ctx)
}
func (iq *ImageQuery) AllX(ctx context.Context) []*Image {
	nodes, err := iq.All(ctx)
	if err != nil {
		panic(err)
	}
	return nodes
}
func (iq *ImageQuery) IDs(ctx context.Context) ([]uuid.UUID, error) {
	var ids []uuid.UUID
	if err := iq.Select(image.FieldID).Scan(ctx, &ids); err != nil {
		return nil, err
	}
	return ids, nil
}
func (iq *ImageQuery) IDsX(ctx context.Context) []uuid.UUID {
	ids, err := iq.IDs(ctx)
	if err != nil {
		panic(err)
	}
	return ids
}
func (iq *ImageQuery) Count(ctx context.Context) (int, error) {
	if err := iq.prepareQuery(ctx); err != nil {
		return 0, err
	}
	return iq.sqlCount(ctx)
}
func (iq *ImageQuery) CountX(ctx context.Context) int {
	count, err := iq.Count(ctx)
	if err != nil {
		panic(err)
	}
	return count
}
func (iq *ImageQuery) Exist(ctx context.Context) (bool, error) {
	if err := iq.prepareQuery(ctx); err != nil {
		return false, err
	}
	return iq.sqlExist(ctx)
}
func (iq *ImageQuery) ExistX(ctx context.Context) bool {
	exist, err := iq.Exist(ctx)
	if err != nil {
		panic(err)
	}
	return exist
}
func (iq *ImageQuery) Clone() *ImageQuery {
	if iq == nil {
		return nil
	}
	return &ImageQuery{
		config:             iq.config,
		limit:              iq.limit,
		offset:             iq.offset,
		order:              append([]OrderFunc{}, iq.order...),
		predicates:         append([]predicate.Image{}, iq.predicates...),
		withUserProfilePic: iq.withUserProfilePic.Clone(),
		sql:  iq.sql.Clone(),
		path: iq.path,
	}
}
func (iq *ImageQuery) WithUserProfilePic(opts ...func(*UserQuery)) *ImageQuery {
	query := &UserQuery{config: iq.config}
	for _, opt := range opts {
		opt(query)
	}
	iq.withUserProfilePic = query
	return iq
}
func (iq *ImageQuery) GroupBy(field string, fields ...string) *ImageGroupBy {
	group := &ImageGroupBy{config: iq.config}
	group.fields = append([]string{field}, fields...)
	group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
		if err := iq.prepareQuery(ctx); err != nil {
			return nil, err
		}
		return iq.sqlQuery(ctx), nil
	}
	return group
}
func (iq *ImageQuery) Select(fields ...string) *ImageSelect {
	iq.fields = append(iq.fields, fields...)
	return &ImageSelect{ImageQuery: iq}
}
func (iq *ImageQuery) prepareQuery(ctx context.Context) error {
	for _, f := range iq.fields {
		if !image.ValidColumn(f) {
			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
		}
	}
	if iq.path != nil {
		prev, err := iq.path(ctx)
		if err != nil {
			return err
		}
		iq.sql = prev
	}
	return nil
}
func (iq *ImageQuery) sqlAll(ctx context.Context) ([]*Image, error) {
	var (
		nodes       = []*Image{}
		_spec       = iq.querySpec()
		loadedTypes = [1]bool{
			iq.withUserProfilePic != nil,
		}
	)
	_spec.ScanValues = func(columns []string) ([]interface{}, error) {
		node := &Image{config: iq.config}
		nodes = append(nodes, node)
		return node.scanValues(columns)
	}
	_spec.Assign = func(columns []string, values []interface{}) error {
		if len(nodes) == 0 {
			return fmt.Errorf("ent: Assign called without calling ScanValues")
		}
		node := nodes[len(nodes)-1]
		node.Edges.loadedTypes = loadedTypes
		return node.assignValues(columns, values)
	}
	if err := sqlgraph.QueryNodes(ctx, iq.driver, _spec); err != nil {
		return nil, err
	}
	if len(nodes) == 0 {
		return nodes, nil
	}
	if query := iq.withUserProfilePic; query != nil {
		fks := make([]driver.Value, 0, len(nodes))
		nodeids := make(map[uuid.UUID]*Image)
		for i := range nodes {
			fks = append(fks, nodes[i].ID)
			nodeids[nodes[i].ID] = nodes[i]
			nodes[i].Edges.UserProfilePic = []*User{}
		}
		query.withFKs = true
		query.Where(predicate.User(func(s *sql.Selector) {
			s.Where(sql.InValues(image.UserProfilePicColumn, fks...))
		}))
		neighbors, err := query.All(ctx)
		if err != nil {
			return nil, err
		}
		for _, n := range neighbors {
			fk := n.user_profile_pic
			if fk == nil {
				return nil, fmt.Errorf(`foreign-key "user_profile_pic" is nil for node %v`, n.ID)
			}
			node, ok := nodeids[*fk]
			if !ok {
				return nil, fmt.Errorf(`unexpected foreign-key "user_profile_pic" returned %v for node %v`, *fk, n.ID)
			}
			node.Edges.UserProfilePic = append(node.Edges.UserProfilePic, n)
		}
	}
	return nodes, nil
}
func (iq *ImageQuery) sqlCount(ctx context.Context) (int, error) {
	_spec := iq.querySpec()
	return sqlgraph.CountNodes(ctx, iq.driver, _spec)
}
func (iq *ImageQuery) sqlExist(ctx context.Context) (bool, error) {
	n, err := iq.sqlCount(ctx)
	if err != nil {
		return false, fmt.Errorf("ent: check existence: %w", err)
	}
	return n > 0, nil
}
func (iq *ImageQuery) querySpec() *sqlgraph.QuerySpec {
	_spec := &sqlgraph.QuerySpec{
		Node: &sqlgraph.NodeSpec{
			Table:   image.Table,
			Columns: image.Columns,
			ID: &sqlgraph.FieldSpec{
				Type:   field.TypeUUID,
				Column: image.FieldID,
			},
		},
		From:   iq.sql,
		Unique: true,
	}
	if unique := iq.unique; unique != nil {
		_spec.Unique = *unique
	}
	if fields := iq.fields; len(fields) > 0 {
		_spec.Node.Columns = make([]string, 0, len(fields))
		_spec.Node.Columns = append(_spec.Node.Columns, image.FieldID)
		for i := range fields {
			if fields[i] != image.FieldID {
				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
			}
		}
	}
	if ps := iq.predicates; len(ps) > 0 {
		_spec.Predicate = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	if limit := iq.limit; limit != nil {
		_spec.Limit = *limit
	}
	if offset := iq.offset; offset != nil {
		_spec.Offset = *offset
	}
	if ps := iq.order; len(ps) > 0 {
		_spec.Order = func(selector *sql.Selector) {
			for i := range ps {
				ps[i](selector)
			}
		}
	}
	return _spec
}
func (iq *ImageQuery) sqlQuery(ctx context.Context) *sql.Selector {
	builder := sql.Dialect(iq.driver.Dialect())
	t1 := builder.Table(image.Table)
	columns := iq.fields
	if len(columns) == 0 {
		columns = image.Columns
	}
	selector := builder.Select(t1.Columns(columns...)...).From(t1)
	if iq.sql != nil {
		selector = iq.sql
		selector.Select(selector.Columns(columns...)...)
	}
	for _, p := range iq.predicates {
		p(selector)
	}
	for _, p := range iq.order {
		p(selector)
	}
	if offset := iq.offset; offset != nil {
		selector.Offset(*offset).Limit(math.MaxInt32)
	}
	if limit := iq.limit; limit != nil {
		selector.Limit(*limit)
	}
	return selector
}
type ImageGroupBy struct {
	config
	fields []string
	fns    []AggregateFunc
	sql  *sql.Selector
	path func(context.Context) (*sql.Selector, error)
}
func (igb *ImageGroupBy) Aggregate(fns ...AggregateFunc) *ImageGroupBy {
	igb.fns = append(igb.fns, fns...)
	return igb
}