user_query.go 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  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. withSpouse *UserQuery
  30. withFollowers *UserQuery
  31. withFollowing *UserQuery
  32. withFriends *UserQuery
  33. // intermediate query (i.e. traversal path).
  34. sql *sql.Selector
  35. path func(context.Context) (*sql.Selector, error)
  36. }
  37. // Where adds a new predicate for the UserQuery builder.
  38. func (uq *UserQuery) Where(ps ...predicate.User) *UserQuery {
  39. uq.predicates = append(uq.predicates, ps...)
  40. return uq
  41. }
  42. // Limit adds a limit step to the query.
  43. func (uq *UserQuery) Limit(limit int) *UserQuery {
  44. uq.limit = &limit
  45. return uq
  46. }
  47. // Offset adds an offset step to the query.
  48. func (uq *UserQuery) Offset(offset int) *UserQuery {
  49. uq.offset = &offset
  50. return uq
  51. }
  52. // Unique configures the query builder to filter duplicate records on query.
  53. // By default, unique is set to true, and can be disabled using this method.
  54. func (uq *UserQuery) Unique(unique bool) *UserQuery {
  55. uq.unique = &unique
  56. return uq
  57. }
  58. // Order adds an order step to the query.
  59. func (uq *UserQuery) Order(o ...OrderFunc) *UserQuery {
  60. uq.order = append(uq.order, o...)
  61. return uq
  62. }
  63. // QueryCars chains the current query on the "cars" edge.
  64. func (uq *UserQuery) QueryCars() *CarQuery {
  65. query := &CarQuery{config: uq.config}
  66. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  67. if err := uq.prepareQuery(ctx); err != nil {
  68. return nil, err
  69. }
  70. selector := uq.sqlQuery(ctx)
  71. if err := selector.Err(); err != nil {
  72. return nil, err
  73. }
  74. step := sqlgraph.NewStep(
  75. sqlgraph.From(user.Table, user.FieldID, selector),
  76. sqlgraph.To(car.Table, car.FieldID),
  77. sqlgraph.Edge(sqlgraph.O2M, false, user.CarsTable, user.CarsColumn),
  78. )
  79. fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
  80. return fromU, nil
  81. }
  82. return query
  83. }
  84. // QueryGroups chains the current query on the "groups" edge.
  85. func (uq *UserQuery) QueryGroups() *GroupQuery {
  86. query := &GroupQuery{config: uq.config}
  87. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  88. if err := uq.prepareQuery(ctx); err != nil {
  89. return nil, err
  90. }
  91. selector := uq.sqlQuery(ctx)
  92. if err := selector.Err(); err != nil {
  93. return nil, err
  94. }
  95. step := sqlgraph.NewStep(
  96. sqlgraph.From(user.Table, user.FieldID, selector),
  97. sqlgraph.To(group.Table, group.FieldID),
  98. sqlgraph.Edge(sqlgraph.M2M, true, user.GroupsTable, user.GroupsPrimaryKey...),
  99. )
  100. fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
  101. return fromU, nil
  102. }
  103. return query
  104. }
  105. // QuerySpouse chains the current query on the "spouse" edge.
  106. func (uq *UserQuery) QuerySpouse() *UserQuery {
  107. query := &UserQuery{config: uq.config}
  108. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  109. if err := uq.prepareQuery(ctx); err != nil {
  110. return nil, err
  111. }
  112. selector := uq.sqlQuery(ctx)
  113. if err := selector.Err(); err != nil {
  114. return nil, err
  115. }
  116. step := sqlgraph.NewStep(
  117. sqlgraph.From(user.Table, user.FieldID, selector),
  118. sqlgraph.To(user.Table, user.FieldID),
  119. sqlgraph.Edge(sqlgraph.O2O, false, user.SpouseTable, user.SpouseColumn),
  120. )
  121. fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
  122. return fromU, nil
  123. }
  124. return query
  125. }
  126. // QueryFollowers chains the current query on the "followers" edge.
  127. func (uq *UserQuery) QueryFollowers() *UserQuery {
  128. query := &UserQuery{config: uq.config}
  129. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  130. if err := uq.prepareQuery(ctx); err != nil {
  131. return nil, err
  132. }
  133. selector := uq.sqlQuery(ctx)
  134. if err := selector.Err(); err != nil {
  135. return nil, err
  136. }
  137. step := sqlgraph.NewStep(
  138. sqlgraph.From(user.Table, user.FieldID, selector),
  139. sqlgraph.To(user.Table, user.FieldID),
  140. sqlgraph.Edge(sqlgraph.M2M, true, user.FollowersTable, user.FollowersPrimaryKey...),
  141. )
  142. fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
  143. return fromU, nil
  144. }
  145. return query
  146. }
  147. // QueryFollowing chains the current query on the "following" edge.
  148. func (uq *UserQuery) QueryFollowing() *UserQuery {
  149. query := &UserQuery{config: uq.config}
  150. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  151. if err := uq.prepareQuery(ctx); err != nil {
  152. return nil, err
  153. }
  154. selector := uq.sqlQuery(ctx)
  155. if err := selector.Err(); err != nil {
  156. return nil, err
  157. }
  158. step := sqlgraph.NewStep(
  159. sqlgraph.From(user.Table, user.FieldID, selector),
  160. sqlgraph.To(user.Table, user.FieldID),
  161. sqlgraph.Edge(sqlgraph.M2M, false, user.FollowingTable, user.FollowingPrimaryKey...),
  162. )
  163. fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
  164. return fromU, nil
  165. }
  166. return query
  167. }
  168. // QueryFriends chains the current query on the "friends" edge.
  169. func (uq *UserQuery) QueryFriends() *UserQuery {
  170. query := &UserQuery{config: uq.config}
  171. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  172. if err := uq.prepareQuery(ctx); err != nil {
  173. return nil, err
  174. }
  175. selector := uq.sqlQuery(ctx)
  176. if err := selector.Err(); err != nil {
  177. return nil, err
  178. }
  179. step := sqlgraph.NewStep(
  180. sqlgraph.From(user.Table, user.FieldID, selector),
  181. sqlgraph.To(user.Table, user.FieldID),
  182. sqlgraph.Edge(sqlgraph.M2M, false, user.FriendsTable, user.FriendsPrimaryKey...),
  183. )
  184. fromU = sqlgraph.SetNeighbors(uq.driver.Dialect(), step)
  185. return fromU, nil
  186. }
  187. return query
  188. }
  189. // First returns the first User entity from the query.
  190. // Returns a *NotFoundError when no User was found.
  191. func (uq *UserQuery) First(ctx context.Context) (*User, error) {
  192. nodes, err := uq.Limit(1).All(ctx)
  193. if err != nil {
  194. return nil, err
  195. }
  196. if len(nodes) == 0 {
  197. return nil, &NotFoundError{user.Label}
  198. }
  199. return nodes[0], nil
  200. }
  201. // FirstX is like First, but panics if an error occurs.
  202. func (uq *UserQuery) FirstX(ctx context.Context) *User {
  203. node, err := uq.First(ctx)
  204. if err != nil && !IsNotFound(err) {
  205. panic(err)
  206. }
  207. return node
  208. }
  209. // FirstID returns the first User ID from the query.
  210. // Returns a *NotFoundError when no User ID was found.
  211. func (uq *UserQuery) FirstID(ctx context.Context) (id int, err error) {
  212. var ids []int
  213. if ids, err = uq.Limit(1).IDs(ctx); err != nil {
  214. return
  215. }
  216. if len(ids) == 0 {
  217. err = &NotFoundError{user.Label}
  218. return
  219. }
  220. return ids[0], nil
  221. }
  222. // FirstIDX is like FirstID, but panics if an error occurs.
  223. func (uq *UserQuery) FirstIDX(ctx context.Context) int {
  224. id, err := uq.FirstID(ctx)
  225. if err != nil && !IsNotFound(err) {
  226. panic(err)
  227. }
  228. return id
  229. }
  230. // Only returns a single User entity found by the query, ensuring it only returns one.
  231. // Returns a *NotSingularError when more than one User entity is found.
  232. // Returns a *NotFoundError when no User entities are found.
  233. func (uq *UserQuery) Only(ctx context.Context) (*User, error) {
  234. nodes, err := uq.Limit(2).All(ctx)
  235. if err != nil {
  236. return nil, err
  237. }
  238. switch len(nodes) {
  239. case 1:
  240. return nodes[0], nil
  241. case 0:
  242. return nil, &NotFoundError{user.Label}
  243. default:
  244. return nil, &NotSingularError{user.Label}
  245. }
  246. }
  247. // OnlyX is like Only, but panics if an error occurs.
  248. func (uq *UserQuery) OnlyX(ctx context.Context) *User {
  249. node, err := uq.Only(ctx)
  250. if err != nil {
  251. panic(err)
  252. }
  253. return node
  254. }
  255. // OnlyID is like Only, but returns the only User ID in the query.
  256. // Returns a *NotSingularError when more than one User ID is found.
  257. // Returns a *NotFoundError when no entities are found.
  258. func (uq *UserQuery) OnlyID(ctx context.Context) (id int, err error) {
  259. var ids []int
  260. if ids, err = uq.Limit(2).IDs(ctx); err != nil {
  261. return
  262. }
  263. switch len(ids) {
  264. case 1:
  265. id = ids[0]
  266. case 0:
  267. err = &NotFoundError{user.Label}
  268. default:
  269. err = &NotSingularError{user.Label}
  270. }
  271. return
  272. }
  273. // OnlyIDX is like OnlyID, but panics if an error occurs.
  274. func (uq *UserQuery) OnlyIDX(ctx context.Context) int {
  275. id, err := uq.OnlyID(ctx)
  276. if err != nil {
  277. panic(err)
  278. }
  279. return id
  280. }
  281. // All executes the query and returns a list of Users.
  282. func (uq *UserQuery) All(ctx context.Context) ([]*User, error) {
  283. if err := uq.prepareQuery(ctx); err != nil {
  284. return nil, err
  285. }
  286. return uq.sqlAll(ctx)
  287. }
  288. // AllX is like All, but panics if an error occurs.
  289. func (uq *UserQuery) AllX(ctx context.Context) []*User {
  290. nodes, err := uq.All(ctx)
  291. if err != nil {
  292. panic(err)
  293. }
  294. return nodes
  295. }
  296. // IDs executes the query and returns a list of User IDs.
  297. func (uq *UserQuery) IDs(ctx context.Context) ([]int, error) {
  298. var ids []int
  299. if err := uq.Select(user.FieldID).Scan(ctx, &ids); err != nil {
  300. return nil, err
  301. }
  302. return ids, nil
  303. }
  304. // IDsX is like IDs, but panics if an error occurs.
  305. func (uq *UserQuery) IDsX(ctx context.Context) []int {
  306. ids, err := uq.IDs(ctx)
  307. if err != nil {
  308. panic(err)
  309. }
  310. return ids
  311. }
  312. // Count returns the count of the given query.
  313. func (uq *UserQuery) Count(ctx context.Context) (int, error) {
  314. if err := uq.prepareQuery(ctx); err != nil {
  315. return 0, err
  316. }
  317. return uq.sqlCount(ctx)
  318. }
  319. // CountX is like Count, but panics if an error occurs.
  320. func (uq *UserQuery) CountX(ctx context.Context) int {
  321. count, err := uq.Count(ctx)
  322. if err != nil {
  323. panic(err)
  324. }
  325. return count
  326. }
  327. // Exist returns true if the query has elements in the graph.
  328. func (uq *UserQuery) Exist(ctx context.Context) (bool, error) {
  329. if err := uq.prepareQuery(ctx); err != nil {
  330. return false, err
  331. }
  332. return uq.sqlExist(ctx)
  333. }
  334. // ExistX is like Exist, but panics if an error occurs.
  335. func (uq *UserQuery) ExistX(ctx context.Context) bool {
  336. exist, err := uq.Exist(ctx)
  337. if err != nil {
  338. panic(err)
  339. }
  340. return exist
  341. }
  342. // Clone returns a duplicate of the UserQuery builder, including all associated steps. It can be
  343. // used to prepare common query builders and use them differently after the clone is made.
  344. func (uq *UserQuery) Clone() *UserQuery {
  345. if uq == nil {
  346. return nil
  347. }
  348. return &UserQuery{
  349. config: uq.config,
  350. limit: uq.limit,
  351. offset: uq.offset,
  352. order: append([]OrderFunc{}, uq.order...),
  353. predicates: append([]predicate.User{}, uq.predicates...),
  354. withCars: uq.withCars.Clone(),
  355. withGroups: uq.withGroups.Clone(),
  356. withSpouse: uq.withSpouse.Clone(),
  357. withFollowers: uq.withFollowers.Clone(),
  358. withFollowing: uq.withFollowing.Clone(),
  359. withFriends: uq.withFriends.Clone(),
  360. // clone intermediate query.
  361. sql: uq.sql.Clone(),
  362. path: uq.path,
  363. unique: uq.unique,
  364. }
  365. }
  366. // WithCars tells the query-builder to eager-load the nodes that are connected to
  367. // the "cars" edge. The optional arguments are used to configure the query builder of the edge.
  368. func (uq *UserQuery) WithCars(opts ...func(*CarQuery)) *UserQuery {
  369. query := &CarQuery{config: uq.config}
  370. for _, opt := range opts {
  371. opt(query)
  372. }
  373. uq.withCars = query
  374. return uq
  375. }
  376. // WithGroups tells the query-builder to eager-load the nodes that are connected to
  377. // the "groups" edge. The optional arguments are used to configure the query builder of the edge.
  378. func (uq *UserQuery) WithGroups(opts ...func(*GroupQuery)) *UserQuery {
  379. query := &GroupQuery{config: uq.config}
  380. for _, opt := range opts {
  381. opt(query)
  382. }
  383. uq.withGroups = query
  384. return uq
  385. }
  386. // WithSpouse tells the query-builder to eager-load the nodes that are connected to
  387. // the "spouse" edge. The optional arguments are used to configure the query builder of the edge.
  388. func (uq *UserQuery) WithSpouse(opts ...func(*UserQuery)) *UserQuery {
  389. query := &UserQuery{config: uq.config}
  390. for _, opt := range opts {
  391. opt(query)
  392. }
  393. uq.withSpouse = query
  394. return uq
  395. }
  396. // WithFollowers tells the query-builder to eager-load the nodes that are connected to
  397. // the "followers" edge. The optional arguments are used to configure the query builder of the edge.
  398. func (uq *UserQuery) WithFollowers(opts ...func(*UserQuery)) *UserQuery {
  399. query := &UserQuery{config: uq.config}
  400. for _, opt := range opts {
  401. opt(query)
  402. }
  403. uq.withFollowers = query
  404. return uq
  405. }
  406. // WithFollowing tells the query-builder to eager-load the nodes that are connected to
  407. // the "following" edge. The optional arguments are used to configure the query builder of the edge.
  408. func (uq *UserQuery) WithFollowing(opts ...func(*UserQuery)) *UserQuery {
  409. query := &UserQuery{config: uq.config}
  410. for _, opt := range opts {
  411. opt(query)
  412. }
  413. uq.withFollowing = query
  414. return uq
  415. }
  416. // WithFriends tells the query-builder to eager-load the nodes that are connected to
  417. // the "friends" edge. The optional arguments are used to configure the query builder of the edge.
  418. func (uq *UserQuery) WithFriends(opts ...func(*UserQuery)) *UserQuery {
  419. query := &UserQuery{config: uq.config}
  420. for _, opt := range opts {
  421. opt(query)
  422. }
  423. uq.withFriends = query
  424. return uq
  425. }
  426. // GroupBy is used to group vertices by one or more fields/columns.
  427. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  428. //
  429. // Example:
  430. //
  431. // var v []struct {
  432. // Age int `json:"age,omitempty"`
  433. // Count int `json:"count,omitempty"`
  434. // }
  435. //
  436. // client.User.Query().
  437. // GroupBy(user.FieldAge).
  438. // Aggregate(ent.Count()).
  439. // Scan(ctx, &v)
  440. //
  441. func (uq *UserQuery) GroupBy(field string, fields ...string) *UserGroupBy {
  442. group := &UserGroupBy{config: uq.config}
  443. group.fields = append([]string{field}, fields...)
  444. group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
  445. if err := uq.prepareQuery(ctx); err != nil {
  446. return nil, err
  447. }
  448. return uq.sqlQuery(ctx), nil
  449. }
  450. return group
  451. }
  452. // Select allows the selection one or more fields/columns for the given query,
  453. // instead of selecting all fields in the entity.
  454. //
  455. // Example:
  456. //
  457. // var v []struct {
  458. // Age int `json:"age,omitempty"`
  459. // }
  460. //
  461. // client.User.Query().
  462. // Select(user.FieldAge).
  463. // Scan(ctx, &v)
  464. //
  465. func (uq *UserQuery) Select(fields ...string) *UserSelect {
  466. uq.fields = append(uq.fields, fields...)
  467. return &UserSelect{UserQuery: uq}
  468. }
  469. func (uq *UserQuery) prepareQuery(ctx context.Context) error {
  470. for _, f := range uq.fields {
  471. if !user.ValidColumn(f) {
  472. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  473. }
  474. }
  475. if uq.path != nil {
  476. prev, err := uq.path(ctx)
  477. if err != nil {
  478. return err
  479. }
  480. uq.sql = prev
  481. }
  482. return nil
  483. }
  484. func (uq *UserQuery) sqlAll(ctx context.Context) ([]*User, error) {
  485. var (
  486. nodes = []*User{}
  487. _spec = uq.querySpec()
  488. loadedTypes = [6]bool{
  489. uq.withCars != nil,
  490. uq.withGroups != nil,
  491. uq.withSpouse != nil,
  492. uq.withFollowers != nil,
  493. uq.withFollowing != nil,
  494. uq.withFriends != nil,
  495. }
  496. )
  497. _spec.ScanValues = func(columns []string) ([]interface{}, error) {
  498. node := &User{config: uq.config}
  499. nodes = append(nodes, node)
  500. return node.scanValues(columns)
  501. }
  502. _spec.Assign = func(columns []string, values []interface{}) error {
  503. if len(nodes) == 0 {
  504. return fmt.Errorf("ent: Assign called without calling ScanValues")
  505. }
  506. node := nodes[len(nodes)-1]
  507. node.Edges.loadedTypes = loadedTypes
  508. return node.assignValues(columns, values)
  509. }
  510. if err := sqlgraph.QueryNodes(ctx, uq.driver, _spec); err != nil {
  511. return nil, err
  512. }
  513. if len(nodes) == 0 {
  514. return nodes, nil
  515. }
  516. if query := uq.withCars; query != nil {
  517. fks := make([]driver.Value, 0, len(nodes))
  518. nodeids := make(map[int]*User)
  519. for i := range nodes {
  520. fks = append(fks, nodes[i].ID)
  521. nodeids[nodes[i].ID] = nodes[i]
  522. nodes[i].Edges.Cars = []*Car{}
  523. }
  524. query.withFKs = true
  525. query.Where(predicate.Car(func(s *sql.Selector) {
  526. s.Where(sql.InValues(user.CarsColumn, fks...))
  527. }))
  528. neighbors, err := query.All(ctx)
  529. if err != nil {
  530. return nil, err
  531. }
  532. for _, n := range neighbors {
  533. fk := n.user_cars
  534. if fk == nil {
  535. return nil, fmt.Errorf(`foreign-key "user_cars" is nil for node %v`, n.ID)
  536. }
  537. node, ok := nodeids[*fk]
  538. if !ok {
  539. return nil, fmt.Errorf(`unexpected foreign-key "user_cars" returned %v for node %v`, *fk, n.ID)
  540. }
  541. node.Edges.Cars = append(node.Edges.Cars, n)
  542. }
  543. }
  544. if query := uq.withGroups; query != nil {
  545. fks := make([]driver.Value, 0, len(nodes))
  546. ids := make(map[int]*User, len(nodes))
  547. for _, node := range nodes {
  548. ids[node.ID] = node
  549. fks = append(fks, node.ID)
  550. node.Edges.Groups = []*Group{}
  551. }
  552. var (
  553. edgeids []int
  554. edges = make(map[int][]*User)
  555. )
  556. _spec := &sqlgraph.EdgeQuerySpec{
  557. Edge: &sqlgraph.EdgeSpec{
  558. Inverse: true,
  559. Table: user.GroupsTable,
  560. Columns: user.GroupsPrimaryKey,
  561. },
  562. Predicate: func(s *sql.Selector) {
  563. s.Where(sql.InValues(user.GroupsPrimaryKey[1], fks...))
  564. },
  565. ScanValues: func() [2]interface{} {
  566. return [2]interface{}{new(sql.NullInt64), new(sql.NullInt64)}
  567. },
  568. Assign: func(out, in interface{}) error {
  569. eout, ok := out.(*sql.NullInt64)
  570. if !ok || eout == nil {
  571. return fmt.Errorf("unexpected id value for edge-out")
  572. }
  573. ein, ok := in.(*sql.NullInt64)
  574. if !ok || ein == nil {
  575. return fmt.Errorf("unexpected id value for edge-in")
  576. }
  577. outValue := int(eout.Int64)
  578. inValue := int(ein.Int64)
  579. node, ok := ids[outValue]
  580. if !ok {
  581. return fmt.Errorf("unexpected node id in edges: %v", outValue)
  582. }
  583. if _, ok := edges[inValue]; !ok {
  584. edgeids = append(edgeids, inValue)
  585. }
  586. edges[inValue] = append(edges[inValue], node)
  587. return nil
  588. },
  589. }
  590. if err := sqlgraph.QueryEdges(ctx, uq.driver, _spec); err != nil {
  591. return nil, fmt.Errorf(`query edges "groups": %w`, err)
  592. }
  593. query.Where(group.IDIn(edgeids...))
  594. neighbors, err := query.All(ctx)
  595. if err != nil {
  596. return nil, err
  597. }
  598. for _, n := range neighbors {
  599. nodes, ok := edges[n.ID]
  600. if !ok {
  601. return nil, fmt.Errorf(`unexpected "groups" node returned %v`, n.ID)
  602. }
  603. for i := range nodes {
  604. nodes[i].Edges.Groups = append(nodes[i].Edges.Groups, n)
  605. }
  606. }
  607. }
  608. if query := uq.withSpouse; query != nil {
  609. ids := make([]int, 0, len(nodes))
  610. nodeids := make(map[int][]*User)
  611. for i := range nodes {
  612. fk := nodes[i].SpouseID
  613. if _, ok := nodeids[fk]; !ok {
  614. ids = append(ids, fk)
  615. }
  616. nodeids[fk] = append(nodeids[fk], nodes[i])
  617. }
  618. query.Where(user.IDIn(ids...))
  619. neighbors, err := query.All(ctx)
  620. if err != nil {
  621. return nil, err
  622. }
  623. for _, n := range neighbors {
  624. nodes, ok := nodeids[n.ID]
  625. if !ok {
  626. return nil, fmt.Errorf(`unexpected foreign-key "spouse_id" returned %v`, n.ID)
  627. }
  628. for i := range nodes {
  629. nodes[i].Edges.Spouse = n
  630. }
  631. }
  632. }
  633. if query := uq.withFollowers; query != nil {
  634. fks := make([]driver.Value, 0, len(nodes))
  635. ids := make(map[int]*User, len(nodes))
  636. for _, node := range nodes {
  637. ids[node.ID] = node
  638. fks = append(fks, node.ID)
  639. node.Edges.Followers = []*User{}
  640. }
  641. var (
  642. edgeids []int
  643. edges = make(map[int][]*User)
  644. )
  645. _spec := &sqlgraph.EdgeQuerySpec{
  646. Edge: &sqlgraph.EdgeSpec{
  647. Inverse: true,
  648. Table: user.FollowersTable,
  649. Columns: user.FollowersPrimaryKey,
  650. },
  651. Predicate: func(s *sql.Selector) {
  652. s.Where(sql.InValues(user.FollowersPrimaryKey[1], fks...))
  653. },
  654. ScanValues: func() [2]interface{} {
  655. return [2]interface{}{new(sql.NullInt64), new(sql.NullInt64)}
  656. },
  657. Assign: func(out, in interface{}) error {
  658. eout, ok := out.(*sql.NullInt64)
  659. if !ok || eout == nil {
  660. return fmt.Errorf("unexpected id value for edge-out")
  661. }
  662. ein, ok := in.(*sql.NullInt64)
  663. if !ok || ein == nil {
  664. return fmt.Errorf("unexpected id value for edge-in")
  665. }
  666. outValue := int(eout.Int64)
  667. inValue := int(ein.Int64)
  668. node, ok := ids[outValue]
  669. if !ok {
  670. return fmt.Errorf("unexpected node id in edges: %v", outValue)
  671. }
  672. if _, ok := edges[inValue]; !ok {
  673. edgeids = append(edgeids, inValue)
  674. }
  675. edges[inValue] = append(edges[inValue], node)
  676. return nil
  677. },
  678. }
  679. if err := sqlgraph.QueryEdges(ctx, uq.driver, _spec); err != nil {
  680. return nil, fmt.Errorf(`query edges "followers": %w`, err)
  681. }
  682. query.Where(user.IDIn(edgeids...))
  683. neighbors, err := query.All(ctx)
  684. if err != nil {
  685. return nil, err
  686. }
  687. for _, n := range neighbors {
  688. nodes, ok := edges[n.ID]
  689. if !ok {
  690. return nil, fmt.Errorf(`unexpected "followers" node returned %v`, n.ID)
  691. }
  692. for i := range nodes {
  693. nodes[i].Edges.Followers = append(nodes[i].Edges.Followers, n)
  694. }
  695. }
  696. }
  697. if query := uq.withFollowing; query != nil {
  698. fks := make([]driver.Value, 0, len(nodes))
  699. ids := make(map[int]*User, len(nodes))
  700. for _, node := range nodes {
  701. ids[node.ID] = node
  702. fks = append(fks, node.ID)
  703. node.Edges.Following = []*User{}
  704. }
  705. var (
  706. edgeids []int
  707. edges = make(map[int][]*User)
  708. )
  709. _spec := &sqlgraph.EdgeQuerySpec{
  710. Edge: &sqlgraph.EdgeSpec{
  711. Inverse: false,
  712. Table: user.FollowingTable,
  713. Columns: user.FollowingPrimaryKey,
  714. },
  715. Predicate: func(s *sql.Selector) {
  716. s.Where(sql.InValues(user.FollowingPrimaryKey[0], fks...))
  717. },
  718. ScanValues: func() [2]interface{} {
  719. return [2]interface{}{new(sql.NullInt64), new(sql.NullInt64)}
  720. },
  721. Assign: func(out, in interface{}) error {
  722. eout, ok := out.(*sql.NullInt64)
  723. if !ok || eout == nil {
  724. return fmt.Errorf("unexpected id value for edge-out")
  725. }
  726. ein, ok := in.(*sql.NullInt64)
  727. if !ok || ein == nil {
  728. return fmt.Errorf("unexpected id value for edge-in")
  729. }
  730. outValue := int(eout.Int64)
  731. inValue := int(ein.Int64)
  732. node, ok := ids[outValue]
  733. if !ok {
  734. return fmt.Errorf("unexpected node id in edges: %v", outValue)
  735. }
  736. if _, ok := edges[inValue]; !ok {
  737. edgeids = append(edgeids, inValue)
  738. }
  739. edges[inValue] = append(edges[inValue], node)
  740. return nil
  741. },
  742. }
  743. if err := sqlgraph.QueryEdges(ctx, uq.driver, _spec); err != nil {
  744. return nil, fmt.Errorf(`query edges "following": %w`, err)
  745. }
  746. query.Where(user.IDIn(edgeids...))
  747. neighbors, err := query.All(ctx)
  748. if err != nil {
  749. return nil, err
  750. }
  751. for _, n := range neighbors {
  752. nodes, ok := edges[n.ID]
  753. if !ok {
  754. return nil, fmt.Errorf(`unexpected "following" node returned %v`, n.ID)
  755. }
  756. for i := range nodes {
  757. nodes[i].Edges.Following = append(nodes[i].Edges.Following, n)
  758. }
  759. }
  760. }
  761. if query := uq.withFriends; query != nil {
  762. fks := make([]driver.Value, 0, len(nodes))
  763. ids := make(map[int]*User, len(nodes))
  764. for _, node := range nodes {
  765. ids[node.ID] = node
  766. fks = append(fks, node.ID)
  767. node.Edges.Friends = []*User{}
  768. }
  769. var (
  770. edgeids []int
  771. edges = make(map[int][]*User)
  772. )
  773. _spec := &sqlgraph.EdgeQuerySpec{
  774. Edge: &sqlgraph.EdgeSpec{
  775. Inverse: false,
  776. Table: user.FriendsTable,
  777. Columns: user.FriendsPrimaryKey,
  778. },
  779. Predicate: func(s *sql.Selector) {
  780. s.Where(sql.InValues(user.FriendsPrimaryKey[0], fks...))
  781. },
  782. ScanValues: func() [2]interface{} {
  783. return [2]interface{}{new(sql.NullInt64), new(sql.NullInt64)}
  784. },
  785. Assign: func(out, in interface{}) error {
  786. eout, ok := out.(*sql.NullInt64)
  787. if !ok || eout == nil {
  788. return fmt.Errorf("unexpected id value for edge-out")
  789. }
  790. ein, ok := in.(*sql.NullInt64)
  791. if !ok || ein == nil {
  792. return fmt.Errorf("unexpected id value for edge-in")
  793. }
  794. outValue := int(eout.Int64)
  795. inValue := int(ein.Int64)
  796. node, ok := ids[outValue]
  797. if !ok {
  798. return fmt.Errorf("unexpected node id in edges: %v", outValue)
  799. }
  800. if _, ok := edges[inValue]; !ok {
  801. edgeids = append(edgeids, inValue)
  802. }
  803. edges[inValue] = append(edges[inValue], node)
  804. return nil
  805. },
  806. }
  807. if err := sqlgraph.QueryEdges(ctx, uq.driver, _spec); err != nil {
  808. return nil, fmt.Errorf(`query edges "friends": %w`, err)
  809. }
  810. query.Where(user.IDIn(edgeids...))
  811. neighbors, err := query.All(ctx)
  812. if err != nil {
  813. return nil, err
  814. }
  815. for _, n := range neighbors {
  816. nodes, ok := edges[n.ID]
  817. if !ok {
  818. return nil, fmt.Errorf(`unexpected "friends" node returned %v`, n.ID)
  819. }
  820. for i := range nodes {
  821. nodes[i].Edges.Friends = append(nodes[i].Edges.Friends, n)
  822. }
  823. }
  824. }
  825. return nodes, nil
  826. }
  827. func (uq *UserQuery) sqlCount(ctx context.Context) (int, error) {
  828. _spec := uq.querySpec()
  829. _spec.Node.Columns = uq.fields
  830. if len(uq.fields) > 0 {
  831. _spec.Unique = uq.unique != nil && *uq.unique
  832. }
  833. return sqlgraph.CountNodes(ctx, uq.driver, _spec)
  834. }
  835. func (uq *UserQuery) sqlExist(ctx context.Context) (bool, error) {
  836. n, err := uq.sqlCount(ctx)
  837. if err != nil {
  838. return false, fmt.Errorf("ent: check existence: %w", err)
  839. }
  840. return n > 0, nil
  841. }
  842. func (uq *UserQuery) querySpec() *sqlgraph.QuerySpec {
  843. _spec := &sqlgraph.QuerySpec{
  844. Node: &sqlgraph.NodeSpec{
  845. Table: user.Table,
  846. Columns: user.Columns,
  847. ID: &sqlgraph.FieldSpec{
  848. Type: field.TypeInt,
  849. Column: user.FieldID,
  850. },
  851. },
  852. From: uq.sql,
  853. Unique: true,
  854. }
  855. if unique := uq.unique; unique != nil {
  856. _spec.Unique = *unique
  857. }
  858. if fields := uq.fields; len(fields) > 0 {
  859. _spec.Node.Columns = make([]string, 0, len(fields))
  860. _spec.Node.Columns = append(_spec.Node.Columns, user.FieldID)
  861. for i := range fields {
  862. if fields[i] != user.FieldID {
  863. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  864. }
  865. }
  866. }
  867. if ps := uq.predicates; len(ps) > 0 {
  868. _spec.Predicate = func(selector *sql.Selector) {
  869. for i := range ps {
  870. ps[i](selector)
  871. }
  872. }
  873. }
  874. if limit := uq.limit; limit != nil {
  875. _spec.Limit = *limit
  876. }
  877. if offset := uq.offset; offset != nil {
  878. _spec.Offset = *offset
  879. }
  880. if ps := uq.order; len(ps) > 0 {
  881. _spec.Order = func(selector *sql.Selector) {
  882. for i := range ps {
  883. ps[i](selector)
  884. }
  885. }
  886. }
  887. return _spec
  888. }
  889. func (uq *UserQuery) sqlQuery(ctx context.Context) *sql.Selector {
  890. builder := sql.Dialect(uq.driver.Dialect())
  891. t1 := builder.Table(user.Table)
  892. columns := uq.fields
  893. if len(columns) == 0 {
  894. columns = user.Columns
  895. }
  896. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  897. if uq.sql != nil {
  898. selector = uq.sql
  899. selector.Select(selector.Columns(columns...)...)
  900. }
  901. if uq.unique != nil && *uq.unique {
  902. selector.Distinct()
  903. }
  904. for _, p := range uq.predicates {
  905. p(selector)
  906. }
  907. for _, p := range uq.order {
  908. p(selector)
  909. }
  910. if offset := uq.offset; offset != nil {
  911. // limit is mandatory for offset clause. We start
  912. // with default value, and override it below if needed.
  913. selector.Offset(*offset).Limit(math.MaxInt32)
  914. }
  915. if limit := uq.limit; limit != nil {
  916. selector.Limit(*limit)
  917. }
  918. return selector
  919. }
  920. // UserGroupBy is the group-by builder for User entities.
  921. type UserGroupBy struct {
  922. config
  923. fields []string
  924. fns []AggregateFunc
  925. // intermediate query (i.e. traversal path).
  926. sql *sql.Selector
  927. path func(context.Context) (*sql.Selector, error)
  928. }
  929. // Aggregate adds the given aggregation functions to the group-by query.
  930. func (ugb *UserGroupBy) Aggregate(fns ...AggregateFunc) *UserGroupBy {
  931. ugb.fns = append(ugb.fns, fns...)
  932. return ugb
  933. }
  934. // Scan applies the group-by query and scans the result into the given value.
  935. func (ugb *UserGroupBy) Scan(ctx context.Context, v interface{}) error {
  936. query, err := ugb.path(ctx)
  937. if err != nil {
  938. return err
  939. }
  940. ugb.sql = query
  941. return ugb.sqlScan(ctx, v)
  942. }
  943. // ScanX is like Scan, but panics if an error occurs.
  944. func (ugb *UserGroupBy) ScanX(ctx context.Context, v interface{}) {
  945. if err := ugb.Scan(ctx, v); err != nil {
  946. panic(err)
  947. }
  948. }
  949. // Strings returns list of strings from group-by.
  950. // It is only allowed when executing a group-by query with one field.
  951. func (ugb *UserGroupBy) Strings(ctx context.Context) ([]string, error) {
  952. if len(ugb.fields) > 1 {
  953. return nil, errors.New("ent: UserGroupBy.Strings is not achievable when grouping more than 1 field")
  954. }
  955. var v []string
  956. if err := ugb.Scan(ctx, &v); err != nil {
  957. return nil, err
  958. }
  959. return v, nil
  960. }
  961. // StringsX is like Strings, but panics if an error occurs.
  962. func (ugb *UserGroupBy) StringsX(ctx context.Context) []string {
  963. v, err := ugb.Strings(ctx)
  964. if err != nil {
  965. panic(err)
  966. }
  967. return v
  968. }
  969. // String returns a single string from a group-by query.
  970. // It is only allowed when executing a group-by query with one field.
  971. func (ugb *UserGroupBy) String(ctx context.Context) (_ string, err error) {
  972. var v []string
  973. if v, err = ugb.Strings(ctx); err != nil {
  974. return
  975. }
  976. switch len(v) {
  977. case 1:
  978. return v[0], nil
  979. case 0:
  980. err = &NotFoundError{user.Label}
  981. default:
  982. err = fmt.Errorf("ent: UserGroupBy.Strings returned %d results when one was expected", len(v))
  983. }
  984. return
  985. }
  986. // StringX is like String, but panics if an error occurs.
  987. func (ugb *UserGroupBy) StringX(ctx context.Context) string {
  988. v, err := ugb.String(ctx)
  989. if err != nil {
  990. panic(err)
  991. }
  992. return v
  993. }
  994. // Ints returns list of ints from group-by.
  995. // It is only allowed when executing a group-by query with one field.
  996. func (ugb *UserGroupBy) Ints(ctx context.Context) ([]int, error) {
  997. if len(ugb.fields) > 1 {
  998. return nil, errors.New("ent: UserGroupBy.Ints is not achievable when grouping more than 1 field")
  999. }
  1000. var v []int
  1001. if err := ugb.Scan(ctx, &v); err != nil {
  1002. return nil, err
  1003. }
  1004. return v, nil
  1005. }
  1006. // IntsX is like Ints, but panics if an error occurs.
  1007. func (ugb *UserGroupBy) IntsX(ctx context.Context) []int {
  1008. v, err := ugb.Ints(ctx)
  1009. if err != nil {
  1010. panic(err)
  1011. }
  1012. return v
  1013. }
  1014. // Int returns a single int from a group-by query.
  1015. // It is only allowed when executing a group-by query with one field.
  1016. func (ugb *UserGroupBy) Int(ctx context.Context) (_ int, err error) {
  1017. var v []int
  1018. if v, err = ugb.Ints(ctx); err != nil {
  1019. return
  1020. }
  1021. switch len(v) {
  1022. case 1:
  1023. return v[0], nil
  1024. case 0:
  1025. err = &NotFoundError{user.Label}
  1026. default:
  1027. err = fmt.Errorf("ent: UserGroupBy.Ints returned %d results when one was expected", len(v))
  1028. }
  1029. return
  1030. }
  1031. // IntX is like Int, but panics if an error occurs.
  1032. func (ugb *UserGroupBy) IntX(ctx context.Context) int {
  1033. v, err := ugb.Int(ctx)
  1034. if err != nil {
  1035. panic(err)
  1036. }
  1037. return v
  1038. }
  1039. // Float64s returns list of float64s from group-by.
  1040. // It is only allowed when executing a group-by query with one field.
  1041. func (ugb *UserGroupBy) Float64s(ctx context.Context) ([]float64, error) {
  1042. if len(ugb.fields) > 1 {
  1043. return nil, errors.New("ent: UserGroupBy.Float64s is not achievable when grouping more than 1 field")
  1044. }
  1045. var v []float64
  1046. if err := ugb.Scan(ctx, &v); err != nil {
  1047. return nil, err
  1048. }
  1049. return v, nil
  1050. }
  1051. // Float64sX is like Float64s, but panics if an error occurs.
  1052. func (ugb *UserGroupBy) Float64sX(ctx context.Context) []float64 {
  1053. v, err := ugb.Float64s(ctx)
  1054. if err != nil {
  1055. panic(err)
  1056. }
  1057. return v
  1058. }
  1059. // Float64 returns a single float64 from a group-by query.
  1060. // It is only allowed when executing a group-by query with one field.
  1061. func (ugb *UserGroupBy) Float64(ctx context.Context) (_ float64, err error) {
  1062. var v []float64
  1063. if v, err = ugb.Float64s(ctx); err != nil {
  1064. return
  1065. }
  1066. switch len(v) {
  1067. case 1:
  1068. return v[0], nil
  1069. case 0:
  1070. err = &NotFoundError{user.Label}
  1071. default:
  1072. err = fmt.Errorf("ent: UserGroupBy.Float64s returned %d results when one was expected", len(v))
  1073. }
  1074. return
  1075. }
  1076. // Float64X is like Float64, but panics if an error occurs.
  1077. func (ugb *UserGroupBy) Float64X(ctx context.Context) float64 {
  1078. v, err := ugb.Float64(ctx)
  1079. if err != nil {
  1080. panic(err)
  1081. }
  1082. return v
  1083. }
  1084. // Bools returns list of bools from group-by.
  1085. // It is only allowed when executing a group-by query with one field.
  1086. func (ugb *UserGroupBy) Bools(ctx context.Context) ([]bool, error) {
  1087. if len(ugb.fields) > 1 {
  1088. return nil, errors.New("ent: UserGroupBy.Bools is not achievable when grouping more than 1 field")
  1089. }
  1090. var v []bool
  1091. if err := ugb.Scan(ctx, &v); err != nil {
  1092. return nil, err
  1093. }
  1094. return v, nil
  1095. }
  1096. // BoolsX is like Bools, but panics if an error occurs.
  1097. func (ugb *UserGroupBy) BoolsX(ctx context.Context) []bool {
  1098. v, err := ugb.Bools(ctx)
  1099. if err != nil {
  1100. panic(err)
  1101. }
  1102. return v
  1103. }
  1104. // Bool returns a single bool from a group-by query.
  1105. // It is only allowed when executing a group-by query with one field.
  1106. func (ugb *UserGroupBy) Bool(ctx context.Context) (_ bool, err error) {
  1107. var v []bool
  1108. if v, err = ugb.Bools(ctx); err != nil {
  1109. return
  1110. }
  1111. switch len(v) {
  1112. case 1:
  1113. return v[0], nil
  1114. case 0:
  1115. err = &NotFoundError{user.Label}
  1116. default:
  1117. err = fmt.Errorf("ent: UserGroupBy.Bools returned %d results when one was expected", len(v))
  1118. }
  1119. return
  1120. }
  1121. // BoolX is like Bool, but panics if an error occurs.
  1122. func (ugb *UserGroupBy) BoolX(ctx context.Context) bool {
  1123. v, err := ugb.Bool(ctx)
  1124. if err != nil {
  1125. panic(err)
  1126. }
  1127. return v
  1128. }
  1129. func (ugb *UserGroupBy) sqlScan(ctx context.Context, v interface{}) error {
  1130. for _, f := range ugb.fields {
  1131. if !user.ValidColumn(f) {
  1132. return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
  1133. }
  1134. }
  1135. selector := ugb.sqlQuery()
  1136. if err := selector.Err(); err != nil {
  1137. return err
  1138. }
  1139. rows := &sql.Rows{}
  1140. query, args := selector.Query()
  1141. if err := ugb.driver.Query(ctx, query, args, rows); err != nil {
  1142. return err
  1143. }
  1144. defer rows.Close()
  1145. return sql.ScanSlice(rows, v)
  1146. }
  1147. func (ugb *UserGroupBy) sqlQuery() *sql.Selector {
  1148. selector := ugb.sql.Select()
  1149. aggregation := make([]string, 0, len(ugb.fns))
  1150. for _, fn := range ugb.fns {
  1151. aggregation = append(aggregation, fn(selector))
  1152. }
  1153. // If no columns were selected in a custom aggregation function, the default
  1154. // selection is the fields used for "group-by", and the aggregation functions.
  1155. if len(selector.SelectedColumns()) == 0 {
  1156. columns := make([]string, 0, len(ugb.fields)+len(ugb.fns))
  1157. for _, f := range ugb.fields {
  1158. columns = append(columns, selector.C(f))
  1159. }
  1160. columns = append(columns, aggregation...)
  1161. selector.Select(columns...)
  1162. }
  1163. return selector.GroupBy(selector.Columns(ugb.fields...)...)
  1164. }
  1165. // UserSelect is the builder for selecting fields of User entities.
  1166. type UserSelect struct {
  1167. *UserQuery
  1168. // intermediate query (i.e. traversal path).
  1169. sql *sql.Selector
  1170. }
  1171. // Scan applies the selector query and scans the result into the given value.
  1172. func (us *UserSelect) Scan(ctx context.Context, v interface{}) error {
  1173. if err := us.prepareQuery(ctx); err != nil {
  1174. return err
  1175. }
  1176. us.sql = us.UserQuery.sqlQuery(ctx)
  1177. return us.sqlScan(ctx, v)
  1178. }
  1179. // ScanX is like Scan, but panics if an error occurs.
  1180. func (us *UserSelect) ScanX(ctx context.Context, v interface{}) {
  1181. if err := us.Scan(ctx, v); err != nil {
  1182. panic(err)
  1183. }
  1184. }
  1185. // Strings returns list of strings from a selector. It is only allowed when selecting one field.
  1186. func (us *UserSelect) Strings(ctx context.Context) ([]string, error) {
  1187. if len(us.fields) > 1 {
  1188. return nil, errors.New("ent: UserSelect.Strings is not achievable when selecting more than 1 field")
  1189. }
  1190. var v []string
  1191. if err := us.Scan(ctx, &v); err != nil {
  1192. return nil, err
  1193. }
  1194. return v, nil
  1195. }
  1196. // StringsX is like Strings, but panics if an error occurs.
  1197. func (us *UserSelect) StringsX(ctx context.Context) []string {
  1198. v, err := us.Strings(ctx)
  1199. if err != nil {
  1200. panic(err)
  1201. }
  1202. return v
  1203. }
  1204. // String returns a single string from a selector. It is only allowed when selecting one field.
  1205. func (us *UserSelect) String(ctx context.Context) (_ string, err error) {
  1206. var v []string
  1207. if v, err = us.Strings(ctx); err != nil {
  1208. return
  1209. }
  1210. switch len(v) {
  1211. case 1:
  1212. return v[0], nil
  1213. case 0:
  1214. err = &NotFoundError{user.Label}
  1215. default:
  1216. err = fmt.Errorf("ent: UserSelect.Strings returned %d results when one was expected", len(v))
  1217. }
  1218. return
  1219. }
  1220. // StringX is like String, but panics if an error occurs.
  1221. func (us *UserSelect) StringX(ctx context.Context) string {
  1222. v, err := us.String(ctx)
  1223. if err != nil {
  1224. panic(err)
  1225. }
  1226. return v
  1227. }
  1228. // Ints returns list of ints from a selector. It is only allowed when selecting one field.
  1229. func (us *UserSelect) Ints(ctx context.Context) ([]int, error) {
  1230. if len(us.fields) > 1 {
  1231. return nil, errors.New("ent: UserSelect.Ints is not achievable when selecting more than 1 field")
  1232. }
  1233. var v []int
  1234. if err := us.Scan(ctx, &v); err != nil {
  1235. return nil, err
  1236. }
  1237. return v, nil
  1238. }
  1239. // IntsX is like Ints, but panics if an error occurs.
  1240. func (us *UserSelect) IntsX(ctx context.Context) []int {
  1241. v, err := us.Ints(ctx)
  1242. if err != nil {
  1243. panic(err)
  1244. }
  1245. return v
  1246. }
  1247. // Int returns a single int from a selector. It is only allowed when selecting one field.
  1248. func (us *UserSelect) Int(ctx context.Context) (_ int, err error) {
  1249. var v []int
  1250. if v, err = us.Ints(ctx); err != nil {
  1251. return
  1252. }
  1253. switch len(v) {
  1254. case 1:
  1255. return v[0], nil
  1256. case 0:
  1257. err = &NotFoundError{user.Label}
  1258. default:
  1259. err = fmt.Errorf("ent: UserSelect.Ints returned %d results when one was expected", len(v))
  1260. }
  1261. return
  1262. }
  1263. // IntX is like Int, but panics if an error occurs.
  1264. func (us *UserSelect) IntX(ctx context.Context) int {
  1265. v, err := us.Int(ctx)
  1266. if err != nil {
  1267. panic(err)
  1268. }
  1269. return v
  1270. }
  1271. // Float64s returns list of float64s from a selector. It is only allowed when selecting one field.
  1272. func (us *UserSelect) Float64s(ctx context.Context) ([]float64, error) {
  1273. if len(us.fields) > 1 {
  1274. return nil, errors.New("ent: UserSelect.Float64s is not achievable when selecting more than 1 field")
  1275. }
  1276. var v []float64
  1277. if err := us.Scan(ctx, &v); err != nil {
  1278. return nil, err
  1279. }
  1280. return v, nil
  1281. }
  1282. // Float64sX is like Float64s, but panics if an error occurs.
  1283. func (us *UserSelect) Float64sX(ctx context.Context) []float64 {
  1284. v, err := us.Float64s(ctx)
  1285. if err != nil {
  1286. panic(err)
  1287. }
  1288. return v
  1289. }
  1290. // Float64 returns a single float64 from a selector. It is only allowed when selecting one field.
  1291. func (us *UserSelect) Float64(ctx context.Context) (_ float64, err error) {
  1292. var v []float64
  1293. if v, err = us.Float64s(ctx); err != nil {
  1294. return
  1295. }
  1296. switch len(v) {
  1297. case 1:
  1298. return v[0], nil
  1299. case 0:
  1300. err = &NotFoundError{user.Label}
  1301. default:
  1302. err = fmt.Errorf("ent: UserSelect.Float64s returned %d results when one was expected", len(v))
  1303. }
  1304. return
  1305. }
  1306. // Float64X is like Float64, but panics if an error occurs.
  1307. func (us *UserSelect) Float64X(ctx context.Context) float64 {
  1308. v, err := us.Float64(ctx)
  1309. if err != nil {
  1310. panic(err)
  1311. }
  1312. return v
  1313. }
  1314. // Bools returns list of bools from a selector. It is only allowed when selecting one field.
  1315. func (us *UserSelect) Bools(ctx context.Context) ([]bool, error) {
  1316. if len(us.fields) > 1 {
  1317. return nil, errors.New("ent: UserSelect.Bools is not achievable when selecting more than 1 field")
  1318. }
  1319. var v []bool
  1320. if err := us.Scan(ctx, &v); err != nil {
  1321. return nil, err
  1322. }
  1323. return v, nil
  1324. }
  1325. // BoolsX is like Bools, but panics if an error occurs.
  1326. func (us *UserSelect) BoolsX(ctx context.Context) []bool {
  1327. v, err := us.Bools(ctx)
  1328. if err != nil {
  1329. panic(err)
  1330. }
  1331. return v
  1332. }
  1333. // Bool returns a single bool from a selector. It is only allowed when selecting one field.
  1334. func (us *UserSelect) Bool(ctx context.Context) (_ bool, err error) {
  1335. var v []bool
  1336. if v, err = us.Bools(ctx); err != nil {
  1337. return
  1338. }
  1339. switch len(v) {
  1340. case 1:
  1341. return v[0], nil
  1342. case 0:
  1343. err = &NotFoundError{user.Label}
  1344. default:
  1345. err = fmt.Errorf("ent: UserSelect.Bools returned %d results when one was expected", len(v))
  1346. }
  1347. return
  1348. }
  1349. // BoolX is like Bool, but panics if an error occurs.
  1350. func (us *UserSelect) BoolX(ctx context.Context) bool {
  1351. v, err := us.Bool(ctx)
  1352. if err != nil {
  1353. panic(err)
  1354. }
  1355. return v
  1356. }
  1357. func (us *UserSelect) sqlScan(ctx context.Context, v interface{}) error {
  1358. rows := &sql.Rows{}
  1359. query, args := us.sql.Query()
  1360. if err := us.driver.Query(ctx, query, args, rows); err != nil {
  1361. return err
  1362. }
  1363. defer rows.Close()
  1364. return sql.ScanSlice(rows, v)
  1365. }