usagelog_query.go 27 KB

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