userattributedefinition_query.go 20 KB

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