ent.go 16 KB

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