messagerecords_query.go 23 KB

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