tss-rocks/backend/ent/postcontent_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
18 KiB
Go

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