ent.go 18 KB

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