sopnode_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. "entgo.io/ent/dialect/sql"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. "github.com/suyuan32/simple-admin-job/ent/messagerecords"
  12. "github.com/suyuan32/simple-admin-job/ent/predicate"
  13. "github.com/suyuan32/simple-admin-job/ent/sopnode"
  14. "github.com/suyuan32/simple-admin-job/ent/sopstage"
  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. query.withFKs = true
  452. query.Where(predicate.MessageRecords(func(s *sql.Selector) {
  453. s.Where(sql.InValues(s.C(sopnode.NodeMessagesColumn), fks...))
  454. }))
  455. neighbors, err := query.All(ctx)
  456. if err != nil {
  457. return err
  458. }
  459. for _, n := range neighbors {
  460. fk := n.sop_node_node_messages
  461. if fk == nil {
  462. return fmt.Errorf(`foreign-key "sop_node_node_messages" is nil for node %v`, n.ID)
  463. }
  464. node, ok := nodeids[*fk]
  465. if !ok {
  466. return fmt.Errorf(`unexpected referenced foreign-key "sop_node_node_messages" returned %v for node %v`, *fk, n.ID)
  467. }
  468. assign(node, n)
  469. }
  470. return nil
  471. }
  472. func (snq *SopNodeQuery) sqlCount(ctx context.Context) (int, error) {
  473. _spec := snq.querySpec()
  474. _spec.Node.Columns = snq.ctx.Fields
  475. if len(snq.ctx.Fields) > 0 {
  476. _spec.Unique = snq.ctx.Unique != nil && *snq.ctx.Unique
  477. }
  478. return sqlgraph.CountNodes(ctx, snq.driver, _spec)
  479. }
  480. func (snq *SopNodeQuery) querySpec() *sqlgraph.QuerySpec {
  481. _spec := sqlgraph.NewQuerySpec(sopnode.Table, sopnode.Columns, sqlgraph.NewFieldSpec(sopnode.FieldID, field.TypeUint64))
  482. _spec.From = snq.sql
  483. if unique := snq.ctx.Unique; unique != nil {
  484. _spec.Unique = *unique
  485. } else if snq.path != nil {
  486. _spec.Unique = true
  487. }
  488. if fields := snq.ctx.Fields; len(fields) > 0 {
  489. _spec.Node.Columns = make([]string, 0, len(fields))
  490. _spec.Node.Columns = append(_spec.Node.Columns, sopnode.FieldID)
  491. for i := range fields {
  492. if fields[i] != sopnode.FieldID {
  493. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  494. }
  495. }
  496. if snq.withSopStage != nil {
  497. _spec.Node.AddColumnOnce(sopnode.FieldStageID)
  498. }
  499. }
  500. if ps := snq.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 := snq.ctx.Limit; limit != nil {
  508. _spec.Limit = *limit
  509. }
  510. if offset := snq.ctx.Offset; offset != nil {
  511. _spec.Offset = *offset
  512. }
  513. if ps := snq.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 (snq *SopNodeQuery) sqlQuery(ctx context.Context) *sql.Selector {
  523. builder := sql.Dialect(snq.driver.Dialect())
  524. t1 := builder.Table(sopnode.Table)
  525. columns := snq.ctx.Fields
  526. if len(columns) == 0 {
  527. columns = sopnode.Columns
  528. }
  529. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  530. if snq.sql != nil {
  531. selector = snq.sql
  532. selector.Select(selector.Columns(columns...)...)
  533. }
  534. if snq.ctx.Unique != nil && *snq.ctx.Unique {
  535. selector.Distinct()
  536. }
  537. for _, p := range snq.predicates {
  538. p(selector)
  539. }
  540. for _, p := range snq.order {
  541. p(selector)
  542. }
  543. if offset := snq.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 := snq.ctx.Limit; limit != nil {
  549. selector.Limit(*limit)
  550. }
  551. return selector
  552. }
  553. // SopNodeGroupBy is the group-by builder for SopNode entities.
  554. type SopNodeGroupBy struct {
  555. selector
  556. build *SopNodeQuery
  557. }
  558. // Aggregate adds the given aggregation functions to the group-by query.
  559. func (sngb *SopNodeGroupBy) Aggregate(fns ...AggregateFunc) *SopNodeGroupBy {
  560. sngb.fns = append(sngb.fns, fns...)
  561. return sngb
  562. }
  563. // Scan applies the selector query and scans the result into the given value.
  564. func (sngb *SopNodeGroupBy) Scan(ctx context.Context, v any) error {
  565. ctx = setContextOp(ctx, sngb.build.ctx, "GroupBy")
  566. if err := sngb.build.prepareQuery(ctx); err != nil {
  567. return err
  568. }
  569. return scanWithInterceptors[*SopNodeQuery, *SopNodeGroupBy](ctx, sngb.build, sngb, sngb.build.inters, v)
  570. }
  571. func (sngb *SopNodeGroupBy) sqlScan(ctx context.Context, root *SopNodeQuery, v any) error {
  572. selector := root.sqlQuery(ctx).Select()
  573. aggregation := make([]string, 0, len(sngb.fns))
  574. for _, fn := range sngb.fns {
  575. aggregation = append(aggregation, fn(selector))
  576. }
  577. if len(selector.SelectedColumns()) == 0 {
  578. columns := make([]string, 0, len(*sngb.flds)+len(sngb.fns))
  579. for _, f := range *sngb.flds {
  580. columns = append(columns, selector.C(f))
  581. }
  582. columns = append(columns, aggregation...)
  583. selector.Select(columns...)
  584. }
  585. selector.GroupBy(selector.Columns(*sngb.flds...)...)
  586. if err := selector.Err(); err != nil {
  587. return err
  588. }
  589. rows := &sql.Rows{}
  590. query, args := selector.Query()
  591. if err := sngb.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. // SopNodeSelect is the builder for selecting fields of SopNode entities.
  598. type SopNodeSelect struct {
  599. *SopNodeQuery
  600. selector
  601. }
  602. // Aggregate adds the given aggregation functions to the selector query.
  603. func (sns *SopNodeSelect) Aggregate(fns ...AggregateFunc) *SopNodeSelect {
  604. sns.fns = append(sns.fns, fns...)
  605. return sns
  606. }
  607. // Scan applies the selector query and scans the result into the given value.
  608. func (sns *SopNodeSelect) Scan(ctx context.Context, v any) error {
  609. ctx = setContextOp(ctx, sns.ctx, "Select")
  610. if err := sns.prepareQuery(ctx); err != nil {
  611. return err
  612. }
  613. return scanWithInterceptors[*SopNodeQuery, *SopNodeSelect](ctx, sns.SopNodeQuery, sns, sns.inters, v)
  614. }
  615. func (sns *SopNodeSelect) sqlScan(ctx context.Context, root *SopNodeQuery, v any) error {
  616. selector := root.sqlQuery(ctx)
  617. aggregation := make([]string, 0, len(sns.fns))
  618. for _, fn := range sns.fns {
  619. aggregation = append(aggregation, fn(selector))
  620. }
  621. switch n := len(*sns.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 := sns.driver.Query(ctx, query, args, rows); err != nil {
  630. return err
  631. }
  632. defer rows.Close()
  633. return sql.ScanSlice(rows, v)
  634. }