github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/configitem_query.go (about)

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