mirror of
https://github.com/ent/ent.git
synced 2026-04-30 06:30:55 +03:00
entc/gen: use query ops in generated code (#4005)
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/examples/jsonencode/ent/card"
|
||||
@@ -64,7 +65,7 @@ func (cq *CardQuery) Order(o ...card.OrderOption) *CardQuery {
|
||||
// First returns the first Card entity from the query.
|
||||
// Returns a *NotFoundError when no Card was found.
|
||||
func (cq *CardQuery) First(ctx context.Context) (*Card, error) {
|
||||
nodes, err := cq.Limit(1).All(setContextOp(ctx, cq.ctx, "First"))
|
||||
nodes, err := cq.Limit(1).All(setContextOp(ctx, cq.ctx, ent.OpQueryFirst))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -87,7 +88,7 @@ func (cq *CardQuery) FirstX(ctx context.Context) *Card {
|
||||
// Returns a *NotFoundError when no Card ID was found.
|
||||
func (cq *CardQuery) FirstID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = cq.Limit(1).IDs(setContextOp(ctx, cq.ctx, "FirstID")); err != nil {
|
||||
if ids, err = cq.Limit(1).IDs(setContextOp(ctx, cq.ctx, ent.OpQueryFirstID)); err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
@@ -110,7 +111,7 @@ func (cq *CardQuery) FirstIDX(ctx context.Context) int {
|
||||
// Returns a *NotSingularError when more than one Card entity is found.
|
||||
// Returns a *NotFoundError when no Card entities are found.
|
||||
func (cq *CardQuery) Only(ctx context.Context) (*Card, error) {
|
||||
nodes, err := cq.Limit(2).All(setContextOp(ctx, cq.ctx, "Only"))
|
||||
nodes, err := cq.Limit(2).All(setContextOp(ctx, cq.ctx, ent.OpQueryOnly))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -138,7 +139,7 @@ func (cq *CardQuery) OnlyX(ctx context.Context) *Card {
|
||||
// Returns a *NotFoundError when no entities are found.
|
||||
func (cq *CardQuery) OnlyID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = cq.Limit(2).IDs(setContextOp(ctx, cq.ctx, "OnlyID")); err != nil {
|
||||
if ids, err = cq.Limit(2).IDs(setContextOp(ctx, cq.ctx, ent.OpQueryOnlyID)); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(ids) {
|
||||
@@ -163,7 +164,7 @@ func (cq *CardQuery) OnlyIDX(ctx context.Context) int {
|
||||
|
||||
// All executes the query and returns a list of Cards.
|
||||
func (cq *CardQuery) All(ctx context.Context) ([]*Card, error) {
|
||||
ctx = setContextOp(ctx, cq.ctx, "All")
|
||||
ctx = setContextOp(ctx, cq.ctx, ent.OpQueryAll)
|
||||
if err := cq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -185,7 +186,7 @@ func (cq *CardQuery) IDs(ctx context.Context) (ids []int, err error) {
|
||||
if cq.ctx.Unique == nil && cq.path != nil {
|
||||
cq.Unique(true)
|
||||
}
|
||||
ctx = setContextOp(ctx, cq.ctx, "IDs")
|
||||
ctx = setContextOp(ctx, cq.ctx, ent.OpQueryIDs)
|
||||
if err = cq.Select(card.FieldID).Scan(ctx, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -203,7 +204,7 @@ func (cq *CardQuery) IDsX(ctx context.Context) []int {
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (cq *CardQuery) Count(ctx context.Context) (int, error) {
|
||||
ctx = setContextOp(ctx, cq.ctx, "Count")
|
||||
ctx = setContextOp(ctx, cq.ctx, ent.OpQueryCount)
|
||||
if err := cq.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -221,7 +222,7 @@ func (cq *CardQuery) CountX(ctx context.Context) int {
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (cq *CardQuery) Exist(ctx context.Context) (bool, error) {
|
||||
ctx = setContextOp(ctx, cq.ctx, "Exist")
|
||||
ctx = setContextOp(ctx, cq.ctx, ent.OpQueryExist)
|
||||
switch _, err := cq.FirstID(ctx); {
|
||||
case IsNotFound(err):
|
||||
return false, nil
|
||||
@@ -453,7 +454,7 @@ func (cgb *CardGroupBy) Aggregate(fns ...AggregateFunc) *CardGroupBy {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (cgb *CardGroupBy) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, cgb.build.ctx, "GroupBy")
|
||||
ctx = setContextOp(ctx, cgb.build.ctx, ent.OpQueryGroupBy)
|
||||
if err := cgb.build.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -501,7 +502,7 @@ func (cs *CardSelect) Aggregate(fns ...AggregateFunc) *CardSelect {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (cs *CardSelect) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, cs.ctx, "Select")
|
||||
ctx = setContextOp(ctx, cs.ctx, ent.OpQuerySelect)
|
||||
if err := cs.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"entgo.io/ent"
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"entgo.io/ent/dialect/sql/sqlgraph"
|
||||
"entgo.io/ent/examples/jsonencode/ent/pet"
|
||||
@@ -88,7 +89,7 @@ func (pq *PetQuery) QueryOwner() *UserQuery {
|
||||
// First returns the first Pet entity from the query.
|
||||
// Returns a *NotFoundError when no Pet was found.
|
||||
func (pq *PetQuery) First(ctx context.Context) (*Pet, error) {
|
||||
nodes, err := pq.Limit(1).All(setContextOp(ctx, pq.ctx, "First"))
|
||||
nodes, err := pq.Limit(1).All(setContextOp(ctx, pq.ctx, ent.OpQueryFirst))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -111,7 +112,7 @@ func (pq *PetQuery) FirstX(ctx context.Context) *Pet {
|
||||
// Returns a *NotFoundError when no Pet ID was found.
|
||||
func (pq *PetQuery) FirstID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = pq.Limit(1).IDs(setContextOp(ctx, pq.ctx, "FirstID")); err != nil {
|
||||
if ids, err = pq.Limit(1).IDs(setContextOp(ctx, pq.ctx, ent.OpQueryFirstID)); err != nil {
|
||||
return
|
||||
}
|
||||
if len(ids) == 0 {
|
||||
@@ -134,7 +135,7 @@ func (pq *PetQuery) FirstIDX(ctx context.Context) int {
|
||||
// Returns a *NotSingularError when more than one Pet entity is found.
|
||||
// Returns a *NotFoundError when no Pet entities are found.
|
||||
func (pq *PetQuery) Only(ctx context.Context) (*Pet, error) {
|
||||
nodes, err := pq.Limit(2).All(setContextOp(ctx, pq.ctx, "Only"))
|
||||
nodes, err := pq.Limit(2).All(setContextOp(ctx, pq.ctx, ent.OpQueryOnly))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -162,7 +163,7 @@ func (pq *PetQuery) OnlyX(ctx context.Context) *Pet {
|
||||
// Returns a *NotFoundError when no entities are found.
|
||||
func (pq *PetQuery) OnlyID(ctx context.Context) (id int, err error) {
|
||||
var ids []int
|
||||
if ids, err = pq.Limit(2).IDs(setContextOp(ctx, pq.ctx, "OnlyID")); err != nil {
|
||||
if ids, err = pq.Limit(2).IDs(setContextOp(ctx, pq.ctx, ent.OpQueryOnlyID)); err != nil {
|
||||
return
|
||||
}
|
||||
switch len(ids) {
|
||||
@@ -187,7 +188,7 @@ func (pq *PetQuery) OnlyIDX(ctx context.Context) int {
|
||||
|
||||
// All executes the query and returns a list of Pets.
|
||||
func (pq *PetQuery) All(ctx context.Context) ([]*Pet, error) {
|
||||
ctx = setContextOp(ctx, pq.ctx, "All")
|
||||
ctx = setContextOp(ctx, pq.ctx, ent.OpQueryAll)
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -209,7 +210,7 @@ func (pq *PetQuery) IDs(ctx context.Context) (ids []int, err error) {
|
||||
if pq.ctx.Unique == nil && pq.path != nil {
|
||||
pq.Unique(true)
|
||||
}
|
||||
ctx = setContextOp(ctx, pq.ctx, "IDs")
|
||||
ctx = setContextOp(ctx, pq.ctx, ent.OpQueryIDs)
|
||||
if err = pq.Select(pet.FieldID).Scan(ctx, &ids); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -227,7 +228,7 @@ func (pq *PetQuery) IDsX(ctx context.Context) []int {
|
||||
|
||||
// Count returns the count of the given query.
|
||||
func (pq *PetQuery) Count(ctx context.Context) (int, error) {
|
||||
ctx = setContextOp(ctx, pq.ctx, "Count")
|
||||
ctx = setContextOp(ctx, pq.ctx, ent.OpQueryCount)
|
||||
if err := pq.prepareQuery(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
@@ -245,7 +246,7 @@ func (pq *PetQuery) CountX(ctx context.Context) int {
|
||||
|
||||
// Exist returns true if the query has elements in the graph.
|
||||
func (pq *PetQuery) Exist(ctx context.Context) (bool, error) {
|
||||
ctx = setContextOp(ctx, pq.ctx, "Exist")
|
||||
ctx = setContextOp(ctx, pq.ctx, ent.OpQueryExist)
|
||||
switch _, err := pq.FirstID(ctx); {
|
||||
case IsNotFound(err):
|
||||
return false, nil
|
||||
@@ -532,7 +533,7 @@ func (pgb *PetGroupBy) Aggregate(fns ...AggregateFunc) *PetGroupBy {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (pgb *PetGroupBy) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, pgb.build.ctx, "GroupBy")
|
||||
ctx = setContextOp(ctx, pgb.build.ctx, ent.OpQueryGroupBy)
|
||||
if err := pgb.build.prepareQuery(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -580,7 +581,7 @@ func (ps *PetSelect) Aggregate(fns ...AggregateFunc) *PetSelect {
|
||||
|
||||
// Scan applies the selector query and scans the result into the given value.
|
||||
func (ps *PetSelect) Scan(ctx context.Context, v any) error {
|
||||
ctx = setContextOp(ctx, ps.ctx, "Select")
|
||||
ctx = setContextOp(ctx, ps.ctx, ent.OpQuerySelect)
|
||||
if err := ps.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/jsonencode/ent/pet"
|
||||
@@ -89,7 +90,7 @@ func (uq *UserQuery) QueryPets() *PetQuery {
|
||||
// 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
|
||||
}
|
||||
@@ -112,7 +113,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 {
|
||||
@@ -135,7 +136,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
|
||||
}
|
||||
@@ -163,7 +164,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) {
|
||||
@@ -188,7 +189,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
|
||||
}
|
||||
@@ -210,7 +211,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
|
||||
}
|
||||
@@ -228,7 +229,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
|
||||
}
|
||||
@@ -246,7 +247,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
|
||||
@@ -532,7 +533,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
|
||||
}
|
||||
@@ -580,7 +581,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