agent_query.go 19 KB

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