ent.go 17 KB

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