agent_query.go 23 KB

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