employeeconfig_query.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "math"
  7. "wechat-api/ent/employeeconfig"
  8. "wechat-api/ent/predicate"
  9. "entgo.io/ent/dialect/sql"
  10. "entgo.io/ent/dialect/sql/sqlgraph"
  11. "entgo.io/ent/schema/field"
  12. )
  13. // EmployeeConfigQuery is the builder for querying EmployeeConfig entities.
  14. type EmployeeConfigQuery struct {
  15. config
  16. ctx *QueryContext
  17. order []employeeconfig.OrderOption
  18. inters []Interceptor
  19. predicates []predicate.EmployeeConfig
  20. // intermediate query (i.e. traversal path).
  21. sql *sql.Selector
  22. path func(context.Context) (*sql.Selector, error)
  23. }
  24. // Where adds a new predicate for the EmployeeConfigQuery builder.
  25. func (ecq *EmployeeConfigQuery) Where(ps ...predicate.EmployeeConfig) *EmployeeConfigQuery {
  26. ecq.predicates = append(ecq.predicates, ps...)
  27. return ecq
  28. }
  29. // Limit the number of records to be returned by this query.
  30. func (ecq *EmployeeConfigQuery) Limit(limit int) *EmployeeConfigQuery {
  31. ecq.ctx.Limit = &limit
  32. return ecq
  33. }
  34. // Offset to start from.
  35. func (ecq *EmployeeConfigQuery) Offset(offset int) *EmployeeConfigQuery {
  36. ecq.ctx.Offset = &offset
  37. return ecq
  38. }
  39. // Unique configures the query builder to filter duplicate records on query.
  40. // By default, unique is set to true, and can be disabled using this method.
  41. func (ecq *EmployeeConfigQuery) Unique(unique bool) *EmployeeConfigQuery {
  42. ecq.ctx.Unique = &unique
  43. return ecq
  44. }
  45. // Order specifies how the records should be ordered.
  46. func (ecq *EmployeeConfigQuery) Order(o ...employeeconfig.OrderOption) *EmployeeConfigQuery {
  47. ecq.order = append(ecq.order, o...)
  48. return ecq
  49. }
  50. // First returns the first EmployeeConfig entity from the query.
  51. // Returns a *NotFoundError when no EmployeeConfig was found.
  52. func (ecq *EmployeeConfigQuery) First(ctx context.Context) (*EmployeeConfig, error) {
  53. nodes, err := ecq.Limit(1).All(setContextOp(ctx, ecq.ctx, "First"))
  54. if err != nil {
  55. return nil, err
  56. }
  57. if len(nodes) == 0 {
  58. return nil, &NotFoundError{employeeconfig.Label}
  59. }
  60. return nodes[0], nil
  61. }
  62. // FirstX is like First, but panics if an error occurs.
  63. func (ecq *EmployeeConfigQuery) FirstX(ctx context.Context) *EmployeeConfig {
  64. node, err := ecq.First(ctx)
  65. if err != nil && !IsNotFound(err) {
  66. panic(err)
  67. }
  68. return node
  69. }
  70. // FirstID returns the first EmployeeConfig ID from the query.
  71. // Returns a *NotFoundError when no EmployeeConfig ID was found.
  72. func (ecq *EmployeeConfigQuery) FirstID(ctx context.Context) (id uint64, err error) {
  73. var ids []uint64
  74. if ids, err = ecq.Limit(1).IDs(setContextOp(ctx, ecq.ctx, "FirstID")); err != nil {
  75. return
  76. }
  77. if len(ids) == 0 {
  78. err = &NotFoundError{employeeconfig.Label}
  79. return
  80. }
  81. return ids[0], nil
  82. }
  83. // FirstIDX is like FirstID, but panics if an error occurs.
  84. func (ecq *EmployeeConfigQuery) FirstIDX(ctx context.Context) uint64 {
  85. id, err := ecq.FirstID(ctx)
  86. if err != nil && !IsNotFound(err) {
  87. panic(err)
  88. }
  89. return id
  90. }
  91. // Only returns a single EmployeeConfig entity found by the query, ensuring it only returns one.
  92. // Returns a *NotSingularError when more than one EmployeeConfig entity is found.
  93. // Returns a *NotFoundError when no EmployeeConfig entities are found.
  94. func (ecq *EmployeeConfigQuery) Only(ctx context.Context) (*EmployeeConfig, error) {
  95. nodes, err := ecq.Limit(2).All(setContextOp(ctx, ecq.ctx, "Only"))
  96. if err != nil {
  97. return nil, err
  98. }
  99. switch len(nodes) {
  100. case 1:
  101. return nodes[0], nil
  102. case 0:
  103. return nil, &NotFoundError{employeeconfig.Label}
  104. default:
  105. return nil, &NotSingularError{employeeconfig.Label}
  106. }
  107. }
  108. // OnlyX is like Only, but panics if an error occurs.
  109. func (ecq *EmployeeConfigQuery) OnlyX(ctx context.Context) *EmployeeConfig {
  110. node, err := ecq.Only(ctx)
  111. if err != nil {
  112. panic(err)
  113. }
  114. return node
  115. }
  116. // OnlyID is like Only, but returns the only EmployeeConfig ID in the query.
  117. // Returns a *NotSingularError when more than one EmployeeConfig ID is found.
  118. // Returns a *NotFoundError when no entities are found.
  119. func (ecq *EmployeeConfigQuery) OnlyID(ctx context.Context) (id uint64, err error) {
  120. var ids []uint64
  121. if ids, err = ecq.Limit(2).IDs(setContextOp(ctx, ecq.ctx, "OnlyID")); err != nil {
  122. return
  123. }
  124. switch len(ids) {
  125. case 1:
  126. id = ids[0]
  127. case 0:
  128. err = &NotFoundError{employeeconfig.Label}
  129. default:
  130. err = &NotSingularError{employeeconfig.Label}
  131. }
  132. return
  133. }
  134. // OnlyIDX is like OnlyID, but panics if an error occurs.
  135. func (ecq *EmployeeConfigQuery) OnlyIDX(ctx context.Context) uint64 {
  136. id, err := ecq.OnlyID(ctx)
  137. if err != nil {
  138. panic(err)
  139. }
  140. return id
  141. }
  142. // All executes the query and returns a list of EmployeeConfigs.
  143. func (ecq *EmployeeConfigQuery) All(ctx context.Context) ([]*EmployeeConfig, error) {
  144. ctx = setContextOp(ctx, ecq.ctx, "All")
  145. if err := ecq.prepareQuery(ctx); err != nil {
  146. return nil, err
  147. }
  148. qr := querierAll[[]*EmployeeConfig, *EmployeeConfigQuery]()
  149. return withInterceptors[[]*EmployeeConfig](ctx, ecq, qr, ecq.inters)
  150. }
  151. // AllX is like All, but panics if an error occurs.
  152. func (ecq *EmployeeConfigQuery) AllX(ctx context.Context) []*EmployeeConfig {
  153. nodes, err := ecq.All(ctx)
  154. if err != nil {
  155. panic(err)
  156. }
  157. return nodes
  158. }
  159. // IDs executes the query and returns a list of EmployeeConfig IDs.
  160. func (ecq *EmployeeConfigQuery) IDs(ctx context.Context) (ids []uint64, err error) {
  161. if ecq.ctx.Unique == nil && ecq.path != nil {
  162. ecq.Unique(true)
  163. }
  164. ctx = setContextOp(ctx, ecq.ctx, "IDs")
  165. if err = ecq.Select(employeeconfig.FieldID).Scan(ctx, &ids); err != nil {
  166. return nil, err
  167. }
  168. return ids, nil
  169. }
  170. // IDsX is like IDs, but panics if an error occurs.
  171. func (ecq *EmployeeConfigQuery) IDsX(ctx context.Context) []uint64 {
  172. ids, err := ecq.IDs(ctx)
  173. if err != nil {
  174. panic(err)
  175. }
  176. return ids
  177. }
  178. // Count returns the count of the given query.
  179. func (ecq *EmployeeConfigQuery) Count(ctx context.Context) (int, error) {
  180. ctx = setContextOp(ctx, ecq.ctx, "Count")
  181. if err := ecq.prepareQuery(ctx); err != nil {
  182. return 0, err
  183. }
  184. return withInterceptors[int](ctx, ecq, querierCount[*EmployeeConfigQuery](), ecq.inters)
  185. }
  186. // CountX is like Count, but panics if an error occurs.
  187. func (ecq *EmployeeConfigQuery) CountX(ctx context.Context) int {
  188. count, err := ecq.Count(ctx)
  189. if err != nil {
  190. panic(err)
  191. }
  192. return count
  193. }
  194. // Exist returns true if the query has elements in the graph.
  195. func (ecq *EmployeeConfigQuery) Exist(ctx context.Context) (bool, error) {
  196. ctx = setContextOp(ctx, ecq.ctx, "Exist")
  197. switch _, err := ecq.FirstID(ctx); {
  198. case IsNotFound(err):
  199. return false, nil
  200. case err != nil:
  201. return false, fmt.Errorf("ent: check existence: %w", err)
  202. default:
  203. return true, nil
  204. }
  205. }
  206. // ExistX is like Exist, but panics if an error occurs.
  207. func (ecq *EmployeeConfigQuery) ExistX(ctx context.Context) bool {
  208. exist, err := ecq.Exist(ctx)
  209. if err != nil {
  210. panic(err)
  211. }
  212. return exist
  213. }
  214. // Clone returns a duplicate of the EmployeeConfigQuery builder, including all associated steps. It can be
  215. // used to prepare common query builders and use them differently after the clone is made.
  216. func (ecq *EmployeeConfigQuery) Clone() *EmployeeConfigQuery {
  217. if ecq == nil {
  218. return nil
  219. }
  220. return &EmployeeConfigQuery{
  221. config: ecq.config,
  222. ctx: ecq.ctx.Clone(),
  223. order: append([]employeeconfig.OrderOption{}, ecq.order...),
  224. inters: append([]Interceptor{}, ecq.inters...),
  225. predicates: append([]predicate.EmployeeConfig{}, ecq.predicates...),
  226. // clone intermediate query.
  227. sql: ecq.sql.Clone(),
  228. path: ecq.path,
  229. }
  230. }
  231. // GroupBy is used to group vertices by one or more fields/columns.
  232. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  233. //
  234. // Example:
  235. //
  236. // var v []struct {
  237. // CreatedAt time.Time `json:"created_at,omitempty"`
  238. // Count int `json:"count,omitempty"`
  239. // }
  240. //
  241. // client.EmployeeConfig.Query().
  242. // GroupBy(employeeconfig.FieldCreatedAt).
  243. // Aggregate(ent.Count()).
  244. // Scan(ctx, &v)
  245. func (ecq *EmployeeConfigQuery) GroupBy(field string, fields ...string) *EmployeeConfigGroupBy {
  246. ecq.ctx.Fields = append([]string{field}, fields...)
  247. grbuild := &EmployeeConfigGroupBy{build: ecq}
  248. grbuild.flds = &ecq.ctx.Fields
  249. grbuild.label = employeeconfig.Label
  250. grbuild.scan = grbuild.Scan
  251. return grbuild
  252. }
  253. // Select allows the selection one or more fields/columns for the given query,
  254. // instead of selecting all fields in the entity.
  255. //
  256. // Example:
  257. //
  258. // var v []struct {
  259. // CreatedAt time.Time `json:"created_at,omitempty"`
  260. // }
  261. //
  262. // client.EmployeeConfig.Query().
  263. // Select(employeeconfig.FieldCreatedAt).
  264. // Scan(ctx, &v)
  265. func (ecq *EmployeeConfigQuery) Select(fields ...string) *EmployeeConfigSelect {
  266. ecq.ctx.Fields = append(ecq.ctx.Fields, fields...)
  267. sbuild := &EmployeeConfigSelect{EmployeeConfigQuery: ecq}
  268. sbuild.label = employeeconfig.Label
  269. sbuild.flds, sbuild.scan = &ecq.ctx.Fields, sbuild.Scan
  270. return sbuild
  271. }
  272. // Aggregate returns a EmployeeConfigSelect configured with the given aggregations.
  273. func (ecq *EmployeeConfigQuery) Aggregate(fns ...AggregateFunc) *EmployeeConfigSelect {
  274. return ecq.Select().Aggregate(fns...)
  275. }
  276. func (ecq *EmployeeConfigQuery) prepareQuery(ctx context.Context) error {
  277. for _, inter := range ecq.inters {
  278. if inter == nil {
  279. return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
  280. }
  281. if trv, ok := inter.(Traverser); ok {
  282. if err := trv.Traverse(ctx, ecq); err != nil {
  283. return err
  284. }
  285. }
  286. }
  287. for _, f := range ecq.ctx.Fields {
  288. if !employeeconfig.ValidColumn(f) {
  289. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  290. }
  291. }
  292. if ecq.path != nil {
  293. prev, err := ecq.path(ctx)
  294. if err != nil {
  295. return err
  296. }
  297. ecq.sql = prev
  298. }
  299. return nil
  300. }
  301. func (ecq *EmployeeConfigQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*EmployeeConfig, error) {
  302. var (
  303. nodes = []*EmployeeConfig{}
  304. _spec = ecq.querySpec()
  305. )
  306. _spec.ScanValues = func(columns []string) ([]any, error) {
  307. return (*EmployeeConfig).scanValues(nil, columns)
  308. }
  309. _spec.Assign = func(columns []string, values []any) error {
  310. node := &EmployeeConfig{config: ecq.config}
  311. nodes = append(nodes, node)
  312. return node.assignValues(columns, values)
  313. }
  314. for i := range hooks {
  315. hooks[i](ctx, _spec)
  316. }
  317. if err := sqlgraph.QueryNodes(ctx, ecq.driver, _spec); err != nil {
  318. return nil, err
  319. }
  320. if len(nodes) == 0 {
  321. return nodes, nil
  322. }
  323. return nodes, nil
  324. }
  325. func (ecq *EmployeeConfigQuery) sqlCount(ctx context.Context) (int, error) {
  326. _spec := ecq.querySpec()
  327. _spec.Node.Columns = ecq.ctx.Fields
  328. if len(ecq.ctx.Fields) > 0 {
  329. _spec.Unique = ecq.ctx.Unique != nil && *ecq.ctx.Unique
  330. }
  331. return sqlgraph.CountNodes(ctx, ecq.driver, _spec)
  332. }
  333. func (ecq *EmployeeConfigQuery) querySpec() *sqlgraph.QuerySpec {
  334. _spec := sqlgraph.NewQuerySpec(employeeconfig.Table, employeeconfig.Columns, sqlgraph.NewFieldSpec(employeeconfig.FieldID, field.TypeUint64))
  335. _spec.From = ecq.sql
  336. if unique := ecq.ctx.Unique; unique != nil {
  337. _spec.Unique = *unique
  338. } else if ecq.path != nil {
  339. _spec.Unique = true
  340. }
  341. if fields := ecq.ctx.Fields; len(fields) > 0 {
  342. _spec.Node.Columns = make([]string, 0, len(fields))
  343. _spec.Node.Columns = append(_spec.Node.Columns, employeeconfig.FieldID)
  344. for i := range fields {
  345. if fields[i] != employeeconfig.FieldID {
  346. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  347. }
  348. }
  349. }
  350. if ps := ecq.predicates; len(ps) > 0 {
  351. _spec.Predicate = func(selector *sql.Selector) {
  352. for i := range ps {
  353. ps[i](selector)
  354. }
  355. }
  356. }
  357. if limit := ecq.ctx.Limit; limit != nil {
  358. _spec.Limit = *limit
  359. }
  360. if offset := ecq.ctx.Offset; offset != nil {
  361. _spec.Offset = *offset
  362. }
  363. if ps := ecq.order; len(ps) > 0 {
  364. _spec.Order = func(selector *sql.Selector) {
  365. for i := range ps {
  366. ps[i](selector)
  367. }
  368. }
  369. }
  370. return _spec
  371. }
  372. func (ecq *EmployeeConfigQuery) sqlQuery(ctx context.Context) *sql.Selector {
  373. builder := sql.Dialect(ecq.driver.Dialect())
  374. t1 := builder.Table(employeeconfig.Table)
  375. columns := ecq.ctx.Fields
  376. if len(columns) == 0 {
  377. columns = employeeconfig.Columns
  378. }
  379. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  380. if ecq.sql != nil {
  381. selector = ecq.sql
  382. selector.Select(selector.Columns(columns...)...)
  383. }
  384. if ecq.ctx.Unique != nil && *ecq.ctx.Unique {
  385. selector.Distinct()
  386. }
  387. for _, p := range ecq.predicates {
  388. p(selector)
  389. }
  390. for _, p := range ecq.order {
  391. p(selector)
  392. }
  393. if offset := ecq.ctx.Offset; offset != nil {
  394. // limit is mandatory for offset clause. We start
  395. // with default value, and override it below if needed.
  396. selector.Offset(*offset).Limit(math.MaxInt32)
  397. }
  398. if limit := ecq.ctx.Limit; limit != nil {
  399. selector.Limit(*limit)
  400. }
  401. return selector
  402. }
  403. // EmployeeConfigGroupBy is the group-by builder for EmployeeConfig entities.
  404. type EmployeeConfigGroupBy struct {
  405. selector
  406. build *EmployeeConfigQuery
  407. }
  408. // Aggregate adds the given aggregation functions to the group-by query.
  409. func (ecgb *EmployeeConfigGroupBy) Aggregate(fns ...AggregateFunc) *EmployeeConfigGroupBy {
  410. ecgb.fns = append(ecgb.fns, fns...)
  411. return ecgb
  412. }
  413. // Scan applies the selector query and scans the result into the given value.
  414. func (ecgb *EmployeeConfigGroupBy) Scan(ctx context.Context, v any) error {
  415. ctx = setContextOp(ctx, ecgb.build.ctx, "GroupBy")
  416. if err := ecgb.build.prepareQuery(ctx); err != nil {
  417. return err
  418. }
  419. return scanWithInterceptors[*EmployeeConfigQuery, *EmployeeConfigGroupBy](ctx, ecgb.build, ecgb, ecgb.build.inters, v)
  420. }
  421. func (ecgb *EmployeeConfigGroupBy) sqlScan(ctx context.Context, root *EmployeeConfigQuery, v any) error {
  422. selector := root.sqlQuery(ctx).Select()
  423. aggregation := make([]string, 0, len(ecgb.fns))
  424. for _, fn := range ecgb.fns {
  425. aggregation = append(aggregation, fn(selector))
  426. }
  427. if len(selector.SelectedColumns()) == 0 {
  428. columns := make([]string, 0, len(*ecgb.flds)+len(ecgb.fns))
  429. for _, f := range *ecgb.flds {
  430. columns = append(columns, selector.C(f))
  431. }
  432. columns = append(columns, aggregation...)
  433. selector.Select(columns...)
  434. }
  435. selector.GroupBy(selector.Columns(*ecgb.flds...)...)
  436. if err := selector.Err(); err != nil {
  437. return err
  438. }
  439. rows := &sql.Rows{}
  440. query, args := selector.Query()
  441. if err := ecgb.build.driver.Query(ctx, query, args, rows); err != nil {
  442. return err
  443. }
  444. defer rows.Close()
  445. return sql.ScanSlice(rows, v)
  446. }
  447. // EmployeeConfigSelect is the builder for selecting fields of EmployeeConfig entities.
  448. type EmployeeConfigSelect struct {
  449. *EmployeeConfigQuery
  450. selector
  451. }
  452. // Aggregate adds the given aggregation functions to the selector query.
  453. func (ecs *EmployeeConfigSelect) Aggregate(fns ...AggregateFunc) *EmployeeConfigSelect {
  454. ecs.fns = append(ecs.fns, fns...)
  455. return ecs
  456. }
  457. // Scan applies the selector query and scans the result into the given value.
  458. func (ecs *EmployeeConfigSelect) Scan(ctx context.Context, v any) error {
  459. ctx = setContextOp(ctx, ecs.ctx, "Select")
  460. if err := ecs.prepareQuery(ctx); err != nil {
  461. return err
  462. }
  463. return scanWithInterceptors[*EmployeeConfigQuery, *EmployeeConfigSelect](ctx, ecs.EmployeeConfigQuery, ecs, ecs.inters, v)
  464. }
  465. func (ecs *EmployeeConfigSelect) sqlScan(ctx context.Context, root *EmployeeConfigQuery, v any) error {
  466. selector := root.sqlQuery(ctx)
  467. aggregation := make([]string, 0, len(ecs.fns))
  468. for _, fn := range ecs.fns {
  469. aggregation = append(aggregation, fn(selector))
  470. }
  471. switch n := len(*ecs.selector.flds); {
  472. case n == 0 && len(aggregation) > 0:
  473. selector.Select(aggregation...)
  474. case n != 0 && len(aggregation) > 0:
  475. selector.AppendSelect(aggregation...)
  476. }
  477. rows := &sql.Rows{}
  478. query, args := selector.Query()
  479. if err := ecs.driver.Query(ctx, query, args, rows); err != nil {
  480. return err
  481. }
  482. defer rows.Close()
  483. return sql.ScanSlice(rows, v)
  484. }