tss-rocks/backend/ent/contributorsociallink_query.go
CDN 05ddc1f783
Some checks failed
Build Backend / Build Docker Image (push) Successful in 3m33s
Test Backend / test (push) Failing after 31s
[feature] migrate to monorepo
2025-02-21 00:49:20 +08:00

614 lines
19 KiB
Go

// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"fmt"
"math"
"tss-rocks-be/ent/contributor"
"tss-rocks-be/ent/contributorsociallink"
"tss-rocks-be/ent/predicate"
"entgo.io/ent"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
)
// ContributorSocialLinkQuery is the builder for querying ContributorSocialLink entities.
type ContributorSocialLinkQuery struct {
config
ctx *QueryContext
order []contributorsociallink.OrderOption
inters []Interceptor
predicates []predicate.ContributorSocialLink
withContributor *ContributorQuery
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 ContributorSocialLinkQuery builder.
func (cslq *ContributorSocialLinkQuery) Where(ps ...predicate.ContributorSocialLink) *ContributorSocialLinkQuery {
cslq.predicates = append(cslq.predicates, ps...)
return cslq
}
// Limit the number of records to be returned by this query.
func (cslq *ContributorSocialLinkQuery) Limit(limit int) *ContributorSocialLinkQuery {
cslq.ctx.Limit = &limit
return cslq
}
// Offset to start from.
func (cslq *ContributorSocialLinkQuery) Offset(offset int) *ContributorSocialLinkQuery {
cslq.ctx.Offset = &offset
return cslq
}
// 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 (cslq *ContributorSocialLinkQuery) Unique(unique bool) *ContributorSocialLinkQuery {
cslq.ctx.Unique = &unique
return cslq
}
// Order specifies how the records should be ordered.
func (cslq *ContributorSocialLinkQuery) Order(o ...contributorsociallink.OrderOption) *ContributorSocialLinkQuery {
cslq.order = append(cslq.order, o...)
return cslq
}
// QueryContributor chains the current query on the "contributor" edge.
func (cslq *ContributorSocialLinkQuery) QueryContributor() *ContributorQuery {
query := (&ContributorClient{config: cslq.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := cslq.prepareQuery(ctx); err != nil {
return nil, err
}
selector := cslq.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(contributorsociallink.Table, contributorsociallink.FieldID, selector),
sqlgraph.To(contributor.Table, contributor.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, contributorsociallink.ContributorTable, contributorsociallink.ContributorColumn),
)
fromU = sqlgraph.SetNeighbors(cslq.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first ContributorSocialLink entity from the query.
// Returns a *NotFoundError when no ContributorSocialLink was found.
func (cslq *ContributorSocialLinkQuery) First(ctx context.Context) (*ContributorSocialLink, error) {
nodes, err := cslq.Limit(1).All(setContextOp(ctx, cslq.ctx, ent.OpQueryFirst))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{contributorsociallink.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (cslq *ContributorSocialLinkQuery) FirstX(ctx context.Context) *ContributorSocialLink {
node, err := cslq.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first ContributorSocialLink ID from the query.
// Returns a *NotFoundError when no ContributorSocialLink ID was found.
func (cslq *ContributorSocialLinkQuery) FirstID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = cslq.Limit(1).IDs(setContextOp(ctx, cslq.ctx, ent.OpQueryFirstID)); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{contributorsociallink.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (cslq *ContributorSocialLinkQuery) FirstIDX(ctx context.Context) int {
id, err := cslq.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single ContributorSocialLink entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one ContributorSocialLink entity is found.
// Returns a *NotFoundError when no ContributorSocialLink entities are found.
func (cslq *ContributorSocialLinkQuery) Only(ctx context.Context) (*ContributorSocialLink, error) {
nodes, err := cslq.Limit(2).All(setContextOp(ctx, cslq.ctx, ent.OpQueryOnly))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{contributorsociallink.Label}
default:
return nil, &NotSingularError{contributorsociallink.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (cslq *ContributorSocialLinkQuery) OnlyX(ctx context.Context) *ContributorSocialLink {
node, err := cslq.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only ContributorSocialLink ID in the query.
// Returns a *NotSingularError when more than one ContributorSocialLink ID is found.
// Returns a *NotFoundError when no entities are found.
func (cslq *ContributorSocialLinkQuery) OnlyID(ctx context.Context) (id int, err error) {
var ids []int
if ids, err = cslq.Limit(2).IDs(setContextOp(ctx, cslq.ctx, ent.OpQueryOnlyID)); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{contributorsociallink.Label}
default:
err = &NotSingularError{contributorsociallink.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (cslq *ContributorSocialLinkQuery) OnlyIDX(ctx context.Context) int {
id, err := cslq.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of ContributorSocialLinks.
func (cslq *ContributorSocialLinkQuery) All(ctx context.Context) ([]*ContributorSocialLink, error) {
ctx = setContextOp(ctx, cslq.ctx, ent.OpQueryAll)
if err := cslq.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*ContributorSocialLink, *ContributorSocialLinkQuery]()
return withInterceptors[[]*ContributorSocialLink](ctx, cslq, qr, cslq.inters)
}
// AllX is like All, but panics if an error occurs.
func (cslq *ContributorSocialLinkQuery) AllX(ctx context.Context) []*ContributorSocialLink {
nodes, err := cslq.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of ContributorSocialLink IDs.
func (cslq *ContributorSocialLinkQuery) IDs(ctx context.Context) (ids []int, err error) {
if cslq.ctx.Unique == nil && cslq.path != nil {
cslq.Unique(true)
}
ctx = setContextOp(ctx, cslq.ctx, ent.OpQueryIDs)
if err = cslq.Select(contributorsociallink.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (cslq *ContributorSocialLinkQuery) IDsX(ctx context.Context) []int {
ids, err := cslq.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (cslq *ContributorSocialLinkQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, cslq.ctx, ent.OpQueryCount)
if err := cslq.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, cslq, querierCount[*ContributorSocialLinkQuery](), cslq.inters)
}
// CountX is like Count, but panics if an error occurs.
func (cslq *ContributorSocialLinkQuery) CountX(ctx context.Context) int {
count, err := cslq.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (cslq *ContributorSocialLinkQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, cslq.ctx, ent.OpQueryExist)
switch _, err := cslq.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 (cslq *ContributorSocialLinkQuery) ExistX(ctx context.Context) bool {
exist, err := cslq.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the ContributorSocialLinkQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (cslq *ContributorSocialLinkQuery) Clone() *ContributorSocialLinkQuery {
if cslq == nil {
return nil
}
return &ContributorSocialLinkQuery{
config: cslq.config,
ctx: cslq.ctx.Clone(),
order: append([]contributorsociallink.OrderOption{}, cslq.order...),
inters: append([]Interceptor{}, cslq.inters...),
predicates: append([]predicate.ContributorSocialLink{}, cslq.predicates...),
withContributor: cslq.withContributor.Clone(),
// clone intermediate query.
sql: cslq.sql.Clone(),
path: cslq.path,
}
}
// WithContributor tells the query-builder to eager-load the nodes that are connected to
// the "contributor" edge. The optional arguments are used to configure the query builder of the edge.
func (cslq *ContributorSocialLinkQuery) WithContributor(opts ...func(*ContributorQuery)) *ContributorSocialLinkQuery {
query := (&ContributorClient{config: cslq.config}).Query()
for _, opt := range opts {
opt(query)
}
cslq.withContributor = query
return cslq
}
// 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 {
// Type contributorsociallink.Type `json:"type,omitempty"`
// Count int `json:"count,omitempty"`
// }
//
// client.ContributorSocialLink.Query().
// GroupBy(contributorsociallink.FieldType).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (cslq *ContributorSocialLinkQuery) GroupBy(field string, fields ...string) *ContributorSocialLinkGroupBy {
cslq.ctx.Fields = append([]string{field}, fields...)
grbuild := &ContributorSocialLinkGroupBy{build: cslq}
grbuild.flds = &cslq.ctx.Fields
grbuild.label = contributorsociallink.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 {
// Type contributorsociallink.Type `json:"type,omitempty"`
// }
//
// client.ContributorSocialLink.Query().
// Select(contributorsociallink.FieldType).
// Scan(ctx, &v)
func (cslq *ContributorSocialLinkQuery) Select(fields ...string) *ContributorSocialLinkSelect {
cslq.ctx.Fields = append(cslq.ctx.Fields, fields...)
sbuild := &ContributorSocialLinkSelect{ContributorSocialLinkQuery: cslq}
sbuild.label = contributorsociallink.Label
sbuild.flds, sbuild.scan = &cslq.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a ContributorSocialLinkSelect configured with the given aggregations.
func (cslq *ContributorSocialLinkQuery) Aggregate(fns ...AggregateFunc) *ContributorSocialLinkSelect {
return cslq.Select().Aggregate(fns...)
}
func (cslq *ContributorSocialLinkQuery) prepareQuery(ctx context.Context) error {
for _, inter := range cslq.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, cslq); err != nil {
return err
}
}
}
for _, f := range cslq.ctx.Fields {
if !contributorsociallink.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if cslq.path != nil {
prev, err := cslq.path(ctx)
if err != nil {
return err
}
cslq.sql = prev
}
return nil
}
func (cslq *ContributorSocialLinkQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*ContributorSocialLink, error) {
var (
nodes = []*ContributorSocialLink{}
withFKs = cslq.withFKs
_spec = cslq.querySpec()
loadedTypes = [1]bool{
cslq.withContributor != nil,
}
)
if cslq.withContributor != nil {
withFKs = true
}
if withFKs {
_spec.Node.Columns = append(_spec.Node.Columns, contributorsociallink.ForeignKeys...)
}
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*ContributorSocialLink).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &ContributorSocialLink{config: cslq.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, cslq.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := cslq.withContributor; query != nil {
if err := cslq.loadContributor(ctx, query, nodes, nil,
func(n *ContributorSocialLink, e *Contributor) { n.Edges.Contributor = e }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (cslq *ContributorSocialLinkQuery) loadContributor(ctx context.Context, query *ContributorQuery, nodes []*ContributorSocialLink, init func(*ContributorSocialLink), assign func(*ContributorSocialLink, *Contributor)) error {
ids := make([]int, 0, len(nodes))
nodeids := make(map[int][]*ContributorSocialLink)
for i := range nodes {
if nodes[i].contributor_social_links == nil {
continue
}
fk := *nodes[i].contributor_social_links
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(contributor.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 "contributor_social_links" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (cslq *ContributorSocialLinkQuery) sqlCount(ctx context.Context) (int, error) {
_spec := cslq.querySpec()
_spec.Node.Columns = cslq.ctx.Fields
if len(cslq.ctx.Fields) > 0 {
_spec.Unique = cslq.ctx.Unique != nil && *cslq.ctx.Unique
}
return sqlgraph.CountNodes(ctx, cslq.driver, _spec)
}
func (cslq *ContributorSocialLinkQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(contributorsociallink.Table, contributorsociallink.Columns, sqlgraph.NewFieldSpec(contributorsociallink.FieldID, field.TypeInt))
_spec.From = cslq.sql
if unique := cslq.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if cslq.path != nil {
_spec.Unique = true
}
if fields := cslq.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, contributorsociallink.FieldID)
for i := range fields {
if fields[i] != contributorsociallink.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
}
if ps := cslq.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := cslq.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := cslq.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := cslq.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (cslq *ContributorSocialLinkQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(cslq.driver.Dialect())
t1 := builder.Table(contributorsociallink.Table)
columns := cslq.ctx.Fields
if len(columns) == 0 {
columns = contributorsociallink.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if cslq.sql != nil {
selector = cslq.sql
selector.Select(selector.Columns(columns...)...)
}
if cslq.ctx.Unique != nil && *cslq.ctx.Unique {
selector.Distinct()
}
for _, p := range cslq.predicates {
p(selector)
}
for _, p := range cslq.order {
p(selector)
}
if offset := cslq.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 := cslq.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// ContributorSocialLinkGroupBy is the group-by builder for ContributorSocialLink entities.
type ContributorSocialLinkGroupBy struct {
selector
build *ContributorSocialLinkQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (cslgb *ContributorSocialLinkGroupBy) Aggregate(fns ...AggregateFunc) *ContributorSocialLinkGroupBy {
cslgb.fns = append(cslgb.fns, fns...)
return cslgb
}
// Scan applies the selector query and scans the result into the given value.
func (cslgb *ContributorSocialLinkGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, cslgb.build.ctx, ent.OpQueryGroupBy)
if err := cslgb.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*ContributorSocialLinkQuery, *ContributorSocialLinkGroupBy](ctx, cslgb.build, cslgb, cslgb.build.inters, v)
}
func (cslgb *ContributorSocialLinkGroupBy) sqlScan(ctx context.Context, root *ContributorSocialLinkQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(cslgb.fns))
for _, fn := range cslgb.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*cslgb.flds)+len(cslgb.fns))
for _, f := range *cslgb.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*cslgb.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := cslgb.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// ContributorSocialLinkSelect is the builder for selecting fields of ContributorSocialLink entities.
type ContributorSocialLinkSelect struct {
*ContributorSocialLinkQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (csls *ContributorSocialLinkSelect) Aggregate(fns ...AggregateFunc) *ContributorSocialLinkSelect {
csls.fns = append(csls.fns, fns...)
return csls
}
// Scan applies the selector query and scans the result into the given value.
func (csls *ContributorSocialLinkSelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, csls.ctx, ent.OpQuerySelect)
if err := csls.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*ContributorSocialLinkQuery, *ContributorSocialLinkSelect](ctx, csls.ContributorSocialLinkQuery, csls, csls.inters, v)
}
func (csls *ContributorSocialLinkSelect) sqlScan(ctx context.Context, root *ContributorSocialLinkQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(csls.fns))
for _, fn := range csls.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*csls.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 := csls.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}