agent_query.go 21 KB

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