sopnode_query.go 19 KB

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