// Code generated by ent, DO NOT EDIT. package db import ( "context" "fmt" "math" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/dexidp/dex/storage/ent/db/oauth2client" "github.com/dexidp/dex/storage/ent/db/predicate" ) // OAuth2ClientQuery is the builder for querying OAuth2Client entities. type OAuth2ClientQuery struct { config limit *int offset *int unique *bool order []OrderFunc fields []string predicates []predicate.OAuth2Client // intermediate query (i.e. traversal path). sql *sql.Selector path func(context.Context) (*sql.Selector, error) } // Where adds a new predicate for the OAuth2ClientQuery builder. func (oq *OAuth2ClientQuery) Where(ps ...predicate.OAuth2Client) *OAuth2ClientQuery { oq.predicates = append(oq.predicates, ps...) return oq } // Limit adds a limit step to the query. func (oq *OAuth2ClientQuery) Limit(limit int) *OAuth2ClientQuery { oq.limit = &limit return oq } // Offset adds an offset step to the query. func (oq *OAuth2ClientQuery) Offset(offset int) *OAuth2ClientQuery { oq.offset = &offset return oq } // Unique configures the query builder to filter duplicate records on query. // By default, unique is set to true, and can be disabled using this method. func (oq *OAuth2ClientQuery) Unique(unique bool) *OAuth2ClientQuery { oq.unique = &unique return oq } // Order adds an order step to the query. func (oq *OAuth2ClientQuery) Order(o ...OrderFunc) *OAuth2ClientQuery { oq.order = append(oq.order, o...) return oq } // First returns the first OAuth2Client entity from the query. // Returns a *NotFoundError when no OAuth2Client was found. func (oq *OAuth2ClientQuery) First(ctx context.Context) (*OAuth2Client, error) { nodes, err := oq.Limit(1).All(ctx) if err != nil { return nil, err } if len(nodes) == 0 { return nil, &NotFoundError{oauth2client.Label} } return nodes[0], nil } // FirstX is like First, but panics if an error occurs. func (oq *OAuth2ClientQuery) FirstX(ctx context.Context) *OAuth2Client { node, err := oq.First(ctx) if err != nil && !IsNotFound(err) { panic(err) } return node } // FirstID returns the first OAuth2Client ID from the query. // Returns a *NotFoundError when no OAuth2Client ID was found. func (oq *OAuth2ClientQuery) FirstID(ctx context.Context) (id string, err error) { var ids []string if ids, err = oq.Limit(1).IDs(ctx); err != nil { return } if len(ids) == 0 { err = &NotFoundError{oauth2client.Label} return } return ids[0], nil } // FirstIDX is like FirstID, but panics if an error occurs. func (oq *OAuth2ClientQuery) FirstIDX(ctx context.Context) string { id, err := oq.FirstID(ctx) if err != nil && !IsNotFound(err) { panic(err) } return id } // Only returns a single OAuth2Client entity found by the query, ensuring it only returns one. // Returns a *NotSingularError when more than one OAuth2Client entity is found. // Returns a *NotFoundError when no OAuth2Client entities are found. func (oq *OAuth2ClientQuery) Only(ctx context.Context) (*OAuth2Client, error) { nodes, err := oq.Limit(2).All(ctx) if err != nil { return nil, err } switch len(nodes) { case 1: return nodes[0], nil case 0: return nil, &NotFoundError{oauth2client.Label} default: return nil, &NotSingularError{oauth2client.Label} } } // OnlyX is like Only, but panics if an error occurs. func (oq *OAuth2ClientQuery) OnlyX(ctx context.Context) *OAuth2Client { node, err := oq.Only(ctx) if err != nil { panic(err) } return node } // OnlyID is like Only, but returns the only OAuth2Client ID in the query. // Returns a *NotSingularError when more than one OAuth2Client ID is found. // Returns a *NotFoundError when no entities are found. func (oq *OAuth2ClientQuery) OnlyID(ctx context.Context) (id string, err error) { var ids []string if ids, err = oq.Limit(2).IDs(ctx); err != nil { return } switch len(ids) { case 1: id = ids[0] case 0: err = &NotFoundError{oauth2client.Label} default: err = &NotSingularError{oauth2client.Label} } return } // OnlyIDX is like OnlyID, but panics if an error occurs. func (oq *OAuth2ClientQuery) OnlyIDX(ctx context.Context) string { id, err := oq.OnlyID(ctx) if err != nil { panic(err) } return id } // All executes the query and returns a list of OAuth2Clients. func (oq *OAuth2ClientQuery) All(ctx context.Context) ([]*OAuth2Client, error) { if err := oq.prepareQuery(ctx); err != nil { return nil, err } return oq.sqlAll(ctx) } // AllX is like All, but panics if an error occurs. func (oq *OAuth2ClientQuery) AllX(ctx context.Context) []*OAuth2Client { nodes, err := oq.All(ctx) if err != nil { panic(err) } return nodes } // IDs executes the query and returns a list of OAuth2Client IDs. func (oq *OAuth2ClientQuery) IDs(ctx context.Context) ([]string, error) { var ids []string if err := oq.Select(oauth2client.FieldID).Scan(ctx, &ids); err != nil { return nil, err } return ids, nil } // IDsX is like IDs, but panics if an error occurs. func (oq *OAuth2ClientQuery) IDsX(ctx context.Context) []string { ids, err := oq.IDs(ctx) if err != nil { panic(err) } return ids } // Count returns the count of the given query. func (oq *OAuth2ClientQuery) Count(ctx context.Context) (int, error) { if err := oq.prepareQuery(ctx); err != nil { return 0, err } return oq.sqlCount(ctx) } // CountX is like Count, but panics if an error occurs. func (oq *OAuth2ClientQuery) CountX(ctx context.Context) int { count, err := oq.Count(ctx) if err != nil { panic(err) } return count } // Exist returns true if the query has elements in the graph. func (oq *OAuth2ClientQuery) Exist(ctx context.Context) (bool, error) { if err := oq.prepareQuery(ctx); err != nil { return false, err } return oq.sqlExist(ctx) } // ExistX is like Exist, but panics if an error occurs. func (oq *OAuth2ClientQuery) ExistX(ctx context.Context) bool { exist, err := oq.Exist(ctx) if err != nil { panic(err) } return exist } // Clone returns a duplicate of the OAuth2ClientQuery builder, including all associated steps. It can be // used to prepare common query builders and use them differently after the clone is made. func (oq *OAuth2ClientQuery) Clone() *OAuth2ClientQuery { if oq == nil { return nil } return &OAuth2ClientQuery{ config: oq.config, limit: oq.limit, offset: oq.offset, order: append([]OrderFunc{}, oq.order...), predicates: append([]predicate.OAuth2Client{}, oq.predicates...), // clone intermediate query. sql: oq.sql.Clone(), path: oq.path, unique: oq.unique, } } // GroupBy is used to group vertices by one or more fields/columns. // It is often used with aggregate functions, like: count, max, mean, min, sum. // // Example: // // var v []struct { // Secret string `json:"secret,omitempty"` // Count int `json:"count,omitempty"` // } // // client.OAuth2Client.Query(). // GroupBy(oauth2client.FieldSecret). // Aggregate(db.Count()). // Scan(ctx, &v) // func (oq *OAuth2ClientQuery) GroupBy(field string, fields ...string) *OAuth2ClientGroupBy { grbuild := &OAuth2ClientGroupBy{config: oq.config} grbuild.fields = append([]string{field}, fields...) grbuild.path = func(ctx context.Context) (prev *sql.Selector, err error) { if err := oq.prepareQuery(ctx); err != nil { return nil, err } return oq.sqlQuery(ctx), nil } grbuild.label = oauth2client.Label grbuild.flds, grbuild.scan = &grbuild.fields, grbuild.Scan return grbuild } // Select allows the selection one or more fields/columns for the given query, // instead of selecting all fields in the entity. // // Example: // // var v []struct { // Secret string `json:"secret,omitempty"` // } // // client.OAuth2Client.Query(). // Select(oauth2client.FieldSecret). // Scan(ctx, &v) // func (oq *OAuth2ClientQuery) Select(fields ...string) *OAuth2ClientSelect { oq.fields = append(oq.fields, fields...) selbuild := &OAuth2ClientSelect{OAuth2ClientQuery: oq} selbuild.label = oauth2client.Label selbuild.flds, selbuild.scan = &oq.fields, selbuild.Scan return selbuild } func (oq *OAuth2ClientQuery) prepareQuery(ctx context.Context) error { for _, f := range oq.fields { if !oauth2client.ValidColumn(f) { return &ValidationError{Name: f, err: fmt.Errorf("db: invalid field %q for query", f)} } } if oq.path != nil { prev, err := oq.path(ctx) if err != nil { return err } oq.sql = prev } return nil } func (oq *OAuth2ClientQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*OAuth2Client, error) { var ( nodes = []*OAuth2Client{} _spec = oq.querySpec() ) _spec.ScanValues = func(columns []string) ([]interface{}, error) { return (*OAuth2Client).scanValues(nil, columns) } _spec.Assign = func(columns []string, values []interface{}) error { node := &OAuth2Client{config: oq.config} nodes = append(nodes, node) return node.assignValues(columns, values) } for i := range hooks { hooks[i](ctx, _spec) } if err := sqlgraph.QueryNodes(ctx, oq.driver, _spec); err != nil { return nil, err } if len(nodes) == 0 { return nodes, nil } return nodes, nil } func (oq *OAuth2ClientQuery) sqlCount(ctx context.Context) (int, error) { _spec := oq.querySpec() _spec.Node.Columns = oq.fields if len(oq.fields) > 0 { _spec.Unique = oq.unique != nil && *oq.unique } return sqlgraph.CountNodes(ctx, oq.driver, _spec) } func (oq *OAuth2ClientQuery) sqlExist(ctx context.Context) (bool, error) { n, err := oq.sqlCount(ctx) if err != nil { return false, fmt.Errorf("db: check existence: %w", err) } return n > 0, nil } func (oq *OAuth2ClientQuery) querySpec() *sqlgraph.QuerySpec { _spec := &sqlgraph.QuerySpec{ Node: &sqlgraph.NodeSpec{ Table: oauth2client.Table, Columns: oauth2client.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeString, Column: oauth2client.FieldID, }, }, From: oq.sql, Unique: true, } if unique := oq.unique; unique != nil { _spec.Unique = *unique } if fields := oq.fields; len(fields) > 0 { _spec.Node.Columns = make([]string, 0, len(fields)) _spec.Node.Columns = append(_spec.Node.Columns, oauth2client.FieldID) for i := range fields { if fields[i] != oauth2client.FieldID { _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) } } } if ps := oq.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if limit := oq.limit; limit != nil { _spec.Limit = *limit } if offset := oq.offset; offset != nil { _spec.Offset = *offset } if ps := oq.order; len(ps) > 0 { _spec.Order = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } return _spec } func (oq *OAuth2ClientQuery) sqlQuery(ctx context.Context) *sql.Selector { builder := sql.Dialect(oq.driver.Dialect()) t1 := builder.Table(oauth2client.Table) columns := oq.fields if len(columns) == 0 { columns = oauth2client.Columns } selector := builder.Select(t1.Columns(columns...)...).From(t1) if oq.sql != nil { selector = oq.sql selector.Select(selector.Columns(columns...)...) } if oq.unique != nil && *oq.unique { selector.Distinct() } for _, p := range oq.predicates { p(selector) } for _, p := range oq.order { p(selector) } if offset := oq.offset; offset != nil { // limit is mandatory for offset clause. We start // with default value, and override it below if needed. selector.Offset(*offset).Limit(math.MaxInt32) } if limit := oq.limit; limit != nil { selector.Limit(*limit) } return selector } // OAuth2ClientGroupBy is the group-by builder for OAuth2Client entities. type OAuth2ClientGroupBy struct { config selector fields []string fns []AggregateFunc // intermediate query (i.e. traversal path). sql *sql.Selector path func(context.Context) (*sql.Selector, error) } // Aggregate adds the given aggregation functions to the group-by query. func (ogb *OAuth2ClientGroupBy) Aggregate(fns ...AggregateFunc) *OAuth2ClientGroupBy { ogb.fns = append(ogb.fns, fns...) return ogb } // Scan applies the group-by query and scans the result into the given value. func (ogb *OAuth2ClientGroupBy) Scan(ctx context.Context, v interface{}) error { query, err := ogb.path(ctx) if err != nil { return err } ogb.sql = query return ogb.sqlScan(ctx, v) } func (ogb *OAuth2ClientGroupBy) sqlScan(ctx context.Context, v interface{}) error { for _, f := range ogb.fields { if !oauth2client.ValidColumn(f) { return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)} } } selector := ogb.sqlQuery() if err := selector.Err(); err != nil { return err } rows := &sql.Rows{} query, args := selector.Query() if err := ogb.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() return sql.ScanSlice(rows, v) } func (ogb *OAuth2ClientGroupBy) sqlQuery() *sql.Selector { selector := ogb.sql.Select() aggregation := make([]string, 0, len(ogb.fns)) for _, fn := range ogb.fns { aggregation = append(aggregation, fn(selector)) } // If no columns were selected in a custom aggregation function, the default // selection is the fields used for "group-by", and the aggregation functions. if len(selector.SelectedColumns()) == 0 { columns := make([]string, 0, len(ogb.fields)+len(ogb.fns)) for _, f := range ogb.fields { columns = append(columns, selector.C(f)) } columns = append(columns, aggregation...) selector.Select(columns...) } return selector.GroupBy(selector.Columns(ogb.fields...)...) } // OAuth2ClientSelect is the builder for selecting fields of OAuth2Client entities. type OAuth2ClientSelect struct { *OAuth2ClientQuery selector // intermediate query (i.e. traversal path). sql *sql.Selector } // Scan applies the selector query and scans the result into the given value. func (os *OAuth2ClientSelect) Scan(ctx context.Context, v interface{}) error { if err := os.prepareQuery(ctx); err != nil { return err } os.sql = os.OAuth2ClientQuery.sqlQuery(ctx) return os.sqlScan(ctx, v) } func (os *OAuth2ClientSelect) sqlScan(ctx context.Context, v interface{}) error { rows := &sql.Rows{} query, args := os.sql.Query() if err := os.driver.Query(ctx, query, args, rows); err != nil { return err } defer rows.Close() return sql.ScanSlice(rows, v) }