contact_query.go 22 KB

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