contact_query.go 20 KB

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