wxcard_query.go 14 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/predicate"
  8. "wechat-api/ent/wxcard"
  9. "entgo.io/ent/dialect/sql"
  10. "entgo.io/ent/dialect/sql/sqlgraph"
  11. "entgo.io/ent/schema/field"
  12. )
  13. // WxCardQuery is the builder for querying WxCard entities.
  14. type WxCardQuery struct {
  15. config
  16. ctx *QueryContext
  17. order []wxcard.OrderOption
  18. inters []Interceptor
  19. predicates []predicate.WxCard
  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 WxCardQuery builder.
  25. func (wcq *WxCardQuery) Where(ps ...predicate.WxCard) *WxCardQuery {
  26. wcq.predicates = append(wcq.predicates, ps...)
  27. return wcq
  28. }
  29. // Limit the number of records to be returned by this query.
  30. func (wcq *WxCardQuery) Limit(limit int) *WxCardQuery {
  31. wcq.ctx.Limit = &limit
  32. return wcq
  33. }
  34. // Offset to start from.
  35. func (wcq *WxCardQuery) Offset(offset int) *WxCardQuery {
  36. wcq.ctx.Offset = &offset
  37. return wcq
  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 (wcq *WxCardQuery) Unique(unique bool) *WxCardQuery {
  42. wcq.ctx.Unique = &unique
  43. return wcq
  44. }
  45. // Order specifies how the records should be ordered.
  46. func (wcq *WxCardQuery) Order(o ...wxcard.OrderOption) *WxCardQuery {
  47. wcq.order = append(wcq.order, o...)
  48. return wcq
  49. }
  50. // First returns the first WxCard entity from the query.
  51. // Returns a *NotFoundError when no WxCard was found.
  52. func (wcq *WxCardQuery) First(ctx context.Context) (*WxCard, error) {
  53. nodes, err := wcq.Limit(1).All(setContextOp(ctx, wcq.ctx, "First"))
  54. if err != nil {
  55. return nil, err
  56. }
  57. if len(nodes) == 0 {
  58. return nil, &NotFoundError{wxcard.Label}
  59. }
  60. return nodes[0], nil
  61. }
  62. // FirstX is like First, but panics if an error occurs.
  63. func (wcq *WxCardQuery) FirstX(ctx context.Context) *WxCard {
  64. node, err := wcq.First(ctx)
  65. if err != nil && !IsNotFound(err) {
  66. panic(err)
  67. }
  68. return node
  69. }
  70. // FirstID returns the first WxCard ID from the query.
  71. // Returns a *NotFoundError when no WxCard ID was found.
  72. func (wcq *WxCardQuery) FirstID(ctx context.Context) (id uint64, err error) {
  73. var ids []uint64
  74. if ids, err = wcq.Limit(1).IDs(setContextOp(ctx, wcq.ctx, "FirstID")); err != nil {
  75. return
  76. }
  77. if len(ids) == 0 {
  78. err = &NotFoundError{wxcard.Label}
  79. return
  80. }
  81. return ids[0], nil
  82. }
  83. // FirstIDX is like FirstID, but panics if an error occurs.
  84. func (wcq *WxCardQuery) FirstIDX(ctx context.Context) uint64 {
  85. id, err := wcq.FirstID(ctx)
  86. if err != nil && !IsNotFound(err) {
  87. panic(err)
  88. }
  89. return id
  90. }
  91. // Only returns a single WxCard entity found by the query, ensuring it only returns one.
  92. // Returns a *NotSingularError when more than one WxCard entity is found.
  93. // Returns a *NotFoundError when no WxCard entities are found.
  94. func (wcq *WxCardQuery) Only(ctx context.Context) (*WxCard, error) {
  95. nodes, err := wcq.Limit(2).All(setContextOp(ctx, wcq.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{wxcard.Label}
  104. default:
  105. return nil, &NotSingularError{wxcard.Label}
  106. }
  107. }
  108. // OnlyX is like Only, but panics if an error occurs.
  109. func (wcq *WxCardQuery) OnlyX(ctx context.Context) *WxCard {
  110. node, err := wcq.Only(ctx)
  111. if err != nil {
  112. panic(err)
  113. }
  114. return node
  115. }
  116. // OnlyID is like Only, but returns the only WxCard ID in the query.
  117. // Returns a *NotSingularError when more than one WxCard ID is found.
  118. // Returns a *NotFoundError when no entities are found.
  119. func (wcq *WxCardQuery) OnlyID(ctx context.Context) (id uint64, err error) {
  120. var ids []uint64
  121. if ids, err = wcq.Limit(2).IDs(setContextOp(ctx, wcq.ctx, "OnlyID")); err != nil {
  122. return
  123. }
  124. switch len(ids) {
  125. case 1:
  126. id = ids[0]
  127. case 0:
  128. err = &NotFoundError{wxcard.Label}
  129. default:
  130. err = &NotSingularError{wxcard.Label}
  131. }
  132. return
  133. }
  134. // OnlyIDX is like OnlyID, but panics if an error occurs.
  135. func (wcq *WxCardQuery) OnlyIDX(ctx context.Context) uint64 {
  136. id, err := wcq.OnlyID(ctx)
  137. if err != nil {
  138. panic(err)
  139. }
  140. return id
  141. }
  142. // All executes the query and returns a list of WxCards.
  143. func (wcq *WxCardQuery) All(ctx context.Context) ([]*WxCard, error) {
  144. ctx = setContextOp(ctx, wcq.ctx, "All")
  145. if err := wcq.prepareQuery(ctx); err != nil {
  146. return nil, err
  147. }
  148. qr := querierAll[[]*WxCard, *WxCardQuery]()
  149. return withInterceptors[[]*WxCard](ctx, wcq, qr, wcq.inters)
  150. }
  151. // AllX is like All, but panics if an error occurs.
  152. func (wcq *WxCardQuery) AllX(ctx context.Context) []*WxCard {
  153. nodes, err := wcq.All(ctx)
  154. if err != nil {
  155. panic(err)
  156. }
  157. return nodes
  158. }
  159. // IDs executes the query and returns a list of WxCard IDs.
  160. func (wcq *WxCardQuery) IDs(ctx context.Context) (ids []uint64, err error) {
  161. if wcq.ctx.Unique == nil && wcq.path != nil {
  162. wcq.Unique(true)
  163. }
  164. ctx = setContextOp(ctx, wcq.ctx, "IDs")
  165. if err = wcq.Select(wxcard.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 (wcq *WxCardQuery) IDsX(ctx context.Context) []uint64 {
  172. ids, err := wcq.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 (wcq *WxCardQuery) Count(ctx context.Context) (int, error) {
  180. ctx = setContextOp(ctx, wcq.ctx, "Count")
  181. if err := wcq.prepareQuery(ctx); err != nil {
  182. return 0, err
  183. }
  184. return withInterceptors[int](ctx, wcq, querierCount[*WxCardQuery](), wcq.inters)
  185. }
  186. // CountX is like Count, but panics if an error occurs.
  187. func (wcq *WxCardQuery) CountX(ctx context.Context) int {
  188. count, err := wcq.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 (wcq *WxCardQuery) Exist(ctx context.Context) (bool, error) {
  196. ctx = setContextOp(ctx, wcq.ctx, "Exist")
  197. switch _, err := wcq.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 (wcq *WxCardQuery) ExistX(ctx context.Context) bool {
  208. exist, err := wcq.Exist(ctx)
  209. if err != nil {
  210. panic(err)
  211. }
  212. return exist
  213. }
  214. // Clone returns a duplicate of the WxCardQuery 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 (wcq *WxCardQuery) Clone() *WxCardQuery {
  217. if wcq == nil {
  218. return nil
  219. }
  220. return &WxCardQuery{
  221. config: wcq.config,
  222. ctx: wcq.ctx.Clone(),
  223. order: append([]wxcard.OrderOption{}, wcq.order...),
  224. inters: append([]Interceptor{}, wcq.inters...),
  225. predicates: append([]predicate.WxCard{}, wcq.predicates...),
  226. // clone intermediate query.
  227. sql: wcq.sql.Clone(),
  228. path: wcq.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.WxCard.Query().
  242. // GroupBy(wxcard.FieldCreatedAt).
  243. // Aggregate(ent.Count()).
  244. // Scan(ctx, &v)
  245. func (wcq *WxCardQuery) GroupBy(field string, fields ...string) *WxCardGroupBy {
  246. wcq.ctx.Fields = append([]string{field}, fields...)
  247. grbuild := &WxCardGroupBy{build: wcq}
  248. grbuild.flds = &wcq.ctx.Fields
  249. grbuild.label = wxcard.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.WxCard.Query().
  263. // Select(wxcard.FieldCreatedAt).
  264. // Scan(ctx, &v)
  265. func (wcq *WxCardQuery) Select(fields ...string) *WxCardSelect {
  266. wcq.ctx.Fields = append(wcq.ctx.Fields, fields...)
  267. sbuild := &WxCardSelect{WxCardQuery: wcq}
  268. sbuild.label = wxcard.Label
  269. sbuild.flds, sbuild.scan = &wcq.ctx.Fields, sbuild.Scan
  270. return sbuild
  271. }
  272. // Aggregate returns a WxCardSelect configured with the given aggregations.
  273. func (wcq *WxCardQuery) Aggregate(fns ...AggregateFunc) *WxCardSelect {
  274. return wcq.Select().Aggregate(fns...)
  275. }
  276. func (wcq *WxCardQuery) prepareQuery(ctx context.Context) error {
  277. for _, inter := range wcq.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, wcq); err != nil {
  283. return err
  284. }
  285. }
  286. }
  287. for _, f := range wcq.ctx.Fields {
  288. if !wxcard.ValidColumn(f) {
  289. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  290. }
  291. }
  292. if wcq.path != nil {
  293. prev, err := wcq.path(ctx)
  294. if err != nil {
  295. return err
  296. }
  297. wcq.sql = prev
  298. }
  299. return nil
  300. }
  301. func (wcq *WxCardQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*WxCard, error) {
  302. var (
  303. nodes = []*WxCard{}
  304. _spec = wcq.querySpec()
  305. )
  306. _spec.ScanValues = func(columns []string) ([]any, error) {
  307. return (*WxCard).scanValues(nil, columns)
  308. }
  309. _spec.Assign = func(columns []string, values []any) error {
  310. node := &WxCard{config: wcq.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, wcq.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 (wcq *WxCardQuery) sqlCount(ctx context.Context) (int, error) {
  326. _spec := wcq.querySpec()
  327. _spec.Node.Columns = wcq.ctx.Fields
  328. if len(wcq.ctx.Fields) > 0 {
  329. _spec.Unique = wcq.ctx.Unique != nil && *wcq.ctx.Unique
  330. }
  331. return sqlgraph.CountNodes(ctx, wcq.driver, _spec)
  332. }
  333. func (wcq *WxCardQuery) querySpec() *sqlgraph.QuerySpec {
  334. _spec := sqlgraph.NewQuerySpec(wxcard.Table, wxcard.Columns, sqlgraph.NewFieldSpec(wxcard.FieldID, field.TypeUint64))
  335. _spec.From = wcq.sql
  336. if unique := wcq.ctx.Unique; unique != nil {
  337. _spec.Unique = *unique
  338. } else if wcq.path != nil {
  339. _spec.Unique = true
  340. }
  341. if fields := wcq.ctx.Fields; len(fields) > 0 {
  342. _spec.Node.Columns = make([]string, 0, len(fields))
  343. _spec.Node.Columns = append(_spec.Node.Columns, wxcard.FieldID)
  344. for i := range fields {
  345. if fields[i] != wxcard.FieldID {
  346. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  347. }
  348. }
  349. }
  350. if ps := wcq.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 := wcq.ctx.Limit; limit != nil {
  358. _spec.Limit = *limit
  359. }
  360. if offset := wcq.ctx.Offset; offset != nil {
  361. _spec.Offset = *offset
  362. }
  363. if ps := wcq.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 (wcq *WxCardQuery) sqlQuery(ctx context.Context) *sql.Selector {
  373. builder := sql.Dialect(wcq.driver.Dialect())
  374. t1 := builder.Table(wxcard.Table)
  375. columns := wcq.ctx.Fields
  376. if len(columns) == 0 {
  377. columns = wxcard.Columns
  378. }
  379. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  380. if wcq.sql != nil {
  381. selector = wcq.sql
  382. selector.Select(selector.Columns(columns...)...)
  383. }
  384. if wcq.ctx.Unique != nil && *wcq.ctx.Unique {
  385. selector.Distinct()
  386. }
  387. for _, p := range wcq.predicates {
  388. p(selector)
  389. }
  390. for _, p := range wcq.order {
  391. p(selector)
  392. }
  393. if offset := wcq.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 := wcq.ctx.Limit; limit != nil {
  399. selector.Limit(*limit)
  400. }
  401. return selector
  402. }
  403. // WxCardGroupBy is the group-by builder for WxCard entities.
  404. type WxCardGroupBy struct {
  405. selector
  406. build *WxCardQuery
  407. }
  408. // Aggregate adds the given aggregation functions to the group-by query.
  409. func (wcgb *WxCardGroupBy) Aggregate(fns ...AggregateFunc) *WxCardGroupBy {
  410. wcgb.fns = append(wcgb.fns, fns...)
  411. return wcgb
  412. }
  413. // Scan applies the selector query and scans the result into the given value.
  414. func (wcgb *WxCardGroupBy) Scan(ctx context.Context, v any) error {
  415. ctx = setContextOp(ctx, wcgb.build.ctx, "GroupBy")
  416. if err := wcgb.build.prepareQuery(ctx); err != nil {
  417. return err
  418. }
  419. return scanWithInterceptors[*WxCardQuery, *WxCardGroupBy](ctx, wcgb.build, wcgb, wcgb.build.inters, v)
  420. }
  421. func (wcgb *WxCardGroupBy) sqlScan(ctx context.Context, root *WxCardQuery, v any) error {
  422. selector := root.sqlQuery(ctx).Select()
  423. aggregation := make([]string, 0, len(wcgb.fns))
  424. for _, fn := range wcgb.fns {
  425. aggregation = append(aggregation, fn(selector))
  426. }
  427. if len(selector.SelectedColumns()) == 0 {
  428. columns := make([]string, 0, len(*wcgb.flds)+len(wcgb.fns))
  429. for _, f := range *wcgb.flds {
  430. columns = append(columns, selector.C(f))
  431. }
  432. columns = append(columns, aggregation...)
  433. selector.Select(columns...)
  434. }
  435. selector.GroupBy(selector.Columns(*wcgb.flds...)...)
  436. if err := selector.Err(); err != nil {
  437. return err
  438. }
  439. rows := &sql.Rows{}
  440. query, args := selector.Query()
  441. if err := wcgb.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. // WxCardSelect is the builder for selecting fields of WxCard entities.
  448. type WxCardSelect struct {
  449. *WxCardQuery
  450. selector
  451. }
  452. // Aggregate adds the given aggregation functions to the selector query.
  453. func (wcs *WxCardSelect) Aggregate(fns ...AggregateFunc) *WxCardSelect {
  454. wcs.fns = append(wcs.fns, fns...)
  455. return wcs
  456. }
  457. // Scan applies the selector query and scans the result into the given value.
  458. func (wcs *WxCardSelect) Scan(ctx context.Context, v any) error {
  459. ctx = setContextOp(ctx, wcs.ctx, "Select")
  460. if err := wcs.prepareQuery(ctx); err != nil {
  461. return err
  462. }
  463. return scanWithInterceptors[*WxCardQuery, *WxCardSelect](ctx, wcs.WxCardQuery, wcs, wcs.inters, v)
  464. }
  465. func (wcs *WxCardSelect) sqlScan(ctx context.Context, root *WxCardQuery, v any) error {
  466. selector := root.sqlQuery(ctx)
  467. aggregation := make([]string, 0, len(wcs.fns))
  468. for _, fn := range wcs.fns {
  469. aggregation = append(aggregation, fn(selector))
  470. }
  471. switch n := len(*wcs.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 := wcs.driver.Query(ctx, query, args, rows); err != nil {
  480. return err
  481. }
  482. defer rows.Close()
  483. return sql.ScanSlice(rows, v)
  484. }