employee_query.go 20 KB

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