label_query.go 17 KB

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