account_query.go 31 KB

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