agent_query.go 16 KB

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