publishregistration_query.go 16 KB

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