rano/db/ent/verifytoken_query.go

615 lines
18 KiB
Go
Raw Normal View History

2024-11-17 16:58:29 +00:00
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"math"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"gitserver.in/patialtech/rano/db/ent/predicate"
"gitserver.in/patialtech/rano/db/ent/user"
"gitserver.in/patialtech/rano/db/ent/verifytoken"
)
// VerifyTokenQuery is the builder for querying VerifyToken entities.
type VerifyTokenQuery struct {
config
ctx *QueryContext
order []verifytoken.OrderOption
inters []Interceptor
predicates []predicate.VerifyToken
withUser *UserQuery
withFKs bool
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the VerifyTokenQuery builder.
func (vtq *VerifyTokenQuery) Where(ps ...predicate.VerifyToken) *VerifyTokenQuery {
vtq.predicates = append(vtq.predicates, ps...)
return vtq
}
// Limit the number of records to be returned by this query.
func (vtq *VerifyTokenQuery) Limit(limit int) *VerifyTokenQuery {
vtq.ctx.Limit = &limit
return vtq
}
// Offset to start from.
func (vtq *VerifyTokenQuery) Offset(offset int) *VerifyTokenQuery {
vtq.ctx.Offset = &offset
return vtq
}
// 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 (vtq *VerifyTokenQuery) Unique(unique bool) *VerifyTokenQuery {
vtq.ctx.Unique = &unique
return vtq
}
// Order specifies how the records should be ordered.
func (vtq *VerifyTokenQuery) Order(o ...verifytoken.OrderOption) *VerifyTokenQuery {
vtq.order = append(vtq.order, o...)
return vtq
}
// QueryUser chains the current query on the "user" edge.
func (vtq *VerifyTokenQuery) QueryUser() *UserQuery {
query := (&UserClient{config: vtq.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := vtq.prepareQuery(ctx); err != nil {
return nil, err
}
selector := vtq.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(verifytoken.Table, verifytoken.FieldID, selector),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, verifytoken.UserTable, verifytoken.UserColumn),
)
fromU = sqlgraph.SetNeighbors(vtq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first VerifyToken entity from the query.
// Returns a *NotFoundError when no VerifyToken was found.
func (vtq *VerifyTokenQuery) First(ctx context.Context) (*VerifyToken, error) {
nodes, err := vtq.Limit(1).All(setContextOp(ctx, vtq.ctx, ent.OpQueryFirst))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{verifytoken.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (vtq *VerifyTokenQuery) FirstX(ctx context.Context) *VerifyToken {
node, err := vtq.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first VerifyToken ID from the query.
// Returns a *NotFoundError when no VerifyToken ID was found.
func (vtq *VerifyTokenQuery) FirstID(ctx context.Context) (id int64, err error) {
var ids []int64
if ids, err = vtq.Limit(1).IDs(setContextOp(ctx, vtq.ctx, ent.OpQueryFirstID)); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{verifytoken.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (vtq *VerifyTokenQuery) FirstIDX(ctx context.Context) int64 {
id, err := vtq.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single VerifyToken entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one VerifyToken entity is found.
// Returns a *NotFoundError when no VerifyToken entities are found.
func (vtq *VerifyTokenQuery) Only(ctx context.Context) (*VerifyToken, error) {
nodes, err := vtq.Limit(2).All(setContextOp(ctx, vtq.ctx, ent.OpQueryOnly))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{verifytoken.Label}
default:
return nil, &NotSingularError{verifytoken.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (vtq *VerifyTokenQuery) OnlyX(ctx context.Context) *VerifyToken {
node, err := vtq.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only VerifyToken ID in the query.
// Returns a *NotSingularError when more than one VerifyToken ID is found.
// Returns a *NotFoundError when no entities are found.
func (vtq *VerifyTokenQuery) OnlyID(ctx context.Context) (id int64, err error) {
var ids []int64
if ids, err = vtq.Limit(2).IDs(setContextOp(ctx, vtq.ctx, ent.OpQueryOnlyID)); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{verifytoken.Label}
default:
err = &NotSingularError{verifytoken.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (vtq *VerifyTokenQuery) OnlyIDX(ctx context.Context) int64 {
id, err := vtq.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of VerifyTokens.
func (vtq *VerifyTokenQuery) All(ctx context.Context) ([]*VerifyToken, error) {
ctx = setContextOp(ctx, vtq.ctx, ent.OpQueryAll)
if err := vtq.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*VerifyToken, *VerifyTokenQuery]()
return withInterceptors[[]*VerifyToken](ctx, vtq, qr, vtq.inters)
}
// AllX is like All, but panics if an error occurs.
func (vtq *VerifyTokenQuery) AllX(ctx context.Context) []*VerifyToken {
nodes, err := vtq.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of VerifyToken IDs.
func (vtq *VerifyTokenQuery) IDs(ctx context.Context) (ids []int64, err error) {
if vtq.ctx.Unique == nil && vtq.path != nil {
vtq.Unique(true)
}
ctx = setContextOp(ctx, vtq.ctx, ent.OpQueryIDs)
if err = vtq.Select(verifytoken.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (vtq *VerifyTokenQuery) IDsX(ctx context.Context) []int64 {
ids, err := vtq.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (vtq *VerifyTokenQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, vtq.ctx, ent.OpQueryCount)
if err := vtq.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, vtq, querierCount[*VerifyTokenQuery](), vtq.inters)
}
// CountX is like Count, but panics if an error occurs.
func (vtq *VerifyTokenQuery) CountX(ctx context.Context) int {
count, err := vtq.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (vtq *VerifyTokenQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, vtq.ctx, ent.OpQueryExist)
switch _, err := vtq.FirstID(ctx); {
case IsNotFound(err):
return false, nil
case err != nil:
return false, fmt.Errorf("ent: check existence: %w", err)
default:
return true, nil
}
}
// ExistX is like Exist, but panics if an error occurs.
func (vtq *VerifyTokenQuery) ExistX(ctx context.Context) bool {
exist, err := vtq.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the VerifyTokenQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (vtq *VerifyTokenQuery) Clone() *VerifyTokenQuery {
if vtq == nil {
return nil
}
return &VerifyTokenQuery{
config: vtq.config,
ctx: vtq.ctx.Clone(),
order: append([]verifytoken.OrderOption{}, vtq.order...),
inters: append([]Interceptor{}, vtq.inters...),
predicates: append([]predicate.VerifyToken{}, vtq.predicates...),
withUser: vtq.withUser.Clone(),
// clone intermediate query.
sql: vtq.sql.Clone(),
path: vtq.path,
}
}
// WithUser tells the query-builder to eager-load the nodes that are connected to
// the "user" edge. The optional arguments are used to configure the query builder of the edge.
func (vtq *VerifyTokenQuery) WithUser(opts ...func(*UserQuery)) *VerifyTokenQuery {
query := (&UserClient{config: vtq.config}).Query()
for _, opt := range opts {
opt(query)
}
vtq.withUser = query
return vtq
}
// 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 {
// CreatedAt time.Time `json:"createdAt"`
// Count int `json:"count,omitempty"`
// }
//
// client.VerifyToken.Query().
// GroupBy(verifytoken.FieldCreatedAt).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (vtq *VerifyTokenQuery) GroupBy(field string, fields ...string) *VerifyTokenGroupBy {
vtq.ctx.Fields = append([]string{field}, fields...)
grbuild := &VerifyTokenGroupBy{build: vtq}
grbuild.flds = &vtq.ctx.Fields
grbuild.label = verifytoken.Label
grbuild.scan = 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 {
// CreatedAt time.Time `json:"createdAt"`
// }
//
// client.VerifyToken.Query().
// Select(verifytoken.FieldCreatedAt).
// Scan(ctx, &v)
func (vtq *VerifyTokenQuery) Select(fields ...string) *VerifyTokenSelect {
vtq.ctx.Fields = append(vtq.ctx.Fields, fields...)
sbuild := &VerifyTokenSelect{VerifyTokenQuery: vtq}
sbuild.label = verifytoken.Label
sbuild.flds, sbuild.scan = &vtq.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a VerifyTokenSelect configured with the given aggregations.
func (vtq *VerifyTokenQuery) Aggregate(fns ...AggregateFunc) *VerifyTokenSelect {
return vtq.Select().Aggregate(fns...)
}
func (vtq *VerifyTokenQuery) prepareQuery(ctx context.Context) error {
for _, inter := range vtq.inters {
if inter == nil {
return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
}
if trv, ok := inter.(Traverser); ok {
if err := trv.Traverse(ctx, vtq); err != nil {
return err
}
}
}
for _, f := range vtq.ctx.Fields {
if !verifytoken.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if vtq.path != nil {
prev, err := vtq.path(ctx)
if err != nil {
return err
}
vtq.sql = prev
}
return nil
}
func (vtq *VerifyTokenQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*VerifyToken, error) {
var (
nodes = []*VerifyToken{}
withFKs = vtq.withFKs
_spec = vtq.querySpec()
loadedTypes = [1]bool{
vtq.withUser != nil,
}
)
if vtq.withUser != nil {
withFKs = true
}
if withFKs {
_spec.Node.Columns = append(_spec.Node.Columns, verifytoken.ForeignKeys...)
}
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*VerifyToken).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &VerifyToken{config: vtq.config}
nodes = append(nodes, node)
node.Edges.loadedTypes = loadedTypes
return node.assignValues(columns, values)
}
for i := range hooks {
hooks[i](ctx, _spec)
}
if err := sqlgraph.QueryNodes(ctx, vtq.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := vtq.withUser; query != nil {
if err := vtq.loadUser(ctx, query, nodes, nil,
func(n *VerifyToken, e *User) { n.Edges.User = e }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (vtq *VerifyTokenQuery) loadUser(ctx context.Context, query *UserQuery, nodes []*VerifyToken, init func(*VerifyToken), assign func(*VerifyToken, *User)) error {
ids := make([]int64, 0, len(nodes))
nodeids := make(map[int64][]*VerifyToken)
for i := range nodes {
if nodes[i].user_id == nil {
continue
}
fk := *nodes[i].user_id
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(user.IDIn(ids...))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
nodes, ok := nodeids[n.ID]
if !ok {
return fmt.Errorf(`unexpected foreign-key "user_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (vtq *VerifyTokenQuery) sqlCount(ctx context.Context) (int, error) {
_spec := vtq.querySpec()
_spec.Node.Columns = vtq.ctx.Fields
if len(vtq.ctx.Fields) > 0 {
_spec.Unique = vtq.ctx.Unique != nil && *vtq.ctx.Unique
}
return sqlgraph.CountNodes(ctx, vtq.driver, _spec)
}
func (vtq *VerifyTokenQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(verifytoken.Table, verifytoken.Columns, sqlgraph.NewFieldSpec(verifytoken.FieldID, field.TypeInt64))
_spec.From = vtq.sql
if unique := vtq.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if vtq.path != nil {
_spec.Unique = true
}
if fields := vtq.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, verifytoken.FieldID)
for i := range fields {
if fields[i] != verifytoken.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
}
if ps := vtq.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := vtq.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := vtq.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := vtq.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (vtq *VerifyTokenQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(vtq.driver.Dialect())
t1 := builder.Table(verifytoken.Table)
columns := vtq.ctx.Fields
if len(columns) == 0 {
columns = verifytoken.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if vtq.sql != nil {
selector = vtq.sql
selector.Select(selector.Columns(columns...)...)
}
if vtq.ctx.Unique != nil && *vtq.ctx.Unique {
selector.Distinct()
}
for _, p := range vtq.predicates {
p(selector)
}
for _, p := range vtq.order {
p(selector)
}
if offset := vtq.ctx.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 := vtq.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// VerifyTokenGroupBy is the group-by builder for VerifyToken entities.
type VerifyTokenGroupBy struct {
selector
build *VerifyTokenQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (vtgb *VerifyTokenGroupBy) Aggregate(fns ...AggregateFunc) *VerifyTokenGroupBy {
vtgb.fns = append(vtgb.fns, fns...)
return vtgb
}
// Scan applies the selector query and scans the result into the given value.
func (vtgb *VerifyTokenGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, vtgb.build.ctx, ent.OpQueryGroupBy)
if err := vtgb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*VerifyTokenQuery, *VerifyTokenGroupBy](ctx, vtgb.build, vtgb, vtgb.build.inters, v)
}
func (vtgb *VerifyTokenGroupBy) sqlScan(ctx context.Context, root *VerifyTokenQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(vtgb.fns))
for _, fn := range vtgb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*vtgb.flds)+len(vtgb.fns))
for _, f := range *vtgb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*vtgb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := vtgb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// VerifyTokenSelect is the builder for selecting fields of VerifyToken entities.
type VerifyTokenSelect struct {
*VerifyTokenQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (vts *VerifyTokenSelect) Aggregate(fns ...AggregateFunc) *VerifyTokenSelect {
vts.fns = append(vts.fns, fns...)
return vts
}
// Scan applies the selector query and scans the result into the given value.
func (vts *VerifyTokenSelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, vts.ctx, ent.OpQuerySelect)
if err := vts.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*VerifyTokenQuery, *VerifyTokenSelect](ctx, vts.VerifyTokenQuery, vts, vts.inters, v)
}
func (vts *VerifyTokenSelect) sqlScan(ctx context.Context, root *VerifyTokenQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(vts.fns))
for _, fn := range vts.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*vts.selector.flds); {
case n == 0 && len(aggregation) > 0:
selector.Select(aggregation...)
case n != 0 && len(aggregation) > 0:
selector.AppendSelect(aggregation...)
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := vts.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}