mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
* Improve sqlexist template * Address review comment and regenerate files * Regenerate using go1.19 * Run go generate for examples dir * Address review comment * Update entc/gen/template/dialect/sql/query.tmpl Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
1662 lines
48 KiB
Go
1662 lines
48 KiB
Go
// Copyright 2019-present Facebook Inc. All rights reserved.
|
|
// This source code is licensed under the Apache 2.0 license found
|
|
// in the LICENSE file in the root directory of this source tree.
|
|
|
|
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"math"
|
|
|
|
"entgo.io/ent/dialect"
|
|
"entgo.io/ent/dialect/sql"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/entc/integration/ent/card"
|
|
"entgo.io/ent/entc/integration/ent/file"
|
|
"entgo.io/ent/entc/integration/ent/group"
|
|
"entgo.io/ent/entc/integration/ent/pet"
|
|
"entgo.io/ent/entc/integration/ent/predicate"
|
|
"entgo.io/ent/entc/integration/ent/user"
|
|
"entgo.io/ent/schema/field"
|
|
)
|
|
|
|
// UserQuery is the builder for querying User entities.
|
|
type UserQuery struct {
|
|
config
|
|
limit *int
|
|
offset *int
|
|
unique *bool
|
|
order []OrderFunc
|
|
fields []string
|
|
predicates []predicate.User
|
|
withCard *CardQuery
|
|
withPets *PetQuery
|
|
withFiles *FileQuery
|
|
withGroups *GroupQuery
|
|
withFriends *UserQuery
|
|
withFollowers *UserQuery
|
|
withFollowing *UserQuery
|
|
withTeam *PetQuery
|
|
withSpouse *UserQuery
|
|
withChildren *UserQuery
|
|
withParent *UserQuery
|
|
withFKs bool
|
|
modifiers []func(*sql.Selector)
|
|
withNamedPets map[string]*PetQuery
|
|
withNamedFiles map[string]*FileQuery
|
|
withNamedGroups map[string]*GroupQuery
|
|
withNamedFriends map[string]*UserQuery
|
|
withNamedFollowers map[string]*UserQuery
|
|
withNamedFollowing map[string]*UserQuery
|
|
withNamedChildren map[string]*UserQuery
|
|
// intermediate query (i.e. traversal path).
|
|
sql *sql.Selector
|
|
path func(context.Context) (*sql.Selector, error)
|
|
}
|
|
|
|
// Where adds a new predicate for the UserQuery builder.
|
|
func (uq *UserQuery) Where(ps ...predicate.User) *UserQuery {
|
|
uq.predicates = append(uq.predicates, ps...)
|
|
return uq
|
|
}
|
|
|
|
// Limit adds a limit step to the query.
|
|
func (uq *UserQuery) Limit(limit int) *UserQuery {
|
|
uq.limit = &limit
|
|
return uq
|
|
}
|
|
|
|
// Offset adds an offset step to the query.
|
|
func (uq *UserQuery) Offset(offset int) *UserQuery {
|
|
uq.offset = &offset
|
|
return uq
|
|
}
|
|
|
|
// 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 (uq *UserQuery) Unique(unique bool) *UserQuery {
|
|
uq.unique = &unique
|
|
return uq
|
|
}
|
|
|
|
// Order adds an order step to the query.
|
|
func (uq *UserQuery) Order(o ...OrderFunc) *UserQuery {
|
|
uq.order = append(uq.order, o...)
|
|
return uq
|
|
}
|
|
|
|
// QueryCard chains the current query on the "card" edge.
|
|
func (uq *UserQuery) QueryCard() *CardQuery {
|
|
query := &CardQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(card.Table, card.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2O, false, user.CardTable, user.CardColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryPets chains the current query on the "pets" edge.
|
|
func (uq *UserQuery) QueryPets() *PetQuery {
|
|
query := &PetQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(pet.Table, pet.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, false, user.PetsTable, user.PetsColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryFiles chains the current query on the "files" edge.
|
|
func (uq *UserQuery) QueryFiles() *FileQuery {
|
|
query := &FileQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(file.Table, file.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, false, user.FilesTable, user.FilesColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryGroups chains the current query on the "groups" edge.
|
|
func (uq *UserQuery) QueryGroups() *GroupQuery {
|
|
query := &GroupQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(group.Table, group.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2M, false, user.GroupsTable, user.GroupsPrimaryKey...),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryFriends chains the current query on the "friends" edge.
|
|
func (uq *UserQuery) QueryFriends() *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(user.Table, user.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2M, false, user.FriendsTable, user.FriendsPrimaryKey...),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryFollowers chains the current query on the "followers" edge.
|
|
func (uq *UserQuery) QueryFollowers() *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(user.Table, user.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2M, true, user.FollowersTable, user.FollowersPrimaryKey...),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryFollowing chains the current query on the "following" edge.
|
|
func (uq *UserQuery) QueryFollowing() *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(user.Table, user.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2M, false, user.FollowingTable, user.FollowingPrimaryKey...),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryTeam chains the current query on the "team" edge.
|
|
func (uq *UserQuery) QueryTeam() *PetQuery {
|
|
query := &PetQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(pet.Table, pet.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2O, false, user.TeamTable, user.TeamColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QuerySpouse chains the current query on the "spouse" edge.
|
|
func (uq *UserQuery) QuerySpouse() *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(user.Table, user.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2O, false, user.SpouseTable, user.SpouseColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryChildren chains the current query on the "children" edge.
|
|
func (uq *UserQuery) QueryChildren() *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(user.Table, user.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, true, user.ChildrenTable, user.ChildrenColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryParent chains the current query on the "parent" edge.
|
|
func (uq *UserQuery) QueryParent() *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := uq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(user.Table, user.FieldID, selector),
|
|
sqlgraph.To(user.Table, user.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2O, false, user.ParentTable, user.ParentColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// 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(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nil, &NotFoundError{user.Label}
|
|
}
|
|
return nodes[0], nil
|
|
}
|
|
|
|
// FirstX is like First, but panics if an error occurs.
|
|
func (uq *UserQuery) FirstX(ctx context.Context) *User {
|
|
node, err := uq.First(ctx)
|
|
if err != nil && !IsNotFound(err) {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// FirstID returns the first User ID from the query.
|
|
// 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(ctx); err != nil {
|
|
return
|
|
}
|
|
if len(ids) == 0 {
|
|
err = &NotFoundError{user.Label}
|
|
return
|
|
}
|
|
return ids[0], nil
|
|
}
|
|
|
|
// FirstIDX is like FirstID, but panics if an error occurs.
|
|
func (uq *UserQuery) FirstIDX(ctx context.Context) int {
|
|
id, err := uq.FirstID(ctx)
|
|
if err != nil && !IsNotFound(err) {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// Only returns a single User entity found by the query, ensuring it only returns one.
|
|
// 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(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch len(nodes) {
|
|
case 1:
|
|
return nodes[0], nil
|
|
case 0:
|
|
return nil, &NotFoundError{user.Label}
|
|
default:
|
|
return nil, &NotSingularError{user.Label}
|
|
}
|
|
}
|
|
|
|
// OnlyX is like Only, but panics if an error occurs.
|
|
func (uq *UserQuery) OnlyX(ctx context.Context) *User {
|
|
node, err := uq.Only(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// OnlyID is like Only, but returns the only User ID in the query.
|
|
// Returns a *NotSingularError when more than one User ID is found.
|
|
// 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(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(ids) {
|
|
case 1:
|
|
id = ids[0]
|
|
case 0:
|
|
err = &NotFoundError{user.Label}
|
|
default:
|
|
err = &NotSingularError{user.Label}
|
|
}
|
|
return
|
|
}
|
|
|
|
// OnlyIDX is like OnlyID, but panics if an error occurs.
|
|
func (uq *UserQuery) OnlyIDX(ctx context.Context) int {
|
|
id, err := uq.OnlyID(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// All executes the query and returns a list of Users.
|
|
func (uq *UserQuery) All(ctx context.Context) ([]*User, error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return uq.sqlAll(ctx)
|
|
}
|
|
|
|
// AllX is like All, but panics if an error occurs.
|
|
func (uq *UserQuery) AllX(ctx context.Context) []*User {
|
|
nodes, err := uq.All(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return nodes
|
|
}
|
|
|
|
// IDs executes the query and returns a list of User IDs.
|
|
func (uq *UserQuery) IDs(ctx context.Context) ([]int, error) {
|
|
var ids []int
|
|
if err := uq.Select(user.FieldID).Scan(ctx, &ids); err != nil {
|
|
return nil, err
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
// IDsX is like IDs, but panics if an error occurs.
|
|
func (uq *UserQuery) IDsX(ctx context.Context) []int {
|
|
ids, err := uq.IDs(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// Count returns the count of the given query.
|
|
func (uq *UserQuery) Count(ctx context.Context) (int, error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return 0, err
|
|
}
|
|
return uq.sqlCount(ctx)
|
|
}
|
|
|
|
// CountX is like Count, but panics if an error occurs.
|
|
func (uq *UserQuery) CountX(ctx context.Context) int {
|
|
count, err := uq.Count(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return count
|
|
}
|
|
|
|
// Exist returns true if the query has elements in the graph.
|
|
func (uq *UserQuery) Exist(ctx context.Context) (bool, error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return false, err
|
|
}
|
|
return uq.sqlExist(ctx)
|
|
}
|
|
|
|
// ExistX is like Exist, but panics if an error occurs.
|
|
func (uq *UserQuery) ExistX(ctx context.Context) bool {
|
|
exist, err := uq.Exist(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return exist
|
|
}
|
|
|
|
// Clone returns a duplicate of the UserQuery builder, including all associated steps. It can be
|
|
// used to prepare common query builders and use them differently after the clone is made.
|
|
func (uq *UserQuery) Clone() *UserQuery {
|
|
if uq == nil {
|
|
return nil
|
|
}
|
|
return &UserQuery{
|
|
config: uq.config,
|
|
limit: uq.limit,
|
|
offset: uq.offset,
|
|
order: append([]OrderFunc{}, uq.order...),
|
|
predicates: append([]predicate.User{}, uq.predicates...),
|
|
withCard: uq.withCard.Clone(),
|
|
withPets: uq.withPets.Clone(),
|
|
withFiles: uq.withFiles.Clone(),
|
|
withGroups: uq.withGroups.Clone(),
|
|
withFriends: uq.withFriends.Clone(),
|
|
withFollowers: uq.withFollowers.Clone(),
|
|
withFollowing: uq.withFollowing.Clone(),
|
|
withTeam: uq.withTeam.Clone(),
|
|
withSpouse: uq.withSpouse.Clone(),
|
|
withChildren: uq.withChildren.Clone(),
|
|
withParent: uq.withParent.Clone(),
|
|
// clone intermediate query.
|
|
sql: uq.sql.Clone(),
|
|
path: uq.path,
|
|
unique: uq.unique,
|
|
}
|
|
}
|
|
|
|
// WithCard tells the query-builder to eager-load the nodes that are connected to
|
|
// the "card" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithCard(opts ...func(*CardQuery)) *UserQuery {
|
|
query := &CardQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withCard = query
|
|
return uq
|
|
}
|
|
|
|
// WithPets tells the query-builder to eager-load the nodes that are connected to
|
|
// the "pets" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithPets(opts ...func(*PetQuery)) *UserQuery {
|
|
query := &PetQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withPets = query
|
|
return uq
|
|
}
|
|
|
|
// WithFiles tells the query-builder to eager-load the nodes that are connected to
|
|
// the "files" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithFiles(opts ...func(*FileQuery)) *UserQuery {
|
|
query := &FileQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withFiles = query
|
|
return uq
|
|
}
|
|
|
|
// WithGroups tells the query-builder to eager-load the nodes that are connected to
|
|
// the "groups" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithGroups(opts ...func(*GroupQuery)) *UserQuery {
|
|
query := &GroupQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withGroups = query
|
|
return uq
|
|
}
|
|
|
|
// WithFriends tells the query-builder to eager-load the nodes that are connected to
|
|
// the "friends" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithFriends(opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withFriends = query
|
|
return uq
|
|
}
|
|
|
|
// WithFollowers tells the query-builder to eager-load the nodes that are connected to
|
|
// the "followers" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithFollowers(opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withFollowers = query
|
|
return uq
|
|
}
|
|
|
|
// WithFollowing tells the query-builder to eager-load the nodes that are connected to
|
|
// the "following" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithFollowing(opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withFollowing = query
|
|
return uq
|
|
}
|
|
|
|
// WithTeam tells the query-builder to eager-load the nodes that are connected to
|
|
// the "team" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithTeam(opts ...func(*PetQuery)) *UserQuery {
|
|
query := &PetQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withTeam = query
|
|
return uq
|
|
}
|
|
|
|
// WithSpouse tells the query-builder to eager-load the nodes that are connected to
|
|
// the "spouse" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithSpouse(opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withSpouse = query
|
|
return uq
|
|
}
|
|
|
|
// WithChildren tells the query-builder to eager-load the nodes that are connected to
|
|
// the "children" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithChildren(opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withChildren = query
|
|
return uq
|
|
}
|
|
|
|
// WithParent tells the query-builder to eager-load the nodes that are connected to
|
|
// the "parent" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithParent(opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
uq.withParent = query
|
|
return uq
|
|
}
|
|
|
|
// 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 {
|
|
// OptionalInt int `json:"optional_int,omitempty"`
|
|
// Count int `json:"count,omitempty"`
|
|
// }
|
|
//
|
|
// client.User.Query().
|
|
// GroupBy(user.FieldOptionalInt).
|
|
// Aggregate(ent.Count()).
|
|
// Scan(ctx, &v)
|
|
func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
|
|
grbuild := &UserGroupBy{config: uq.config}
|
|
grbuild.fields = append([]string{field}, fields...)
|
|
grbuild.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
|
if err := uq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return uq.sqlQuery(ctx), nil
|
|
}
|
|
grbuild.label = user.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 {
|
|
// OptionalInt int `json:"optional_int,omitempty"`
|
|
// }
|
|
//
|
|
// client.User.Query().
|
|
// Select(user.FieldOptionalInt).
|
|
// Scan(ctx, &v)
|
|
func (uq *UserQuery) Select(fields ...string) *UserSelect {
|
|
uq.fields = append(uq.fields, fields...)
|
|
selbuild := &UserSelect{UserQuery: uq}
|
|
selbuild.label = user.Label
|
|
selbuild.flds, selbuild.scan = &uq.fields, selbuild.Scan
|
|
return selbuild
|
|
}
|
|
|
|
func (uq *UserQuery) prepareQuery(ctx context.Context) error {
|
|
for _, f := range uq.fields {
|
|
if !user.ValidColumn(f) {
|
|
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
}
|
|
if uq.path != nil {
|
|
prev, err := uq.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
uq.sql = prev
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (uq *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, error) {
|
|
var (
|
|
nodes = []*User{}
|
|
withFKs = uq.withFKs
|
|
_spec = uq.querySpec()
|
|
loadedTypes = [11]bool{
|
|
uq.withCard != nil,
|
|
uq.withPets != nil,
|
|
uq.withFiles != nil,
|
|
uq.withGroups != nil,
|
|
uq.withFriends != nil,
|
|
uq.withFollowers != nil,
|
|
uq.withFollowing != nil,
|
|
uq.withTeam != nil,
|
|
uq.withSpouse != nil,
|
|
uq.withChildren != nil,
|
|
uq.withParent != nil,
|
|
}
|
|
)
|
|
if uq.withSpouse != nil || uq.withParent != nil {
|
|
withFKs = true
|
|
}
|
|
if withFKs {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, user.ForeignKeys...)
|
|
}
|
|
_spec.ScanValues = func(columns []string) ([]any, error) {
|
|
return (*User).scanValues(nil, columns)
|
|
}
|
|
_spec.Assign = func(columns []string, values []any) error {
|
|
node := &User{config: uq.config}
|
|
nodes = append(nodes, node)
|
|
node.Edges.loadedTypes = loadedTypes
|
|
return node.assignValues(columns, values)
|
|
}
|
|
if len(uq.modifiers) > 0 {
|
|
_spec.Modifiers = uq.modifiers
|
|
}
|
|
for i := range hooks {
|
|
hooks[i](ctx, _spec)
|
|
}
|
|
if err := sqlgraph.QueryNodes(ctx, uq.driver, _spec); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nodes, nil
|
|
}
|
|
if query := uq.withCard; query != nil {
|
|
if err := uq.loadCard(ctx, query, nodes, nil,
|
|
func(n *User, e *Card) { n.Edges.Card = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withPets; query != nil {
|
|
if err := uq.loadPets(ctx, query, nodes,
|
|
func(n *User) { n.Edges.Pets = []*Pet{} },
|
|
func(n *User, e *Pet) { n.Edges.Pets = append(n.Edges.Pets, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withFiles; query != nil {
|
|
if err := uq.loadFiles(ctx, query, nodes,
|
|
func(n *User) { n.Edges.Files = []*File{} },
|
|
func(n *User, e *File) { n.Edges.Files = append(n.Edges.Files, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withGroups; query != nil {
|
|
if err := uq.loadGroups(ctx, query, nodes,
|
|
func(n *User) { n.Edges.Groups = []*Group{} },
|
|
func(n *User, e *Group) { n.Edges.Groups = append(n.Edges.Groups, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withFriends; query != nil {
|
|
if err := uq.loadFriends(ctx, query, nodes,
|
|
func(n *User) { n.Edges.Friends = []*User{} },
|
|
func(n *User, e *User) { n.Edges.Friends = append(n.Edges.Friends, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withFollowers; query != nil {
|
|
if err := uq.loadFollowers(ctx, query, nodes,
|
|
func(n *User) { n.Edges.Followers = []*User{} },
|
|
func(n *User, e *User) { n.Edges.Followers = append(n.Edges.Followers, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withFollowing; query != nil {
|
|
if err := uq.loadFollowing(ctx, query, nodes,
|
|
func(n *User) { n.Edges.Following = []*User{} },
|
|
func(n *User, e *User) { n.Edges.Following = append(n.Edges.Following, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withTeam; query != nil {
|
|
if err := uq.loadTeam(ctx, query, nodes, nil,
|
|
func(n *User, e *Pet) { n.Edges.Team = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withSpouse; query != nil {
|
|
if err := uq.loadSpouse(ctx, query, nodes, nil,
|
|
func(n *User, e *User) { n.Edges.Spouse = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withChildren; query != nil {
|
|
if err := uq.loadChildren(ctx, query, nodes,
|
|
func(n *User) { n.Edges.Children = []*User{} },
|
|
func(n *User, e *User) { n.Edges.Children = append(n.Edges.Children, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := uq.withParent; query != nil {
|
|
if err := uq.loadParent(ctx, query, nodes, nil,
|
|
func(n *User, e *User) { n.Edges.Parent = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
for name, query := range uq.withNamedPets {
|
|
if err := uq.loadPets(ctx, query, nodes,
|
|
func(n *User) { n.appendNamedPets(name) },
|
|
func(n *User, e *Pet) { n.appendNamedPets(name, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
for name, query := range uq.withNamedFiles {
|
|
if err := uq.loadFiles(ctx, query, nodes,
|
|
func(n *User) { n.appendNamedFiles(name) },
|
|
func(n *User, e *File) { n.appendNamedFiles(name, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
for name, query := range uq.withNamedGroups {
|
|
if err := uq.loadGroups(ctx, query, nodes,
|
|
func(n *User) { n.appendNamedGroups(name) },
|
|
func(n *User, e *Group) { n.appendNamedGroups(name, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
for name, query := range uq.withNamedFriends {
|
|
if err := uq.loadFriends(ctx, query, nodes,
|
|
func(n *User) { n.appendNamedFriends(name) },
|
|
func(n *User, e *User) { n.appendNamedFriends(name, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
for name, query := range uq.withNamedFollowers {
|
|
if err := uq.loadFollowers(ctx, query, nodes,
|
|
func(n *User) { n.appendNamedFollowers(name) },
|
|
func(n *User, e *User) { n.appendNamedFollowers(name, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
for name, query := range uq.withNamedFollowing {
|
|
if err := uq.loadFollowing(ctx, query, nodes,
|
|
func(n *User) { n.appendNamedFollowing(name) },
|
|
func(n *User, e *User) { n.appendNamedFollowing(name, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
for name, query := range uq.withNamedChildren {
|
|
if err := uq.loadChildren(ctx, query, nodes,
|
|
func(n *User) { n.appendNamedChildren(name) },
|
|
func(n *User, e *User) { n.appendNamedChildren(name, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
func (uq *UserQuery) loadCard(ctx context.Context, query *CardQuery, nodes []*User, init func(*User), assign func(*User, *Card)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[int]*User)
|
|
for i := range nodes {
|
|
fks = append(fks, nodes[i].ID)
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
}
|
|
query.withFKs = true
|
|
query.Where(predicate.Card(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(user.CardColumn, fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.user_card
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "user_card" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected foreign-key "user_card" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadPets(ctx context.Context, query *PetQuery, nodes []*User, init func(*User), assign func(*User, *Pet)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[int]*User)
|
|
for i := range nodes {
|
|
fks = append(fks, nodes[i].ID)
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
if init != nil {
|
|
init(nodes[i])
|
|
}
|
|
}
|
|
query.withFKs = true
|
|
query.Where(predicate.Pet(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(user.PetsColumn, fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.user_pets
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "user_pets" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected foreign-key "user_pets" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadFiles(ctx context.Context, query *FileQuery, nodes []*User, init func(*User), assign func(*User, *File)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[int]*User)
|
|
for i := range nodes {
|
|
fks = append(fks, nodes[i].ID)
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
if init != nil {
|
|
init(nodes[i])
|
|
}
|
|
}
|
|
query.withFKs = true
|
|
query.Where(predicate.File(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(user.FilesColumn, fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.user_files
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "user_files" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected foreign-key "user_files" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadGroups(ctx context.Context, query *GroupQuery, nodes []*User, init func(*User), assign func(*User, *Group)) error {
|
|
edgeIDs := make([]driver.Value, len(nodes))
|
|
byID := make(map[int]*User)
|
|
nids := make(map[int]map[*User]struct{})
|
|
for i, node := range nodes {
|
|
edgeIDs[i] = node.ID
|
|
byID[node.ID] = node
|
|
if init != nil {
|
|
init(node)
|
|
}
|
|
}
|
|
query.Where(func(s *sql.Selector) {
|
|
joinT := sql.Table(user.GroupsTable)
|
|
s.Join(joinT).On(s.C(group.FieldID), joinT.C(user.GroupsPrimaryKey[1]))
|
|
s.Where(sql.InValues(joinT.C(user.GroupsPrimaryKey[0]), edgeIDs...))
|
|
columns := s.SelectedColumns()
|
|
s.Select(joinT.C(user.GroupsPrimaryKey[0]))
|
|
s.AppendSelect(columns...)
|
|
s.SetDistinct(false)
|
|
})
|
|
if err := query.prepareQuery(ctx); err != nil {
|
|
return err
|
|
}
|
|
neighbors, err := query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
|
|
assign := spec.Assign
|
|
values := spec.ScanValues
|
|
spec.ScanValues = func(columns []string) ([]any, error) {
|
|
values, err := values(columns[1:])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append([]any{new(sql.NullInt64)}, values...), nil
|
|
}
|
|
spec.Assign = func(columns []string, values []any) error {
|
|
outValue := int(values[0].(*sql.NullInt64).Int64)
|
|
inValue := int(values[1].(*sql.NullInt64).Int64)
|
|
if nids[inValue] == nil {
|
|
nids[inValue] = map[*User]struct{}{byID[outValue]: struct{}{}}
|
|
return assign(columns[1:], values[1:])
|
|
}
|
|
nids[inValue][byID[outValue]] = struct{}{}
|
|
return nil
|
|
}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
nodes, ok := nids[n.ID]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected "groups" node returned %v`, n.ID)
|
|
}
|
|
for kn := range nodes {
|
|
assign(kn, n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadFriends(ctx context.Context, query *UserQuery, nodes []*User, init func(*User), assign func(*User, *User)) error {
|
|
edgeIDs := make([]driver.Value, len(nodes))
|
|
byID := make(map[int]*User)
|
|
nids := make(map[int]map[*User]struct{})
|
|
for i, node := range nodes {
|
|
edgeIDs[i] = node.ID
|
|
byID[node.ID] = node
|
|
if init != nil {
|
|
init(node)
|
|
}
|
|
}
|
|
query.Where(func(s *sql.Selector) {
|
|
joinT := sql.Table(user.FriendsTable)
|
|
s.Join(joinT).On(s.C(user.FieldID), joinT.C(user.FriendsPrimaryKey[1]))
|
|
s.Where(sql.InValues(joinT.C(user.FriendsPrimaryKey[0]), edgeIDs...))
|
|
columns := s.SelectedColumns()
|
|
s.Select(joinT.C(user.FriendsPrimaryKey[0]))
|
|
s.AppendSelect(columns...)
|
|
s.SetDistinct(false)
|
|
})
|
|
if err := query.prepareQuery(ctx); err != nil {
|
|
return err
|
|
}
|
|
neighbors, err := query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
|
|
assign := spec.Assign
|
|
values := spec.ScanValues
|
|
spec.ScanValues = func(columns []string) ([]any, error) {
|
|
values, err := values(columns[1:])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append([]any{new(sql.NullInt64)}, values...), nil
|
|
}
|
|
spec.Assign = func(columns []string, values []any) error {
|
|
outValue := int(values[0].(*sql.NullInt64).Int64)
|
|
inValue := int(values[1].(*sql.NullInt64).Int64)
|
|
if nids[inValue] == nil {
|
|
nids[inValue] = map[*User]struct{}{byID[outValue]: struct{}{}}
|
|
return assign(columns[1:], values[1:])
|
|
}
|
|
nids[inValue][byID[outValue]] = struct{}{}
|
|
return nil
|
|
}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
nodes, ok := nids[n.ID]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected "friends" node returned %v`, n.ID)
|
|
}
|
|
for kn := range nodes {
|
|
assign(kn, n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadFollowers(ctx context.Context, query *UserQuery, nodes []*User, init func(*User), assign func(*User, *User)) error {
|
|
edgeIDs := make([]driver.Value, len(nodes))
|
|
byID := make(map[int]*User)
|
|
nids := make(map[int]map[*User]struct{})
|
|
for i, node := range nodes {
|
|
edgeIDs[i] = node.ID
|
|
byID[node.ID] = node
|
|
if init != nil {
|
|
init(node)
|
|
}
|
|
}
|
|
query.Where(func(s *sql.Selector) {
|
|
joinT := sql.Table(user.FollowersTable)
|
|
s.Join(joinT).On(s.C(user.FieldID), joinT.C(user.FollowersPrimaryKey[0]))
|
|
s.Where(sql.InValues(joinT.C(user.FollowersPrimaryKey[1]), edgeIDs...))
|
|
columns := s.SelectedColumns()
|
|
s.Select(joinT.C(user.FollowersPrimaryKey[1]))
|
|
s.AppendSelect(columns...)
|
|
s.SetDistinct(false)
|
|
})
|
|
if err := query.prepareQuery(ctx); err != nil {
|
|
return err
|
|
}
|
|
neighbors, err := query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
|
|
assign := spec.Assign
|
|
values := spec.ScanValues
|
|
spec.ScanValues = func(columns []string) ([]any, error) {
|
|
values, err := values(columns[1:])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append([]any{new(sql.NullInt64)}, values...), nil
|
|
}
|
|
spec.Assign = func(columns []string, values []any) error {
|
|
outValue := int(values[0].(*sql.NullInt64).Int64)
|
|
inValue := int(values[1].(*sql.NullInt64).Int64)
|
|
if nids[inValue] == nil {
|
|
nids[inValue] = map[*User]struct{}{byID[outValue]: struct{}{}}
|
|
return assign(columns[1:], values[1:])
|
|
}
|
|
nids[inValue][byID[outValue]] = struct{}{}
|
|
return nil
|
|
}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
nodes, ok := nids[n.ID]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected "followers" node returned %v`, n.ID)
|
|
}
|
|
for kn := range nodes {
|
|
assign(kn, n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadFollowing(ctx context.Context, query *UserQuery, nodes []*User, init func(*User), assign func(*User, *User)) error {
|
|
edgeIDs := make([]driver.Value, len(nodes))
|
|
byID := make(map[int]*User)
|
|
nids := make(map[int]map[*User]struct{})
|
|
for i, node := range nodes {
|
|
edgeIDs[i] = node.ID
|
|
byID[node.ID] = node
|
|
if init != nil {
|
|
init(node)
|
|
}
|
|
}
|
|
query.Where(func(s *sql.Selector) {
|
|
joinT := sql.Table(user.FollowingTable)
|
|
s.Join(joinT).On(s.C(user.FieldID), joinT.C(user.FollowingPrimaryKey[1]))
|
|
s.Where(sql.InValues(joinT.C(user.FollowingPrimaryKey[0]), edgeIDs...))
|
|
columns := s.SelectedColumns()
|
|
s.Select(joinT.C(user.FollowingPrimaryKey[0]))
|
|
s.AppendSelect(columns...)
|
|
s.SetDistinct(false)
|
|
})
|
|
if err := query.prepareQuery(ctx); err != nil {
|
|
return err
|
|
}
|
|
neighbors, err := query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
|
|
assign := spec.Assign
|
|
values := spec.ScanValues
|
|
spec.ScanValues = func(columns []string) ([]any, error) {
|
|
values, err := values(columns[1:])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return append([]any{new(sql.NullInt64)}, values...), nil
|
|
}
|
|
spec.Assign = func(columns []string, values []any) error {
|
|
outValue := int(values[0].(*sql.NullInt64).Int64)
|
|
inValue := int(values[1].(*sql.NullInt64).Int64)
|
|
if nids[inValue] == nil {
|
|
nids[inValue] = map[*User]struct{}{byID[outValue]: struct{}{}}
|
|
return assign(columns[1:], values[1:])
|
|
}
|
|
nids[inValue][byID[outValue]] = struct{}{}
|
|
return nil
|
|
}
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
nodes, ok := nids[n.ID]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected "following" node returned %v`, n.ID)
|
|
}
|
|
for kn := range nodes {
|
|
assign(kn, n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadTeam(ctx context.Context, query *PetQuery, nodes []*User, init func(*User), assign func(*User, *Pet)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[int]*User)
|
|
for i := range nodes {
|
|
fks = append(fks, nodes[i].ID)
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
}
|
|
query.withFKs = true
|
|
query.Where(predicate.Pet(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(user.TeamColumn, fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.user_team
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "user_team" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected foreign-key "user_team" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadSpouse(ctx context.Context, query *UserQuery, nodes []*User, init func(*User), assign func(*User, *User)) error {
|
|
ids := make([]int, 0, len(nodes))
|
|
nodeids := make(map[int][]*User)
|
|
for i := range nodes {
|
|
if nodes[i].user_spouse == nil {
|
|
continue
|
|
}
|
|
fk := *nodes[i].user_spouse
|
|
if _, ok := nodeids[fk]; !ok {
|
|
ids = append(ids, fk)
|
|
}
|
|
nodeids[fk] = append(nodeids[fk], nodes[i])
|
|
}
|
|
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_spouse" returned %v`, n.ID)
|
|
}
|
|
for i := range nodes {
|
|
assign(nodes[i], n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadChildren(ctx context.Context, query *UserQuery, nodes []*User, init func(*User), assign func(*User, *User)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[int]*User)
|
|
for i := range nodes {
|
|
fks = append(fks, nodes[i].ID)
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
if init != nil {
|
|
init(nodes[i])
|
|
}
|
|
}
|
|
query.withFKs = true
|
|
query.Where(predicate.User(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(user.ChildrenColumn, fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.user_parent
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "user_parent" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected foreign-key "user_parent" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (uq *UserQuery) loadParent(ctx context.Context, query *UserQuery, nodes []*User, init func(*User), assign func(*User, *User)) error {
|
|
ids := make([]int, 0, len(nodes))
|
|
nodeids := make(map[int][]*User)
|
|
for i := range nodes {
|
|
if nodes[i].user_parent == nil {
|
|
continue
|
|
}
|
|
fk := *nodes[i].user_parent
|
|
if _, ok := nodeids[fk]; !ok {
|
|
ids = append(ids, fk)
|
|
}
|
|
nodeids[fk] = append(nodeids[fk], nodes[i])
|
|
}
|
|
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_parent" returned %v`, n.ID)
|
|
}
|
|
for i := range nodes {
|
|
assign(nodes[i], n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (uq *UserQuery) sqlCount(ctx context.Context) (int, error) {
|
|
_spec := uq.querySpec()
|
|
if len(uq.modifiers) > 0 {
|
|
_spec.Modifiers = uq.modifiers
|
|
}
|
|
_spec.Node.Columns = uq.fields
|
|
if len(uq.fields) > 0 {
|
|
_spec.Unique = uq.unique != nil && *uq.unique
|
|
}
|
|
return sqlgraph.CountNodes(ctx, uq.driver, _spec)
|
|
}
|
|
|
|
func (uq *UserQuery) sqlExist(ctx context.Context) (bool, error) {
|
|
switch _, err := uq.FirstID(ctx); {
|
|
case IsNotFound(err):
|
|
return false, nil
|
|
case err != nil:
|
|
return false, fmt.Errorf("ent: check existence: %w", err)
|
|
default:
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
func (uq *UserQuery) querySpec() *sqlgraph.QuerySpec {
|
|
_spec := &sqlgraph.QuerySpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: user.Table,
|
|
Columns: user.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: user.FieldID,
|
|
},
|
|
},
|
|
From: uq.sql,
|
|
Unique: true,
|
|
}
|
|
if unique := uq.unique; unique != nil {
|
|
_spec.Unique = *unique
|
|
}
|
|
if fields := uq.fields; len(fields) > 0 {
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
_spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
|
|
for i := range fields {
|
|
if fields[i] != user.FieldID {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
|
|
}
|
|
}
|
|
}
|
|
if ps := uq.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if limit := uq.limit; limit != nil {
|
|
_spec.Limit = *limit
|
|
}
|
|
if offset := uq.offset; offset != nil {
|
|
_spec.Offset = *offset
|
|
}
|
|
if ps := uq.order; len(ps) > 0 {
|
|
_spec.Order = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
return _spec
|
|
}
|
|
|
|
func (uq *UserQuery) sqlQuery(ctx context.Context) *sql.Selector {
|
|
builder := sql.Dialect(uq.driver.Dialect())
|
|
t1 := builder.Table(user.Table)
|
|
columns := uq.fields
|
|
if len(columns) == 0 {
|
|
columns = user.Columns
|
|
}
|
|
selector := builder.Select(t1.Columns(columns...)...).From(t1)
|
|
if uq.sql != nil {
|
|
selector = uq.sql
|
|
selector.Select(selector.Columns(columns...)...)
|
|
}
|
|
if uq.unique != nil && *uq.unique {
|
|
selector.Distinct()
|
|
}
|
|
for _, m := range uq.modifiers {
|
|
m(selector)
|
|
}
|
|
for _, p := range uq.predicates {
|
|
p(selector)
|
|
}
|
|
for _, p := range uq.order {
|
|
p(selector)
|
|
}
|
|
if offset := uq.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 := uq.limit; limit != nil {
|
|
selector.Limit(*limit)
|
|
}
|
|
return selector
|
|
}
|
|
|
|
// ForUpdate locks the selected rows against concurrent updates, and prevent them from being
|
|
// updated, deleted or "selected ... for update" by other sessions, until the transaction is
|
|
// either committed or rolled-back.
|
|
func (uq *UserQuery) ForUpdate(opts ...sql.LockOption) *UserQuery {
|
|
if uq.driver.Dialect() == dialect.Postgres {
|
|
uq.Unique(false)
|
|
}
|
|
uq.modifiers = append(uq.modifiers, func(s *sql.Selector) {
|
|
s.ForUpdate(opts...)
|
|
})
|
|
return uq
|
|
}
|
|
|
|
// ForShare behaves similarly to ForUpdate, except that it acquires a shared mode lock
|
|
// on any rows that are read. Other sessions can read the rows, but cannot modify them
|
|
// until your transaction commits.
|
|
func (uq *UserQuery) ForShare(opts ...sql.LockOption) *UserQuery {
|
|
if uq.driver.Dialect() == dialect.Postgres {
|
|
uq.Unique(false)
|
|
}
|
|
uq.modifiers = append(uq.modifiers, func(s *sql.Selector) {
|
|
s.ForShare(opts...)
|
|
})
|
|
return uq
|
|
}
|
|
|
|
// Modify adds a query modifier for attaching custom logic to queries.
|
|
func (uq *UserQuery) Modify(modifiers ...func(s *sql.Selector)) *UserSelect {
|
|
uq.modifiers = append(uq.modifiers, modifiers...)
|
|
return uq.Select()
|
|
}
|
|
|
|
// WithNamedPets tells the query-builder to eager-load the nodes that are connected to the "pets"
|
|
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithNamedPets(name string, opts ...func(*PetQuery)) *UserQuery {
|
|
query := &PetQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
if uq.withNamedPets == nil {
|
|
uq.withNamedPets = make(map[string]*PetQuery)
|
|
}
|
|
uq.withNamedPets[name] = query
|
|
return uq
|
|
}
|
|
|
|
// WithNamedFiles tells the query-builder to eager-load the nodes that are connected to the "files"
|
|
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithNamedFiles(name string, opts ...func(*FileQuery)) *UserQuery {
|
|
query := &FileQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
if uq.withNamedFiles == nil {
|
|
uq.withNamedFiles = make(map[string]*FileQuery)
|
|
}
|
|
uq.withNamedFiles[name] = query
|
|
return uq
|
|
}
|
|
|
|
// WithNamedGroups tells the query-builder to eager-load the nodes that are connected to the "groups"
|
|
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithNamedGroups(name string, opts ...func(*GroupQuery)) *UserQuery {
|
|
query := &GroupQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
if uq.withNamedGroups == nil {
|
|
uq.withNamedGroups = make(map[string]*GroupQuery)
|
|
}
|
|
uq.withNamedGroups[name] = query
|
|
return uq
|
|
}
|
|
|
|
// WithNamedFriends tells the query-builder to eager-load the nodes that are connected to the "friends"
|
|
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithNamedFriends(name string, opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
if uq.withNamedFriends == nil {
|
|
uq.withNamedFriends = make(map[string]*UserQuery)
|
|
}
|
|
uq.withNamedFriends[name] = query
|
|
return uq
|
|
}
|
|
|
|
// WithNamedFollowers tells the query-builder to eager-load the nodes that are connected to the "followers"
|
|
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithNamedFollowers(name string, opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
if uq.withNamedFollowers == nil {
|
|
uq.withNamedFollowers = make(map[string]*UserQuery)
|
|
}
|
|
uq.withNamedFollowers[name] = query
|
|
return uq
|
|
}
|
|
|
|
// WithNamedFollowing tells the query-builder to eager-load the nodes that are connected to the "following"
|
|
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithNamedFollowing(name string, opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
if uq.withNamedFollowing == nil {
|
|
uq.withNamedFollowing = make(map[string]*UserQuery)
|
|
}
|
|
uq.withNamedFollowing[name] = query
|
|
return uq
|
|
}
|
|
|
|
// WithNamedChildren tells the query-builder to eager-load the nodes that are connected to the "children"
|
|
// edge with the given name. The optional arguments are used to configure the query builder of the edge.
|
|
func (uq *UserQuery) WithNamedChildren(name string, opts ...func(*UserQuery)) *UserQuery {
|
|
query := &UserQuery{config: uq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
if uq.withNamedChildren == nil {
|
|
uq.withNamedChildren = make(map[string]*UserQuery)
|
|
}
|
|
uq.withNamedChildren[name] = query
|
|
return uq
|
|
}
|
|
|
|
// UserGroupBy is the group-by builder for User entities.
|
|
type UserGroupBy 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 (ugb *UserGroupBy) Aggregate(fns ...AggregateFunc) *UserGroupBy {
|
|
ugb.fns = append(ugb.fns, fns...)
|
|
return ugb
|
|
}
|
|
|
|
// Scan applies the group-by query and scans the result into the given value.
|
|
func (ugb *UserGroupBy) Scan(ctx context.Context, v any) error {
|
|
query, err := ugb.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ugb.sql = query
|
|
return ugb.sqlScan(ctx, v)
|
|
}
|
|
|
|
func (ugb *UserGroupBy) sqlScan(ctx context.Context, v any) error {
|
|
for _, f := range ugb.fields {
|
|
if !user.ValidColumn(f) {
|
|
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
|
|
}
|
|
}
|
|
selector := ugb.sqlQuery()
|
|
if err := selector.Err(); err != nil {
|
|
return err
|
|
}
|
|
rows := &sql.Rows{}
|
|
query, args := selector.Query()
|
|
if err := ugb.driver.Query(ctx, query, args, rows); err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
return sql.ScanSlice(rows, v)
|
|
}
|
|
|
|
func (ugb *UserGroupBy) sqlQuery() *sql.Selector {
|
|
selector := ugb.sql.Select()
|
|
aggregation := make([]string, 0, len(ugb.fns))
|
|
for _, fn := range ugb.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(ugb.fields)+len(ugb.fns))
|
|
for _, f := range ugb.fields {
|
|
columns = append(columns, selector.C(f))
|
|
}
|
|
columns = append(columns, aggregation...)
|
|
selector.Select(columns...)
|
|
}
|
|
return selector.GroupBy(selector.Columns(ugb.fields...)...)
|
|
}
|
|
|
|
// UserSelect is the builder for selecting fields of User entities.
|
|
type UserSelect struct {
|
|
*UserQuery
|
|
selector
|
|
// intermediate query (i.e. traversal path).
|
|
sql *sql.Selector
|
|
}
|
|
|
|
// Scan applies the selector query and scans the result into the given value.
|
|
func (us *UserSelect) Scan(ctx context.Context, v any) error {
|
|
if err := us.prepareQuery(ctx); err != nil {
|
|
return err
|
|
}
|
|
us.sql = us.UserQuery.sqlQuery(ctx)
|
|
return us.sqlScan(ctx, v)
|
|
}
|
|
|
|
func (us *UserSelect) sqlScan(ctx context.Context, v any) error {
|
|
rows := &sql.Rows{}
|
|
query, args := us.sql.Query()
|
|
if err := us.driver.Query(ctx, query, args, rows); err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
return sql.ScanSlice(rows, v)
|
|
}
|
|
|
|
// Modify adds a query modifier for attaching custom logic to queries.
|
|
func (us *UserSelect) Modify(modifiers ...func(s *sql.Selector)) *UserSelect {
|
|
us.modifiers = append(us.modifiers, modifiers...)
|
|
return us
|
|
}
|