github.com/ngocphuongnb/tetua@v0.0.7-alpha/packages/entrepository/ent/user_query.go (about)

     1  // Code generated by entc, DO NOT EDIT.
     2  
     3  package ent
     4  
     5  import (
     6  	"context"
     7  	"database/sql/driver"
     8  	"errors"
     9  	"fmt"
    10  	"math"
    11  
    12  	"entgo.io/ent/dialect/sql"
    13  	"entgo.io/ent/dialect/sql/sqlgraph"
    14  	"entgo.io/ent/schema/field"
    15  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/comment"
    16  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/file"
    17  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/post"
    18  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/predicate"
    19  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/role"
    20  	"github.com/ngocphuongnb/tetua/packages/entrepository/ent/user"
    21  )
    22  
    23  // UserQuery is the builder for querying User entities.
    24  type UserQuery struct {
    25  	config
    26  	limit      *int
    27  	offset     *int
    28  	unique     *bool
    29  	order      []OrderFunc
    30  	fields     []string
    31  	predicates []predicate.User
    32  	// eager-loading edges.
    33  	withPosts       *PostQuery
    34  	withFiles       *FileQuery
    35  	withComments    *CommentQuery
    36  	withRoles       *RoleQuery
    37  	withAvatarImage *FileQuery
    38  	// intermediate query (i.e. traversal path).
    39  	sql  *sql.Selector
    40  	path func(context.Context) (*sql.Selector, error)
    41  }
    42  
    43  // Where adds a new predicate for the UserQuery builder.
    44  func (uq *UserQuery) Where(ps ...predicate.User) *UserQuery {
    45  	uq.predicates = append(uq.predicates, ps...)
    46  	return uq
    47  }
    48  
    49  // Limit adds a limit step to the query.
    50  func (uq *UserQuery) Limit(limit int) *UserQuery {
    51  	uq.limit = &limit
    52  	return uq
    53  }
    54  
    55  // Offset adds an offset step to the query.
    56  func (uq *UserQuery) Offset(offset int) *UserQuery {
    57  	uq.offset = &offset
    58  	return uq
    59  }
    60  
    61  // Unique configures the query builder to filter duplicate records on query.
    62  // By default, unique is set to true, and can be disabled using this method.
    63  func (uq *UserQuery) Unique(unique bool) *UserQuery {
    64  	uq.unique = &unique
    65  	return uq
    66  }
    67  
    68  // Order adds an order step to the query.
    69  func (uq *UserQuery) Order(o ...OrderFunc) *UserQuery {
    70  	uq.order = append(uq.order, o...)
    71  	return uq
    72  }
    73  
    74  // QueryPosts chains the current query on the "posts" edge.
    75  func (uq *UserQuery) QueryPosts() *PostQuery {
    76  	query := &PostQuery{config: uq.config}
    77  	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
    78  		if err := uq.prepareQuery(ctx); err != nil {
    79  			return nil, err
    80  		}
    81  		selector := uq.sqlQuery(ctx)
    82  		if err := selector.Err(); err != nil {
    83  			return nil, err
    84  		}
    85  		step := sqlgraph.NewStep(
    86  			sqlgraph.From(user.Table, user.FieldID, selector),
    87  			sqlgraph.To(post.Table, post.FieldID),
    88  			sqlgraph.Edge(sqlgraph.O2M, false, user.PostsTable, user.PostsColumn),
    89  		)
    90  		fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
    91  		return fromU, nil
    92  	}
    93  	return query
    94  }
    95  
    96  // QueryFiles chains the current query on the "files" edge.
    97  func (uq *UserQuery) QueryFiles() *FileQuery {
    98  	query := &FileQuery{config: uq.config}
    99  	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
   100  		if err := uq.prepareQuery(ctx); err != nil {
   101  			return nil, err
   102  		}
   103  		selector := uq.sqlQuery(ctx)
   104  		if err := selector.Err(); err != nil {
   105  			return nil, err
   106  		}
   107  		step := sqlgraph.NewStep(
   108  			sqlgraph.From(user.Table, user.FieldID, selector),
   109  			sqlgraph.To(file.Table, file.FieldID),
   110  			sqlgraph.Edge(sqlgraph.O2M, false, user.FilesTable, user.FilesColumn),
   111  		)
   112  		fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
   113  		return fromU, nil
   114  	}
   115  	return query
   116  }
   117  
   118  // QueryComments chains the current query on the "comments" edge.
   119  func (uq *UserQuery) QueryComments() *CommentQuery {
   120  	query := &CommentQuery{config: uq.config}
   121  	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
   122  		if err := uq.prepareQuery(ctx); err != nil {
   123  			return nil, err
   124  		}
   125  		selector := uq.sqlQuery(ctx)
   126  		if err := selector.Err(); err != nil {
   127  			return nil, err
   128  		}
   129  		step := sqlgraph.NewStep(
   130  			sqlgraph.From(user.Table, user.FieldID, selector),
   131  			sqlgraph.To(comment.Table, comment.FieldID),
   132  			sqlgraph.Edge(sqlgraph.O2M, false, user.CommentsTable, user.CommentsColumn),
   133  		)
   134  		fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
   135  		return fromU, nil
   136  	}
   137  	return query
   138  }
   139  
   140  // QueryRoles chains the current query on the "roles" edge.
   141  func (uq *UserQuery) QueryRoles() *RoleQuery {
   142  	query := &RoleQuery{config: uq.config}
   143  	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
   144  		if err := uq.prepareQuery(ctx); err != nil {
   145  			return nil, err
   146  		}
   147  		selector := uq.sqlQuery(ctx)
   148  		if err := selector.Err(); err != nil {
   149  			return nil, err
   150  		}
   151  		step := sqlgraph.NewStep(
   152  			sqlgraph.From(user.Table, user.FieldID, selector),
   153  			sqlgraph.To(role.Table, role.FieldID),
   154  			sqlgraph.Edge(sqlgraph.M2M, true, user.RolesTable, user.RolesPrimaryKey...),
   155  		)
   156  		fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
   157  		return fromU, nil
   158  	}
   159  	return query
   160  }
   161  
   162  // QueryAvatarImage chains the current query on the "avatar_image" edge.
   163  func (uq *UserQuery) QueryAvatarImage() *FileQuery {
   164  	query := &FileQuery{config: uq.config}
   165  	query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
   166  		if err := uq.prepareQuery(ctx); err != nil {
   167  			return nil, err
   168  		}
   169  		selector := uq.sqlQuery(ctx)
   170  		if err := selector.Err(); err != nil {
   171  			return nil, err
   172  		}
   173  		step := sqlgraph.NewStep(
   174  			sqlgraph.From(user.Table, user.FieldID, selector),
   175  			sqlgraph.To(file.Table, file.FieldID),
   176  			sqlgraph.Edge(sqlgraph.M2O, true, user.AvatarImageTable, user.AvatarImageColumn),
   177  		)
   178  		fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
   179  		return fromU, nil
   180  	}
   181  	return query
   182  }
   183  
   184  // First returns the first User entity from the query.
   185  // Returns a *NotFoundError when no User was found.
   186  func (uq *UserQuery) First(ctx context.Context) (*User, error) {
   187  	nodes, err := uq.Limit(1).All(ctx)
   188  	if err != nil {
   189  		return nil, err
   190  	}
   191  	if len(nodes) == 0 {
   192  		return nil, &NotFoundError{user.Label}
   193  	}
   194  	return nodes[0], nil
   195  }
   196  
   197  // FirstX is like First, but panics if an error occurs.
   198  func (uq *UserQuery) FirstX(ctx context.Context) *User {
   199  	node, err := uq.First(ctx)
   200  	if err != nil && !IsNotFound(err) {
   201  		panic(err)
   202  	}
   203  	return node
   204  }
   205  
   206  // FirstID returns the first User ID from the query.
   207  // Returns a *NotFoundError when no User ID was found.
   208  func (uq *UserQuery) FirstID(ctx context.Context) (id int, err error) {
   209  	var ids []int
   210  	if ids, err = uq.Limit(1).IDs(ctx); err != nil {
   211  		return
   212  	}
   213  	if len(ids) == 0 {
   214  		err = &NotFoundError{user.Label}
   215  		return
   216  	}
   217  	return ids[0], nil
   218  }
   219  
   220  // FirstIDX is like FirstID, but panics if an error occurs.
   221  func (uq *UserQuery) FirstIDX(ctx context.Context) int {
   222  	id, err := uq.FirstID(ctx)
   223  	if err != nil && !IsNotFound(err) {
   224  		panic(err)
   225  	}
   226  	return id
   227  }
   228  
   229  // Only returns a single User entity found by the query, ensuring it only returns one.
   230  // Returns a *NotSingularError when more than one User entity is found.
   231  // Returns a *NotFoundError when no User entities are found.
   232  func (uq *UserQuery) Only(ctx context.Context) (*User, error) {
   233  	nodes, err := uq.Limit(2).All(ctx)
   234  	if err != nil {
   235  		return nil, err
   236  	}
   237  	switch len(nodes) {
   238  	case 1:
   239  		return nodes[0], nil
   240  	case 0:
   241  		return nil, &NotFoundError{user.Label}
   242  	default:
   243  		return nil, &NotSingularError{user.Label}
   244  	}
   245  }
   246  
   247  // OnlyX is like Only, but panics if an error occurs.
   248  func (uq *UserQuery) OnlyX(ctx context.Context) *User {
   249  	node, err := uq.Only(ctx)
   250  	if err != nil {
   251  		panic(err)
   252  	}
   253  	return node
   254  }
   255  
   256  // OnlyID is like Only, but returns the only User ID in the query.
   257  // Returns a *NotSingularError when more than one User ID is found.
   258  // Returns a *NotFoundError when no entities are found.
   259  func (uq *UserQuery) OnlyID(ctx context.Context) (id int, err error) {
   260  	var ids []int
   261  	if ids, err = uq.Limit(2).IDs(ctx); err != nil {
   262  		return
   263  	}
   264  	switch len(ids) {
   265  	case 1:
   266  		id = ids[0]
   267  	case 0:
   268  		err = &NotFoundError{user.Label}
   269  	default:
   270  		err = &NotSingularError{user.Label}
   271  	}
   272  	return
   273  }
   274  
   275  // OnlyIDX is like OnlyID, but panics if an error occurs.
   276  func (uq *UserQuery) OnlyIDX(ctx context.Context) int {
   277  	id, err := uq.OnlyID(ctx)
   278  	if err != nil {
   279  		panic(err)
   280  	}
   281  	return id
   282  }
   283  
   284  // All executes the query and returns a list of Users.
   285  func (uq *UserQuery) All(ctx context.Context) ([]*User, error) {
   286  	if err := uq.prepareQuery(ctx); err != nil {
   287  		return nil, err
   288  	}
   289  	return uq.sqlAll(ctx)
   290  }
   291  
   292  // AllX is like All, but panics if an error occurs.
   293  func (uq *UserQuery) AllX(ctx context.Context) []*User {
   294  	nodes, err := uq.All(ctx)
   295  	if err != nil {
   296  		panic(err)
   297  	}
   298  	return nodes
   299  }
   300  
   301  // IDs executes the query and returns a list of User IDs.
   302  func (uq *UserQuery) IDs(ctx context.Context) ([]int, error) {
   303  	var ids []int
   304  	if err := uq.Select(user.FieldID).Scan(ctx, &ids); err != nil {
   305  		return nil, err
   306  	}
   307  	return ids, nil
   308  }
   309  
   310  // IDsX is like IDs, but panics if an error occurs.
   311  func (uq *UserQuery) IDsX(ctx context.Context) []int {
   312  	ids, err := uq.IDs(ctx)
   313  	if err != nil {
   314  		panic(err)
   315  	}
   316  	return ids
   317  }
   318  
   319  // Count returns the count of the given query.
   320  func (uq *UserQuery) Count(ctx context.Context) (int, error) {
   321  	if err := uq.prepareQuery(ctx); err != nil {
   322  		return 0, err
   323  	}
   324  	return uq.sqlCount(ctx)
   325  }
   326  
   327  // CountX is like Count, but panics if an error occurs.
   328  func (uq *UserQuery) CountX(ctx context.Context) int {
   329  	count, err := uq.Count(ctx)
   330  	if err != nil {
   331  		panic(err)
   332  	}
   333  	return count
   334  }
   335  
   336  // Exist returns true if the query has elements in the graph.
   337  func (uq *UserQuery) Exist(ctx context.Context) (bool, error) {
   338  	if err := uq.prepareQuery(ctx); err != nil {
   339  		return false, err
   340  	}
   341  	return uq.sqlExist(ctx)
   342  }
   343  
   344  // ExistX is like Exist, but panics if an error occurs.
   345  func (uq *UserQuery) ExistX(ctx context.Context) bool {
   346  	exist, err := uq.Exist(ctx)
   347  	if err != nil {
   348  		panic(err)
   349  	}
   350  	return exist
   351  }
   352  
   353  // Clone returns a duplicate of the UserQuery builder, including all associated steps. It can be
   354  // used to prepare common query builders and use them differently after the clone is made.
   355  func (uq *UserQuery) Clone() *UserQuery {
   356  	if uq == nil {
   357  		return nil
   358  	}
   359  	return &UserQuery{
   360  		config:          uq.config,
   361  		limit:           uq.limit,
   362  		offset:          uq.offset,
   363  		order:           append([]OrderFunc{}, uq.order...),
   364  		predicates:      append([]predicate.User{}, uq.predicates...),
   365  		withPosts:       uq.withPosts.Clone(),
   366  		withFiles:       uq.withFiles.Clone(),
   367  		withComments:    uq.withComments.Clone(),
   368  		withRoles:       uq.withRoles.Clone(),
   369  		withAvatarImage: uq.withAvatarImage.Clone(),
   370  		// clone intermediate query.
   371  		sql:    uq.sql.Clone(),
   372  		path:   uq.path,
   373  		unique: uq.unique,
   374  	}
   375  }
   376  
   377  // WithPosts tells the query-builder to eager-load the nodes that are connected to
   378  // the "posts" edge. The optional arguments are used to configure the query builder of the edge.
   379  func (uq *UserQuery) WithPosts(opts ...func(*PostQuery)) *UserQuery {
   380  	query := &PostQuery{config: uq.config}
   381  	for _, opt := range opts {
   382  		opt(query)
   383  	}
   384  	uq.withPosts = query
   385  	return uq
   386  }
   387  
   388  // WithFiles tells the query-builder to eager-load the nodes that are connected to
   389  // the "files" edge. The optional arguments are used to configure the query builder of the edge.
   390  func (uq *UserQuery) WithFiles(opts ...func(*FileQuery)) *UserQuery {
   391  	query := &FileQuery{config: uq.config}
   392  	for _, opt := range opts {
   393  		opt(query)
   394  	}
   395  	uq.withFiles = query
   396  	return uq
   397  }
   398  
   399  // WithComments tells the query-builder to eager-load the nodes that are connected to
   400  // the "comments" edge. The optional arguments are used to configure the query builder of the edge.
   401  func (uq *UserQuery) WithComments(opts ...func(*CommentQuery)) *UserQuery {
   402  	query := &CommentQuery{config: uq.config}
   403  	for _, opt := range opts {
   404  		opt(query)
   405  	}
   406  	uq.withComments = query
   407  	return uq
   408  }
   409  
   410  // WithRoles tells the query-builder to eager-load the nodes that are connected to
   411  // the "roles" edge. The optional arguments are used to configure the query builder of the edge.
   412  func (uq *UserQuery) WithRoles(opts ...func(*RoleQuery)) *UserQuery {
   413  	query := &RoleQuery{config: uq.config}
   414  	for _, opt := range opts {
   415  		opt(query)
   416  	}
   417  	uq.withRoles = query
   418  	return uq
   419  }
   420  
   421  // WithAvatarImage tells the query-builder to eager-load the nodes that are connected to
   422  // the "avatar_image" edge. The optional arguments are used to configure the query builder of the edge.
   423  func (uq *UserQuery) WithAvatarImage(opts ...func(*FileQuery)) *UserQuery {
   424  	query := &FileQuery{config: uq.config}
   425  	for _, opt := range opts {
   426  		opt(query)
   427  	}
   428  	uq.withAvatarImage = query
   429  	return uq
   430  }
   431  
   432  // GroupBy is used to group vertices by one or more fields/columns.
   433  // It is often used with aggregate functions, like: count, max, mean, min, sum.
   434  //
   435  // Example:
   436  //
   437  //	var v []struct {
   438  //		CreatedAt time.Time `json:"omitempty"`
   439  //		Count int `json:"count,omitempty"`
   440  //	}
   441  //
   442  //	client.User.Query().
   443  //		GroupBy(user.FieldCreatedAt).
   444  //		Aggregate(ent.Count()).
   445  //		Scan(ctx, &v)
   446  //
   447  func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
   448  	group := &UserGroupBy{config: uq.config}
   449  	group.fields = append([]string{field}, fields...)
   450  	group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
   451  		if err := uq.prepareQuery(ctx); err != nil {
   452  			return nil, err
   453  		}
   454  		return uq.sqlQuery(ctx), nil
   455  	}
   456  	return group
   457  }
   458  
   459  // Select allows the selection one or more fields/columns for the given query,
   460  // instead of selecting all fields in the entity.
   461  //
   462  // Example:
   463  //
   464  //	var v []struct {
   465  //		CreatedAt time.Time `json:"omitempty"`
   466  //	}
   467  //
   468  //	client.User.Query().
   469  //		Select(user.FieldCreatedAt).
   470  //		Scan(ctx, &v)
   471  //
   472  func (uq *UserQuery) Select(fields ...string) *UserSelect {
   473  	uq.fields = append(uq.fields, fields...)
   474  	return &UserSelect{UserQuery: uq}
   475  }
   476  
   477  func (uq *UserQuery) prepareQuery(ctx context.Context) error {
   478  	for _, f := range uq.fields {
   479  		if !user.ValidColumn(f) {
   480  			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
   481  		}
   482  	}
   483  	if uq.path != nil {
   484  		prev, err := uq.path(ctx)
   485  		if err != nil {
   486  			return err
   487  		}
   488  		uq.sql = prev
   489  	}
   490  	return nil
   491  }
   492  
   493  func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
   494  	var (
   495  		nodes       = []*User{}
   496  		_spec       = uq.querySpec()
   497  		loadedTypes = [5]bool{
   498  			uq.withPosts != nil,
   499  			uq.withFiles != nil,
   500  			uq.withComments != nil,
   501  			uq.withRoles != nil,
   502  			uq.withAvatarImage != nil,
   503  		}
   504  	)
   505  	_spec.ScanValues = func(columns []string) ([]interface{}, error) {
   506  		node := &User{config: uq.config}
   507  		nodes = append(nodes, node)
   508  		return node.scanValues(columns)
   509  	}
   510  	_spec.Assign = func(columns []string, values []interface{}) error {
   511  		if len(nodes) == 0 {
   512  			return fmt.Errorf("ent: Assign called without calling ScanValues")
   513  		}
   514  		node := nodes[len(nodes)-1]
   515  		node.Edges.loadedTypes = loadedTypes
   516  		return node.assignValues(columns, values)
   517  	}
   518  	if err := sqlgraph.QueryNodes(ctx, uq.driver, _spec); err != nil {
   519  		return nil, err
   520  	}
   521  	if len(nodes) == 0 {
   522  		return nodes, nil
   523  	}
   524  
   525  	if query := uq.withPosts; query != nil {
   526  		fks := make([]driver.Value, 0, len(nodes))
   527  		nodeids := make(map[int]*User)
   528  		for i := range nodes {
   529  			fks = append(fks, nodes[i].ID)
   530  			nodeids[nodes[i].ID] = nodes[i]
   531  			nodes[i].Edges.Posts = []*Post{}
   532  		}
   533  		query.Where(predicate.Post(func(s *sql.Selector) {
   534  			s.Where(sql.InValues(user.PostsColumn, fks...))
   535  		}))
   536  		neighbors, err := query.All(ctx)
   537  		if err != nil {
   538  			return nil, err
   539  		}
   540  		for _, n := range neighbors {
   541  			fk := n.UserID
   542  			node, ok := nodeids[fk]
   543  			if !ok {
   544  				return nil, fmt.Errorf(`unexpected foreign-key "user_id" returned %v for node %v`, fk, n.ID)
   545  			}
   546  			node.Edges.Posts = append(node.Edges.Posts, n)
   547  		}
   548  	}
   549  
   550  	if query := uq.withFiles; query != nil {
   551  		fks := make([]driver.Value, 0, len(nodes))
   552  		nodeids := make(map[int]*User)
   553  		for i := range nodes {
   554  			fks = append(fks, nodes[i].ID)
   555  			nodeids[nodes[i].ID] = nodes[i]
   556  			nodes[i].Edges.Files = []*File{}
   557  		}
   558  		query.Where(predicate.File(func(s *sql.Selector) {
   559  			s.Where(sql.InValues(user.FilesColumn, fks...))
   560  		}))
   561  		neighbors, err := query.All(ctx)
   562  		if err != nil {
   563  			return nil, err
   564  		}
   565  		for _, n := range neighbors {
   566  			fk := n.UserID
   567  			node, ok := nodeids[fk]
   568  			if !ok {
   569  				return nil, fmt.Errorf(`unexpected foreign-key "user_id" returned %v for node %v`, fk, n.ID)
   570  			}
   571  			node.Edges.Files = append(node.Edges.Files, n)
   572  		}
   573  	}
   574  
   575  	if query := uq.withComments; query != nil {
   576  		fks := make([]driver.Value, 0, len(nodes))
   577  		nodeids := make(map[int]*User)
   578  		for i := range nodes {
   579  			fks = append(fks, nodes[i].ID)
   580  			nodeids[nodes[i].ID] = nodes[i]
   581  			nodes[i].Edges.Comments = []*Comment{}
   582  		}
   583  		query.Where(predicate.Comment(func(s *sql.Selector) {
   584  			s.Where(sql.InValues(user.CommentsColumn, fks...))
   585  		}))
   586  		neighbors, err := query.All(ctx)
   587  		if err != nil {
   588  			return nil, err
   589  		}
   590  		for _, n := range neighbors {
   591  			fk := n.UserID
   592  			node, ok := nodeids[fk]
   593  			if !ok {
   594  				return nil, fmt.Errorf(`unexpected foreign-key "user_id" returned %v for node %v`, fk, n.ID)
   595  			}
   596  			node.Edges.Comments = append(node.Edges.Comments, n)
   597  		}
   598  	}
   599  
   600  	if query := uq.withRoles; query != nil {
   601  		fks := make([]driver.Value, 0, len(nodes))
   602  		ids := make(map[int]*User, len(nodes))
   603  		for _, node := range nodes {
   604  			ids[node.ID] = node
   605  			fks = append(fks, node.ID)
   606  			node.Edges.Roles = []*Role{}
   607  		}
   608  		var (
   609  			edgeids []int
   610  			edges   = make(map[int][]*User)
   611  		)
   612  		_spec := &sqlgraph.EdgeQuerySpec{
   613  			Edge: &sqlgraph.EdgeSpec{
   614  				Inverse: true,
   615  				Table:   user.RolesTable,
   616  				Columns: user.RolesPrimaryKey,
   617  			},
   618  			Predicate: func(s *sql.Selector) {
   619  				s.Where(sql.InValues(user.RolesPrimaryKey[1], fks...))
   620  			},
   621  			ScanValues: func() [2]interface{} {
   622  				return [2]interface{}{new(sql.NullInt64), new(sql.NullInt64)}
   623  			},
   624  			Assign: func(out, in interface{}) error {
   625  				eout, ok := out.(*sql.NullInt64)
   626  				if !ok || eout == nil {
   627  					return fmt.Errorf("unexpected id value for edge-out")
   628  				}
   629  				ein, ok := in.(*sql.NullInt64)
   630  				if !ok || ein == nil {
   631  					return fmt.Errorf("unexpected id value for edge-in")
   632  				}
   633  				outValue := int(eout.Int64)
   634  				inValue := int(ein.Int64)
   635  				node, ok := ids[outValue]
   636  				if !ok {
   637  					return fmt.Errorf("unexpected node id in edges: %v", outValue)
   638  				}
   639  				if _, ok := edges[inValue]; !ok {
   640  					edgeids = append(edgeids, inValue)
   641  				}
   642  				edges[inValue] = append(edges[inValue], node)
   643  				return nil
   644  			},
   645  		}
   646  		if err := sqlgraph.QueryEdges(ctx, uq.driver, _spec); err != nil {
   647  			return nil, fmt.Errorf(`query edges "roles": %w`, err)
   648  		}
   649  		query.Where(role.IDIn(edgeids...))
   650  		neighbors, err := query.All(ctx)
   651  		if err != nil {
   652  			return nil, err
   653  		}
   654  		for _, n := range neighbors {
   655  			nodes, ok := edges[n.ID]
   656  			if !ok {
   657  				return nil, fmt.Errorf(`unexpected "roles" node returned %v`, n.ID)
   658  			}
   659  			for i := range nodes {
   660  				nodes[i].Edges.Roles = append(nodes[i].Edges.Roles, n)
   661  			}
   662  		}
   663  	}
   664  
   665  	if query := uq.withAvatarImage; query != nil {
   666  		ids := make([]int, 0, len(nodes))
   667  		nodeids := make(map[int][]*User)
   668  		for i := range nodes {
   669  			fk := nodes[i].AvatarImageID
   670  			if _, ok := nodeids[fk]; !ok {
   671  				ids = append(ids, fk)
   672  			}
   673  			nodeids[fk] = append(nodeids[fk], nodes[i])
   674  		}
   675  		query.Where(file.IDIn(ids...))
   676  		neighbors, err := query.All(ctx)
   677  		if err != nil {
   678  			return nil, err
   679  		}
   680  		for _, n := range neighbors {
   681  			nodes, ok := nodeids[n.ID]
   682  			if !ok {
   683  				return nil, fmt.Errorf(`unexpected foreign-key "avatar_image_id" returned %v`, n.ID)
   684  			}
   685  			for i := range nodes {
   686  				nodes[i].Edges.AvatarImage = n
   687  			}
   688  		}
   689  	}
   690  
   691  	return nodes, nil
   692  }
   693  
   694  func (uq *UserQuery) sqlCount(ctx context.Context) (int, error) {
   695  	_spec := uq.querySpec()
   696  	_spec.Node.Columns = uq.fields
   697  	if len(uq.fields) > 0 {
   698  		_spec.Unique = uq.unique != nil && *uq.unique
   699  	}
   700  	return sqlgraph.CountNodes(ctx, uq.driver, _spec)
   701  }
   702  
   703  func (uq *UserQuery) sqlExist(ctx context.Context) (bool, error) {
   704  	n, err := uq.sqlCount(ctx)
   705  	if err != nil {
   706  		return false, fmt.Errorf("ent: check existence: %w", err)
   707  	}
   708  	return n > 0, nil
   709  }
   710  
   711  func (uq *UserQuery) querySpec() *sqlgraph.QuerySpec {
   712  	_spec := &sqlgraph.QuerySpec{
   713  		Node: &sqlgraph.NodeSpec{
   714  			Table:   user.Table,
   715  			Columns: user.Columns,
   716  			ID: &sqlgraph.FieldSpec{
   717  				Type:   field.TypeInt,
   718  				Column: user.FieldID,
   719  			},
   720  		},
   721  		From:   uq.sql,
   722  		Unique: true,
   723  	}
   724  	if unique := uq.unique; unique != nil {
   725  		_spec.Unique = *unique
   726  	}
   727  	if fields := uq.fields; len(fields) > 0 {
   728  		_spec.Node.Columns = make([]string, 0, len(fields))
   729  		_spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
   730  		for i := range fields {
   731  			if fields[i] != user.FieldID {
   732  				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
   733  			}
   734  		}
   735  	}
   736  	if ps := uq.predicates; len(ps) > 0 {
   737  		_spec.Predicate = func(selector *sql.Selector) {
   738  			for i := range ps {
   739  				ps[i](selector)
   740  			}
   741  		}
   742  	}
   743  	if limit := uq.limit; limit != nil {
   744  		_spec.Limit = *limit
   745  	}
   746  	if offset := uq.offset; offset != nil {
   747  		_spec.Offset = *offset
   748  	}
   749  	if ps := uq.order; len(ps) > 0 {
   750  		_spec.Order = func(selector *sql.Selector) {
   751  			for i := range ps {
   752  				ps[i](selector)
   753  			}
   754  		}
   755  	}
   756  	return _spec
   757  }
   758  
   759  func (uq *UserQuery) sqlQuery(ctx context.Context) *sql.Selector {
   760  	builder := sql.Dialect(uq.driver.Dialect())
   761  	t1 := builder.Table(user.Table)
   762  	columns := uq.fields
   763  	if len(columns) == 0 {
   764  		columns = user.Columns
   765  	}
   766  	selector := builder.Select(t1.Columns(columns...)...).From(t1)
   767  	if uq.sql != nil {
   768  		selector = uq.sql
   769  		selector.Select(selector.Columns(columns...)...)
   770  	}
   771  	if uq.unique != nil && *uq.unique {
   772  		selector.Distinct()
   773  	}
   774  	for _, p := range uq.predicates {
   775  		p(selector)
   776  	}
   777  	for _, p := range uq.order {
   778  		p(selector)
   779  	}
   780  	if offset := uq.offset; offset != nil {
   781  		// limit is mandatory for offset clause. We start
   782  		// with default value, and override it below if needed.
   783  		selector.Offset(*offset).Limit(math.MaxInt32)
   784  	}
   785  	if limit := uq.limit; limit != nil {
   786  		selector.Limit(*limit)
   787  	}
   788  	return selector
   789  }
   790  
   791  // UserGroupBy is the group-by builder for User entities.
   792  type UserGroupBy struct {
   793  	config
   794  	fields []string
   795  	fns    []AggregateFunc
   796  	// intermediate query (i.e. traversal path).
   797  	sql  *sql.Selector
   798  	path func(context.Context) (*sql.Selector, error)
   799  }
   800  
   801  // Aggregate adds the given aggregation functions to the group-by query.
   802  func (ugb *UserGroupBy) Aggregate(fns ...AggregateFunc) *UserGroupBy {
   803  	ugb.fns = append(ugb.fns, fns...)
   804  	return ugb
   805  }
   806  
   807  // Scan applies the group-by query and scans the result into the given value.
   808  func (ugb *UserGroupBy) Scan(ctx context.Context, v interface{}) error {
   809  	query, err := ugb.path(ctx)
   810  	if err != nil {
   811  		return err
   812  	}
   813  	ugb.sql = query
   814  	return ugb.sqlScan(ctx, v)
   815  }
   816  
   817  // ScanX is like Scan, but panics if an error occurs.
   818  func (ugb *UserGroupBy) ScanX(ctx context.Context, v interface{}) {
   819  	if err := ugb.Scan(ctx, v); err != nil {
   820  		panic(err)
   821  	}
   822  }
   823  
   824  // Strings returns list of strings from group-by.
   825  // It is only allowed when executing a group-by query with one field.
   826  func (ugb *UserGroupBy) Strings(ctx context.Context) ([]string, error) {
   827  	if len(ugb.fields) > 1 {
   828  		return nil, errors.New("ent: UserGroupBy.Strings is not achievable when grouping more than 1 field")
   829  	}
   830  	var v []string
   831  	if err := ugb.Scan(ctx, &v); err != nil {
   832  		return nil, err
   833  	}
   834  	return v, nil
   835  }
   836  
   837  // StringsX is like Strings, but panics if an error occurs.
   838  func (ugb *UserGroupBy) StringsX(ctx context.Context) []string {
   839  	v, err := ugb.Strings(ctx)
   840  	if err != nil {
   841  		panic(err)
   842  	}
   843  	return v
   844  }
   845  
   846  // String returns a single string from a group-by query.
   847  // It is only allowed when executing a group-by query with one field.
   848  func (ugb *UserGroupBy) String(ctx context.Context) (_ string, err error) {
   849  	var v []string
   850  	if v, err = ugb.Strings(ctx); err != nil {
   851  		return
   852  	}
   853  	switch len(v) {
   854  	case 1:
   855  		return v[0], nil
   856  	case 0:
   857  		err = &NotFoundError{user.Label}
   858  	default:
   859  		err = fmt.Errorf("ent: UserGroupBy.Strings returned %d results when one was expected", len(v))
   860  	}
   861  	return
   862  }
   863  
   864  // StringX is like String, but panics if an error occurs.
   865  func (ugb *UserGroupBy) StringX(ctx context.Context) string {
   866  	v, err := ugb.String(ctx)
   867  	if err != nil {
   868  		panic(err)
   869  	}
   870  	return v
   871  }
   872  
   873  // Ints returns list of ints from group-by.
   874  // It is only allowed when executing a group-by query with one field.
   875  func (ugb *UserGroupBy) Ints(ctx context.Context) ([]int, error) {
   876  	if len(ugb.fields) > 1 {
   877  		return nil, errors.New("ent: UserGroupBy.Ints is not achievable when grouping more than 1 field")
   878  	}
   879  	var v []int
   880  	if err := ugb.Scan(ctx, &v); err != nil {
   881  		return nil, err
   882  	}
   883  	return v, nil
   884  }
   885  
   886  // IntsX is like Ints, but panics if an error occurs.
   887  func (ugb *UserGroupBy) IntsX(ctx context.Context) []int {
   888  	v, err := ugb.Ints(ctx)
   889  	if err != nil {
   890  		panic(err)
   891  	}
   892  	return v
   893  }
   894  
   895  // Int returns a single int from a group-by query.
   896  // It is only allowed when executing a group-by query with one field.
   897  func (ugb *UserGroupBy) Int(ctx context.Context) (_ int, err error) {
   898  	var v []int
   899  	if v, err = ugb.Ints(ctx); err != nil {
   900  		return
   901  	}
   902  	switch len(v) {
   903  	case 1:
   904  		return v[0], nil
   905  	case 0:
   906  		err = &NotFoundError{user.Label}
   907  	default:
   908  		err = fmt.Errorf("ent: UserGroupBy.Ints returned %d results when one was expected", len(v))
   909  	}
   910  	return
   911  }
   912  
   913  // IntX is like Int, but panics if an error occurs.
   914  func (ugb *UserGroupBy) IntX(ctx context.Context) int {
   915  	v, err := ugb.Int(ctx)
   916  	if err != nil {
   917  		panic(err)
   918  	}
   919  	return v
   920  }
   921  
   922  // Float64s returns list of float64s from group-by.
   923  // It is only allowed when executing a group-by query with one field.
   924  func (ugb *UserGroupBy) Float64s(ctx context.Context) ([]float64, error) {
   925  	if len(ugb.fields) > 1 {
   926  		return nil, errors.New("ent: UserGroupBy.Float64s is not achievable when grouping more than 1 field")
   927  	}
   928  	var v []float64
   929  	if err := ugb.Scan(ctx, &v); err != nil {
   930  		return nil, err
   931  	}
   932  	return v, nil
   933  }
   934  
   935  // Float64sX is like Float64s, but panics if an error occurs.
   936  func (ugb *UserGroupBy) Float64sX(ctx context.Context) []float64 {
   937  	v, err := ugb.Float64s(ctx)
   938  	if err != nil {
   939  		panic(err)
   940  	}
   941  	return v
   942  }
   943  
   944  // Float64 returns a single float64 from a group-by query.
   945  // It is only allowed when executing a group-by query with one field.
   946  func (ugb *UserGroupBy) Float64(ctx context.Context) (_ float64, err error) {
   947  	var v []float64
   948  	if v, err = ugb.Float64s(ctx); err != nil {
   949  		return
   950  	}
   951  	switch len(v) {
   952  	case 1:
   953  		return v[0], nil
   954  	case 0:
   955  		err = &NotFoundError{user.Label}
   956  	default:
   957  		err = fmt.Errorf("ent: UserGroupBy.Float64s returned %d results when one was expected", len(v))
   958  	}
   959  	return
   960  }
   961  
   962  // Float64X is like Float64, but panics if an error occurs.
   963  func (ugb *UserGroupBy) Float64X(ctx context.Context) float64 {
   964  	v, err := ugb.Float64(ctx)
   965  	if err != nil {
   966  		panic(err)
   967  	}
   968  	return v
   969  }
   970  
   971  // Bools returns list of bools from group-by.
   972  // It is only allowed when executing a group-by query with one field.
   973  func (ugb *UserGroupBy) Bools(ctx context.Context) ([]bool, error) {
   974  	if len(ugb.fields) > 1 {
   975  		return nil, errors.New("ent: UserGroupBy.Bools is not achievable when grouping more than 1 field")
   976  	}
   977  	var v []bool
   978  	if err := ugb.Scan(ctx, &v); err != nil {
   979  		return nil, err
   980  	}
   981  	return v, nil
   982  }
   983  
   984  // BoolsX is like Bools, but panics if an error occurs.
   985  func (ugb *UserGroupBy) BoolsX(ctx context.Context) []bool {
   986  	v, err := ugb.Bools(ctx)
   987  	if err != nil {
   988  		panic(err)
   989  	}
   990  	return v
   991  }
   992  
   993  // Bool returns a single bool from a group-by query.
   994  // It is only allowed when executing a group-by query with one field.
   995  func (ugb *UserGroupBy) Bool(ctx context.Context) (_ bool, err error) {
   996  	var v []bool
   997  	if v, err = ugb.Bools(ctx); err != nil {
   998  		return
   999  	}
  1000  	switch len(v) {
  1001  	case 1:
  1002  		return v[0], nil
  1003  	case 0:
  1004  		err = &NotFoundError{user.Label}
  1005  	default:
  1006  		err = fmt.Errorf("ent: UserGroupBy.Bools returned %d results when one was expected", len(v))
  1007  	}
  1008  	return
  1009  }
  1010  
  1011  // BoolX is like Bool, but panics if an error occurs.
  1012  func (ugb *UserGroupBy) BoolX(ctx context.Context) bool {
  1013  	v, err := ugb.Bool(ctx)
  1014  	if err != nil {
  1015  		panic(err)
  1016  	}
  1017  	return v
  1018  }
  1019  
  1020  func (ugb *UserGroupBy) sqlScan(ctx context.Context, v interface{}) error {
  1021  	for _, f := range ugb.fields {
  1022  		if !user.ValidColumn(f) {
  1023  			return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
  1024  		}
  1025  	}
  1026  	selector := ugb.sqlQuery()
  1027  	if err := selector.Err(); err != nil {
  1028  		return err
  1029  	}
  1030  	rows := &sql.Rows{}
  1031  	query, args := selector.Query()
  1032  	if err := ugb.driver.Query(ctx, query, args, rows); err != nil {
  1033  		return err
  1034  	}
  1035  	defer rows.Close()
  1036  	return sql.ScanSlice(rows, v)
  1037  }
  1038  
  1039  func (ugb *UserGroupBy) sqlQuery() *sql.Selector {
  1040  	selector := ugb.sql.Select()
  1041  	aggregation := make([]string, 0, len(ugb.fns))
  1042  	for _, fn := range ugb.fns {
  1043  		aggregation = append(aggregation, fn(selector))
  1044  	}
  1045  	// If no columns were selected in a custom aggregation function, the default
  1046  	// selection is the fields used for "group-by", and the aggregation functions.
  1047  	if len(selector.SelectedColumns()) == 0 {
  1048  		columns := make([]string, 0, len(ugb.fields)+len(ugb.fns))
  1049  		for _, f := range ugb.fields {
  1050  			columns = append(columns, selector.C(f))
  1051  		}
  1052  		columns = append(columns, aggregation...)
  1053  		selector.Select(columns...)
  1054  	}
  1055  	return selector.GroupBy(selector.Columns(ugb.fields...)...)
  1056  }
  1057  
  1058  // UserSelect is the builder for selecting fields of User entities.
  1059  type UserSelect struct {
  1060  	*UserQuery
  1061  	// intermediate query (i.e. traversal path).
  1062  	sql *sql.Selector
  1063  }
  1064  
  1065  // Scan applies the selector query and scans the result into the given value.
  1066  func (us *UserSelect) Scan(ctx context.Context, v interface{}) error {
  1067  	if err := us.prepareQuery(ctx); err != nil {
  1068  		return err
  1069  	}
  1070  	us.sql = us.UserQuery.sqlQuery(ctx)
  1071  	return us.sqlScan(ctx, v)
  1072  }
  1073  
  1074  // ScanX is like Scan, but panics if an error occurs.
  1075  func (us *UserSelect) ScanX(ctx context.Context, v interface{}) {
  1076  	if err := us.Scan(ctx, v); err != nil {
  1077  		panic(err)
  1078  	}
  1079  }
  1080  
  1081  // Strings returns list of strings from a selector. It is only allowed when selecting one field.
  1082  func (us *UserSelect) Strings(ctx context.Context) ([]string, error) {
  1083  	if len(us.fields) > 1 {
  1084  		return nil, errors.New("ent: UserSelect.Strings is not achievable when selecting more than 1 field")
  1085  	}
  1086  	var v []string
  1087  	if err := us.Scan(ctx, &v); err != nil {
  1088  		return nil, err
  1089  	}
  1090  	return v, nil
  1091  }
  1092  
  1093  // StringsX is like Strings, but panics if an error occurs.
  1094  func (us *UserSelect) StringsX(ctx context.Context) []string {
  1095  	v, err := us.Strings(ctx)
  1096  	if err != nil {
  1097  		panic(err)
  1098  	}
  1099  	return v
  1100  }
  1101  
  1102  // String returns a single string from a selector. It is only allowed when selecting one field.
  1103  func (us *UserSelect) String(ctx context.Context) (_ string, err error) {
  1104  	var v []string
  1105  	if v, err = us.Strings(ctx); err != nil {
  1106  		return
  1107  	}
  1108  	switch len(v) {
  1109  	case 1:
  1110  		return v[0], nil
  1111  	case 0:
  1112  		err = &NotFoundError{user.Label}
  1113  	default:
  1114  		err = fmt.Errorf("ent: UserSelect.Strings returned %d results when one was expected", len(v))
  1115  	}
  1116  	return
  1117  }
  1118  
  1119  // StringX is like String, but panics if an error occurs.
  1120  func (us *UserSelect) StringX(ctx context.Context) string {
  1121  	v, err := us.String(ctx)
  1122  	if err != nil {
  1123  		panic(err)
  1124  	}
  1125  	return v
  1126  }
  1127  
  1128  // Ints returns list of ints from a selector. It is only allowed when selecting one field.
  1129  func (us *UserSelect) Ints(ctx context.Context) ([]int, error) {
  1130  	if len(us.fields) > 1 {
  1131  		return nil, errors.New("ent: UserSelect.Ints is not achievable when selecting more than 1 field")
  1132  	}
  1133  	var v []int
  1134  	if err := us.Scan(ctx, &v); err != nil {
  1135  		return nil, err
  1136  	}
  1137  	return v, nil
  1138  }
  1139  
  1140  // IntsX is like Ints, but panics if an error occurs.
  1141  func (us *UserSelect) IntsX(ctx context.Context) []int {
  1142  	v, err := us.Ints(ctx)
  1143  	if err != nil {
  1144  		panic(err)
  1145  	}
  1146  	return v
  1147  }
  1148  
  1149  // Int returns a single int from a selector. It is only allowed when selecting one field.
  1150  func (us *UserSelect) Int(ctx context.Context) (_ int, err error) {
  1151  	var v []int
  1152  	if v, err = us.Ints(ctx); err != nil {
  1153  		return
  1154  	}
  1155  	switch len(v) {
  1156  	case 1:
  1157  		return v[0], nil
  1158  	case 0:
  1159  		err = &NotFoundError{user.Label}
  1160  	default:
  1161  		err = fmt.Errorf("ent: UserSelect.Ints returned %d results when one was expected", len(v))
  1162  	}
  1163  	return
  1164  }
  1165  
  1166  // IntX is like Int, but panics if an error occurs.
  1167  func (us *UserSelect) IntX(ctx context.Context) int {
  1168  	v, err := us.Int(ctx)
  1169  	if err != nil {
  1170  		panic(err)
  1171  	}
  1172  	return v
  1173  }
  1174  
  1175  // Float64s returns list of float64s from a selector. It is only allowed when selecting one field.
  1176  func (us *UserSelect) Float64s(ctx context.Context) ([]float64, error) {
  1177  	if len(us.fields) > 1 {
  1178  		return nil, errors.New("ent: UserSelect.Float64s is not achievable when selecting more than 1 field")
  1179  	}
  1180  	var v []float64
  1181  	if err := us.Scan(ctx, &v); err != nil {
  1182  		return nil, err
  1183  	}
  1184  	return v, nil
  1185  }
  1186  
  1187  // Float64sX is like Float64s, but panics if an error occurs.
  1188  func (us *UserSelect) Float64sX(ctx context.Context) []float64 {
  1189  	v, err := us.Float64s(ctx)
  1190  	if err != nil {
  1191  		panic(err)
  1192  	}
  1193  	return v
  1194  }
  1195  
  1196  // Float64 returns a single float64 from a selector. It is only allowed when selecting one field.
  1197  func (us *UserSelect) Float64(ctx context.Context) (_ float64, err error) {
  1198  	var v []float64
  1199  	if v, err = us.Float64s(ctx); err != nil {
  1200  		return
  1201  	}
  1202  	switch len(v) {
  1203  	case 1:
  1204  		return v[0], nil
  1205  	case 0:
  1206  		err = &NotFoundError{user.Label}
  1207  	default:
  1208  		err = fmt.Errorf("ent: UserSelect.Float64s returned %d results when one was expected", len(v))
  1209  	}
  1210  	return
  1211  }
  1212  
  1213  // Float64X is like Float64, but panics if an error occurs.
  1214  func (us *UserSelect) Float64X(ctx context.Context) float64 {
  1215  	v, err := us.Float64(ctx)
  1216  	if err != nil {
  1217  		panic(err)
  1218  	}
  1219  	return v
  1220  }
  1221  
  1222  // Bools returns list of bools from a selector. It is only allowed when selecting one field.
  1223  func (us *UserSelect) Bools(ctx context.Context) ([]bool, error) {
  1224  	if len(us.fields) > 1 {
  1225  		return nil, errors.New("ent: UserSelect.Bools is not achievable when selecting more than 1 field")
  1226  	}
  1227  	var v []bool
  1228  	if err := us.Scan(ctx, &v); err != nil {
  1229  		return nil, err
  1230  	}
  1231  	return v, nil
  1232  }
  1233  
  1234  // BoolsX is like Bools, but panics if an error occurs.
  1235  func (us *UserSelect) BoolsX(ctx context.Context) []bool {
  1236  	v, err := us.Bools(ctx)
  1237  	if err != nil {
  1238  		panic(err)
  1239  	}
  1240  	return v
  1241  }
  1242  
  1243  // Bool returns a single bool from a selector. It is only allowed when selecting one field.
  1244  func (us *UserSelect) Bool(ctx context.Context) (_ bool, err error) {
  1245  	var v []bool
  1246  	if v, err = us.Bools(ctx); err != nil {
  1247  		return
  1248  	}
  1249  	switch len(v) {
  1250  	case 1:
  1251  		return v[0], nil
  1252  	case 0:
  1253  		err = &NotFoundError{user.Label}
  1254  	default:
  1255  		err = fmt.Errorf("ent: UserSelect.Bools returned %d results when one was expected", len(v))
  1256  	}
  1257  	return
  1258  }
  1259  
  1260  // BoolX is like Bool, but panics if an error occurs.
  1261  func (us *UserSelect) BoolX(ctx context.Context) bool {
  1262  	v, err := us.Bool(ctx)
  1263  	if err != nil {
  1264  		panic(err)
  1265  	}
  1266  	return v
  1267  }
  1268  
  1269  func (us *UserSelect) sqlScan(ctx context.Context, v interface{}) error {
  1270  	rows := &sql.Rows{}
  1271  	query, args := us.sql.Query()
  1272  	if err := us.driver.Query(ctx, query, args, rows); err != nil {
  1273  		return err
  1274  	}
  1275  	defer rows.Close()
  1276  	return sql.ScanSlice(rows, v)
  1277  }