user_query.go
1 // Code generated by ent, DO NOT EDIT. 2 3 package ent 4 5 import ( 6 "context" 7 "database/sql/driver" 8 "fmt" 9 "math" 10 11 "entgo.io/ent/dialect/sql" 12 "entgo.io/ent/dialect/sql/sqlgraph" 13 "entgo.io/ent/schema/field" 14 "github.com/google/uuid" 15 "github.com/mrusme/journalist/ent/feed" 16 "github.com/mrusme/journalist/ent/item" 17 "github.com/mrusme/journalist/ent/predicate" 18 "github.com/mrusme/journalist/ent/read" 19 "github.com/mrusme/journalist/ent/subscription" 20 "github.com/mrusme/journalist/ent/token" 21 "github.com/mrusme/journalist/ent/user" 22 ) 23 24 // UserQuery is the builder for querying User entities. 25 type UserQuery struct { 26 config 27 ctx *QueryContext 28 order []user.OrderOption 29 inters []Interceptor 30 predicates []predicate.User 31 withTokens *TokenQuery 32 withSubscribedFeeds *FeedQuery 33 withReadItems *ItemQuery 34 withSubscriptions *SubscriptionQuery 35 withReads *ReadQuery 36 // intermediate query (i.e. traversal path). 37 sql *sql.Selector 38 path func(context.Context) (*sql.Selector, error) 39 } 40 41 // Where adds a new predicate for the UserQuery builder. 42 func (uq *UserQuery) Where(ps ...predicate.User) *UserQuery { 43 uq.predicates = append(uq.predicates, ps...) 44 return uq 45 } 46 47 // Limit the number of records to be returned by this query. 48 func (uq *UserQuery) Limit(limit int) *UserQuery { 49 uq.ctx.Limit = &limit 50 return uq 51 } 52 53 // Offset to start from. 54 func (uq *UserQuery) Offset(offset int) *UserQuery { 55 uq.ctx.Offset = &offset 56 return uq 57 } 58 59 // Unique configures the query builder to filter duplicate records on query. 60 // By default, unique is set to true, and can be disabled using this method. 61 func (uq *UserQuery) Unique(unique bool) *UserQuery { 62 uq.ctx.Unique = &unique 63 return uq 64 } 65 66 // Order specifies how the records should be ordered. 67 func (uq *UserQuery) Order(o ...user.OrderOption) *UserQuery { 68 uq.order = append(uq.order, o...) 69 return uq 70 } 71 72 // QueryTokens chains the current query on the "tokens" edge. 73 func (uq *UserQuery) QueryTokens() *TokenQuery { 74 query := (&TokenClient{config: uq.config}).Query() 75 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 76 if err := uq.prepareQuery(ctx); err != nil { 77 return nil, err 78 } 79 selector := uq.sqlQuery(ctx) 80 if err := selector.Err(); err != nil { 81 return nil, err 82 } 83 step := sqlgraph.NewStep( 84 sqlgraph.From(user.Table, user.FieldID, selector), 85 sqlgraph.To(token.Table, token.FieldID), 86 sqlgraph.Edge(sqlgraph.O2M, false, user.TokensTable, user.TokensColumn), 87 ) 88 fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step) 89 return fromU, nil 90 } 91 return query 92 } 93 94 // QuerySubscribedFeeds chains the current query on the "subscribed_feeds" edge. 95 func (uq *UserQuery) QuerySubscribedFeeds() *FeedQuery { 96 query := (&FeedClient{config: uq.config}).Query() 97 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 98 if err := uq.prepareQuery(ctx); err != nil { 99 return nil, err 100 } 101 selector := uq.sqlQuery(ctx) 102 if err := selector.Err(); err != nil { 103 return nil, err 104 } 105 step := sqlgraph.NewStep( 106 sqlgraph.From(user.Table, user.FieldID, selector), 107 sqlgraph.To(feed.Table, feed.FieldID), 108 sqlgraph.Edge(sqlgraph.M2M, false, user.SubscribedFeedsTable, user.SubscribedFeedsPrimaryKey...), 109 ) 110 fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step) 111 return fromU, nil 112 } 113 return query 114 } 115 116 // QueryReadItems chains the current query on the "read_items" edge. 117 func (uq *UserQuery) QueryReadItems() *ItemQuery { 118 query := (&ItemClient{config: uq.config}).Query() 119 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 120 if err := uq.prepareQuery(ctx); err != nil { 121 return nil, err 122 } 123 selector := uq.sqlQuery(ctx) 124 if err := selector.Err(); err != nil { 125 return nil, err 126 } 127 step := sqlgraph.NewStep( 128 sqlgraph.From(user.Table, user.FieldID, selector), 129 sqlgraph.To(item.Table, item.FieldID), 130 sqlgraph.Edge(sqlgraph.M2M, false, user.ReadItemsTable, user.ReadItemsPrimaryKey...), 131 ) 132 fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step) 133 return fromU, nil 134 } 135 return query 136 } 137 138 // QuerySubscriptions chains the current query on the "subscriptions" edge. 139 func (uq *UserQuery) QuerySubscriptions() *SubscriptionQuery { 140 query := (&SubscriptionClient{config: uq.config}).Query() 141 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 142 if err := uq.prepareQuery(ctx); err != nil { 143 return nil, err 144 } 145 selector := uq.sqlQuery(ctx) 146 if err := selector.Err(); err != nil { 147 return nil, err 148 } 149 step := sqlgraph.NewStep( 150 sqlgraph.From(user.Table, user.FieldID, selector), 151 sqlgraph.To(subscription.Table, subscription.FieldID), 152 sqlgraph.Edge(sqlgraph.O2M, true, user.SubscriptionsTable, user.SubscriptionsColumn), 153 ) 154 fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step) 155 return fromU, nil 156 } 157 return query 158 } 159 160 // QueryReads chains the current query on the "reads" edge. 161 func (uq *UserQuery) QueryReads() *ReadQuery { 162 query := (&ReadClient{config: uq.config}).Query() 163 query.path = func(ctx context.Context) (fromU *sql.Selector, err error) { 164 if err := uq.prepareQuery(ctx); err != nil { 165 return nil, err 166 } 167 selector := uq.sqlQuery(ctx) 168 if err := selector.Err(); err != nil { 169 return nil, err 170 } 171 step := sqlgraph.NewStep( 172 sqlgraph.From(user.Table, user.FieldID, selector), 173 sqlgraph.To(read.Table, read.FieldID), 174 sqlgraph.Edge(sqlgraph.O2M, true, user.ReadsTable, user.ReadsColumn), 175 ) 176 fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step) 177 return fromU, nil 178 } 179 return query 180 } 181 182 // First returns the first User entity from the query. 183 // Returns a *NotFoundError when no User was found. 184 func (uq *UserQuery) First(ctx context.Context) (*User, error) { 185 nodes, err := uq.Limit(1).All(setContextOp(ctx, uq.ctx, "First")) 186 if err != nil { 187 return nil, err 188 } 189 if len(nodes) == 0 { 190 return nil, &NotFoundError{user.Label} 191 } 192 return nodes[0], nil 193 } 194 195 // FirstX is like First, but panics if an error occurs. 196 func (uq *UserQuery) FirstX(ctx context.Context) *User { 197 node, err := uq.First(ctx) 198 if err != nil && !IsNotFound(err) { 199 panic(err) 200 } 201 return node 202 } 203 204 // FirstID returns the first User ID from the query. 205 // Returns a *NotFoundError when no User ID was found. 206 func (uq *UserQuery) FirstID(ctx context.Context) (id uuid.UUID, err error) { 207 var ids []uuid.UUID 208 if ids, err = uq.Limit(1).IDs(setContextOp(ctx, uq.ctx, "FirstID")); err != nil { 209 return 210 } 211 if len(ids) == 0 { 212 err = &NotFoundError{user.Label} 213 return 214 } 215 return ids[0], nil 216 } 217 218 // FirstIDX is like FirstID, but panics if an error occurs. 219 func (uq *UserQuery) FirstIDX(ctx context.Context) uuid.UUID { 220 id, err := uq.FirstID(ctx) 221 if err != nil && !IsNotFound(err) { 222 panic(err) 223 } 224 return id 225 } 226 227 // Only returns a single User entity found by the query, ensuring it only returns one. 228 // Returns a *NotSingularError when more than one User entity is found. 229 // Returns a *NotFoundError when no User entities are found. 230 func (uq *UserQuery) Only(ctx context.Context) (*User, error) { 231 nodes, err := uq.Limit(2).All(setContextOp(ctx, uq.ctx, "Only")) 232 if err != nil { 233 return nil, err 234 } 235 switch len(nodes) { 236 case 1: 237 return nodes[0], nil 238 case 0: 239 return nil, &NotFoundError{user.Label} 240 default: 241 return nil, &NotSingularError{user.Label} 242 } 243 } 244 245 // OnlyX is like Only, but panics if an error occurs. 246 func (uq *UserQuery) OnlyX(ctx context.Context) *User { 247 node, err := uq.Only(ctx) 248 if err != nil { 249 panic(err) 250 } 251 return node 252 } 253 254 // OnlyID is like Only, but returns the only User ID in the query. 255 // Returns a *NotSingularError when more than one User ID is found. 256 // Returns a *NotFoundError when no entities are found. 257 func (uq *UserQuery) OnlyID(ctx context.Context) (id uuid.UUID, err error) { 258 var ids []uuid.UUID 259 if ids, err = uq.Limit(2).IDs(setContextOp(ctx, uq.ctx, "OnlyID")); err != nil { 260 return 261 } 262 switch len(ids) { 263 case 1: 264 id = ids[0] 265 case 0: 266 err = &NotFoundError{user.Label} 267 default: 268 err = &NotSingularError{user.Label} 269 } 270 return 271 } 272 273 // OnlyIDX is like OnlyID, but panics if an error occurs. 274 func (uq *UserQuery) OnlyIDX(ctx context.Context) uuid.UUID { 275 id, err := uq.OnlyID(ctx) 276 if err != nil { 277 panic(err) 278 } 279 return id 280 } 281 282 // All executes the query and returns a list of Users. 283 func (uq *UserQuery) All(ctx context.Context) ([]*User, error) { 284 ctx = setContextOp(ctx, uq.ctx, "All") 285 if err := uq.prepareQuery(ctx); err != nil { 286 return nil, err 287 } 288 qr := querierAll[[]*User, *UserQuery]() 289 return withInterceptors[[]*User](ctx, uq, qr, uq.inters) 290 } 291 292 // AllX is like All, but panics if an error occurs. 293 func (uq *UserQuery) AllX(ctx context.Context) []*User { 294 nodes, err := uq.All(ctx) 295 if err != nil { 296 panic(err) 297 } 298 return nodes 299 } 300 301 // IDs executes the query and returns a list of User IDs. 302 func (uq *UserQuery) IDs(ctx context.Context) (ids []uuid.UUID, err error) { 303 if uq.ctx.Unique == nil && uq.path != nil { 304 uq.Unique(true) 305 } 306 ctx = setContextOp(ctx, uq.ctx, "IDs") 307 if err = uq.Select(user.FieldID).Scan(ctx, &ids); err != nil { 308 return nil, err 309 } 310 return ids, nil 311 } 312 313 // IDsX is like IDs, but panics if an error occurs. 314 func (uq *UserQuery) IDsX(ctx context.Context) []uuid.UUID { 315 ids, err := uq.IDs(ctx) 316 if err != nil { 317 panic(err) 318 } 319 return ids 320 } 321 322 // Count returns the count of the given query. 323 func (uq *UserQuery) Count(ctx context.Context) (int, error) { 324 ctx = setContextOp(ctx, uq.ctx, "Count") 325 if err := uq.prepareQuery(ctx); err != nil { 326 return 0, err 327 } 328 return withInterceptors[int](ctx, uq, querierCount[*UserQuery](), uq.inters) 329 } 330 331 // CountX is like Count, but panics if an error occurs. 332 func (uq *UserQuery) CountX(ctx context.Context) int { 333 count, err := uq.Count(ctx) 334 if err != nil { 335 panic(err) 336 } 337 return count 338 } 339 340 // Exist returns true if the query has elements in the graph. 341 func (uq *UserQuery) Exist(ctx context.Context) (bool, error) { 342 ctx = setContextOp(ctx, uq.ctx, "Exist") 343 switch _, err := uq.FirstID(ctx); { 344 case IsNotFound(err): 345 return false, nil 346 case err != nil: 347 return false, fmt.Errorf("ent: check existence: %w", err) 348 default: 349 return true, nil 350 } 351 } 352 353 // ExistX is like Exist, but panics if an error occurs. 354 func (uq *UserQuery) ExistX(ctx context.Context) bool { 355 exist, err := uq.Exist(ctx) 356 if err != nil { 357 panic(err) 358 } 359 return exist 360 } 361 362 // Clone returns a duplicate of the UserQuery builder, including all associated steps. It can be 363 // used to prepare common query builders and use them differently after the clone is made. 364 func (uq *UserQuery) Clone() *UserQuery { 365 if uq == nil { 366 return nil 367 } 368 return &UserQuery{ 369 config: uq.config, 370 ctx: uq.ctx.Clone(), 371 order: append([]user.OrderOption{}, uq.order...), 372 inters: append([]Interceptor{}, uq.inters...), 373 predicates: append([]predicate.User{}, uq.predicates...), 374 withTokens: uq.withTokens.Clone(), 375 withSubscribedFeeds: uq.withSubscribedFeeds.Clone(), 376 withReadItems: uq.withReadItems.Clone(), 377 withSubscriptions: uq.withSubscriptions.Clone(), 378 withReads: uq.withReads.Clone(), 379 // clone intermediate query. 380 sql: uq.sql.Clone(), 381 path: uq.path, 382 } 383 } 384 385 // WithTokens tells the query-builder to eager-load the nodes that are connected to 386 // the "tokens" edge. The optional arguments are used to configure the query builder of the edge. 387 func (uq *UserQuery) WithTokens(opts ...func(*TokenQuery)) *UserQuery { 388 query := (&TokenClient{config: uq.config}).Query() 389 for _, opt := range opts { 390 opt(query) 391 } 392 uq.withTokens = query 393 return uq 394 } 395 396 // WithSubscribedFeeds tells the query-builder to eager-load the nodes that are connected to 397 // the "subscribed_feeds" edge. The optional arguments are used to configure the query builder of the edge. 398 func (uq *UserQuery) WithSubscribedFeeds(opts ...func(*FeedQuery)) *UserQuery { 399 query := (&FeedClient{config: uq.config}).Query() 400 for _, opt := range opts { 401 opt(query) 402 } 403 uq.withSubscribedFeeds = query 404 return uq 405 } 406 407 // WithReadItems tells the query-builder to eager-load the nodes that are connected to 408 // the "read_items" edge. The optional arguments are used to configure the query builder of the edge. 409 func (uq *UserQuery) WithReadItems(opts ...func(*ItemQuery)) *UserQuery { 410 query := (&ItemClient{config: uq.config}).Query() 411 for _, opt := range opts { 412 opt(query) 413 } 414 uq.withReadItems = query 415 return uq 416 } 417 418 // WithSubscriptions tells the query-builder to eager-load the nodes that are connected to 419 // the "subscriptions" edge. The optional arguments are used to configure the query builder of the edge. 420 func (uq *UserQuery) WithSubscriptions(opts ...func(*SubscriptionQuery)) *UserQuery { 421 query := (&SubscriptionClient{config: uq.config}).Query() 422 for _, opt := range opts { 423 opt(query) 424 } 425 uq.withSubscriptions = query 426 return uq 427 } 428 429 // WithReads tells the query-builder to eager-load the nodes that are connected to 430 // the "reads" edge. The optional arguments are used to configure the query builder of the edge. 431 func (uq *UserQuery) WithReads(opts ...func(*ReadQuery)) *UserQuery { 432 query := (&ReadClient{config: uq.config}).Query() 433 for _, opt := range opts { 434 opt(query) 435 } 436 uq.withReads = query 437 return uq 438 } 439 440 // GroupBy is used to group vertices by one or more fields/columns. 441 // It is often used with aggregate functions, like: count, max, mean, min, sum. 442 // 443 // Example: 444 // 445 // var v []struct { 446 // Username string `json:"username,omitempty"` 447 // Count int `json:"count,omitempty"` 448 // } 449 // 450 // client.User.Query(). 451 // GroupBy(user.FieldUsername). 452 // Aggregate(ent.Count()). 453 // Scan(ctx, &v) 454 func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy { 455 uq.ctx.Fields = append([]string{field}, fields...) 456 grbuild := &UserGroupBy{build: uq} 457 grbuild.flds = &uq.ctx.Fields 458 grbuild.label = user.Label 459 grbuild.scan = grbuild.Scan 460 return grbuild 461 } 462 463 // Select allows the selection one or more fields/columns for the given query, 464 // instead of selecting all fields in the entity. 465 // 466 // Example: 467 // 468 // var v []struct { 469 // Username string `json:"username,omitempty"` 470 // } 471 // 472 // client.User.Query(). 473 // Select(user.FieldUsername). 474 // Scan(ctx, &v) 475 func (uq *UserQuery) Select(fields ...string) *UserSelect { 476 uq.ctx.Fields = append(uq.ctx.Fields, fields...) 477 sbuild := &UserSelect{UserQuery: uq} 478 sbuild.label = user.Label 479 sbuild.flds, sbuild.scan = &uq.ctx.Fields, sbuild.Scan 480 return sbuild 481 } 482 483 // Aggregate returns a UserSelect configured with the given aggregations. 484 func (uq *UserQuery) Aggregate(fns ...AggregateFunc) *UserSelect { 485 return uq.Select().Aggregate(fns...) 486 } 487 488 func (uq *UserQuery) prepareQuery(ctx context.Context) error { 489 for _, inter := range uq.inters { 490 if inter == nil { 491 return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)") 492 } 493 if trv, ok := inter.(Traverser); ok { 494 if err := trv.Traverse(ctx, uq); err != nil { 495 return err 496 } 497 } 498 } 499 for _, f := range uq.ctx.Fields { 500 if !user.ValidColumn(f) { 501 return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 502 } 503 } 504 if uq.path != nil { 505 prev, err := uq.path(ctx) 506 if err != nil { 507 return err 508 } 509 uq.sql = prev 510 } 511 return nil 512 } 513 514 func (uq *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, error) { 515 var ( 516 nodes = []*User{} 517 _spec = uq.querySpec() 518 loadedTypes = [5]bool{ 519 uq.withTokens != nil, 520 uq.withSubscribedFeeds != nil, 521 uq.withReadItems != nil, 522 uq.withSubscriptions != nil, 523 uq.withReads != nil, 524 } 525 ) 526 _spec.ScanValues = func(columns []string) ([]any, error) { 527 return (*User).scanValues(nil, columns) 528 } 529 _spec.Assign = func(columns []string, values []any) error { 530 node := &User{config: uq.config} 531 nodes = append(nodes, node) 532 node.Edges.loadedTypes = loadedTypes 533 return node.assignValues(columns, values) 534 } 535 for i := range hooks { 536 hooks[i](ctx, _spec) 537 } 538 if err := sqlgraph.QueryNodes(ctx, uq.driver, _spec); err != nil { 539 return nil, err 540 } 541 if len(nodes) == 0 { 542 return nodes, nil 543 } 544 if query := uq.withTokens; query != nil { 545 if err := uq.loadTokens(ctx, query, nodes, 546 func(n *User) { n.Edges.Tokens = []*Token{} }, 547 func(n *User, e *Token) { n.Edges.Tokens = append(n.Edges.Tokens, e) }); err != nil { 548 return nil, err 549 } 550 } 551 if query := uq.withSubscribedFeeds; query != nil { 552 if err := uq.loadSubscribedFeeds(ctx, query, nodes, 553 func(n *User) { n.Edges.SubscribedFeeds = []*Feed{} }, 554 func(n *User, e *Feed) { n.Edges.SubscribedFeeds = append(n.Edges.SubscribedFeeds, e) }); err != nil { 555 return nil, err 556 } 557 } 558 if query := uq.withReadItems; query != nil { 559 if err := uq.loadReadItems(ctx, query, nodes, 560 func(n *User) { n.Edges.ReadItems = []*Item{} }, 561 func(n *User, e *Item) { n.Edges.ReadItems = append(n.Edges.ReadItems, e) }); err != nil { 562 return nil, err 563 } 564 } 565 if query := uq.withSubscriptions; query != nil { 566 if err := uq.loadSubscriptions(ctx, query, nodes, 567 func(n *User) { n.Edges.Subscriptions = []*Subscription{} }, 568 func(n *User, e *Subscription) { n.Edges.Subscriptions = append(n.Edges.Subscriptions, e) }); err != nil { 569 return nil, err 570 } 571 } 572 if query := uq.withReads; query != nil { 573 if err := uq.loadReads(ctx, query, nodes, 574 func(n *User) { n.Edges.Reads = []*Read{} }, 575 func(n *User, e *Read) { n.Edges.Reads = append(n.Edges.Reads, e) }); err != nil { 576 return nil, err 577 } 578 } 579 return nodes, nil 580 } 581 582 func (uq *UserQuery) loadTokens(ctx context.Context, query *TokenQuery, nodes []*User, init func(*User), assign func(*User, *Token)) error { 583 fks := make([]driver.Value, 0, len(nodes)) 584 nodeids := make(map[uuid.UUID]*User) 585 for i := range nodes { 586 fks = append(fks, nodes[i].ID) 587 nodeids[nodes[i].ID] = nodes[i] 588 if init != nil { 589 init(nodes[i]) 590 } 591 } 592 query.withFKs = true 593 query.Where(predicate.Token(func(s *sql.Selector) { 594 s.Where(sql.InValues(s.C(user.TokensColumn), fks...)) 595 })) 596 neighbors, err := query.All(ctx) 597 if err != nil { 598 return err 599 } 600 for _, n := range neighbors { 601 fk := n.user_tokens 602 if fk == nil { 603 return fmt.Errorf(`foreign-key "user_tokens" is nil for node %v`, n.ID) 604 } 605 node, ok := nodeids[*fk] 606 if !ok { 607 return fmt.Errorf(`unexpected referenced foreign-key "user_tokens" returned %v for node %v`, *fk, n.ID) 608 } 609 assign(node, n) 610 } 611 return nil 612 } 613 func (uq *UserQuery) loadSubscribedFeeds(ctx context.Context, query *FeedQuery, nodes []*User, init func(*User), assign func(*User, *Feed)) error { 614 edgeIDs := make([]driver.Value, len(nodes)) 615 byID := make(map[uuid.UUID]*User) 616 nids := make(map[uuid.UUID]map[*User]struct{}) 617 for i, node := range nodes { 618 edgeIDs[i] = node.ID 619 byID[node.ID] = node 620 if init != nil { 621 init(node) 622 } 623 } 624 query.Where(func(s *sql.Selector) { 625 joinT := sql.Table(user.SubscribedFeedsTable) 626 s.Join(joinT).On(s.C(feed.FieldID), joinT.C(user.SubscribedFeedsPrimaryKey[1])) 627 s.Where(sql.InValues(joinT.C(user.SubscribedFeedsPrimaryKey[0]), edgeIDs...)) 628 columns := s.SelectedColumns() 629 s.Select(joinT.C(user.SubscribedFeedsPrimaryKey[0])) 630 s.AppendSelect(columns...) 631 s.SetDistinct(false) 632 }) 633 if err := query.prepareQuery(ctx); err != nil { 634 return err 635 } 636 qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) { 637 return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) { 638 assign := spec.Assign 639 values := spec.ScanValues 640 spec.ScanValues = func(columns []string) ([]any, error) { 641 values, err := values(columns[1:]) 642 if err != nil { 643 return nil, err 644 } 645 return append([]any{new(uuid.UUID)}, values...), nil 646 } 647 spec.Assign = func(columns []string, values []any) error { 648 outValue := *values[0].(*uuid.UUID) 649 inValue := *values[1].(*uuid.UUID) 650 if nids[inValue] == nil { 651 nids[inValue] = map[*User]struct{}{byID[outValue]: {}} 652 return assign(columns[1:], values[1:]) 653 } 654 nids[inValue][byID[outValue]] = struct{}{} 655 return nil 656 } 657 }) 658 }) 659 neighbors, err := withInterceptors[[]*Feed](ctx, query, qr, query.inters) 660 if err != nil { 661 return err 662 } 663 for _, n := range neighbors { 664 nodes, ok := nids[n.ID] 665 if !ok { 666 return fmt.Errorf(`unexpected "subscribed_feeds" node returned %v`, n.ID) 667 } 668 for kn := range nodes { 669 assign(kn, n) 670 } 671 } 672 return nil 673 } 674 func (uq *UserQuery) loadReadItems(ctx context.Context, query *ItemQuery, nodes []*User, init func(*User), assign func(*User, *Item)) error { 675 edgeIDs := make([]driver.Value, len(nodes)) 676 byID := make(map[uuid.UUID]*User) 677 nids := make(map[uuid.UUID]map[*User]struct{}) 678 for i, node := range nodes { 679 edgeIDs[i] = node.ID 680 byID[node.ID] = node 681 if init != nil { 682 init(node) 683 } 684 } 685 query.Where(func(s *sql.Selector) { 686 joinT := sql.Table(user.ReadItemsTable) 687 s.Join(joinT).On(s.C(item.FieldID), joinT.C(user.ReadItemsPrimaryKey[1])) 688 s.Where(sql.InValues(joinT.C(user.ReadItemsPrimaryKey[0]), edgeIDs...)) 689 columns := s.SelectedColumns() 690 s.Select(joinT.C(user.ReadItemsPrimaryKey[0])) 691 s.AppendSelect(columns...) 692 s.SetDistinct(false) 693 }) 694 if err := query.prepareQuery(ctx); err != nil { 695 return err 696 } 697 qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) { 698 return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) { 699 assign := spec.Assign 700 values := spec.ScanValues 701 spec.ScanValues = func(columns []string) ([]any, error) { 702 values, err := values(columns[1:]) 703 if err != nil { 704 return nil, err 705 } 706 return append([]any{new(uuid.UUID)}, values...), nil 707 } 708 spec.Assign = func(columns []string, values []any) error { 709 outValue := *values[0].(*uuid.UUID) 710 inValue := *values[1].(*uuid.UUID) 711 if nids[inValue] == nil { 712 nids[inValue] = map[*User]struct{}{byID[outValue]: {}} 713 return assign(columns[1:], values[1:]) 714 } 715 nids[inValue][byID[outValue]] = struct{}{} 716 return nil 717 } 718 }) 719 }) 720 neighbors, err := withInterceptors[[]*Item](ctx, query, qr, query.inters) 721 if err != nil { 722 return err 723 } 724 for _, n := range neighbors { 725 nodes, ok := nids[n.ID] 726 if !ok { 727 return fmt.Errorf(`unexpected "read_items" node returned %v`, n.ID) 728 } 729 for kn := range nodes { 730 assign(kn, n) 731 } 732 } 733 return nil 734 } 735 func (uq *UserQuery) loadSubscriptions(ctx context.Context, query *SubscriptionQuery, nodes []*User, init func(*User), assign func(*User, *Subscription)) error { 736 fks := make([]driver.Value, 0, len(nodes)) 737 nodeids := make(map[uuid.UUID]*User) 738 for i := range nodes { 739 fks = append(fks, nodes[i].ID) 740 nodeids[nodes[i].ID] = nodes[i] 741 if init != nil { 742 init(nodes[i]) 743 } 744 } 745 if len(query.ctx.Fields) > 0 { 746 query.ctx.AppendFieldOnce(subscription.FieldUserID) 747 } 748 query.Where(predicate.Subscription(func(s *sql.Selector) { 749 s.Where(sql.InValues(s.C(user.SubscriptionsColumn), fks...)) 750 })) 751 neighbors, err := query.All(ctx) 752 if err != nil { 753 return err 754 } 755 for _, n := range neighbors { 756 fk := n.UserID 757 node, ok := nodeids[fk] 758 if !ok { 759 return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID) 760 } 761 assign(node, n) 762 } 763 return nil 764 } 765 func (uq *UserQuery) loadReads(ctx context.Context, query *ReadQuery, nodes []*User, init func(*User), assign func(*User, *Read)) error { 766 fks := make([]driver.Value, 0, len(nodes)) 767 nodeids := make(map[uuid.UUID]*User) 768 for i := range nodes { 769 fks = append(fks, nodes[i].ID) 770 nodeids[nodes[i].ID] = nodes[i] 771 if init != nil { 772 init(nodes[i]) 773 } 774 } 775 if len(query.ctx.Fields) > 0 { 776 query.ctx.AppendFieldOnce(read.FieldUserID) 777 } 778 query.Where(predicate.Read(func(s *sql.Selector) { 779 s.Where(sql.InValues(s.C(user.ReadsColumn), fks...)) 780 })) 781 neighbors, err := query.All(ctx) 782 if err != nil { 783 return err 784 } 785 for _, n := range neighbors { 786 fk := n.UserID 787 node, ok := nodeids[fk] 788 if !ok { 789 return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID) 790 } 791 assign(node, n) 792 } 793 return nil 794 } 795 796 func (uq *UserQuery) sqlCount(ctx context.Context) (int, error) { 797 _spec := uq.querySpec() 798 _spec.Node.Columns = uq.ctx.Fields 799 if len(uq.ctx.Fields) > 0 { 800 _spec.Unique = uq.ctx.Unique != nil && *uq.ctx.Unique 801 } 802 return sqlgraph.CountNodes(ctx, uq.driver, _spec) 803 } 804 805 func (uq *UserQuery) querySpec() *sqlgraph.QuerySpec { 806 _spec := sqlgraph.NewQuerySpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeUUID)) 807 _spec.From = uq.sql 808 if unique := uq.ctx.Unique; unique != nil { 809 _spec.Unique = *unique 810 } else if uq.path != nil { 811 _spec.Unique = true 812 } 813 if fields := uq.ctx.Fields; len(fields) > 0 { 814 _spec.Node.Columns = make([]string, 0, len(fields)) 815 _spec.Node.Columns = append(_spec.Node.Columns, user.FieldID) 816 for i := range fields { 817 if fields[i] != user.FieldID { 818 _spec.Node.Columns = append(_spec.Node.Columns, fields[i]) 819 } 820 } 821 } 822 if ps := uq.predicates; len(ps) > 0 { 823 _spec.Predicate = func(selector *sql.Selector) { 824 for i := range ps { 825 ps[i](selector) 826 } 827 } 828 } 829 if limit := uq.ctx.Limit; limit != nil { 830 _spec.Limit = *limit 831 } 832 if offset := uq.ctx.Offset; offset != nil { 833 _spec.Offset = *offset 834 } 835 if ps := uq.order; len(ps) > 0 { 836 _spec.Order = func(selector *sql.Selector) { 837 for i := range ps { 838 ps[i](selector) 839 } 840 } 841 } 842 return _spec 843 } 844 845 func (uq *UserQuery) sqlQuery(ctx context.Context) *sql.Selector { 846 builder := sql.Dialect(uq.driver.Dialect()) 847 t1 := builder.Table(user.Table) 848 columns := uq.ctx.Fields 849 if len(columns) == 0 { 850 columns = user.Columns 851 } 852 selector := builder.Select(t1.Columns(columns...)...).From(t1) 853 if uq.sql != nil { 854 selector = uq.sql 855 selector.Select(selector.Columns(columns...)...) 856 } 857 if uq.ctx.Unique != nil && *uq.ctx.Unique { 858 selector.Distinct() 859 } 860 for _, p := range uq.predicates { 861 p(selector) 862 } 863 for _, p := range uq.order { 864 p(selector) 865 } 866 if offset := uq.ctx.Offset; offset != nil { 867 // limit is mandatory for offset clause. We start 868 // with default value, and override it below if needed. 869 selector.Offset(*offset).Limit(math.MaxInt32) 870 } 871 if limit := uq.ctx.Limit; limit != nil { 872 selector.Limit(*limit) 873 } 874 return selector 875 } 876 877 // UserGroupBy is the group-by builder for User entities. 878 type UserGroupBy struct { 879 selector 880 build *UserQuery 881 } 882 883 // Aggregate adds the given aggregation functions to the group-by query. 884 func (ugb *UserGroupBy) Aggregate(fns ...AggregateFunc) *UserGroupBy { 885 ugb.fns = append(ugb.fns, fns...) 886 return ugb 887 } 888 889 // Scan applies the selector query and scans the result into the given value. 890 func (ugb *UserGroupBy) Scan(ctx context.Context, v any) error { 891 ctx = setContextOp(ctx, ugb.build.ctx, "GroupBy") 892 if err := ugb.build.prepareQuery(ctx); err != nil { 893 return err 894 } 895 return scanWithInterceptors[*UserQuery, *UserGroupBy](ctx, ugb.build, ugb, ugb.build.inters, v) 896 } 897 898 func (ugb *UserGroupBy) sqlScan(ctx context.Context, root *UserQuery, v any) error { 899 selector := root.sqlQuery(ctx).Select() 900 aggregation := make([]string, 0, len(ugb.fns)) 901 for _, fn := range ugb.fns { 902 aggregation = append(aggregation, fn(selector)) 903 } 904 if len(selector.SelectedColumns()) == 0 { 905 columns := make([]string, 0, len(*ugb.flds)+len(ugb.fns)) 906 for _, f := range *ugb.flds { 907 columns = append(columns, selector.C(f)) 908 } 909 columns = append(columns, aggregation...) 910 selector.Select(columns...) 911 } 912 selector.GroupBy(selector.Columns(*ugb.flds...)...) 913 if err := selector.Err(); err != nil { 914 return err 915 } 916 rows := &sql.Rows{} 917 query, args := selector.Query() 918 if err := ugb.build.driver.Query(ctx, query, args, rows); err != nil { 919 return err 920 } 921 defer rows.Close() 922 return sql.ScanSlice(rows, v) 923 } 924 925 // UserSelect is the builder for selecting fields of User entities. 926 type UserSelect struct { 927 *UserQuery 928 selector 929 } 930 931 // Aggregate adds the given aggregation functions to the selector query. 932 func (us *UserSelect) Aggregate(fns ...AggregateFunc) *UserSelect { 933 us.fns = append(us.fns, fns...) 934 return us 935 } 936 937 // Scan applies the selector query and scans the result into the given value. 938 func (us *UserSelect) Scan(ctx context.Context, v any) error { 939 ctx = setContextOp(ctx, us.ctx, "Select") 940 if err := us.prepareQuery(ctx); err != nil { 941 return err 942 } 943 return scanWithInterceptors[*UserQuery, *UserSelect](ctx, us.UserQuery, us, us.inters, v) 944 } 945 946 func (us *UserSelect) sqlScan(ctx context.Context, root *UserQuery, v any) error { 947 selector := root.sqlQuery(ctx) 948 aggregation := make([]string, 0, len(us.fns)) 949 for _, fn := range us.fns { 950 aggregation = append(aggregation, fn(selector)) 951 } 952 switch n := len(*us.selector.flds); { 953 case n == 0 && len(aggregation) > 0: 954 selector.Select(aggregation...) 955 case n != 0 && len(aggregation) > 0: 956 selector.AppendSelect(aggregation...) 957 } 958 rows := &sql.Rows{} 959 query, args := selector.Query() 960 if err := us.driver.Query(ctx, query, args, rows); err != nil { 961 return err 962 } 963 defer rows.Close() 964 return sql.ScanSlice(rows, v) 965 }