mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2026-01-01 10:37:22 +01:00
1315 lines
40 KiB
Go
1315 lines
40 KiB
Go
// Code generated by ent, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"database/sql/driver"
|
|
"fmt"
|
|
"math"
|
|
|
|
"entgo.io/ent"
|
|
"entgo.io/ent/dialect/sql"
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
"entgo.io/ent/schema/field"
|
|
"github.com/google/uuid"
|
|
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/attachment"
|
|
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/entity"
|
|
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/entityfield"
|
|
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/entitytype"
|
|
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/group"
|
|
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/label"
|
|
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/maintenanceentry"
|
|
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/predicate"
|
|
)
|
|
|
|
// EntityQuery is the builder for querying Entity entities.
|
|
type EntityQuery struct {
|
|
config
|
|
ctx *QueryContext
|
|
order []entity.OrderOption
|
|
inters []Interceptor
|
|
predicates []predicate.Entity
|
|
withGroup *GroupQuery
|
|
withParent *EntityQuery
|
|
withChildren *EntityQuery
|
|
withEntity *EntityQuery
|
|
withLocation *EntityQuery
|
|
withLabel *LabelQuery
|
|
withType *EntityTypeQuery
|
|
withFields *EntityFieldQuery
|
|
withMaintenanceEntries *MaintenanceEntryQuery
|
|
withAttachments *AttachmentQuery
|
|
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 EntityQuery builder.
|
|
func (eq *EntityQuery) Where(ps ...predicate.Entity) *EntityQuery {
|
|
eq.predicates = append(eq.predicates, ps...)
|
|
return eq
|
|
}
|
|
|
|
// Limit the number of records to be returned by this query.
|
|
func (eq *EntityQuery) Limit(limit int) *EntityQuery {
|
|
eq.ctx.Limit = &limit
|
|
return eq
|
|
}
|
|
|
|
// Offset to start from.
|
|
func (eq *EntityQuery) Offset(offset int) *EntityQuery {
|
|
eq.ctx.Offset = &offset
|
|
return eq
|
|
}
|
|
|
|
// 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 (eq *EntityQuery) Unique(unique bool) *EntityQuery {
|
|
eq.ctx.Unique = &unique
|
|
return eq
|
|
}
|
|
|
|
// Order specifies how the records should be ordered.
|
|
func (eq *EntityQuery) Order(o ...entity.OrderOption) *EntityQuery {
|
|
eq.order = append(eq.order, o...)
|
|
return eq
|
|
}
|
|
|
|
// QueryGroup chains the current query on the "group" edge.
|
|
func (eq *EntityQuery) QueryGroup() *GroupQuery {
|
|
query := (&GroupClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(group.Table, group.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2O, true, entity.GroupTable, entity.GroupColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryParent chains the current query on the "parent" edge.
|
|
func (eq *EntityQuery) QueryParent() *EntityQuery {
|
|
query := (&EntityClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(entity.Table, entity.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2O, true, entity.ParentTable, entity.ParentColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryChildren chains the current query on the "children" edge.
|
|
func (eq *EntityQuery) QueryChildren() *EntityQuery {
|
|
query := (&EntityClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(entity.Table, entity.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, false, entity.ChildrenTable, entity.ChildrenColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryEntity chains the current query on the "entity" edge.
|
|
func (eq *EntityQuery) QueryEntity() *EntityQuery {
|
|
query := (&EntityClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(entity.Table, entity.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2O, true, entity.EntityTable, entity.EntityColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryLocation chains the current query on the "location" edge.
|
|
func (eq *EntityQuery) QueryLocation() *EntityQuery {
|
|
query := (&EntityClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(entity.Table, entity.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2O, false, entity.LocationTable, entity.LocationColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryLabel chains the current query on the "label" edge.
|
|
func (eq *EntityQuery) QueryLabel() *LabelQuery {
|
|
query := (&LabelClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(label.Table, label.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2M, true, entity.LabelTable, entity.LabelPrimaryKey...),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryType chains the current query on the "type" edge.
|
|
func (eq *EntityQuery) QueryType() *EntityTypeQuery {
|
|
query := (&EntityTypeClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(entitytype.Table, entitytype.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2O, true, entity.TypeTable, entity.TypeColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryFields chains the current query on the "fields" edge.
|
|
func (eq *EntityQuery) QueryFields() *EntityFieldQuery {
|
|
query := (&EntityFieldClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(entityfield.Table, entityfield.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, false, entity.FieldsTable, entity.FieldsColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryMaintenanceEntries chains the current query on the "maintenance_entries" edge.
|
|
func (eq *EntityQuery) QueryMaintenanceEntries() *MaintenanceEntryQuery {
|
|
query := (&MaintenanceEntryClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(maintenanceentry.Table, maintenanceentry.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, false, entity.MaintenanceEntriesTable, entity.MaintenanceEntriesColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryAttachments chains the current query on the "attachments" edge.
|
|
func (eq *EntityQuery) QueryAttachments() *AttachmentQuery {
|
|
query := (&AttachmentClient{config: eq.config}).Query()
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := eq.sqlQuery(ctx)
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(entity.Table, entity.FieldID, selector),
|
|
sqlgraph.To(attachment.Table, attachment.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, false, entity.AttachmentsTable, entity.AttachmentsColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(eq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// First returns the first Entity entity from the query.
|
|
// Returns a *NotFoundError when no Entity was found.
|
|
func (eq *EntityQuery) First(ctx context.Context) (*Entity, error) {
|
|
nodes, err := eq.Limit(1).All(setContextOp(ctx, eq.ctx, ent.OpQueryFirst))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nil, &NotFoundError{entity.Label}
|
|
}
|
|
return nodes[0], nil
|
|
}
|
|
|
|
// FirstX is like First, but panics if an error occurs.
|
|
func (eq *EntityQuery) FirstX(ctx context.Context) *Entity {
|
|
node, err := eq.First(ctx)
|
|
if err != nil && !IsNotFound(err) {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// FirstID returns the first Entity ID from the query.
|
|
// Returns a *NotFoundError when no Entity ID was found.
|
|
func (eq *EntityQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) {
|
|
var ids []uuid.UUID
|
|
if ids, err = eq.Limit(1).IDs(setContextOp(ctx, eq.ctx, ent.OpQueryFirstID)); err != nil {
|
|
return
|
|
}
|
|
if len(ids) == 0 {
|
|
err = &NotFoundError{entity.Label}
|
|
return
|
|
}
|
|
return ids[0], nil
|
|
}
|
|
|
|
// FirstIDX is like FirstID, but panics if an error occurs.
|
|
func (eq *EntityQuery) FirstIDX(ctx context.Context) uuid.UUID {
|
|
id, err := eq.FirstID(ctx)
|
|
if err != nil && !IsNotFound(err) {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// Only returns a single Entity entity found by the query, ensuring it only returns one.
|
|
// Returns a *NotSingularError when more than one Entity entity is found.
|
|
// Returns a *NotFoundError when no Entity entities are found.
|
|
func (eq *EntityQuery) Only(ctx context.Context) (*Entity, error) {
|
|
nodes, err := eq.Limit(2).All(setContextOp(ctx, eq.ctx, ent.OpQueryOnly))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch len(nodes) {
|
|
case 1:
|
|
return nodes[0], nil
|
|
case 0:
|
|
return nil, &NotFoundError{entity.Label}
|
|
default:
|
|
return nil, &NotSingularError{entity.Label}
|
|
}
|
|
}
|
|
|
|
// OnlyX is like Only, but panics if an error occurs.
|
|
func (eq *EntityQuery) OnlyX(ctx context.Context) *Entity {
|
|
node, err := eq.Only(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// OnlyID is like Only, but returns the only Entity ID in the query.
|
|
// Returns a *NotSingularError when more than one Entity ID is found.
|
|
// Returns a *NotFoundError when no entities are found.
|
|
func (eq *EntityQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) {
|
|
var ids []uuid.UUID
|
|
if ids, err = eq.Limit(2).IDs(setContextOp(ctx, eq.ctx, ent.OpQueryOnlyID)); err != nil {
|
|
return
|
|
}
|
|
switch len(ids) {
|
|
case 1:
|
|
id = ids[0]
|
|
case 0:
|
|
err = &NotFoundError{entity.Label}
|
|
default:
|
|
err = &NotSingularError{entity.Label}
|
|
}
|
|
return
|
|
}
|
|
|
|
// OnlyIDX is like OnlyID, but panics if an error occurs.
|
|
func (eq *EntityQuery) OnlyIDX(ctx context.Context) uuid.UUID {
|
|
id, err := eq.OnlyID(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// All executes the query and returns a list of Entities.
|
|
func (eq *EntityQuery) All(ctx context.Context) ([]*Entity, error) {
|
|
ctx = setContextOp(ctx, eq.ctx, ent.OpQueryAll)
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
qr := querierAll[[]*Entity, *EntityQuery]()
|
|
return withInterceptors[[]*Entity](ctx, eq, qr, eq.inters)
|
|
}
|
|
|
|
// AllX is like All, but panics if an error occurs.
|
|
func (eq *EntityQuery) AllX(ctx context.Context) []*Entity {
|
|
nodes, err := eq.All(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return nodes
|
|
}
|
|
|
|
// IDs executes the query and returns a list of Entity IDs.
|
|
func (eq *EntityQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) {
|
|
if eq.ctx.Unique == nil && eq.path != nil {
|
|
eq.Unique(true)
|
|
}
|
|
ctx = setContextOp(ctx, eq.ctx, ent.OpQueryIDs)
|
|
if err = eq.Select(entity.FieldID).Scan(ctx, &ids); err != nil {
|
|
return nil, err
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
// IDsX is like IDs, but panics if an error occurs.
|
|
func (eq *EntityQuery) IDsX(ctx context.Context) []uuid.UUID {
|
|
ids, err := eq.IDs(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// Count returns the count of the given query.
|
|
func (eq *EntityQuery) Count(ctx context.Context) (int, error) {
|
|
ctx = setContextOp(ctx, eq.ctx, ent.OpQueryCount)
|
|
if err := eq.prepareQuery(ctx); err != nil {
|
|
return 0, err
|
|
}
|
|
return withInterceptors[int](ctx, eq, querierCount[*EntityQuery](), eq.inters)
|
|
}
|
|
|
|
// CountX is like Count, but panics if an error occurs.
|
|
func (eq *EntityQuery) CountX(ctx context.Context) int {
|
|
count, err := eq.Count(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return count
|
|
}
|
|
|
|
// Exist returns true if the query has elements in the graph.
|
|
func (eq *EntityQuery) Exist(ctx context.Context) (bool, error) {
|
|
ctx = setContextOp(ctx, eq.ctx, ent.OpQueryExist)
|
|
switch _, err := eq.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 (eq *EntityQuery) ExistX(ctx context.Context) bool {
|
|
exist, err := eq.Exist(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return exist
|
|
}
|
|
|
|
// Clone returns a duplicate of the EntityQuery builder, including all associated steps. It can be
|
|
// used to prepare common query builders and use them differently after the clone is made.
|
|
func (eq *EntityQuery) Clone() *EntityQuery {
|
|
if eq == nil {
|
|
return nil
|
|
}
|
|
return &EntityQuery{
|
|
config: eq.config,
|
|
ctx: eq.ctx.Clone(),
|
|
order: append([]entity.OrderOption{}, eq.order...),
|
|
inters: append([]Interceptor{}, eq.inters...),
|
|
predicates: append([]predicate.Entity{}, eq.predicates...),
|
|
withGroup: eq.withGroup.Clone(),
|
|
withParent: eq.withParent.Clone(),
|
|
withChildren: eq.withChildren.Clone(),
|
|
withEntity: eq.withEntity.Clone(),
|
|
withLocation: eq.withLocation.Clone(),
|
|
withLabel: eq.withLabel.Clone(),
|
|
withType: eq.withType.Clone(),
|
|
withFields: eq.withFields.Clone(),
|
|
withMaintenanceEntries: eq.withMaintenanceEntries.Clone(),
|
|
withAttachments: eq.withAttachments.Clone(),
|
|
// clone intermediate query.
|
|
sql: eq.sql.Clone(),
|
|
path: eq.path,
|
|
}
|
|
}
|
|
|
|
// WithGroup tells the query-builder to eager-load the nodes that are connected to
|
|
// the "group" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (eq *EntityQuery) WithGroup(opts ...func(*GroupQuery)) *EntityQuery {
|
|
query := (&GroupClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withGroup = query
|
|
return eq
|
|
}
|
|
|
|
// 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 (eq *EntityQuery) WithParent(opts ...func(*EntityQuery)) *EntityQuery {
|
|
query := (&EntityClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withParent = query
|
|
return eq
|
|
}
|
|
|
|
// 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 (eq *EntityQuery) WithChildren(opts ...func(*EntityQuery)) *EntityQuery {
|
|
query := (&EntityClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withChildren = query
|
|
return eq
|
|
}
|
|
|
|
// WithEntity tells the query-builder to eager-load the nodes that are connected to
|
|
// the "entity" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (eq *EntityQuery) WithEntity(opts ...func(*EntityQuery)) *EntityQuery {
|
|
query := (&EntityClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withEntity = query
|
|
return eq
|
|
}
|
|
|
|
// WithLocation tells the query-builder to eager-load the nodes that are connected to
|
|
// the "location" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (eq *EntityQuery) WithLocation(opts ...func(*EntityQuery)) *EntityQuery {
|
|
query := (&EntityClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withLocation = query
|
|
return eq
|
|
}
|
|
|
|
// WithLabel tells the query-builder to eager-load the nodes that are connected to
|
|
// the "label" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (eq *EntityQuery) WithLabel(opts ...func(*LabelQuery)) *EntityQuery {
|
|
query := (&LabelClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withLabel = query
|
|
return eq
|
|
}
|
|
|
|
// WithType tells the query-builder to eager-load the nodes that are connected to
|
|
// the "type" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (eq *EntityQuery) WithType(opts ...func(*EntityTypeQuery)) *EntityQuery {
|
|
query := (&EntityTypeClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withType = query
|
|
return eq
|
|
}
|
|
|
|
// WithFields tells the query-builder to eager-load the nodes that are connected to
|
|
// the "fields" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (eq *EntityQuery) WithFields(opts ...func(*EntityFieldQuery)) *EntityQuery {
|
|
query := (&EntityFieldClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withFields = query
|
|
return eq
|
|
}
|
|
|
|
// WithMaintenanceEntries tells the query-builder to eager-load the nodes that are connected to
|
|
// the "maintenance_entries" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (eq *EntityQuery) WithMaintenanceEntries(opts ...func(*MaintenanceEntryQuery)) *EntityQuery {
|
|
query := (&MaintenanceEntryClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withMaintenanceEntries = query
|
|
return eq
|
|
}
|
|
|
|
// WithAttachments tells the query-builder to eager-load the nodes that are connected to
|
|
// the "attachments" edge. The optional arguments are used to configure the query builder of the edge.
|
|
func (eq *EntityQuery) WithAttachments(opts ...func(*AttachmentQuery)) *EntityQuery {
|
|
query := (&AttachmentClient{config: eq.config}).Query()
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
eq.withAttachments = query
|
|
return eq
|
|
}
|
|
|
|
// GroupBy is used to group vertices by one or more fields/columns.
|
|
// It is often used with aggregate functions, like: count, max, mean, min, sum.
|
|
//
|
|
// Example:
|
|
//
|
|
// var v []struct {
|
|
// CreatedAt time.Time `json:"created_at,omitempty"`
|
|
// Count int `json:"count,omitempty"`
|
|
// }
|
|
//
|
|
// client.Entity.Query().
|
|
// GroupBy(entity.FieldCreatedAt).
|
|
// Aggregate(ent.Count()).
|
|
// Scan(ctx, &v)
|
|
func (eq *EntityQuery) GroupBy(field string, fields ...string) *EntityGroupBy {
|
|
eq.ctx.Fields = append([]string{field}, fields...)
|
|
grbuild := &EntityGroupBy{build: eq}
|
|
grbuild.flds = &eq.ctx.Fields
|
|
grbuild.label = entity.Label
|
|
grbuild.scan = grbuild.Scan
|
|
return grbuild
|
|
}
|
|
|
|
// Select allows the selection one or more fields/columns for the given query,
|
|
// instead of selecting all fields in the entity.
|
|
//
|
|
// Example:
|
|
//
|
|
// var v []struct {
|
|
// CreatedAt time.Time `json:"created_at,omitempty"`
|
|
// }
|
|
//
|
|
// client.Entity.Query().
|
|
// Select(entity.FieldCreatedAt).
|
|
// Scan(ctx, &v)
|
|
func (eq *EntityQuery) Select(fields ...string) *EntitySelect {
|
|
eq.ctx.Fields = append(eq.ctx.Fields, fields...)
|
|
sbuild := &EntitySelect{EntityQuery: eq}
|
|
sbuild.label = entity.Label
|
|
sbuild.flds, sbuild.scan = &eq.ctx.Fields, sbuild.Scan
|
|
return sbuild
|
|
}
|
|
|
|
// Aggregate returns a EntitySelect configured with the given aggregations.
|
|
func (eq *EntityQuery) Aggregate(fns ...AggregateFunc) *EntitySelect {
|
|
return eq.Select().Aggregate(fns...)
|
|
}
|
|
|
|
func (eq *EntityQuery) prepareQuery(ctx context.Context) error {
|
|
for _, inter := range eq.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, eq); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
for _, f := range eq.ctx.Fields {
|
|
if !entity.ValidColumn(f) {
|
|
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
|
|
}
|
|
}
|
|
if eq.path != nil {
|
|
prev, err := eq.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
eq.sql = prev
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (eq *EntityQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Entity, error) {
|
|
var (
|
|
nodes = []*Entity{}
|
|
withFKs = eq.withFKs
|
|
_spec = eq.querySpec()
|
|
loadedTypes = [10]bool{
|
|
eq.withGroup != nil,
|
|
eq.withParent != nil,
|
|
eq.withChildren != nil,
|
|
eq.withEntity != nil,
|
|
eq.withLocation != nil,
|
|
eq.withLabel != nil,
|
|
eq.withType != nil,
|
|
eq.withFields != nil,
|
|
eq.withMaintenanceEntries != nil,
|
|
eq.withAttachments != nil,
|
|
}
|
|
)
|
|
if eq.withGroup != nil || eq.withParent != nil || eq.withEntity != nil || eq.withType != nil {
|
|
withFKs = true
|
|
}
|
|
if withFKs {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, entity.ForeignKeys...)
|
|
}
|
|
_spec.ScanValues = func(columns []string) ([]any, error) {
|
|
return (*Entity).scanValues(nil, columns)
|
|
}
|
|
_spec.Assign = func(columns []string, values []any) error {
|
|
node := &Entity{config: eq.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, eq.driver, _spec); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nodes, nil
|
|
}
|
|
if query := eq.withGroup; query != nil {
|
|
if err := eq.loadGroup(ctx, query, nodes, nil,
|
|
func(n *Entity, e *Group) { n.Edges.Group = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withParent; query != nil {
|
|
if err := eq.loadParent(ctx, query, nodes, nil,
|
|
func(n *Entity, e *Entity) { n.Edges.Parent = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withChildren; query != nil {
|
|
if err := eq.loadChildren(ctx, query, nodes,
|
|
func(n *Entity) { n.Edges.Children = []*Entity{} },
|
|
func(n *Entity, e *Entity) { n.Edges.Children = append(n.Edges.Children, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withEntity; query != nil {
|
|
if err := eq.loadEntity(ctx, query, nodes, nil,
|
|
func(n *Entity, e *Entity) { n.Edges.Entity = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withLocation; query != nil {
|
|
if err := eq.loadLocation(ctx, query, nodes, nil,
|
|
func(n *Entity, e *Entity) { n.Edges.Location = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withLabel; query != nil {
|
|
if err := eq.loadLabel(ctx, query, nodes,
|
|
func(n *Entity) { n.Edges.Label = []*Label{} },
|
|
func(n *Entity, e *Label) { n.Edges.Label = append(n.Edges.Label, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withType; query != nil {
|
|
if err := eq.loadType(ctx, query, nodes, nil,
|
|
func(n *Entity, e *EntityType) { n.Edges.Type = e }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withFields; query != nil {
|
|
if err := eq.loadFields(ctx, query, nodes,
|
|
func(n *Entity) { n.Edges.Fields = []*EntityField{} },
|
|
func(n *Entity, e *EntityField) { n.Edges.Fields = append(n.Edges.Fields, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withMaintenanceEntries; query != nil {
|
|
if err := eq.loadMaintenanceEntries(ctx, query, nodes,
|
|
func(n *Entity) { n.Edges.MaintenanceEntries = []*MaintenanceEntry{} },
|
|
func(n *Entity, e *MaintenanceEntry) {
|
|
n.Edges.MaintenanceEntries = append(n.Edges.MaintenanceEntries, e)
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if query := eq.withAttachments; query != nil {
|
|
if err := eq.loadAttachments(ctx, query, nodes,
|
|
func(n *Entity) { n.Edges.Attachments = []*Attachment{} },
|
|
func(n *Entity, e *Attachment) { n.Edges.Attachments = append(n.Edges.Attachments, e) }); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
func (eq *EntityQuery) loadGroup(ctx context.Context, query *GroupQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *Group)) error {
|
|
ids := make([]uuid.UUID, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID][]*Entity)
|
|
for i := range nodes {
|
|
if nodes[i].group_entities == nil {
|
|
continue
|
|
}
|
|
fk := *nodes[i].group_entities
|
|
if _, ok := nodeids[fk]; !ok {
|
|
ids = append(ids, fk)
|
|
}
|
|
nodeids[fk] = append(nodeids[fk], nodes[i])
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
query.Where(group.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 "group_entities" returned %v`, n.ID)
|
|
}
|
|
for i := range nodes {
|
|
assign(nodes[i], n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadParent(ctx context.Context, query *EntityQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *Entity)) error {
|
|
ids := make([]uuid.UUID, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID][]*Entity)
|
|
for i := range nodes {
|
|
if nodes[i].entity_children == nil {
|
|
continue
|
|
}
|
|
fk := *nodes[i].entity_children
|
|
if _, ok := nodeids[fk]; !ok {
|
|
ids = append(ids, fk)
|
|
}
|
|
nodeids[fk] = append(nodeids[fk], nodes[i])
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
query.Where(entity.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 "entity_children" returned %v`, n.ID)
|
|
}
|
|
for i := range nodes {
|
|
assign(nodes[i], n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadChildren(ctx context.Context, query *EntityQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *Entity)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID]*Entity)
|
|
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.Entity(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(s.C(entity.ChildrenColumn), fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.entity_children
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "entity_children" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected referenced foreign-key "entity_children" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadEntity(ctx context.Context, query *EntityQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *Entity)) error {
|
|
ids := make([]uuid.UUID, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID][]*Entity)
|
|
for i := range nodes {
|
|
if nodes[i].entity_location == nil {
|
|
continue
|
|
}
|
|
fk := *nodes[i].entity_location
|
|
if _, ok := nodeids[fk]; !ok {
|
|
ids = append(ids, fk)
|
|
}
|
|
nodeids[fk] = append(nodeids[fk], nodes[i])
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
query.Where(entity.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 "entity_location" returned %v`, n.ID)
|
|
}
|
|
for i := range nodes {
|
|
assign(nodes[i], n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadLocation(ctx context.Context, query *EntityQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *Entity)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID]*Entity)
|
|
for i := range nodes {
|
|
fks = append(fks, nodes[i].ID)
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
}
|
|
query.withFKs = true
|
|
query.Where(predicate.Entity(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(s.C(entity.LocationColumn), fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.entity_location
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "entity_location" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected referenced foreign-key "entity_location" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadLabel(ctx context.Context, query *LabelQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *Label)) error {
|
|
edgeIDs := make([]driver.Value, len(nodes))
|
|
byID := make(map[uuid.UUID]*Entity)
|
|
nids := make(map[uuid.UUID]map[*Entity]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(entity.LabelTable)
|
|
s.Join(joinT).On(s.C(label.FieldID), joinT.C(entity.LabelPrimaryKey[0]))
|
|
s.Where(sql.InValues(joinT.C(entity.LabelPrimaryKey[1]), edgeIDs...))
|
|
columns := s.SelectedColumns()
|
|
s.Select(joinT.C(entity.LabelPrimaryKey[1]))
|
|
s.AppendSelect(columns...)
|
|
s.SetDistinct(false)
|
|
})
|
|
if err := query.prepareQuery(ctx); err != nil {
|
|
return err
|
|
}
|
|
qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
|
|
return 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(uuid.UUID)}, values...), nil
|
|
}
|
|
spec.Assign = func(columns []string, values []any) error {
|
|
outValue := *values[0].(*uuid.UUID)
|
|
inValue := *values[1].(*uuid.UUID)
|
|
if nids[inValue] == nil {
|
|
nids[inValue] = map[*Entity]struct{}{byID[outValue]: {}}
|
|
return assign(columns[1:], values[1:])
|
|
}
|
|
nids[inValue][byID[outValue]] = struct{}{}
|
|
return nil
|
|
}
|
|
})
|
|
})
|
|
neighbors, err := withInterceptors[[]*Label](ctx, query, qr, query.inters)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
nodes, ok := nids[n.ID]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected "label" node returned %v`, n.ID)
|
|
}
|
|
for kn := range nodes {
|
|
assign(kn, n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadType(ctx context.Context, query *EntityTypeQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *EntityType)) error {
|
|
ids := make([]uuid.UUID, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID][]*Entity)
|
|
for i := range nodes {
|
|
if nodes[i].entity_type_entities == nil {
|
|
continue
|
|
}
|
|
fk := *nodes[i].entity_type_entities
|
|
if _, ok := nodeids[fk]; !ok {
|
|
ids = append(ids, fk)
|
|
}
|
|
nodeids[fk] = append(nodeids[fk], nodes[i])
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
query.Where(entitytype.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 "entity_type_entities" returned %v`, n.ID)
|
|
}
|
|
for i := range nodes {
|
|
assign(nodes[i], n)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadFields(ctx context.Context, query *EntityFieldQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *EntityField)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID]*Entity)
|
|
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.EntityField(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(s.C(entity.FieldsColumn), fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.entity_fields
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "entity_fields" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected referenced foreign-key "entity_fields" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadMaintenanceEntries(ctx context.Context, query *MaintenanceEntryQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *MaintenanceEntry)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID]*Entity)
|
|
for i := range nodes {
|
|
fks = append(fks, nodes[i].ID)
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
if init != nil {
|
|
init(nodes[i])
|
|
}
|
|
}
|
|
if len(query.ctx.Fields) > 0 {
|
|
query.ctx.AppendFieldOnce(maintenanceentry.FieldEntityID)
|
|
}
|
|
query.Where(predicate.MaintenanceEntry(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(s.C(entity.MaintenanceEntriesColumn), fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.EntityID
|
|
node, ok := nodeids[fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected referenced foreign-key "entity_id" returned %v for node %v`, fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
func (eq *EntityQuery) loadAttachments(ctx context.Context, query *AttachmentQuery, nodes []*Entity, init func(*Entity), assign func(*Entity, *Attachment)) error {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[uuid.UUID]*Entity)
|
|
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.Attachment(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(s.C(entity.AttachmentsColumn), fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.entity_attachments
|
|
if fk == nil {
|
|
return fmt.Errorf(`foreign-key "entity_attachments" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return fmt.Errorf(`unexpected referenced foreign-key "entity_attachments" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
assign(node, n)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (eq *EntityQuery) sqlCount(ctx context.Context) (int, error) {
|
|
_spec := eq.querySpec()
|
|
_spec.Node.Columns = eq.ctx.Fields
|
|
if len(eq.ctx.Fields) > 0 {
|
|
_spec.Unique = eq.ctx.Unique != nil && *eq.ctx.Unique
|
|
}
|
|
return sqlgraph.CountNodes(ctx, eq.driver, _spec)
|
|
}
|
|
|
|
func (eq *EntityQuery) querySpec() *sqlgraph.QuerySpec {
|
|
_spec := sqlgraph.NewQuerySpec(entity.Table, entity.Columns, sqlgraph.NewFieldSpec(entity.FieldID, field.TypeUUID))
|
|
_spec.From = eq.sql
|
|
if unique := eq.ctx.Unique; unique != nil {
|
|
_spec.Unique = *unique
|
|
} else if eq.path != nil {
|
|
_spec.Unique = true
|
|
}
|
|
if fields := eq.ctx.Fields; len(fields) > 0 {
|
|
_spec.Node.Columns = make([]string, 0, len(fields))
|
|
_spec.Node.Columns = append(_spec.Node.Columns, entity.FieldID)
|
|
for i := range fields {
|
|
if fields[i] != entity.FieldID {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
|
|
}
|
|
}
|
|
}
|
|
if ps := eq.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if limit := eq.ctx.Limit; limit != nil {
|
|
_spec.Limit = *limit
|
|
}
|
|
if offset := eq.ctx.Offset; offset != nil {
|
|
_spec.Offset = *offset
|
|
}
|
|
if ps := eq.order; len(ps) > 0 {
|
|
_spec.Order = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
return _spec
|
|
}
|
|
|
|
func (eq *EntityQuery) sqlQuery(ctx context.Context) *sql.Selector {
|
|
builder := sql.Dialect(eq.driver.Dialect())
|
|
t1 := builder.Table(entity.Table)
|
|
columns := eq.ctx.Fields
|
|
if len(columns) == 0 {
|
|
columns = entity.Columns
|
|
}
|
|
selector := builder.Select(t1.Columns(columns...)...).From(t1)
|
|
if eq.sql != nil {
|
|
selector = eq.sql
|
|
selector.Select(selector.Columns(columns...)...)
|
|
}
|
|
if eq.ctx.Unique != nil && *eq.ctx.Unique {
|
|
selector.Distinct()
|
|
}
|
|
for _, p := range eq.predicates {
|
|
p(selector)
|
|
}
|
|
for _, p := range eq.order {
|
|
p(selector)
|
|
}
|
|
if offset := eq.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 := eq.ctx.Limit; limit != nil {
|
|
selector.Limit(*limit)
|
|
}
|
|
return selector
|
|
}
|
|
|
|
// EntityGroupBy is the group-by builder for Entity entities.
|
|
type EntityGroupBy struct {
|
|
selector
|
|
build *EntityQuery
|
|
}
|
|
|
|
// Aggregate adds the given aggregation functions to the group-by query.
|
|
func (egb *EntityGroupBy) Aggregate(fns ...AggregateFunc) *EntityGroupBy {
|
|
egb.fns = append(egb.fns, fns...)
|
|
return egb
|
|
}
|
|
|
|
// Scan applies the selector query and scans the result into the given value.
|
|
func (egb *EntityGroupBy) Scan(ctx context.Context, v any) error {
|
|
ctx = setContextOp(ctx, egb.build.ctx, ent.OpQueryGroupBy)
|
|
if err := egb.build.prepareQuery(ctx); err != nil {
|
|
return err
|
|
}
|
|
return scanWithInterceptors[*EntityQuery, *EntityGroupBy](ctx, egb.build, egb, egb.build.inters, v)
|
|
}
|
|
|
|
func (egb *EntityGroupBy) sqlScan(ctx context.Context, root *EntityQuery, v any) error {
|
|
selector := root.sqlQuery(ctx).Select()
|
|
aggregation := make([]string, 0, len(egb.fns))
|
|
for _, fn := range egb.fns {
|
|
aggregation = append(aggregation, fn(selector))
|
|
}
|
|
if len(selector.SelectedColumns()) == 0 {
|
|
columns := make([]string, 0, len(*egb.flds)+len(egb.fns))
|
|
for _, f := range *egb.flds {
|
|
columns = append(columns, selector.C(f))
|
|
}
|
|
columns = append(columns, aggregation...)
|
|
selector.Select(columns...)
|
|
}
|
|
selector.GroupBy(selector.Columns(*egb.flds...)...)
|
|
if err := selector.Err(); err != nil {
|
|
return err
|
|
}
|
|
rows := &sql.Rows{}
|
|
query, args := selector.Query()
|
|
if err := egb.build.driver.Query(ctx, query, args, rows); err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
return sql.ScanSlice(rows, v)
|
|
}
|
|
|
|
// EntitySelect is the builder for selecting fields of Entity entities.
|
|
type EntitySelect struct {
|
|
*EntityQuery
|
|
selector
|
|
}
|
|
|
|
// Aggregate adds the given aggregation functions to the selector query.
|
|
func (es *EntitySelect) Aggregate(fns ...AggregateFunc) *EntitySelect {
|
|
es.fns = append(es.fns, fns...)
|
|
return es
|
|
}
|
|
|
|
// Scan applies the selector query and scans the result into the given value.
|
|
func (es *EntitySelect) Scan(ctx context.Context, v any) error {
|
|
ctx = setContextOp(ctx, es.ctx, ent.OpQuerySelect)
|
|
if err := es.prepareQuery(ctx); err != nil {
|
|
return err
|
|
}
|
|
return scanWithInterceptors[*EntityQuery, *EntitySelect](ctx, es.EntityQuery, es, es.inters, v)
|
|
}
|
|
|
|
func (es *EntitySelect) sqlScan(ctx context.Context, root *EntityQuery, v any) error {
|
|
selector := root.sqlQuery(ctx)
|
|
aggregation := make([]string, 0, len(es.fns))
|
|
for _, fn := range es.fns {
|
|
aggregation = append(aggregation, fn(selector))
|
|
}
|
|
switch n := len(*es.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 := es.driver.Query(ctx, query, args, rows); err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
return sql.ScanSlice(rows, v)
|
|
}
|