ent.go 16 KB

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