wx_query.go 18 KB

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