user_query.go 28 KB

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