group_query.go 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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/account"
  14. "github.com/Wei-Shaw/sub2api/ent/accountgroup"
  15. "github.com/Wei-Shaw/sub2api/ent/apikey"
  16. "github.com/Wei-Shaw/sub2api/ent/group"
  17. "github.com/Wei-Shaw/sub2api/ent/predicate"
  18. "github.com/Wei-Shaw/sub2api/ent/redeemcode"
  19. "github.com/Wei-Shaw/sub2api/ent/usagelog"
  20. "github.com/Wei-Shaw/sub2api/ent/user"
  21. "github.com/Wei-Shaw/sub2api/ent/userallowedgroup"
  22. "github.com/Wei-Shaw/sub2api/ent/usersubscription"
  23. )
  24. // GroupQuery is the builder for querying Group entities.
  25. type GroupQuery struct {
  26. config
  27. ctx *QueryContext
  28. order []group.OrderOption
  29. inters []Interceptor
  30. predicates []predicate.Group
  31. withAPIKeys *APIKeyQuery
  32. withRedeemCodes *RedeemCodeQuery
  33. withSubscriptions *UserSubscriptionQuery
  34. withUsageLogs *UsageLogQuery
  35. withAccounts *AccountQuery
  36. withAllowedUsers *UserQuery
  37. withAccountGroups *AccountGroupQuery
  38. withUserAllowedGroups *UserAllowedGroupQuery
  39. modifiers []func(*sql.Selector)
  40. // intermediate query (i.e. traversal path).
  41. sql *sql.Selector
  42. path func(context.Context) (*sql.Selector, error)
  43. }
  44. // Where adds a new predicate for the GroupQuery builder.
  45. func (_q *GroupQuery) Where(ps ...predicate.Group) *GroupQuery {
  46. _q.predicates = append(_q.predicates, ps...)
  47. return _q
  48. }
  49. // Limit the number of records to be returned by this query.
  50. func (_q *GroupQuery) Limit(limit int) *GroupQuery {
  51. _q.ctx.Limit = &limit
  52. return _q
  53. }
  54. // Offset to start from.
  55. func (_q *GroupQuery) Offset(offset int) *GroupQuery {
  56. _q.ctx.Offset = &offset
  57. return _q
  58. }
  59. // Unique configures the query builder to filter duplicate records on query.
  60. // By default, unique is set to true, and can be disabled using this method.
  61. func (_q *GroupQuery) Unique(unique bool) *GroupQuery {
  62. _q.ctx.Unique = &unique
  63. return _q
  64. }
  65. // Order specifies how the records should be ordered.
  66. func (_q *GroupQuery) Order(o ...group.OrderOption) *GroupQuery {
  67. _q.order = append(_q.order, o...)
  68. return _q
  69. }
  70. // QueryAPIKeys chains the current query on the "api_keys" edge.
  71. func (_q *GroupQuery) QueryAPIKeys() *APIKeyQuery {
  72. query := (&APIKeyClient{config: _q.config}).Query()
  73. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  74. if err := _q.prepareQuery(ctx); err != nil {
  75. return nil, err
  76. }
  77. selector := _q.sqlQuery(ctx)
  78. if err := selector.Err(); err != nil {
  79. return nil, err
  80. }
  81. step := sqlgraph.NewStep(
  82. sqlgraph.From(group.Table, group.FieldID, selector),
  83. sqlgraph.To(apikey.Table, apikey.FieldID),
  84. sqlgraph.Edge(sqlgraph.O2M, false, group.APIKeysTable, group.APIKeysColumn),
  85. )
  86. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  87. return fromU, nil
  88. }
  89. return query
  90. }
  91. // QueryRedeemCodes chains the current query on the "redeem_codes" edge.
  92. func (_q *GroupQuery) QueryRedeemCodes() *RedeemCodeQuery {
  93. query := (&RedeemCodeClient{config: _q.config}).Query()
  94. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  95. if err := _q.prepareQuery(ctx); err != nil {
  96. return nil, err
  97. }
  98. selector := _q.sqlQuery(ctx)
  99. if err := selector.Err(); err != nil {
  100. return nil, err
  101. }
  102. step := sqlgraph.NewStep(
  103. sqlgraph.From(group.Table, group.FieldID, selector),
  104. sqlgraph.To(redeemcode.Table, redeemcode.FieldID),
  105. sqlgraph.Edge(sqlgraph.O2M, false, group.RedeemCodesTable, group.RedeemCodesColumn),
  106. )
  107. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  108. return fromU, nil
  109. }
  110. return query
  111. }
  112. // QuerySubscriptions chains the current query on the "subscriptions" edge.
  113. func (_q *GroupQuery) QuerySubscriptions() *UserSubscriptionQuery {
  114. query := (&UserSubscriptionClient{config: _q.config}).Query()
  115. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  116. if err := _q.prepareQuery(ctx); err != nil {
  117. return nil, err
  118. }
  119. selector := _q.sqlQuery(ctx)
  120. if err := selector.Err(); err != nil {
  121. return nil, err
  122. }
  123. step := sqlgraph.NewStep(
  124. sqlgraph.From(group.Table, group.FieldID, selector),
  125. sqlgraph.To(usersubscription.Table, usersubscription.FieldID),
  126. sqlgraph.Edge(sqlgraph.O2M, false, group.SubscriptionsTable, group.SubscriptionsColumn),
  127. )
  128. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  129. return fromU, nil
  130. }
  131. return query
  132. }
  133. // QueryUsageLogs chains the current query on the "usage_logs" edge.
  134. func (_q *GroupQuery) QueryUsageLogs() *UsageLogQuery {
  135. query := (&UsageLogClient{config: _q.config}).Query()
  136. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  137. if err := _q.prepareQuery(ctx); err != nil {
  138. return nil, err
  139. }
  140. selector := _q.sqlQuery(ctx)
  141. if err := selector.Err(); err != nil {
  142. return nil, err
  143. }
  144. step := sqlgraph.NewStep(
  145. sqlgraph.From(group.Table, group.FieldID, selector),
  146. sqlgraph.To(usagelog.Table, usagelog.FieldID),
  147. sqlgraph.Edge(sqlgraph.O2M, false, group.UsageLogsTable, group.UsageLogsColumn),
  148. )
  149. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  150. return fromU, nil
  151. }
  152. return query
  153. }
  154. // QueryAccounts chains the current query on the "accounts" edge.
  155. func (_q *GroupQuery) QueryAccounts() *AccountQuery {
  156. query := (&AccountClient{config: _q.config}).Query()
  157. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  158. if err := _q.prepareQuery(ctx); err != nil {
  159. return nil, err
  160. }
  161. selector := _q.sqlQuery(ctx)
  162. if err := selector.Err(); err != nil {
  163. return nil, err
  164. }
  165. step := sqlgraph.NewStep(
  166. sqlgraph.From(group.Table, group.FieldID, selector),
  167. sqlgraph.To(account.Table, account.FieldID),
  168. sqlgraph.Edge(sqlgraph.M2M, true, group.AccountsTable, group.AccountsPrimaryKey...),
  169. )
  170. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  171. return fromU, nil
  172. }
  173. return query
  174. }
  175. // QueryAllowedUsers chains the current query on the "allowed_users" edge.
  176. func (_q *GroupQuery) QueryAllowedUsers() *UserQuery {
  177. query := (&UserClient{config: _q.config}).Query()
  178. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  179. if err := _q.prepareQuery(ctx); err != nil {
  180. return nil, err
  181. }
  182. selector := _q.sqlQuery(ctx)
  183. if err := selector.Err(); err != nil {
  184. return nil, err
  185. }
  186. step := sqlgraph.NewStep(
  187. sqlgraph.From(group.Table, group.FieldID, selector),
  188. sqlgraph.To(user.Table, user.FieldID),
  189. sqlgraph.Edge(sqlgraph.M2M, true, group.AllowedUsersTable, group.AllowedUsersPrimaryKey...),
  190. )
  191. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  192. return fromU, nil
  193. }
  194. return query
  195. }
  196. // QueryAccountGroups chains the current query on the "account_groups" edge.
  197. func (_q *GroupQuery) QueryAccountGroups() *AccountGroupQuery {
  198. query := (&AccountGroupClient{config: _q.config}).Query()
  199. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  200. if err := _q.prepareQuery(ctx); err != nil {
  201. return nil, err
  202. }
  203. selector := _q.sqlQuery(ctx)
  204. if err := selector.Err(); err != nil {
  205. return nil, err
  206. }
  207. step := sqlgraph.NewStep(
  208. sqlgraph.From(group.Table, group.FieldID, selector),
  209. sqlgraph.To(accountgroup.Table, accountgroup.GroupColumn),
  210. sqlgraph.Edge(sqlgraph.O2M, true, group.AccountGroupsTable, group.AccountGroupsColumn),
  211. )
  212. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  213. return fromU, nil
  214. }
  215. return query
  216. }
  217. // QueryUserAllowedGroups chains the current query on the "user_allowed_groups" edge.
  218. func (_q *GroupQuery) QueryUserAllowedGroups() *UserAllowedGroupQuery {
  219. query := (&UserAllowedGroupClient{config: _q.config}).Query()
  220. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  221. if err := _q.prepareQuery(ctx); err != nil {
  222. return nil, err
  223. }
  224. selector := _q.sqlQuery(ctx)
  225. if err := selector.Err(); err != nil {
  226. return nil, err
  227. }
  228. step := sqlgraph.NewStep(
  229. sqlgraph.From(group.Table, group.FieldID, selector),
  230. sqlgraph.To(userallowedgroup.Table, userallowedgroup.GroupColumn),
  231. sqlgraph.Edge(sqlgraph.O2M, true, group.UserAllowedGroupsTable, group.UserAllowedGroupsColumn),
  232. )
  233. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  234. return fromU, nil
  235. }
  236. return query
  237. }
  238. // First returns the first Group entity from the query.
  239. // Returns a *NotFoundError when no Group was found.
  240. func (_q *GroupQuery) First(ctx context.Context) (*Group, error) {
  241. nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst))
  242. if err != nil {
  243. return nil, err
  244. }
  245. if len(nodes) == 0 {
  246. return nil, &NotFoundError{group.Label}
  247. }
  248. return nodes[0], nil
  249. }
  250. // FirstX is like First, but panics if an error occurs.
  251. func (_q *GroupQuery) FirstX(ctx context.Context) *Group {
  252. node, err := _q.First(ctx)
  253. if err != nil && !IsNotFound(err) {
  254. panic(err)
  255. }
  256. return node
  257. }
  258. // FirstID returns the first Group ID from the query.
  259. // Returns a *NotFoundError when no Group ID was found.
  260. func (_q *GroupQuery) FirstID(ctx context.Context) (id int64, err error) {
  261. var ids []int64
  262. if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil {
  263. return
  264. }
  265. if len(ids) == 0 {
  266. err = &NotFoundError{group.Label}
  267. return
  268. }
  269. return ids[0], nil
  270. }
  271. // FirstIDX is like FirstID, but panics if an error occurs.
  272. func (_q *GroupQuery) FirstIDX(ctx context.Context) int64 {
  273. id, err := _q.FirstID(ctx)
  274. if err != nil && !IsNotFound(err) {
  275. panic(err)
  276. }
  277. return id
  278. }
  279. // Only returns a single Group entity found by the query, ensuring it only returns one.
  280. // Returns a *NotSingularError when more than one Group entity is found.
  281. // Returns a *NotFoundError when no Group entities are found.
  282. func (_q *GroupQuery) Only(ctx context.Context) (*Group, error) {
  283. nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
  284. if err != nil {
  285. return nil, err
  286. }
  287. switch len(nodes) {
  288. case 1:
  289. return nodes[0], nil
  290. case 0:
  291. return nil, &NotFoundError{group.Label}
  292. default:
  293. return nil, &NotSingularError{group.Label}
  294. }
  295. }
  296. // OnlyX is like Only, but panics if an error occurs.
  297. func (_q *GroupQuery) OnlyX(ctx context.Context) *Group {
  298. node, err := _q.Only(ctx)
  299. if err != nil {
  300. panic(err)
  301. }
  302. return node
  303. }
  304. // OnlyID is like Only, but returns the only Group ID in the query.
  305. // Returns a *NotSingularError when more than one Group ID is found.
  306. // Returns a *NotFoundError when no entities are found.
  307. func (_q *GroupQuery) OnlyID(ctx context.Context) (id int64, err error) {
  308. var ids []int64
  309. if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil {
  310. return
  311. }
  312. switch len(ids) {
  313. case 1:
  314. id = ids[0]
  315. case 0:
  316. err = &NotFoundError{group.Label}
  317. default:
  318. err = &NotSingularError{group.Label}
  319. }
  320. return
  321. }
  322. // OnlyIDX is like OnlyID, but panics if an error occurs.
  323. func (_q *GroupQuery) OnlyIDX(ctx context.Context) int64 {
  324. id, err := _q.OnlyID(ctx)
  325. if err != nil {
  326. panic(err)
  327. }
  328. return id
  329. }
  330. // All executes the query and returns a list of Groups.
  331. func (_q *GroupQuery) All(ctx context.Context) ([]*Group, error) {
  332. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll)
  333. if err := _q.prepareQuery(ctx); err != nil {
  334. return nil, err
  335. }
  336. qr := querierAll[[]*Group, *GroupQuery]()
  337. return withInterceptors[[]*Group](ctx, _q, qr, _q.inters)
  338. }
  339. // AllX is like All, but panics if an error occurs.
  340. func (_q *GroupQuery) AllX(ctx context.Context) []*Group {
  341. nodes, err := _q.All(ctx)
  342. if err != nil {
  343. panic(err)
  344. }
  345. return nodes
  346. }
  347. // IDs executes the query and returns a list of Group IDs.
  348. func (_q *GroupQuery) IDs(ctx context.Context) (ids []int64, err error) {
  349. if _q.ctx.Unique == nil && _q.path != nil {
  350. _q.Unique(true)
  351. }
  352. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs)
  353. if err = _q.Select(group.FieldID).Scan(ctx, &ids); err != nil {
  354. return nil, err
  355. }
  356. return ids, nil
  357. }
  358. // IDsX is like IDs, but panics if an error occurs.
  359. func (_q *GroupQuery) IDsX(ctx context.Context) []int64 {
  360. ids, err := _q.IDs(ctx)
  361. if err != nil {
  362. panic(err)
  363. }
  364. return ids
  365. }
  366. // Count returns the count of the given query.
  367. func (_q *GroupQuery) Count(ctx context.Context) (int, error) {
  368. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount)
  369. if err := _q.prepareQuery(ctx); err != nil {
  370. return 0, err
  371. }
  372. return withInterceptors[int](ctx, _q, querierCount[*GroupQuery](), _q.inters)
  373. }
  374. // CountX is like Count, but panics if an error occurs.
  375. func (_q *GroupQuery) CountX(ctx context.Context) int {
  376. count, err := _q.Count(ctx)
  377. if err != nil {
  378. panic(err)
  379. }
  380. return count
  381. }
  382. // Exist returns true if the query has elements in the graph.
  383. func (_q *GroupQuery) Exist(ctx context.Context) (bool, error) {
  384. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist)
  385. switch _, err := _q.FirstID(ctx); {
  386. case IsNotFound(err):
  387. return false, nil
  388. case err != nil:
  389. return false, fmt.Errorf("ent: check existence: %w", err)
  390. default:
  391. return true, nil
  392. }
  393. }
  394. // ExistX is like Exist, but panics if an error occurs.
  395. func (_q *GroupQuery) ExistX(ctx context.Context) bool {
  396. exist, err := _q.Exist(ctx)
  397. if err != nil {
  398. panic(err)
  399. }
  400. return exist
  401. }
  402. // Clone returns a duplicate of the GroupQuery builder, including all associated steps. It can be
  403. // used to prepare common query builders and use them differently after the clone is made.
  404. func (_q *GroupQuery) Clone() *GroupQuery {
  405. if _q == nil {
  406. return nil
  407. }
  408. return &GroupQuery{
  409. config: _q.config,
  410. ctx: _q.ctx.Clone(),
  411. order: append([]group.OrderOption{}, _q.order...),
  412. inters: append([]Interceptor{}, _q.inters...),
  413. predicates: append([]predicate.Group{}, _q.predicates...),
  414. withAPIKeys: _q.withAPIKeys.Clone(),
  415. withRedeemCodes: _q.withRedeemCodes.Clone(),
  416. withSubscriptions: _q.withSubscriptions.Clone(),
  417. withUsageLogs: _q.withUsageLogs.Clone(),
  418. withAccounts: _q.withAccounts.Clone(),
  419. withAllowedUsers: _q.withAllowedUsers.Clone(),
  420. withAccountGroups: _q.withAccountGroups.Clone(),
  421. withUserAllowedGroups: _q.withUserAllowedGroups.Clone(),
  422. // clone intermediate query.
  423. sql: _q.sql.Clone(),
  424. path: _q.path,
  425. }
  426. }
  427. // WithAPIKeys tells the query-builder to eager-load the nodes that are connected to
  428. // the "api_keys" edge. The optional arguments are used to configure the query builder of the edge.
  429. func (_q *GroupQuery) WithAPIKeys(opts ...func(*APIKeyQuery)) *GroupQuery {
  430. query := (&APIKeyClient{config: _q.config}).Query()
  431. for _, opt := range opts {
  432. opt(query)
  433. }
  434. _q.withAPIKeys = query
  435. return _q
  436. }
  437. // WithRedeemCodes tells the query-builder to eager-load the nodes that are connected to
  438. // the "redeem_codes" edge. The optional arguments are used to configure the query builder of the edge.
  439. func (_q *GroupQuery) WithRedeemCodes(opts ...func(*RedeemCodeQuery)) *GroupQuery {
  440. query := (&RedeemCodeClient{config: _q.config}).Query()
  441. for _, opt := range opts {
  442. opt(query)
  443. }
  444. _q.withRedeemCodes = query
  445. return _q
  446. }
  447. // WithSubscriptions tells the query-builder to eager-load the nodes that are connected to
  448. // the "subscriptions" edge. The optional arguments are used to configure the query builder of the edge.
  449. func (_q *GroupQuery) WithSubscriptions(opts ...func(*UserSubscriptionQuery)) *GroupQuery {
  450. query := (&UserSubscriptionClient{config: _q.config}).Query()
  451. for _, opt := range opts {
  452. opt(query)
  453. }
  454. _q.withSubscriptions = query
  455. return _q
  456. }
  457. // WithUsageLogs tells the query-builder to eager-load the nodes that are connected to
  458. // the "usage_logs" edge. The optional arguments are used to configure the query builder of the edge.
  459. func (_q *GroupQuery) WithUsageLogs(opts ...func(*UsageLogQuery)) *GroupQuery {
  460. query := (&UsageLogClient{config: _q.config}).Query()
  461. for _, opt := range opts {
  462. opt(query)
  463. }
  464. _q.withUsageLogs = query
  465. return _q
  466. }
  467. // WithAccounts tells the query-builder to eager-load the nodes that are connected to
  468. // the "accounts" edge. The optional arguments are used to configure the query builder of the edge.
  469. func (_q *GroupQuery) WithAccounts(opts ...func(*AccountQuery)) *GroupQuery {
  470. query := (&AccountClient{config: _q.config}).Query()
  471. for _, opt := range opts {
  472. opt(query)
  473. }
  474. _q.withAccounts = query
  475. return _q
  476. }
  477. // WithAllowedUsers tells the query-builder to eager-load the nodes that are connected to
  478. // the "allowed_users" edge. The optional arguments are used to configure the query builder of the edge.
  479. func (_q *GroupQuery) WithAllowedUsers(opts ...func(*UserQuery)) *GroupQuery {
  480. query := (&UserClient{config: _q.config}).Query()
  481. for _, opt := range opts {
  482. opt(query)
  483. }
  484. _q.withAllowedUsers = query
  485. return _q
  486. }
  487. // WithAccountGroups tells the query-builder to eager-load the nodes that are connected to
  488. // the "account_groups" edge. The optional arguments are used to configure the query builder of the edge.
  489. func (_q *GroupQuery) WithAccountGroups(opts ...func(*AccountGroupQuery)) *GroupQuery {
  490. query := (&AccountGroupClient{config: _q.config}).Query()
  491. for _, opt := range opts {
  492. opt(query)
  493. }
  494. _q.withAccountGroups = query
  495. return _q
  496. }
  497. // WithUserAllowedGroups tells the query-builder to eager-load the nodes that are connected to
  498. // the "user_allowed_groups" edge. The optional arguments are used to configure the query builder of the edge.
  499. func (_q *GroupQuery) WithUserAllowedGroups(opts ...func(*UserAllowedGroupQuery)) *GroupQuery {
  500. query := (&UserAllowedGroupClient{config: _q.config}).Query()
  501. for _, opt := range opts {
  502. opt(query)
  503. }
  504. _q.withUserAllowedGroups = query
  505. return _q
  506. }
  507. // GroupBy is used to group vertices by one or more fields/columns.
  508. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  509. //
  510. // Example:
  511. //
  512. // var v []struct {
  513. // CreatedAt time.Time `json:"created_at,omitempty"`
  514. // Count int `json:"count,omitempty"`
  515. // }
  516. //
  517. // client.Group.Query().
  518. // GroupBy(group.FieldCreatedAt).
  519. // Aggregate(ent.Count()).
  520. // Scan(ctx, &v)
  521. func (_q *GroupQuery) GroupBy(field string, fields ...string) *GroupGroupBy {
  522. _q.ctx.Fields = append([]string{field}, fields...)
  523. grbuild := &GroupGroupBy{build: _q}
  524. grbuild.flds = &_q.ctx.Fields
  525. grbuild.label = group.Label
  526. grbuild.scan = grbuild.Scan
  527. return grbuild
  528. }
  529. // Select allows the selection one or more fields/columns for the given query,
  530. // instead of selecting all fields in the entity.
  531. //
  532. // Example:
  533. //
  534. // var v []struct {
  535. // CreatedAt time.Time `json:"created_at,omitempty"`
  536. // }
  537. //
  538. // client.Group.Query().
  539. // Select(group.FieldCreatedAt).
  540. // Scan(ctx, &v)
  541. func (_q *GroupQuery) Select(fields ...string) *GroupSelect {
  542. _q.ctx.Fields = append(_q.ctx.Fields, fields...)
  543. sbuild := &GroupSelect{GroupQuery: _q}
  544. sbuild.label = group.Label
  545. sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan
  546. return sbuild
  547. }
  548. // Aggregate returns a GroupSelect configured with the given aggregations.
  549. func (_q *GroupQuery) Aggregate(fns ...AggregateFunc) *GroupSelect {
  550. return _q.Select().Aggregate(fns...)
  551. }
  552. func (_q *GroupQuery) prepareQuery(ctx context.Context) error {
  553. for _, inter := range _q.inters {
  554. if inter == nil {
  555. return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
  556. }
  557. if trv, ok := inter.(Traverser); ok {
  558. if err := trv.Traverse(ctx, _q); err != nil {
  559. return err
  560. }
  561. }
  562. }
  563. for _, f := range _q.ctx.Fields {
  564. if !group.ValidColumn(f) {
  565. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  566. }
  567. }
  568. if _q.path != nil {
  569. prev, err := _q.path(ctx)
  570. if err != nil {
  571. return err
  572. }
  573. _q.sql = prev
  574. }
  575. return nil
  576. }
  577. func (_q *GroupQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Group, error) {
  578. var (
  579. nodes = []*Group{}
  580. _spec = _q.querySpec()
  581. loadedTypes = [8]bool{
  582. _q.withAPIKeys != nil,
  583. _q.withRedeemCodes != nil,
  584. _q.withSubscriptions != nil,
  585. _q.withUsageLogs != nil,
  586. _q.withAccounts != nil,
  587. _q.withAllowedUsers != nil,
  588. _q.withAccountGroups != nil,
  589. _q.withUserAllowedGroups != nil,
  590. }
  591. )
  592. _spec.ScanValues = func(columns []string) ([]any, error) {
  593. return (*Group).scanValues(nil, columns)
  594. }
  595. _spec.Assign = func(columns []string, values []any) error {
  596. node := &Group{config: _q.config}
  597. nodes = append(nodes, node)
  598. node.Edges.loadedTypes = loadedTypes
  599. return node.assignValues(columns, values)
  600. }
  601. if len(_q.modifiers) > 0 {
  602. _spec.Modifiers = _q.modifiers
  603. }
  604. for i := range hooks {
  605. hooks[i](ctx, _spec)
  606. }
  607. if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil {
  608. return nil, err
  609. }
  610. if len(nodes) == 0 {
  611. return nodes, nil
  612. }
  613. if query := _q.withAPIKeys; query != nil {
  614. if err := _q.loadAPIKeys(ctx, query, nodes,
  615. func(n *Group) { n.Edges.APIKeys = []*APIKey{} },
  616. func(n *Group, e *APIKey) { n.Edges.APIKeys = append(n.Edges.APIKeys, e) }); err != nil {
  617. return nil, err
  618. }
  619. }
  620. if query := _q.withRedeemCodes; query != nil {
  621. if err := _q.loadRedeemCodes(ctx, query, nodes,
  622. func(n *Group) { n.Edges.RedeemCodes = []*RedeemCode{} },
  623. func(n *Group, e *RedeemCode) { n.Edges.RedeemCodes = append(n.Edges.RedeemCodes, e) }); err != nil {
  624. return nil, err
  625. }
  626. }
  627. if query := _q.withSubscriptions; query != nil {
  628. if err := _q.loadSubscriptions(ctx, query, nodes,
  629. func(n *Group) { n.Edges.Subscriptions = []*UserSubscription{} },
  630. func(n *Group, e *UserSubscription) { n.Edges.Subscriptions = append(n.Edges.Subscriptions, e) }); err != nil {
  631. return nil, err
  632. }
  633. }
  634. if query := _q.withUsageLogs; query != nil {
  635. if err := _q.loadUsageLogs(ctx, query, nodes,
  636. func(n *Group) { n.Edges.UsageLogs = []*UsageLog{} },
  637. func(n *Group, e *UsageLog) { n.Edges.UsageLogs = append(n.Edges.UsageLogs, e) }); err != nil {
  638. return nil, err
  639. }
  640. }
  641. if query := _q.withAccounts; query != nil {
  642. if err := _q.loadAccounts(ctx, query, nodes,
  643. func(n *Group) { n.Edges.Accounts = []*Account{} },
  644. func(n *Group, e *Account) { n.Edges.Accounts = append(n.Edges.Accounts, e) }); err != nil {
  645. return nil, err
  646. }
  647. }
  648. if query := _q.withAllowedUsers; query != nil {
  649. if err := _q.loadAllowedUsers(ctx, query, nodes,
  650. func(n *Group) { n.Edges.AllowedUsers = []*User{} },
  651. func(n *Group, e *User) { n.Edges.AllowedUsers = append(n.Edges.AllowedUsers, e) }); err != nil {
  652. return nil, err
  653. }
  654. }
  655. if query := _q.withAccountGroups; query != nil {
  656. if err := _q.loadAccountGroups(ctx, query, nodes,
  657. func(n *Group) { n.Edges.AccountGroups = []*AccountGroup{} },
  658. func(n *Group, e *AccountGroup) { n.Edges.AccountGroups = append(n.Edges.AccountGroups, e) }); err != nil {
  659. return nil, err
  660. }
  661. }
  662. if query := _q.withUserAllowedGroups; query != nil {
  663. if err := _q.loadUserAllowedGroups(ctx, query, nodes,
  664. func(n *Group) { n.Edges.UserAllowedGroups = []*UserAllowedGroup{} },
  665. func(n *Group, e *UserAllowedGroup) { n.Edges.UserAllowedGroups = append(n.Edges.UserAllowedGroups, e) }); err != nil {
  666. return nil, err
  667. }
  668. }
  669. return nodes, nil
  670. }
  671. func (_q *GroupQuery) loadAPIKeys(ctx context.Context, query *APIKeyQuery, nodes []*Group, init func(*Group), assign func(*Group, *APIKey)) error {
  672. fks := make([]driver.Value, 0, len(nodes))
  673. nodeids := make(map[int64]*Group)
  674. for i := range nodes {
  675. fks = append(fks, nodes[i].ID)
  676. nodeids[nodes[i].ID] = nodes[i]
  677. if init != nil {
  678. init(nodes[i])
  679. }
  680. }
  681. if len(query.ctx.Fields) > 0 {
  682. query.ctx.AppendFieldOnce(apikey.FieldGroupID)
  683. }
  684. query.Where(predicate.APIKey(func(s *sql.Selector) {
  685. s.Where(sql.InValues(s.C(group.APIKeysColumn), fks...))
  686. }))
  687. neighbors, err := query.All(ctx)
  688. if err != nil {
  689. return err
  690. }
  691. for _, n := range neighbors {
  692. fk := n.GroupID
  693. if fk == nil {
  694. return fmt.Errorf(`foreign-key "group_id" is nil for node %v`, n.ID)
  695. }
  696. node, ok := nodeids[*fk]
  697. if !ok {
  698. return fmt.Errorf(`unexpected referenced foreign-key "group_id" returned %v for node %v`, *fk, n.ID)
  699. }
  700. assign(node, n)
  701. }
  702. return nil
  703. }
  704. func (_q *GroupQuery) loadRedeemCodes(ctx context.Context, query *RedeemCodeQuery, nodes []*Group, init func(*Group), assign func(*Group, *RedeemCode)) error {
  705. fks := make([]driver.Value, 0, len(nodes))
  706. nodeids := make(map[int64]*Group)
  707. for i := range nodes {
  708. fks = append(fks, nodes[i].ID)
  709. nodeids[nodes[i].ID] = nodes[i]
  710. if init != nil {
  711. init(nodes[i])
  712. }
  713. }
  714. if len(query.ctx.Fields) > 0 {
  715. query.ctx.AppendFieldOnce(redeemcode.FieldGroupID)
  716. }
  717. query.Where(predicate.RedeemCode(func(s *sql.Selector) {
  718. s.Where(sql.InValues(s.C(group.RedeemCodesColumn), fks...))
  719. }))
  720. neighbors, err := query.All(ctx)
  721. if err != nil {
  722. return err
  723. }
  724. for _, n := range neighbors {
  725. fk := n.GroupID
  726. if fk == nil {
  727. return fmt.Errorf(`foreign-key "group_id" is nil for node %v`, n.ID)
  728. }
  729. node, ok := nodeids[*fk]
  730. if !ok {
  731. return fmt.Errorf(`unexpected referenced foreign-key "group_id" returned %v for node %v`, *fk, n.ID)
  732. }
  733. assign(node, n)
  734. }
  735. return nil
  736. }
  737. func (_q *GroupQuery) loadSubscriptions(ctx context.Context, query *UserSubscriptionQuery, nodes []*Group, init func(*Group), assign func(*Group, *UserSubscription)) error {
  738. fks := make([]driver.Value, 0, len(nodes))
  739. nodeids := make(map[int64]*Group)
  740. for i := range nodes {
  741. fks = append(fks, nodes[i].ID)
  742. nodeids[nodes[i].ID] = nodes[i]
  743. if init != nil {
  744. init(nodes[i])
  745. }
  746. }
  747. if len(query.ctx.Fields) > 0 {
  748. query.ctx.AppendFieldOnce(usersubscription.FieldGroupID)
  749. }
  750. query.Where(predicate.UserSubscription(func(s *sql.Selector) {
  751. s.Where(sql.InValues(s.C(group.SubscriptionsColumn), fks...))
  752. }))
  753. neighbors, err := query.All(ctx)
  754. if err != nil {
  755. return err
  756. }
  757. for _, n := range neighbors {
  758. fk := n.GroupID
  759. node, ok := nodeids[fk]
  760. if !ok {
  761. return fmt.Errorf(`unexpected referenced foreign-key "group_id" returned %v for node %v`, fk, n.ID)
  762. }
  763. assign(node, n)
  764. }
  765. return nil
  766. }
  767. func (_q *GroupQuery) loadUsageLogs(ctx context.Context, query *UsageLogQuery, nodes []*Group, init func(*Group), assign func(*Group, *UsageLog)) error {
  768. fks := make([]driver.Value, 0, len(nodes))
  769. nodeids := make(map[int64]*Group)
  770. for i := range nodes {
  771. fks = append(fks, nodes[i].ID)
  772. nodeids[nodes[i].ID] = nodes[i]
  773. if init != nil {
  774. init(nodes[i])
  775. }
  776. }
  777. if len(query.ctx.Fields) > 0 {
  778. query.ctx.AppendFieldOnce(usagelog.FieldGroupID)
  779. }
  780. query.Where(predicate.UsageLog(func(s *sql.Selector) {
  781. s.Where(sql.InValues(s.C(group.UsageLogsColumn), fks...))
  782. }))
  783. neighbors, err := query.All(ctx)
  784. if err != nil {
  785. return err
  786. }
  787. for _, n := range neighbors {
  788. fk := n.GroupID
  789. if fk == nil {
  790. return fmt.Errorf(`foreign-key "group_id" is nil for node %v`, n.ID)
  791. }
  792. node, ok := nodeids[*fk]
  793. if !ok {
  794. return fmt.Errorf(`unexpected referenced foreign-key "group_id" returned %v for node %v`, *fk, n.ID)
  795. }
  796. assign(node, n)
  797. }
  798. return nil
  799. }
  800. func (_q *GroupQuery) loadAccounts(ctx context.Context, query *AccountQuery, nodes []*Group, init func(*Group), assign func(*Group, *Account)) error {
  801. edgeIDs := make([]driver.Value, len(nodes))
  802. byID := make(map[int64]*Group)
  803. nids := make(map[int64]map[*Group]struct{})
  804. for i, node := range nodes {
  805. edgeIDs[i] = node.ID
  806. byID[node.ID] = node
  807. if init != nil {
  808. init(node)
  809. }
  810. }
  811. query.Where(func(s *sql.Selector) {
  812. joinT := sql.Table(group.AccountsTable)
  813. s.Join(joinT).On(s.C(account.FieldID), joinT.C(group.AccountsPrimaryKey[0]))
  814. s.Where(sql.InValues(joinT.C(group.AccountsPrimaryKey[1]), edgeIDs...))
  815. columns := s.SelectedColumns()
  816. s.Select(joinT.C(group.AccountsPrimaryKey[1]))
  817. s.AppendSelect(columns...)
  818. s.SetDistinct(false)
  819. })
  820. if err := query.prepareQuery(ctx); err != nil {
  821. return err
  822. }
  823. qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
  824. return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
  825. assign := spec.Assign
  826. values := spec.ScanValues
  827. spec.ScanValues = func(columns []string) ([]any, error) {
  828. values, err := values(columns[1:])
  829. if err != nil {
  830. return nil, err
  831. }
  832. return append([]any{new(sql.NullInt64)}, values...), nil
  833. }
  834. spec.Assign = func(columns []string, values []any) error {
  835. outValue := values[0].(*sql.NullInt64).Int64
  836. inValue := values[1].(*sql.NullInt64).Int64
  837. if nids[inValue] == nil {
  838. nids[inValue] = map[*Group]struct{}{byID[outValue]: {}}
  839. return assign(columns[1:], values[1:])
  840. }
  841. nids[inValue][byID[outValue]] = struct{}{}
  842. return nil
  843. }
  844. })
  845. })
  846. neighbors, err := withInterceptors[[]*Account](ctx, query, qr, query.inters)
  847. if err != nil {
  848. return err
  849. }
  850. for _, n := range neighbors {
  851. nodes, ok := nids[n.ID]
  852. if !ok {
  853. return fmt.Errorf(`unexpected "accounts" node returned %v`, n.ID)
  854. }
  855. for kn := range nodes {
  856. assign(kn, n)
  857. }
  858. }
  859. return nil
  860. }
  861. func (_q *GroupQuery) loadAllowedUsers(ctx context.Context, query *UserQuery, nodes []*Group, init func(*Group), assign func(*Group, *User)) error {
  862. edgeIDs := make([]driver.Value, len(nodes))
  863. byID := make(map[int64]*Group)
  864. nids := make(map[int64]map[*Group]struct{})
  865. for i, node := range nodes {
  866. edgeIDs[i] = node.ID
  867. byID[node.ID] = node
  868. if init != nil {
  869. init(node)
  870. }
  871. }
  872. query.Where(func(s *sql.Selector) {
  873. joinT := sql.Table(group.AllowedUsersTable)
  874. s.Join(joinT).On(s.C(user.FieldID), joinT.C(group.AllowedUsersPrimaryKey[0]))
  875. s.Where(sql.InValues(joinT.C(group.AllowedUsersPrimaryKey[1]), edgeIDs...))
  876. columns := s.SelectedColumns()
  877. s.Select(joinT.C(group.AllowedUsersPrimaryKey[1]))
  878. s.AppendSelect(columns...)
  879. s.SetDistinct(false)
  880. })
  881. if err := query.prepareQuery(ctx); err != nil {
  882. return err
  883. }
  884. qr := QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
  885. return query.sqlAll(ctx, func(_ context.Context, spec *sqlgraph.QuerySpec) {
  886. assign := spec.Assign
  887. values := spec.ScanValues
  888. spec.ScanValues = func(columns []string) ([]any, error) {
  889. values, err := values(columns[1:])
  890. if err != nil {
  891. return nil, err
  892. }
  893. return append([]any{new(sql.NullInt64)}, values...), nil
  894. }
  895. spec.Assign = func(columns []string, values []any) error {
  896. outValue := values[0].(*sql.NullInt64).Int64
  897. inValue := values[1].(*sql.NullInt64).Int64
  898. if nids[inValue] == nil {
  899. nids[inValue] = map[*Group]struct{}{byID[outValue]: {}}
  900. return assign(columns[1:], values[1:])
  901. }
  902. nids[inValue][byID[outValue]] = struct{}{}
  903. return nil
  904. }
  905. })
  906. })
  907. neighbors, err := withInterceptors[[]*User](ctx, query, qr, query.inters)
  908. if err != nil {
  909. return err
  910. }
  911. for _, n := range neighbors {
  912. nodes, ok := nids[n.ID]
  913. if !ok {
  914. return fmt.Errorf(`unexpected "allowed_users" node returned %v`, n.ID)
  915. }
  916. for kn := range nodes {
  917. assign(kn, n)
  918. }
  919. }
  920. return nil
  921. }
  922. func (_q *GroupQuery) loadAccountGroups(ctx context.Context, query *AccountGroupQuery, nodes []*Group, init func(*Group), assign func(*Group, *AccountGroup)) error {
  923. fks := make([]driver.Value, 0, len(nodes))
  924. nodeids := make(map[int64]*Group)
  925. for i := range nodes {
  926. fks = append(fks, nodes[i].ID)
  927. nodeids[nodes[i].ID] = nodes[i]
  928. if init != nil {
  929. init(nodes[i])
  930. }
  931. }
  932. if len(query.ctx.Fields) > 0 {
  933. query.ctx.AppendFieldOnce(accountgroup.FieldGroupID)
  934. }
  935. query.Where(predicate.AccountGroup(func(s *sql.Selector) {
  936. s.Where(sql.InValues(s.C(group.AccountGroupsColumn), fks...))
  937. }))
  938. neighbors, err := query.All(ctx)
  939. if err != nil {
  940. return err
  941. }
  942. for _, n := range neighbors {
  943. fk := n.GroupID
  944. node, ok := nodeids[fk]
  945. if !ok {
  946. return fmt.Errorf(`unexpected referenced foreign-key "group_id" returned %v for node %v`, fk, n)
  947. }
  948. assign(node, n)
  949. }
  950. return nil
  951. }
  952. func (_q *GroupQuery) loadUserAllowedGroups(ctx context.Context, query *UserAllowedGroupQuery, nodes []*Group, init func(*Group), assign func(*Group, *UserAllowedGroup)) error {
  953. fks := make([]driver.Value, 0, len(nodes))
  954. nodeids := make(map[int64]*Group)
  955. for i := range nodes {
  956. fks = append(fks, nodes[i].ID)
  957. nodeids[nodes[i].ID] = nodes[i]
  958. if init != nil {
  959. init(nodes[i])
  960. }
  961. }
  962. if len(query.ctx.Fields) > 0 {
  963. query.ctx.AppendFieldOnce(userallowedgroup.FieldGroupID)
  964. }
  965. query.Where(predicate.UserAllowedGroup(func(s *sql.Selector) {
  966. s.Where(sql.InValues(s.C(group.UserAllowedGroupsColumn), fks...))
  967. }))
  968. neighbors, err := query.All(ctx)
  969. if err != nil {
  970. return err
  971. }
  972. for _, n := range neighbors {
  973. fk := n.GroupID
  974. node, ok := nodeids[fk]
  975. if !ok {
  976. return fmt.Errorf(`unexpected referenced foreign-key "group_id" returned %v for node %v`, fk, n)
  977. }
  978. assign(node, n)
  979. }
  980. return nil
  981. }
  982. func (_q *GroupQuery) sqlCount(ctx context.Context) (int, error) {
  983. _spec := _q.querySpec()
  984. if len(_q.modifiers) > 0 {
  985. _spec.Modifiers = _q.modifiers
  986. }
  987. _spec.Node.Columns = _q.ctx.Fields
  988. if len(_q.ctx.Fields) > 0 {
  989. _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique
  990. }
  991. return sqlgraph.CountNodes(ctx, _q.driver, _spec)
  992. }
  993. func (_q *GroupQuery) querySpec() *sqlgraph.QuerySpec {
  994. _spec := sqlgraph.NewQuerySpec(group.Table, group.Columns, sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64))
  995. _spec.From = _q.sql
  996. if unique := _q.ctx.Unique; unique != nil {
  997. _spec.Unique = *unique
  998. } else if _q.path != nil {
  999. _spec.Unique = true
  1000. }
  1001. if fields := _q.ctx.Fields; len(fields) > 0 {
  1002. _spec.Node.Columns = make([]string, 0, len(fields))
  1003. _spec.Node.Columns = append(_spec.Node.Columns, group.FieldID)
  1004. for i := range fields {
  1005. if fields[i] != group.FieldID {
  1006. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  1007. }
  1008. }
  1009. }
  1010. if ps := _q.predicates; len(ps) > 0 {
  1011. _spec.Predicate = func(selector *sql.Selector) {
  1012. for i := range ps {
  1013. ps[i](selector)
  1014. }
  1015. }
  1016. }
  1017. if limit := _q.ctx.Limit; limit != nil {
  1018. _spec.Limit = *limit
  1019. }
  1020. if offset := _q.ctx.Offset; offset != nil {
  1021. _spec.Offset = *offset
  1022. }
  1023. if ps := _q.order; len(ps) > 0 {
  1024. _spec.Order = func(selector *sql.Selector) {
  1025. for i := range ps {
  1026. ps[i](selector)
  1027. }
  1028. }
  1029. }
  1030. return _spec
  1031. }
  1032. func (_q *GroupQuery) sqlQuery(ctx context.Context) *sql.Selector {
  1033. builder := sql.Dialect(_q.driver.Dialect())
  1034. t1 := builder.Table(group.Table)
  1035. columns := _q.ctx.Fields
  1036. if len(columns) == 0 {
  1037. columns = group.Columns
  1038. }
  1039. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  1040. if _q.sql != nil {
  1041. selector = _q.sql
  1042. selector.Select(selector.Columns(columns...)...)
  1043. }
  1044. if _q.ctx.Unique != nil && *_q.ctx.Unique {
  1045. selector.Distinct()
  1046. }
  1047. for _, m := range _q.modifiers {
  1048. m(selector)
  1049. }
  1050. for _, p := range _q.predicates {
  1051. p(selector)
  1052. }
  1053. for _, p := range _q.order {
  1054. p(selector)
  1055. }
  1056. if offset := _q.ctx.Offset; offset != nil {
  1057. // limit is mandatory for offset clause. We start
  1058. // with default value, and override it below if needed.
  1059. selector.Offset(*offset).Limit(math.MaxInt32)
  1060. }
  1061. if limit := _q.ctx.Limit; limit != nil {
  1062. selector.Limit(*limit)
  1063. }
  1064. return selector
  1065. }
  1066. // ForUpdate locks the selected rows against concurrent updates, and prevent them from being
  1067. // updated, deleted or "selected ... for update" by other sessions, until the transaction is
  1068. // either committed or rolled-back.
  1069. func (_q *GroupQuery) ForUpdate(opts ...sql.LockOption) *GroupQuery {
  1070. if _q.driver.Dialect() == dialect.Postgres {
  1071. _q.Unique(false)
  1072. }
  1073. _q.modifiers = append(_q.modifiers, func(s *sql.Selector) {
  1074. s.ForUpdate(opts...)
  1075. })
  1076. return _q
  1077. }
  1078. // ForShare behaves similarly to ForUpdate, except that it acquires a shared mode lock
  1079. // on any rows that are read. Other sessions can read the rows, but cannot modify them
  1080. // until your transaction commits.
  1081. func (_q *GroupQuery) ForShare(opts ...sql.LockOption) *GroupQuery {
  1082. if _q.driver.Dialect() == dialect.Postgres {
  1083. _q.Unique(false)
  1084. }
  1085. _q.modifiers = append(_q.modifiers, func(s *sql.Selector) {
  1086. s.ForShare(opts...)
  1087. })
  1088. return _q
  1089. }
  1090. // GroupGroupBy is the group-by builder for Group entities.
  1091. type GroupGroupBy struct {
  1092. selector
  1093. build *GroupQuery
  1094. }
  1095. // Aggregate adds the given aggregation functions to the group-by query.
  1096. func (_g *GroupGroupBy) Aggregate(fns ...AggregateFunc) *GroupGroupBy {
  1097. _g.fns = append(_g.fns, fns...)
  1098. return _g
  1099. }
  1100. // Scan applies the selector query and scans the result into the given value.
  1101. func (_g *GroupGroupBy) Scan(ctx context.Context, v any) error {
  1102. ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy)
  1103. if err := _g.build.prepareQuery(ctx); err != nil {
  1104. return err
  1105. }
  1106. return scanWithInterceptors[*GroupQuery, *GroupGroupBy](ctx, _g.build, _g, _g.build.inters, v)
  1107. }
  1108. func (_g *GroupGroupBy) sqlScan(ctx context.Context, root *GroupQuery, v any) error {
  1109. selector := root.sqlQuery(ctx).Select()
  1110. aggregation := make([]string, 0, len(_g.fns))
  1111. for _, fn := range _g.fns {
  1112. aggregation = append(aggregation, fn(selector))
  1113. }
  1114. if len(selector.SelectedColumns()) == 0 {
  1115. columns := make([]string, 0, len(*_g.flds)+len(_g.fns))
  1116. for _, f := range *_g.flds {
  1117. columns = append(columns, selector.C(f))
  1118. }
  1119. columns = append(columns, aggregation...)
  1120. selector.Select(columns...)
  1121. }
  1122. selector.GroupBy(selector.Columns(*_g.flds...)...)
  1123. if err := selector.Err(); err != nil {
  1124. return err
  1125. }
  1126. rows := &sql.Rows{}
  1127. query, args := selector.Query()
  1128. if err := _g.build.driver.Query(ctx, query, args, rows); err != nil {
  1129. return err
  1130. }
  1131. defer rows.Close()
  1132. return sql.ScanSlice(rows, v)
  1133. }
  1134. // GroupSelect is the builder for selecting fields of Group entities.
  1135. type GroupSelect struct {
  1136. *GroupQuery
  1137. selector
  1138. }
  1139. // Aggregate adds the given aggregation functions to the selector query.
  1140. func (_s *GroupSelect) Aggregate(fns ...AggregateFunc) *GroupSelect {
  1141. _s.fns = append(_s.fns, fns...)
  1142. return _s
  1143. }
  1144. // Scan applies the selector query and scans the result into the given value.
  1145. func (_s *GroupSelect) Scan(ctx context.Context, v any) error {
  1146. ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect)
  1147. if err := _s.prepareQuery(ctx); err != nil {
  1148. return err
  1149. }
  1150. return scanWithInterceptors[*GroupQuery, *GroupSelect](ctx, _s.GroupQuery, _s, _s.inters, v)
  1151. }
  1152. func (_s *GroupSelect) sqlScan(ctx context.Context, root *GroupQuery, v any) error {
  1153. selector := root.sqlQuery(ctx)
  1154. aggregation := make([]string, 0, len(_s.fns))
  1155. for _, fn := range _s.fns {
  1156. aggregation = append(aggregation, fn(selector))
  1157. }
  1158. switch n := len(*_s.selector.flds); {
  1159. case n == 0 && len(aggregation) > 0:
  1160. selector.Select(aggregation...)
  1161. case n != 0 && len(aggregation) > 0:
  1162. selector.AppendSelect(aggregation...)
  1163. }
  1164. rows := &sql.Rows{}
  1165. query, args := selector.Query()
  1166. if err := _s.driver.Query(ctx, query, args, rows); err != nil {
  1167. return err
  1168. }
  1169. defer rows.Close()
  1170. return sql.ScanSlice(rows, v)
  1171. }