ent.go 17 KB

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