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