Files
unR2/internal/ent/cloudflarer2_query.go
2025-07-19 02:03:24 +08:00

615 lines
18 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"math"
"unR2/internal/ent/cloudflareaccounts"
"unR2/internal/ent/cloudflarer2"
"unR2/internal/ent/predicate"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// CloudflareR2Query is the builder for querying CloudflareR2 entities.
type CloudflareR2Query struct {
config
ctx *QueryContext
order []cloudflarer2.OrderOption
inters []Interceptor
predicates []predicate.CloudflareR2
withCloudflareAccount *CloudflareAccountsQuery
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 CloudflareR2Query builder.
func (cr *CloudflareR2Query) Where(ps ...predicate.CloudflareR2) *CloudflareR2Query {
cr.predicates = append(cr.predicates, ps...)
return cr
}
// Limit the number of records to be returned by this query.
func (cr *CloudflareR2Query) Limit(limit int) *CloudflareR2Query {
cr.ctx.Limit = &limit
return cr
}
// Offset to start from.
func (cr *CloudflareR2Query) Offset(offset int) *CloudflareR2Query {
cr.ctx.Offset = &offset
return cr
}
// 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 (cr *CloudflareR2Query) Unique(unique bool) *CloudflareR2Query {
cr.ctx.Unique = &unique
return cr
}
// Order specifies how the records should be ordered.
func (cr *CloudflareR2Query) Order(o ...cloudflarer2.OrderOption) *CloudflareR2Query {
cr.order = append(cr.order, o...)
return cr
}
// QueryCloudflareAccount chains the current query on the "cloudflare_account" edge.
func (cr *CloudflareR2Query) QueryCloudflareAccount() *CloudflareAccountsQuery {
query := (&CloudflareAccountsClient{config: cr.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := cr.prepareQuery(ctx); err != nil {
return nil, err
}
selector := cr.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(cloudflarer2.Table, cloudflarer2.FieldID, selector),
sqlgraph.To(cloudflareaccounts.Table, cloudflareaccounts.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, cloudflarer2.CloudflareAccountTable, cloudflarer2.CloudflareAccountColumn),
)
fromU = sqlgraph.SetNeighbors(cr.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first CloudflareR2 entity from the query.
// Returns a *NotFoundError when no CloudflareR2 was found.
func (cr *CloudflareR2Query) First(ctx context.Context) (*CloudflareR2, error) {
nodes, err := cr.Limit(1).All(setContextOp(ctx, cr.ctx, ent.OpQueryFirst))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{cloudflarer2.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (cr *CloudflareR2Query) FirstX(ctx context.Context) *CloudflareR2 {
node, err := cr.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first CloudflareR2 ID from the query.
// Returns a *NotFoundError when no CloudflareR2 ID was found.
func (cr *CloudflareR2Query) FirstID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = cr.Limit(1).IDs(setContextOp(ctx, cr.ctx, ent.OpQueryFirstID)); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{cloudflarer2.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (cr *CloudflareR2Query) FirstIDX(ctx context.Context) int {
id, err := cr.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single CloudflareR2 entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one CloudflareR2 entity is found.
// Returns a *NotFoundError when no CloudflareR2 entities are found.
func (cr *CloudflareR2Query) Only(ctx context.Context) (*CloudflareR2, error) {
nodes, err := cr.Limit(2).All(setContextOp(ctx, cr.ctx, ent.OpQueryOnly))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{cloudflarer2.Label}
default:
return nil, &NotSingularError{cloudflarer2.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (cr *CloudflareR2Query) OnlyX(ctx context.Context) *CloudflareR2 {
node, err := cr.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only CloudflareR2 ID in the query.
// Returns a *NotSingularError when more than one CloudflareR2 ID is found.
// Returns a *NotFoundError when no entities are found.
func (cr *CloudflareR2Query) OnlyID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = cr.Limit(2).IDs(setContextOp(ctx, cr.ctx, ent.OpQueryOnlyID)); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{cloudflarer2.Label}
default:
err = &NotSingularError{cloudflarer2.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (cr *CloudflareR2Query) OnlyIDX(ctx context.Context) int {
id, err := cr.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of CloudflareR2s.
func (cr *CloudflareR2Query) All(ctx context.Context) ([]*CloudflareR2, error) {
ctx = setContextOp(ctx, cr.ctx, ent.OpQueryAll)
if err := cr.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*CloudflareR2, *CloudflareR2Query]()
return withInterceptors[[]*CloudflareR2](ctx, cr, qr, cr.inters)
}
// AllX is like All, but panics if an error occurs.
func (cr *CloudflareR2Query) AllX(ctx context.Context) []*CloudflareR2 {
nodes, err := cr.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of CloudflareR2 IDs.
func (cr *CloudflareR2Query) IDs(ctx context.Context) (ids []int, err error) {
if cr.ctx.Unique == nil && cr.path != nil {
cr.Unique(true)
}
ctx = setContextOp(ctx, cr.ctx, ent.OpQueryIDs)
if err = cr.Select(cloudflarer2.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (cr *CloudflareR2Query) IDsX(ctx context.Context) []int {
ids, err := cr.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (cr *CloudflareR2Query) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, cr.ctx, ent.OpQueryCount)
if err := cr.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, cr, querierCount[*CloudflareR2Query](), cr.inters)
}
// CountX is like Count, but panics if an error occurs.
func (cr *CloudflareR2Query) CountX(ctx context.Context) int {
count, err := cr.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (cr *CloudflareR2Query) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, cr.ctx, ent.OpQueryExist)
switch _, err := cr.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 (cr *CloudflareR2Query) ExistX(ctx context.Context) bool {
exist, err := cr.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the CloudflareR2Query builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (cr *CloudflareR2Query) Clone() *CloudflareR2Query {
if cr == nil {
return nil
}
return &CloudflareR2Query{
config: cr.config,
ctx: cr.ctx.Clone(),
order: append([]cloudflarer2.OrderOption{}, cr.order...),
inters: append([]Interceptor{}, cr.inters...),
predicates: append([]predicate.CloudflareR2{}, cr.predicates...),
withCloudflareAccount: cr.withCloudflareAccount.Clone(),
// clone intermediate query.
sql: cr.sql.Clone(),
path: cr.path,
}
}
// WithCloudflareAccount tells the query-builder to eager-load the nodes that are connected to
// the "cloudflare_account" edge. The optional arguments are used to configure the query builder of the edge.
func (cr *CloudflareR2Query) WithCloudflareAccount(opts ...func(*CloudflareAccountsQuery)) *CloudflareR2Query {
query := (&CloudflareAccountsClient{config: cr.config}).Query()
for _, opt := range opts {
opt(query)
}
cr.withCloudflareAccount = query
return cr
}
// 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 {
// OwnerID string `json:"owner_id,omitempty"`
// Count int `json:"count,omitempty"`
// }
//
// client.CloudflareR2.Query().
// GroupBy(cloudflarer2.FieldOwnerID).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (cr *CloudflareR2Query) GroupBy(field string, fields ...string) *CloudflareR2GroupBy {
cr.ctx.Fields = append([]string{field}, fields...)
grbuild := &CloudflareR2GroupBy{build: cr}
grbuild.flds = &cr.ctx.Fields
grbuild.label = cloudflarer2.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 {
// OwnerID string `json:"owner_id,omitempty"`
// }
//
// client.CloudflareR2.Query().
// Select(cloudflarer2.FieldOwnerID).
// Scan(ctx, &v)
func (cr *CloudflareR2Query) Select(fields ...string) *CloudflareR2Select {
cr.ctx.Fields = append(cr.ctx.Fields, fields...)
sbuild := &CloudflareR2Select{CloudflareR2Query: cr}
sbuild.label = cloudflarer2.Label
sbuild.flds, sbuild.scan = &cr.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a CloudflareR2Select configured with the given aggregations.
func (cr *CloudflareR2Query) Aggregate(fns ...AggregateFunc) *CloudflareR2Select {
return cr.Select().Aggregate(fns...)
}
func (cr *CloudflareR2Query) prepareQuery(ctx context.Context) error {
for _, inter := range cr.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, cr); err != nil {
return err
}
}
}
for _, f := range cr.ctx.Fields {
if !cloudflarer2.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if cr.path != nil {
prev, err := cr.path(ctx)
if err != nil {
return err
}
cr.sql = prev
}
return nil
}
func (cr *CloudflareR2Query) sqlAll(ctx context.Context, hooks ...queryHook) ([]*CloudflareR2, error) {
var (
nodes = []*CloudflareR2{}
withFKs = cr.withFKs
_spec = cr.querySpec()
loadedTypes = [1]bool{
cr.withCloudflareAccount != nil,
}
)
if cr.withCloudflareAccount != nil {
withFKs = true
}
if withFKs {
_spec.Node.Columns = append(_spec.Node.Columns, cloudflarer2.ForeignKeys...)
}
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*CloudflareR2).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &CloudflareR2{config: cr.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, cr.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := cr.withCloudflareAccount; query != nil {
if err := cr.loadCloudflareAccount(ctx, query, nodes, nil,
func(n *CloudflareR2, e *CloudflareAccounts) { n.Edges.CloudflareAccount = e }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (cr *CloudflareR2Query) loadCloudflareAccount(ctx context.Context, query *CloudflareAccountsQuery, nodes []*CloudflareR2, init func(*CloudflareR2), assign func(*CloudflareR2, *CloudflareAccounts)) error {
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*CloudflareR2)
for i := range nodes {
if nodes[i].cloudflare_accounts_cloudflare_buckets == nil {
continue
}
fk := *nodes[i].cloudflare_accounts_cloudflare_buckets
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(cloudflareaccounts.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 "cloudflare_accounts_cloudflare_buckets" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (cr *CloudflareR2Query) sqlCount(ctx context.Context) (int, error) {
_spec := cr.querySpec()
_spec.Node.Columns = cr.ctx.Fields
if len(cr.ctx.Fields) > 0 {
_spec.Unique = cr.ctx.Unique != nil && *cr.ctx.Unique
}
return sqlgraph.CountNodes(ctx, cr.driver, _spec)
}
func (cr *CloudflareR2Query) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(cloudflarer2.Table, cloudflarer2.Columns, sqlgraph.NewFieldSpec(cloudflarer2.FieldID, field.TypeInt))
_spec.From = cr.sql
if unique := cr.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if cr.path != nil {
_spec.Unique = true
}
if fields := cr.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, cloudflarer2.FieldID)
for i := range fields {
if fields[i] != cloudflarer2.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
}
if ps := cr.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := cr.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := cr.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := cr.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (cr *CloudflareR2Query) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(cr.driver.Dialect())
t1 := builder.Table(cloudflarer2.Table)
columns := cr.ctx.Fields
if len(columns) == 0 {
columns = cloudflarer2.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if cr.sql != nil {
selector = cr.sql
selector.Select(selector.Columns(columns...)...)
}
if cr.ctx.Unique != nil && *cr.ctx.Unique {
selector.Distinct()
}
for _, p := range cr.predicates {
p(selector)
}
for _, p := range cr.order {
p(selector)
}
if offset := cr.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 := cr.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// CloudflareR2GroupBy is the group-by builder for CloudflareR2 entities.
type CloudflareR2GroupBy struct {
selector
build *CloudflareR2Query
}
// Aggregate adds the given aggregation functions to the group-by query.
func (crb *CloudflareR2GroupBy) Aggregate(fns ...AggregateFunc) *CloudflareR2GroupBy {
crb.fns = append(crb.fns, fns...)
return crb
}
// Scan applies the selector query and scans the result into the given value.
func (crb *CloudflareR2GroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, crb.build.ctx, ent.OpQueryGroupBy)
if err := crb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*CloudflareR2Query, *CloudflareR2GroupBy](ctx, crb.build, crb, crb.build.inters, v)
}
func (crb *CloudflareR2GroupBy) sqlScan(ctx context.Context, root *CloudflareR2Query, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(crb.fns))
for _, fn := range crb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*crb.flds)+len(crb.fns))
for _, f := range *crb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*crb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := crb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// CloudflareR2Select is the builder for selecting fields of CloudflareR2 entities.
type CloudflareR2Select struct {
*CloudflareR2Query
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (cr *CloudflareR2Select) Aggregate(fns ...AggregateFunc) *CloudflareR2Select {
cr.fns = append(cr.fns, fns...)
return cr
}
// Scan applies the selector query and scans the result into the given value.
func (cr *CloudflareR2Select) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, cr.ctx, ent.OpQuerySelect)
if err := cr.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*CloudflareR2Query, *CloudflareR2Select](ctx, cr.CloudflareR2Query, cr, cr.inters, v)
}
func (cr *CloudflareR2Select) sqlScan(ctx context.Context, root *CloudflareR2Query, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(cr.fns))
for _, fn := range cr.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*cr.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 := cr.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}