token_query.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "math"
  7. "wechat-api/ent/agent"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/ent/token"
  10. "entgo.io/ent/dialect/sql"
  11. "entgo.io/ent/dialect/sql/sqlgraph"
  12. "entgo.io/ent/schema/field"
  13. )
  14. // TokenQuery is the builder for querying Token entities.
  15. type TokenQuery struct {
  16. config
  17. ctx *QueryContext
  18. order []token.OrderOption
  19. inters []Interceptor
  20. predicates []predicate.Token
  21. withAgent *AgentQuery
  22. // intermediate query (i.e. traversal path).
  23. sql *sql.Selector
  24. path func(context.Context) (*sql.Selector, error)
  25. }
  26. // Where adds a new predicate for the TokenQuery builder.
  27. func (tq *TokenQuery) Where(ps ...predicate.Token) *TokenQuery {
  28. tq.predicates = append(tq.predicates, ps...)
  29. return tq
  30. }
  31. // Limit the number of records to be returned by this query.
  32. func (tq *TokenQuery) Limit(limit int) *TokenQuery {
  33. tq.ctx.Limit = &limit
  34. return tq
  35. }
  36. // Offset to start from.
  37. func (tq *TokenQuery) Offset(offset int) *TokenQuery {
  38. tq.ctx.Offset = &offset
  39. return tq
  40. }
  41. // Unique configures the query builder to filter duplicate records on query.
  42. // By default, unique is set to true, and can be disabled using this method.
  43. func (tq *TokenQuery) Unique(unique bool) *TokenQuery {
  44. tq.ctx.Unique = &unique
  45. return tq
  46. }
  47. // Order specifies how the records should be ordered.
  48. func (tq *TokenQuery) Order(o ...token.OrderOption) *TokenQuery {
  49. tq.order = append(tq.order, o...)
  50. return tq
  51. }
  52. // QueryAgent chains the current query on the "agent" edge.
  53. func (tq *TokenQuery) QueryAgent() *AgentQuery {
  54. query := (&AgentClient{config: tq.config}).Query()
  55. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  56. if err := tq.prepareQuery(ctx); err != nil {
  57. return nil, err
  58. }
  59. selector := tq.sqlQuery(ctx)
  60. if err := selector.Err(); err != nil {
  61. return nil, err
  62. }
  63. step := sqlgraph.NewStep(
  64. sqlgraph.From(token.Table, token.FieldID, selector),
  65. sqlgraph.To(agent.Table, agent.FieldID),
  66. sqlgraph.Edge(sqlgraph.M2O, true, token.AgentTable, token.AgentColumn),
  67. )
  68. fromU = sqlgraph.SetNeighbors(tq.driver.Dialect(), step)
  69. return fromU, nil
  70. }
  71. return query
  72. }
  73. // First returns the first Token entity from the query.
  74. // Returns a *NotFoundError when no Token was found.
  75. func (tq *TokenQuery) First(ctx context.Context) (*Token, error) {
  76. nodes, err := tq.Limit(1).All(setContextOp(ctx, tq.ctx, "First"))
  77. if err != nil {
  78. return nil, err
  79. }
  80. if len(nodes) == 0 {
  81. return nil, &NotFoundError{token.Label}
  82. }
  83. return nodes[0], nil
  84. }
  85. // FirstX is like First, but panics if an error occurs.
  86. func (tq *TokenQuery) FirstX(ctx context.Context) *Token {
  87. node, err := tq.First(ctx)
  88. if err != nil && !IsNotFound(err) {
  89. panic(err)
  90. }
  91. return node
  92. }
  93. // FirstID returns the first Token ID from the query.
  94. // Returns a *NotFoundError when no Token ID was found.
  95. func (tq *TokenQuery) FirstID(ctx context.Context) (id uint64, err error) {
  96. var ids []uint64
  97. if ids, err = tq.Limit(1).IDs(setContextOp(ctx, tq.ctx, "FirstID")); err != nil {
  98. return
  99. }
  100. if len(ids) == 0 {
  101. err = &NotFoundError{token.Label}
  102. return
  103. }
  104. return ids[0], nil
  105. }
  106. // FirstIDX is like FirstID, but panics if an error occurs.
  107. func (tq *TokenQuery) FirstIDX(ctx context.Context) uint64 {
  108. id, err := tq.FirstID(ctx)
  109. if err != nil && !IsNotFound(err) {
  110. panic(err)
  111. }
  112. return id
  113. }
  114. // Only returns a single Token entity found by the query, ensuring it only returns one.
  115. // Returns a *NotSingularError when more than one Token entity is found.
  116. // Returns a *NotFoundError when no Token entities are found.
  117. func (tq *TokenQuery) Only(ctx context.Context) (*Token, error) {
  118. nodes, err := tq.Limit(2).All(setContextOp(ctx, tq.ctx, "Only"))
  119. if err != nil {
  120. return nil, err
  121. }
  122. switch len(nodes) {
  123. case 1:
  124. return nodes[0], nil
  125. case 0:
  126. return nil, &NotFoundError{token.Label}
  127. default:
  128. return nil, &NotSingularError{token.Label}
  129. }
  130. }
  131. // OnlyX is like Only, but panics if an error occurs.
  132. func (tq *TokenQuery) OnlyX(ctx context.Context) *Token {
  133. node, err := tq.Only(ctx)
  134. if err != nil {
  135. panic(err)
  136. }
  137. return node
  138. }
  139. // OnlyID is like Only, but returns the only Token ID in the query.
  140. // Returns a *NotSingularError when more than one Token ID is found.
  141. // Returns a *NotFoundError when no entities are found.
  142. func (tq *TokenQuery) OnlyID(ctx context.Context) (id uint64, err error) {
  143. var ids []uint64
  144. if ids, err = tq.Limit(2).IDs(setContextOp(ctx, tq.ctx, "OnlyID")); err != nil {
  145. return
  146. }
  147. switch len(ids) {
  148. case 1:
  149. id = ids[0]
  150. case 0:
  151. err = &NotFoundError{token.Label}
  152. default:
  153. err = &NotSingularError{token.Label}
  154. }
  155. return
  156. }
  157. // OnlyIDX is like OnlyID, but panics if an error occurs.
  158. func (tq *TokenQuery) OnlyIDX(ctx context.Context) uint64 {
  159. id, err := tq.OnlyID(ctx)
  160. if err != nil {
  161. panic(err)
  162. }
  163. return id
  164. }
  165. // All executes the query and returns a list of Tokens.
  166. func (tq *TokenQuery) All(ctx context.Context) ([]*Token, error) {
  167. ctx = setContextOp(ctx, tq.ctx, "All")
  168. if err := tq.prepareQuery(ctx); err != nil {
  169. return nil, err
  170. }
  171. qr := querierAll[[]*Token, *TokenQuery]()
  172. return withInterceptors[[]*Token](ctx, tq, qr, tq.inters)
  173. }
  174. // AllX is like All, but panics if an error occurs.
  175. func (tq *TokenQuery) AllX(ctx context.Context) []*Token {
  176. nodes, err := tq.All(ctx)
  177. if err != nil {
  178. panic(err)
  179. }
  180. return nodes
  181. }
  182. // IDs executes the query and returns a list of Token IDs.
  183. func (tq *TokenQuery) IDs(ctx context.Context) (ids []uint64, err error) {
  184. if tq.ctx.Unique == nil && tq.path != nil {
  185. tq.Unique(true)
  186. }
  187. ctx = setContextOp(ctx, tq.ctx, "IDs")
  188. if err = tq.Select(token.FieldID).Scan(ctx, &ids); err != nil {
  189. return nil, err
  190. }
  191. return ids, nil
  192. }
  193. // IDsX is like IDs, but panics if an error occurs.
  194. func (tq *TokenQuery) IDsX(ctx context.Context) []uint64 {
  195. ids, err := tq.IDs(ctx)
  196. if err != nil {
  197. panic(err)
  198. }
  199. return ids
  200. }
  201. // Count returns the count of the given query.
  202. func (tq *TokenQuery) Count(ctx context.Context) (int, error) {
  203. ctx = setContextOp(ctx, tq.ctx, "Count")
  204. if err := tq.prepareQuery(ctx); err != nil {
  205. return 0, err
  206. }
  207. return withInterceptors[int](ctx, tq, querierCount[*TokenQuery](), tq.inters)
  208. }
  209. // CountX is like Count, but panics if an error occurs.
  210. func (tq *TokenQuery) CountX(ctx context.Context) int {
  211. count, err := tq.Count(ctx)
  212. if err != nil {
  213. panic(err)
  214. }
  215. return count
  216. }
  217. // Exist returns true if the query has elements in the graph.
  218. func (tq *TokenQuery) Exist(ctx context.Context) (bool, error) {
  219. ctx = setContextOp(ctx, tq.ctx, "Exist")
  220. switch _, err := tq.FirstID(ctx); {
  221. case IsNotFound(err):
  222. return false, nil
  223. case err != nil:
  224. return false, fmt.Errorf("ent: check existence: %w", err)
  225. default:
  226. return true, nil
  227. }
  228. }
  229. // ExistX is like Exist, but panics if an error occurs.
  230. func (tq *TokenQuery) ExistX(ctx context.Context) bool {
  231. exist, err := tq.Exist(ctx)
  232. if err != nil {
  233. panic(err)
  234. }
  235. return exist
  236. }
  237. // Clone returns a duplicate of the TokenQuery builder, including all associated steps. It can be
  238. // used to prepare common query builders and use them differently after the clone is made.
  239. func (tq *TokenQuery) Clone() *TokenQuery {
  240. if tq == nil {
  241. return nil
  242. }
  243. return &TokenQuery{
  244. config: tq.config,
  245. ctx: tq.ctx.Clone(),
  246. order: append([]token.OrderOption{}, tq.order...),
  247. inters: append([]Interceptor{}, tq.inters...),
  248. predicates: append([]predicate.Token{}, tq.predicates...),
  249. withAgent: tq.withAgent.Clone(),
  250. // clone intermediate query.
  251. sql: tq.sql.Clone(),
  252. path: tq.path,
  253. }
  254. }
  255. // WithAgent tells the query-builder to eager-load the nodes that are connected to
  256. // the "agent" edge. The optional arguments are used to configure the query builder of the edge.
  257. func (tq *TokenQuery) WithAgent(opts ...func(*AgentQuery)) *TokenQuery {
  258. query := (&AgentClient{config: tq.config}).Query()
  259. for _, opt := range opts {
  260. opt(query)
  261. }
  262. tq.withAgent = query
  263. return tq
  264. }
  265. // GroupBy is used to group vertices by one or more fields/columns.
  266. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  267. //
  268. // Example:
  269. //
  270. // var v []struct {
  271. // CreatedAt time.Time `json:"created_at,omitempty"`
  272. // Count int `json:"count,omitempty"`
  273. // }
  274. //
  275. // client.Token.Query().
  276. // GroupBy(token.FieldCreatedAt).
  277. // Aggregate(ent.Count()).
  278. // Scan(ctx, &v)
  279. func (tq *TokenQuery) GroupBy(field string, fields ...string) *TokenGroupBy {
  280. tq.ctx.Fields = append([]string{field}, fields...)
  281. grbuild := &TokenGroupBy{build: tq}
  282. grbuild.flds = &tq.ctx.Fields
  283. grbuild.label = token.Label
  284. grbuild.scan = grbuild.Scan
  285. return grbuild
  286. }
  287. // Select allows the selection one or more fields/columns for the given query,
  288. // instead of selecting all fields in the entity.
  289. //
  290. // Example:
  291. //
  292. // var v []struct {
  293. // CreatedAt time.Time `json:"created_at,omitempty"`
  294. // }
  295. //
  296. // client.Token.Query().
  297. // Select(token.FieldCreatedAt).
  298. // Scan(ctx, &v)
  299. func (tq *TokenQuery) Select(fields ...string) *TokenSelect {
  300. tq.ctx.Fields = append(tq.ctx.Fields, fields...)
  301. sbuild := &TokenSelect{TokenQuery: tq}
  302. sbuild.label = token.Label
  303. sbuild.flds, sbuild.scan = &tq.ctx.Fields, sbuild.Scan
  304. return sbuild
  305. }
  306. // Aggregate returns a TokenSelect configured with the given aggregations.
  307. func (tq *TokenQuery) Aggregate(fns ...AggregateFunc) *TokenSelect {
  308. return tq.Select().Aggregate(fns...)
  309. }
  310. func (tq *TokenQuery) prepareQuery(ctx context.Context) error {
  311. for _, inter := range tq.inters {
  312. if inter == nil {
  313. return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
  314. }
  315. if trv, ok := inter.(Traverser); ok {
  316. if err := trv.Traverse(ctx, tq); err != nil {
  317. return err
  318. }
  319. }
  320. }
  321. for _, f := range tq.ctx.Fields {
  322. if !token.ValidColumn(f) {
  323. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  324. }
  325. }
  326. if tq.path != nil {
  327. prev, err := tq.path(ctx)
  328. if err != nil {
  329. return err
  330. }
  331. tq.sql = prev
  332. }
  333. return nil
  334. }
  335. func (tq *TokenQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Token, error) {
  336. var (
  337. nodes = []*Token{}
  338. _spec = tq.querySpec()
  339. loadedTypes = [1]bool{
  340. tq.withAgent != nil,
  341. }
  342. )
  343. _spec.ScanValues = func(columns []string) ([]any, error) {
  344. return (*Token).scanValues(nil, columns)
  345. }
  346. _spec.Assign = func(columns []string, values []any) error {
  347. node := &Token{config: tq.config}
  348. nodes = append(nodes, node)
  349. node.Edges.loadedTypes = loadedTypes
  350. return node.assignValues(columns, values)
  351. }
  352. for i := range hooks {
  353. hooks[i](ctx, _spec)
  354. }
  355. if err := sqlgraph.QueryNodes(ctx, tq.driver, _spec); err != nil {
  356. return nil, err
  357. }
  358. if len(nodes) == 0 {
  359. return nodes, nil
  360. }
  361. if query := tq.withAgent; query != nil {
  362. if err := tq.loadAgent(ctx, query, nodes, nil,
  363. func(n *Token, e *Agent) { n.Edges.Agent = e }); err != nil {
  364. return nil, err
  365. }
  366. }
  367. return nodes, nil
  368. }
  369. func (tq *TokenQuery) loadAgent(ctx context.Context, query *AgentQuery, nodes []*Token, init func(*Token), assign func(*Token, *Agent)) error {
  370. ids := make([]uint64, 0, len(nodes))
  371. nodeids := make(map[uint64][]*Token)
  372. for i := range nodes {
  373. fk := nodes[i].AgentID
  374. if _, ok := nodeids[fk]; !ok {
  375. ids = append(ids, fk)
  376. }
  377. nodeids[fk] = append(nodeids[fk], nodes[i])
  378. }
  379. if len(ids) == 0 {
  380. return nil
  381. }
  382. query.Where(agent.IDIn(ids...))
  383. neighbors, err := query.All(ctx)
  384. if err != nil {
  385. return err
  386. }
  387. for _, n := range neighbors {
  388. nodes, ok := nodeids[n.ID]
  389. if !ok {
  390. return fmt.Errorf(`unexpected foreign-key "agent_id" returned %v`, n.ID)
  391. }
  392. for i := range nodes {
  393. assign(nodes[i], n)
  394. }
  395. }
  396. return nil
  397. }
  398. func (tq *TokenQuery) sqlCount(ctx context.Context) (int, error) {
  399. _spec := tq.querySpec()
  400. _spec.Node.Columns = tq.ctx.Fields
  401. if len(tq.ctx.Fields) > 0 {
  402. _spec.Unique = tq.ctx.Unique != nil && *tq.ctx.Unique
  403. }
  404. return sqlgraph.CountNodes(ctx, tq.driver, _spec)
  405. }
  406. func (tq *TokenQuery) querySpec() *sqlgraph.QuerySpec {
  407. _spec := sqlgraph.NewQuerySpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
  408. _spec.From = tq.sql
  409. if unique := tq.ctx.Unique; unique != nil {
  410. _spec.Unique = *unique
  411. } else if tq.path != nil {
  412. _spec.Unique = true
  413. }
  414. if fields := tq.ctx.Fields; len(fields) > 0 {
  415. _spec.Node.Columns = make([]string, 0, len(fields))
  416. _spec.Node.Columns = append(_spec.Node.Columns, token.FieldID)
  417. for i := range fields {
  418. if fields[i] != token.FieldID {
  419. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  420. }
  421. }
  422. if tq.withAgent != nil {
  423. _spec.Node.AddColumnOnce(token.FieldAgentID)
  424. }
  425. }
  426. if ps := tq.predicates; len(ps) > 0 {
  427. _spec.Predicate = func(selector *sql.Selector) {
  428. for i := range ps {
  429. ps[i](selector)
  430. }
  431. }
  432. }
  433. if limit := tq.ctx.Limit; limit != nil {
  434. _spec.Limit = *limit
  435. }
  436. if offset := tq.ctx.Offset; offset != nil {
  437. _spec.Offset = *offset
  438. }
  439. if ps := tq.order; len(ps) > 0 {
  440. _spec.Order = func(selector *sql.Selector) {
  441. for i := range ps {
  442. ps[i](selector)
  443. }
  444. }
  445. }
  446. return _spec
  447. }
  448. func (tq *TokenQuery) sqlQuery(ctx context.Context) *sql.Selector {
  449. builder := sql.Dialect(tq.driver.Dialect())
  450. t1 := builder.Table(token.Table)
  451. columns := tq.ctx.Fields
  452. if len(columns) == 0 {
  453. columns = token.Columns
  454. }
  455. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  456. if tq.sql != nil {
  457. selector = tq.sql
  458. selector.Select(selector.Columns(columns...)...)
  459. }
  460. if tq.ctx.Unique != nil && *tq.ctx.Unique {
  461. selector.Distinct()
  462. }
  463. for _, p := range tq.predicates {
  464. p(selector)
  465. }
  466. for _, p := range tq.order {
  467. p(selector)
  468. }
  469. if offset := tq.ctx.Offset; offset != nil {
  470. // limit is mandatory for offset clause. We start
  471. // with default value, and override it below if needed.
  472. selector.Offset(*offset).Limit(math.MaxInt32)
  473. }
  474. if limit := tq.ctx.Limit; limit != nil {
  475. selector.Limit(*limit)
  476. }
  477. return selector
  478. }
  479. // TokenGroupBy is the group-by builder for Token entities.
  480. type TokenGroupBy struct {
  481. selector
  482. build *TokenQuery
  483. }
  484. // Aggregate adds the given aggregation functions to the group-by query.
  485. func (tgb *TokenGroupBy) Aggregate(fns ...AggregateFunc) *TokenGroupBy {
  486. tgb.fns = append(tgb.fns, fns...)
  487. return tgb
  488. }
  489. // Scan applies the selector query and scans the result into the given value.
  490. func (tgb *TokenGroupBy) Scan(ctx context.Context, v any) error {
  491. ctx = setContextOp(ctx, tgb.build.ctx, "GroupBy")
  492. if err := tgb.build.prepareQuery(ctx); err != nil {
  493. return err
  494. }
  495. return scanWithInterceptors[*TokenQuery, *TokenGroupBy](ctx, tgb.build, tgb, tgb.build.inters, v)
  496. }
  497. func (tgb *TokenGroupBy) sqlScan(ctx context.Context, root *TokenQuery, v any) error {
  498. selector := root.sqlQuery(ctx).Select()
  499. aggregation := make([]string, 0, len(tgb.fns))
  500. for _, fn := range tgb.fns {
  501. aggregation = append(aggregation, fn(selector))
  502. }
  503. if len(selector.SelectedColumns()) == 0 {
  504. columns := make([]string, 0, len(*tgb.flds)+len(tgb.fns))
  505. for _, f := range *tgb.flds {
  506. columns = append(columns, selector.C(f))
  507. }
  508. columns = append(columns, aggregation...)
  509. selector.Select(columns...)
  510. }
  511. selector.GroupBy(selector.Columns(*tgb.flds...)...)
  512. if err := selector.Err(); err != nil {
  513. return err
  514. }
  515. rows := &sql.Rows{}
  516. query, args := selector.Query()
  517. if err := tgb.build.driver.Query(ctx, query, args, rows); err != nil {
  518. return err
  519. }
  520. defer rows.Close()
  521. return sql.ScanSlice(rows, v)
  522. }
  523. // TokenSelect is the builder for selecting fields of Token entities.
  524. type TokenSelect struct {
  525. *TokenQuery
  526. selector
  527. }
  528. // Aggregate adds the given aggregation functions to the selector query.
  529. func (ts *TokenSelect) Aggregate(fns ...AggregateFunc) *TokenSelect {
  530. ts.fns = append(ts.fns, fns...)
  531. return ts
  532. }
  533. // Scan applies the selector query and scans the result into the given value.
  534. func (ts *TokenSelect) Scan(ctx context.Context, v any) error {
  535. ctx = setContextOp(ctx, ts.ctx, "Select")
  536. if err := ts.prepareQuery(ctx); err != nil {
  537. return err
  538. }
  539. return scanWithInterceptors[*TokenQuery, *TokenSelect](ctx, ts.TokenQuery, ts, ts.inters, v)
  540. }
  541. func (ts *TokenSelect) sqlScan(ctx context.Context, root *TokenQuery, v any) error {
  542. selector := root.sqlQuery(ctx)
  543. aggregation := make([]string, 0, len(ts.fns))
  544. for _, fn := range ts.fns {
  545. aggregation = append(aggregation, fn(selector))
  546. }
  547. switch n := len(*ts.selector.flds); {
  548. case n == 0 && len(aggregation) > 0:
  549. selector.Select(aggregation...)
  550. case n != 0 && len(aggregation) > 0:
  551. selector.AppendSelect(aggregation...)
  552. }
  553. rows := &sql.Rows{}
  554. query, args := selector.Query()
  555. if err := ts.driver.Query(ctx, query, args, rows); err != nil {
  556. return err
  557. }
  558. defer rows.Close()
  559. return sql.ScanSlice(rows, v)
  560. }