mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
entc/gen: use query ops in generated code (#4005)
This commit is contained in:
@@ -13,6 +13,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/examples/privacytenant/ent/group"
|
||||
@@ -114,7 +115,7 @@ func (gq *GroupQuery) QueryUsers() *UserQuery {
|
||||
// First returns the first Group entity from the query.
|
||||
// Returns a *NotFoundError when no Group was found.
|
||||
func (gq *GroupQuery) First(ctx context.Context) (*Group, error) {
|
||||
nodes, err := gq.Limit(1).All(setContextOp(ctx, gq.ctx, "First"))
|
||||
nodes, err := gq.Limit(1).All(setContextOp(ctx, gq.ctx, ent.OpQueryFirst))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -137,7 +138,7 @@ func (gq *GroupQuery) FirstX(ctx context.Context) *Group {
|
||||
// Returns a *NotFoundError when no Group ID was found.
|
||||
func (gq *GroupQuery) FirstID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = gq.Limit(1).IDs(setContextOp(ctx, gq.ctx, "FirstID")); err != nil {
|
||||
if ids, err = gq.Limit(1).IDs(setContextOp(ctx, gq.ctx, ent.OpQueryFirstID)); err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
@@ -160,7 +161,7 @@ func (gq *GroupQuery) FirstIDX(ctx context.Context) int {
|
||||
// Returns a *NotSingularError when more than one Group entity is found.
|
||||
// Returns a *NotFoundError when no Group entities are found.
|
||||
func (gq *GroupQuery) Only(ctx context.Context) (*Group, error) {
|
||||
nodes, err := gq.Limit(2).All(setContextOp(ctx, gq.ctx, "Only"))
|
||||
nodes, err := gq.Limit(2).All(setContextOp(ctx, gq.ctx, ent.OpQueryOnly))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -188,7 +189,7 @@ func (gq *GroupQuery) OnlyX(ctx context.Context) *Group {
|
||||
// Returns a *NotFoundError when no entities are found.
|
||||
func (gq *GroupQuery) OnlyID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = gq.Limit(2).IDs(setContextOp(ctx, gq.ctx, "OnlyID")); err != nil {
|
||||
if ids, err = gq.Limit(2).IDs(setContextOp(ctx, gq.ctx, ent.OpQueryOnlyID)); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(ids) {
|
||||
@@ -213,7 +214,7 @@ func (gq *GroupQuery) OnlyIDX(ctx context.Context) int {
|
||||
|
||||
// All executes the query and returns a list of Groups.
|
||||
func (gq *GroupQuery) All(ctx context.Context) ([]*Group, error) {
|
||||
ctx = setContextOp(ctx, gq.ctx, "All")
|
||||
ctx = setContextOp(ctx, gq.ctx, ent.OpQueryAll)
|
||||
if err := gq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -235,7 +236,7 @@ func (gq *GroupQuery) IDs(ctx context.Context) (ids []int, err error) {
|
||||
if gq.ctx.Unique == nil && gq.path != nil {
|
||||
gq.Unique(true)
|
||||
}
|
||||
ctx = setContextOp(ctx, gq.ctx, "IDs")
|
||||
ctx = setContextOp(ctx, gq.ctx, ent.OpQueryIDs)
|
||||
if err = gq.Select(group.FieldID).Scan(ctx, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -253,7 +254,7 @@ func (gq *GroupQuery) IDsX(ctx context.Context) []int {
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (gq *GroupQuery) Count(ctx context.Context) (int, error) {
|
||||
ctx = setContextOp(ctx, gq.ctx, "Count")
|
||||
ctx = setContextOp(ctx, gq.ctx, ent.OpQueryCount)
|
||||
if err := gq.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -271,7 +272,7 @@ func (gq *GroupQuery) CountX(ctx context.Context) int {
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (gq *GroupQuery) Exist(ctx context.Context) (bool, error) {
|
||||
ctx = setContextOp(ctx, gq.ctx, "Exist")
|
||||
ctx = setContextOp(ctx, gq.ctx, ent.OpQueryExist)
|
||||
switch _, err := gq.FirstID(ctx); {
|
||||
case IsNotFound(err):
|
||||
return false, nil
|
||||
@@ -645,7 +646,7 @@ func (ggb *GroupGroupBy) Aggregate(fns ...AggregateFunc) *GroupGroupBy {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (ggb *GroupGroupBy) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, ggb.build.ctx, "GroupBy")
|
||||
ctx = setContextOp(ctx, ggb.build.ctx, ent.OpQueryGroupBy)
|
||||
if err := ggb.build.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -693,7 +694,7 @@ func (gs *GroupSelect) Aggregate(fns ...AggregateFunc) *GroupSelect {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (gs *GroupSelect) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, gs.ctx, "Select")
|
||||
ctx = setContextOp(ctx, gs.ctx, ent.OpQuerySelect)
|
||||
if err := gs.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/examples/privacytenant/ent/predicate"
|
||||
@@ -65,7 +66,7 @@ func (tq *TenantQuery) Order(o ...tenant.OrderOption) *TenantQuery {
|
||||
// First returns the first Tenant entity from the query.
|
||||
// Returns a *NotFoundError when no Tenant was found.
|
||||
func (tq *TenantQuery) First(ctx context.Context) (*Tenant, error) {
|
||||
nodes, err := tq.Limit(1).All(setContextOp(ctx, tq.ctx, "First"))
|
||||
nodes, err := tq.Limit(1).All(setContextOp(ctx, tq.ctx, ent.OpQueryFirst))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -88,7 +89,7 @@ func (tq *TenantQuery) FirstX(ctx context.Context) *Tenant {
|
||||
// Returns a *NotFoundError when no Tenant ID was found.
|
||||
func (tq *TenantQuery) FirstID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = tq.Limit(1).IDs(setContextOp(ctx, tq.ctx, "FirstID")); err != nil {
|
||||
if ids, err = tq.Limit(1).IDs(setContextOp(ctx, tq.ctx, ent.OpQueryFirstID)); err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
@@ -111,7 +112,7 @@ func (tq *TenantQuery) FirstIDX(ctx context.Context) int {
|
||||
// Returns a *NotSingularError when more than one Tenant entity is found.
|
||||
// Returns a *NotFoundError when no Tenant entities are found.
|
||||
func (tq *TenantQuery) Only(ctx context.Context) (*Tenant, error) {
|
||||
nodes, err := tq.Limit(2).All(setContextOp(ctx, tq.ctx, "Only"))
|
||||
nodes, err := tq.Limit(2).All(setContextOp(ctx, tq.ctx, ent.OpQueryOnly))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -139,7 +140,7 @@ func (tq *TenantQuery) OnlyX(ctx context.Context) *Tenant {
|
||||
// Returns a *NotFoundError when no entities are found.
|
||||
func (tq *TenantQuery) OnlyID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = tq.Limit(2).IDs(setContextOp(ctx, tq.ctx, "OnlyID")); err != nil {
|
||||
if ids, err = tq.Limit(2).IDs(setContextOp(ctx, tq.ctx, ent.OpQueryOnlyID)); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(ids) {
|
||||
@@ -164,7 +165,7 @@ func (tq *TenantQuery) OnlyIDX(ctx context.Context) int {
|
||||
|
||||
// All executes the query and returns a list of Tenants.
|
||||
func (tq *TenantQuery) All(ctx context.Context) ([]*Tenant, error) {
|
||||
ctx = setContextOp(ctx, tq.ctx, "All")
|
||||
ctx = setContextOp(ctx, tq.ctx, ent.OpQueryAll)
|
||||
if err := tq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -186,7 +187,7 @@ func (tq *TenantQuery) IDs(ctx context.Context) (ids []int, err error) {
|
||||
if tq.ctx.Unique == nil && tq.path != nil {
|
||||
tq.Unique(true)
|
||||
}
|
||||
ctx = setContextOp(ctx, tq.ctx, "IDs")
|
||||
ctx = setContextOp(ctx, tq.ctx, ent.OpQueryIDs)
|
||||
if err = tq.Select(tenant.FieldID).Scan(ctx, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -204,7 +205,7 @@ func (tq *TenantQuery) IDsX(ctx context.Context) []int {
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (tq *TenantQuery) Count(ctx context.Context) (int, error) {
|
||||
ctx = setContextOp(ctx, tq.ctx, "Count")
|
||||
ctx = setContextOp(ctx, tq.ctx, ent.OpQueryCount)
|
||||
if err := tq.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -222,7 +223,7 @@ func (tq *TenantQuery) CountX(ctx context.Context) int {
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (tq *TenantQuery) Exist(ctx context.Context) (bool, error) {
|
||||
ctx = setContextOp(ctx, tq.ctx, "Exist")
|
||||
ctx = setContextOp(ctx, tq.ctx, ent.OpQueryExist)
|
||||
switch _, err := tq.FirstID(ctx); {
|
||||
case IsNotFound(err):
|
||||
return false, nil
|
||||
@@ -460,7 +461,7 @@ func (tgb *TenantGroupBy) Aggregate(fns ...AggregateFunc) *TenantGroupBy {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (tgb *TenantGroupBy) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, tgb.build.ctx, "GroupBy")
|
||||
ctx = setContextOp(ctx, tgb.build.ctx, ent.OpQueryGroupBy)
|
||||
if err := tgb.build.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -508,7 +509,7 @@ func (ts *TenantSelect) Aggregate(fns ...AggregateFunc) *TenantSelect {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (ts *TenantSelect) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, ts.ctx, "Select")
|
||||
ctx = setContextOp(ctx, ts.ctx, ent.OpQuerySelect)
|
||||
if err := ts.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/examples/privacytenant/ent/group"
|
||||
@@ -114,7 +115,7 @@ func (uq *UserQuery) QueryGroups() *GroupQuery {
|
||||
// First returns the first User entity from the query.
|
||||
// Returns a *NotFoundError when no User was found.
|
||||
func (uq *UserQuery) First(ctx context.Context) (*User, error) {
|
||||
nodes, err := uq.Limit(1).All(setContextOp(ctx, uq.ctx, "First"))
|
||||
nodes, err := uq.Limit(1).All(setContextOp(ctx, uq.ctx, ent.OpQueryFirst))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -137,7 +138,7 @@ func (uq *UserQuery) FirstX(ctx context.Context) *User {
|
||||
// Returns a *NotFoundError when no User ID was found.
|
||||
func (uq *UserQuery) FirstID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = uq.Limit(1).IDs(setContextOp(ctx, uq.ctx, "FirstID")); err != nil {
|
||||
if ids, err = uq.Limit(1).IDs(setContextOp(ctx, uq.ctx, ent.OpQueryFirstID)); err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
@@ -160,7 +161,7 @@ func (uq *UserQuery) FirstIDX(ctx context.Context) int {
|
||||
// Returns a *NotSingularError when more than one User entity is found.
|
||||
// Returns a *NotFoundError when no User entities are found.
|
||||
func (uq *UserQuery) Only(ctx context.Context) (*User, error) {
|
||||
nodes, err := uq.Limit(2).All(setContextOp(ctx, uq.ctx, "Only"))
|
||||
nodes, err := uq.Limit(2).All(setContextOp(ctx, uq.ctx, ent.OpQueryOnly))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -188,7 +189,7 @@ func (uq *UserQuery) OnlyX(ctx context.Context) *User {
|
||||
// Returns a *NotFoundError when no entities are found.
|
||||
func (uq *UserQuery) OnlyID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = uq.Limit(2).IDs(setContextOp(ctx, uq.ctx, "OnlyID")); err != nil {
|
||||
if ids, err = uq.Limit(2).IDs(setContextOp(ctx, uq.ctx, ent.OpQueryOnlyID)); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(ids) {
|
||||
@@ -213,7 +214,7 @@ func (uq *UserQuery) OnlyIDX(ctx context.Context) int {
|
||||
|
||||
// All executes the query and returns a list of Users.
|
||||
func (uq *UserQuery) All(ctx context.Context) ([]*User, error) {
|
||||
ctx = setContextOp(ctx, uq.ctx, "All")
|
||||
ctx = setContextOp(ctx, uq.ctx, ent.OpQueryAll)
|
||||
if err := uq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -235,7 +236,7 @@ func (uq *UserQuery) IDs(ctx context.Context) (ids []int, err error) {
|
||||
if uq.ctx.Unique == nil && uq.path != nil {
|
||||
uq.Unique(true)
|
||||
}
|
||||
ctx = setContextOp(ctx, uq.ctx, "IDs")
|
||||
ctx = setContextOp(ctx, uq.ctx, ent.OpQueryIDs)
|
||||
if err = uq.Select(user.FieldID).Scan(ctx, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -253,7 +254,7 @@ func (uq *UserQuery) IDsX(ctx context.Context) []int {
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (uq *UserQuery) Count(ctx context.Context) (int, error) {
|
||||
ctx = setContextOp(ctx, uq.ctx, "Count")
|
||||
ctx = setContextOp(ctx, uq.ctx, ent.OpQueryCount)
|
||||
if err := uq.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -271,7 +272,7 @@ func (uq *UserQuery) CountX(ctx context.Context) int {
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (uq *UserQuery) Exist(ctx context.Context) (bool, error) {
|
||||
ctx = setContextOp(ctx, uq.ctx, "Exist")
|
||||
ctx = setContextOp(ctx, uq.ctx, ent.OpQueryExist)
|
||||
switch _, err := uq.FirstID(ctx); {
|
||||
case IsNotFound(err):
|
||||
return false, nil
|
||||
@@ -645,7 +646,7 @@ func (ugb *UserGroupBy) Aggregate(fns ...AggregateFunc) *UserGroupBy {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (ugb *UserGroupBy) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, ugb.build.ctx, "GroupBy")
|
||||
ctx = setContextOp(ctx, ugb.build.ctx, ent.OpQueryGroupBy)
|
||||
if err := ugb.build.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -693,7 +694,7 @@ func (us *UserSelect) Aggregate(fns ...AggregateFunc) *UserSelect {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (us *UserSelect) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, us.ctx, "Select")
|
||||
ctx = setContextOp(ctx, us.ctx, ent.OpQuerySelect)
|
||||
if err := us.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user