ent.go 19 KB

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