ent.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "reflect"
  8. "sync"
  9. "w303a/server/app/gate/internal/data/ent/firebase"
  10. "w303a/server/app/gate/internal/data/ent/pushregistration"
  11. "entgo.io/ent"
  12. "entgo.io/ent/dialect/sql"
  13. "entgo.io/ent/dialect/sql/sqlgraph"
  14. )
  15. // ent aliases to avoid import conflicts in user's code.
  16. type (
  17. Op = ent.Op
  18. Hook = ent.Hook
  19. Value = ent.Value
  20. Query = ent.Query
  21. QueryContext = ent.QueryContext
  22. Querier = ent.Querier
  23. QuerierFunc = ent.QuerierFunc
  24. Interceptor = ent.Interceptor
  25. InterceptFunc = ent.InterceptFunc
  26. Traverser = ent.Traverser
  27. TraverseFunc = ent.TraverseFunc
  28. Policy = ent.Policy
  29. Mutator = ent.Mutator
  30. Mutation = ent.Mutation
  31. MutateFunc = ent.MutateFunc
  32. )
  33. type clientCtxKey struct{}
  34. // FromContext returns a Client stored inside a context, or nil if there isn't one.
  35. func FromContext(ctx context.Context) *Client {
  36. c, _ := ctx.Value(clientCtxKey{}).(*Client)
  37. return c
  38. }
  39. // NewContext returns a new context with the given Client attached.
  40. func NewContext(parent context.Context, c *Client) context.Context {
  41. return context.WithValue(parent, clientCtxKey{}, c)
  42. }
  43. type txCtxKey struct{}
  44. // TxFromContext returns a Tx stored inside a context, or nil if there isn't one.
  45. func TxFromContext(ctx context.Context) *Tx {
  46. tx, _ := ctx.Value(txCtxKey{}).(*Tx)
  47. return tx
  48. }
  49. // NewTxContext returns a new context with the given Tx attached.
  50. func NewTxContext(parent context.Context, tx *Tx) context.Context {
  51. return context.WithValue(parent, txCtxKey{}, tx)
  52. }
  53. // OrderFunc applies an ordering on the sql selector.
  54. // Deprecated: Use Asc/Desc functions or the package builders instead.
  55. type OrderFunc func(*sql.Selector)
  56. var (
  57. initCheck sync.Once
  58. columnCheck sql.ColumnCheck
  59. )
  60. // checkColumn checks if the column exists in the given table.
  61. func checkColumn(table, column string) error {
  62. initCheck.Do(func() {
  63. columnCheck = sql.NewColumnCheck(map[string]func(string) bool{
  64. firebase.Table: firebase.ValidColumn,
  65. pushregistration.Table: pushregistration.ValidColumn,
  66. })
  67. })
  68. return columnCheck(table, column)
  69. }
  70. // Asc applies the given fields in ASC order.
  71. func Asc(fields ...string) func(*sql.Selector) {
  72. return func(s *sql.Selector) {
  73. for _, f := range fields {
  74. if err := checkColumn(s.TableName(), f); err != nil {
  75. s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)})
  76. }
  77. s.OrderBy(sql.Asc(s.C(f)))
  78. }
  79. }
  80. }
  81. // Desc applies the given fields in DESC order.
  82. func Desc(fields ...string) func(*sql.Selector) {
  83. return func(s *sql.Selector) {
  84. for _, f := range fields {
  85. if err := checkColumn(s.TableName(), f); err != nil {
  86. s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)})
  87. }
  88. s.OrderBy(sql.Desc(s.C(f)))
  89. }
  90. }
  91. }
  92. // AggregateFunc applies an aggregation step on the group-by traversal/selector.
  93. type AggregateFunc func(*sql.Selector) string
  94. // As is a pseudo aggregation function for renaming another other functions with custom names. For example:
  95. //
  96. // GroupBy(field1, field2).
  97. // Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")).
  98. // Scan(ctx, &v)
  99. func As(fn AggregateFunc, end string) AggregateFunc {
  100. return func(s *sql.Selector) string {
  101. return sql.As(fn(s), end)
  102. }
  103. }
  104. // Count applies the "count" aggregation function on each group.
  105. func Count() AggregateFunc {
  106. return func(s *sql.Selector) string {
  107. return sql.Count("*")
  108. }
  109. }
  110. // Max applies the "max" aggregation function on the given field of each group.
  111. func Max(field string) AggregateFunc {
  112. return func(s *sql.Selector) string {
  113. if err := checkColumn(s.TableName(), field); err != nil {
  114. s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
  115. return ""
  116. }
  117. return sql.Max(s.C(field))
  118. }
  119. }
  120. // Mean applies the "mean" aggregation function on the given field of each group.
  121. func Mean(field string) AggregateFunc {
  122. return func(s *sql.Selector) string {
  123. if err := checkColumn(s.TableName(), field); err != nil {
  124. s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
  125. return ""
  126. }
  127. return sql.Avg(s.C(field))
  128. }
  129. }
  130. // Min applies the "min" aggregation function on the given field of each group.
  131. func Min(field string) AggregateFunc {
  132. return func(s *sql.Selector) string {
  133. if err := checkColumn(s.TableName(), field); err != nil {
  134. s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
  135. return ""
  136. }
  137. return sql.Min(s.C(field))
  138. }
  139. }
  140. // Sum applies the "sum" aggregation function on the given field of each group.
  141. func Sum(field string) AggregateFunc {
  142. return func(s *sql.Selector) string {
  143. if err := checkColumn(s.TableName(), field); err != nil {
  144. s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
  145. return ""
  146. }
  147. return sql.Sum(s.C(field))
  148. }
  149. }
  150. // ValidationError returns when validating a field or edge fails.
  151. type ValidationError struct {
  152. Name string // Field or edge name.
  153. err error
  154. }
  155. // Error implements the error interface.
  156. func (e *ValidationError) Error() string {
  157. return e.err.Error()
  158. }
  159. // Unwrap implements the errors.Wrapper interface.
  160. func (e *ValidationError) Unwrap() error {
  161. return e.err
  162. }
  163. // IsValidationError returns a boolean indicating whether the error is a validation error.
  164. func IsValidationError(err error) bool {
  165. if err == nil {
  166. return false
  167. }
  168. var e *ValidationError
  169. return errors.As(err, &e)
  170. }
  171. // NotFoundError returns when trying to fetch a specific entity and it was not found in the database.
  172. type NotFoundError struct {
  173. label string
  174. }
  175. // Error implements the error interface.
  176. func (e *NotFoundError) Error() string {
  177. return "ent: " + e.label + " not found"
  178. }
  179. // IsNotFound returns a boolean indicating whether the error is a not found error.
  180. func IsNotFound(err error) bool {
  181. if err == nil {
  182. return false
  183. }
  184. var e *NotFoundError
  185. return errors.As(err, &e)
  186. }
  187. // MaskNotFound masks not found error.
  188. func MaskNotFound(err error) error {
  189. if IsNotFound(err) {
  190. return nil
  191. }
  192. return err
  193. }
  194. // NotSingularError returns when trying to fetch a singular entity and more then one was found in the database.
  195. type NotSingularError struct {
  196. label string
  197. }
  198. // Error implements the error interface.
  199. func (e *NotSingularError) Error() string {
  200. return "ent: " + e.label + " not singular"
  201. }
  202. // IsNotSingular returns a boolean indicating whether the error is a not singular error.
  203. func IsNotSingular(err error) bool {
  204. if err == nil {
  205. return false
  206. }
  207. var e *NotSingularError
  208. return errors.As(err, &e)
  209. }
  210. // NotLoadedError returns when trying to get a node that was not loaded by the query.
  211. type NotLoadedError struct {
  212. edge string
  213. }
  214. // Error implements the error interface.
  215. func (e *NotLoadedError) Error() string {
  216. return "ent: " + e.edge + " edge was not loaded"
  217. }
  218. // IsNotLoaded returns a boolean indicating whether the error is a not loaded error.
  219. func IsNotLoaded(err error) bool {
  220. if err == nil {
  221. return false
  222. }
  223. var e *NotLoadedError
  224. return errors.As(err, &e)
  225. }
  226. // ConstraintError returns when trying to create/update one or more entities and
  227. // one or more of their constraints failed. For example, violation of edge or
  228. // field uniqueness.
  229. type ConstraintError struct {
  230. msg string
  231. wrap error
  232. }
  233. // Error implements the error interface.
  234. func (e ConstraintError) Error() string {
  235. return "ent: constraint failed: " + e.msg
  236. }
  237. // Unwrap implements the errors.Wrapper interface.
  238. func (e *ConstraintError) Unwrap() error {
  239. return e.wrap
  240. }
  241. // IsConstraintError returns a boolean indicating whether the error is a constraint failure.
  242. func IsConstraintError(err error) bool {
  243. if err == nil {
  244. return false
  245. }
  246. var e *ConstraintError
  247. return errors.As(err, &e)
  248. }
  249. // selector embedded by the different Select/GroupBy builders.
  250. type selector struct {
  251. label string
  252. flds *[]string
  253. fns []AggregateFunc
  254. scan func(context.Context, any) error
  255. }
  256. // ScanX is like Scan, but panics if an error occurs.
  257. func (s *selector) ScanX(ctx context.Context, v any) {
  258. if err := s.scan(ctx, v); err != nil {
  259. panic(err)
  260. }
  261. }
  262. // Strings returns list of strings from a selector. It is only allowed when selecting one field.
  263. func (s *selector) Strings(ctx context.Context) ([]string, error) {
  264. if len(*s.flds) > 1 {
  265. return nil, errors.New("ent: Strings is not achievable when selecting more than 1 field")
  266. }
  267. var v []string
  268. if err := s.scan(ctx, &v); err != nil {
  269. return nil, err
  270. }
  271. return v, nil
  272. }
  273. // StringsX is like Strings, but panics if an error occurs.
  274. func (s *selector) StringsX(ctx context.Context) []string {
  275. v, err := s.Strings(ctx)
  276. if err != nil {
  277. panic(err)
  278. }
  279. return v
  280. }
  281. // String returns a single string from a selector. It is only allowed when selecting one field.
  282. func (s *selector) String(ctx context.Context) (_ string, err error) {
  283. var v []string
  284. if v, err = s.Strings(ctx); err != nil {
  285. return
  286. }
  287. switch len(v) {
  288. case 1:
  289. return v[0], nil
  290. case 0:
  291. err = &NotFoundError{s.label}
  292. default:
  293. err = fmt.Errorf("ent: Strings returned %d results when one was expected", len(v))
  294. }
  295. return
  296. }
  297. // StringX is like String, but panics if an error occurs.
  298. func (s *selector) StringX(ctx context.Context) string {
  299. v, err := s.String(ctx)
  300. if err != nil {
  301. panic(err)
  302. }
  303. return v
  304. }
  305. // Ints returns list of ints from a selector. It is only allowed when selecting one field.
  306. func (s *selector) Ints(ctx context.Context) ([]int, error) {
  307. if len(*s.flds) > 1 {
  308. return nil, errors.New("ent: Ints is not achievable when selecting more than 1 field")
  309. }
  310. var v []int
  311. if err := s.scan(ctx, &v); err != nil {
  312. return nil, err
  313. }
  314. return v, nil
  315. }
  316. // IntsX is like Ints, but panics if an error occurs.
  317. func (s *selector) IntsX(ctx context.Context) []int {
  318. v, err := s.Ints(ctx)
  319. if err != nil {
  320. panic(err)
  321. }
  322. return v
  323. }
  324. // Int returns a single int from a selector. It is only allowed when selecting one field.
  325. func (s *selector) Int(ctx context.Context) (_ int, err error) {
  326. var v []int
  327. if v, err = s.Ints(ctx); err != nil {
  328. return
  329. }
  330. switch len(v) {
  331. case 1:
  332. return v[0], nil
  333. case 0:
  334. err = &NotFoundError{s.label}
  335. default:
  336. err = fmt.Errorf("ent: Ints returned %d results when one was expected", len(v))
  337. }
  338. return
  339. }
  340. // IntX is like Int, but panics if an error occurs.
  341. func (s *selector) IntX(ctx context.Context) int {
  342. v, err := s.Int(ctx)
  343. if err != nil {
  344. panic(err)
  345. }
  346. return v
  347. }
  348. // Float64s returns list of float64s from a selector. It is only allowed when selecting one field.
  349. func (s *selector) Float64s(ctx context.Context) ([]float64, error) {
  350. if len(*s.flds) > 1 {
  351. return nil, errors.New("ent: Float64s is not achievable when selecting more than 1 field")
  352. }
  353. var v []float64
  354. if err := s.scan(ctx, &v); err != nil {
  355. return nil, err
  356. }
  357. return v, nil
  358. }
  359. // Float64sX is like Float64s, but panics if an error occurs.
  360. func (s *selector) Float64sX(ctx context.Context) []float64 {
  361. v, err := s.Float64s(ctx)
  362. if err != nil {
  363. panic(err)
  364. }
  365. return v
  366. }
  367. // Float64 returns a single float64 from a selector. It is only allowed when selecting one field.
  368. func (s *selector) Float64(ctx context.Context) (_ float64, err error) {
  369. var v []float64
  370. if v, err = s.Float64s(ctx); err != nil {
  371. return
  372. }
  373. switch len(v) {
  374. case 1:
  375. return v[0], nil
  376. case 0:
  377. err = &NotFoundError{s.label}
  378. default:
  379. err = fmt.Errorf("ent: Float64s returned %d results when one was expected", len(v))
  380. }
  381. return
  382. }
  383. // Float64X is like Float64, but panics if an error occurs.
  384. func (s *selector) Float64X(ctx context.Context) float64 {
  385. v, err := s.Float64(ctx)
  386. if err != nil {
  387. panic(err)
  388. }
  389. return v
  390. }
  391. // Bools returns list of bools from a selector. It is only allowed when selecting one field.
  392. func (s *selector) Bools(ctx context.Context) ([]bool, error) {
  393. if len(*s.flds) > 1 {
  394. return nil, errors.New("ent: Bools is not achievable when selecting more than 1 field")
  395. }
  396. var v []bool
  397. if err := s.scan(ctx, &v); err != nil {
  398. return nil, err
  399. }
  400. return v, nil
  401. }
  402. // BoolsX is like Bools, but panics if an error occurs.
  403. func (s *selector) BoolsX(ctx context.Context) []bool {
  404. v, err := s.Bools(ctx)
  405. if err != nil {
  406. panic(err)
  407. }
  408. return v
  409. }
  410. // Bool returns a single bool from a selector. It is only allowed when selecting one field.
  411. func (s *selector) Bool(ctx context.Context) (_ bool, err error) {
  412. var v []bool
  413. if v, err = s.Bools(ctx); err != nil {
  414. return
  415. }
  416. switch len(v) {
  417. case 1:
  418. return v[0], nil
  419. case 0:
  420. err = &NotFoundError{s.label}
  421. default:
  422. err = fmt.Errorf("ent: Bools returned %d results when one was expected", len(v))
  423. }
  424. return
  425. }
  426. // BoolX is like Bool, but panics if an error occurs.
  427. func (s *selector) BoolX(ctx context.Context) bool {
  428. v, err := s.Bool(ctx)
  429. if err != nil {
  430. panic(err)
  431. }
  432. return v
  433. }
  434. // withHooks invokes the builder operation with the given hooks, if any.
  435. func withHooks[V Value, M any, PM interface {
  436. *M
  437. Mutation
  438. }](ctx context.Context, exec func(context.Context) (V, error), mutation PM, hooks []Hook) (value V, err error) {
  439. if len(hooks) == 0 {
  440. return exec(ctx)
  441. }
  442. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  443. mutationT, ok := any(m).(PM)
  444. if !ok {
  445. return nil, fmt.Errorf("unexpected mutation type %T", m)
  446. }
  447. // Set the mutation to the builder.
  448. *mutation = *mutationT
  449. return exec(ctx)
  450. })
  451. for i := len(hooks) - 1; i >= 0; i-- {
  452. if hooks[i] == nil {
  453. return value, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  454. }
  455. mut = hooks[i](mut)
  456. }
  457. v, err := mut.Mutate(ctx, mutation)
  458. if err != nil {
  459. return value, err
  460. }
  461. nv, ok := v.(V)
  462. if !ok {
  463. return value, fmt.Errorf("unexpected node type %T returned from %T", v, mutation)
  464. }
  465. return nv, nil
  466. }
  467. // setContextOp returns a new context with the given QueryContext attached (including its op) in case it does not exist.
  468. func setContextOp(ctx context.Context, qc *QueryContext, op string) context.Context {
  469. if ent.QueryFromContext(ctx) == nil {
  470. qc.Op = op
  471. ctx = ent.NewQueryContext(ctx, qc)
  472. }
  473. return ctx
  474. }
  475. func querierAll[V Value, Q interface {
  476. sqlAll(context.Context, ...queryHook) (V, error)
  477. }]() Querier {
  478. return QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
  479. query, ok := q.(Q)
  480. if !ok {
  481. return nil, fmt.Errorf("unexpected query type %T", q)
  482. }
  483. return query.sqlAll(ctx)
  484. })
  485. }
  486. func querierCount[Q interface {
  487. sqlCount(context.Context) (int, error)
  488. }]() Querier {
  489. return QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
  490. query, ok := q.(Q)
  491. if !ok {
  492. return nil, fmt.Errorf("unexpected query type %T", q)
  493. }
  494. return query.sqlCount(ctx)
  495. })
  496. }
  497. func withInterceptors[V Value](ctx context.Context, q Query, qr Querier, inters []Interceptor) (v V, err error) {
  498. for i := len(inters) - 1; i >= 0; i-- {
  499. qr = inters[i].Intercept(qr)
  500. }
  501. rv, err := qr.Query(ctx, q)
  502. if err != nil {
  503. return v, err
  504. }
  505. vt, ok := rv.(V)
  506. if !ok {
  507. return v, fmt.Errorf("unexpected type %T returned from %T. expected type: %T", vt, q, v)
  508. }
  509. return vt, nil
  510. }
  511. func scanWithInterceptors[Q1 ent.Query, Q2 interface {
  512. sqlScan(context.Context, Q1, any) error
  513. }](ctx context.Context, rootQuery Q1, selectOrGroup Q2, inters []Interceptor, v any) error {
  514. rv := reflect.ValueOf(v)
  515. var qr Querier = QuerierFunc(func(ctx context.Context, q Query) (Value, error) {
  516. query, ok := q.(Q1)
  517. if !ok {
  518. return nil, fmt.Errorf("unexpected query type %T", q)
  519. }
  520. if err := selectOrGroup.sqlScan(ctx, query, v); err != nil {
  521. return nil, err
  522. }
  523. if k := rv.Kind(); k == reflect.Pointer && rv.Elem().CanInterface() {
  524. return rv.Elem().Interface(), nil
  525. }
  526. return v, nil
  527. })
  528. for i := len(inters) - 1; i >= 0; i-- {
  529. qr = inters[i].Intercept(qr)
  530. }
  531. vv, err := qr.Query(ctx, rootQuery)
  532. if err != nil {
  533. return err
  534. }
  535. switch rv2 := reflect.ValueOf(vv); {
  536. case rv.IsNil(), rv2.IsNil(), rv.Kind() != reflect.Pointer:
  537. case rv.Type() == rv2.Type():
  538. rv.Elem().Set(rv2.Elem())
  539. case rv.Elem().Type() == rv2.Type():
  540. rv.Elem().Set(rv2)
  541. }
  542. return nil
  543. }
  544. // queryHook describes an internal hook for the different sqlAll methods.
  545. type queryHook func(context.Context, *sqlgraph.QuerySpec)