ent.go 19 KB

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