ent.go 19 KB

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