github.com/iasthc/atlas/cmd/atlas@v0.0.0-20230523071841-73246df3f88d/internal/migrate/ent/revision_query.go (about)

     1  // Copyright 2021-present The Atlas Authors. All rights reserved.
     2  // This source code is licensed under the Apache 2.0 license found
     3  // in the LICENSE file in the root directory of this source tree.
     4  
     5  // Code generated by entc, DO NOT EDIT.
     6  
     7  package ent
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"math"
    13  
    14  	"github.com/iasthc/atlas/cmd/atlas/internal/migrate/ent/internal"
    15  	"github.com/iasthc/atlas/cmd/atlas/internal/migrate/ent/predicate"
    16  	"github.com/iasthc/atlas/cmd/atlas/internal/migrate/ent/revision"
    17  	"entgo.io/ent/dialect/sql"
    18  	"entgo.io/ent/dialect/sql/sqlgraph"
    19  	"entgo.io/ent/schema/field"
    20  )
    21  
    22  // RevisionQuery is the builder for querying Revision entities.
    23  type RevisionQuery struct {
    24  	config
    25  	ctx        *QueryContext
    26  	order      []revision.OrderOption
    27  	inters     []Interceptor
    28  	predicates []predicate.Revision
    29  	// intermediate query (i.e. traversal path).
    30  	sql  *sql.Selector
    31  	path func(context.Context) (*sql.Selector, error)
    32  }
    33  
    34  // Where adds a new predicate for the RevisionQuery builder.
    35  func (rq *RevisionQuery) Where(ps ...predicate.Revision) *RevisionQuery {
    36  	rq.predicates = append(rq.predicates, ps...)
    37  	return rq
    38  }
    39  
    40  // Limit the number of records to be returned by this query.
    41  func (rq *RevisionQuery) Limit(limit int) *RevisionQuery {
    42  	rq.ctx.Limit = &limit
    43  	return rq
    44  }
    45  
    46  // Offset to start from.
    47  func (rq *RevisionQuery) Offset(offset int) *RevisionQuery {
    48  	rq.ctx.Offset = &offset
    49  	return rq
    50  }
    51  
    52  // Unique configures the query builder to filter duplicate records on query.
    53  // By default, unique is set to true, and can be disabled using this method.
    54  func (rq *RevisionQuery) Unique(unique bool) *RevisionQuery {
    55  	rq.ctx.Unique = &unique
    56  	return rq
    57  }
    58  
    59  // Order specifies how the records should be ordered.
    60  func (rq *RevisionQuery) Order(o ...revision.OrderOption) *RevisionQuery {
    61  	rq.order = append(rq.order, o...)
    62  	return rq
    63  }
    64  
    65  // First returns the first Revision entity from the query.
    66  // Returns a *NotFoundError when no Revision was found.
    67  func (rq *RevisionQuery) First(ctx context.Context) (*Revision, error) {
    68  	nodes, err := rq.Limit(1).All(setContextOp(ctx, rq.ctx, "First"))
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  	if len(nodes) == 0 {
    73  		return nil, &NotFoundError{revision.Label}
    74  	}
    75  	return nodes[0], nil
    76  }
    77  
    78  // FirstX is like First, but panics if an error occurs.
    79  func (rq *RevisionQuery) FirstX(ctx context.Context) *Revision {
    80  	node, err := rq.First(ctx)
    81  	if err != nil && !IsNotFound(err) {
    82  		panic(err)
    83  	}
    84  	return node
    85  }
    86  
    87  // FirstID returns the first Revision ID from the query.
    88  // Returns a *NotFoundError when no Revision ID was found.
    89  func (rq *RevisionQuery) FirstID(ctx context.Context) (id string, err error) {
    90  	var ids []string
    91  	if ids, err = rq.Limit(1).IDs(setContextOp(ctx, rq.ctx, "FirstID")); err != nil {
    92  		return
    93  	}
    94  	if len(ids) == 0 {
    95  		err = &NotFoundError{revision.Label}
    96  		return
    97  	}
    98  	return ids[0], nil
    99  }
   100  
   101  // FirstIDX is like FirstID, but panics if an error occurs.
   102  func (rq *RevisionQuery) FirstIDX(ctx context.Context) string {
   103  	id, err := rq.FirstID(ctx)
   104  	if err != nil && !IsNotFound(err) {
   105  		panic(err)
   106  	}
   107  	return id
   108  }
   109  
   110  // Only returns a single Revision entity found by the query, ensuring it only returns one.
   111  // Returns a *NotSingularError when more than one Revision entity is found.
   112  // Returns a *NotFoundError when no Revision entities are found.
   113  func (rq *RevisionQuery) Only(ctx context.Context) (*Revision, error) {
   114  	nodes, err := rq.Limit(2).All(setContextOp(ctx, rq.ctx, "Only"))
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  	switch len(nodes) {
   119  	case 1:
   120  		return nodes[0], nil
   121  	case 0:
   122  		return nil, &NotFoundError{revision.Label}
   123  	default:
   124  		return nil, &NotSingularError{revision.Label}
   125  	}
   126  }
   127  
   128  // OnlyX is like Only, but panics if an error occurs.
   129  func (rq *RevisionQuery) OnlyX(ctx context.Context) *Revision {
   130  	node, err := rq.Only(ctx)
   131  	if err != nil {
   132  		panic(err)
   133  	}
   134  	return node
   135  }
   136  
   137  // OnlyID is like Only, but returns the only Revision ID in the query.
   138  // Returns a *NotSingularError when more than one Revision ID is found.
   139  // Returns a *NotFoundError when no entities are found.
   140  func (rq *RevisionQuery) OnlyID(ctx context.Context) (id string, err error) {
   141  	var ids []string
   142  	if ids, err = rq.Limit(2).IDs(setContextOp(ctx, rq.ctx, "OnlyID")); err != nil {
   143  		return
   144  	}
   145  	switch len(ids) {
   146  	case 1:
   147  		id = ids[0]
   148  	case 0:
   149  		err = &NotFoundError{revision.Label}
   150  	default:
   151  		err = &NotSingularError{revision.Label}
   152  	}
   153  	return
   154  }
   155  
   156  // OnlyIDX is like OnlyID, but panics if an error occurs.
   157  func (rq *RevisionQuery) OnlyIDX(ctx context.Context) string {
   158  	id, err := rq.OnlyID(ctx)
   159  	if err != nil {
   160  		panic(err)
   161  	}
   162  	return id
   163  }
   164  
   165  // All executes the query and returns a list of Revisions.
   166  func (rq *RevisionQuery) All(ctx context.Context) ([]*Revision, error) {
   167  	ctx = setContextOp(ctx, rq.ctx, "All")
   168  	if err := rq.prepareQuery(ctx); err != nil {
   169  		return nil, err
   170  	}
   171  	qr := querierAll[[]*Revision, *RevisionQuery]()
   172  	return withInterceptors[[]*Revision](ctx, rq, qr, rq.inters)
   173  }
   174  
   175  // AllX is like All, but panics if an error occurs.
   176  func (rq *RevisionQuery) AllX(ctx context.Context) []*Revision {
   177  	nodes, err := rq.All(ctx)
   178  	if err != nil {
   179  		panic(err)
   180  	}
   181  	return nodes
   182  }
   183  
   184  // IDs executes the query and returns a list of Revision IDs.
   185  func (rq *RevisionQuery) IDs(ctx context.Context) (ids []string, err error) {
   186  	if rq.ctx.Unique == nil && rq.path != nil {
   187  		rq.Unique(true)
   188  	}
   189  	ctx = setContextOp(ctx, rq.ctx, "IDs")
   190  	if err = rq.Select(revision.FieldID).Scan(ctx, &ids); err != nil {
   191  		return nil, err
   192  	}
   193  	return ids, nil
   194  }
   195  
   196  // IDsX is like IDs, but panics if an error occurs.
   197  func (rq *RevisionQuery) IDsX(ctx context.Context) []string {
   198  	ids, err := rq.IDs(ctx)
   199  	if err != nil {
   200  		panic(err)
   201  	}
   202  	return ids
   203  }
   204  
   205  // Count returns the count of the given query.
   206  func (rq *RevisionQuery) Count(ctx context.Context) (int, error) {
   207  	ctx = setContextOp(ctx, rq.ctx, "Count")
   208  	if err := rq.prepareQuery(ctx); err != nil {
   209  		return 0, err
   210  	}
   211  	return withInterceptors[int](ctx, rq, querierCount[*RevisionQuery](), rq.inters)
   212  }
   213  
   214  // CountX is like Count, but panics if an error occurs.
   215  func (rq *RevisionQuery) CountX(ctx context.Context) int {
   216  	count, err := rq.Count(ctx)
   217  	if err != nil {
   218  		panic(err)
   219  	}
   220  	return count
   221  }
   222  
   223  // Exist returns true if the query has elements in the graph.
   224  func (rq *RevisionQuery) Exist(ctx context.Context) (bool, error) {
   225  	ctx = setContextOp(ctx, rq.ctx, "Exist")
   226  	switch _, err := rq.FirstID(ctx); {
   227  	case IsNotFound(err):
   228  		return false, nil
   229  	case err != nil:
   230  		return false, fmt.Errorf("ent: check existence: %w", err)
   231  	default:
   232  		return true, nil
   233  	}
   234  }
   235  
   236  // ExistX is like Exist, but panics if an error occurs.
   237  func (rq *RevisionQuery) ExistX(ctx context.Context) bool {
   238  	exist, err := rq.Exist(ctx)
   239  	if err != nil {
   240  		panic(err)
   241  	}
   242  	return exist
   243  }
   244  
   245  // Clone returns a duplicate of the RevisionQuery builder, including all associated steps. It can be
   246  // used to prepare common query builders and use them differently after the clone is made.
   247  func (rq *RevisionQuery) Clone() *RevisionQuery {
   248  	if rq == nil {
   249  		return nil
   250  	}
   251  	return &RevisionQuery{
   252  		config:     rq.config,
   253  		ctx:        rq.ctx.Clone(),
   254  		order:      append([]revision.OrderOption{}, rq.order...),
   255  		inters:     append([]Interceptor{}, rq.inters...),
   256  		predicates: append([]predicate.Revision{}, rq.predicates...),
   257  		// clone intermediate query.
   258  		sql:  rq.sql.Clone(),
   259  		path: rq.path,
   260  	}
   261  }
   262  
   263  // GroupBy is used to group vertices by one or more fields/columns.
   264  // It is often used with aggregate functions, like: count, max, mean, min, sum.
   265  //
   266  // Example:
   267  //
   268  //	var v []struct {
   269  //		Description string `json:"description,omitempty"`
   270  //		Count int `json:"count,omitempty"`
   271  //	}
   272  //
   273  //	client.Revision.Query().
   274  //		GroupBy(revision.FieldDescription).
   275  //		Aggregate(ent.Count()).
   276  //		Scan(ctx, &v)
   277  func (rq *RevisionQuery) GroupBy(field string, fields ...string) *RevisionGroupBy {
   278  	rq.ctx.Fields = append([]string{field}, fields...)
   279  	grbuild := &RevisionGroupBy{build: rq}
   280  	grbuild.flds = &rq.ctx.Fields
   281  	grbuild.label = revision.Label
   282  	grbuild.scan = grbuild.Scan
   283  	return grbuild
   284  }
   285  
   286  // Select allows the selection one or more fields/columns for the given query,
   287  // instead of selecting all fields in the entity.
   288  //
   289  // Example:
   290  //
   291  //	var v []struct {
   292  //		Description string `json:"description,omitempty"`
   293  //	}
   294  //
   295  //	client.Revision.Query().
   296  //		Select(revision.FieldDescription).
   297  //		Scan(ctx, &v)
   298  func (rq *RevisionQuery) Select(fields ...string) *RevisionSelect {
   299  	rq.ctx.Fields = append(rq.ctx.Fields, fields...)
   300  	sbuild := &RevisionSelect{RevisionQuery: rq}
   301  	sbuild.label = revision.Label
   302  	sbuild.flds, sbuild.scan = &rq.ctx.Fields, sbuild.Scan
   303  	return sbuild
   304  }
   305  
   306  // Aggregate returns a RevisionSelect configured with the given aggregations.
   307  func (rq *RevisionQuery) Aggregate(fns ...AggregateFunc) *RevisionSelect {
   308  	return rq.Select().Aggregate(fns...)
   309  }
   310  
   311  func (rq *RevisionQuery) prepareQuery(ctx context.Context) error {
   312  	for _, inter := range rq.inters {
   313  		if inter == nil {
   314  			return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
   315  		}
   316  		if trv, ok := inter.(Traverser); ok {
   317  			if err := trv.Traverse(ctx, rq); err != nil {
   318  				return err
   319  			}
   320  		}
   321  	}
   322  	for _, f := range rq.ctx.Fields {
   323  		if !revision.ValidColumn(f) {
   324  			return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
   325  		}
   326  	}
   327  	if rq.path != nil {
   328  		prev, err := rq.path(ctx)
   329  		if err != nil {
   330  			return err
   331  		}
   332  		rq.sql = prev
   333  	}
   334  	return nil
   335  }
   336  
   337  func (rq *RevisionQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Revision, error) {
   338  	var (
   339  		nodes = []*Revision{}
   340  		_spec = rq.querySpec()
   341  	)
   342  	_spec.ScanValues = func(columns []string) ([]any, error) {
   343  		return (*Revision).scanValues(nil, columns)
   344  	}
   345  	_spec.Assign = func(columns []string, values []any) error {
   346  		node := &Revision{config: rq.config}
   347  		nodes = append(nodes, node)
   348  		return node.assignValues(columns, values)
   349  	}
   350  	_spec.Node.Schema = rq.schemaConfig.Revision
   351  	ctx = internal.NewSchemaConfigContext(ctx, rq.schemaConfig)
   352  	for i := range hooks {
   353  		hooks[i](ctx, _spec)
   354  	}
   355  	if err := sqlgraph.QueryNodes(ctx, rq.driver, _spec); err != nil {
   356  		return nil, err
   357  	}
   358  	if len(nodes) == 0 {
   359  		return nodes, nil
   360  	}
   361  	return nodes, nil
   362  }
   363  
   364  func (rq *RevisionQuery) sqlCount(ctx context.Context) (int, error) {
   365  	_spec := rq.querySpec()
   366  	_spec.Node.Schema = rq.schemaConfig.Revision
   367  	ctx = internal.NewSchemaConfigContext(ctx, rq.schemaConfig)
   368  	_spec.Node.Columns = rq.ctx.Fields
   369  	if len(rq.ctx.Fields) > 0 {
   370  		_spec.Unique = rq.ctx.Unique != nil && *rq.ctx.Unique
   371  	}
   372  	return sqlgraph.CountNodes(ctx, rq.driver, _spec)
   373  }
   374  
   375  func (rq *RevisionQuery) querySpec() *sqlgraph.QuerySpec {
   376  	_spec := sqlgraph.NewQuerySpec(revision.Table, revision.Columns, sqlgraph.NewFieldSpec(revision.FieldID, field.TypeString))
   377  	_spec.From = rq.sql
   378  	if unique := rq.ctx.Unique; unique != nil {
   379  		_spec.Unique = *unique
   380  	} else if rq.path != nil {
   381  		_spec.Unique = true
   382  	}
   383  	if fields := rq.ctx.Fields; len(fields) > 0 {
   384  		_spec.Node.Columns = make([]string, 0, len(fields))
   385  		_spec.Node.Columns = append(_spec.Node.Columns, revision.FieldID)
   386  		for i := range fields {
   387  			if fields[i] != revision.FieldID {
   388  				_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
   389  			}
   390  		}
   391  	}
   392  	if ps := rq.predicates; len(ps) > 0 {
   393  		_spec.Predicate = func(selector *sql.Selector) {
   394  			for i := range ps {
   395  				ps[i](selector)
   396  			}
   397  		}
   398  	}
   399  	if limit := rq.ctx.Limit; limit != nil {
   400  		_spec.Limit = *limit
   401  	}
   402  	if offset := rq.ctx.Offset; offset != nil {
   403  		_spec.Offset = *offset
   404  	}
   405  	if ps := rq.order; len(ps) > 0 {
   406  		_spec.Order = func(selector *sql.Selector) {
   407  			for i := range ps {
   408  				ps[i](selector)
   409  			}
   410  		}
   411  	}
   412  	return _spec
   413  }
   414  
   415  func (rq *RevisionQuery) sqlQuery(ctx context.Context) *sql.Selector {
   416  	builder := sql.Dialect(rq.driver.Dialect())
   417  	t1 := builder.Table(revision.Table)
   418  	columns := rq.ctx.Fields
   419  	if len(columns) == 0 {
   420  		columns = revision.Columns
   421  	}
   422  	selector := builder.Select(t1.Columns(columns...)...).From(t1)
   423  	if rq.sql != nil {
   424  		selector = rq.sql
   425  		selector.Select(selector.Columns(columns...)...)
   426  	}
   427  	if rq.ctx.Unique != nil && *rq.ctx.Unique {
   428  		selector.Distinct()
   429  	}
   430  	t1.Schema(rq.schemaConfig.Revision)
   431  	ctx = internal.NewSchemaConfigContext(ctx, rq.schemaConfig)
   432  	selector.WithContext(ctx)
   433  	for _, p := range rq.predicates {
   434  		p(selector)
   435  	}
   436  	for _, p := range rq.order {
   437  		p(selector)
   438  	}
   439  	if offset := rq.ctx.Offset; offset != nil {
   440  		// limit is mandatory for offset clause. We start
   441  		// with default value, and override it below if needed.
   442  		selector.Offset(*offset).Limit(math.MaxInt32)
   443  	}
   444  	if limit := rq.ctx.Limit; limit != nil {
   445  		selector.Limit(*limit)
   446  	}
   447  	return selector
   448  }
   449  
   450  // RevisionGroupBy is the group-by builder for Revision entities.
   451  type RevisionGroupBy struct {
   452  	selector
   453  	build *RevisionQuery
   454  }
   455  
   456  // Aggregate adds the given aggregation functions to the group-by query.
   457  func (rgb *RevisionGroupBy) Aggregate(fns ...AggregateFunc) *RevisionGroupBy {
   458  	rgb.fns = append(rgb.fns, fns...)
   459  	return rgb
   460  }
   461  
   462  // Scan applies the selector query and scans the result into the given value.
   463  func (rgb *RevisionGroupBy) Scan(ctx context.Context, v any) error {
   464  	ctx = setContextOp(ctx, rgb.build.ctx, "GroupBy")
   465  	if err := rgb.build.prepareQuery(ctx); err != nil {
   466  		return err
   467  	}
   468  	return scanWithInterceptors[*RevisionQuery, *RevisionGroupBy](ctx, rgb.build, rgb, rgb.build.inters, v)
   469  }
   470  
   471  func (rgb *RevisionGroupBy) sqlScan(ctx context.Context, root *RevisionQuery, v any) error {
   472  	selector := root.sqlQuery(ctx).Select()
   473  	aggregation := make([]string, 0, len(rgb.fns))
   474  	for _, fn := range rgb.fns {
   475  		aggregation = append(aggregation, fn(selector))
   476  	}
   477  	if len(selector.SelectedColumns()) == 0 {
   478  		columns := make([]string, 0, len(*rgb.flds)+len(rgb.fns))
   479  		for _, f := range *rgb.flds {
   480  			columns = append(columns, selector.C(f))
   481  		}
   482  		columns = append(columns, aggregation...)
   483  		selector.Select(columns...)
   484  	}
   485  	selector.GroupBy(selector.Columns(*rgb.flds...)...)
   486  	if err := selector.Err(); err != nil {
   487  		return err
   488  	}
   489  	rows := &sql.Rows{}
   490  	query, args := selector.Query()
   491  	if err := rgb.build.driver.Query(ctx, query, args, rows); err != nil {
   492  		return err
   493  	}
   494  	defer rows.Close()
   495  	return sql.ScanSlice(rows, v)
   496  }
   497  
   498  // RevisionSelect is the builder for selecting fields of Revision entities.
   499  type RevisionSelect struct {
   500  	*RevisionQuery
   501  	selector
   502  }
   503  
   504  // Aggregate adds the given aggregation functions to the selector query.
   505  func (rs *RevisionSelect) Aggregate(fns ...AggregateFunc) *RevisionSelect {
   506  	rs.fns = append(rs.fns, fns...)
   507  	return rs
   508  }
   509  
   510  // Scan applies the selector query and scans the result into the given value.
   511  func (rs *RevisionSelect) Scan(ctx context.Context, v any) error {
   512  	ctx = setContextOp(ctx, rs.ctx, "Select")
   513  	if err := rs.prepareQuery(ctx); err != nil {
   514  		return err
   515  	}
   516  	return scanWithInterceptors[*RevisionQuery, *RevisionSelect](ctx, rs.RevisionQuery, rs, rs.inters, v)
   517  }
   518  
   519  func (rs *RevisionSelect) sqlScan(ctx context.Context, root *RevisionQuery, v any) error {
   520  	selector := root.sqlQuery(ctx)
   521  	aggregation := make([]string, 0, len(rs.fns))
   522  	for _, fn := range rs.fns {
   523  		aggregation = append(aggregation, fn(selector))
   524  	}
   525  	switch n := len(*rs.selector.flds); {
   526  	case n == 0 && len(aggregation) > 0:
   527  		selector.Select(aggregation...)
   528  	case n != 0 && len(aggregation) > 0:
   529  		selector.AppendSelect(aggregation...)
   530  	}
   531  	rows := &sql.Rows{}
   532  	query, args := selector.Query()
   533  	if err := rs.driver.Query(ctx, query, args, rows); err != nil {
   534  		return err
   535  	}
   536  	defer rows.Close()
   537  	return sql.ScanSlice(rows, v)
   538  }