group_query.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "database/sql/driver"
  6. "errors"
  7. "fmt"
  8. "math"
  9. "code.osinet.fr/fgm/go__ent_demo/ent/group"
  10. "code.osinet.fr/fgm/go__ent_demo/ent/predicate"
  11. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  12. "entgo.io/ent/dialect/sql"
  13. "entgo.io/ent/dialect/sql/sqlgraph"
  14. "entgo.io/ent/schema/field"
  15. )
  16. // GroupQuery is the builder for querying Group entities.
  17. type GroupQuery struct {
  18. config
  19. limit *int
  20. offset *int
  21. unique *bool
  22. order []OrderFunc
  23. fields []string
  24. predicates []predicate.Group
  25. // eager-loading edges.
  26. withUsers *UserQuery
  27. // intermediate query (i.e. traversal path).
  28. sql *sql.Selector
  29. path func(context.Context) (*sql.Selector, error)
  30. }
  31. // Where adds a new predicate for the GroupQuery builder.
  32. func (gq *GroupQuery) Where(ps ...predicate.Group) *GroupQuery {
  33. gq.predicates = append(gq.predicates, ps...)
  34. return gq
  35. }
  36. // Limit adds a limit step to the query.
  37. func (gq *GroupQuery) Limit(limit int) *GroupQuery {
  38. gq.limit = &limit
  39. return gq
  40. }
  41. // Offset adds an offset step to the query.
  42. func (gq *GroupQuery) Offset(offset int) *GroupQuery {
  43. gq.offset = &offset
  44. return gq
  45. }
  46. // Unique configures the query builder to filter duplicate records on query.
  47. // By default, unique is set to true, and can be disabled using this method.
  48. func (gq *GroupQuery) Unique(unique bool) *GroupQuery {
  49. gq.unique = &unique
  50. return gq
  51. }
  52. // Order adds an order step to the query.
  53. func (gq *GroupQuery) Order(o ...OrderFunc) *GroupQuery {
  54. gq.order = append(gq.order, o...)
  55. return gq
  56. }
  57. // QueryUsers chains the current query on the "users" edge.
  58. func (gq *GroupQuery) QueryUsers() *UserQuery {
  59. query := &UserQuery{config: gq.config}
  60. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  61. if err := gq.prepareQuery(ctx); err != nil {
  62. return nil, err
  63. }
  64. selector := gq.sqlQuery(ctx)
  65. if err := selector.Err(); err != nil {
  66. return nil, err
  67. }
  68. step := sqlgraph.NewStep(
  69. sqlgraph.From(group.Table, group.FieldID, selector),
  70. sqlgraph.To(user.Table, user.FieldID),
  71. sqlgraph.Edge(sqlgraph.M2M, false, group.UsersTable, group.UsersPrimaryKey...),
  72. )
  73. fromU = sqlgraph.SetNeighbors(gq.driver.Dialect(), step)
  74. return fromU, nil
  75. }
  76. return query
  77. }
  78. // First returns the first Group entity from the query.
  79. // Returns a *NotFoundError when no Group was found.
  80. func (gq *GroupQuery) First(ctx context.Context) (*Group, error) {
  81. nodes, err := gq.Limit(1).All(ctx)
  82. if err != nil {
  83. return nil, err
  84. }
  85. if len(nodes) == 0 {
  86. return nil, &NotFoundError{group.Label}
  87. }
  88. return nodes[0], nil
  89. }
  90. // FirstX is like First, but panics if an error occurs.
  91. func (gq *GroupQuery) FirstX(ctx context.Context) *Group {
  92. node, err := gq.First(ctx)
  93. if err != nil && !IsNotFound(err) {
  94. panic(err)
  95. }
  96. return node
  97. }
  98. // FirstID returns the first Group ID from the query.
  99. // Returns a *NotFoundError when no Group ID was found.
  100. func (gq *GroupQuery) FirstID(ctx context.Context) (id int, err error) {
  101. var ids []int
  102. if ids, err = gq.Limit(1).IDs(ctx); err != nil {
  103. return
  104. }
  105. if len(ids) == 0 {
  106. err = &NotFoundError{group.Label}
  107. return
  108. }
  109. return ids[0], nil
  110. }
  111. // FirstIDX is like FirstID, but panics if an error occurs.
  112. func (gq *GroupQuery) FirstIDX(ctx context.Context) int {
  113. id, err := gq.FirstID(ctx)
  114. if err != nil && !IsNotFound(err) {
  115. panic(err)
  116. }
  117. return id
  118. }
  119. // Only returns a single Group entity found by the query, ensuring it only returns one.
  120. // Returns a *NotSingularError when more than one Group entity is found.
  121. // Returns a *NotFoundError when no Group entities are found.
  122. func (gq *GroupQuery) Only(ctx context.Context) (*Group, error) {
  123. nodes, err := gq.Limit(2).All(ctx)
  124. if err != nil {
  125. return nil, err
  126. }
  127. switch len(nodes) {
  128. case 1:
  129. return nodes[0], nil
  130. case 0:
  131. return nil, &NotFoundError{group.Label}
  132. default:
  133. return nil, &NotSingularError{group.Label}
  134. }
  135. }
  136. // OnlyX is like Only, but panics if an error occurs.
  137. func (gq *GroupQuery) OnlyX(ctx context.Context) *Group {
  138. node, err := gq.Only(ctx)
  139. if err != nil {
  140. panic(err)
  141. }
  142. return node
  143. }
  144. // OnlyID is like Only, but returns the only Group ID in the query.
  145. // Returns a *NotSingularError when more than one Group ID is found.
  146. // Returns a *NotFoundError when no entities are found.
  147. func (gq *GroupQuery) OnlyID(ctx context.Context) (id int, err error) {
  148. var ids []int
  149. if ids, err = gq.Limit(2).IDs(ctx); err != nil {
  150. return
  151. }
  152. switch len(ids) {
  153. case 1:
  154. id = ids[0]
  155. case 0:
  156. err = &NotFoundError{group.Label}
  157. default:
  158. err = &NotSingularError{group.Label}
  159. }
  160. return
  161. }
  162. // OnlyIDX is like OnlyID, but panics if an error occurs.
  163. func (gq *GroupQuery) OnlyIDX(ctx context.Context) int {
  164. id, err := gq.OnlyID(ctx)
  165. if err != nil {
  166. panic(err)
  167. }
  168. return id
  169. }
  170. // All executes the query and returns a list of Groups.
  171. func (gq *GroupQuery) All(ctx context.Context) ([]*Group, error) {
  172. if err := gq.prepareQuery(ctx); err != nil {
  173. return nil, err
  174. }
  175. return gq.sqlAll(ctx)
  176. }
  177. // AllX is like All, but panics if an error occurs.
  178. func (gq *GroupQuery) AllX(ctx context.Context) []*Group {
  179. nodes, err := gq.All(ctx)
  180. if err != nil {
  181. panic(err)
  182. }
  183. return nodes
  184. }
  185. // IDs executes the query and returns a list of Group IDs.
  186. func (gq *GroupQuery) IDs(ctx context.Context) ([]int, error) {
  187. var ids []int
  188. if err := gq.Select(group.FieldID).Scan(ctx, &ids); err != nil {
  189. return nil, err
  190. }
  191. return ids, nil
  192. }
  193. // IDsX is like IDs, but panics if an error occurs.
  194. func (gq *GroupQuery) IDsX(ctx context.Context) []int {
  195. ids, err := gq.IDs(ctx)
  196. if err != nil {
  197. panic(err)
  198. }
  199. return ids
  200. }
  201. // Count returns the count of the given query.
  202. func (gq *GroupQuery) Count(ctx context.Context) (int, error) {
  203. if err := gq.prepareQuery(ctx); err != nil {
  204. return 0, err
  205. }
  206. return gq.sqlCount(ctx)
  207. }
  208. // CountX is like Count, but panics if an error occurs.
  209. func (gq *GroupQuery) CountX(ctx context.Context) int {
  210. count, err := gq.Count(ctx)
  211. if err != nil {
  212. panic(err)
  213. }
  214. return count
  215. }
  216. // Exist returns true if the query has elements in the graph.
  217. func (gq *GroupQuery) Exist(ctx context.Context) (bool, error) {
  218. if err := gq.prepareQuery(ctx); err != nil {
  219. return false, err
  220. }
  221. return gq.sqlExist(ctx)
  222. }
  223. // ExistX is like Exist, but panics if an error occurs.
  224. func (gq *GroupQuery) ExistX(ctx context.Context) bool {
  225. exist, err := gq.Exist(ctx)
  226. if err != nil {
  227. panic(err)
  228. }
  229. return exist
  230. }
  231. // Clone returns a duplicate of the GroupQuery builder, including all associated steps. It can be
  232. // used to prepare common query builders and use them differently after the clone is made.
  233. func (gq *GroupQuery) Clone() *GroupQuery {
  234. if gq == nil {
  235. return nil
  236. }
  237. return &GroupQuery{
  238. config: gq.config,
  239. limit: gq.limit,
  240. offset: gq.offset,
  241. order: append([]OrderFunc{}, gq.order...),
  242. predicates: append([]predicate.Group{}, gq.predicates...),
  243. withUsers: gq.withUsers.Clone(),
  244. // clone intermediate query.
  245. sql: gq.sql.Clone(),
  246. path: gq.path,
  247. unique: gq.unique,
  248. }
  249. }
  250. // WithUsers tells the query-builder to eager-load the nodes that are connected to
  251. // the "users" edge. The optional arguments are used to configure the query builder of the edge.
  252. func (gq *GroupQuery) WithUsers(opts ...func(*UserQuery)) *GroupQuery {
  253. query := &UserQuery{config: gq.config}
  254. for _, opt := range opts {
  255. opt(query)
  256. }
  257. gq.withUsers = query
  258. return gq
  259. }
  260. // GroupBy is used to group vertices by one or more fields/columns.
  261. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  262. //
  263. // Example:
  264. //
  265. // var v []struct {
  266. // Name string `json:"name,omitempty"`
  267. // Count int `json:"count,omitempty"`
  268. // }
  269. //
  270. // client.Group.Query().
  271. // GroupBy(group.FieldName).
  272. // Aggregate(ent.Count()).
  273. // Scan(ctx, &v)
  274. //
  275. func (gq *GroupQuery) GroupBy(field string, fields ...string) *GroupGroupBy {
  276. group := &GroupGroupBy{config: gq.config}
  277. group.fields = append([]string{field}, fields...)
  278. group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
  279. if err := gq.prepareQuery(ctx); err != nil {
  280. return nil, err
  281. }
  282. return gq.sqlQuery(ctx), nil
  283. }
  284. return group
  285. }
  286. // Select allows the selection one or more fields/columns for the given query,
  287. // instead of selecting all fields in the entity.
  288. //
  289. // Example:
  290. //
  291. // var v []struct {
  292. // Name string `json:"name,omitempty"`
  293. // }
  294. //
  295. // client.Group.Query().
  296. // Select(group.FieldName).
  297. // Scan(ctx, &v)
  298. //
  299. func (gq *GroupQuery) Select(fields ...string) *GroupSelect {
  300. gq.fields = append(gq.fields, fields...)
  301. return &GroupSelect{GroupQuery: gq}
  302. }
  303. func (gq *GroupQuery) prepareQuery(ctx context.Context) error {
  304. for _, f := range gq.fields {
  305. if !group.ValidColumn(f) {
  306. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  307. }
  308. }
  309. if gq.path != nil {
  310. prev, err := gq.path(ctx)
  311. if err != nil {
  312. return err
  313. }
  314. gq.sql = prev
  315. }
  316. return nil
  317. }
  318. func (gq *GroupQuery) sqlAll(ctx context.Context) ([]*Group, error) {
  319. var (
  320. nodes = []*Group{}
  321. _spec = gq.querySpec()
  322. loadedTypes = [1]bool{
  323. gq.withUsers != nil,
  324. }
  325. )
  326. _spec.ScanValues = func(columns []string) ([]interface{}, error) {
  327. node := &Group{config: gq.config}
  328. nodes = append(nodes, node)
  329. return node.scanValues(columns)
  330. }
  331. _spec.Assign = func(columns []string, values []interface{}) error {
  332. if len(nodes) == 0 {
  333. return fmt.Errorf("ent: Assign called without calling ScanValues")
  334. }
  335. node := nodes[len(nodes)-1]
  336. node.Edges.loadedTypes = loadedTypes
  337. return node.assignValues(columns, values)
  338. }
  339. if err := sqlgraph.QueryNodes(ctx, gq.driver, _spec); err != nil {
  340. return nil, err
  341. }
  342. if len(nodes) == 0 {
  343. return nodes, nil
  344. }
  345. if query := gq.withUsers; query != nil {
  346. fks := make([]driver.Value, 0, len(nodes))
  347. ids := make(map[int]*Group, len(nodes))
  348. for _, node := range nodes {
  349. ids[node.ID] = node
  350. fks = append(fks, node.ID)
  351. node.Edges.Users = []*User{}
  352. }
  353. var (
  354. edgeids []int
  355. edges = make(map[int][]*Group)
  356. )
  357. _spec := &sqlgraph.EdgeQuerySpec{
  358. Edge: &sqlgraph.EdgeSpec{
  359. Inverse: false,
  360. Table: group.UsersTable,
  361. Columns: group.UsersPrimaryKey,
  362. },
  363. Predicate: func(s *sql.Selector) {
  364. s.Where(sql.InValues(group.UsersPrimaryKey[0], fks...))
  365. },
  366. ScanValues: func() [2]interface{} {
  367. return [2]interface{}{new(sql.NullInt64), new(sql.NullInt64)}
  368. },
  369. Assign: func(out, in interface{}) error {
  370. eout, ok := out.(*sql.NullInt64)
  371. if !ok || eout == nil {
  372. return fmt.Errorf("unexpected id value for edge-out")
  373. }
  374. ein, ok := in.(*sql.NullInt64)
  375. if !ok || ein == nil {
  376. return fmt.Errorf("unexpected id value for edge-in")
  377. }
  378. outValue := int(eout.Int64)
  379. inValue := int(ein.Int64)
  380. node, ok := ids[outValue]
  381. if !ok {
  382. return fmt.Errorf("unexpected node id in edges: %v", outValue)
  383. }
  384. if _, ok := edges[inValue]; !ok {
  385. edgeids = append(edgeids, inValue)
  386. }
  387. edges[inValue] = append(edges[inValue], node)
  388. return nil
  389. },
  390. }
  391. if err := sqlgraph.QueryEdges(ctx, gq.driver, _spec); err != nil {
  392. return nil, fmt.Errorf(`query edges "users": %w`, err)
  393. }
  394. query.Where(user.IDIn(edgeids...))
  395. neighbors, err := query.All(ctx)
  396. if err != nil {
  397. return nil, err
  398. }
  399. for _, n := range neighbors {
  400. nodes, ok := edges[n.ID]
  401. if !ok {
  402. return nil, fmt.Errorf(`unexpected "users" node returned %v`, n.ID)
  403. }
  404. for i := range nodes {
  405. nodes[i].Edges.Users = append(nodes[i].Edges.Users, n)
  406. }
  407. }
  408. }
  409. return nodes, nil
  410. }
  411. func (gq *GroupQuery) sqlCount(ctx context.Context) (int, error) {
  412. _spec := gq.querySpec()
  413. _spec.Node.Columns = gq.fields
  414. if len(gq.fields) > 0 {
  415. _spec.Unique = gq.unique != nil && *gq.unique
  416. }
  417. return sqlgraph.CountNodes(ctx, gq.driver, _spec)
  418. }
  419. func (gq *GroupQuery) sqlExist(ctx context.Context) (bool, error) {
  420. n, err := gq.sqlCount(ctx)
  421. if err != nil {
  422. return false, fmt.Errorf("ent: check existence: %w", err)
  423. }
  424. return n > 0, nil
  425. }
  426. func (gq *GroupQuery) querySpec() *sqlgraph.QuerySpec {
  427. _spec := &sqlgraph.QuerySpec{
  428. Node: &sqlgraph.NodeSpec{
  429. Table: group.Table,
  430. Columns: group.Columns,
  431. ID: &sqlgraph.FieldSpec{
  432. Type: field.TypeInt,
  433. Column: group.FieldID,
  434. },
  435. },
  436. From: gq.sql,
  437. Unique: true,
  438. }
  439. if unique := gq.unique; unique != nil {
  440. _spec.Unique = *unique
  441. }
  442. if fields := gq.fields; len(fields) > 0 {
  443. _spec.Node.Columns = make([]string, 0, len(fields))
  444. _spec.Node.Columns = append(_spec.Node.Columns, group.FieldID)
  445. for i := range fields {
  446. if fields[i] != group.FieldID {
  447. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  448. }
  449. }
  450. }
  451. if ps := gq.predicates; len(ps) > 0 {
  452. _spec.Predicate = func(selector *sql.Selector) {
  453. for i := range ps {
  454. ps[i](selector)
  455. }
  456. }
  457. }
  458. if limit := gq.limit; limit != nil {
  459. _spec.Limit = *limit
  460. }
  461. if offset := gq.offset; offset != nil {
  462. _spec.Offset = *offset
  463. }
  464. if ps := gq.order; len(ps) > 0 {
  465. _spec.Order = func(selector *sql.Selector) {
  466. for i := range ps {
  467. ps[i](selector)
  468. }
  469. }
  470. }
  471. return _spec
  472. }
  473. func (gq *GroupQuery) sqlQuery(ctx context.Context) *sql.Selector {
  474. builder := sql.Dialect(gq.driver.Dialect())
  475. t1 := builder.Table(group.Table)
  476. columns := gq.fields
  477. if len(columns) == 0 {
  478. columns = group.Columns
  479. }
  480. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  481. if gq.sql != nil {
  482. selector = gq.sql
  483. selector.Select(selector.Columns(columns...)...)
  484. }
  485. if gq.unique != nil && *gq.unique {
  486. selector.Distinct()
  487. }
  488. for _, p := range gq.predicates {
  489. p(selector)
  490. }
  491. for _, p := range gq.order {
  492. p(selector)
  493. }
  494. if offset := gq.offset; offset != nil {
  495. // limit is mandatory for offset clause. We start
  496. // with default value, and override it below if needed.
  497. selector.Offset(*offset).Limit(math.MaxInt32)
  498. }
  499. if limit := gq.limit; limit != nil {
  500. selector.Limit(*limit)
  501. }
  502. return selector
  503. }
  504. // GroupGroupBy is the group-by builder for Group entities.
  505. type GroupGroupBy struct {
  506. config
  507. fields []string
  508. fns []AggregateFunc
  509. // intermediate query (i.e. traversal path).
  510. sql *sql.Selector
  511. path func(context.Context) (*sql.Selector, error)
  512. }
  513. // Aggregate adds the given aggregation functions to the group-by query.
  514. func (ggb *GroupGroupBy) Aggregate(fns ...AggregateFunc) *GroupGroupBy {
  515. ggb.fns = append(ggb.fns, fns...)
  516. return ggb
  517. }
  518. // Scan applies the group-by query and scans the result into the given value.
  519. func (ggb *GroupGroupBy) Scan(ctx context.Context, v interface{}) error {
  520. query, err := ggb.path(ctx)
  521. if err != nil {
  522. return err
  523. }
  524. ggb.sql = query
  525. return ggb.sqlScan(ctx, v)
  526. }
  527. // ScanX is like Scan, but panics if an error occurs.
  528. func (ggb *GroupGroupBy) ScanX(ctx context.Context, v interface{}) {
  529. if err := ggb.Scan(ctx, v); err != nil {
  530. panic(err)
  531. }
  532. }
  533. // Strings returns list of strings from group-by.
  534. // It is only allowed when executing a group-by query with one field.
  535. func (ggb *GroupGroupBy) Strings(ctx context.Context) ([]string, error) {
  536. if len(ggb.fields) > 1 {
  537. return nil, errors.New("ent: GroupGroupBy.Strings is not achievable when grouping more than 1 field")
  538. }
  539. var v []string
  540. if err := ggb.Scan(ctx, &v); err != nil {
  541. return nil, err
  542. }
  543. return v, nil
  544. }
  545. // StringsX is like Strings, but panics if an error occurs.
  546. func (ggb *GroupGroupBy) StringsX(ctx context.Context) []string {
  547. v, err := ggb.Strings(ctx)
  548. if err != nil {
  549. panic(err)
  550. }
  551. return v
  552. }
  553. // String returns a single string from a group-by query.
  554. // It is only allowed when executing a group-by query with one field.
  555. func (ggb *GroupGroupBy) String(ctx context.Context) (_ string, err error) {
  556. var v []string
  557. if v, err = ggb.Strings(ctx); err != nil {
  558. return
  559. }
  560. switch len(v) {
  561. case 1:
  562. return v[0], nil
  563. case 0:
  564. err = &NotFoundError{group.Label}
  565. default:
  566. err = fmt.Errorf("ent: GroupGroupBy.Strings returned %d results when one was expected", len(v))
  567. }
  568. return
  569. }
  570. // StringX is like String, but panics if an error occurs.
  571. func (ggb *GroupGroupBy) StringX(ctx context.Context) string {
  572. v, err := ggb.String(ctx)
  573. if err != nil {
  574. panic(err)
  575. }
  576. return v
  577. }
  578. // Ints returns list of ints from group-by.
  579. // It is only allowed when executing a group-by query with one field.
  580. func (ggb *GroupGroupBy) Ints(ctx context.Context) ([]int, error) {
  581. if len(ggb.fields) > 1 {
  582. return nil, errors.New("ent: GroupGroupBy.Ints is not achievable when grouping more than 1 field")
  583. }
  584. var v []int
  585. if err := ggb.Scan(ctx, &v); err != nil {
  586. return nil, err
  587. }
  588. return v, nil
  589. }
  590. // IntsX is like Ints, but panics if an error occurs.
  591. func (ggb *GroupGroupBy) IntsX(ctx context.Context) []int {
  592. v, err := ggb.Ints(ctx)
  593. if err != nil {
  594. panic(err)
  595. }
  596. return v
  597. }
  598. // Int returns a single int from a group-by query.
  599. // It is only allowed when executing a group-by query with one field.
  600. func (ggb *GroupGroupBy) Int(ctx context.Context) (_ int, err error) {
  601. var v []int
  602. if v, err = ggb.Ints(ctx); err != nil {
  603. return
  604. }
  605. switch len(v) {
  606. case 1:
  607. return v[0], nil
  608. case 0:
  609. err = &NotFoundError{group.Label}
  610. default:
  611. err = fmt.Errorf("ent: GroupGroupBy.Ints returned %d results when one was expected", len(v))
  612. }
  613. return
  614. }
  615. // IntX is like Int, but panics if an error occurs.
  616. func (ggb *GroupGroupBy) IntX(ctx context.Context) int {
  617. v, err := ggb.Int(ctx)
  618. if err != nil {
  619. panic(err)
  620. }
  621. return v
  622. }
  623. // Float64s returns list of float64s from group-by.
  624. // It is only allowed when executing a group-by query with one field.
  625. func (ggb *GroupGroupBy) Float64s(ctx context.Context) ([]float64, error) {
  626. if len(ggb.fields) > 1 {
  627. return nil, errors.New("ent: GroupGroupBy.Float64s is not achievable when grouping more than 1 field")
  628. }
  629. var v []float64
  630. if err := ggb.Scan(ctx, &v); err != nil {
  631. return nil, err
  632. }
  633. return v, nil
  634. }
  635. // Float64sX is like Float64s, but panics if an error occurs.
  636. func (ggb *GroupGroupBy) Float64sX(ctx context.Context) []float64 {
  637. v, err := ggb.Float64s(ctx)
  638. if err != nil {
  639. panic(err)
  640. }
  641. return v
  642. }
  643. // Float64 returns a single float64 from a group-by query.
  644. // It is only allowed when executing a group-by query with one field.
  645. func (ggb *GroupGroupBy) Float64(ctx context.Context) (_ float64, err error) {
  646. var v []float64
  647. if v, err = ggb.Float64s(ctx); err != nil {
  648. return
  649. }
  650. switch len(v) {
  651. case 1:
  652. return v[0], nil
  653. case 0:
  654. err = &NotFoundError{group.Label}
  655. default:
  656. err = fmt.Errorf("ent: GroupGroupBy.Float64s returned %d results when one was expected", len(v))
  657. }
  658. return
  659. }
  660. // Float64X is like Float64, but panics if an error occurs.
  661. func (ggb *GroupGroupBy) Float64X(ctx context.Context) float64 {
  662. v, err := ggb.Float64(ctx)
  663. if err != nil {
  664. panic(err)
  665. }
  666. return v
  667. }
  668. // Bools returns list of bools from group-by.
  669. // It is only allowed when executing a group-by query with one field.
  670. func (ggb *GroupGroupBy) Bools(ctx context.Context) ([]bool, error) {
  671. if len(ggb.fields) > 1 {
  672. return nil, errors.New("ent: GroupGroupBy.Bools is not achievable when grouping more than 1 field")
  673. }
  674. var v []bool
  675. if err := ggb.Scan(ctx, &v); err != nil {
  676. return nil, err
  677. }
  678. return v, nil
  679. }
  680. // BoolsX is like Bools, but panics if an error occurs.
  681. func (ggb *GroupGroupBy) BoolsX(ctx context.Context) []bool {
  682. v, err := ggb.Bools(ctx)
  683. if err != nil {
  684. panic(err)
  685. }
  686. return v
  687. }
  688. // Bool returns a single bool from a group-by query.
  689. // It is only allowed when executing a group-by query with one field.
  690. func (ggb *GroupGroupBy) Bool(ctx context.Context) (_ bool, err error) {
  691. var v []bool
  692. if v, err = ggb.Bools(ctx); err != nil {
  693. return
  694. }
  695. switch len(v) {
  696. case 1:
  697. return v[0], nil
  698. case 0:
  699. err = &NotFoundError{group.Label}
  700. default:
  701. err = fmt.Errorf("ent: GroupGroupBy.Bools returned %d results when one was expected", len(v))
  702. }
  703. return
  704. }
  705. // BoolX is like Bool, but panics if an error occurs.
  706. func (ggb *GroupGroupBy) BoolX(ctx context.Context) bool {
  707. v, err := ggb.Bool(ctx)
  708. if err != nil {
  709. panic(err)
  710. }
  711. return v
  712. }
  713. func (ggb *GroupGroupBy) sqlScan(ctx context.Context, v interface{}) error {
  714. for _, f := range ggb.fields {
  715. if !group.ValidColumn(f) {
  716. return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
  717. }
  718. }
  719. selector := ggb.sqlQuery()
  720. if err := selector.Err(); err != nil {
  721. return err
  722. }
  723. rows := &sql.Rows{}
  724. query, args := selector.Query()
  725. if err := ggb.driver.Query(ctx, query, args, rows); err != nil {
  726. return err
  727. }
  728. defer rows.Close()
  729. return sql.ScanSlice(rows, v)
  730. }
  731. func (ggb *GroupGroupBy) sqlQuery() *sql.Selector {
  732. selector := ggb.sql.Select()
  733. aggregation := make([]string, 0, len(ggb.fns))
  734. for _, fn := range ggb.fns {
  735. aggregation = append(aggregation, fn(selector))
  736. }
  737. // If no columns were selected in a custom aggregation function, the default
  738. // selection is the fields used for "group-by", and the aggregation functions.
  739. if len(selector.SelectedColumns()) == 0 {
  740. columns := make([]string, 0, len(ggb.fields)+len(ggb.fns))
  741. for _, f := range ggb.fields {
  742. columns = append(columns, selector.C(f))
  743. }
  744. columns = append(columns, aggregation...)
  745. selector.Select(columns...)
  746. }
  747. return selector.GroupBy(selector.Columns(ggb.fields...)...)
  748. }
  749. // GroupSelect is the builder for selecting fields of Group entities.
  750. type GroupSelect struct {
  751. *GroupQuery
  752. // intermediate query (i.e. traversal path).
  753. sql *sql.Selector
  754. }
  755. // Scan applies the selector query and scans the result into the given value.
  756. func (gs *GroupSelect) Scan(ctx context.Context, v interface{}) error {
  757. if err := gs.prepareQuery(ctx); err != nil {
  758. return err
  759. }
  760. gs.sql = gs.GroupQuery.sqlQuery(ctx)
  761. return gs.sqlScan(ctx, v)
  762. }
  763. // ScanX is like Scan, but panics if an error occurs.
  764. func (gs *GroupSelect) ScanX(ctx context.Context, v interface{}) {
  765. if err := gs.Scan(ctx, v); err != nil {
  766. panic(err)
  767. }
  768. }
  769. // Strings returns list of strings from a selector. It is only allowed when selecting one field.
  770. func (gs *GroupSelect) Strings(ctx context.Context) ([]string, error) {
  771. if len(gs.fields) > 1 {
  772. return nil, errors.New("ent: GroupSelect.Strings is not achievable when selecting more than 1 field")
  773. }
  774. var v []string
  775. if err := gs.Scan(ctx, &v); err != nil {
  776. return nil, err
  777. }
  778. return v, nil
  779. }
  780. // StringsX is like Strings, but panics if an error occurs.
  781. func (gs *GroupSelect) StringsX(ctx context.Context) []string {
  782. v, err := gs.Strings(ctx)
  783. if err != nil {
  784. panic(err)
  785. }
  786. return v
  787. }
  788. // String returns a single string from a selector. It is only allowed when selecting one field.
  789. func (gs *GroupSelect) String(ctx context.Context) (_ string, err error) {
  790. var v []string
  791. if v, err = gs.Strings(ctx); err != nil {
  792. return
  793. }
  794. switch len(v) {
  795. case 1:
  796. return v[0], nil
  797. case 0:
  798. err = &NotFoundError{group.Label}
  799. default:
  800. err = fmt.Errorf("ent: GroupSelect.Strings returned %d results when one was expected", len(v))
  801. }
  802. return
  803. }
  804. // StringX is like String, but panics if an error occurs.
  805. func (gs *GroupSelect) StringX(ctx context.Context) string {
  806. v, err := gs.String(ctx)
  807. if err != nil {
  808. panic(err)
  809. }
  810. return v
  811. }
  812. // Ints returns list of ints from a selector. It is only allowed when selecting one field.
  813. func (gs *GroupSelect) Ints(ctx context.Context) ([]int, error) {
  814. if len(gs.fields) > 1 {
  815. return nil, errors.New("ent: GroupSelect.Ints is not achievable when selecting more than 1 field")
  816. }
  817. var v []int
  818. if err := gs.Scan(ctx, &v); err != nil {
  819. return nil, err
  820. }
  821. return v, nil
  822. }
  823. // IntsX is like Ints, but panics if an error occurs.
  824. func (gs *GroupSelect) IntsX(ctx context.Context) []int {
  825. v, err := gs.Ints(ctx)
  826. if err != nil {
  827. panic(err)
  828. }
  829. return v
  830. }
  831. // Int returns a single int from a selector. It is only allowed when selecting one field.
  832. func (gs *GroupSelect) Int(ctx context.Context) (_ int, err error) {
  833. var v []int
  834. if v, err = gs.Ints(ctx); err != nil {
  835. return
  836. }
  837. switch len(v) {
  838. case 1:
  839. return v[0], nil
  840. case 0:
  841. err = &NotFoundError{group.Label}
  842. default:
  843. err = fmt.Errorf("ent: GroupSelect.Ints returned %d results when one was expected", len(v))
  844. }
  845. return
  846. }
  847. // IntX is like Int, but panics if an error occurs.
  848. func (gs *GroupSelect) IntX(ctx context.Context) int {
  849. v, err := gs.Int(ctx)
  850. if err != nil {
  851. panic(err)
  852. }
  853. return v
  854. }
  855. // Float64s returns list of float64s from a selector. It is only allowed when selecting one field.
  856. func (gs *GroupSelect) Float64s(ctx context.Context) ([]float64, error) {
  857. if len(gs.fields) > 1 {
  858. return nil, errors.New("ent: GroupSelect.Float64s is not achievable when selecting more than 1 field")
  859. }
  860. var v []float64
  861. if err := gs.Scan(ctx, &v); err != nil {
  862. return nil, err
  863. }
  864. return v, nil
  865. }
  866. // Float64sX is like Float64s, but panics if an error occurs.
  867. func (gs *GroupSelect) Float64sX(ctx context.Context) []float64 {
  868. v, err := gs.Float64s(ctx)
  869. if err != nil {
  870. panic(err)
  871. }
  872. return v
  873. }
  874. // Float64 returns a single float64 from a selector. It is only allowed when selecting one field.
  875. func (gs *GroupSelect) Float64(ctx context.Context) (_ float64, err error) {
  876. var v []float64
  877. if v, err = gs.Float64s(ctx); err != nil {
  878. return
  879. }
  880. switch len(v) {
  881. case 1:
  882. return v[0], nil
  883. case 0:
  884. err = &NotFoundError{group.Label}
  885. default:
  886. err = fmt.Errorf("ent: GroupSelect.Float64s returned %d results when one was expected", len(v))
  887. }
  888. return
  889. }
  890. // Float64X is like Float64, but panics if an error occurs.
  891. func (gs *GroupSelect) Float64X(ctx context.Context) float64 {
  892. v, err := gs.Float64(ctx)
  893. if err != nil {
  894. panic(err)
  895. }
  896. return v
  897. }
  898. // Bools returns list of bools from a selector. It is only allowed when selecting one field.
  899. func (gs *GroupSelect) Bools(ctx context.Context) ([]bool, error) {
  900. if len(gs.fields) > 1 {
  901. return nil, errors.New("ent: GroupSelect.Bools is not achievable when selecting more than 1 field")
  902. }
  903. var v []bool
  904. if err := gs.Scan(ctx, &v); err != nil {
  905. return nil, err
  906. }
  907. return v, nil
  908. }
  909. // BoolsX is like Bools, but panics if an error occurs.
  910. func (gs *GroupSelect) BoolsX(ctx context.Context) []bool {
  911. v, err := gs.Bools(ctx)
  912. if err != nil {
  913. panic(err)
  914. }
  915. return v
  916. }
  917. // Bool returns a single bool from a selector. It is only allowed when selecting one field.
  918. func (gs *GroupSelect) Bool(ctx context.Context) (_ bool, err error) {
  919. var v []bool
  920. if v, err = gs.Bools(ctx); err != nil {
  921. return
  922. }
  923. switch len(v) {
  924. case 1:
  925. return v[0], nil
  926. case 0:
  927. err = &NotFoundError{group.Label}
  928. default:
  929. err = fmt.Errorf("ent: GroupSelect.Bools returned %d results when one was expected", len(v))
  930. }
  931. return
  932. }
  933. // BoolX is like Bool, but panics if an error occurs.
  934. func (gs *GroupSelect) BoolX(ctx context.Context) bool {
  935. v, err := gs.Bool(ctx)
  936. if err != nil {
  937. panic(err)
  938. }
  939. return v
  940. }
  941. func (gs *GroupSelect) sqlScan(ctx context.Context, v interface{}) error {
  942. rows := &sql.Rows{}
  943. query, args := gs.sql.Query()
  944. if err := gs.driver.Query(ctx, query, args, rows); err != nil {
  945. return err
  946. }
  947. defer rows.Close()
  948. return sql.ScanSlice(rows, v)
  949. }