ent.go 16 KB

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