pushregistration_query.go 16 KB

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