apikey_query.go 22 KB

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