labelrelationship_query.go 21 KB

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