pendingauthsession_query.go 22 KB

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