ent.go 19 KB

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