user_query.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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/entdemo/ent/car"
  10. "code.osinet.fr/fgm/entdemo/ent/group"
  11. "code.osinet.fr/fgm/entdemo/ent/predicate"
  12. "code.osinet.fr/fgm/entdemo/ent/user"
  13. "github.com/facebookincubator/ent/dialect/sql"
  14. "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
  15. "github.com/facebookincubator/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. order []Order
  23. unique []string
  24. predicates []predicate.User
  25. // eager-loading edges.
  26. withCars *CarQuery
  27. withGroups *GroupQuery
  28. // intermediate query.
  29. sql *sql.Selector
  30. }
  31. // Where adds a new predicate for the builder.
  32. func (uq *UserQuery) Where(ps ...predicate.User) *UserQuery {
  33. uq.predicates = append(uq.predicates, ps...)
  34. return uq
  35. }
  36. // Limit adds a limit step to the query.
  37. func (uq *UserQuery) Limit(limit int) *UserQuery {
  38. uq.limit = &limit
  39. return uq
  40. }
  41. // Offset adds an offset step to the query.
  42. func (uq *UserQuery) Offset(offset int) *UserQuery {
  43. uq.offset = &offset
  44. return uq
  45. }
  46. // Order adds an order step to the query.
  47. func (uq *UserQuery) Order(o ...Order) *UserQuery {
  48. uq.order = append(uq.order, o...)
  49. return uq
  50. }
  51. // QueryCars chains the current query on the cars edge.
  52. func (uq *UserQuery) QueryCars() *CarQuery {
  53. query := &CarQuery{config: uq.config}
  54. step := sqlgraph.NewStep(
  55. sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
  56. sqlgraph.To(car.Table, car.FieldID),
  57. sqlgraph.Edge(sqlgraph.O2M, false, user.CarsTable, user.CarsColumn),
  58. )
  59. query.sql = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
  60. return query
  61. }
  62. // QueryGroups chains the current query on the groups edge.
  63. func (uq *UserQuery) QueryGroups() *GroupQuery {
  64. query := &GroupQuery{config: uq.config}
  65. step := sqlgraph.NewStep(
  66. sqlgraph.From(user.Table, user.FieldID, uq.sqlQuery()),
  67. sqlgraph.To(group.Table, group.FieldID),
  68. sqlgraph.Edge(sqlgraph.M2M, true, user.GroupsTable, user.GroupsPrimaryKey...),
  69. )
  70. query.sql = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
  71. return query
  72. }
  73. // First returns the first User entity in the query. Returns *NotFoundError when no user was found.
  74. func (uq *UserQuery) First(ctx context.Context) (*User, error) {
  75. us, err := uq.Limit(1).All(ctx)
  76. if err != nil {
  77. return nil, err
  78. }
  79. if len(us) == 0 {
  80. return nil, &NotFoundError{user.Label}
  81. }
  82. return us[0], nil
  83. }
  84. // FirstX is like First, but panics if an error occurs.
  85. func (uq *UserQuery) FirstX(ctx context.Context) *User {
  86. u, err := uq.First(ctx)
  87. if err != nil && !IsNotFound(err) {
  88. panic(err)
  89. }
  90. return u
  91. }
  92. // FirstID returns the first User id in the query. Returns *NotFoundError when no id was found.
  93. func (uq *UserQuery) FirstID(ctx context.Context) (id int, err error) {
  94. var ids []int
  95. if ids, err = uq.Limit(1).IDs(ctx); err != nil {
  96. return
  97. }
  98. if len(ids) == 0 {
  99. err = &NotFoundError{user.Label}
  100. return
  101. }
  102. return ids[0], nil
  103. }
  104. // FirstXID is like FirstID, but panics if an error occurs.
  105. func (uq *UserQuery) FirstXID(ctx context.Context) int {
  106. id, err := uq.FirstID(ctx)
  107. if err != nil && !IsNotFound(err) {
  108. panic(err)
  109. }
  110. return id
  111. }
  112. // Only returns the only User entity in the query, returns an error if not exactly one entity was returned.
  113. func (uq *UserQuery) Only(ctx context.Context) (*User, error) {
  114. us, err := uq.Limit(2).All(ctx)
  115. if err != nil {
  116. return nil, err
  117. }
  118. switch len(us) {
  119. case 1:
  120. return us[0], nil
  121. case 0:
  122. return nil, &NotFoundError{user.Label}
  123. default:
  124. return nil, &NotSingularError{user.Label}
  125. }
  126. }
  127. // OnlyX is like Only, but panics if an error occurs.
  128. func (uq *UserQuery) OnlyX(ctx context.Context) *User {
  129. u, err := uq.Only(ctx)
  130. if err != nil {
  131. panic(err)
  132. }
  133. return u
  134. }
  135. // OnlyID returns the only User id in the query, returns an error if not exactly one id was returned.
  136. func (uq *UserQuery) OnlyID(ctx context.Context) (id int, err error) {
  137. var ids []int
  138. if ids, err = uq.Limit(2).IDs(ctx); err != nil {
  139. return
  140. }
  141. switch len(ids) {
  142. case 1:
  143. id = ids[0]
  144. case 0:
  145. err = &NotFoundError{user.Label}
  146. default:
  147. err = &NotSingularError{user.Label}
  148. }
  149. return
  150. }
  151. // OnlyXID is like OnlyID, but panics if an error occurs.
  152. func (uq *UserQuery) OnlyXID(ctx context.Context) int {
  153. id, err := uq.OnlyID(ctx)
  154. if err != nil {
  155. panic(err)
  156. }
  157. return id
  158. }
  159. // All executes the query and returns a list of Users.
  160. func (uq *UserQuery) All(ctx context.Context) ([]*User, error) {
  161. return uq.sqlAll(ctx)
  162. }
  163. // AllX is like All, but panics if an error occurs.
  164. func (uq *UserQuery) AllX(ctx context.Context) []*User {
  165. us, err := uq.All(ctx)
  166. if err != nil {
  167. panic(err)
  168. }
  169. return us
  170. }
  171. // IDs executes the query and returns a list of User ids.
  172. func (uq *UserQuery) IDs(ctx context.Context) ([]int, error) {
  173. var ids []int
  174. if err := uq.Select(user.FieldID).Scan(ctx, &ids); err != nil {
  175. return nil, err
  176. }
  177. return ids, nil
  178. }
  179. // IDsX is like IDs, but panics if an error occurs.
  180. func (uq *UserQuery) IDsX(ctx context.Context) []int {
  181. ids, err := uq.IDs(ctx)
  182. if err != nil {
  183. panic(err)
  184. }
  185. return ids
  186. }
  187. // Count returns the count of the given query.
  188. func (uq *UserQuery) Count(ctx context.Context) (int, error) {
  189. return uq.sqlCount(ctx)
  190. }
  191. // CountX is like Count, but panics if an error occurs.
  192. func (uq *UserQuery) CountX(ctx context.Context) int {
  193. count, err := uq.Count(ctx)
  194. if err != nil {
  195. panic(err)
  196. }
  197. return count
  198. }
  199. // Exist returns true if the query has elements in the graph.
  200. func (uq *UserQuery) Exist(ctx context.Context) (bool, error) {
  201. return uq.sqlExist(ctx)
  202. }
  203. // ExistX is like Exist, but panics if an error occurs.
  204. func (uq *UserQuery) ExistX(ctx context.Context) bool {
  205. exist, err := uq.Exist(ctx)
  206. if err != nil {
  207. panic(err)
  208. }
  209. return exist
  210. }
  211. // Clone returns a duplicate of the query builder, including all associated steps. It can be
  212. // used to prepare common query builders and use them differently after the clone is made.
  213. func (uq *UserQuery) Clone() *UserQuery {
  214. return &UserQuery{
  215. config: uq.config,
  216. limit: uq.limit,
  217. offset: uq.offset,
  218. order: append([]Order{}, uq.order...),
  219. unique: append([]string{}, uq.unique...),
  220. predicates: append([]predicate.User{}, uq.predicates...),
  221. // clone intermediate query.
  222. sql: uq.sql.Clone(),
  223. }
  224. }
  225. // WithCars tells the query-builder to eager-loads the nodes that are connected to
  226. // the "cars" edge. The optional arguments used to configure the query builder of the edge.
  227. func (uq *UserQuery) WithCars(opts ...func(*CarQuery)) *UserQuery {
  228. query := &CarQuery{config: uq.config}
  229. for _, opt := range opts {
  230. opt(query)
  231. }
  232. uq.withCars = query
  233. return uq
  234. }
  235. // WithGroups tells the query-builder to eager-loads the nodes that are connected to
  236. // the "groups" edge. The optional arguments used to configure the query builder of the edge.
  237. func (uq *UserQuery) WithGroups(opts ...func(*GroupQuery)) *UserQuery {
  238. query := &GroupQuery{config: uq.config}
  239. for _, opt := range opts {
  240. opt(query)
  241. }
  242. uq.withGroups = query
  243. return uq
  244. }
  245. // GroupBy used to group vertices by one or more fields/columns.
  246. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  247. //
  248. // Example:
  249. //
  250. // var v []struct {
  251. // Age int `json:"age,omitempty"`
  252. // Count int `json:"count,omitempty"`
  253. // }
  254. //
  255. // client.User.Query().
  256. // GroupBy(user.FieldAge).
  257. // Aggregate(ent.Count()).
  258. // Scan(ctx, &v)
  259. //
  260. func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
  261. group := &UserGroupBy{config: uq.config}
  262. group.fields = append([]string{field}, fields...)
  263. group.sql = uq.sqlQuery()
  264. return group
  265. }
  266. // Select one or more fields from the given query.
  267. //
  268. // Example:
  269. //
  270. // var v []struct {
  271. // Age int `json:"age,omitempty"`
  272. // }
  273. //
  274. // client.User.Query().
  275. // Select(user.FieldAge).
  276. // Scan(ctx, &v)
  277. //
  278. func (uq *UserQuery) Select(field string, fields ...string) *UserSelect {
  279. selector := &UserSelect{config: uq.config}
  280. selector.fields = append([]string{field}, fields...)
  281. selector.sql = uq.sqlQuery()
  282. return selector
  283. }
  284. func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
  285. var (
  286. nodes = []*User{}
  287. _spec = uq.querySpec()
  288. loadedTypes = [2]bool{
  289. uq.withCars != nil,
  290. uq.withGroups != nil,
  291. }
  292. )
  293. _spec.ScanValues = func() []interface{} {
  294. node := &User{config: uq.config}
  295. nodes = append(nodes, node)
  296. values := node.scanValues()
  297. return values
  298. }
  299. _spec.Assign = func(values ...interface{}) error {
  300. if len(nodes) == 0 {
  301. return fmt.Errorf("ent: Assign called without calling ScanValues")
  302. }
  303. node := nodes[len(nodes)-1]
  304. node.Edges.loadedTypes = loadedTypes
  305. return node.assignValues(values...)
  306. }
  307. if err := sqlgraph.QueryNodes(ctx, uq.driver, _spec); err != nil {
  308. return nil, err
  309. }
  310. if len(nodes) == 0 {
  311. return nodes, nil
  312. }
  313. if query := uq.withCars; query != nil {
  314. fks := make([]driver.Value, 0, len(nodes))
  315. nodeids := make(map[int]*User)
  316. for i := range nodes {
  317. fks = append(fks, nodes[i].ID)
  318. nodeids[nodes[i].ID] = nodes[i]
  319. }
  320. query.withFKs = true
  321. query.Where(predicate.Car(func(s *sql.Selector) {
  322. s.Where(sql.InValues(user.CarsColumn, fks...))
  323. }))
  324. neighbors, err := query.All(ctx)
  325. if err != nil {
  326. return nil, err
  327. }
  328. for _, n := range neighbors {
  329. fk := n.user_cars
  330. if fk == nil {
  331. return nil, fmt.Errorf(`foreign-key "user_cars" is nil for node %v`, n.ID)
  332. }
  333. node, ok := nodeids[*fk]
  334. if !ok {
  335. return nil, fmt.Errorf(`unexpected foreign-key "user_cars" returned %v for node %v`, *fk, n.ID)
  336. }
  337. node.Edges.Cars = append(node.Edges.Cars, n)
  338. }
  339. }
  340. if query := uq.withGroups; query != nil {
  341. fks := make([]driver.Value, 0, len(nodes))
  342. ids := make(map[int]*User, len(nodes))
  343. for _, node := range nodes {
  344. ids[node.ID] = node
  345. fks = append(fks, node.ID)
  346. }
  347. var (
  348. edgeids []int
  349. edges = make(map[int][]*User)
  350. )
  351. _spec := &sqlgraph.EdgeQuerySpec{
  352. Edge: &sqlgraph.EdgeSpec{
  353. Inverse: true,
  354. Table: user.GroupsTable,
  355. Columns: user.GroupsPrimaryKey,
  356. },
  357. Predicate: func(s *sql.Selector) {
  358. s.Where(sql.InValues(user.GroupsPrimaryKey[1], fks...))
  359. },
  360. ScanValues: func() [2]interface{} {
  361. return [2]interface{}{&sql.NullInt64{}, &sql.NullInt64{}}
  362. },
  363. Assign: func(out, in interface{}) error {
  364. eout, ok := out.(*sql.NullInt64)
  365. if !ok || eout == nil {
  366. return fmt.Errorf("unexpected id value for edge-out")
  367. }
  368. ein, ok := in.(*sql.NullInt64)
  369. if !ok || ein == nil {
  370. return fmt.Errorf("unexpected id value for edge-in")
  371. }
  372. outValue := int(eout.Int64)
  373. inValue := int(ein.Int64)
  374. node, ok := ids[outValue]
  375. if !ok {
  376. return fmt.Errorf("unexpected node id in edges: %v", outValue)
  377. }
  378. edgeids = append(edgeids, inValue)
  379. edges[inValue] = append(edges[inValue], node)
  380. return nil
  381. },
  382. }
  383. if err := sqlgraph.QueryEdges(ctx, uq.driver, _spec); err != nil {
  384. return nil, fmt.Errorf(`query edges "groups": %v`, err)
  385. }
  386. query.Where(group.IDIn(edgeids...))
  387. neighbors, err := query.All(ctx)
  388. if err != nil {
  389. return nil, err
  390. }
  391. for _, n := range neighbors {
  392. nodes, ok := edges[n.ID]
  393. if !ok {
  394. return nil, fmt.Errorf(`unexpected "groups" node returned %v`, n.ID)
  395. }
  396. for i := range nodes {
  397. nodes[i].Edges.Groups = append(nodes[i].Edges.Groups, n)
  398. }
  399. }
  400. }
  401. return nodes, nil
  402. }
  403. func (uq *UserQuery) sqlCount(ctx context.Context) (int, error) {
  404. _spec := uq.querySpec()
  405. return sqlgraph.CountNodes(ctx, uq.driver, _spec)
  406. }
  407. func (uq *UserQuery) sqlExist(ctx context.Context) (bool, error) {
  408. n, err := uq.sqlCount(ctx)
  409. if err != nil {
  410. return false, fmt.Errorf("ent: check existence: %v", err)
  411. }
  412. return n > 0, nil
  413. }
  414. func (uq *UserQuery) querySpec() *sqlgraph.QuerySpec {
  415. _spec := &sqlgraph.QuerySpec{
  416. Node: &sqlgraph.NodeSpec{
  417. Table: user.Table,
  418. Columns: user.Columns,
  419. ID: &sqlgraph.FieldSpec{
  420. Type: field.TypeInt,
  421. Column: user.FieldID,
  422. },
  423. },
  424. From: uq.sql,
  425. Unique: true,
  426. }
  427. if ps := uq.predicates; len(ps) > 0 {
  428. _spec.Predicate = func(selector *sql.Selector) {
  429. for i := range ps {
  430. ps[i](selector)
  431. }
  432. }
  433. }
  434. if limit := uq.limit; limit != nil {
  435. _spec.Limit = *limit
  436. }
  437. if offset := uq.offset; offset != nil {
  438. _spec.Offset = *offset
  439. }
  440. if ps := uq.order; len(ps) > 0 {
  441. _spec.Order = func(selector *sql.Selector) {
  442. for i := range ps {
  443. ps[i](selector)
  444. }
  445. }
  446. }
  447. return _spec
  448. }
  449. func (uq *UserQuery) sqlQuery() *sql.Selector {
  450. builder := sql.Dialect(uq.driver.Dialect())
  451. t1 := builder.Table(user.Table)
  452. selector := builder.Select(t1.Columns(user.Columns...)...).From(t1)
  453. if uq.sql != nil {
  454. selector = uq.sql
  455. selector.Select(selector.Columns(user.Columns...)...)
  456. }
  457. for _, p := range uq.predicates {
  458. p(selector)
  459. }
  460. for _, p := range uq.order {
  461. p(selector)
  462. }
  463. if offset := uq.offset; offset != nil {
  464. // limit is mandatory for offset clause. We start
  465. // with default value, and override it below if needed.
  466. selector.Offset(*offset).Limit(math.MaxInt32)
  467. }
  468. if limit := uq.limit; limit != nil {
  469. selector.Limit(*limit)
  470. }
  471. return selector
  472. }
  473. // UserGroupBy is the builder for group-by User entities.
  474. type UserGroupBy struct {
  475. config
  476. fields []string
  477. fns []Aggregate
  478. // intermediate query.
  479. sql *sql.Selector
  480. }
  481. // Aggregate adds the given aggregation functions to the group-by query.
  482. func (ugb *UserGroupBy) Aggregate(fns ...Aggregate) *UserGroupBy {
  483. ugb.fns = append(ugb.fns, fns...)
  484. return ugb
  485. }
  486. // Scan applies the group-by query and scan the result into the given value.
  487. func (ugb *UserGroupBy) Scan(ctx context.Context, v interface{}) error {
  488. return ugb.sqlScan(ctx, v)
  489. }
  490. // ScanX is like Scan, but panics if an error occurs.
  491. func (ugb *UserGroupBy) ScanX(ctx context.Context, v interface{}) {
  492. if err := ugb.Scan(ctx, v); err != nil {
  493. panic(err)
  494. }
  495. }
  496. // Strings returns list of strings from group-by. It is only allowed when querying group-by with one field.
  497. func (ugb *UserGroupBy) Strings(ctx context.Context) ([]string, error) {
  498. if len(ugb.fields) > 1 {
  499. return nil, errors.New("ent: UserGroupBy.Strings is not achievable when grouping more than 1 field")
  500. }
  501. var v []string
  502. if err := ugb.Scan(ctx, &v); err != nil {
  503. return nil, err
  504. }
  505. return v, nil
  506. }
  507. // StringsX is like Strings, but panics if an error occurs.
  508. func (ugb *UserGroupBy) StringsX(ctx context.Context) []string {
  509. v, err := ugb.Strings(ctx)
  510. if err != nil {
  511. panic(err)
  512. }
  513. return v
  514. }
  515. // Ints returns list of ints from group-by. It is only allowed when querying group-by with one field.
  516. func (ugb *UserGroupBy) Ints(ctx context.Context) ([]int, error) {
  517. if len(ugb.fields) > 1 {
  518. return nil, errors.New("ent: UserGroupBy.Ints is not achievable when grouping more than 1 field")
  519. }
  520. var v []int
  521. if err := ugb.Scan(ctx, &v); err != nil {
  522. return nil, err
  523. }
  524. return v, nil
  525. }
  526. // IntsX is like Ints, but panics if an error occurs.
  527. func (ugb *UserGroupBy) IntsX(ctx context.Context) []int {
  528. v, err := ugb.Ints(ctx)
  529. if err != nil {
  530. panic(err)
  531. }
  532. return v
  533. }
  534. // Float64s returns list of float64s from group-by. It is only allowed when querying group-by with one field.
  535. func (ugb *UserGroupBy) Float64s(ctx context.Context) ([]float64, error) {
  536. if len(ugb.fields) > 1 {
  537. return nil, errors.New("ent: UserGroupBy.Float64s is not achievable when grouping more than 1 field")
  538. }
  539. var v []float64
  540. if err := ugb.Scan(ctx, &v); err != nil {
  541. return nil, err
  542. }
  543. return v, nil
  544. }
  545. // Float64sX is like Float64s, but panics if an error occurs.
  546. func (ugb *UserGroupBy) Float64sX(ctx context.Context) []float64 {
  547. v, err := ugb.Float64s(ctx)
  548. if err != nil {
  549. panic(err)
  550. }
  551. return v
  552. }
  553. // Bools returns list of bools from group-by. It is only allowed when querying group-by with one field.
  554. func (ugb *UserGroupBy) Bools(ctx context.Context) ([]bool, error) {
  555. if len(ugb.fields) > 1 {
  556. return nil, errors.New("ent: UserGroupBy.Bools is not achievable when grouping more than 1 field")
  557. }
  558. var v []bool
  559. if err := ugb.Scan(ctx, &v); err != nil {
  560. return nil, err
  561. }
  562. return v, nil
  563. }
  564. // BoolsX is like Bools, but panics if an error occurs.
  565. func (ugb *UserGroupBy) BoolsX(ctx context.Context) []bool {
  566. v, err := ugb.Bools(ctx)
  567. if err != nil {
  568. panic(err)
  569. }
  570. return v
  571. }
  572. func (ugb *UserGroupBy) sqlScan(ctx context.Context, v interface{}) error {
  573. rows := &sql.Rows{}
  574. query, args := ugb.sqlQuery().Query()
  575. if err := ugb.driver.Query(ctx, query, args, rows); err != nil {
  576. return err
  577. }
  578. defer rows.Close()
  579. return sql.ScanSlice(rows, v)
  580. }
  581. func (ugb *UserGroupBy) sqlQuery() *sql.Selector {
  582. selector := ugb.sql
  583. columns := make([]string, 0, len(ugb.fields)+len(ugb.fns))
  584. columns = append(columns, ugb.fields...)
  585. for _, fn := range ugb.fns {
  586. columns = append(columns, fn(selector))
  587. }
  588. return selector.Select(columns...).GroupBy(ugb.fields...)
  589. }
  590. // UserSelect is the builder for select fields of User entities.
  591. type UserSelect struct {
  592. config
  593. fields []string
  594. // intermediate queries.
  595. sql *sql.Selector
  596. }
  597. // Scan applies the selector query and scan the result into the given value.
  598. func (us *UserSelect) Scan(ctx context.Context, v interface{}) error {
  599. return us.sqlScan(ctx, v)
  600. }
  601. // ScanX is like Scan, but panics if an error occurs.
  602. func (us *UserSelect) ScanX(ctx context.Context, v interface{}) {
  603. if err := us.Scan(ctx, v); err != nil {
  604. panic(err)
  605. }
  606. }
  607. // Strings returns list of strings from selector. It is only allowed when selecting one field.
  608. func (us *UserSelect) Strings(ctx context.Context) ([]string, error) {
  609. if len(us.fields) > 1 {
  610. return nil, errors.New("ent: UserSelect.Strings is not achievable when selecting more than 1 field")
  611. }
  612. var v []string
  613. if err := us.Scan(ctx, &v); err != nil {
  614. return nil, err
  615. }
  616. return v, nil
  617. }
  618. // StringsX is like Strings, but panics if an error occurs.
  619. func (us *UserSelect) StringsX(ctx context.Context) []string {
  620. v, err := us.Strings(ctx)
  621. if err != nil {
  622. panic(err)
  623. }
  624. return v
  625. }
  626. // Ints returns list of ints from selector. It is only allowed when selecting one field.
  627. func (us *UserSelect) Ints(ctx context.Context) ([]int, error) {
  628. if len(us.fields) > 1 {
  629. return nil, errors.New("ent: UserSelect.Ints is not achievable when selecting more than 1 field")
  630. }
  631. var v []int
  632. if err := us.Scan(ctx, &v); err != nil {
  633. return nil, err
  634. }
  635. return v, nil
  636. }
  637. // IntsX is like Ints, but panics if an error occurs.
  638. func (us *UserSelect) IntsX(ctx context.Context) []int {
  639. v, err := us.Ints(ctx)
  640. if err != nil {
  641. panic(err)
  642. }
  643. return v
  644. }
  645. // Float64s returns list of float64s from selector. It is only allowed when selecting one field.
  646. func (us *UserSelect) Float64s(ctx context.Context) ([]float64, error) {
  647. if len(us.fields) > 1 {
  648. return nil, errors.New("ent: UserSelect.Float64s is not achievable when selecting more than 1 field")
  649. }
  650. var v []float64
  651. if err := us.Scan(ctx, &v); err != nil {
  652. return nil, err
  653. }
  654. return v, nil
  655. }
  656. // Float64sX is like Float64s, but panics if an error occurs.
  657. func (us *UserSelect) Float64sX(ctx context.Context) []float64 {
  658. v, err := us.Float64s(ctx)
  659. if err != nil {
  660. panic(err)
  661. }
  662. return v
  663. }
  664. // Bools returns list of bools from selector. It is only allowed when selecting one field.
  665. func (us *UserSelect) Bools(ctx context.Context) ([]bool, error) {
  666. if len(us.fields) > 1 {
  667. return nil, errors.New("ent: UserSelect.Bools is not achievable when selecting more than 1 field")
  668. }
  669. var v []bool
  670. if err := us.Scan(ctx, &v); err != nil {
  671. return nil, err
  672. }
  673. return v, nil
  674. }
  675. // BoolsX is like Bools, but panics if an error occurs.
  676. func (us *UserSelect) BoolsX(ctx context.Context) []bool {
  677. v, err := us.Bools(ctx)
  678. if err != nil {
  679. panic(err)
  680. }
  681. return v
  682. }
  683. func (us *UserSelect) sqlScan(ctx context.Context, v interface{}) error {
  684. rows := &sql.Rows{}
  685. query, args := us.sqlQuery().Query()
  686. if err := us.driver.Query(ctx, query, args, rows); err != nil {
  687. return err
  688. }
  689. defer rows.Close()
  690. return sql.ScanSlice(rows, v)
  691. }
  692. func (us *UserSelect) sqlQuery() sql.Querier {
  693. selector := us.sql
  694. selector.Select(selector.Columns(us.fields...)...)
  695. return selector
  696. }