mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
When loading relations (using edger-loading) and there are no changes, the response should be an empty slice (and not nil). Similar to the way we handle simple queries (e.g. All()). Closed #790
1089 lines
28 KiB
Go
1089 lines
28 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 entc, DO NOT EDIT.
|
|
|
|
package ent
|
|
|
|
import (
|
|
"context"
|
|
"database/sql/driver"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
|
|
"github.com/facebook/ent/dialect/sql"
|
|
"github.com/facebook/ent/dialect/sql/sqlgraph"
|
|
"github.com/facebook/ent/entc/integration/ent/fieldtype"
|
|
"github.com/facebook/ent/entc/integration/ent/file"
|
|
"github.com/facebook/ent/entc/integration/ent/filetype"
|
|
"github.com/facebook/ent/entc/integration/ent/predicate"
|
|
"github.com/facebook/ent/entc/integration/ent/user"
|
|
"github.com/facebook/ent/schema/field"
|
|
)
|
|
|
|
// FileQuery is the builder for querying File entities.
|
|
type FileQuery struct {
|
|
config
|
|
limit *int
|
|
offset *int
|
|
order []OrderFunc
|
|
unique []string
|
|
predicates []predicate.File
|
|
// eager-loading edges.
|
|
withOwner *UserQuery
|
|
withType *FileTypeQuery
|
|
withField *FieldTypeQuery
|
|
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 builder.
|
|
func (fq *FileQuery) Where(ps ...predicate.File) *FileQuery {
|
|
fq.predicates = append(fq.predicates, ps...)
|
|
return fq
|
|
}
|
|
|
|
// Limit adds a limit step to the query.
|
|
func (fq *FileQuery) Limit(limit int) *FileQuery {
|
|
fq.limit = &limit
|
|
return fq
|
|
}
|
|
|
|
// Offset adds an offset step to the query.
|
|
func (fq *FileQuery) Offset(offset int) *FileQuery {
|
|
fq.offset = &offset
|
|
return fq
|
|
}
|
|
|
|
// Order adds an order step to the query.
|
|
func (fq *FileQuery) Order(o ...OrderFunc) *FileQuery {
|
|
fq.order = append(fq.order, o...)
|
|
return fq
|
|
}
|
|
|
|
// QueryOwner chains the current query on the owner edge.
|
|
func (fq *FileQuery) QueryOwner() *UserQuery {
|
|
query := &UserQuery{config: fq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := fq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := fq.sqlQuery()
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(file.Table, file.FieldID, selector),
|
|
sqlgraph.To(user.Table, user.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2O, true, file.OwnerTable, file.OwnerColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(fq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryType chains the current query on the type edge.
|
|
func (fq *FileQuery) QueryType() *FileTypeQuery {
|
|
query := &FileTypeQuery{config: fq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := fq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := fq.sqlQuery()
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(file.Table, file.FieldID, selector),
|
|
sqlgraph.To(filetype.Table, filetype.FieldID),
|
|
sqlgraph.Edge(sqlgraph.M2O, true, file.TypeTable, file.TypeColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(fq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// QueryField chains the current query on the field edge.
|
|
func (fq *FileQuery) QueryField() *FieldTypeQuery {
|
|
query := &FieldTypeQuery{config: fq.config}
|
|
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
|
|
if err := fq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
selector := fq.sqlQuery()
|
|
if err := selector.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
step := sqlgraph.NewStep(
|
|
sqlgraph.From(file.Table, file.FieldID, selector),
|
|
sqlgraph.To(fieldtype.Table, fieldtype.FieldID),
|
|
sqlgraph.Edge(sqlgraph.O2M, false, file.FieldTable, file.FieldColumn),
|
|
)
|
|
fromU = sqlgraph.SetNeighbors(fq.driver.Dialect(), step)
|
|
return fromU, nil
|
|
}
|
|
return query
|
|
}
|
|
|
|
// First returns the first File entity in the query. Returns *NotFoundError when no file was found.
|
|
func (fq *FileQuery) First(ctx context.Context) (*File, error) {
|
|
nodes, err := fq.Limit(1).All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nil, &NotFoundError{file.Label}
|
|
}
|
|
return nodes[0], nil
|
|
}
|
|
|
|
// FirstX is like First, but panics if an error occurs.
|
|
func (fq *FileQuery) FirstX(ctx context.Context) *File {
|
|
node, err := fq.First(ctx)
|
|
if err != nil && !IsNotFound(err) {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// FirstID returns the first File id in the query. Returns *NotFoundError when no id was found.
|
|
func (fq *FileQuery) FirstID(ctx context.Context) (id int, err error) {
|
|
var ids []int
|
|
if ids, err = fq.Limit(1).IDs(ctx); err != nil {
|
|
return
|
|
}
|
|
if len(ids) == 0 {
|
|
err = &NotFoundError{file.Label}
|
|
return
|
|
}
|
|
return ids[0], nil
|
|
}
|
|
|
|
// FirstXID is like FirstID, but panics if an error occurs.
|
|
func (fq *FileQuery) FirstXID(ctx context.Context) int {
|
|
id, err := fq.FirstID(ctx)
|
|
if err != nil && !IsNotFound(err) {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// Only returns the only File entity in the query, returns an error if not exactly one entity was returned.
|
|
func (fq *FileQuery) Only(ctx context.Context) (*File, error) {
|
|
nodes, err := fq.Limit(2).All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
switch len(nodes) {
|
|
case 1:
|
|
return nodes[0], nil
|
|
case 0:
|
|
return nil, &NotFoundError{file.Label}
|
|
default:
|
|
return nil, &NotSingularError{file.Label}
|
|
}
|
|
}
|
|
|
|
// OnlyX is like Only, but panics if an error occurs.
|
|
func (fq *FileQuery) OnlyX(ctx context.Context) *File {
|
|
node, err := fq.Only(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return node
|
|
}
|
|
|
|
// OnlyID returns the only File id in the query, returns an error if not exactly one id was returned.
|
|
func (fq *FileQuery) OnlyID(ctx context.Context) (id int, err error) {
|
|
var ids []int
|
|
if ids, err = fq.Limit(2).IDs(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(ids) {
|
|
case 1:
|
|
id = ids[0]
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = &NotSingularError{file.Label}
|
|
}
|
|
return
|
|
}
|
|
|
|
// OnlyIDX is like OnlyID, but panics if an error occurs.
|
|
func (fq *FileQuery) OnlyIDX(ctx context.Context) int {
|
|
id, err := fq.OnlyID(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
// All executes the query and returns a list of Files.
|
|
func (fq *FileQuery) All(ctx context.Context) ([]*File, error) {
|
|
if err := fq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return fq.sqlAll(ctx)
|
|
}
|
|
|
|
// AllX is like All, but panics if an error occurs.
|
|
func (fq *FileQuery) AllX(ctx context.Context) []*File {
|
|
nodes, err := fq.All(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return nodes
|
|
}
|
|
|
|
// IDs executes the query and returns a list of File ids.
|
|
func (fq *FileQuery) IDs(ctx context.Context) ([]int, error) {
|
|
var ids []int
|
|
if err := fq.Select(file.FieldID).Scan(ctx, &ids); err != nil {
|
|
return nil, err
|
|
}
|
|
return ids, nil
|
|
}
|
|
|
|
// IDsX is like IDs, but panics if an error occurs.
|
|
func (fq *FileQuery) IDsX(ctx context.Context) []int {
|
|
ids, err := fq.IDs(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// Count returns the count of the given query.
|
|
func (fq *FileQuery) Count(ctx context.Context) (int, error) {
|
|
if err := fq.prepareQuery(ctx); err != nil {
|
|
return 0, err
|
|
}
|
|
return fq.sqlCount(ctx)
|
|
}
|
|
|
|
// CountX is like Count, but panics if an error occurs.
|
|
func (fq *FileQuery) CountX(ctx context.Context) int {
|
|
count, err := fq.Count(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return count
|
|
}
|
|
|
|
// Exist returns true if the query has elements in the graph.
|
|
func (fq *FileQuery) Exist(ctx context.Context) (bool, error) {
|
|
if err := fq.prepareQuery(ctx); err != nil {
|
|
return false, err
|
|
}
|
|
return fq.sqlExist(ctx)
|
|
}
|
|
|
|
// ExistX is like Exist, but panics if an error occurs.
|
|
func (fq *FileQuery) ExistX(ctx context.Context) bool {
|
|
exist, err := fq.Exist(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return exist
|
|
}
|
|
|
|
// Clone returns a duplicate of the query builder, including all associated steps. It can be
|
|
// used to prepare common query builders and use them differently after the clone is made.
|
|
func (fq *FileQuery) Clone() *FileQuery {
|
|
return &FileQuery{
|
|
config: fq.config,
|
|
limit: fq.limit,
|
|
offset: fq.offset,
|
|
order: append([]OrderFunc{}, fq.order...),
|
|
unique: append([]string{}, fq.unique...),
|
|
predicates: append([]predicate.File{}, fq.predicates...),
|
|
// clone intermediate query.
|
|
sql: fq.sql.Clone(),
|
|
path: fq.path,
|
|
}
|
|
}
|
|
|
|
// WithOwner tells the query-builder to eager-loads the nodes that are connected to
|
|
// the "owner" edge. The optional arguments used to configure the query builder of the edge.
|
|
func (fq *FileQuery) WithOwner(opts ...func(*UserQuery)) *FileQuery {
|
|
query := &UserQuery{config: fq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
fq.withOwner = query
|
|
return fq
|
|
}
|
|
|
|
// WithType tells the query-builder to eager-loads the nodes that are connected to
|
|
// the "type" edge. The optional arguments used to configure the query builder of the edge.
|
|
func (fq *FileQuery) WithType(opts ...func(*FileTypeQuery)) *FileQuery {
|
|
query := &FileTypeQuery{config: fq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
fq.withType = query
|
|
return fq
|
|
}
|
|
|
|
// WithField tells the query-builder to eager-loads the nodes that are connected to
|
|
// the "field" edge. The optional arguments used to configure the query builder of the edge.
|
|
func (fq *FileQuery) WithField(opts ...func(*FieldTypeQuery)) *FileQuery {
|
|
query := &FieldTypeQuery{config: fq.config}
|
|
for _, opt := range opts {
|
|
opt(query)
|
|
}
|
|
fq.withField = query
|
|
return fq
|
|
}
|
|
|
|
// GroupBy 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 {
|
|
// Size int `json:"size,omitempty"`
|
|
// Count int `json:"count,omitempty"`
|
|
// }
|
|
//
|
|
// client.File.Query().
|
|
// GroupBy(file.FieldSize).
|
|
// Aggregate(ent.Count()).
|
|
// Scan(ctx, &v)
|
|
//
|
|
func (fq *FileQuery) GroupBy(field string, fields ...string) *FileGroupBy {
|
|
group := &FileGroupBy{config: fq.config}
|
|
group.fields = append([]string{field}, fields...)
|
|
group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
|
if err := fq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return fq.sqlQuery(), nil
|
|
}
|
|
return group
|
|
}
|
|
|
|
// Select one or more fields from the given query.
|
|
//
|
|
// Example:
|
|
//
|
|
// var v []struct {
|
|
// Size int `json:"size,omitempty"`
|
|
// }
|
|
//
|
|
// client.File.Query().
|
|
// Select(file.FieldSize).
|
|
// Scan(ctx, &v)
|
|
//
|
|
func (fq *FileQuery) Select(field string, fields ...string) *FileSelect {
|
|
selector := &FileSelect{config: fq.config}
|
|
selector.fields = append([]string{field}, fields...)
|
|
selector.path = func(ctx context.Context) (prev *sql.Selector, err error) {
|
|
if err := fq.prepareQuery(ctx); err != nil {
|
|
return nil, err
|
|
}
|
|
return fq.sqlQuery(), nil
|
|
}
|
|
return selector
|
|
}
|
|
|
|
func (fq *FileQuery) prepareQuery(ctx context.Context) error {
|
|
if fq.path != nil {
|
|
prev, err := fq.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fq.sql = prev
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (fq *FileQuery) sqlAll(ctx context.Context) ([]*File, error) {
|
|
var (
|
|
nodes = []*File{}
|
|
withFKs = fq.withFKs
|
|
_spec = fq.querySpec()
|
|
loadedTypes = [3]bool{
|
|
fq.withOwner != nil,
|
|
fq.withType != nil,
|
|
fq.withField != nil,
|
|
}
|
|
)
|
|
if fq.withOwner != nil || fq.withType != nil {
|
|
withFKs = true
|
|
}
|
|
if withFKs {
|
|
_spec.Node.Columns = append(_spec.Node.Columns, file.ForeignKeys...)
|
|
}
|
|
_spec.ScanValues = func() []interface{} {
|
|
node := &File{config: fq.config}
|
|
nodes = append(nodes, node)
|
|
values := node.scanValues()
|
|
if withFKs {
|
|
values = append(values, node.fkValues()...)
|
|
}
|
|
return values
|
|
}
|
|
_spec.Assign = func(values ...interface{}) error {
|
|
if len(nodes) == 0 {
|
|
return fmt.Errorf("ent: Assign called without calling ScanValues")
|
|
}
|
|
node := nodes[len(nodes)-1]
|
|
node.Edges.loadedTypes = loadedTypes
|
|
return node.assignValues(values...)
|
|
}
|
|
if err := sqlgraph.QueryNodes(ctx, fq.driver, _spec); err != nil {
|
|
return nil, err
|
|
}
|
|
if len(nodes) == 0 {
|
|
return nodes, nil
|
|
}
|
|
|
|
if query := fq.withOwner; query != nil {
|
|
ids := make([]int, 0, len(nodes))
|
|
nodeids := make(map[int][]*File)
|
|
for i := range nodes {
|
|
if fk := nodes[i].user_files; fk != nil {
|
|
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 nil, err
|
|
}
|
|
for _, n := range neighbors {
|
|
nodes, ok := nodeids[n.ID]
|
|
if !ok {
|
|
return nil, fmt.Errorf(`unexpected foreign-key "user_files" returned %v`, n.ID)
|
|
}
|
|
for i := range nodes {
|
|
nodes[i].Edges.Owner = n
|
|
}
|
|
}
|
|
}
|
|
|
|
if query := fq.withType; query != nil {
|
|
ids := make([]int, 0, len(nodes))
|
|
nodeids := make(map[int][]*File)
|
|
for i := range nodes {
|
|
if fk := nodes[i].file_type_files; fk != nil {
|
|
ids = append(ids, *fk)
|
|
nodeids[*fk] = append(nodeids[*fk], nodes[i])
|
|
}
|
|
}
|
|
query.Where(filetype.IDIn(ids...))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, n := range neighbors {
|
|
nodes, ok := nodeids[n.ID]
|
|
if !ok {
|
|
return nil, fmt.Errorf(`unexpected foreign-key "file_type_files" returned %v`, n.ID)
|
|
}
|
|
for i := range nodes {
|
|
nodes[i].Edges.Type = n
|
|
}
|
|
}
|
|
}
|
|
|
|
if query := fq.withField; query != nil {
|
|
fks := make([]driver.Value, 0, len(nodes))
|
|
nodeids := make(map[int]*File)
|
|
for i := range nodes {
|
|
fks = append(fks, nodes[i].ID)
|
|
nodeids[nodes[i].ID] = nodes[i]
|
|
nodes[i].Edges.Field = []*FieldType{}
|
|
}
|
|
query.withFKs = true
|
|
query.Where(predicate.FieldType(func(s *sql.Selector) {
|
|
s.Where(sql.InValues(file.FieldColumn, fks...))
|
|
}))
|
|
neighbors, err := query.All(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, n := range neighbors {
|
|
fk := n.file_field
|
|
if fk == nil {
|
|
return nil, fmt.Errorf(`foreign-key "file_field" is nil for node %v`, n.ID)
|
|
}
|
|
node, ok := nodeids[*fk]
|
|
if !ok {
|
|
return nil, fmt.Errorf(`unexpected foreign-key "file_field" returned %v for node %v`, *fk, n.ID)
|
|
}
|
|
node.Edges.Field = append(node.Edges.Field, n)
|
|
}
|
|
}
|
|
|
|
return nodes, nil
|
|
}
|
|
|
|
func (fq *FileQuery) sqlCount(ctx context.Context) (int, error) {
|
|
_spec := fq.querySpec()
|
|
return sqlgraph.CountNodes(ctx, fq.driver, _spec)
|
|
}
|
|
|
|
func (fq *FileQuery) sqlExist(ctx context.Context) (bool, error) {
|
|
n, err := fq.sqlCount(ctx)
|
|
if err != nil {
|
|
return false, fmt.Errorf("ent: check existence: %v", err)
|
|
}
|
|
return n > 0, nil
|
|
}
|
|
|
|
func (fq *FileQuery) querySpec() *sqlgraph.QuerySpec {
|
|
_spec := &sqlgraph.QuerySpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: file.Table,
|
|
Columns: file.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeInt,
|
|
Column: file.FieldID,
|
|
},
|
|
},
|
|
From: fq.sql,
|
|
Unique: true,
|
|
}
|
|
if ps := fq.predicates; len(ps) > 0 {
|
|
_spec.Predicate = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector)
|
|
}
|
|
}
|
|
}
|
|
if limit := fq.limit; limit != nil {
|
|
_spec.Limit = *limit
|
|
}
|
|
if offset := fq.offset; offset != nil {
|
|
_spec.Offset = *offset
|
|
}
|
|
if ps := fq.order; len(ps) > 0 {
|
|
_spec.Order = func(selector *sql.Selector) {
|
|
for i := range ps {
|
|
ps[i](selector, file.ValidColumn)
|
|
}
|
|
}
|
|
}
|
|
return _spec
|
|
}
|
|
|
|
func (fq *FileQuery) sqlQuery() *sql.Selector {
|
|
builder := sql.Dialect(fq.driver.Dialect())
|
|
t1 := builder.Table(file.Table)
|
|
selector := builder.Select(t1.Columns(file.Columns...)...).From(t1)
|
|
if fq.sql != nil {
|
|
selector = fq.sql
|
|
selector.Select(selector.Columns(file.Columns...)...)
|
|
}
|
|
for _, p := range fq.predicates {
|
|
p(selector)
|
|
}
|
|
for _, p := range fq.order {
|
|
p(selector, file.ValidColumn)
|
|
}
|
|
if offset := fq.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 := fq.limit; limit != nil {
|
|
selector.Limit(*limit)
|
|
}
|
|
return selector
|
|
}
|
|
|
|
// FileGroupBy is the builder for group-by File entities.
|
|
type FileGroupBy struct {
|
|
config
|
|
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 (fgb *FileGroupBy) Aggregate(fns ...AggregateFunc) *FileGroupBy {
|
|
fgb.fns = append(fgb.fns, fns...)
|
|
return fgb
|
|
}
|
|
|
|
// Scan applies the group-by query and scan the result into the given value.
|
|
func (fgb *FileGroupBy) Scan(ctx context.Context, v interface{}) error {
|
|
query, err := fgb.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fgb.sql = query
|
|
return fgb.sqlScan(ctx, v)
|
|
}
|
|
|
|
// ScanX is like Scan, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) ScanX(ctx context.Context, v interface{}) {
|
|
if err := fgb.Scan(ctx, v); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Strings returns list of strings from group-by. It is only allowed when querying group-by with one field.
|
|
func (fgb *FileGroupBy) Strings(ctx context.Context) ([]string, error) {
|
|
if len(fgb.fields) > 1 {
|
|
return nil, errors.New("ent: FileGroupBy.Strings is not achievable when grouping more than 1 field")
|
|
}
|
|
var v []string
|
|
if err := fgb.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// StringsX is like Strings, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) StringsX(ctx context.Context) []string {
|
|
v, err := fgb.Strings(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// String returns a single string from group-by. It is only allowed when querying group-by with one field.
|
|
func (fgb *FileGroupBy) String(ctx context.Context) (_ string, err error) {
|
|
var v []string
|
|
if v, err = fgb.Strings(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: FileGroupBy.Strings returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// StringX is like String, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) StringX(ctx context.Context) string {
|
|
v, err := fgb.String(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Ints returns list of ints from group-by. It is only allowed when querying group-by with one field.
|
|
func (fgb *FileGroupBy) Ints(ctx context.Context) ([]int, error) {
|
|
if len(fgb.fields) > 1 {
|
|
return nil, errors.New("ent: FileGroupBy.Ints is not achievable when grouping more than 1 field")
|
|
}
|
|
var v []int
|
|
if err := fgb.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// IntsX is like Ints, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) IntsX(ctx context.Context) []int {
|
|
v, err := fgb.Ints(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Int returns a single int from group-by. It is only allowed when querying group-by with one field.
|
|
func (fgb *FileGroupBy) Int(ctx context.Context) (_ int, err error) {
|
|
var v []int
|
|
if v, err = fgb.Ints(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: FileGroupBy.Ints returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// IntX is like Int, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) IntX(ctx context.Context) int {
|
|
v, err := fgb.Int(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Float64s returns list of float64s from group-by. It is only allowed when querying group-by with one field.
|
|
func (fgb *FileGroupBy) Float64s(ctx context.Context) ([]float64, error) {
|
|
if len(fgb.fields) > 1 {
|
|
return nil, errors.New("ent: FileGroupBy.Float64s is not achievable when grouping more than 1 field")
|
|
}
|
|
var v []float64
|
|
if err := fgb.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// Float64sX is like Float64s, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) Float64sX(ctx context.Context) []float64 {
|
|
v, err := fgb.Float64s(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Float64 returns a single float64 from group-by. It is only allowed when querying group-by with one field.
|
|
func (fgb *FileGroupBy) Float64(ctx context.Context) (_ float64, err error) {
|
|
var v []float64
|
|
if v, err = fgb.Float64s(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: FileGroupBy.Float64s returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// Float64X is like Float64, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) Float64X(ctx context.Context) float64 {
|
|
v, err := fgb.Float64(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Bools returns list of bools from group-by. It is only allowed when querying group-by with one field.
|
|
func (fgb *FileGroupBy) Bools(ctx context.Context) ([]bool, error) {
|
|
if len(fgb.fields) > 1 {
|
|
return nil, errors.New("ent: FileGroupBy.Bools is not achievable when grouping more than 1 field")
|
|
}
|
|
var v []bool
|
|
if err := fgb.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// BoolsX is like Bools, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) BoolsX(ctx context.Context) []bool {
|
|
v, err := fgb.Bools(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Bool returns a single bool from group-by. It is only allowed when querying group-by with one field.
|
|
func (fgb *FileGroupBy) Bool(ctx context.Context) (_ bool, err error) {
|
|
var v []bool
|
|
if v, err = fgb.Bools(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: FileGroupBy.Bools returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// BoolX is like Bool, but panics if an error occurs.
|
|
func (fgb *FileGroupBy) BoolX(ctx context.Context) bool {
|
|
v, err := fgb.Bool(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (fgb *FileGroupBy) sqlScan(ctx context.Context, v interface{}) error {
|
|
for _, f := range fgb.fields {
|
|
if !file.ValidColumn(f) {
|
|
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
|
|
}
|
|
}
|
|
selector := fgb.sqlQuery()
|
|
if err := selector.Err(); err != nil {
|
|
return err
|
|
}
|
|
rows := &sql.Rows{}
|
|
query, args := selector.Query()
|
|
if err := fgb.driver.Query(ctx, query, args, rows); err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
return sql.ScanSlice(rows, v)
|
|
}
|
|
|
|
func (fgb *FileGroupBy) sqlQuery() *sql.Selector {
|
|
selector := fgb.sql
|
|
columns := make([]string, 0, len(fgb.fields)+len(fgb.fns))
|
|
columns = append(columns, fgb.fields...)
|
|
for _, fn := range fgb.fns {
|
|
columns = append(columns, fn(selector, file.ValidColumn))
|
|
}
|
|
return selector.Select(columns...).GroupBy(fgb.fields...)
|
|
}
|
|
|
|
// FileSelect is the builder for select fields of File entities.
|
|
type FileSelect struct {
|
|
config
|
|
fields []string
|
|
// intermediate query (i.e. traversal path).
|
|
sql *sql.Selector
|
|
path func(context.Context) (*sql.Selector, error)
|
|
}
|
|
|
|
// Scan applies the selector query and scan the result into the given value.
|
|
func (fs *FileSelect) Scan(ctx context.Context, v interface{}) error {
|
|
query, err := fs.path(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fs.sql = query
|
|
return fs.sqlScan(ctx, v)
|
|
}
|
|
|
|
// ScanX is like Scan, but panics if an error occurs.
|
|
func (fs *FileSelect) ScanX(ctx context.Context, v interface{}) {
|
|
if err := fs.Scan(ctx, v); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Strings returns list of strings from selector. It is only allowed when selecting one field.
|
|
func (fs *FileSelect) Strings(ctx context.Context) ([]string, error) {
|
|
if len(fs.fields) > 1 {
|
|
return nil, errors.New("ent: FileSelect.Strings is not achievable when selecting more than 1 field")
|
|
}
|
|
var v []string
|
|
if err := fs.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// StringsX is like Strings, but panics if an error occurs.
|
|
func (fs *FileSelect) StringsX(ctx context.Context) []string {
|
|
v, err := fs.Strings(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// String returns a single string from selector. It is only allowed when selecting one field.
|
|
func (fs *FileSelect) String(ctx context.Context) (_ string, err error) {
|
|
var v []string
|
|
if v, err = fs.Strings(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: FileSelect.Strings returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// StringX is like String, but panics if an error occurs.
|
|
func (fs *FileSelect) StringX(ctx context.Context) string {
|
|
v, err := fs.String(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Ints returns list of ints from selector. It is only allowed when selecting one field.
|
|
func (fs *FileSelect) Ints(ctx context.Context) ([]int, error) {
|
|
if len(fs.fields) > 1 {
|
|
return nil, errors.New("ent: FileSelect.Ints is not achievable when selecting more than 1 field")
|
|
}
|
|
var v []int
|
|
if err := fs.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// IntsX is like Ints, but panics if an error occurs.
|
|
func (fs *FileSelect) IntsX(ctx context.Context) []int {
|
|
v, err := fs.Ints(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Int returns a single int from selector. It is only allowed when selecting one field.
|
|
func (fs *FileSelect) Int(ctx context.Context) (_ int, err error) {
|
|
var v []int
|
|
if v, err = fs.Ints(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: FileSelect.Ints returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// IntX is like Int, but panics if an error occurs.
|
|
func (fs *FileSelect) IntX(ctx context.Context) int {
|
|
v, err := fs.Int(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Float64s returns list of float64s from selector. It is only allowed when selecting one field.
|
|
func (fs *FileSelect) Float64s(ctx context.Context) ([]float64, error) {
|
|
if len(fs.fields) > 1 {
|
|
return nil, errors.New("ent: FileSelect.Float64s is not achievable when selecting more than 1 field")
|
|
}
|
|
var v []float64
|
|
if err := fs.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// Float64sX is like Float64s, but panics if an error occurs.
|
|
func (fs *FileSelect) Float64sX(ctx context.Context) []float64 {
|
|
v, err := fs.Float64s(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Float64 returns a single float64 from selector. It is only allowed when selecting one field.
|
|
func (fs *FileSelect) Float64(ctx context.Context) (_ float64, err error) {
|
|
var v []float64
|
|
if v, err = fs.Float64s(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: FileSelect.Float64s returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// Float64X is like Float64, but panics if an error occurs.
|
|
func (fs *FileSelect) Float64X(ctx context.Context) float64 {
|
|
v, err := fs.Float64(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Bools returns list of bools from selector. It is only allowed when selecting one field.
|
|
func (fs *FileSelect) Bools(ctx context.Context) ([]bool, error) {
|
|
if len(fs.fields) > 1 {
|
|
return nil, errors.New("ent: FileSelect.Bools is not achievable when selecting more than 1 field")
|
|
}
|
|
var v []bool
|
|
if err := fs.Scan(ctx, &v); err != nil {
|
|
return nil, err
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// BoolsX is like Bools, but panics if an error occurs.
|
|
func (fs *FileSelect) BoolsX(ctx context.Context) []bool {
|
|
v, err := fs.Bools(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
// Bool returns a single bool from selector. It is only allowed when selecting one field.
|
|
func (fs *FileSelect) Bool(ctx context.Context) (_ bool, err error) {
|
|
var v []bool
|
|
if v, err = fs.Bools(ctx); err != nil {
|
|
return
|
|
}
|
|
switch len(v) {
|
|
case 1:
|
|
return v[0], nil
|
|
case 0:
|
|
err = &NotFoundError{file.Label}
|
|
default:
|
|
err = fmt.Errorf("ent: FileSelect.Bools returned %d results when one was expected", len(v))
|
|
}
|
|
return
|
|
}
|
|
|
|
// BoolX is like Bool, but panics if an error occurs.
|
|
func (fs *FileSelect) BoolX(ctx context.Context) bool {
|
|
v, err := fs.Bool(ctx)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return v
|
|
}
|
|
|
|
func (fs *FileSelect) sqlScan(ctx context.Context, v interface{}) error {
|
|
for _, f := range fs.fields {
|
|
if !file.ValidColumn(f) {
|
|
return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for selection", f)}
|
|
}
|
|
}
|
|
rows := &sql.Rows{}
|
|
query, args := fs.sqlQuery().Query()
|
|
if err := fs.driver.Query(ctx, query, args, rows); err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
return sql.ScanSlice(rows, v)
|
|
}
|
|
|
|
func (fs *FileSelect) sqlQuery() sql.Querier {
|
|
selector := fs.sql
|
|
selector.Select(selector.Columns(fs.fields...)...)
|
|
return selector
|
|
}
|