intercept.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // Code generated by ent, DO NOT EDIT.
  2. package intercept
  3. import (
  4. "context"
  5. "fmt"
  6. "wechat-api/ent"
  7. "wechat-api/ent/contact"
  8. "wechat-api/ent/label"
  9. "wechat-api/ent/labelrelationship"
  10. "wechat-api/ent/message"
  11. "wechat-api/ent/messagerecords"
  12. "wechat-api/ent/predicate"
  13. "wechat-api/ent/server"
  14. "wechat-api/ent/sopnode"
  15. "wechat-api/ent/sopstage"
  16. "wechat-api/ent/soptask"
  17. "wechat-api/ent/wx"
  18. "entgo.io/ent/dialect/sql"
  19. )
  20. // The Query interface represents an operation that queries a graph.
  21. // By using this interface, users can write generic code that manipulates
  22. // query builders of different types.
  23. type Query interface {
  24. // Type returns the string representation of the query type.
  25. Type() string
  26. // Limit the number of records to be returned by this query.
  27. Limit(int)
  28. // Offset to start from.
  29. Offset(int)
  30. // Unique configures the query builder to filter duplicate records.
  31. Unique(bool)
  32. // Order specifies how the records should be ordered.
  33. Order(...func(*sql.Selector))
  34. // WhereP appends storage-level predicates to the query builder. Using this method, users
  35. // can use type-assertion to append predicates that do not depend on any generated package.
  36. WhereP(...func(*sql.Selector))
  37. }
  38. // The Func type is an adapter that allows ordinary functions to be used as interceptors.
  39. // Unlike traversal functions, interceptors are skipped during graph traversals. Note that the
  40. // implementation of Func is different from the one defined in entgo.io/ent.InterceptFunc.
  41. type Func func(context.Context, Query) error
  42. // Intercept calls f(ctx, q) and then applied the next Querier.
  43. func (f Func) Intercept(next ent.Querier) ent.Querier {
  44. return ent.QuerierFunc(func(ctx context.Context, q ent.Query) (ent.Value, error) {
  45. query, err := NewQuery(q)
  46. if err != nil {
  47. return nil, err
  48. }
  49. if err := f(ctx, query); err != nil {
  50. return nil, err
  51. }
  52. return next.Query(ctx, q)
  53. })
  54. }
  55. // The TraverseFunc type is an adapter to allow the use of ordinary function as Traverser.
  56. // If f is a function with the appropriate signature, TraverseFunc(f) is a Traverser that calls f.
  57. type TraverseFunc func(context.Context, Query) error
  58. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  59. func (f TraverseFunc) Intercept(next ent.Querier) ent.Querier {
  60. return next
  61. }
  62. // Traverse calls f(ctx, q).
  63. func (f TraverseFunc) Traverse(ctx context.Context, q ent.Query) error {
  64. query, err := NewQuery(q)
  65. if err != nil {
  66. return err
  67. }
  68. return f(ctx, query)
  69. }
  70. // The ContactFunc type is an adapter to allow the use of ordinary function as a Querier.
  71. type ContactFunc func(context.Context, *ent.ContactQuery) (ent.Value, error)
  72. // Query calls f(ctx, q).
  73. func (f ContactFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  74. if q, ok := q.(*ent.ContactQuery); ok {
  75. return f(ctx, q)
  76. }
  77. return nil, fmt.Errorf("unexpected query type %T. expect *ent.ContactQuery", q)
  78. }
  79. // The TraverseContact type is an adapter to allow the use of ordinary function as Traverser.
  80. type TraverseContact func(context.Context, *ent.ContactQuery) error
  81. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  82. func (f TraverseContact) Intercept(next ent.Querier) ent.Querier {
  83. return next
  84. }
  85. // Traverse calls f(ctx, q).
  86. func (f TraverseContact) Traverse(ctx context.Context, q ent.Query) error {
  87. if q, ok := q.(*ent.ContactQuery); ok {
  88. return f(ctx, q)
  89. }
  90. return fmt.Errorf("unexpected query type %T. expect *ent.ContactQuery", q)
  91. }
  92. // The LabelFunc type is an adapter to allow the use of ordinary function as a Querier.
  93. type LabelFunc func(context.Context, *ent.LabelQuery) (ent.Value, error)
  94. // Query calls f(ctx, q).
  95. func (f LabelFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  96. if q, ok := q.(*ent.LabelQuery); ok {
  97. return f(ctx, q)
  98. }
  99. return nil, fmt.Errorf("unexpected query type %T. expect *ent.LabelQuery", q)
  100. }
  101. // The TraverseLabel type is an adapter to allow the use of ordinary function as Traverser.
  102. type TraverseLabel func(context.Context, *ent.LabelQuery) error
  103. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  104. func (f TraverseLabel) Intercept(next ent.Querier) ent.Querier {
  105. return next
  106. }
  107. // Traverse calls f(ctx, q).
  108. func (f TraverseLabel) Traverse(ctx context.Context, q ent.Query) error {
  109. if q, ok := q.(*ent.LabelQuery); ok {
  110. return f(ctx, q)
  111. }
  112. return fmt.Errorf("unexpected query type %T. expect *ent.LabelQuery", q)
  113. }
  114. // The LabelRelationshipFunc type is an adapter to allow the use of ordinary function as a Querier.
  115. type LabelRelationshipFunc func(context.Context, *ent.LabelRelationshipQuery) (ent.Value, error)
  116. // Query calls f(ctx, q).
  117. func (f LabelRelationshipFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  118. if q, ok := q.(*ent.LabelRelationshipQuery); ok {
  119. return f(ctx, q)
  120. }
  121. return nil, fmt.Errorf("unexpected query type %T. expect *ent.LabelRelationshipQuery", q)
  122. }
  123. // The TraverseLabelRelationship type is an adapter to allow the use of ordinary function as Traverser.
  124. type TraverseLabelRelationship func(context.Context, *ent.LabelRelationshipQuery) error
  125. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  126. func (f TraverseLabelRelationship) Intercept(next ent.Querier) ent.Querier {
  127. return next
  128. }
  129. // Traverse calls f(ctx, q).
  130. func (f TraverseLabelRelationship) Traverse(ctx context.Context, q ent.Query) error {
  131. if q, ok := q.(*ent.LabelRelationshipQuery); ok {
  132. return f(ctx, q)
  133. }
  134. return fmt.Errorf("unexpected query type %T. expect *ent.LabelRelationshipQuery", q)
  135. }
  136. // The MessageFunc type is an adapter to allow the use of ordinary function as a Querier.
  137. type MessageFunc func(context.Context, *ent.MessageQuery) (ent.Value, error)
  138. // Query calls f(ctx, q).
  139. func (f MessageFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  140. if q, ok := q.(*ent.MessageQuery); ok {
  141. return f(ctx, q)
  142. }
  143. return nil, fmt.Errorf("unexpected query type %T. expect *ent.MessageQuery", q)
  144. }
  145. // The TraverseMessage type is an adapter to allow the use of ordinary function as Traverser.
  146. type TraverseMessage func(context.Context, *ent.MessageQuery) error
  147. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  148. func (f TraverseMessage) Intercept(next ent.Querier) ent.Querier {
  149. return next
  150. }
  151. // Traverse calls f(ctx, q).
  152. func (f TraverseMessage) Traverse(ctx context.Context, q ent.Query) error {
  153. if q, ok := q.(*ent.MessageQuery); ok {
  154. return f(ctx, q)
  155. }
  156. return fmt.Errorf("unexpected query type %T. expect *ent.MessageQuery", q)
  157. }
  158. // The MessageRecordsFunc type is an adapter to allow the use of ordinary function as a Querier.
  159. type MessageRecordsFunc func(context.Context, *ent.MessageRecordsQuery) (ent.Value, error)
  160. // Query calls f(ctx, q).
  161. func (f MessageRecordsFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  162. if q, ok := q.(*ent.MessageRecordsQuery); ok {
  163. return f(ctx, q)
  164. }
  165. return nil, fmt.Errorf("unexpected query type %T. expect *ent.MessageRecordsQuery", q)
  166. }
  167. // The TraverseMessageRecords type is an adapter to allow the use of ordinary function as Traverser.
  168. type TraverseMessageRecords func(context.Context, *ent.MessageRecordsQuery) error
  169. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  170. func (f TraverseMessageRecords) Intercept(next ent.Querier) ent.Querier {
  171. return next
  172. }
  173. // Traverse calls f(ctx, q).
  174. func (f TraverseMessageRecords) Traverse(ctx context.Context, q ent.Query) error {
  175. if q, ok := q.(*ent.MessageRecordsQuery); ok {
  176. return f(ctx, q)
  177. }
  178. return fmt.Errorf("unexpected query type %T. expect *ent.MessageRecordsQuery", q)
  179. }
  180. // The ServerFunc type is an adapter to allow the use of ordinary function as a Querier.
  181. type ServerFunc func(context.Context, *ent.ServerQuery) (ent.Value, error)
  182. // Query calls f(ctx, q).
  183. func (f ServerFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  184. if q, ok := q.(*ent.ServerQuery); ok {
  185. return f(ctx, q)
  186. }
  187. return nil, fmt.Errorf("unexpected query type %T. expect *ent.ServerQuery", q)
  188. }
  189. // The TraverseServer type is an adapter to allow the use of ordinary function as Traverser.
  190. type TraverseServer func(context.Context, *ent.ServerQuery) error
  191. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  192. func (f TraverseServer) Intercept(next ent.Querier) ent.Querier {
  193. return next
  194. }
  195. // Traverse calls f(ctx, q).
  196. func (f TraverseServer) Traverse(ctx context.Context, q ent.Query) error {
  197. if q, ok := q.(*ent.ServerQuery); ok {
  198. return f(ctx, q)
  199. }
  200. return fmt.Errorf("unexpected query type %T. expect *ent.ServerQuery", q)
  201. }
  202. // The SopNodeFunc type is an adapter to allow the use of ordinary function as a Querier.
  203. type SopNodeFunc func(context.Context, *ent.SopNodeQuery) (ent.Value, error)
  204. // Query calls f(ctx, q).
  205. func (f SopNodeFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  206. if q, ok := q.(*ent.SopNodeQuery); ok {
  207. return f(ctx, q)
  208. }
  209. return nil, fmt.Errorf("unexpected query type %T. expect *ent.SopNodeQuery", q)
  210. }
  211. // The TraverseSopNode type is an adapter to allow the use of ordinary function as Traverser.
  212. type TraverseSopNode func(context.Context, *ent.SopNodeQuery) error
  213. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  214. func (f TraverseSopNode) Intercept(next ent.Querier) ent.Querier {
  215. return next
  216. }
  217. // Traverse calls f(ctx, q).
  218. func (f TraverseSopNode) Traverse(ctx context.Context, q ent.Query) error {
  219. if q, ok := q.(*ent.SopNodeQuery); ok {
  220. return f(ctx, q)
  221. }
  222. return fmt.Errorf("unexpected query type %T. expect *ent.SopNodeQuery", q)
  223. }
  224. // The SopStageFunc type is an adapter to allow the use of ordinary function as a Querier.
  225. type SopStageFunc func(context.Context, *ent.SopStageQuery) (ent.Value, error)
  226. // Query calls f(ctx, q).
  227. func (f SopStageFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  228. if q, ok := q.(*ent.SopStageQuery); ok {
  229. return f(ctx, q)
  230. }
  231. return nil, fmt.Errorf("unexpected query type %T. expect *ent.SopStageQuery", q)
  232. }
  233. // The TraverseSopStage type is an adapter to allow the use of ordinary function as Traverser.
  234. type TraverseSopStage func(context.Context, *ent.SopStageQuery) error
  235. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  236. func (f TraverseSopStage) Intercept(next ent.Querier) ent.Querier {
  237. return next
  238. }
  239. // Traverse calls f(ctx, q).
  240. func (f TraverseSopStage) Traverse(ctx context.Context, q ent.Query) error {
  241. if q, ok := q.(*ent.SopStageQuery); ok {
  242. return f(ctx, q)
  243. }
  244. return fmt.Errorf("unexpected query type %T. expect *ent.SopStageQuery", q)
  245. }
  246. // The SopTaskFunc type is an adapter to allow the use of ordinary function as a Querier.
  247. type SopTaskFunc func(context.Context, *ent.SopTaskQuery) (ent.Value, error)
  248. // Query calls f(ctx, q).
  249. func (f SopTaskFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  250. if q, ok := q.(*ent.SopTaskQuery); ok {
  251. return f(ctx, q)
  252. }
  253. return nil, fmt.Errorf("unexpected query type %T. expect *ent.SopTaskQuery", q)
  254. }
  255. // The TraverseSopTask type is an adapter to allow the use of ordinary function as Traverser.
  256. type TraverseSopTask func(context.Context, *ent.SopTaskQuery) error
  257. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  258. func (f TraverseSopTask) Intercept(next ent.Querier) ent.Querier {
  259. return next
  260. }
  261. // Traverse calls f(ctx, q).
  262. func (f TraverseSopTask) Traverse(ctx context.Context, q ent.Query) error {
  263. if q, ok := q.(*ent.SopTaskQuery); ok {
  264. return f(ctx, q)
  265. }
  266. return fmt.Errorf("unexpected query type %T. expect *ent.SopTaskQuery", q)
  267. }
  268. // The WxFunc type is an adapter to allow the use of ordinary function as a Querier.
  269. type WxFunc func(context.Context, *ent.WxQuery) (ent.Value, error)
  270. // Query calls f(ctx, q).
  271. func (f WxFunc) Query(ctx context.Context, q ent.Query) (ent.Value, error) {
  272. if q, ok := q.(*ent.WxQuery); ok {
  273. return f(ctx, q)
  274. }
  275. return nil, fmt.Errorf("unexpected query type %T. expect *ent.WxQuery", q)
  276. }
  277. // The TraverseWx type is an adapter to allow the use of ordinary function as Traverser.
  278. type TraverseWx func(context.Context, *ent.WxQuery) error
  279. // Intercept is a dummy implementation of Intercept that returns the next Querier in the pipeline.
  280. func (f TraverseWx) Intercept(next ent.Querier) ent.Querier {
  281. return next
  282. }
  283. // Traverse calls f(ctx, q).
  284. func (f TraverseWx) Traverse(ctx context.Context, q ent.Query) error {
  285. if q, ok := q.(*ent.WxQuery); ok {
  286. return f(ctx, q)
  287. }
  288. return fmt.Errorf("unexpected query type %T. expect *ent.WxQuery", q)
  289. }
  290. // NewQuery returns the generic Query interface for the given typed query.
  291. func NewQuery(q ent.Query) (Query, error) {
  292. switch q := q.(type) {
  293. case *ent.ContactQuery:
  294. return &query[*ent.ContactQuery, predicate.Contact, contact.OrderOption]{typ: ent.TypeContact, tq: q}, nil
  295. case *ent.LabelQuery:
  296. return &query[*ent.LabelQuery, predicate.Label, label.OrderOption]{typ: ent.TypeLabel, tq: q}, nil
  297. case *ent.LabelRelationshipQuery:
  298. return &query[*ent.LabelRelationshipQuery, predicate.LabelRelationship, labelrelationship.OrderOption]{typ: ent.TypeLabelRelationship, tq: q}, nil
  299. case *ent.MessageQuery:
  300. return &query[*ent.MessageQuery, predicate.Message, message.OrderOption]{typ: ent.TypeMessage, tq: q}, nil
  301. case *ent.MessageRecordsQuery:
  302. return &query[*ent.MessageRecordsQuery, predicate.MessageRecords, messagerecords.OrderOption]{typ: ent.TypeMessageRecords, tq: q}, nil
  303. case *ent.ServerQuery:
  304. return &query[*ent.ServerQuery, predicate.Server, server.OrderOption]{typ: ent.TypeServer, tq: q}, nil
  305. case *ent.SopNodeQuery:
  306. return &query[*ent.SopNodeQuery, predicate.SopNode, sopnode.OrderOption]{typ: ent.TypeSopNode, tq: q}, nil
  307. case *ent.SopStageQuery:
  308. return &query[*ent.SopStageQuery, predicate.SopStage, sopstage.OrderOption]{typ: ent.TypeSopStage, tq: q}, nil
  309. case *ent.SopTaskQuery:
  310. return &query[*ent.SopTaskQuery, predicate.SopTask, soptask.OrderOption]{typ: ent.TypeSopTask, tq: q}, nil
  311. case *ent.WxQuery:
  312. return &query[*ent.WxQuery, predicate.Wx, wx.OrderOption]{typ: ent.TypeWx, tq: q}, nil
  313. default:
  314. return nil, fmt.Errorf("unknown query type %T", q)
  315. }
  316. }
  317. type query[T any, P ~func(*sql.Selector), R ~func(*sql.Selector)] struct {
  318. typ string
  319. tq interface {
  320. Limit(int) T
  321. Offset(int) T
  322. Unique(bool) T
  323. Order(...R) T
  324. Where(...P) T
  325. }
  326. }
  327. func (q query[T, P, R]) Type() string {
  328. return q.typ
  329. }
  330. func (q query[T, P, R]) Limit(limit int) {
  331. q.tq.Limit(limit)
  332. }
  333. func (q query[T, P, R]) Offset(offset int) {
  334. q.tq.Offset(offset)
  335. }
  336. func (q query[T, P, R]) Unique(unique bool) {
  337. q.tq.Unique(unique)
  338. }
  339. func (q query[T, P, R]) Order(orders ...func(*sql.Selector)) {
  340. rs := make([]R, len(orders))
  341. for i := range orders {
  342. rs[i] = orders[i]
  343. }
  344. q.tq.Order(rs...)
  345. }
  346. func (q query[T, P, R]) WhereP(ps ...func(*sql.Selector)) {
  347. p := make([]P, len(ps))
  348. for i := range ps {
  349. p[i] = ps[i]
  350. }
  351. q.tq.Where(p...)
  352. }