ent.go 17 KB

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