You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cloudreve/ent/sharepurchase_query.go

681 lines
20 KiB

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"math"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
"github.com/cloudreve/Cloudreve/v4/ent/predicate"
"github.com/cloudreve/Cloudreve/v4/ent/share"
"github.com/cloudreve/Cloudreve/v4/ent/sharepurchase"
"github.com/cloudreve/Cloudreve/v4/ent/user"
)
// SharePurchaseQuery is the builder for querying SharePurchase entities.
type SharePurchaseQuery struct {
config
ctx *QueryContext
order []sharepurchase.OrderOption
inters []Interceptor
predicates []predicate.SharePurchase
withShare *ShareQuery
withBuyer *UserQuery
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the SharePurchaseQuery builder.
func (spq *SharePurchaseQuery) Where(ps ...predicate.SharePurchase) *SharePurchaseQuery {
spq.predicates = append(spq.predicates, ps...)
return spq
}
// Limit the number of records to be returned by this query.
func (spq *SharePurchaseQuery) Limit(limit int) *SharePurchaseQuery {
spq.ctx.Limit = &limit
return spq
}
// Offset to start from.
func (spq *SharePurchaseQuery) Offset(offset int) *SharePurchaseQuery {
spq.ctx.Offset = &offset
return spq
}
// 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 (spq *SharePurchaseQuery) Unique(unique bool) *SharePurchaseQuery {
spq.ctx.Unique = &unique
return spq
}
// Order specifies how the records should be ordered.
func (spq *SharePurchaseQuery) Order(o ...sharepurchase.OrderOption) *SharePurchaseQuery {
spq.order = append(spq.order, o...)
return spq
}
// QueryShare chains the current query on the "share" edge.
func (spq *SharePurchaseQuery) QueryShare() *ShareQuery {
query := (&ShareClient{config: spq.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := spq.prepareQuery(ctx); err != nil {
return nil, err
}
selector := spq.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(sharepurchase.Table, sharepurchase.FieldID, selector),
sqlgraph.To(share.Table, share.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, sharepurchase.ShareTable, sharepurchase.ShareColumn),
)
fromU = sqlgraph.SetNeighbors(spq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryBuyer chains the current query on the "buyer" edge.
func (spq *SharePurchaseQuery) QueryBuyer() *UserQuery {
query := (&UserClient{config: spq.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := spq.prepareQuery(ctx); err != nil {
return nil, err
}
selector := spq.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(sharepurchase.Table, sharepurchase.FieldID, selector),
sqlgraph.To(user.Table, user.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, sharepurchase.BuyerTable, sharepurchase.BuyerColumn),
)
fromU = sqlgraph.SetNeighbors(spq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first SharePurchase entity from the query.
// Returns a *NotFoundError when no SharePurchase was found.
func (spq *SharePurchaseQuery) First(ctx context.Context) (*SharePurchase, error) {
nodes, err := spq.Limit(1).All(setContextOp(ctx, spq.ctx, "First"))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{sharepurchase.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (spq *SharePurchaseQuery) FirstX(ctx context.Context) *SharePurchase {
node, err := spq.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first SharePurchase ID from the query.
// Returns a *NotFoundError when no SharePurchase ID was found.
func (spq *SharePurchaseQuery) FirstID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = spq.Limit(1).IDs(setContextOp(ctx, spq.ctx, "FirstID")); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{sharepurchase.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (spq *SharePurchaseQuery) FirstIDX(ctx context.Context) int {
id, err := spq.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single SharePurchase entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one SharePurchase entity is found.
// Returns a *NotFoundError when no SharePurchase entities are found.
func (spq *SharePurchaseQuery) Only(ctx context.Context) (*SharePurchase, error) {
nodes, err := spq.Limit(2).All(setContextOp(ctx, spq.ctx, "Only"))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{sharepurchase.Label}
default:
return nil, &NotSingularError{sharepurchase.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (spq *SharePurchaseQuery) OnlyX(ctx context.Context) *SharePurchase {
node, err := spq.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only SharePurchase ID in the query.
// Returns a *NotSingularError when more than one SharePurchase ID is found.
// Returns a *NotFoundError when no entities are found.
func (spq *SharePurchaseQuery) OnlyID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = spq.Limit(2).IDs(setContextOp(ctx, spq.ctx, "OnlyID")); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{sharepurchase.Label}
default:
err = &NotSingularError{sharepurchase.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (spq *SharePurchaseQuery) OnlyIDX(ctx context.Context) int {
id, err := spq.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of SharePurchases.
func (spq *SharePurchaseQuery) All(ctx context.Context) ([]*SharePurchase, error) {
ctx = setContextOp(ctx, spq.ctx, "All")
if err := spq.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*SharePurchase, *SharePurchaseQuery]()
return withInterceptors[[]*SharePurchase](ctx, spq, qr, spq.inters)
}
// AllX is like All, but panics if an error occurs.
func (spq *SharePurchaseQuery) AllX(ctx context.Context) []*SharePurchase {
nodes, err := spq.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of SharePurchase IDs.
func (spq *SharePurchaseQuery) IDs(ctx context.Context) (ids []int, err error) {
if spq.ctx.Unique == nil && spq.path != nil {
spq.Unique(true)
}
ctx = setContextOp(ctx, spq.ctx, "IDs")
if err = spq.Select(sharepurchase.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (spq *SharePurchaseQuery) IDsX(ctx context.Context) []int {
ids, err := spq.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (spq *SharePurchaseQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, spq.ctx, "Count")
if err := spq.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, spq, querierCount[*SharePurchaseQuery](), spq.inters)
}
// CountX is like Count, but panics if an error occurs.
func (spq *SharePurchaseQuery) CountX(ctx context.Context) int {
count, err := spq.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (spq *SharePurchaseQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, spq.ctx, "Exist")
switch _, err := spq.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 (spq *SharePurchaseQuery) ExistX(ctx context.Context) bool {
exist, err := spq.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the SharePurchaseQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (spq *SharePurchaseQuery) Clone() *SharePurchaseQuery {
if spq == nil {
return nil
}
return &SharePurchaseQuery{
config: spq.config,
ctx: spq.ctx.Clone(),
order: append([]sharepurchase.OrderOption{}, spq.order...),
inters: append([]Interceptor{}, spq.inters...),
predicates: append([]predicate.SharePurchase{}, spq.predicates...),
withShare: spq.withShare.Clone(),
withBuyer: spq.withBuyer.Clone(),
// clone intermediate query.
sql: spq.sql.Clone(),
path: spq.path,
}
}
// WithShare tells the query-builder to eager-load the nodes that are connected to
// the "share" edge. The optional arguments are used to configure the query builder of the edge.
func (spq *SharePurchaseQuery) WithShare(opts ...func(*ShareQuery)) *SharePurchaseQuery {
query := (&ShareClient{config: spq.config}).Query()
for _, opt := range opts {
opt(query)
}
spq.withShare = query
return spq
}
// WithBuyer tells the query-builder to eager-load the nodes that are connected to
// the "buyer" edge. The optional arguments are used to configure the query builder of the edge.
func (spq *SharePurchaseQuery) WithBuyer(opts ...func(*UserQuery)) *SharePurchaseQuery {
query := (&UserClient{config: spq.config}).Query()
for _, opt := range opts {
opt(query)
}
spq.withBuyer = query
return spq
}
// 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.SharePurchase.Query().
// GroupBy(sharepurchase.FieldCreatedAt).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (spq *SharePurchaseQuery) GroupBy(field string, fields ...string) *SharePurchaseGroupBy {
spq.ctx.Fields = append([]string{field}, fields...)
grbuild := &SharePurchaseGroupBy{build: spq}
grbuild.flds = &spq.ctx.Fields
grbuild.label = sharepurchase.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.SharePurchase.Query().
// Select(sharepurchase.FieldCreatedAt).
// Scan(ctx, &v)
func (spq *SharePurchaseQuery) Select(fields ...string) *SharePurchaseSelect {
spq.ctx.Fields = append(spq.ctx.Fields, fields...)
sbuild := &SharePurchaseSelect{SharePurchaseQuery: spq}
sbuild.label = sharepurchase.Label
sbuild.flds, sbuild.scan = &spq.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a SharePurchaseSelect configured with the given aggregations.
func (spq *SharePurchaseQuery) Aggregate(fns ...AggregateFunc) *SharePurchaseSelect {
return spq.Select().Aggregate(fns...)
}
func (spq *SharePurchaseQuery) prepareQuery(ctx context.Context) error {
for _, inter := range spq.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, spq); err != nil {
return err
}
}
}
for _, f := range spq.ctx.Fields {
if !sharepurchase.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if spq.path != nil {
prev, err := spq.path(ctx)
if err != nil {
return err
}
spq.sql = prev
}
return nil
}
func (spq *SharePurchaseQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*SharePurchase, error) {
var (
nodes = []*SharePurchase{}
_spec = spq.querySpec()
loadedTypes = [2]bool{
spq.withShare != nil,
spq.withBuyer != nil,
}
)
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*SharePurchase).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &SharePurchase{config: spq.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, spq.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := spq.withShare; query != nil {
if err := spq.loadShare(ctx, query, nodes, nil,
func(n *SharePurchase, e *Share) { n.Edges.Share = e }); err != nil {
return nil, err
}
}
if query := spq.withBuyer; query != nil {
if err := spq.loadBuyer(ctx, query, nodes, nil,
func(n *SharePurchase, e *User) { n.Edges.Buyer = e }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (spq *SharePurchaseQuery) loadShare(ctx context.Context, query *ShareQuery, nodes []*SharePurchase, init func(*SharePurchase), assign func(*SharePurchase, *Share)) error {
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*SharePurchase)
for i := range nodes {
fk := nodes[i].ShareID
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(share.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 "share_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (spq *SharePurchaseQuery) loadBuyer(ctx context.Context, query *UserQuery, nodes []*SharePurchase, init func(*SharePurchase), assign func(*SharePurchase, *User)) error {
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*SharePurchase)
for i := range nodes {
fk := nodes[i].BuyerID
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
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 "buyer_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (spq *SharePurchaseQuery) sqlCount(ctx context.Context) (int, error) {
_spec := spq.querySpec()
_spec.Node.Columns = spq.ctx.Fields
if len(spq.ctx.Fields) > 0 {
_spec.Unique = spq.ctx.Unique != nil && *spq.ctx.Unique
}
return sqlgraph.CountNodes(ctx, spq.driver, _spec)
}
func (spq *SharePurchaseQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(sharepurchase.Table, sharepurchase.Columns, sqlgraph.NewFieldSpec(sharepurchase.FieldID, field.TypeInt))
_spec.From = spq.sql
if unique := spq.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if spq.path != nil {
_spec.Unique = true
}
if fields := spq.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, sharepurchase.FieldID)
for i := range fields {
if fields[i] != sharepurchase.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
if spq.withShare != nil {
_spec.Node.AddColumnOnce(sharepurchase.FieldShareID)
}
if spq.withBuyer != nil {
_spec.Node.AddColumnOnce(sharepurchase.FieldBuyerID)
}
}
if ps := spq.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := spq.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := spq.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := spq.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (spq *SharePurchaseQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(spq.driver.Dialect())
t1 := builder.Table(sharepurchase.Table)
columns := spq.ctx.Fields
if len(columns) == 0 {
columns = sharepurchase.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if spq.sql != nil {
selector = spq.sql
selector.Select(selector.Columns(columns...)...)
}
if spq.ctx.Unique != nil && *spq.ctx.Unique {
selector.Distinct()
}
for _, p := range spq.predicates {
p(selector)
}
for _, p := range spq.order {
p(selector)
}
if offset := spq.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 := spq.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// SharePurchaseGroupBy is the group-by builder for SharePurchase entities.
type SharePurchaseGroupBy struct {
selector
build *SharePurchaseQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (spgb *SharePurchaseGroupBy) Aggregate(fns ...AggregateFunc) *SharePurchaseGroupBy {
spgb.fns = append(spgb.fns, fns...)
return spgb
}
// Scan applies the selector query and scans the result into the given value.
func (spgb *SharePurchaseGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, spgb.build.ctx, "GroupBy")
if err := spgb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*SharePurchaseQuery, *SharePurchaseGroupBy](ctx, spgb.build, spgb, spgb.build.inters, v)
}
func (spgb *SharePurchaseGroupBy) sqlScan(ctx context.Context, root *SharePurchaseQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(spgb.fns))
for _, fn := range spgb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*spgb.flds)+len(spgb.fns))
for _, f := range *spgb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*spgb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := spgb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// SharePurchaseSelect is the builder for selecting fields of SharePurchase entities.
type SharePurchaseSelect struct {
*SharePurchaseQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (sps *SharePurchaseSelect) Aggregate(fns ...AggregateFunc) *SharePurchaseSelect {
sps.fns = append(sps.fns, fns...)
return sps
}
// Scan applies the selector query and scans the result into the given value.
func (sps *SharePurchaseSelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, sps.ctx, "Select")
if err := sps.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*SharePurchaseQuery, *SharePurchaseSelect](ctx, sps.SharePurchaseQuery, sps, sps.inters, v)
}
func (sps *SharePurchaseSelect) sqlScan(ctx context.Context, root *SharePurchaseQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(sps.fns))
for _, fn := range sps.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*sps.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 := sps.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}