contactfield_query.go 18 KB

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