usersubscription_query.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "database/sql/driver"
  6. "fmt"
  7. "math"
  8. "entgo.io/ent"
  9. "entgo.io/ent/dialect"
  10. "entgo.io/ent/dialect/sql"
  11. "entgo.io/ent/dialect/sql/sqlgraph"
  12. "entgo.io/ent/schema/field"
  13. "github.com/Wei-Shaw/sub2api/ent/group"
  14. "github.com/Wei-Shaw/sub2api/ent/predicate"
  15. "github.com/Wei-Shaw/sub2api/ent/usagelog"
  16. "github.com/Wei-Shaw/sub2api/ent/user"
  17. "github.com/Wei-Shaw/sub2api/ent/usersubscription"
  18. )
  19. // UserSubscriptionQuery is the builder for querying UserSubscription entities.
  20. type UserSubscriptionQuery struct {
  21. config
  22. ctx *QueryContext
  23. order []usersubscription.OrderOption
  24. inters []Interceptor
  25. predicates []predicate.UserSubscription
  26. withUser *UserQuery
  27. withGroup *GroupQuery
  28. withAssignedByUser *UserQuery
  29. withUsageLogs *UsageLogQuery
  30. modifiers []func(*sql.Selector)
  31. // intermediate query (i.e. traversal path).
  32. sql *sql.Selector
  33. path func(context.Context) (*sql.Selector, error)
  34. }
  35. // Where adds a new predicate for the UserSubscriptionQuery builder.
  36. func (_q *UserSubscriptionQuery) Where(ps ...predicate.UserSubscription) *UserSubscriptionQuery {
  37. _q.predicates = append(_q.predicates, ps...)
  38. return _q
  39. }
  40. // Limit the number of records to be returned by this query.
  41. func (_q *UserSubscriptionQuery) Limit(limit int) *UserSubscriptionQuery {
  42. _q.ctx.Limit = &limit
  43. return _q
  44. }
  45. // Offset to start from.
  46. func (_q *UserSubscriptionQuery) Offset(offset int) *UserSubscriptionQuery {
  47. _q.ctx.Offset = &offset
  48. return _q
  49. }
  50. // Unique configures the query builder to filter duplicate records on query.
  51. // By default, unique is set to true, and can be disabled using this method.
  52. func (_q *UserSubscriptionQuery) Unique(unique bool) *UserSubscriptionQuery {
  53. _q.ctx.Unique = &unique
  54. return _q
  55. }
  56. // Order specifies how the records should be ordered.
  57. func (_q *UserSubscriptionQuery) Order(o ...usersubscription.OrderOption) *UserSubscriptionQuery {
  58. _q.order = append(_q.order, o...)
  59. return _q
  60. }
  61. // QueryUser chains the current query on the "user" edge.
  62. func (_q *UserSubscriptionQuery) QueryUser() *UserQuery {
  63. query := (&UserClient{config: _q.config}).Query()
  64. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  65. if err := _q.prepareQuery(ctx); err != nil {
  66. return nil, err
  67. }
  68. selector := _q.sqlQuery(ctx)
  69. if err := selector.Err(); err != nil {
  70. return nil, err
  71. }
  72. step := sqlgraph.NewStep(
  73. sqlgraph.From(usersubscription.Table, usersubscription.FieldID, selector),
  74. sqlgraph.To(user.Table, user.FieldID),
  75. sqlgraph.Edge(sqlgraph.M2O, true, usersubscription.UserTable, usersubscription.UserColumn),
  76. )
  77. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  78. return fromU, nil
  79. }
  80. return query
  81. }
  82. // QueryGroup chains the current query on the "group" edge.
  83. func (_q *UserSubscriptionQuery) QueryGroup() *GroupQuery {
  84. query := (&GroupClient{config: _q.config}).Query()
  85. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  86. if err := _q.prepareQuery(ctx); err != nil {
  87. return nil, err
  88. }
  89. selector := _q.sqlQuery(ctx)
  90. if err := selector.Err(); err != nil {
  91. return nil, err
  92. }
  93. step := sqlgraph.NewStep(
  94. sqlgraph.From(usersubscription.Table, usersubscription.FieldID, selector),
  95. sqlgraph.To(group.Table, group.FieldID),
  96. sqlgraph.Edge(sqlgraph.M2O, true, usersubscription.GroupTable, usersubscription.GroupColumn),
  97. )
  98. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  99. return fromU, nil
  100. }
  101. return query
  102. }
  103. // QueryAssignedByUser chains the current query on the "assigned_by_user" edge.
  104. func (_q *UserSubscriptionQuery) QueryAssignedByUser() *UserQuery {
  105. query := (&UserClient{config: _q.config}).Query()
  106. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  107. if err := _q.prepareQuery(ctx); err != nil {
  108. return nil, err
  109. }
  110. selector := _q.sqlQuery(ctx)
  111. if err := selector.Err(); err != nil {
  112. return nil, err
  113. }
  114. step := sqlgraph.NewStep(
  115. sqlgraph.From(usersubscription.Table, usersubscription.FieldID, selector),
  116. sqlgraph.To(user.Table, user.FieldID),
  117. sqlgraph.Edge(sqlgraph.M2O, true, usersubscription.AssignedByUserTable, usersubscription.AssignedByUserColumn),
  118. )
  119. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  120. return fromU, nil
  121. }
  122. return query
  123. }
  124. // QueryUsageLogs chains the current query on the "usage_logs" edge.
  125. func (_q *UserSubscriptionQuery) QueryUsageLogs() *UsageLogQuery {
  126. query := (&UsageLogClient{config: _q.config}).Query()
  127. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  128. if err := _q.prepareQuery(ctx); err != nil {
  129. return nil, err
  130. }
  131. selector := _q.sqlQuery(ctx)
  132. if err := selector.Err(); err != nil {
  133. return nil, err
  134. }
  135. step := sqlgraph.NewStep(
  136. sqlgraph.From(usersubscription.Table, usersubscription.FieldID, selector),
  137. sqlgraph.To(usagelog.Table, usagelog.FieldID),
  138. sqlgraph.Edge(sqlgraph.O2M, false, usersubscription.UsageLogsTable, usersubscription.UsageLogsColumn),
  139. )
  140. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  141. return fromU, nil
  142. }
  143. return query
  144. }
  145. // First returns the first UserSubscription entity from the query.
  146. // Returns a *NotFoundError when no UserSubscription was found.
  147. func (_q *UserSubscriptionQuery) First(ctx context.Context) (*UserSubscription, error) {
  148. nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst))
  149. if err != nil {
  150. return nil, err
  151. }
  152. if len(nodes) == 0 {
  153. return nil, &NotFoundError{usersubscription.Label}
  154. }
  155. return nodes[0], nil
  156. }
  157. // FirstX is like First, but panics if an error occurs.
  158. func (_q *UserSubscriptionQuery) FirstX(ctx context.Context) *UserSubscription {
  159. node, err := _q.First(ctx)
  160. if err != nil && !IsNotFound(err) {
  161. panic(err)
  162. }
  163. return node
  164. }
  165. // FirstID returns the first UserSubscription ID from the query.
  166. // Returns a *NotFoundError when no UserSubscription ID was found.
  167. func (_q *UserSubscriptionQuery) FirstID(ctx context.Context) (id int64, err error) {
  168. var ids []int64
  169. if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil {
  170. return
  171. }
  172. if len(ids) == 0 {
  173. err = &NotFoundError{usersubscription.Label}
  174. return
  175. }
  176. return ids[0], nil
  177. }
  178. // FirstIDX is like FirstID, but panics if an error occurs.
  179. func (_q *UserSubscriptionQuery) FirstIDX(ctx context.Context) int64 {
  180. id, err := _q.FirstID(ctx)
  181. if err != nil && !IsNotFound(err) {
  182. panic(err)
  183. }
  184. return id
  185. }
  186. // Only returns a single UserSubscription entity found by the query, ensuring it only returns one.
  187. // Returns a *NotSingularError when more than one UserSubscription entity is found.
  188. // Returns a *NotFoundError when no UserSubscription entities are found.
  189. func (_q *UserSubscriptionQuery) Only(ctx context.Context) (*UserSubscription, error) {
  190. nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
  191. if err != nil {
  192. return nil, err
  193. }
  194. switch len(nodes) {
  195. case 1:
  196. return nodes[0], nil
  197. case 0:
  198. return nil, &NotFoundError{usersubscription.Label}
  199. default:
  200. return nil, &NotSingularError{usersubscription.Label}
  201. }
  202. }
  203. // OnlyX is like Only, but panics if an error occurs.
  204. func (_q *UserSubscriptionQuery) OnlyX(ctx context.Context) *UserSubscription {
  205. node, err := _q.Only(ctx)
  206. if err != nil {
  207. panic(err)
  208. }
  209. return node
  210. }
  211. // OnlyID is like Only, but returns the only UserSubscription ID in the query.
  212. // Returns a *NotSingularError when more than one UserSubscription ID is found.
  213. // Returns a *NotFoundError when no entities are found.
  214. func (_q *UserSubscriptionQuery) OnlyID(ctx context.Context) (id int64, err error) {
  215. var ids []int64
  216. if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil {
  217. return
  218. }
  219. switch len(ids) {
  220. case 1:
  221. id = ids[0]
  222. case 0:
  223. err = &NotFoundError{usersubscription.Label}
  224. default:
  225. err = &NotSingularError{usersubscription.Label}
  226. }
  227. return
  228. }
  229. // OnlyIDX is like OnlyID, but panics if an error occurs.
  230. func (_q *UserSubscriptionQuery) OnlyIDX(ctx context.Context) int64 {
  231. id, err := _q.OnlyID(ctx)
  232. if err != nil {
  233. panic(err)
  234. }
  235. return id
  236. }
  237. // All executes the query and returns a list of UserSubscriptions.
  238. func (_q *UserSubscriptionQuery) All(ctx context.Context) ([]*UserSubscription, error) {
  239. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll)
  240. if err := _q.prepareQuery(ctx); err != nil {
  241. return nil, err
  242. }
  243. qr := querierAll[[]*UserSubscription, *UserSubscriptionQuery]()
  244. return withInterceptors[[]*UserSubscription](ctx, _q, qr, _q.inters)
  245. }
  246. // AllX is like All, but panics if an error occurs.
  247. func (_q *UserSubscriptionQuery) AllX(ctx context.Context) []*UserSubscription {
  248. nodes, err := _q.All(ctx)
  249. if err != nil {
  250. panic(err)
  251. }
  252. return nodes
  253. }
  254. // IDs executes the query and returns a list of UserSubscription IDs.
  255. func (_q *UserSubscriptionQuery) IDs(ctx context.Context) (ids []int64, err error) {
  256. if _q.ctx.Unique == nil && _q.path != nil {
  257. _q.Unique(true)
  258. }
  259. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs)
  260. if err = _q.Select(usersubscription.FieldID).Scan(ctx, &ids); err != nil {
  261. return nil, err
  262. }
  263. return ids, nil
  264. }
  265. // IDsX is like IDs, but panics if an error occurs.
  266. func (_q *UserSubscriptionQuery) IDsX(ctx context.Context) []int64 {
  267. ids, err := _q.IDs(ctx)
  268. if err != nil {
  269. panic(err)
  270. }
  271. return ids
  272. }
  273. // Count returns the count of the given query.
  274. func (_q *UserSubscriptionQuery) Count(ctx context.Context) (int, error) {
  275. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount)
  276. if err := _q.prepareQuery(ctx); err != nil {
  277. return 0, err
  278. }
  279. return withInterceptors[int](ctx, _q, querierCount[*UserSubscriptionQuery](), _q.inters)
  280. }
  281. // CountX is like Count, but panics if an error occurs.
  282. func (_q *UserSubscriptionQuery) CountX(ctx context.Context) int {
  283. count, err := _q.Count(ctx)
  284. if err != nil {
  285. panic(err)
  286. }
  287. return count
  288. }
  289. // Exist returns true if the query has elements in the graph.
  290. func (_q *UserSubscriptionQuery) Exist(ctx context.Context) (bool, error) {
  291. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist)
  292. switch _, err := _q.FirstID(ctx); {
  293. case IsNotFound(err):
  294. return false, nil
  295. case err != nil:
  296. return false, fmt.Errorf("ent: check existence: %w", err)
  297. default:
  298. return true, nil
  299. }
  300. }
  301. // ExistX is like Exist, but panics if an error occurs.
  302. func (_q *UserSubscriptionQuery) ExistX(ctx context.Context) bool {
  303. exist, err := _q.Exist(ctx)
  304. if err != nil {
  305. panic(err)
  306. }
  307. return exist
  308. }
  309. // Clone returns a duplicate of the UserSubscriptionQuery builder, including all associated steps. It can be
  310. // used to prepare common query builders and use them differently after the clone is made.
  311. func (_q *UserSubscriptionQuery) Clone() *UserSubscriptionQuery {
  312. if _q == nil {
  313. return nil
  314. }
  315. return &UserSubscriptionQuery{
  316. config: _q.config,
  317. ctx: _q.ctx.Clone(),
  318. order: append([]usersubscription.OrderOption{}, _q.order...),
  319. inters: append([]Interceptor{}, _q.inters...),
  320. predicates: append([]predicate.UserSubscription{}, _q.predicates...),
  321. withUser: _q.withUser.Clone(),
  322. withGroup: _q.withGroup.Clone(),
  323. withAssignedByUser: _q.withAssignedByUser.Clone(),
  324. withUsageLogs: _q.withUsageLogs.Clone(),
  325. // clone intermediate query.
  326. sql: _q.sql.Clone(),
  327. path: _q.path,
  328. }
  329. }
  330. // WithUser tells the query-builder to eager-load the nodes that are connected to
  331. // the "user" edge. The optional arguments are used to configure the query builder of the edge.
  332. func (_q *UserSubscriptionQuery) WithUser(opts ...func(*UserQuery)) *UserSubscriptionQuery {
  333. query := (&UserClient{config: _q.config}).Query()
  334. for _, opt := range opts {
  335. opt(query)
  336. }
  337. _q.withUser = query
  338. return _q
  339. }
  340. // WithGroup tells the query-builder to eager-load the nodes that are connected to
  341. // the "group" edge. The optional arguments are used to configure the query builder of the edge.
  342. func (_q *UserSubscriptionQuery) WithGroup(opts ...func(*GroupQuery)) *UserSubscriptionQuery {
  343. query := (&GroupClient{config: _q.config}).Query()
  344. for _, opt := range opts {
  345. opt(query)
  346. }
  347. _q.withGroup = query
  348. return _q
  349. }
  350. // WithAssignedByUser tells the query-builder to eager-load the nodes that are connected to
  351. // the "assigned_by_user" edge. The optional arguments are used to configure the query builder of the edge.
  352. func (_q *UserSubscriptionQuery) WithAssignedByUser(opts ...func(*UserQuery)) *UserSubscriptionQuery {
  353. query := (&UserClient{config: _q.config}).Query()
  354. for _, opt := range opts {
  355. opt(query)
  356. }
  357. _q.withAssignedByUser = query
  358. return _q
  359. }
  360. // WithUsageLogs tells the query-builder to eager-load the nodes that are connected to
  361. // the "usage_logs" edge. The optional arguments are used to configure the query builder of the edge.
  362. func (_q *UserSubscriptionQuery) WithUsageLogs(opts ...func(*UsageLogQuery)) *UserSubscriptionQuery {
  363. query := (&UsageLogClient{config: _q.config}).Query()
  364. for _, opt := range opts {
  365. opt(query)
  366. }
  367. _q.withUsageLogs = query
  368. return _q
  369. }
  370. // GroupBy is used to group vertices by one or more fields/columns.
  371. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  372. //
  373. // Example:
  374. //
  375. // var v []struct {
  376. // CreatedAt time.Time `json:"created_at,omitempty"`
  377. // Count int `json:"count,omitempty"`
  378. // }
  379. //
  380. // client.UserSubscription.Query().
  381. // GroupBy(usersubscription.FieldCreatedAt).
  382. // Aggregate(ent.Count()).
  383. // Scan(ctx, &v)
  384. func (_q *UserSubscriptionQuery) GroupBy(field string, fields ...string) *UserSubscriptionGroupBy {
  385. _q.ctx.Fields = append([]string{field}, fields...)
  386. grbuild := &UserSubscriptionGroupBy{build: _q}
  387. grbuild.flds = &_q.ctx.Fields
  388. grbuild.label = usersubscription.Label
  389. grbuild.scan = grbuild.Scan
  390. return grbuild
  391. }
  392. // Select allows the selection one or more fields/columns for the given query,
  393. // instead of selecting all fields in the entity.
  394. //
  395. // Example:
  396. //
  397. // var v []struct {
  398. // CreatedAt time.Time `json:"created_at,omitempty"`
  399. // }
  400. //
  401. // client.UserSubscription.Query().
  402. // Select(usersubscription.FieldCreatedAt).
  403. // Scan(ctx, &v)
  404. func (_q *UserSubscriptionQuery) Select(fields ...string) *UserSubscriptionSelect {
  405. _q.ctx.Fields = append(_q.ctx.Fields, fields...)
  406. sbuild := &UserSubscriptionSelect{UserSubscriptionQuery: _q}
  407. sbuild.label = usersubscription.Label
  408. sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan
  409. return sbuild
  410. }
  411. // Aggregate returns a UserSubscriptionSelect configured with the given aggregations.
  412. func (_q *UserSubscriptionQuery) Aggregate(fns ...AggregateFunc) *UserSubscriptionSelect {
  413. return _q.Select().Aggregate(fns...)
  414. }
  415. func (_q *UserSubscriptionQuery) prepareQuery(ctx context.Context) error {
  416. for _, inter := range _q.inters {
  417. if inter == nil {
  418. return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
  419. }
  420. if trv, ok := inter.(Traverser); ok {
  421. if err := trv.Traverse(ctx, _q); err != nil {
  422. return err
  423. }
  424. }
  425. }
  426. for _, f := range _q.ctx.Fields {
  427. if !usersubscription.ValidColumn(f) {
  428. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  429. }
  430. }
  431. if _q.path != nil {
  432. prev, err := _q.path(ctx)
  433. if err != nil {
  434. return err
  435. }
  436. _q.sql = prev
  437. }
  438. return nil
  439. }
  440. func (_q *UserSubscriptionQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*UserSubscription, error) {
  441. var (
  442. nodes = []*UserSubscription{}
  443. _spec = _q.querySpec()
  444. loadedTypes = [4]bool{
  445. _q.withUser != nil,
  446. _q.withGroup != nil,
  447. _q.withAssignedByUser != nil,
  448. _q.withUsageLogs != nil,
  449. }
  450. )
  451. _spec.ScanValues = func(columns []string) ([]any, error) {
  452. return (*UserSubscription).scanValues(nil, columns)
  453. }
  454. _spec.Assign = func(columns []string, values []any) error {
  455. node := &UserSubscription{config: _q.config}
  456. nodes = append(nodes, node)
  457. node.Edges.loadedTypes = loadedTypes
  458. return node.assignValues(columns, values)
  459. }
  460. if len(_q.modifiers) > 0 {
  461. _spec.Modifiers = _q.modifiers
  462. }
  463. for i := range hooks {
  464. hooks[i](ctx, _spec)
  465. }
  466. if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil {
  467. return nil, err
  468. }
  469. if len(nodes) == 0 {
  470. return nodes, nil
  471. }
  472. if query := _q.withUser; query != nil {
  473. if err := _q.loadUser(ctx, query, nodes, nil,
  474. func(n *UserSubscription, e *User) { n.Edges.User = e }); err != nil {
  475. return nil, err
  476. }
  477. }
  478. if query := _q.withGroup; query != nil {
  479. if err := _q.loadGroup(ctx, query, nodes, nil,
  480. func(n *UserSubscription, e *Group) { n.Edges.Group = e }); err != nil {
  481. return nil, err
  482. }
  483. }
  484. if query := _q.withAssignedByUser; query != nil {
  485. if err := _q.loadAssignedByUser(ctx, query, nodes, nil,
  486. func(n *UserSubscription, e *User) { n.Edges.AssignedByUser = e }); err != nil {
  487. return nil, err
  488. }
  489. }
  490. if query := _q.withUsageLogs; query != nil {
  491. if err := _q.loadUsageLogs(ctx, query, nodes,
  492. func(n *UserSubscription) { n.Edges.UsageLogs = []*UsageLog{} },
  493. func(n *UserSubscription, e *UsageLog) { n.Edges.UsageLogs = append(n.Edges.UsageLogs, e) }); err != nil {
  494. return nil, err
  495. }
  496. }
  497. return nodes, nil
  498. }
  499. func (_q *UserSubscriptionQuery) loadUser(ctx context.Context, query *UserQuery, nodes []*UserSubscription, init func(*UserSubscription), assign func(*UserSubscription, *User)) error {
  500. ids := make([]int64, 0, len(nodes))
  501. nodeids := make(map[int64][]*UserSubscription)
  502. for i := range nodes {
  503. fk := nodes[i].UserID
  504. if _, ok := nodeids[fk]; !ok {
  505. ids = append(ids, fk)
  506. }
  507. nodeids[fk] = append(nodeids[fk], nodes[i])
  508. }
  509. if len(ids) == 0 {
  510. return nil
  511. }
  512. query.Where(user.IDIn(ids...))
  513. neighbors, err := query.All(ctx)
  514. if err != nil {
  515. return err
  516. }
  517. for _, n := range neighbors {
  518. nodes, ok := nodeids[n.ID]
  519. if !ok {
  520. return fmt.Errorf(`unexpected foreign-key "user_id" returned %v`, n.ID)
  521. }
  522. for i := range nodes {
  523. assign(nodes[i], n)
  524. }
  525. }
  526. return nil
  527. }
  528. func (_q *UserSubscriptionQuery) loadGroup(ctx context.Context, query *GroupQuery, nodes []*UserSubscription, init func(*UserSubscription), assign func(*UserSubscription, *Group)) error {
  529. ids := make([]int64, 0, len(nodes))
  530. nodeids := make(map[int64][]*UserSubscription)
  531. for i := range nodes {
  532. fk := nodes[i].GroupID
  533. if _, ok := nodeids[fk]; !ok {
  534. ids = append(ids, fk)
  535. }
  536. nodeids[fk] = append(nodeids[fk], nodes[i])
  537. }
  538. if len(ids) == 0 {
  539. return nil
  540. }
  541. query.Where(group.IDIn(ids...))
  542. neighbors, err := query.All(ctx)
  543. if err != nil {
  544. return err
  545. }
  546. for _, n := range neighbors {
  547. nodes, ok := nodeids[n.ID]
  548. if !ok {
  549. return fmt.Errorf(`unexpected foreign-key "group_id" returned %v`, n.ID)
  550. }
  551. for i := range nodes {
  552. assign(nodes[i], n)
  553. }
  554. }
  555. return nil
  556. }
  557. func (_q *UserSubscriptionQuery) loadAssignedByUser(ctx context.Context, query *UserQuery, nodes []*UserSubscription, init func(*UserSubscription), assign func(*UserSubscription, *User)) error {
  558. ids := make([]int64, 0, len(nodes))
  559. nodeids := make(map[int64][]*UserSubscription)
  560. for i := range nodes {
  561. if nodes[i].AssignedBy == nil {
  562. continue
  563. }
  564. fk := *nodes[i].AssignedBy
  565. if _, ok := nodeids[fk]; !ok {
  566. ids = append(ids, fk)
  567. }
  568. nodeids[fk] = append(nodeids[fk], nodes[i])
  569. }
  570. if len(ids) == 0 {
  571. return nil
  572. }
  573. query.Where(user.IDIn(ids...))
  574. neighbors, err := query.All(ctx)
  575. if err != nil {
  576. return err
  577. }
  578. for _, n := range neighbors {
  579. nodes, ok := nodeids[n.ID]
  580. if !ok {
  581. return fmt.Errorf(`unexpected foreign-key "assigned_by" returned %v`, n.ID)
  582. }
  583. for i := range nodes {
  584. assign(nodes[i], n)
  585. }
  586. }
  587. return nil
  588. }
  589. func (_q *UserSubscriptionQuery) loadUsageLogs(ctx context.Context, query *UsageLogQuery, nodes []*UserSubscription, init func(*UserSubscription), assign func(*UserSubscription, *UsageLog)) error {
  590. fks := make([]driver.Value, 0, len(nodes))
  591. nodeids := make(map[int64]*UserSubscription)
  592. for i := range nodes {
  593. fks = append(fks, nodes[i].ID)
  594. nodeids[nodes[i].ID] = nodes[i]
  595. if init != nil {
  596. init(nodes[i])
  597. }
  598. }
  599. if len(query.ctx.Fields) > 0 {
  600. query.ctx.AppendFieldOnce(usagelog.FieldSubscriptionID)
  601. }
  602. query.Where(predicate.UsageLog(func(s *sql.Selector) {
  603. s.Where(sql.InValues(s.C(usersubscription.UsageLogsColumn), fks...))
  604. }))
  605. neighbors, err := query.All(ctx)
  606. if err != nil {
  607. return err
  608. }
  609. for _, n := range neighbors {
  610. fk := n.SubscriptionID
  611. if fk == nil {
  612. return fmt.Errorf(`foreign-key "subscription_id" is nil for node %v`, n.ID)
  613. }
  614. node, ok := nodeids[*fk]
  615. if !ok {
  616. return fmt.Errorf(`unexpected referenced foreign-key "subscription_id" returned %v for node %v`, *fk, n.ID)
  617. }
  618. assign(node, n)
  619. }
  620. return nil
  621. }
  622. func (_q *UserSubscriptionQuery) sqlCount(ctx context.Context) (int, error) {
  623. _spec := _q.querySpec()
  624. if len(_q.modifiers) > 0 {
  625. _spec.Modifiers = _q.modifiers
  626. }
  627. _spec.Node.Columns = _q.ctx.Fields
  628. if len(_q.ctx.Fields) > 0 {
  629. _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique
  630. }
  631. return sqlgraph.CountNodes(ctx, _q.driver, _spec)
  632. }
  633. func (_q *UserSubscriptionQuery) querySpec() *sqlgraph.QuerySpec {
  634. _spec := sqlgraph.NewQuerySpec(usersubscription.Table, usersubscription.Columns, sqlgraph.NewFieldSpec(usersubscription.FieldID, field.TypeInt64))
  635. _spec.From = _q.sql
  636. if unique := _q.ctx.Unique; unique != nil {
  637. _spec.Unique = *unique
  638. } else if _q.path != nil {
  639. _spec.Unique = true
  640. }
  641. if fields := _q.ctx.Fields; len(fields) > 0 {
  642. _spec.Node.Columns = make([]string, 0, len(fields))
  643. _spec.Node.Columns = append(_spec.Node.Columns, usersubscription.FieldID)
  644. for i := range fields {
  645. if fields[i] != usersubscription.FieldID {
  646. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  647. }
  648. }
  649. if _q.withUser != nil {
  650. _spec.Node.AddColumnOnce(usersubscription.FieldUserID)
  651. }
  652. if _q.withGroup != nil {
  653. _spec.Node.AddColumnOnce(usersubscription.FieldGroupID)
  654. }
  655. if _q.withAssignedByUser != nil {
  656. _spec.Node.AddColumnOnce(usersubscription.FieldAssignedBy)
  657. }
  658. }
  659. if ps := _q.predicates; len(ps) > 0 {
  660. _spec.Predicate = func(selector *sql.Selector) {
  661. for i := range ps {
  662. ps[i](selector)
  663. }
  664. }
  665. }
  666. if limit := _q.ctx.Limit; limit != nil {
  667. _spec.Limit = *limit
  668. }
  669. if offset := _q.ctx.Offset; offset != nil {
  670. _spec.Offset = *offset
  671. }
  672. if ps := _q.order; len(ps) > 0 {
  673. _spec.Order = func(selector *sql.Selector) {
  674. for i := range ps {
  675. ps[i](selector)
  676. }
  677. }
  678. }
  679. return _spec
  680. }
  681. func (_q *UserSubscriptionQuery) sqlQuery(ctx context.Context) *sql.Selector {
  682. builder := sql.Dialect(_q.driver.Dialect())
  683. t1 := builder.Table(usersubscription.Table)
  684. columns := _q.ctx.Fields
  685. if len(columns) == 0 {
  686. columns = usersubscription.Columns
  687. }
  688. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  689. if _q.sql != nil {
  690. selector = _q.sql
  691. selector.Select(selector.Columns(columns...)...)
  692. }
  693. if _q.ctx.Unique != nil && *_q.ctx.Unique {
  694. selector.Distinct()
  695. }
  696. for _, m := range _q.modifiers {
  697. m(selector)
  698. }
  699. for _, p := range _q.predicates {
  700. p(selector)
  701. }
  702. for _, p := range _q.order {
  703. p(selector)
  704. }
  705. if offset := _q.ctx.Offset; offset != nil {
  706. // limit is mandatory for offset clause. We start
  707. // with default value, and override it below if needed.
  708. selector.Offset(*offset).Limit(math.MaxInt32)
  709. }
  710. if limit := _q.ctx.Limit; limit != nil {
  711. selector.Limit(*limit)
  712. }
  713. return selector
  714. }
  715. // ForUpdate locks the selected rows against concurrent updates, and prevent them from being
  716. // updated, deleted or "selected ... for update" by other sessions, until the transaction is
  717. // either committed or rolled-back.
  718. func (_q *UserSubscriptionQuery) ForUpdate(opts ...sql.LockOption) *UserSubscriptionQuery {
  719. if _q.driver.Dialect() == dialect.Postgres {
  720. _q.Unique(false)
  721. }
  722. _q.modifiers = append(_q.modifiers, func(s *sql.Selector) {
  723. s.ForUpdate(opts...)
  724. })
  725. return _q
  726. }
  727. // ForShare behaves similarly to ForUpdate, except that it acquires a shared mode lock
  728. // on any rows that are read. Other sessions can read the rows, but cannot modify them
  729. // until your transaction commits.
  730. func (_q *UserSubscriptionQuery) ForShare(opts ...sql.LockOption) *UserSubscriptionQuery {
  731. if _q.driver.Dialect() == dialect.Postgres {
  732. _q.Unique(false)
  733. }
  734. _q.modifiers = append(_q.modifiers, func(s *sql.Selector) {
  735. s.ForShare(opts...)
  736. })
  737. return _q
  738. }
  739. // UserSubscriptionGroupBy is the group-by builder for UserSubscription entities.
  740. type UserSubscriptionGroupBy struct {
  741. selector
  742. build *UserSubscriptionQuery
  743. }
  744. // Aggregate adds the given aggregation functions to the group-by query.
  745. func (_g *UserSubscriptionGroupBy) Aggregate(fns ...AggregateFunc) *UserSubscriptionGroupBy {
  746. _g.fns = append(_g.fns, fns...)
  747. return _g
  748. }
  749. // Scan applies the selector query and scans the result into the given value.
  750. func (_g *UserSubscriptionGroupBy) Scan(ctx context.Context, v any) error {
  751. ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy)
  752. if err := _g.build.prepareQuery(ctx); err != nil {
  753. return err
  754. }
  755. return scanWithInterceptors[*UserSubscriptionQuery, *UserSubscriptionGroupBy](ctx, _g.build, _g, _g.build.inters, v)
  756. }
  757. func (_g *UserSubscriptionGroupBy) sqlScan(ctx context.Context, root *UserSubscriptionQuery, v any) error {
  758. selector := root.sqlQuery(ctx).Select()
  759. aggregation := make([]string, 0, len(_g.fns))
  760. for _, fn := range _g.fns {
  761. aggregation = append(aggregation, fn(selector))
  762. }
  763. if len(selector.SelectedColumns()) == 0 {
  764. columns := make([]string, 0, len(*_g.flds)+len(_g.fns))
  765. for _, f := range *_g.flds {
  766. columns = append(columns, selector.C(f))
  767. }
  768. columns = append(columns, aggregation...)
  769. selector.Select(columns...)
  770. }
  771. selector.GroupBy(selector.Columns(*_g.flds...)...)
  772. if err := selector.Err(); err != nil {
  773. return err
  774. }
  775. rows := &sql.Rows{}
  776. query, args := selector.Query()
  777. if err := _g.build.driver.Query(ctx, query, args, rows); err != nil {
  778. return err
  779. }
  780. defer rows.Close()
  781. return sql.ScanSlice(rows, v)
  782. }
  783. // UserSubscriptionSelect is the builder for selecting fields of UserSubscription entities.
  784. type UserSubscriptionSelect struct {
  785. *UserSubscriptionQuery
  786. selector
  787. }
  788. // Aggregate adds the given aggregation functions to the selector query.
  789. func (_s *UserSubscriptionSelect) Aggregate(fns ...AggregateFunc) *UserSubscriptionSelect {
  790. _s.fns = append(_s.fns, fns...)
  791. return _s
  792. }
  793. // Scan applies the selector query and scans the result into the given value.
  794. func (_s *UserSubscriptionSelect) Scan(ctx context.Context, v any) error {
  795. ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect)
  796. if err := _s.prepareQuery(ctx); err != nil {
  797. return err
  798. }
  799. return scanWithInterceptors[*UserSubscriptionQuery, *UserSubscriptionSelect](ctx, _s.UserSubscriptionQuery, _s, _s.inters, v)
  800. }
  801. func (_s *UserSubscriptionSelect) sqlScan(ctx context.Context, root *UserSubscriptionQuery, v any) error {
  802. selector := root.sqlQuery(ctx)
  803. aggregation := make([]string, 0, len(_s.fns))
  804. for _, fn := range _s.fns {
  805. aggregation = append(aggregation, fn(selector))
  806. }
  807. switch n := len(*_s.selector.flds); {
  808. case n == 0 && len(aggregation) > 0:
  809. selector.Select(aggregation...)
  810. case n != 0 && len(aggregation) > 0:
  811. selector.AppendSelect(aggregation...)
  812. }
  813. rows := &sql.Rows{}
  814. query, args := selector.Query()
  815. if err := _s.driver.Query(ctx, query, args, rows); err != nil {
  816. return err
  817. }
  818. defer rows.Close()
  819. return sql.ScanSlice(rows, v)
  820. }