ent.go 16 KB

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