user_query.go 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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/announcementread"
  14. "github.com/Wei-Shaw/sub2api/ent/apikey"
  15. "github.com/Wei-Shaw/sub2api/ent/authidentity"
  16. "github.com/Wei-Shaw/sub2api/ent/group"
  17. "github.com/Wei-Shaw/sub2api/ent/paymentorder"
  18. "github.com/Wei-Shaw/sub2api/ent/pendingauthsession"
  19. "github.com/Wei-Shaw/sub2api/ent/predicate"
  20. "github.com/Wei-Shaw/sub2api/ent/promocodeusage"
  21. "github.com/Wei-Shaw/sub2api/ent/redeemcode"
  22. "github.com/Wei-Shaw/sub2api/ent/usagelog"
  23. "github.com/Wei-Shaw/sub2api/ent/user"
  24. "github.com/Wei-Shaw/sub2api/ent/userallowedgroup"
  25. "github.com/Wei-Shaw/sub2api/ent/userattributevalue"
  26. "github.com/Wei-Shaw/sub2api/ent/userplatformquota"
  27. "github.com/Wei-Shaw/sub2api/ent/usersubscription"
  28. )
  29. // UserQuery is the builder for querying User entities.
  30. type UserQuery struct {
  31. config
  32. ctx *QueryContext
  33. order []user.OrderOption
  34. inters []Interceptor
  35. predicates []predicate.User
  36. withAPIKeys *APIKeyQuery
  37. withRedeemCodes *RedeemCodeQuery
  38. withSubscriptions *UserSubscriptionQuery
  39. withAssignedSubscriptions *UserSubscriptionQuery
  40. withAnnouncementReads *AnnouncementReadQuery
  41. withAllowedGroups *GroupQuery
  42. withUsageLogs *UsageLogQuery
  43. withAttributeValues *UserAttributeValueQuery
  44. withPromoCodeUsages *PromoCodeUsageQuery
  45. withPaymentOrders *PaymentOrderQuery
  46. withAuthIdentities *AuthIdentityQuery
  47. withPendingAuthSessions *PendingAuthSessionQuery
  48. withPlatformQuotas *UserPlatformQuotaQuery
  49. withUserAllowedGroups *UserAllowedGroupQuery
  50. modifiers []func(*sql.Selector)
  51. // intermediate query (i.e. traversal path).
  52. sql *sql.Selector
  53. path func(context.Context) (*sql.Selector, error)
  54. }
  55. // Where adds a new predicate for the UserQuery builder.
  56. func (_q *UserQuery) Where(ps ...predicate.User) *UserQuery {
  57. _q.predicates = append(_q.predicates, ps...)
  58. return _q
  59. }
  60. // Limit the number of records to be returned by this query.
  61. func (_q *UserQuery) Limit(limit int) *UserQuery {
  62. _q.ctx.Limit = &limit
  63. return _q
  64. }
  65. // Offset to start from.
  66. func (_q *UserQuery) Offset(offset int) *UserQuery {
  67. _q.ctx.Offset = &offset
  68. return _q
  69. }
  70. // Unique configures the query builder to filter duplicate records on query.
  71. // By default, unique is set to true, and can be disabled using this method.
  72. func (_q *UserQuery) Unique(unique bool) *UserQuery {
  73. _q.ctx.Unique = &unique
  74. return _q
  75. }
  76. // Order specifies how the records should be ordered.
  77. func (_q *UserQuery) Order(o ...user.OrderOption) *UserQuery {
  78. _q.order = append(_q.order, o...)
  79. return _q
  80. }
  81. // QueryAPIKeys chains the current query on the "api_keys" edge.
  82. func (_q *UserQuery) QueryAPIKeys() *APIKeyQuery {
  83. query := (&APIKeyClient{config: _q.config}).Query()
  84. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  85. if err := _q.prepareQuery(ctx); err != nil {
  86. return nil, err
  87. }
  88. selector := _q.sqlQuery(ctx)
  89. if err := selector.Err(); err != nil {
  90. return nil, err
  91. }
  92. step := sqlgraph.NewStep(
  93. sqlgraph.From(user.Table, user.FieldID, selector),
  94. sqlgraph.To(apikey.Table, apikey.FieldID),
  95. sqlgraph.Edge(sqlgraph.O2M, false, user.APIKeysTable, user.APIKeysColumn),
  96. )
  97. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  98. return fromU, nil
  99. }
  100. return query
  101. }
  102. // QueryRedeemCodes chains the current query on the "redeem_codes" edge.
  103. func (_q *UserQuery) QueryRedeemCodes() *RedeemCodeQuery {
  104. query := (&RedeemCodeClient{config: _q.config}).Query()
  105. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  106. if err := _q.prepareQuery(ctx); err != nil {
  107. return nil, err
  108. }
  109. selector := _q.sqlQuery(ctx)
  110. if err := selector.Err(); err != nil {
  111. return nil, err
  112. }
  113. step := sqlgraph.NewStep(
  114. sqlgraph.From(user.Table, user.FieldID, selector),
  115. sqlgraph.To(redeemcode.Table, redeemcode.FieldID),
  116. sqlgraph.Edge(sqlgraph.O2M, false, user.RedeemCodesTable, user.RedeemCodesColumn),
  117. )
  118. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  119. return fromU, nil
  120. }
  121. return query
  122. }
  123. // QuerySubscriptions chains the current query on the "subscriptions" edge.
  124. func (_q *UserQuery) QuerySubscriptions() *UserSubscriptionQuery {
  125. query := (&UserSubscriptionClient{config: _q.config}).Query()
  126. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  127. if err := _q.prepareQuery(ctx); err != nil {
  128. return nil, err
  129. }
  130. selector := _q.sqlQuery(ctx)
  131. if err := selector.Err(); err != nil {
  132. return nil, err
  133. }
  134. step := sqlgraph.NewStep(
  135. sqlgraph.From(user.Table, user.FieldID, selector),
  136. sqlgraph.To(usersubscription.Table, usersubscription.FieldID),
  137. sqlgraph.Edge(sqlgraph.O2M, false, user.SubscriptionsTable, user.SubscriptionsColumn),
  138. )
  139. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  140. return fromU, nil
  141. }
  142. return query
  143. }
  144. // QueryAssignedSubscriptions chains the current query on the "assigned_subscriptions" edge.
  145. func (_q *UserQuery) QueryAssignedSubscriptions() *UserSubscriptionQuery {
  146. query := (&UserSubscriptionClient{config: _q.config}).Query()
  147. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  148. if err := _q.prepareQuery(ctx); err != nil {
  149. return nil, err
  150. }
  151. selector := _q.sqlQuery(ctx)
  152. if err := selector.Err(); err != nil {
  153. return nil, err
  154. }
  155. step := sqlgraph.NewStep(
  156. sqlgraph.From(user.Table, user.FieldID, selector),
  157. sqlgraph.To(usersubscription.Table, usersubscription.FieldID),
  158. sqlgraph.Edge(sqlgraph.O2M, false, user.AssignedSubscriptionsTable, user.AssignedSubscriptionsColumn),
  159. )
  160. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  161. return fromU, nil
  162. }
  163. return query
  164. }
  165. // QueryAnnouncementReads chains the current query on the "announcement_reads" edge.
  166. func (_q *UserQuery) QueryAnnouncementReads() *AnnouncementReadQuery {
  167. query := (&AnnouncementReadClient{config: _q.config}).Query()
  168. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  169. if err := _q.prepareQuery(ctx); err != nil {
  170. return nil, err
  171. }
  172. selector := _q.sqlQuery(ctx)
  173. if err := selector.Err(); err != nil {
  174. return nil, err
  175. }
  176. step := sqlgraph.NewStep(
  177. sqlgraph.From(user.Table, user.FieldID, selector),
  178. sqlgraph.To(announcementread.Table, announcementread.FieldID),
  179. sqlgraph.Edge(sqlgraph.O2M, false, user.AnnouncementReadsTable, user.AnnouncementReadsColumn),
  180. )
  181. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  182. return fromU, nil
  183. }
  184. return query
  185. }
  186. // QueryAllowedGroups chains the current query on the "allowed_groups" edge.
  187. func (_q *UserQuery) QueryAllowedGroups() *GroupQuery {
  188. query := (&GroupClient{config: _q.config}).Query()
  189. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  190. if err := _q.prepareQuery(ctx); err != nil {
  191. return nil, err
  192. }
  193. selector := _q.sqlQuery(ctx)
  194. if err := selector.Err(); err != nil {
  195. return nil, err
  196. }
  197. step := sqlgraph.NewStep(
  198. sqlgraph.From(user.Table, user.FieldID, selector),
  199. sqlgraph.To(group.Table, group.FieldID),
  200. sqlgraph.Edge(sqlgraph.M2M, false, user.AllowedGroupsTable, user.AllowedGroupsPrimaryKey...),
  201. )
  202. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  203. return fromU, nil
  204. }
  205. return query
  206. }
  207. // QueryUsageLogs chains the current query on the "usage_logs" edge.
  208. func (_q *UserQuery) QueryUsageLogs() *UsageLogQuery {
  209. query := (&UsageLogClient{config: _q.config}).Query()
  210. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  211. if err := _q.prepareQuery(ctx); err != nil {
  212. return nil, err
  213. }
  214. selector := _q.sqlQuery(ctx)
  215. if err := selector.Err(); err != nil {
  216. return nil, err
  217. }
  218. step := sqlgraph.NewStep(
  219. sqlgraph.From(user.Table, user.FieldID, selector),
  220. sqlgraph.To(usagelog.Table, usagelog.FieldID),
  221. sqlgraph.Edge(sqlgraph.O2M, false, user.UsageLogsTable, user.UsageLogsColumn),
  222. )
  223. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  224. return fromU, nil
  225. }
  226. return query
  227. }
  228. // QueryAttributeValues chains the current query on the "attribute_values" edge.
  229. func (_q *UserQuery) QueryAttributeValues() *UserAttributeValueQuery {
  230. query := (&UserAttributeValueClient{config: _q.config}).Query()
  231. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  232. if err := _q.prepareQuery(ctx); err != nil {
  233. return nil, err
  234. }
  235. selector := _q.sqlQuery(ctx)
  236. if err := selector.Err(); err != nil {
  237. return nil, err
  238. }
  239. step := sqlgraph.NewStep(
  240. sqlgraph.From(user.Table, user.FieldID, selector),
  241. sqlgraph.To(userattributevalue.Table, userattributevalue.FieldID),
  242. sqlgraph.Edge(sqlgraph.O2M, false, user.AttributeValuesTable, user.AttributeValuesColumn),
  243. )
  244. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  245. return fromU, nil
  246. }
  247. return query
  248. }
  249. // QueryPromoCodeUsages chains the current query on the "promo_code_usages" edge.
  250. func (_q *UserQuery) QueryPromoCodeUsages() *PromoCodeUsageQuery {
  251. query := (&PromoCodeUsageClient{config: _q.config}).Query()
  252. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  253. if err := _q.prepareQuery(ctx); err != nil {
  254. return nil, err
  255. }
  256. selector := _q.sqlQuery(ctx)
  257. if err := selector.Err(); err != nil {
  258. return nil, err
  259. }
  260. step := sqlgraph.NewStep(
  261. sqlgraph.From(user.Table, user.FieldID, selector),
  262. sqlgraph.To(promocodeusage.Table, promocodeusage.FieldID),
  263. sqlgraph.Edge(sqlgraph.O2M, false, user.PromoCodeUsagesTable, user.PromoCodeUsagesColumn),
  264. )
  265. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  266. return fromU, nil
  267. }
  268. return query
  269. }
  270. // QueryPaymentOrders chains the current query on the "payment_orders" edge.
  271. func (_q *UserQuery) QueryPaymentOrders() *PaymentOrderQuery {
  272. query := (&PaymentOrderClient{config: _q.config}).Query()
  273. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  274. if err := _q.prepareQuery(ctx); err != nil {
  275. return nil, err
  276. }
  277. selector := _q.sqlQuery(ctx)
  278. if err := selector.Err(); err != nil {
  279. return nil, err
  280. }
  281. step := sqlgraph.NewStep(
  282. sqlgraph.From(user.Table, user.FieldID, selector),
  283. sqlgraph.To(paymentorder.Table, paymentorder.FieldID),
  284. sqlgraph.Edge(sqlgraph.O2M, false, user.PaymentOrdersTable, user.PaymentOrdersColumn),
  285. )
  286. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  287. return fromU, nil
  288. }
  289. return query
  290. }
  291. // QueryAuthIdentities chains the current query on the "auth_identities" edge.
  292. func (_q *UserQuery) QueryAuthIdentities() *AuthIdentityQuery {
  293. query := (&AuthIdentityClient{config: _q.config}).Query()
  294. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  295. if err := _q.prepareQuery(ctx); err != nil {
  296. return nil, err
  297. }
  298. selector := _q.sqlQuery(ctx)
  299. if err := selector.Err(); err != nil {
  300. return nil, err
  301. }
  302. step := sqlgraph.NewStep(
  303. sqlgraph.From(user.Table, user.FieldID, selector),
  304. sqlgraph.To(authidentity.Table, authidentity.FieldID),
  305. sqlgraph.Edge(sqlgraph.O2M, false, user.AuthIdentitiesTable, user.AuthIdentitiesColumn),
  306. )
  307. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  308. return fromU, nil
  309. }
  310. return query
  311. }
  312. // QueryPendingAuthSessions chains the current query on the "pending_auth_sessions" edge.
  313. func (_q *UserQuery) QueryPendingAuthSessions() *PendingAuthSessionQuery {
  314. query := (&PendingAuthSessionClient{config: _q.config}).Query()
  315. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  316. if err := _q.prepareQuery(ctx); err != nil {
  317. return nil, err
  318. }
  319. selector := _q.sqlQuery(ctx)
  320. if err := selector.Err(); err != nil {
  321. return nil, err
  322. }
  323. step := sqlgraph.NewStep(
  324. sqlgraph.From(user.Table, user.FieldID, selector),
  325. sqlgraph.To(pendingauthsession.Table, pendingauthsession.FieldID),
  326. sqlgraph.Edge(sqlgraph.O2M, false, user.PendingAuthSessionsTable, user.PendingAuthSessionsColumn),
  327. )
  328. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  329. return fromU, nil
  330. }
  331. return query
  332. }
  333. // QueryPlatformQuotas chains the current query on the "platform_quotas" edge.
  334. func (_q *UserQuery) QueryPlatformQuotas() *UserPlatformQuotaQuery {
  335. query := (&UserPlatformQuotaClient{config: _q.config}).Query()
  336. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  337. if err := _q.prepareQuery(ctx); err != nil {
  338. return nil, err
  339. }
  340. selector := _q.sqlQuery(ctx)
  341. if err := selector.Err(); err != nil {
  342. return nil, err
  343. }
  344. step := sqlgraph.NewStep(
  345. sqlgraph.From(user.Table, user.FieldID, selector),
  346. sqlgraph.To(userplatformquota.Table, userplatformquota.FieldID),
  347. sqlgraph.Edge(sqlgraph.O2M, false, user.PlatformQuotasTable, user.PlatformQuotasColumn),
  348. )
  349. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  350. return fromU, nil
  351. }
  352. return query
  353. }
  354. // QueryUserAllowedGroups chains the current query on the "user_allowed_groups" edge.
  355. func (_q *UserQuery) QueryUserAllowedGroups() *UserAllowedGroupQuery {
  356. query := (&UserAllowedGroupClient{config: _q.config}).Query()
  357. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  358. if err := _q.prepareQuery(ctx); err != nil {
  359. return nil, err
  360. }
  361. selector := _q.sqlQuery(ctx)
  362. if err := selector.Err(); err != nil {
  363. return nil, err
  364. }
  365. step := sqlgraph.NewStep(
  366. sqlgraph.From(user.Table, user.FieldID, selector),
  367. sqlgraph.To(userallowedgroup.Table, userallowedgroup.UserColumn),
  368. sqlgraph.Edge(sqlgraph.O2M, true, user.UserAllowedGroupsTable, user.UserAllowedGroupsColumn),
  369. )
  370. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  371. return fromU, nil
  372. }
  373. return query
  374. }
  375. // First returns the first User entity from the query.
  376. // Returns a *NotFoundError when no User was found.
  377. func (_q *UserQuery) First(ctx context.Context) (*User, error) {
  378. nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst))
  379. if err != nil {
  380. return nil, err
  381. }
  382. if len(nodes) == 0 {
  383. return nil, &NotFoundError{user.Label}
  384. }
  385. return nodes[0], nil
  386. }
  387. // FirstX is like First, but panics if an error occurs.
  388. func (_q *UserQuery) FirstX(ctx context.Context) *User {
  389. node, err := _q.First(ctx)
  390. if err != nil && !IsNotFound(err) {
  391. panic(err)
  392. }
  393. return node
  394. }
  395. // FirstID returns the first User ID from the query.
  396. // Returns a *NotFoundError when no User ID was found.
  397. func (_q *UserQuery) FirstID(ctx context.Context) (id int64, err error) {
  398. var ids []int64
  399. if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil {
  400. return
  401. }
  402. if len(ids) == 0 {
  403. err = &NotFoundError{user.Label}
  404. return
  405. }
  406. return ids[0], nil
  407. }
  408. // FirstIDX is like FirstID, but panics if an error occurs.
  409. func (_q *UserQuery) FirstIDX(ctx context.Context) int64 {
  410. id, err := _q.FirstID(ctx)
  411. if err != nil && !IsNotFound(err) {
  412. panic(err)
  413. }
  414. return id
  415. }
  416. // Only returns a single User entity found by the query, ensuring it only returns one.
  417. // Returns a *NotSingularError when more than one User entity is found.
  418. // Returns a *NotFoundError when no User entities are found.
  419. func (_q *UserQuery) Only(ctx context.Context) (*User, error) {
  420. nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
  421. if err != nil {
  422. return nil, err
  423. }
  424. switch len(nodes) {
  425. case 1:
  426. return nodes[0], nil
  427. case 0:
  428. return nil, &NotFoundError{user.Label}
  429. default:
  430. return nil, &NotSingularError{user.Label}
  431. }
  432. }
  433. // OnlyX is like Only, but panics if an error occurs.
  434. func (_q *UserQuery) OnlyX(ctx context.Context) *User {
  435. node, err := _q.Only(ctx)
  436. if err != nil {
  437. panic(err)
  438. }
  439. return node
  440. }
  441. // OnlyID is like Only, but returns the only User ID in the query.
  442. // Returns a *NotSingularError when more than one User ID is found.
  443. // Returns a *NotFoundError when no entities are found.
  444. func (_q *UserQuery) OnlyID(ctx context.Context) (id int64, err error) {
  445. var ids []int64
  446. if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil {
  447. return
  448. }
  449. switch len(ids) {
  450. case 1:
  451. id = ids[0]
  452. case 0:
  453. err = &NotFoundError{user.Label}
  454. default:
  455. err = &NotSingularError{user.Label}
  456. }
  457. return
  458. }
  459. // OnlyIDX is like OnlyID, but panics if an error occurs.
  460. func (_q *UserQuery) OnlyIDX(ctx context.Context) int64 {
  461. id, err := _q.OnlyID(ctx)
  462. if err != nil {
  463. panic(err)
  464. }
  465. return id
  466. }
  467. // All executes the query and returns a list of Users.
  468. func (_q *UserQuery) All(ctx context.Context) ([]*User, error) {
  469. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll)
  470. if err := _q.prepareQuery(ctx); err != nil {
  471. return nil, err
  472. }
  473. qr := querierAll[[]*User, *UserQuery]()
  474. return withInterceptors[[]*User](ctx, _q, qr, _q.inters)
  475. }
  476. // AllX is like All, but panics if an error occurs.
  477. func (_q *UserQuery) AllX(ctx context.Context) []*User {
  478. nodes, err := _q.All(ctx)
  479. if err != nil {
  480. panic(err)
  481. }
  482. return nodes
  483. }
  484. // IDs executes the query and returns a list of User IDs.
  485. func (_q *UserQuery) IDs(ctx context.Context) (ids []int64, err error) {
  486. if _q.ctx.Unique == nil && _q.path != nil {
  487. _q.Unique(true)
  488. }
  489. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs)
  490. if err = _q.Select(user.FieldID).Scan(ctx, &ids); err != nil {
  491. return nil, err
  492. }
  493. return ids, nil
  494. }
  495. // IDsX is like IDs, but panics if an error occurs.
  496. func (_q *UserQuery) IDsX(ctx context.Context) []int64 {
  497. ids, err := _q.IDs(ctx)
  498. if err != nil {
  499. panic(err)
  500. }
  501. return ids
  502. }
  503. // Count returns the count of the given query.
  504. func (_q *UserQuery) Count(ctx context.Context) (int, error) {
  505. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount)
  506. if err := _q.prepareQuery(ctx); err != nil {
  507. return 0, err
  508. }
  509. return withInterceptors[int](ctx, _q, querierCount[*UserQuery](), _q.inters)
  510. }
  511. // CountX is like Count, but panics if an error occurs.
  512. func (_q *UserQuery) CountX(ctx context.Context) int {
  513. count, err := _q.Count(ctx)
  514. if err != nil {
  515. panic(err)
  516. }
  517. return count
  518. }
  519. // Exist returns true if the query has elements in the graph.
  520. func (_q *UserQuery) Exist(ctx context.Context) (bool, error) {
  521. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist)
  522. switch _, err := _q.FirstID(ctx); {
  523. case IsNotFound(err):
  524. return false, nil
  525. case err != nil:
  526. return false, fmt.Errorf("ent: check existence: %w", err)
  527. default:
  528. return true, nil
  529. }
  530. }
  531. // ExistX is like Exist, but panics if an error occurs.
  532. func (_q *UserQuery) ExistX(ctx context.Context) bool {
  533. exist, err := _q.Exist(ctx)
  534. if err != nil {
  535. panic(err)
  536. }
  537. return exist
  538. }
  539. // Clone returns a duplicate of the UserQuery builder, including all associated steps. It can be
  540. // used to prepare common query builders and use them differently after the clone is made.
  541. func (_q *UserQuery) Clone() *UserQuery {
  542. if _q == nil {
  543. return nil
  544. }
  545. return &UserQuery{
  546. config: _q.config,
  547. ctx: _q.ctx.Clone(),
  548. order: append([]user.OrderOption{}, _q.order...),
  549. inters: append([]Interceptor{}, _q.inters...),
  550. predicates: append([]predicate.User{}, _q.predicates...),
  551. withAPIKeys: _q.withAPIKeys.Clone(),
  552. withRedeemCodes: _q.withRedeemCodes.Clone(),
  553. withSubscriptions: _q.withSubscriptions.Clone(),
  554. withAssignedSubscriptions: _q.withAssignedSubscriptions.Clone(),
  555. withAnnouncementReads: _q.withAnnouncementReads.Clone(),
  556. withAllowedGroups: _q.withAllowedGroups.Clone(),
  557. withUsageLogs: _q.withUsageLogs.Clone(),
  558. withAttributeValues: _q.withAttributeValues.Clone(),
  559. withPromoCodeUsages: _q.withPromoCodeUsages.Clone(),
  560. withPaymentOrders: _q.withPaymentOrders.Clone(),
  561. withAuthIdentities: _q.withAuthIdentities.Clone(),
  562. withPendingAuthSessions: _q.withPendingAuthSessions.Clone(),
  563. withPlatformQuotas: _q.withPlatformQuotas.Clone(),
  564. withUserAllowedGroups: _q.withUserAllowedGroups.Clone(),
  565. // clone intermediate query.
  566. sql: _q.sql.Clone(),
  567. path: _q.path,
  568. }
  569. }
  570. // WithAPIKeys tells the query-builder to eager-load the nodes that are connected to
  571. // the "api_keys" edge. The optional arguments are used to configure the query builder of the edge.
  572. func (_q *UserQuery) WithAPIKeys(opts ...func(*APIKeyQuery)) *UserQuery {
  573. query := (&APIKeyClient{config: _q.config}).Query()
  574. for _, opt := range opts {
  575. opt(query)
  576. }
  577. _q.withAPIKeys = query
  578. return _q
  579. }
  580. // WithRedeemCodes tells the query-builder to eager-load the nodes that are connected to
  581. // the "redeem_codes" edge. The optional arguments are used to configure the query builder of the edge.
  582. func (_q *UserQuery) WithRedeemCodes(opts ...func(*RedeemCodeQuery)) *UserQuery {
  583. query := (&RedeemCodeClient{config: _q.config}).Query()
  584. for _, opt := range opts {
  585. opt(query)
  586. }
  587. _q.withRedeemCodes = query
  588. return _q
  589. }
  590. // WithSubscriptions tells the query-builder to eager-load the nodes that are connected to
  591. // the "subscriptions" edge. The optional arguments are used to configure the query builder of the edge.
  592. func (_q *UserQuery) WithSubscriptions(opts ...func(*UserSubscriptionQuery)) *UserQuery {
  593. query := (&UserSubscriptionClient{config: _q.config}).Query()
  594. for _, opt := range opts {
  595. opt(query)
  596. }
  597. _q.withSubscriptions = query
  598. return _q
  599. }
  600. // WithAssignedSubscriptions tells the query-builder to eager-load the nodes that are connected to
  601. // the "assigned_subscriptions" edge. The optional arguments are used to configure the query builder of the edge.
  602. func (_q *UserQuery) WithAssignedSubscriptions(opts ...func(*UserSubscriptionQuery)) *UserQuery {
  603. query := (&UserSubscriptionClient{config: _q.config}).Query()
  604. for _, opt := range opts {
  605. opt(query)
  606. }
  607. _q.withAssignedSubscriptions = query
  608. return _q
  609. }
  610. // WithAnnouncementReads tells the query-builder to eager-load the nodes that are connected to
  611. // the "announcement_reads" edge. The optional arguments are used to configure the query builder of the edge.
  612. func (_q *UserQuery) WithAnnouncementReads(opts ...func(*AnnouncementReadQuery)) *UserQuery {
  613. query := (&AnnouncementReadClient{config: _q.config}).Query()
  614. for _, opt := range opts {
  615. opt(query)
  616. }
  617. _q.withAnnouncementReads = query
  618. return _q
  619. }
  620. // WithAllowedGroups tells the query-builder to eager-load the nodes that are connected to
  621. // the "allowed_groups" edge. The optional arguments are used to configure the query builder of the edge.
  622. func (_q *UserQuery) WithAllowedGroups(opts ...func(*GroupQuery)) *UserQuery {
  623. query := (&GroupClient{config: _q.config}).Query()
  624. for _, opt := range opts {
  625. opt(query)
  626. }
  627. _q.withAllowedGroups = query
  628. return _q
  629. }
  630. // WithUsageLogs tells the query-builder to eager-load the nodes that are connected to
  631. // the "usage_logs" edge. The optional arguments are used to configure the query builder of the edge.
  632. func (_q *UserQuery) WithUsageLogs(opts ...func(*UsageLogQuery)) *UserQuery {
  633. query := (&UsageLogClient{config: _q.config}).Query()
  634. for _, opt := range opts {
  635. opt(query)
  636. }
  637. _q.withUsageLogs = query
  638. return _q
  639. }
  640. // WithAttributeValues tells the query-builder to eager-load the nodes that are connected to
  641. // the "attribute_values" edge. The optional arguments are used to configure the query builder of the edge.
  642. func (_q *UserQuery) WithAttributeValues(opts ...func(*UserAttributeValueQuery)) *UserQuery {
  643. query := (&UserAttributeValueClient{config: _q.config}).Query()
  644. for _, opt := range opts {
  645. opt(query)
  646. }
  647. _q.withAttributeValues = query
  648. return _q
  649. }
  650. // WithPromoCodeUsages tells the query-builder to eager-load the nodes that are connected to
  651. // the "promo_code_usages" edge. The optional arguments are used to configure the query builder of the edge.
  652. func (_q *UserQuery) WithPromoCodeUsages(opts ...func(*PromoCodeUsageQuery)) *UserQuery {
  653. query := (&PromoCodeUsageClient{config: _q.config}).Query()
  654. for _, opt := range opts {
  655. opt(query)
  656. }
  657. _q.withPromoCodeUsages = query
  658. return _q
  659. }
  660. // WithPaymentOrders tells the query-builder to eager-load the nodes that are connected to
  661. // the "payment_orders" edge. The optional arguments are used to configure the query builder of the edge.
  662. func (_q *UserQuery) WithPaymentOrders(opts ...func(*PaymentOrderQuery)) *UserQuery {
  663. query := (&PaymentOrderClient{config: _q.config}).Query()
  664. for _, opt := range opts {
  665. opt(query)
  666. }
  667. _q.withPaymentOrders = query
  668. return _q
  669. }
  670. // WithAuthIdentities tells the query-builder to eager-load the nodes that are connected to
  671. // the "auth_identities" edge. The optional arguments are used to configure the query builder of the edge.
  672. func (_q *UserQuery) WithAuthIdentities(opts ...func(*AuthIdentityQuery)) *UserQuery {
  673. query := (&AuthIdentityClient{config: _q.config}).Query()
  674. for _, opt := range opts {
  675. opt(query)
  676. }
  677. _q.withAuthIdentities = query
  678. return _q
  679. }
  680. // WithPendingAuthSessions tells the query-builder to eager-load the nodes that are connected to
  681. // the "pending_auth_sessions" edge. The optional arguments are used to configure the query builder of the edge.
  682. func (_q *UserQuery) WithPendingAuthSessions(opts ...func(*PendingAuthSessionQuery)) *UserQuery {
  683. query := (&PendingAuthSessionClient{config: _q.config}).Query()
  684. for _, opt := range opts {
  685. opt(query)
  686. }
  687. _q.withPendingAuthSessions = query
  688. return _q
  689. }
  690. // WithPlatformQuotas tells the query-builder to eager-load the nodes that are connected to
  691. // the "platform_quotas" edge. The optional arguments are used to configure the query builder of the edge.
  692. func (_q *UserQuery) WithPlatformQuotas(opts ...func(*UserPlatformQuotaQuery)) *UserQuery {
  693. query := (&UserPlatformQuotaClient{config: _q.config}).Query()
  694. for _, opt := range opts {
  695. opt(query)
  696. }
  697. _q.withPlatformQuotas = query
  698. return _q
  699. }
  700. // WithUserAllowedGroups tells the query-builder to eager-load the nodes that are connected to
  701. // the "user_allowed_groups" edge. The optional arguments are used to configure the query builder of the edge.
  702. func (_q *UserQuery) WithUserAllowedGroups(opts ...func(*UserAllowedGroupQuery)) *UserQuery {
  703. query := (&UserAllowedGroupClient{config: _q.config}).Query()
  704. for _, opt := range opts {
  705. opt(query)
  706. }
  707. _q.withUserAllowedGroups = query
  708. return _q
  709. }
  710. // GroupBy is used to group vertices by one or more fields/columns.
  711. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  712. //
  713. // Example:
  714. //
  715. // var v []struct {
  716. // CreatedAt time.Time `json:"created_at,omitempty"`
  717. // Count int `json:"count,omitempty"`
  718. // }
  719. //
  720. // client.User.Query().
  721. // GroupBy(user.FieldCreatedAt).
  722. // Aggregate(ent.Count()).
  723. // Scan(ctx, &v)
  724. func (_q *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
  725. _q.ctx.Fields = append([]string{field}, fields...)
  726. grbuild := &UserGroupBy{build: _q}
  727. grbuild.flds = &_q.ctx.Fields
  728. grbuild.label = user.Label
  729. grbuild.scan = grbuild.Scan
  730. return grbuild
  731. }
  732. // Select allows the selection one or more fields/columns for the given query,
  733. // instead of selecting all fields in the entity.
  734. //
  735. // Example:
  736. //
  737. // var v []struct {
  738. // CreatedAt time.Time `json:"created_at,omitempty"`
  739. // }
  740. //
  741. // client.User.Query().
  742. // Select(user.FieldCreatedAt).
  743. // Scan(ctx, &v)
  744. func (_q *UserQuery) Select(fields ...string) *UserSelect {
  745. _q.ctx.Fields = append(_q.ctx.Fields, fields...)
  746. sbuild := &UserSelect{UserQuery: _q}
  747. sbuild.label = user.Label
  748. sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan
  749. return sbuild
  750. }
  751. // Aggregate returns a UserSelect configured with the given aggregations.
  752. func (_q *UserQuery) Aggregate(fns ...AggregateFunc) *UserSelect {
  753. return _q.Select().Aggregate(fns...)
  754. }
  755. func (_q *UserQuery) prepareQuery(ctx context.Context) error {
  756. for _, inter := range _q.inters {
  757. if inter == nil {
  758. return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
  759. }
  760. if trv, ok := inter.(Traverser); ok {
  761. if err := trv.Traverse(ctx, _q); err != nil {
  762. return err
  763. }
  764. }
  765. }
  766. for _, f := range _q.ctx.Fields {
  767. if !user.ValidColumn(f) {
  768. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  769. }
  770. }
  771. if _q.path != nil {
  772. prev, err := _q.path(ctx)
  773. if err != nil {
  774. return err
  775. }
  776. _q.sql = prev
  777. }
  778. return nil
  779. }
  780. func (_q *UserQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*User, error) {
  781. var (
  782. nodes = []*User{}
  783. _spec = _q.querySpec()
  784. loadedTypes = [14]bool{
  785. _q.withAPIKeys != nil,
  786. _q.withRedeemCodes != nil,
  787. _q.withSubscriptions != nil,
  788. _q.withAssignedSubscriptions != nil,
  789. _q.withAnnouncementReads != nil,
  790. _q.withAllowedGroups != nil,
  791. _q.withUsageLogs != nil,
  792. _q.withAttributeValues != nil,
  793. _q.withPromoCodeUsages != nil,
  794. _q.withPaymentOrders != nil,
  795. _q.withAuthIdentities != nil,
  796. _q.withPendingAuthSessions != nil,
  797. _q.withPlatformQuotas != nil,
  798. _q.withUserAllowedGroups != nil,
  799. }
  800. )
  801. _spec.ScanValues = func(columns []string) ([]any, error) {
  802. return (*User).scanValues(nil, columns)
  803. }
  804. _spec.Assign = func(columns []string, values []any) error {
  805. node := &User{config: _q.config}
  806. nodes = append(nodes, node)
  807. node.Edges.loadedTypes = loadedTypes
  808. return node.assignValues(columns, values)
  809. }
  810. if len(_q.modifiers) > 0 {
  811. _spec.Modifiers = _q.modifiers
  812. }
  813. for i := range hooks {
  814. hooks[i](ctx, _spec)
  815. }
  816. if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil {
  817. return nil, err
  818. }
  819. if len(nodes) == 0 {
  820. return nodes, nil
  821. }
  822. if query := _q.withAPIKeys; query != nil {
  823. if err := _q.loadAPIKeys(ctx, query, nodes,
  824. func(n *User) { n.Edges.APIKeys = []*APIKey{} },
  825. func(n *User, e *APIKey) { n.Edges.APIKeys = append(n.Edges.APIKeys, e) }); err != nil {
  826. return nil, err
  827. }
  828. }
  829. if query := _q.withRedeemCodes; query != nil {
  830. if err := _q.loadRedeemCodes(ctx, query, nodes,
  831. func(n *User) { n.Edges.RedeemCodes = []*RedeemCode{} },
  832. func(n *User, e *RedeemCode) { n.Edges.RedeemCodes = append(n.Edges.RedeemCodes, e) }); err != nil {
  833. return nil, err
  834. }
  835. }
  836. if query := _q.withSubscriptions; query != nil {
  837. if err := _q.loadSubscriptions(ctx, query, nodes,
  838. func(n *User) { n.Edges.Subscriptions = []*UserSubscription{} },
  839. func(n *User, e *UserSubscription) { n.Edges.Subscriptions = append(n.Edges.Subscriptions, e) }); err != nil {
  840. return nil, err
  841. }
  842. }
  843. if query := _q.withAssignedSubscriptions; query != nil {
  844. if err := _q.loadAssignedSubscriptions(ctx, query, nodes,
  845. func(n *User) { n.Edges.AssignedSubscriptions = []*UserSubscription{} },
  846. func(n *User, e *UserSubscription) {
  847. n.Edges.AssignedSubscriptions = append(n.Edges.AssignedSubscriptions, e)
  848. }); err != nil {
  849. return nil, err
  850. }
  851. }
  852. if query := _q.withAnnouncementReads; query != nil {
  853. if err := _q.loadAnnouncementReads(ctx, query, nodes,
  854. func(n *User) { n.Edges.AnnouncementReads = []*AnnouncementRead{} },
  855. func(n *User, e *AnnouncementRead) { n.Edges.AnnouncementReads = append(n.Edges.AnnouncementReads, e) }); err != nil {
  856. return nil, err
  857. }
  858. }
  859. if query := _q.withAllowedGroups; query != nil {
  860. if err := _q.loadAllowedGroups(ctx, query, nodes,
  861. func(n *User) { n.Edges.AllowedGroups = []*Group{} },
  862. func(n *User, e *Group) { n.Edges.AllowedGroups = append(n.Edges.AllowedGroups, e) }); err != nil {
  863. return nil, err
  864. }
  865. }
  866. if query := _q.withUsageLogs; query != nil {
  867. if err := _q.loadUsageLogs(ctx, query, nodes,
  868. func(n *User) { n.Edges.UsageLogs = []*UsageLog{} },
  869. func(n *User, e *UsageLog) { n.Edges.UsageLogs = append(n.Edges.UsageLogs, e) }); err != nil {
  870. return nil, err
  871. }
  872. }
  873. if query := _q.withAttributeValues; query != nil {
  874. if err := _q.loadAttributeValues(ctx, query, nodes,
  875. func(n *User) { n.Edges.AttributeValues = []*UserAttributeValue{} },
  876. func(n *User, e *UserAttributeValue) { n.Edges.AttributeValues = append(n.Edges.AttributeValues, e) }); err != nil {
  877. return nil, err
  878. }
  879. }
  880. if query := _q.withPromoCodeUsages; query != nil {
  881. if err := _q.loadPromoCodeUsages(ctx, query, nodes,
  882. func(n *User) { n.Edges.PromoCodeUsages = []*PromoCodeUsage{} },
  883. func(n *User, e *PromoCodeUsage) { n.Edges.PromoCodeUsages = append(n.Edges.PromoCodeUsages, e) }); err != nil {
  884. return nil, err
  885. }
  886. }
  887. if query := _q.withPaymentOrders; query != nil {
  888. if err := _q.loadPaymentOrders(ctx, query, nodes,
  889. func(n *User) { n.Edges.PaymentOrders = []*PaymentOrder{} },
  890. func(n *User, e *PaymentOrder) { n.Edges.PaymentOrders = append(n.Edges.PaymentOrders, e) }); err != nil {
  891. return nil, err
  892. }
  893. }
  894. if query := _q.withAuthIdentities; query != nil {
  895. if err := _q.loadAuthIdentities(ctx, query, nodes,
  896. func(n *User) { n.Edges.AuthIdentities = []*AuthIdentity{} },
  897. func(n *User, e *AuthIdentity) { n.Edges.AuthIdentities = append(n.Edges.AuthIdentities, e) }); err != nil {
  898. return nil, err
  899. }
  900. }
  901. if query := _q.withPendingAuthSessions; query != nil {
  902. if err := _q.loadPendingAuthSessions(ctx, query, nodes,
  903. func(n *User) { n.Edges.PendingAuthSessions = []*PendingAuthSession{} },
  904. func(n *User, e *PendingAuthSession) {
  905. n.Edges.PendingAuthSessions = append(n.Edges.PendingAuthSessions, e)
  906. }); err != nil {
  907. return nil, err
  908. }
  909. }
  910. if query := _q.withPlatformQuotas; query != nil {
  911. if err := _q.loadPlatformQuotas(ctx, query, nodes,
  912. func(n *User) { n.Edges.PlatformQuotas = []*UserPlatformQuota{} },
  913. func(n *User, e *UserPlatformQuota) { n.Edges.PlatformQuotas = append(n.Edges.PlatformQuotas, e) }); err != nil {
  914. return nil, err
  915. }
  916. }
  917. if query := _q.withUserAllowedGroups; query != nil {
  918. if err := _q.loadUserAllowedGroups(ctx, query, nodes,
  919. func(n *User) { n.Edges.UserAllowedGroups = []*UserAllowedGroup{} },
  920. func(n *User, e *UserAllowedGroup) { n.Edges.UserAllowedGroups = append(n.Edges.UserAllowedGroups, e) }); err != nil {
  921. return nil, err
  922. }
  923. }
  924. return nodes, nil
  925. }
  926. func (_q *UserQuery) loadAPIKeys(ctx context.Context, query *APIKeyQuery, nodes []*User, init func(*User), assign func(*User, *APIKey)) error {
  927. fks := make([]driver.Value, 0, len(nodes))
  928. nodeids := make(map[int64]*User)
  929. for i := range nodes {
  930. fks = append(fks, nodes[i].ID)
  931. nodeids[nodes[i].ID] = nodes[i]
  932. if init != nil {
  933. init(nodes[i])
  934. }
  935. }
  936. if len(query.ctx.Fields) > 0 {
  937. query.ctx.AppendFieldOnce(apikey.FieldUserID)
  938. }
  939. query.Where(predicate.APIKey(func(s *sql.Selector) {
  940. s.Where(sql.InValues(s.C(user.APIKeysColumn), fks...))
  941. }))
  942. neighbors, err := query.All(ctx)
  943. if err != nil {
  944. return err
  945. }
  946. for _, n := range neighbors {
  947. fk := n.UserID
  948. node, ok := nodeids[fk]
  949. if !ok {
  950. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  951. }
  952. assign(node, n)
  953. }
  954. return nil
  955. }
  956. func (_q *UserQuery) loadRedeemCodes(ctx context.Context, query *RedeemCodeQuery, nodes []*User, init func(*User), assign func(*User, *RedeemCode)) error {
  957. fks := make([]driver.Value, 0, len(nodes))
  958. nodeids := make(map[int64]*User)
  959. for i := range nodes {
  960. fks = append(fks, nodes[i].ID)
  961. nodeids[nodes[i].ID] = nodes[i]
  962. if init != nil {
  963. init(nodes[i])
  964. }
  965. }
  966. if len(query.ctx.Fields) > 0 {
  967. query.ctx.AppendFieldOnce(redeemcode.FieldUsedBy)
  968. }
  969. query.Where(predicate.RedeemCode(func(s *sql.Selector) {
  970. s.Where(sql.InValues(s.C(user.RedeemCodesColumn), fks...))
  971. }))
  972. neighbors, err := query.All(ctx)
  973. if err != nil {
  974. return err
  975. }
  976. for _, n := range neighbors {
  977. fk := n.UsedBy
  978. if fk == nil {
  979. return fmt.Errorf(`foreign-key "used_by" is nil for node %v`, n.ID)
  980. }
  981. node, ok := nodeids[*fk]
  982. if !ok {
  983. return fmt.Errorf(`unexpected referenced foreign-key "used_by" returned %v for node %v`, *fk, n.ID)
  984. }
  985. assign(node, n)
  986. }
  987. return nil
  988. }
  989. func (_q *UserQuery) loadSubscriptions(ctx context.Context, query *UserSubscriptionQuery, nodes []*User, init func(*User), assign func(*User, *UserSubscription)) error {
  990. fks := make([]driver.Value, 0, len(nodes))
  991. nodeids := make(map[int64]*User)
  992. for i := range nodes {
  993. fks = append(fks, nodes[i].ID)
  994. nodeids[nodes[i].ID] = nodes[i]
  995. if init != nil {
  996. init(nodes[i])
  997. }
  998. }
  999. if len(query.ctx.Fields) > 0 {
  1000. query.ctx.AppendFieldOnce(usersubscription.FieldUserID)
  1001. }
  1002. query.Where(predicate.UserSubscription(func(s *sql.Selector) {
  1003. s.Where(sql.InValues(s.C(user.SubscriptionsColumn), fks...))
  1004. }))
  1005. neighbors, err := query.All(ctx)
  1006. if err != nil {
  1007. return err
  1008. }
  1009. for _, n := range neighbors {
  1010. fk := n.UserID
  1011. node, ok := nodeids[fk]
  1012. if !ok {
  1013. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  1014. }
  1015. assign(node, n)
  1016. }
  1017. return nil
  1018. }
  1019. func (_q *UserQuery) loadAssignedSubscriptions(ctx context.Context, query *UserSubscriptionQuery, nodes []*User, init func(*User), assign func(*User, *UserSubscription)) error {
  1020. fks := make([]driver.Value, 0, len(nodes))
  1021. nodeids := make(map[int64]*User)
  1022. for i := range nodes {
  1023. fks = append(fks, nodes[i].ID)
  1024. nodeids[nodes[i].ID] = nodes[i]
  1025. if init != nil {
  1026. init(nodes[i])
  1027. }
  1028. }
  1029. if len(query.ctx.Fields) > 0 {
  1030. query.ctx.AppendFieldOnce(usersubscription.FieldAssignedBy)
  1031. }
  1032. query.Where(predicate.UserSubscription(func(s *sql.Selector) {
  1033. s.Where(sql.InValues(s.C(user.AssignedSubscriptionsColumn), fks...))
  1034. }))
  1035. neighbors, err := query.All(ctx)
  1036. if err != nil {
  1037. return err
  1038. }
  1039. for _, n := range neighbors {
  1040. fk := n.AssignedBy
  1041. if fk == nil {
  1042. return fmt.Errorf(`foreign-key "assigned_by" is nil for node %v`, n.ID)
  1043. }
  1044. node, ok := nodeids[*fk]
  1045. if !ok {
  1046. return fmt.Errorf(`unexpected referenced foreign-key "assigned_by" returned %v for node %v`, *fk, n.ID)
  1047. }
  1048. assign(node, n)
  1049. }
  1050. return nil
  1051. }
  1052. func (_q *UserQuery) loadAnnouncementReads(ctx context.Context, query *AnnouncementReadQuery, nodes []*User, init func(*User), assign func(*User, *AnnouncementRead)) error {
  1053. fks := make([]driver.Value, 0, len(nodes))
  1054. nodeids := make(map[int64]*User)
  1055. for i := range nodes {
  1056. fks = append(fks, nodes[i].ID)
  1057. nodeids[nodes[i].ID] = nodes[i]
  1058. if init != nil {
  1059. init(nodes[i])
  1060. }
  1061. }
  1062. if len(query.ctx.Fields) > 0 {
  1063. query.ctx.AppendFieldOnce(announcementread.FieldUserID)
  1064. }
  1065. query.Where(predicate.AnnouncementRead(func(s *sql.Selector) {
  1066. s.Where(sql.InValues(s.C(user.AnnouncementReadsColumn), fks...))
  1067. }))
  1068. neighbors, err := query.All(ctx)
  1069. if err != nil {
  1070. return err
  1071. }
  1072. for _, n := range neighbors {
  1073. fk := n.UserID
  1074. node, ok := nodeids[fk]
  1075. if !ok {
  1076. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  1077. }
  1078. assign(node, n)
  1079. }
  1080. return nil
  1081. }
  1082. func (_q *UserQuery) loadAllowedGroups(ctx context.Context, query *GroupQuery, nodes []*User, init func(*User), assign func(*User, *Group)) error {
  1083. edgeIDs := make([]driver.Value, len(nodes))
  1084. byID := make(map[int64]*User)
  1085. nids := make(map[int64]map[*User]struct{})
  1086. for i, node := range nodes {
  1087. edgeIDs[i] = node.ID
  1088. byID[node.ID] = node
  1089. if init != nil {
  1090. init(node)
  1091. }
  1092. }
  1093. query.Where(func(s *sql.Selector) {
  1094. joinT := sql.Table(user.AllowedGroupsTable)
  1095. s.Join(joinT).On(s.C(group.FieldID), joinT.C(user.AllowedGroupsPrimaryKey[1]))
  1096. s.Where(sql.InValues(joinT.C(user.AllowedGroupsPrimaryKey[0]), edgeIDs...))
  1097. columns := s.SelectedColumns()
  1098. s.Select(joinT.C(user.AllowedGroupsPrimaryKey[0]))
  1099. s.AppendSelect(columns...)
  1100. s.SetDistinct(false)
  1101. })
  1102. if err := query.prepareQuery(ctx); err != nil {
  1103. return err
  1104. }
  1105. qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
  1106. return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
  1107. assign := spec.Assign
  1108. values := spec.ScanValues
  1109. spec.ScanValues = func(columns []string) ([]any, error) {
  1110. values, err := values(columns[1:])
  1111. if err != nil {
  1112. return nil, err
  1113. }
  1114. return append([]any{new(sql.NullInt64)}, values...), nil
  1115. }
  1116. spec.Assign = func(columns []string, values []any) error {
  1117. outValue := values[0].(*sql.NullInt64).Int64
  1118. inValue := values[1].(*sql.NullInt64).Int64
  1119. if nids[inValue] == nil {
  1120. nids[inValue] = map[*User]struct{}{byID[outValue]: {}}
  1121. return assign(columns[1:], values[1:])
  1122. }
  1123. nids[inValue][byID[outValue]] = struct{}{}
  1124. return nil
  1125. }
  1126. })
  1127. })
  1128. neighbors, err := withInterceptors[[]*Group](ctx, query, qr, query.inters)
  1129. if err != nil {
  1130. return err
  1131. }
  1132. for _, n := range neighbors {
  1133. nodes, ok := nids[n.ID]
  1134. if !ok {
  1135. return fmt.Errorf(`unexpected "allowed_groups" node returned %v`, n.ID)
  1136. }
  1137. for kn := range nodes {
  1138. assign(kn, n)
  1139. }
  1140. }
  1141. return nil
  1142. }
  1143. func (_q *UserQuery) loadUsageLogs(ctx context.Context, query *UsageLogQuery, nodes []*User, init func(*User), assign func(*User, *UsageLog)) error {
  1144. fks := make([]driver.Value, 0, len(nodes))
  1145. nodeids := make(map[int64]*User)
  1146. for i := range nodes {
  1147. fks = append(fks, nodes[i].ID)
  1148. nodeids[nodes[i].ID] = nodes[i]
  1149. if init != nil {
  1150. init(nodes[i])
  1151. }
  1152. }
  1153. if len(query.ctx.Fields) > 0 {
  1154. query.ctx.AppendFieldOnce(usagelog.FieldUserID)
  1155. }
  1156. query.Where(predicate.UsageLog(func(s *sql.Selector) {
  1157. s.Where(sql.InValues(s.C(user.UsageLogsColumn), fks...))
  1158. }))
  1159. neighbors, err := query.All(ctx)
  1160. if err != nil {
  1161. return err
  1162. }
  1163. for _, n := range neighbors {
  1164. fk := n.UserID
  1165. node, ok := nodeids[fk]
  1166. if !ok {
  1167. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  1168. }
  1169. assign(node, n)
  1170. }
  1171. return nil
  1172. }
  1173. func (_q *UserQuery) loadAttributeValues(ctx context.Context, query *UserAttributeValueQuery, nodes []*User, init func(*User), assign func(*User, *UserAttributeValue)) error {
  1174. fks := make([]driver.Value, 0, len(nodes))
  1175. nodeids := make(map[int64]*User)
  1176. for i := range nodes {
  1177. fks = append(fks, nodes[i].ID)
  1178. nodeids[nodes[i].ID] = nodes[i]
  1179. if init != nil {
  1180. init(nodes[i])
  1181. }
  1182. }
  1183. if len(query.ctx.Fields) > 0 {
  1184. query.ctx.AppendFieldOnce(userattributevalue.FieldUserID)
  1185. }
  1186. query.Where(predicate.UserAttributeValue(func(s *sql.Selector) {
  1187. s.Where(sql.InValues(s.C(user.AttributeValuesColumn), fks...))
  1188. }))
  1189. neighbors, err := query.All(ctx)
  1190. if err != nil {
  1191. return err
  1192. }
  1193. for _, n := range neighbors {
  1194. fk := n.UserID
  1195. node, ok := nodeids[fk]
  1196. if !ok {
  1197. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  1198. }
  1199. assign(node, n)
  1200. }
  1201. return nil
  1202. }
  1203. func (_q *UserQuery) loadPromoCodeUsages(ctx context.Context, query *PromoCodeUsageQuery, nodes []*User, init func(*User), assign func(*User, *PromoCodeUsage)) error {
  1204. fks := make([]driver.Value, 0, len(nodes))
  1205. nodeids := make(map[int64]*User)
  1206. for i := range nodes {
  1207. fks = append(fks, nodes[i].ID)
  1208. nodeids[nodes[i].ID] = nodes[i]
  1209. if init != nil {
  1210. init(nodes[i])
  1211. }
  1212. }
  1213. if len(query.ctx.Fields) > 0 {
  1214. query.ctx.AppendFieldOnce(promocodeusage.FieldUserID)
  1215. }
  1216. query.Where(predicate.PromoCodeUsage(func(s *sql.Selector) {
  1217. s.Where(sql.InValues(s.C(user.PromoCodeUsagesColumn), fks...))
  1218. }))
  1219. neighbors, err := query.All(ctx)
  1220. if err != nil {
  1221. return err
  1222. }
  1223. for _, n := range neighbors {
  1224. fk := n.UserID
  1225. node, ok := nodeids[fk]
  1226. if !ok {
  1227. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  1228. }
  1229. assign(node, n)
  1230. }
  1231. return nil
  1232. }
  1233. func (_q *UserQuery) loadPaymentOrders(ctx context.Context, query *PaymentOrderQuery, nodes []*User, init func(*User), assign func(*User, *PaymentOrder)) error {
  1234. fks := make([]driver.Value, 0, len(nodes))
  1235. nodeids := make(map[int64]*User)
  1236. for i := range nodes {
  1237. fks = append(fks, nodes[i].ID)
  1238. nodeids[nodes[i].ID] = nodes[i]
  1239. if init != nil {
  1240. init(nodes[i])
  1241. }
  1242. }
  1243. if len(query.ctx.Fields) > 0 {
  1244. query.ctx.AppendFieldOnce(paymentorder.FieldUserID)
  1245. }
  1246. query.Where(predicate.PaymentOrder(func(s *sql.Selector) {
  1247. s.Where(sql.InValues(s.C(user.PaymentOrdersColumn), fks...))
  1248. }))
  1249. neighbors, err := query.All(ctx)
  1250. if err != nil {
  1251. return err
  1252. }
  1253. for _, n := range neighbors {
  1254. fk := n.UserID
  1255. node, ok := nodeids[fk]
  1256. if !ok {
  1257. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  1258. }
  1259. assign(node, n)
  1260. }
  1261. return nil
  1262. }
  1263. func (_q *UserQuery) loadAuthIdentities(ctx context.Context, query *AuthIdentityQuery, nodes []*User, init func(*User), assign func(*User, *AuthIdentity)) error {
  1264. fks := make([]driver.Value, 0, len(nodes))
  1265. nodeids := make(map[int64]*User)
  1266. for i := range nodes {
  1267. fks = append(fks, nodes[i].ID)
  1268. nodeids[nodes[i].ID] = nodes[i]
  1269. if init != nil {
  1270. init(nodes[i])
  1271. }
  1272. }
  1273. if len(query.ctx.Fields) > 0 {
  1274. query.ctx.AppendFieldOnce(authidentity.FieldUserID)
  1275. }
  1276. query.Where(predicate.AuthIdentity(func(s *sql.Selector) {
  1277. s.Where(sql.InValues(s.C(user.AuthIdentitiesColumn), fks...))
  1278. }))
  1279. neighbors, err := query.All(ctx)
  1280. if err != nil {
  1281. return err
  1282. }
  1283. for _, n := range neighbors {
  1284. fk := n.UserID
  1285. node, ok := nodeids[fk]
  1286. if !ok {
  1287. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  1288. }
  1289. assign(node, n)
  1290. }
  1291. return nil
  1292. }
  1293. func (_q *UserQuery) loadPendingAuthSessions(ctx context.Context, query *PendingAuthSessionQuery, nodes []*User, init func(*User), assign func(*User, *PendingAuthSession)) error {
  1294. fks := make([]driver.Value, 0, len(nodes))
  1295. nodeids := make(map[int64]*User)
  1296. for i := range nodes {
  1297. fks = append(fks, nodes[i].ID)
  1298. nodeids[nodes[i].ID] = nodes[i]
  1299. if init != nil {
  1300. init(nodes[i])
  1301. }
  1302. }
  1303. if len(query.ctx.Fields) > 0 {
  1304. query.ctx.AppendFieldOnce(pendingauthsession.FieldTargetUserID)
  1305. }
  1306. query.Where(predicate.PendingAuthSession(func(s *sql.Selector) {
  1307. s.Where(sql.InValues(s.C(user.PendingAuthSessionsColumn), fks...))
  1308. }))
  1309. neighbors, err := query.All(ctx)
  1310. if err != nil {
  1311. return err
  1312. }
  1313. for _, n := range neighbors {
  1314. fk := n.TargetUserID
  1315. if fk == nil {
  1316. return fmt.Errorf(`foreign-key "target_user_id" is nil for node %v`, n.ID)
  1317. }
  1318. node, ok := nodeids[*fk]
  1319. if !ok {
  1320. return fmt.Errorf(`unexpected referenced foreign-key "target_user_id" returned %v for node %v`, *fk, n.ID)
  1321. }
  1322. assign(node, n)
  1323. }
  1324. return nil
  1325. }
  1326. func (_q *UserQuery) loadPlatformQuotas(ctx context.Context, query *UserPlatformQuotaQuery, nodes []*User, init func(*User), assign func(*User, *UserPlatformQuota)) error {
  1327. fks := make([]driver.Value, 0, len(nodes))
  1328. nodeids := make(map[int64]*User)
  1329. for i := range nodes {
  1330. fks = append(fks, nodes[i].ID)
  1331. nodeids[nodes[i].ID] = nodes[i]
  1332. if init != nil {
  1333. init(nodes[i])
  1334. }
  1335. }
  1336. if len(query.ctx.Fields) > 0 {
  1337. query.ctx.AppendFieldOnce(userplatformquota.FieldUserID)
  1338. }
  1339. query.Where(predicate.UserPlatformQuota(func(s *sql.Selector) {
  1340. s.Where(sql.InValues(s.C(user.PlatformQuotasColumn), fks...))
  1341. }))
  1342. neighbors, err := query.All(ctx)
  1343. if err != nil {
  1344. return err
  1345. }
  1346. for _, n := range neighbors {
  1347. fk := n.UserID
  1348. node, ok := nodeids[fk]
  1349. if !ok {
  1350. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n.ID)
  1351. }
  1352. assign(node, n)
  1353. }
  1354. return nil
  1355. }
  1356. func (_q *UserQuery) loadUserAllowedGroups(ctx context.Context, query *UserAllowedGroupQuery, nodes []*User, init func(*User), assign func(*User, *UserAllowedGroup)) error {
  1357. fks := make([]driver.Value, 0, len(nodes))
  1358. nodeids := make(map[int64]*User)
  1359. for i := range nodes {
  1360. fks = append(fks, nodes[i].ID)
  1361. nodeids[nodes[i].ID] = nodes[i]
  1362. if init != nil {
  1363. init(nodes[i])
  1364. }
  1365. }
  1366. if len(query.ctx.Fields) > 0 {
  1367. query.ctx.AppendFieldOnce(userallowedgroup.FieldUserID)
  1368. }
  1369. query.Where(predicate.UserAllowedGroup(func(s *sql.Selector) {
  1370. s.Where(sql.InValues(s.C(user.UserAllowedGroupsColumn), fks...))
  1371. }))
  1372. neighbors, err := query.All(ctx)
  1373. if err != nil {
  1374. return err
  1375. }
  1376. for _, n := range neighbors {
  1377. fk := n.UserID
  1378. node, ok := nodeids[fk]
  1379. if !ok {
  1380. return fmt.Errorf(`unexpected referenced foreign-key "user_id" returned %v for node %v`, fk, n)
  1381. }
  1382. assign(node, n)
  1383. }
  1384. return nil
  1385. }
  1386. func (_q *UserQuery) sqlCount(ctx context.Context) (int, error) {
  1387. _spec := _q.querySpec()
  1388. if len(_q.modifiers) > 0 {
  1389. _spec.Modifiers = _q.modifiers
  1390. }
  1391. _spec.Node.Columns = _q.ctx.Fields
  1392. if len(_q.ctx.Fields) > 0 {
  1393. _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique
  1394. }
  1395. return sqlgraph.CountNodes(ctx, _q.driver, _spec)
  1396. }
  1397. func (_q *UserQuery) querySpec() *sqlgraph.QuerySpec {
  1398. _spec := sqlgraph.NewQuerySpec(user.Table, user.Columns, sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64))
  1399. _spec.From = _q.sql
  1400. if unique := _q.ctx.Unique; unique != nil {
  1401. _spec.Unique = *unique
  1402. } else if _q.path != nil {
  1403. _spec.Unique = true
  1404. }
  1405. if fields := _q.ctx.Fields; len(fields) > 0 {
  1406. _spec.Node.Columns = make([]string, 0, len(fields))
  1407. _spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
  1408. for i := range fields {
  1409. if fields[i] != user.FieldID {
  1410. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  1411. }
  1412. }
  1413. }
  1414. if ps := _q.predicates; len(ps) > 0 {
  1415. _spec.Predicate = func(selector *sql.Selector) {
  1416. for i := range ps {
  1417. ps[i](selector)
  1418. }
  1419. }
  1420. }
  1421. if limit := _q.ctx.Limit; limit != nil {
  1422. _spec.Limit = *limit
  1423. }
  1424. if offset := _q.ctx.Offset; offset != nil {
  1425. _spec.Offset = *offset
  1426. }
  1427. if ps := _q.order; len(ps) > 0 {
  1428. _spec.Order = func(selector *sql.Selector) {
  1429. for i := range ps {
  1430. ps[i](selector)
  1431. }
  1432. }
  1433. }
  1434. return _spec
  1435. }
  1436. func (_q *UserQuery) sqlQuery(ctx context.Context) *sql.Selector {
  1437. builder := sql.Dialect(_q.driver.Dialect())
  1438. t1 := builder.Table(user.Table)
  1439. columns := _q.ctx.Fields
  1440. if len(columns) == 0 {
  1441. columns = user.Columns
  1442. }
  1443. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  1444. if _q.sql != nil {
  1445. selector = _q.sql
  1446. selector.Select(selector.Columns(columns...)...)
  1447. }
  1448. if _q.ctx.Unique != nil && *_q.ctx.Unique {
  1449. selector.Distinct()
  1450. }
  1451. for _, m := range _q.modifiers {
  1452. m(selector)
  1453. }
  1454. for _, p := range _q.predicates {
  1455. p(selector)
  1456. }
  1457. for _, p := range _q.order {
  1458. p(selector)
  1459. }
  1460. if offset := _q.ctx.Offset; offset != nil {
  1461. // limit is mandatory for offset clause. We start
  1462. // with default value, and override it below if needed.
  1463. selector.Offset(*offset).Limit(math.MaxInt32)
  1464. }
  1465. if limit := _q.ctx.Limit; limit != nil {
  1466. selector.Limit(*limit)
  1467. }
  1468. return selector
  1469. }
  1470. // ForUpdate locks the selected rows against concurrent updates, and prevent them from being
  1471. // updated, deleted or "selected ... for update" by other sessions, until the transaction is
  1472. // either committed or rolled-back.
  1473. func (_q *UserQuery) ForUpdate(opts ...sql.LockOption) *UserQuery {
  1474. if _q.driver.Dialect() == dialect.Postgres {
  1475. _q.Unique(false)
  1476. }
  1477. _q.modifiers = append(_q.modifiers, func(s *sql.Selector) {
  1478. s.ForUpdate(opts...)
  1479. })
  1480. return _q
  1481. }
  1482. // ForShare behaves similarly to ForUpdate, except that it acquires a shared mode lock
  1483. // on any rows that are read. Other sessions can read the rows, but cannot modify them
  1484. // until your transaction commits.
  1485. func (_q *UserQuery) ForShare(opts ...sql.LockOption) *UserQuery {
  1486. if _q.driver.Dialect() == dialect.Postgres {
  1487. _q.Unique(false)
  1488. }
  1489. _q.modifiers = append(_q.modifiers, func(s *sql.Selector) {
  1490. s.ForShare(opts...)
  1491. })
  1492. return _q
  1493. }
  1494. // UserGroupBy is the group-by builder for User entities.
  1495. type UserGroupBy struct {
  1496. selector
  1497. build *UserQuery
  1498. }
  1499. // Aggregate adds the given aggregation functions to the group-by query.
  1500. func (_g *UserGroupBy) Aggregate(fns ...AggregateFunc) *UserGroupBy {
  1501. _g.fns = append(_g.fns, fns...)
  1502. return _g
  1503. }
  1504. // Scan applies the selector query and scans the result into the given value.
  1505. func (_g *UserGroupBy) Scan(ctx context.Context, v any) error {
  1506. ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy)
  1507. if err := _g.build.prepareQuery(ctx); err != nil {
  1508. return err
  1509. }
  1510. return scanWithInterceptors[*UserQuery, *UserGroupBy](ctx, _g.build, _g, _g.build.inters, v)
  1511. }
  1512. func (_g *UserGroupBy) sqlScan(ctx context.Context, root *UserQuery, v any) error {
  1513. selector := root.sqlQuery(ctx).Select()
  1514. aggregation := make([]string, 0, len(_g.fns))
  1515. for _, fn := range _g.fns {
  1516. aggregation = append(aggregation, fn(selector))
  1517. }
  1518. if len(selector.SelectedColumns()) == 0 {
  1519. columns := make([]string, 0, len(*_g.flds)+len(_g.fns))
  1520. for _, f := range *_g.flds {
  1521. columns = append(columns, selector.C(f))
  1522. }
  1523. columns = append(columns, aggregation...)
  1524. selector.Select(columns...)
  1525. }
  1526. selector.GroupBy(selector.Columns(*_g.flds...)...)
  1527. if err := selector.Err(); err != nil {
  1528. return err
  1529. }
  1530. rows := &sql.Rows{}
  1531. query, args := selector.Query()
  1532. if err := _g.build.driver.Query(ctx, query, args, rows); err != nil {
  1533. return err
  1534. }
  1535. defer rows.Close()
  1536. return sql.ScanSlice(rows, v)
  1537. }
  1538. // UserSelect is the builder for selecting fields of User entities.
  1539. type UserSelect struct {
  1540. *UserQuery
  1541. selector
  1542. }
  1543. // Aggregate adds the given aggregation functions to the selector query.
  1544. func (_s *UserSelect) Aggregate(fns ...AggregateFunc) *UserSelect {
  1545. _s.fns = append(_s.fns, fns...)
  1546. return _s
  1547. }
  1548. // Scan applies the selector query and scans the result into the given value.
  1549. func (_s *UserSelect) Scan(ctx context.Context, v any) error {
  1550. ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect)
  1551. if err := _s.prepareQuery(ctx); err != nil {
  1552. return err
  1553. }
  1554. return scanWithInterceptors[*UserQuery, *UserSelect](ctx, _s.UserQuery, _s, _s.inters, v)
  1555. }
  1556. func (_s *UserSelect) sqlScan(ctx context.Context, root *UserQuery, v any) error {
  1557. selector := root.sqlQuery(ctx)
  1558. aggregation := make([]string, 0, len(_s.fns))
  1559. for _, fn := range _s.fns {
  1560. aggregation = append(aggregation, fn(selector))
  1561. }
  1562. switch n := len(*_s.selector.flds); {
  1563. case n == 0 && len(aggregation) > 0:
  1564. selector.Select(aggregation...)
  1565. case n != 0 && len(aggregation) > 0:
  1566. selector.AppendSelect(aggregation...)
  1567. }
  1568. rows := &sql.Rows{}
  1569. query, args := selector.Query()
  1570. if err := _s.driver.Query(ctx, query, args, rows); err != nil {
  1571. return err
  1572. }
  1573. defer rows.Close()
  1574. return sql.ScanSlice(rows, v)
  1575. }