post_query.go•56.8 kB
// 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/Southclaws/storyden/internal/ent/account"
"github.com/Southclaws/storyden/internal/ent/asset"
"github.com/Southclaws/storyden/internal/ent/category"
"github.com/Southclaws/storyden/internal/ent/collection"
"github.com/Southclaws/storyden/internal/ent/event"
"github.com/Southclaws/storyden/internal/ent/likepost"
"github.com/Southclaws/storyden/internal/ent/link"
"github.com/Southclaws/storyden/internal/ent/mentionprofile"
"github.com/Southclaws/storyden/internal/ent/post"
"github.com/Southclaws/storyden/internal/ent/postread"
"github.com/Southclaws/storyden/internal/ent/predicate"
"github.com/Southclaws/storyden/internal/ent/react"
"github.com/Southclaws/storyden/internal/ent/tag"
"github.com/rs/xid"
)
// PostQuery is the builder for querying Post entities.
type PostQuery struct {
config
ctx *QueryContext
order []post.OrderOption
inters []Interceptor
predicates []predicate.Post
withAuthor *AccountQuery
withCategory *CategoryQuery
withTags *TagQuery
withRoot *PostQuery
withPosts *PostQuery
withReplyTo *PostQuery
withReplies *PostQuery
withReacts *ReactQuery
withLikes *LikePostQuery
withMentions *MentionProfileQuery
withAssets *AssetQuery
withCollections *CollectionQuery
withLink *LinkQuery
withContentLinks *LinkQuery
withEvent *EventQuery
withPostReads *PostReadQuery
modifiers []func(*sql.Selector)
// intermediate query (i.e. traversal path).
sql *sql.Selector
path func(context.Context) (*sql.Selector, error)
}
// Where adds a new predicate for the PostQuery builder.
func (_q *PostQuery) Where(ps ...predicate.Post) *PostQuery {
_q.predicates = append(_q.predicates, ps...)
return _q
}
// Limit the number of records to be returned by this query.
func (_q *PostQuery) Limit(limit int) *PostQuery {
_q.ctx.Limit = &limit
return _q
}
// Offset to start from.
func (_q *PostQuery) Offset(offset int) *PostQuery {
_q.ctx.Offset = &offset
return _q
}
// 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 (_q *PostQuery) Unique(unique bool) *PostQuery {
_q.ctx.Unique = &unique
return _q
}
// Order specifies how the records should be ordered.
func (_q *PostQuery) Order(o ...post.OrderOption) *PostQuery {
_q.order = append(_q.order, o...)
return _q
}
// QueryAuthor chains the current query on the "author" edge.
func (_q *PostQuery) QueryAuthor() *AccountQuery {
query := (&AccountClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(account.Table, account.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, post.AuthorTable, post.AuthorColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryCategory chains the current query on the "category" edge.
func (_q *PostQuery) QueryCategory() *CategoryQuery {
query := (&CategoryClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(category.Table, category.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, post.CategoryTable, post.CategoryColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryTags chains the current query on the "tags" edge.
func (_q *PostQuery) QueryTags() *TagQuery {
query := (&TagClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(tag.Table, tag.FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, post.TagsTable, post.TagsPrimaryKey...),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryRoot chains the current query on the "root" edge.
func (_q *PostQuery) QueryRoot() *PostQuery {
query := (&PostClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(post.Table, post.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, post.RootTable, post.RootColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryPosts chains the current query on the "posts" edge.
func (_q *PostQuery) QueryPosts() *PostQuery {
query := (&PostClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(post.Table, post.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, post.PostsTable, post.PostsColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryReplyTo chains the current query on the "replyTo" edge.
func (_q *PostQuery) QueryReplyTo() *PostQuery {
query := (&PostClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(post.Table, post.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, post.ReplyToTable, post.ReplyToColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryReplies chains the current query on the "replies" edge.
func (_q *PostQuery) QueryReplies() *PostQuery {
query := (&PostClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(post.Table, post.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, post.RepliesTable, post.RepliesColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryReacts chains the current query on the "reacts" edge.
func (_q *PostQuery) QueryReacts() *ReactQuery {
query := (&ReactClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(react.Table, react.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, post.ReactsTable, post.ReactsColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryLikes chains the current query on the "likes" edge.
func (_q *PostQuery) QueryLikes() *LikePostQuery {
query := (&LikePostClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(likepost.Table, likepost.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, post.LikesTable, post.LikesColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryMentions chains the current query on the "mentions" edge.
func (_q *PostQuery) QueryMentions() *MentionProfileQuery {
query := (&MentionProfileClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(mentionprofile.Table, mentionprofile.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, post.MentionsTable, post.MentionsColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryAssets chains the current query on the "assets" edge.
func (_q *PostQuery) QueryAssets() *AssetQuery {
query := (&AssetClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(asset.Table, asset.FieldID),
sqlgraph.Edge(sqlgraph.M2M, false, post.AssetsTable, post.AssetsPrimaryKey...),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryCollections chains the current query on the "collections" edge.
func (_q *PostQuery) QueryCollections() *CollectionQuery {
query := (&CollectionClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(collection.Table, collection.FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, post.CollectionsTable, post.CollectionsPrimaryKey...),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryLink chains the current query on the "link" edge.
func (_q *PostQuery) QueryLink() *LinkQuery {
query := (&LinkClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(link.Table, link.FieldID),
sqlgraph.Edge(sqlgraph.M2O, true, post.LinkTable, post.LinkColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryContentLinks chains the current query on the "content_links" edge.
func (_q *PostQuery) QueryContentLinks() *LinkQuery {
query := (&LinkClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(link.Table, link.FieldID),
sqlgraph.Edge(sqlgraph.M2M, true, post.ContentLinksTable, post.ContentLinksPrimaryKey...),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryEvent chains the current query on the "event" edge.
func (_q *PostQuery) QueryEvent() *EventQuery {
query := (&EventClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(event.Table, event.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, post.EventTable, post.EventColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// QueryPostReads chains the current query on the "post_reads" edge.
func (_q *PostQuery) QueryPostReads() *PostReadQuery {
query := (&PostReadClient{config: _q.config}).Query()
query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
selector := _q.sqlQuery(ctx)
if err := selector.Err(); err != nil {
return nil, err
}
step := sqlgraph.NewStep(
sqlgraph.From(post.Table, post.FieldID, selector),
sqlgraph.To(postread.Table, postread.FieldID),
sqlgraph.Edge(sqlgraph.O2M, false, post.PostReadsTable, post.PostReadsColumn),
)
fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
return fromU, nil
}
return query
}
// First returns the first Post entity from the query.
// Returns a *NotFoundError when no Post was found.
func (_q *PostQuery) First(ctx context.Context) (*Post, error) {
nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst))
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{post.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func (_q *PostQuery) FirstX(ctx context.Context) *Post {
node, err := _q.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first Post ID from the query.
// Returns a *NotFoundError when no Post ID was found.
func (_q *PostQuery) FirstID(ctx context.Context) (id xid.ID, err error) {
var ids []xid.ID
if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{post.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func (_q *PostQuery) FirstIDX(ctx context.Context) xid.ID {
id, err := _q.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single Post entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when more than one Post entity is found.
// Returns a *NotFoundError when no Post entities are found.
func (_q *PostQuery) Only(ctx context.Context) (*Post, error) {
nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{post.Label}
default:
return nil, &NotSingularError{post.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func (_q *PostQuery) OnlyX(ctx context.Context) *Post {
node, err := _q.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only Post ID in the query.
// Returns a *NotSingularError when more than one Post ID is found.
// Returns a *NotFoundError when no entities are found.
func (_q *PostQuery) OnlyID(ctx context.Context) (id xid.ID, err error) {
var ids []xid.ID
if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{post.Label}
default:
err = &NotSingularError{post.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func (_q *PostQuery) OnlyIDX(ctx context.Context) xid.ID {
id, err := _q.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of Posts.
func (_q *PostQuery) All(ctx context.Context) ([]*Post, error) {
ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll)
if err := _q.prepareQuery(ctx); err != nil {
return nil, err
}
qr := querierAll[[]*Post, *PostQuery]()
return withInterceptors[[]*Post](ctx, _q, qr, _q.inters)
}
// AllX is like All, but panics if an error occurs.
func (_q *PostQuery) AllX(ctx context.Context) []*Post {
nodes, err := _q.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of Post IDs.
func (_q *PostQuery) IDs(ctx context.Context) (ids []xid.ID, err error) {
if _q.ctx.Unique == nil && _q.path != nil {
_q.Unique(true)
}
ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs)
if err = _q.Select(post.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func (_q *PostQuery) IDsX(ctx context.Context) []xid.ID {
ids, err := _q.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func (_q *PostQuery) Count(ctx context.Context) (int, error) {
ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount)
if err := _q.prepareQuery(ctx); err != nil {
return 0, err
}
return withInterceptors[int](ctx, _q, querierCount[*PostQuery](), _q.inters)
}
// CountX is like Count, but panics if an error occurs.
func (_q *PostQuery) CountX(ctx context.Context) int {
count, err := _q.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func (_q *PostQuery) Exist(ctx context.Context) (bool, error) {
ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist)
switch _, err := _q.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 (_q *PostQuery) ExistX(ctx context.Context) bool {
exist, err := _q.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the PostQuery builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func (_q *PostQuery) Clone() *PostQuery {
if _q == nil {
return nil
}
return &PostQuery{
config: _q.config,
ctx: _q.ctx.Clone(),
order: append([]post.OrderOption{}, _q.order...),
inters: append([]Interceptor{}, _q.inters...),
predicates: append([]predicate.Post{}, _q.predicates...),
withAuthor: _q.withAuthor.Clone(),
withCategory: _q.withCategory.Clone(),
withTags: _q.withTags.Clone(),
withRoot: _q.withRoot.Clone(),
withPosts: _q.withPosts.Clone(),
withReplyTo: _q.withReplyTo.Clone(),
withReplies: _q.withReplies.Clone(),
withReacts: _q.withReacts.Clone(),
withLikes: _q.withLikes.Clone(),
withMentions: _q.withMentions.Clone(),
withAssets: _q.withAssets.Clone(),
withCollections: _q.withCollections.Clone(),
withLink: _q.withLink.Clone(),
withContentLinks: _q.withContentLinks.Clone(),
withEvent: _q.withEvent.Clone(),
withPostReads: _q.withPostReads.Clone(),
// clone intermediate query.
sql: _q.sql.Clone(),
path: _q.path,
modifiers: append([]func(*sql.Selector){}, _q.modifiers...),
}
}
// WithAuthor tells the query-builder to eager-load the nodes that are connected to
// the "author" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithAuthor(opts ...func(*AccountQuery)) *PostQuery {
query := (&AccountClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withAuthor = query
return _q
}
// WithCategory tells the query-builder to eager-load the nodes that are connected to
// the "category" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithCategory(opts ...func(*CategoryQuery)) *PostQuery {
query := (&CategoryClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withCategory = query
return _q
}
// WithTags tells the query-builder to eager-load the nodes that are connected to
// the "tags" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithTags(opts ...func(*TagQuery)) *PostQuery {
query := (&TagClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withTags = query
return _q
}
// WithRoot tells the query-builder to eager-load the nodes that are connected to
// the "root" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithRoot(opts ...func(*PostQuery)) *PostQuery {
query := (&PostClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withRoot = query
return _q
}
// WithPosts tells the query-builder to eager-load the nodes that are connected to
// the "posts" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithPosts(opts ...func(*PostQuery)) *PostQuery {
query := (&PostClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withPosts = query
return _q
}
// WithReplyTo tells the query-builder to eager-load the nodes that are connected to
// the "replyTo" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithReplyTo(opts ...func(*PostQuery)) *PostQuery {
query := (&PostClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withReplyTo = query
return _q
}
// WithReplies tells the query-builder to eager-load the nodes that are connected to
// the "replies" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithReplies(opts ...func(*PostQuery)) *PostQuery {
query := (&PostClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withReplies = query
return _q
}
// WithReacts tells the query-builder to eager-load the nodes that are connected to
// the "reacts" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithReacts(opts ...func(*ReactQuery)) *PostQuery {
query := (&ReactClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withReacts = query
return _q
}
// WithLikes tells the query-builder to eager-load the nodes that are connected to
// the "likes" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithLikes(opts ...func(*LikePostQuery)) *PostQuery {
query := (&LikePostClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withLikes = query
return _q
}
// WithMentions tells the query-builder to eager-load the nodes that are connected to
// the "mentions" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithMentions(opts ...func(*MentionProfileQuery)) *PostQuery {
query := (&MentionProfileClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withMentions = query
return _q
}
// WithAssets tells the query-builder to eager-load the nodes that are connected to
// the "assets" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithAssets(opts ...func(*AssetQuery)) *PostQuery {
query := (&AssetClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withAssets = query
return _q
}
// WithCollections tells the query-builder to eager-load the nodes that are connected to
// the "collections" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithCollections(opts ...func(*CollectionQuery)) *PostQuery {
query := (&CollectionClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withCollections = query
return _q
}
// WithLink tells the query-builder to eager-load the nodes that are connected to
// the "link" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithLink(opts ...func(*LinkQuery)) *PostQuery {
query := (&LinkClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withLink = query
return _q
}
// WithContentLinks tells the query-builder to eager-load the nodes that are connected to
// the "content_links" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithContentLinks(opts ...func(*LinkQuery)) *PostQuery {
query := (&LinkClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withContentLinks = query
return _q
}
// WithEvent tells the query-builder to eager-load the nodes that are connected to
// the "event" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithEvent(opts ...func(*EventQuery)) *PostQuery {
query := (&EventClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withEvent = query
return _q
}
// WithPostReads tells the query-builder to eager-load the nodes that are connected to
// the "post_reads" edge. The optional arguments are used to configure the query builder of the edge.
func (_q *PostQuery) WithPostReads(opts ...func(*PostReadQuery)) *PostQuery {
query := (&PostReadClient{config: _q.config}).Query()
for _, opt := range opts {
opt(query)
}
_q.withPostReads = query
return _q
}
// 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.Post.Query().
// GroupBy(post.FieldCreatedAt).
// Aggregate(ent.Count()).
// Scan(ctx, &v)
func (_q *PostQuery) GroupBy(field string, fields ...string) *PostGroupBy {
_q.ctx.Fields = append([]string{field}, fields...)
grbuild := &PostGroupBy{build: _q}
grbuild.flds = &_q.ctx.Fields
grbuild.label = post.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.Post.Query().
// Select(post.FieldCreatedAt).
// Scan(ctx, &v)
func (_q *PostQuery) Select(fields ...string) *PostSelect {
_q.ctx.Fields = append(_q.ctx.Fields, fields...)
sbuild := &PostSelect{PostQuery: _q}
sbuild.label = post.Label
sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan
return sbuild
}
// Aggregate returns a PostSelect configured with the given aggregations.
func (_q *PostQuery) Aggregate(fns ...AggregateFunc) *PostSelect {
return _q.Select().Aggregate(fns...)
}
func (_q *PostQuery) prepareQuery(ctx context.Context) error {
for _, inter := range _q.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, _q); err != nil {
return err
}
}
}
for _, f := range _q.ctx.Fields {
if !post.ValidColumn(f) {
return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
}
if _q.path != nil {
prev, err := _q.path(ctx)
if err != nil {
return err
}
_q.sql = prev
}
return nil
}
func (_q *PostQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Post, error) {
var (
nodes = []*Post{}
_spec = _q.querySpec()
loadedTypes = [16]bool{
_q.withAuthor != nil,
_q.withCategory != nil,
_q.withTags != nil,
_q.withRoot != nil,
_q.withPosts != nil,
_q.withReplyTo != nil,
_q.withReplies != nil,
_q.withReacts != nil,
_q.withLikes != nil,
_q.withMentions != nil,
_q.withAssets != nil,
_q.withCollections != nil,
_q.withLink != nil,
_q.withContentLinks != nil,
_q.withEvent != nil,
_q.withPostReads != nil,
}
)
_spec.ScanValues = func(columns []string) ([]any, error) {
return (*Post).scanValues(nil, columns)
}
_spec.Assign = func(columns []string, values []any) error {
node := &Post{config: _q.config}
nodes = append(nodes, node)
node.Edges.loadedTypes = loadedTypes
return node.assignValues(columns, values)
}
if len(_q.modifiers) > 0 {
_spec.Modifiers = _q.modifiers
}
for i := range hooks {
hooks[i](ctx, _spec)
}
if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil {
return nil, err
}
if len(nodes) == 0 {
return nodes, nil
}
if query := _q.withAuthor; query != nil {
if err := _q.loadAuthor(ctx, query, nodes, nil,
func(n *Post, e *Account) { n.Edges.Author = e }); err != nil {
return nil, err
}
}
if query := _q.withCategory; query != nil {
if err := _q.loadCategory(ctx, query, nodes, nil,
func(n *Post, e *Category) { n.Edges.Category = e }); err != nil {
return nil, err
}
}
if query := _q.withTags; query != nil {
if err := _q.loadTags(ctx, query, nodes,
func(n *Post) { n.Edges.Tags = []*Tag{} },
func(n *Post, e *Tag) { n.Edges.Tags = append(n.Edges.Tags, e) }); err != nil {
return nil, err
}
}
if query := _q.withRoot; query != nil {
if err := _q.loadRoot(ctx, query, nodes, nil,
func(n *Post, e *Post) { n.Edges.Root = e }); err != nil {
return nil, err
}
}
if query := _q.withPosts; query != nil {
if err := _q.loadPosts(ctx, query, nodes,
func(n *Post) { n.Edges.Posts = []*Post{} },
func(n *Post, e *Post) { n.Edges.Posts = append(n.Edges.Posts, e) }); err != nil {
return nil, err
}
}
if query := _q.withReplyTo; query != nil {
if err := _q.loadReplyTo(ctx, query, nodes, nil,
func(n *Post, e *Post) { n.Edges.ReplyTo = e }); err != nil {
return nil, err
}
}
if query := _q.withReplies; query != nil {
if err := _q.loadReplies(ctx, query, nodes,
func(n *Post) { n.Edges.Replies = []*Post{} },
func(n *Post, e *Post) { n.Edges.Replies = append(n.Edges.Replies, e) }); err != nil {
return nil, err
}
}
if query := _q.withReacts; query != nil {
if err := _q.loadReacts(ctx, query, nodes,
func(n *Post) { n.Edges.Reacts = []*React{} },
func(n *Post, e *React) { n.Edges.Reacts = append(n.Edges.Reacts, e) }); err != nil {
return nil, err
}
}
if query := _q.withLikes; query != nil {
if err := _q.loadLikes(ctx, query, nodes,
func(n *Post) { n.Edges.Likes = []*LikePost{} },
func(n *Post, e *LikePost) { n.Edges.Likes = append(n.Edges.Likes, e) }); err != nil {
return nil, err
}
}
if query := _q.withMentions; query != nil {
if err := _q.loadMentions(ctx, query, nodes,
func(n *Post) { n.Edges.Mentions = []*MentionProfile{} },
func(n *Post, e *MentionProfile) { n.Edges.Mentions = append(n.Edges.Mentions, e) }); err != nil {
return nil, err
}
}
if query := _q.withAssets; query != nil {
if err := _q.loadAssets(ctx, query, nodes,
func(n *Post) { n.Edges.Assets = []*Asset{} },
func(n *Post, e *Asset) { n.Edges.Assets = append(n.Edges.Assets, e) }); err != nil {
return nil, err
}
}
if query := _q.withCollections; query != nil {
if err := _q.loadCollections(ctx, query, nodes,
func(n *Post) { n.Edges.Collections = []*Collection{} },
func(n *Post, e *Collection) { n.Edges.Collections = append(n.Edges.Collections, e) }); err != nil {
return nil, err
}
}
if query := _q.withLink; query != nil {
if err := _q.loadLink(ctx, query, nodes, nil,
func(n *Post, e *Link) { n.Edges.Link = e }); err != nil {
return nil, err
}
}
if query := _q.withContentLinks; query != nil {
if err := _q.loadContentLinks(ctx, query, nodes,
func(n *Post) { n.Edges.ContentLinks = []*Link{} },
func(n *Post, e *Link) { n.Edges.ContentLinks = append(n.Edges.ContentLinks, e) }); err != nil {
return nil, err
}
}
if query := _q.withEvent; query != nil {
if err := _q.loadEvent(ctx, query, nodes,
func(n *Post) { n.Edges.Event = []*Event{} },
func(n *Post, e *Event) { n.Edges.Event = append(n.Edges.Event, e) }); err != nil {
return nil, err
}
}
if query := _q.withPostReads; query != nil {
if err := _q.loadPostReads(ctx, query, nodes,
func(n *Post) { n.Edges.PostReads = []*PostRead{} },
func(n *Post, e *PostRead) { n.Edges.PostReads = append(n.Edges.PostReads, e) }); err != nil {
return nil, err
}
}
return nodes, nil
}
func (_q *PostQuery) loadAuthor(ctx context.Context, query *AccountQuery, nodes []*Post, init func(*Post), assign func(*Post, *Account)) error {
ids := make([]xid.ID, 0, len(nodes))
nodeids := make(map[xid.ID][]*Post)
for i := range nodes {
fk := nodes[i].AccountPosts
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(account.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 "account_posts" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (_q *PostQuery) loadCategory(ctx context.Context, query *CategoryQuery, nodes []*Post, init func(*Post), assign func(*Post, *Category)) error {
ids := make([]xid.ID, 0, len(nodes))
nodeids := make(map[xid.ID][]*Post)
for i := range nodes {
fk := nodes[i].CategoryID
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(category.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 "category_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (_q *PostQuery) loadTags(ctx context.Context, query *TagQuery, nodes []*Post, init func(*Post), assign func(*Post, *Tag)) error {
edgeIDs := make([]driver.Value, len(nodes))
byID := make(map[xid.ID]*Post)
nids := make(map[xid.ID]map[*Post]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(post.TagsTable)
s.Join(joinT).On(s.C(tag.FieldID), joinT.C(post.TagsPrimaryKey[0]))
s.Where(sql.InValues(joinT.C(post.TagsPrimaryKey[1]), edgeIDs...))
columns := s.SelectedColumns()
s.Select(joinT.C(post.TagsPrimaryKey[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(xid.ID)}, values...), nil
}
spec.Assign = func(columns []string, values []any) error {
outValue := *values[0].(*xid.ID)
inValue := *values[1].(*xid.ID)
if nids[inValue] == nil {
nids[inValue] = map[*Post]struct{}{byID[outValue]: {}}
return assign(columns[1:], values[1:])
}
nids[inValue][byID[outValue]] = struct{}{}
return nil
}
})
})
neighbors, err := withInterceptors[[]*Tag](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 "tags" node returned %v`, n.ID)
}
for kn := range nodes {
assign(kn, n)
}
}
return nil
}
func (_q *PostQuery) loadRoot(ctx context.Context, query *PostQuery, nodes []*Post, init func(*Post), assign func(*Post, *Post)) error {
ids := make([]xid.ID, 0, len(nodes))
nodeids := make(map[xid.ID][]*Post)
for i := range nodes {
fk := nodes[i].RootPostID
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 "root_post_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (_q *PostQuery) loadPosts(ctx context.Context, query *PostQuery, nodes []*Post, init func(*Post), assign func(*Post, *Post)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[xid.ID]*Post)
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(post.FieldRootPostID)
}
query.Where(predicate.Post(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(post.PostsColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.RootPostID
node, ok := nodeids[fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "root_post_id" returned %v for node %v`, fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *PostQuery) loadReplyTo(ctx context.Context, query *PostQuery, nodes []*Post, init func(*Post), assign func(*Post, *Post)) error {
ids := make([]xid.ID, 0, len(nodes))
nodeids := make(map[xid.ID][]*Post)
for i := range nodes {
fk := nodes[i].ReplyToPostID
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 "reply_to_post_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (_q *PostQuery) loadReplies(ctx context.Context, query *PostQuery, nodes []*Post, init func(*Post), assign func(*Post, *Post)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[xid.ID]*Post)
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(post.FieldReplyToPostID)
}
query.Where(predicate.Post(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(post.RepliesColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.ReplyToPostID
node, ok := nodeids[fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "reply_to_post_id" returned %v for node %v`, fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *PostQuery) loadReacts(ctx context.Context, query *ReactQuery, nodes []*Post, init func(*Post), assign func(*Post, *React)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[xid.ID]*Post)
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(react.FieldPostID)
}
query.Where(predicate.React(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(post.ReactsColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.PostID
node, ok := nodeids[fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "post_id" returned %v for node %v`, fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *PostQuery) loadLikes(ctx context.Context, query *LikePostQuery, nodes []*Post, init func(*Post), assign func(*Post, *LikePost)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[xid.ID]*Post)
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(likepost.FieldPostID)
}
query.Where(predicate.LikePost(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(post.LikesColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.PostID
node, ok := nodeids[fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "post_id" returned %v for node %v`, fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *PostQuery) loadMentions(ctx context.Context, query *MentionProfileQuery, nodes []*Post, init func(*Post), assign func(*Post, *MentionProfile)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[xid.ID]*Post)
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(mentionprofile.FieldPostID)
}
query.Where(predicate.MentionProfile(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(post.MentionsColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.PostID
node, ok := nodeids[fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "post_id" returned %v for node %v`, fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *PostQuery) loadAssets(ctx context.Context, query *AssetQuery, nodes []*Post, init func(*Post), assign func(*Post, *Asset)) error {
edgeIDs := make([]driver.Value, len(nodes))
byID := make(map[xid.ID]*Post)
nids := make(map[xid.ID]map[*Post]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(post.AssetsTable)
s.Join(joinT).On(s.C(asset.FieldID), joinT.C(post.AssetsPrimaryKey[1]))
s.Where(sql.InValues(joinT.C(post.AssetsPrimaryKey[0]), edgeIDs...))
columns := s.SelectedColumns()
s.Select(joinT.C(post.AssetsPrimaryKey[0]))
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(xid.ID)}, values...), nil
}
spec.Assign = func(columns []string, values []any) error {
outValue := *values[0].(*xid.ID)
inValue := *values[1].(*xid.ID)
if nids[inValue] == nil {
nids[inValue] = map[*Post]struct{}{byID[outValue]: {}}
return assign(columns[1:], values[1:])
}
nids[inValue][byID[outValue]] = struct{}{}
return nil
}
})
})
neighbors, err := withInterceptors[[]*Asset](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 "assets" node returned %v`, n.ID)
}
for kn := range nodes {
assign(kn, n)
}
}
return nil
}
func (_q *PostQuery) loadCollections(ctx context.Context, query *CollectionQuery, nodes []*Post, init func(*Post), assign func(*Post, *Collection)) error {
edgeIDs := make([]driver.Value, len(nodes))
byID := make(map[xid.ID]*Post)
nids := make(map[xid.ID]map[*Post]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(post.CollectionsTable)
s.Join(joinT).On(s.C(collection.FieldID), joinT.C(post.CollectionsPrimaryKey[0]))
s.Where(sql.InValues(joinT.C(post.CollectionsPrimaryKey[1]), edgeIDs...))
columns := s.SelectedColumns()
s.Select(joinT.C(post.CollectionsPrimaryKey[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(xid.ID)}, values...), nil
}
spec.Assign = func(columns []string, values []any) error {
outValue := *values[0].(*xid.ID)
inValue := *values[1].(*xid.ID)
if nids[inValue] == nil {
nids[inValue] = map[*Post]struct{}{byID[outValue]: {}}
return assign(columns[1:], values[1:])
}
nids[inValue][byID[outValue]] = struct{}{}
return nil
}
})
})
neighbors, err := withInterceptors[[]*Collection](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 "collections" node returned %v`, n.ID)
}
for kn := range nodes {
assign(kn, n)
}
}
return nil
}
func (_q *PostQuery) loadLink(ctx context.Context, query *LinkQuery, nodes []*Post, init func(*Post), assign func(*Post, *Link)) error {
ids := make([]xid.ID, 0, len(nodes))
nodeids := make(map[xid.ID][]*Post)
for i := range nodes {
fk := nodes[i].LinkID
if _, ok := nodeids[fk]; !ok {
ids = append(ids, fk)
}
nodeids[fk] = append(nodeids[fk], nodes[i])
}
if len(ids) == 0 {
return nil
}
query.Where(link.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 "link_id" returned %v`, n.ID)
}
for i := range nodes {
assign(nodes[i], n)
}
}
return nil
}
func (_q *PostQuery) loadContentLinks(ctx context.Context, query *LinkQuery, nodes []*Post, init func(*Post), assign func(*Post, *Link)) error {
edgeIDs := make([]driver.Value, len(nodes))
byID := make(map[xid.ID]*Post)
nids := make(map[xid.ID]map[*Post]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(post.ContentLinksTable)
s.Join(joinT).On(s.C(link.FieldID), joinT.C(post.ContentLinksPrimaryKey[0]))
s.Where(sql.InValues(joinT.C(post.ContentLinksPrimaryKey[1]), edgeIDs...))
columns := s.SelectedColumns()
s.Select(joinT.C(post.ContentLinksPrimaryKey[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(xid.ID)}, values...), nil
}
spec.Assign = func(columns []string, values []any) error {
outValue := *values[0].(*xid.ID)
inValue := *values[1].(*xid.ID)
if nids[inValue] == nil {
nids[inValue] = map[*Post]struct{}{byID[outValue]: {}}
return assign(columns[1:], values[1:])
}
nids[inValue][byID[outValue]] = struct{}{}
return nil
}
})
})
neighbors, err := withInterceptors[[]*Link](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 "content_links" node returned %v`, n.ID)
}
for kn := range nodes {
assign(kn, n)
}
}
return nil
}
func (_q *PostQuery) loadEvent(ctx context.Context, query *EventQuery, nodes []*Post, init func(*Post), assign func(*Post, *Event)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[xid.ID]*Post)
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.Event(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(post.EventColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.post_event
if fk == nil {
return fmt.Errorf(`foreign-key "post_event" is nil for node %v`, n.ID)
}
node, ok := nodeids[*fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "post_event" returned %v for node %v`, *fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *PostQuery) loadPostReads(ctx context.Context, query *PostReadQuery, nodes []*Post, init func(*Post), assign func(*Post, *PostRead)) error {
fks := make([]driver.Value, 0, len(nodes))
nodeids := make(map[xid.ID]*Post)
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(postread.FieldRootPostID)
}
query.Where(predicate.PostRead(func(s *sql.Selector) {
s.Where(sql.InValues(s.C(post.PostReadsColumn), fks...))
}))
neighbors, err := query.All(ctx)
if err != nil {
return err
}
for _, n := range neighbors {
fk := n.RootPostID
node, ok := nodeids[fk]
if !ok {
return fmt.Errorf(`unexpected referenced foreign-key "root_post_id" returned %v for node %v`, fk, n.ID)
}
assign(node, n)
}
return nil
}
func (_q *PostQuery) sqlCount(ctx context.Context) (int, error) {
_spec := _q.querySpec()
if len(_q.modifiers) > 0 {
_spec.Modifiers = _q.modifiers
}
_spec.Node.Columns = _q.ctx.Fields
if len(_q.ctx.Fields) > 0 {
_spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique
}
return sqlgraph.CountNodes(ctx, _q.driver, _spec)
}
func (_q *PostQuery) querySpec() *sqlgraph.QuerySpec {
_spec := sqlgraph.NewQuerySpec(post.Table, post.Columns, sqlgraph.NewFieldSpec(post.FieldID, field.TypeString))
_spec.From = _q.sql
if unique := _q.ctx.Unique; unique != nil {
_spec.Unique = *unique
} else if _q.path != nil {
_spec.Unique = true
}
if fields := _q.ctx.Fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, post.FieldID)
for i := range fields {
if fields[i] != post.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, fields[i])
}
}
if _q.withAuthor != nil {
_spec.Node.AddColumnOnce(post.FieldAccountPosts)
}
if _q.withCategory != nil {
_spec.Node.AddColumnOnce(post.FieldCategoryID)
}
if _q.withRoot != nil {
_spec.Node.AddColumnOnce(post.FieldRootPostID)
}
if _q.withReplyTo != nil {
_spec.Node.AddColumnOnce(post.FieldReplyToPostID)
}
if _q.withLink != nil {
_spec.Node.AddColumnOnce(post.FieldLinkID)
}
}
if ps := _q.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if limit := _q.ctx.Limit; limit != nil {
_spec.Limit = *limit
}
if offset := _q.ctx.Offset; offset != nil {
_spec.Offset = *offset
}
if ps := _q.order; len(ps) > 0 {
_spec.Order = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
return _spec
}
func (_q *PostQuery) sqlQuery(ctx context.Context) *sql.Selector {
builder := sql.Dialect(_q.driver.Dialect())
t1 := builder.Table(post.Table)
columns := _q.ctx.Fields
if len(columns) == 0 {
columns = post.Columns
}
selector := builder.Select(t1.Columns(columns...)...).From(t1)
if _q.sql != nil {
selector = _q.sql
selector.Select(selector.Columns(columns...)...)
}
if _q.ctx.Unique != nil && *_q.ctx.Unique {
selector.Distinct()
}
for _, m := range _q.modifiers {
m(selector)
}
for _, p := range _q.predicates {
p(selector)
}
for _, p := range _q.order {
p(selector)
}
if offset := _q.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 := _q.ctx.Limit; limit != nil {
selector.Limit(*limit)
}
return selector
}
// Modify adds a query modifier for attaching custom logic to queries.
func (_q *PostQuery) Modify(modifiers ...func(s *sql.Selector)) *PostSelect {
_q.modifiers = append(_q.modifiers, modifiers...)
return _q.Select()
}
// PostGroupBy is the group-by builder for Post entities.
type PostGroupBy struct {
selector
build *PostQuery
}
// Aggregate adds the given aggregation functions to the group-by query.
func (_g *PostGroupBy) Aggregate(fns ...AggregateFunc) *PostGroupBy {
_g.fns = append(_g.fns, fns...)
return _g
}
// Scan applies the selector query and scans the result into the given value.
func (_g *PostGroupBy) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy)
if err := _g.build.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*PostQuery, *PostGroupBy](ctx, _g.build, _g, _g.build.inters, v)
}
func (_g *PostGroupBy) sqlScan(ctx context.Context, root *PostQuery, v any) error {
selector := root.sqlQuery(ctx).Select()
aggregation := make([]string, 0, len(_g.fns))
for _, fn := range _g.fns {
aggregation = append(aggregation, fn(selector))
}
if len(selector.SelectedColumns()) == 0 {
columns := make([]string, 0, len(*_g.flds)+len(_g.fns))
for _, f := range *_g.flds {
columns = append(columns, selector.C(f))
}
columns = append(columns, aggregation...)
selector.Select(columns...)
}
selector.GroupBy(selector.Columns(*_g.flds...)...)
if err := selector.Err(); err != nil {
return err
}
rows := &sql.Rows{}
query, args := selector.Query()
if err := _g.build.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// PostSelect is the builder for selecting fields of Post entities.
type PostSelect struct {
*PostQuery
selector
}
// Aggregate adds the given aggregation functions to the selector query.
func (_s *PostSelect) Aggregate(fns ...AggregateFunc) *PostSelect {
_s.fns = append(_s.fns, fns...)
return _s
}
// Scan applies the selector query and scans the result into the given value.
func (_s *PostSelect) Scan(ctx context.Context, v any) error {
ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect)
if err := _s.prepareQuery(ctx); err != nil {
return err
}
return scanWithInterceptors[*PostQuery, *PostSelect](ctx, _s.PostQuery, _s, _s.inters, v)
}
func (_s *PostSelect) sqlScan(ctx context.Context, root *PostQuery, v any) error {
selector := root.sqlQuery(ctx)
aggregation := make([]string, 0, len(_s.fns))
for _, fn := range _s.fns {
aggregation = append(aggregation, fn(selector))
}
switch n := len(*_s.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 := _s.driver.Query(ctx, query, args, rows); err != nil {
return err
}
defer rows.Close()
return sql.ScanSlice(rows, v)
}
// Modify adds a query modifier for attaching custom logic to queries.
func (_s *PostSelect) Modify(modifiers ...func(s *sql.Selector)) *PostSelect {
_s.modifiers = append(_s.modifiers, modifiers...)
return _s
}