subscriptionplan_query.go 17 KB

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