ent.go 17 KB

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