wx_query.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "math"
  7. "wechat-api/ent/agent"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/ent/server"
  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. // WxQuery is the builder for querying Wx entities.
  16. type WxQuery struct {
  17. config
  18. ctx *QueryContext
  19. order []wx.OrderOption
  20. inters []Interceptor
  21. predicates []predicate.Wx
  22. withServer *ServerQuery
  23. withAgent *AgentQuery
  24. withFKs bool
  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 WxQuery builder.
  30. func (wq *WxQuery) Where(ps ...predicate.Wx) *WxQuery {
  31. wq.predicates = append(wq.predicates, ps...)
  32. return wq
  33. }
  34. // Limit the number of records to be returned by this query.
  35. func (wq *WxQuery) Limit(limit int) *WxQuery {
  36. wq.ctx.Limit = &limit
  37. return wq
  38. }
  39. // Offset to start from.
  40. func (wq *WxQuery) Offset(offset int) *WxQuery {
  41. wq.ctx.Offset = &offset
  42. return wq
  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 (wq *WxQuery) Unique(unique bool) *WxQuery {
  47. wq.ctx.Unique = &unique
  48. return wq
  49. }
  50. // Order specifies how the records should be ordered.
  51. func (wq *WxQuery) Order(o ...wx.OrderOption) *WxQuery {
  52. wq.order = append(wq.order, o...)
  53. return wq
  54. }
  55. // QueryServer chains the current query on the "server" edge.
  56. func (wq *WxQuery) QueryServer() *ServerQuery {
  57. query := (&ServerClient{config: wq.config}).Query()
  58. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  59. if err := wq.prepareQuery(ctx); err != nil {
  60. return nil, err
  61. }
  62. selector := wq.sqlQuery(ctx)
  63. if err := selector.Err(); err != nil {
  64. return nil, err
  65. }
  66. step := sqlgraph.NewStep(
  67. sqlgraph.From(wx.Table, wx.FieldID, selector),
  68. sqlgraph.To(server.Table, server.FieldID),
  69. sqlgraph.Edge(sqlgraph.M2O, true, wx.ServerTable, wx.ServerColumn),
  70. )
  71. fromU = sqlgraph.SetNeighbors(wq.driver.Dialect(), step)
  72. return fromU, nil
  73. }
  74. return query
  75. }
  76. // QueryAgent chains the current query on the "agent" edge.
  77. func (wq *WxQuery) QueryAgent() *AgentQuery {
  78. query := (&AgentClient{config: wq.config}).Query()
  79. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  80. if err := wq.prepareQuery(ctx); err != nil {
  81. return nil, err
  82. }
  83. selector := wq.sqlQuery(ctx)
  84. if err := selector.Err(); err != nil {
  85. return nil, err
  86. }
  87. step := sqlgraph.NewStep(
  88. sqlgraph.From(wx.Table, wx.FieldID, selector),
  89. sqlgraph.To(agent.Table, agent.FieldID),
  90. sqlgraph.Edge(sqlgraph.M2O, true, wx.AgentTable, wx.AgentColumn),
  91. )
  92. fromU = sqlgraph.SetNeighbors(wq.driver.Dialect(), step)
  93. return fromU, nil
  94. }
  95. return query
  96. }
  97. // First returns the first Wx entity from the query.
  98. // Returns a *NotFoundError when no Wx was found.
  99. func (wq *WxQuery) First(ctx context.Context) (*Wx, error) {
  100. nodes, err := wq.Limit(1).All(setContextOp(ctx, wq.ctx, "First"))
  101. if err != nil {
  102. return nil, err
  103. }
  104. if len(nodes) == 0 {
  105. return nil, &NotFoundError{wx.Label}
  106. }
  107. return nodes[0], nil
  108. }
  109. // FirstX is like First, but panics if an error occurs.
  110. func (wq *WxQuery) FirstX(ctx context.Context) *Wx {
  111. node, err := wq.First(ctx)
  112. if err != nil && !IsNotFound(err) {
  113. panic(err)
  114. }
  115. return node
  116. }
  117. // FirstID returns the first Wx ID from the query.
  118. // Returns a *NotFoundError when no Wx ID was found.
  119. func (wq *WxQuery) FirstID(ctx context.Context) (id uint64, err error) {
  120. var ids []uint64
  121. if ids, err = wq.Limit(1).IDs(setContextOp(ctx, wq.ctx, "FirstID")); err != nil {
  122. return
  123. }
  124. if len(ids) == 0 {
  125. err = &NotFoundError{wx.Label}
  126. return
  127. }
  128. return ids[0], nil
  129. }
  130. // FirstIDX is like FirstID, but panics if an error occurs.
  131. func (wq *WxQuery) FirstIDX(ctx context.Context) uint64 {
  132. id, err := wq.FirstID(ctx)
  133. if err != nil && !IsNotFound(err) {
  134. panic(err)
  135. }
  136. return id
  137. }
  138. // Only returns a single Wx entity found by the query, ensuring it only returns one.
  139. // Returns a *NotSingularError when more than one Wx entity is found.
  140. // Returns a *NotFoundError when no Wx entities are found.
  141. func (wq *WxQuery) Only(ctx context.Context) (*Wx, error) {
  142. nodes, err := wq.Limit(2).All(setContextOp(ctx, wq.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{wx.Label}
  151. default:
  152. return nil, &NotSingularError{wx.Label}
  153. }
  154. }
  155. // OnlyX is like Only, but panics if an error occurs.
  156. func (wq *WxQuery) OnlyX(ctx context.Context) *Wx {
  157. node, err := wq.Only(ctx)
  158. if err != nil {
  159. panic(err)
  160. }
  161. return node
  162. }
  163. // OnlyID is like Only, but returns the only Wx ID in the query.
  164. // Returns a *NotSingularError when more than one Wx ID is found.
  165. // Returns a *NotFoundError when no entities are found.
  166. func (wq *WxQuery) OnlyID(ctx context.Context) (id uint64, err error) {
  167. var ids []uint64
  168. if ids, err = wq.Limit(2).IDs(setContextOp(ctx, wq.ctx, "OnlyID")); err != nil {
  169. return
  170. }
  171. switch len(ids) {
  172. case 1:
  173. id = ids[0]
  174. case 0:
  175. err = &NotFoundError{wx.Label}
  176. default:
  177. err = &NotSingularError{wx.Label}
  178. }
  179. return
  180. }
  181. // OnlyIDX is like OnlyID, but panics if an error occurs.
  182. func (wq *WxQuery) OnlyIDX(ctx context.Context) uint64 {
  183. id, err := wq.OnlyID(ctx)
  184. if err != nil {
  185. panic(err)
  186. }
  187. return id
  188. }
  189. // All executes the query and returns a list of Wxes.
  190. func (wq *WxQuery) All(ctx context.Context) ([]*Wx, error) {
  191. ctx = setContextOp(ctx, wq.ctx, "All")
  192. if err := wq.prepareQuery(ctx); err != nil {
  193. return nil, err
  194. }
  195. qr := querierAll[[]*Wx, *WxQuery]()
  196. return withInterceptors[[]*Wx](ctx, wq, qr, wq.inters)
  197. }
  198. // AllX is like All, but panics if an error occurs.
  199. func (wq *WxQuery) AllX(ctx context.Context) []*Wx {
  200. nodes, err := wq.All(ctx)
  201. if err != nil {
  202. panic(err)
  203. }
  204. return nodes
  205. }
  206. // IDs executes the query and returns a list of Wx IDs.
  207. func (wq *WxQuery) IDs(ctx context.Context) (ids []uint64, err error) {
  208. if wq.ctx.Unique == nil && wq.path != nil {
  209. wq.Unique(true)
  210. }
  211. ctx = setContextOp(ctx, wq.ctx, "IDs")
  212. if err = wq.Select(wx.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 (wq *WxQuery) IDsX(ctx context.Context) []uint64 {
  219. ids, err := wq.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 (wq *WxQuery) Count(ctx context.Context) (int, error) {
  227. ctx = setContextOp(ctx, wq.ctx, "Count")
  228. if err := wq.prepareQuery(ctx); err != nil {
  229. return 0, err
  230. }
  231. return withInterceptors[int](ctx, wq, querierCount[*WxQuery](), wq.inters)
  232. }
  233. // CountX is like Count, but panics if an error occurs.
  234. func (wq *WxQuery) CountX(ctx context.Context) int {
  235. count, err := wq.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 (wq *WxQuery) Exist(ctx context.Context) (bool, error) {
  243. ctx = setContextOp(ctx, wq.ctx, "Exist")
  244. switch _, err := wq.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 (wq *WxQuery) ExistX(ctx context.Context) bool {
  255. exist, err := wq.Exist(ctx)
  256. if err != nil {
  257. panic(err)
  258. }
  259. return exist
  260. }
  261. // Clone returns a duplicate of the WxQuery 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 (wq *WxQuery) Clone() *WxQuery {
  264. if wq == nil {
  265. return nil
  266. }
  267. return &WxQuery{
  268. config: wq.config,
  269. ctx: wq.ctx.Clone(),
  270. order: append([]wx.OrderOption{}, wq.order...),
  271. inters: append([]Interceptor{}, wq.inters...),
  272. predicates: append([]predicate.Wx{}, wq.predicates...),
  273. withServer: wq.withServer.Clone(),
  274. withAgent: wq.withAgent.Clone(),
  275. // clone intermediate query.
  276. sql: wq.sql.Clone(),
  277. path: wq.path,
  278. }
  279. }
  280. // WithServer tells the query-builder to eager-load the nodes that are connected to
  281. // the "server" edge. The optional arguments are used to configure the query builder of the edge.
  282. func (wq *WxQuery) WithServer(opts ...func(*ServerQuery)) *WxQuery {
  283. query := (&ServerClient{config: wq.config}).Query()
  284. for _, opt := range opts {
  285. opt(query)
  286. }
  287. wq.withServer = query
  288. return wq
  289. }
  290. // WithAgent tells the query-builder to eager-load the nodes that are connected to
  291. // the "agent" edge. The optional arguments are used to configure the query builder of the edge.
  292. func (wq *WxQuery) WithAgent(opts ...func(*AgentQuery)) *WxQuery {
  293. query := (&AgentClient{config: wq.config}).Query()
  294. for _, opt := range opts {
  295. opt(query)
  296. }
  297. wq.withAgent = query
  298. return wq
  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.Wx.Query().
  311. // GroupBy(wx.FieldCreatedAt).
  312. // Aggregate(ent.Count()).
  313. // Scan(ctx, &v)
  314. func (wq *WxQuery) GroupBy(field string, fields ...string) *WxGroupBy {
  315. wq.ctx.Fields = append([]string{field}, fields...)
  316. grbuild := &WxGroupBy{build: wq}
  317. grbuild.flds = &wq.ctx.Fields
  318. grbuild.label = wx.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.Wx.Query().
  332. // Select(wx.FieldCreatedAt).
  333. // Scan(ctx, &v)
  334. func (wq *WxQuery) Select(fields ...string) *WxSelect {
  335. wq.ctx.Fields = append(wq.ctx.Fields, fields...)
  336. sbuild := &WxSelect{WxQuery: wq}
  337. sbuild.label = wx.Label
  338. sbuild.flds, sbuild.scan = &wq.ctx.Fields, sbuild.Scan
  339. return sbuild
  340. }
  341. // Aggregate returns a WxSelect configured with the given aggregations.
  342. func (wq *WxQuery) Aggregate(fns ...AggregateFunc) *WxSelect {
  343. return wq.Select().Aggregate(fns...)
  344. }
  345. func (wq *WxQuery) prepareQuery(ctx context.Context) error {
  346. for _, inter := range wq.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, wq); err != nil {
  352. return err
  353. }
  354. }
  355. }
  356. for _, f := range wq.ctx.Fields {
  357. if !wx.ValidColumn(f) {
  358. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  359. }
  360. }
  361. if wq.path != nil {
  362. prev, err := wq.path(ctx)
  363. if err != nil {
  364. return err
  365. }
  366. wq.sql = prev
  367. }
  368. return nil
  369. }
  370. func (wq *WxQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Wx, error) {
  371. var (
  372. nodes = []*Wx{}
  373. withFKs = wq.withFKs
  374. _spec = wq.querySpec()
  375. loadedTypes = [2]bool{
  376. wq.withServer != nil,
  377. wq.withAgent != nil,
  378. }
  379. )
  380. if withFKs {
  381. _spec.Node.Columns = append(_spec.Node.Columns, wx.ForeignKeys...)
  382. }
  383. _spec.ScanValues = func(columns []string) ([]any, error) {
  384. return (*Wx).scanValues(nil, columns)
  385. }
  386. _spec.Assign = func(columns []string, values []any) error {
  387. node := &Wx{config: wq.config}
  388. nodes = append(nodes, node)
  389. node.Edges.loadedTypes = loadedTypes
  390. return node.assignValues(columns, values)
  391. }
  392. for i := range hooks {
  393. hooks[i](ctx, _spec)
  394. }
  395. if err := sqlgraph.QueryNodes(ctx, wq.driver, _spec); err != nil {
  396. return nil, err
  397. }
  398. if len(nodes) == 0 {
  399. return nodes, nil
  400. }
  401. if query := wq.withServer; query != nil {
  402. if err := wq.loadServer(ctx, query, nodes, nil,
  403. func(n *Wx, e *Server) { n.Edges.Server = e }); err != nil {
  404. return nil, err
  405. }
  406. }
  407. if query := wq.withAgent; query != nil {
  408. if err := wq.loadAgent(ctx, query, nodes, nil,
  409. func(n *Wx, e *Agent) { n.Edges.Agent = e }); err != nil {
  410. return nil, err
  411. }
  412. }
  413. return nodes, nil
  414. }
  415. func (wq *WxQuery) loadServer(ctx context.Context, query *ServerQuery, nodes []*Wx, init func(*Wx), assign func(*Wx, *Server)) error {
  416. ids := make([]uint64, 0, len(nodes))
  417. nodeids := make(map[uint64][]*Wx)
  418. for i := range nodes {
  419. fk := nodes[i].ServerID
  420. if _, ok := nodeids[fk]; !ok {
  421. ids = append(ids, fk)
  422. }
  423. nodeids[fk] = append(nodeids[fk], nodes[i])
  424. }
  425. if len(ids) == 0 {
  426. return nil
  427. }
  428. query.Where(server.IDIn(ids...))
  429. neighbors, err := query.All(ctx)
  430. if err != nil {
  431. return err
  432. }
  433. for _, n := range neighbors {
  434. nodes, ok := nodeids[n.ID]
  435. if !ok {
  436. return fmt.Errorf(`unexpected foreign-key "server_id" returned %v`, n.ID)
  437. }
  438. for i := range nodes {
  439. assign(nodes[i], n)
  440. }
  441. }
  442. return nil
  443. }
  444. func (wq *WxQuery) loadAgent(ctx context.Context, query *AgentQuery, nodes []*Wx, init func(*Wx), assign func(*Wx, *Agent)) error {
  445. ids := make([]uint64, 0, len(nodes))
  446. nodeids := make(map[uint64][]*Wx)
  447. for i := range nodes {
  448. fk := nodes[i].AgentID
  449. if _, ok := nodeids[fk]; !ok {
  450. ids = append(ids, fk)
  451. }
  452. nodeids[fk] = append(nodeids[fk], nodes[i])
  453. }
  454. if len(ids) == 0 {
  455. return nil
  456. }
  457. query.Where(agent.IDIn(ids...))
  458. neighbors, err := query.All(ctx)
  459. if err != nil {
  460. return err
  461. }
  462. for _, n := range neighbors {
  463. nodes, ok := nodeids[n.ID]
  464. if !ok {
  465. return fmt.Errorf(`unexpected foreign-key "agent_id" returned %v`, n.ID)
  466. }
  467. for i := range nodes {
  468. assign(nodes[i], n)
  469. }
  470. }
  471. return nil
  472. }
  473. func (wq *WxQuery) sqlCount(ctx context.Context) (int, error) {
  474. _spec := wq.querySpec()
  475. _spec.Node.Columns = wq.ctx.Fields
  476. if len(wq.ctx.Fields) > 0 {
  477. _spec.Unique = wq.ctx.Unique != nil && *wq.ctx.Unique
  478. }
  479. return sqlgraph.CountNodes(ctx, wq.driver, _spec)
  480. }
  481. func (wq *WxQuery) querySpec() *sqlgraph.QuerySpec {
  482. _spec := sqlgraph.NewQuerySpec(wx.Table, wx.Columns, sqlgraph.NewFieldSpec(wx.FieldID, field.TypeUint64))
  483. _spec.From = wq.sql
  484. if unique := wq.ctx.Unique; unique != nil {
  485. _spec.Unique = *unique
  486. } else if wq.path != nil {
  487. _spec.Unique = true
  488. }
  489. if fields := wq.ctx.Fields; len(fields) > 0 {
  490. _spec.Node.Columns = make([]string, 0, len(fields))
  491. _spec.Node.Columns = append(_spec.Node.Columns, wx.FieldID)
  492. for i := range fields {
  493. if fields[i] != wx.FieldID {
  494. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  495. }
  496. }
  497. if wq.withServer != nil {
  498. _spec.Node.AddColumnOnce(wx.FieldServerID)
  499. }
  500. if wq.withAgent != nil {
  501. _spec.Node.AddColumnOnce(wx.FieldAgentID)
  502. }
  503. }
  504. if ps := wq.predicates; len(ps) > 0 {
  505. _spec.Predicate = func(selector *sql.Selector) {
  506. for i := range ps {
  507. ps[i](selector)
  508. }
  509. }
  510. }
  511. if limit := wq.ctx.Limit; limit != nil {
  512. _spec.Limit = *limit
  513. }
  514. if offset := wq.ctx.Offset; offset != nil {
  515. _spec.Offset = *offset
  516. }
  517. if ps := wq.order; len(ps) > 0 {
  518. _spec.Order = func(selector *sql.Selector) {
  519. for i := range ps {
  520. ps[i](selector)
  521. }
  522. }
  523. }
  524. return _spec
  525. }
  526. func (wq *WxQuery) sqlQuery(ctx context.Context) *sql.Selector {
  527. builder := sql.Dialect(wq.driver.Dialect())
  528. t1 := builder.Table(wx.Table)
  529. columns := wq.ctx.Fields
  530. if len(columns) == 0 {
  531. columns = wx.Columns
  532. }
  533. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  534. if wq.sql != nil {
  535. selector = wq.sql
  536. selector.Select(selector.Columns(columns...)...)
  537. }
  538. if wq.ctx.Unique != nil && *wq.ctx.Unique {
  539. selector.Distinct()
  540. }
  541. for _, p := range wq.predicates {
  542. p(selector)
  543. }
  544. for _, p := range wq.order {
  545. p(selector)
  546. }
  547. if offset := wq.ctx.Offset; offset != nil {
  548. // limit is mandatory for offset clause. We start
  549. // with default value, and override it below if needed.
  550. selector.Offset(*offset).Limit(math.MaxInt32)
  551. }
  552. if limit := wq.ctx.Limit; limit != nil {
  553. selector.Limit(*limit)
  554. }
  555. return selector
  556. }
  557. // WxGroupBy is the group-by builder for Wx entities.
  558. type WxGroupBy struct {
  559. selector
  560. build *WxQuery
  561. }
  562. // Aggregate adds the given aggregation functions to the group-by query.
  563. func (wgb *WxGroupBy) Aggregate(fns ...AggregateFunc) *WxGroupBy {
  564. wgb.fns = append(wgb.fns, fns...)
  565. return wgb
  566. }
  567. // Scan applies the selector query and scans the result into the given value.
  568. func (wgb *WxGroupBy) Scan(ctx context.Context, v any) error {
  569. ctx = setContextOp(ctx, wgb.build.ctx, "GroupBy")
  570. if err := wgb.build.prepareQuery(ctx); err != nil {
  571. return err
  572. }
  573. return scanWithInterceptors[*WxQuery, *WxGroupBy](ctx, wgb.build, wgb, wgb.build.inters, v)
  574. }
  575. func (wgb *WxGroupBy) sqlScan(ctx context.Context, root *WxQuery, v any) error {
  576. selector := root.sqlQuery(ctx).Select()
  577. aggregation := make([]string, 0, len(wgb.fns))
  578. for _, fn := range wgb.fns {
  579. aggregation = append(aggregation, fn(selector))
  580. }
  581. if len(selector.SelectedColumns()) == 0 {
  582. columns := make([]string, 0, len(*wgb.flds)+len(wgb.fns))
  583. for _, f := range *wgb.flds {
  584. columns = append(columns, selector.C(f))
  585. }
  586. columns = append(columns, aggregation...)
  587. selector.Select(columns...)
  588. }
  589. selector.GroupBy(selector.Columns(*wgb.flds...)...)
  590. if err := selector.Err(); err != nil {
  591. return err
  592. }
  593. rows := &sql.Rows{}
  594. query, args := selector.Query()
  595. if err := wgb.build.driver.Query(ctx, query, args, rows); err != nil {
  596. return err
  597. }
  598. defer rows.Close()
  599. return sql.ScanSlice(rows, v)
  600. }
  601. // WxSelect is the builder for selecting fields of Wx entities.
  602. type WxSelect struct {
  603. *WxQuery
  604. selector
  605. }
  606. // Aggregate adds the given aggregation functions to the selector query.
  607. func (ws *WxSelect) Aggregate(fns ...AggregateFunc) *WxSelect {
  608. ws.fns = append(ws.fns, fns...)
  609. return ws
  610. }
  611. // Scan applies the selector query and scans the result into the given value.
  612. func (ws *WxSelect) Scan(ctx context.Context, v any) error {
  613. ctx = setContextOp(ctx, ws.ctx, "Select")
  614. if err := ws.prepareQuery(ctx); err != nil {
  615. return err
  616. }
  617. return scanWithInterceptors[*WxQuery, *WxSelect](ctx, ws.WxQuery, ws, ws.inters, v)
  618. }
  619. func (ws *WxSelect) sqlScan(ctx context.Context, root *WxQuery, v any) error {
  620. selector := root.sqlQuery(ctx)
  621. aggregation := make([]string, 0, len(ws.fns))
  622. for _, fn := range ws.fns {
  623. aggregation = append(aggregation, fn(selector))
  624. }
  625. switch n := len(*ws.selector.flds); {
  626. case n == 0 && len(aggregation) > 0:
  627. selector.Select(aggregation...)
  628. case n != 0 && len(aggregation) > 0:
  629. selector.AppendSelect(aggregation...)
  630. }
  631. rows := &sql.Rows{}
  632. query, args := selector.Query()
  633. if err := ws.driver.Query(ctx, query, args, rows); err != nil {
  634. return err
  635. }
  636. defer rows.Close()
  637. return sql.ScanSlice(rows, v)
  638. }