ent.go 19 KB

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