channelmonitor_query.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "database/sql/driver"
  6. "fmt"
  7. "math"
  8. "entgo.io/ent"
  9. "entgo.io/ent/dialect"
  10. "entgo.io/ent/dialect/sql"
  11. "entgo.io/ent/dialect/sql/sqlgraph"
  12. "entgo.io/ent/schema/field"
  13. "github.com/Wei-Shaw/sub2api/ent/channelmonitor"
  14. "github.com/Wei-Shaw/sub2api/ent/channelmonitordailyrollup"
  15. "github.com/Wei-Shaw/sub2api/ent/channelmonitorhistory"
  16. "github.com/Wei-Shaw/sub2api/ent/channelmonitorrequesttemplate"
  17. "github.com/Wei-Shaw/sub2api/ent/predicate"
  18. )
  19. // ChannelMonitorQuery is the builder for querying ChannelMonitor entities.
  20. type ChannelMonitorQuery struct {
  21. config
  22. ctx *QueryContext
  23. order []channelmonitor.OrderOption
  24. inters []Interceptor
  25. predicates []predicate.ChannelMonitor
  26. withHistory *ChannelMonitorHistoryQuery
  27. withDailyRollups *ChannelMonitorDailyRollupQuery
  28. withRequestTemplate *ChannelMonitorRequestTemplateQuery
  29. modifiers []func(*sql.Selector)
  30. // intermediate query (i.e. traversal path).
  31. sql *sql.Selector
  32. path func(context.Context) (*sql.Selector, error)
  33. }
  34. // Where adds a new predicate for the ChannelMonitorQuery builder.
  35. func (_q *ChannelMonitorQuery) Where(ps ...predicate.ChannelMonitor) *ChannelMonitorQuery {
  36. _q.predicates = append(_q.predicates, ps...)
  37. return _q
  38. }
  39. // Limit the number of records to be returned by this query.
  40. func (_q *ChannelMonitorQuery) Limit(limit int) *ChannelMonitorQuery {
  41. _q.ctx.Limit = &limit
  42. return _q
  43. }
  44. // Offset to start from.
  45. func (_q *ChannelMonitorQuery) Offset(offset int) *ChannelMonitorQuery {
  46. _q.ctx.Offset = &offset
  47. return _q
  48. }
  49. // Unique configures the query builder to filter duplicate records on query.
  50. // By default, unique is set to true, and can be disabled using this method.
  51. func (_q *ChannelMonitorQuery) Unique(unique bool) *ChannelMonitorQuery {
  52. _q.ctx.Unique = &unique
  53. return _q
  54. }
  55. // Order specifies how the records should be ordered.
  56. func (_q *ChannelMonitorQuery) Order(o ...channelmonitor.OrderOption) *ChannelMonitorQuery {
  57. _q.order = append(_q.order, o...)
  58. return _q
  59. }
  60. // QueryHistory chains the current query on the "history" edge.
  61. func (_q *ChannelMonitorQuery) QueryHistory() *ChannelMonitorHistoryQuery {
  62. query := (&ChannelMonitorHistoryClient{config: _q.config}).Query()
  63. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  64. if err := _q.prepareQuery(ctx); err != nil {
  65. return nil, err
  66. }
  67. selector := _q.sqlQuery(ctx)
  68. if err := selector.Err(); err != nil {
  69. return nil, err
  70. }
  71. step := sqlgraph.NewStep(
  72. sqlgraph.From(channelmonitor.Table, channelmonitor.FieldID, selector),
  73. sqlgraph.To(channelmonitorhistory.Table, channelmonitorhistory.FieldID),
  74. sqlgraph.Edge(sqlgraph.O2M, false, channelmonitor.HistoryTable, channelmonitor.HistoryColumn),
  75. )
  76. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  77. return fromU, nil
  78. }
  79. return query
  80. }
  81. // QueryDailyRollups chains the current query on the "daily_rollups" edge.
  82. func (_q *ChannelMonitorQuery) QueryDailyRollups() *ChannelMonitorDailyRollupQuery {
  83. query := (&ChannelMonitorDailyRollupClient{config: _q.config}).Query()
  84. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  85. if err := _q.prepareQuery(ctx); err != nil {
  86. return nil, err
  87. }
  88. selector := _q.sqlQuery(ctx)
  89. if err := selector.Err(); err != nil {
  90. return nil, err
  91. }
  92. step := sqlgraph.NewStep(
  93. sqlgraph.From(channelmonitor.Table, channelmonitor.FieldID, selector),
  94. sqlgraph.To(channelmonitordailyrollup.Table, channelmonitordailyrollup.FieldID),
  95. sqlgraph.Edge(sqlgraph.O2M, false, channelmonitor.DailyRollupsTable, channelmonitor.DailyRollupsColumn),
  96. )
  97. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  98. return fromU, nil
  99. }
  100. return query
  101. }
  102. // QueryRequestTemplate chains the current query on the "request_template" edge.
  103. func (_q *ChannelMonitorQuery) QueryRequestTemplate() *ChannelMonitorRequestTemplateQuery {
  104. query := (&ChannelMonitorRequestTemplateClient{config: _q.config}).Query()
  105. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  106. if err := _q.prepareQuery(ctx); err != nil {
  107. return nil, err
  108. }
  109. selector := _q.sqlQuery(ctx)
  110. if err := selector.Err(); err != nil {
  111. return nil, err
  112. }
  113. step := sqlgraph.NewStep(
  114. sqlgraph.From(channelmonitor.Table, channelmonitor.FieldID, selector),
  115. sqlgraph.To(channelmonitorrequesttemplate.Table, channelmonitorrequesttemplate.FieldID),
  116. sqlgraph.Edge(sqlgraph.M2O, false, channelmonitor.RequestTemplateTable, channelmonitor.RequestTemplateColumn),
  117. )
  118. fromU = sqlgraph.SetNeighbors(_q.driver.Dialect(), step)
  119. return fromU, nil
  120. }
  121. return query
  122. }
  123. // First returns the first ChannelMonitor entity from the query.
  124. // Returns a *NotFoundError when no ChannelMonitor was found.
  125. func (_q *ChannelMonitorQuery) First(ctx context.Context) (*ChannelMonitor, error) {
  126. nodes, err := _q.Limit(1).All(setContextOp(ctx, _q.ctx, ent.OpQueryFirst))
  127. if err != nil {
  128. return nil, err
  129. }
  130. if len(nodes) == 0 {
  131. return nil, &NotFoundError{channelmonitor.Label}
  132. }
  133. return nodes[0], nil
  134. }
  135. // FirstX is like First, but panics if an error occurs.
  136. func (_q *ChannelMonitorQuery) FirstX(ctx context.Context) *ChannelMonitor {
  137. node, err := _q.First(ctx)
  138. if err != nil && !IsNotFound(err) {
  139. panic(err)
  140. }
  141. return node
  142. }
  143. // FirstID returns the first ChannelMonitor ID from the query.
  144. // Returns a *NotFoundError when no ChannelMonitor ID was found.
  145. func (_q *ChannelMonitorQuery) FirstID(ctx context.Context) (id int64, err error) {
  146. var ids []int64
  147. if ids, err = _q.Limit(1).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryFirstID)); err != nil {
  148. return
  149. }
  150. if len(ids) == 0 {
  151. err = &NotFoundError{channelmonitor.Label}
  152. return
  153. }
  154. return ids[0], nil
  155. }
  156. // FirstIDX is like FirstID, but panics if an error occurs.
  157. func (_q *ChannelMonitorQuery) FirstIDX(ctx context.Context) int64 {
  158. id, err := _q.FirstID(ctx)
  159. if err != nil && !IsNotFound(err) {
  160. panic(err)
  161. }
  162. return id
  163. }
  164. // Only returns a single ChannelMonitor entity found by the query, ensuring it only returns one.
  165. // Returns a *NotSingularError when more than one ChannelMonitor entity is found.
  166. // Returns a *NotFoundError when no ChannelMonitor entities are found.
  167. func (_q *ChannelMonitorQuery) Only(ctx context.Context) (*ChannelMonitor, error) {
  168. nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
  169. if err != nil {
  170. return nil, err
  171. }
  172. switch len(nodes) {
  173. case 1:
  174. return nodes[0], nil
  175. case 0:
  176. return nil, &NotFoundError{channelmonitor.Label}
  177. default:
  178. return nil, &NotSingularError{channelmonitor.Label}
  179. }
  180. }
  181. // OnlyX is like Only, but panics if an error occurs.
  182. func (_q *ChannelMonitorQuery) OnlyX(ctx context.Context) *ChannelMonitor {
  183. node, err := _q.Only(ctx)
  184. if err != nil {
  185. panic(err)
  186. }
  187. return node
  188. }
  189. // OnlyID is like Only, but returns the only ChannelMonitor ID in the query.
  190. // Returns a *NotSingularError when more than one ChannelMonitor ID is found.
  191. // Returns a *NotFoundError when no entities are found.
  192. func (_q *ChannelMonitorQuery) OnlyID(ctx context.Context) (id int64, err error) {
  193. var ids []int64
  194. if ids, err = _q.Limit(2).IDs(setContextOp(ctx, _q.ctx, ent.OpQueryOnlyID)); err != nil {
  195. return
  196. }
  197. switch len(ids) {
  198. case 1:
  199. id = ids[0]
  200. case 0:
  201. err = &NotFoundError{channelmonitor.Label}
  202. default:
  203. err = &NotSingularError{channelmonitor.Label}
  204. }
  205. return
  206. }
  207. // OnlyIDX is like OnlyID, but panics if an error occurs.
  208. func (_q *ChannelMonitorQuery) OnlyIDX(ctx context.Context) int64 {
  209. id, err := _q.OnlyID(ctx)
  210. if err != nil {
  211. panic(err)
  212. }
  213. return id
  214. }
  215. // All executes the query and returns a list of ChannelMonitors.
  216. func (_q *ChannelMonitorQuery) All(ctx context.Context) ([]*ChannelMonitor, error) {
  217. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryAll)
  218. if err := _q.prepareQuery(ctx); err != nil {
  219. return nil, err
  220. }
  221. qr := querierAll[[]*ChannelMonitor, *ChannelMonitorQuery]()
  222. return withInterceptors[[]*ChannelMonitor](ctx, _q, qr, _q.inters)
  223. }
  224. // AllX is like All, but panics if an error occurs.
  225. func (_q *ChannelMonitorQuery) AllX(ctx context.Context) []*ChannelMonitor {
  226. nodes, err := _q.All(ctx)
  227. if err != nil {
  228. panic(err)
  229. }
  230. return nodes
  231. }
  232. // IDs executes the query and returns a list of ChannelMonitor IDs.
  233. func (_q *ChannelMonitorQuery) IDs(ctx context.Context) (ids []int64, err error) {
  234. if _q.ctx.Unique == nil && _q.path != nil {
  235. _q.Unique(true)
  236. }
  237. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryIDs)
  238. if err = _q.Select(channelmonitor.FieldID).Scan(ctx, &ids); err != nil {
  239. return nil, err
  240. }
  241. return ids, nil
  242. }
  243. // IDsX is like IDs, but panics if an error occurs.
  244. func (_q *ChannelMonitorQuery) IDsX(ctx context.Context) []int64 {
  245. ids, err := _q.IDs(ctx)
  246. if err != nil {
  247. panic(err)
  248. }
  249. return ids
  250. }
  251. // Count returns the count of the given query.
  252. func (_q *ChannelMonitorQuery) Count(ctx context.Context) (int, error) {
  253. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryCount)
  254. if err := _q.prepareQuery(ctx); err != nil {
  255. return 0, err
  256. }
  257. return withInterceptors[int](ctx, _q, querierCount[*ChannelMonitorQuery](), _q.inters)
  258. }
  259. // CountX is like Count, but panics if an error occurs.
  260. func (_q *ChannelMonitorQuery) CountX(ctx context.Context) int {
  261. count, err := _q.Count(ctx)
  262. if err != nil {
  263. panic(err)
  264. }
  265. return count
  266. }
  267. // Exist returns true if the query has elements in the graph.
  268. func (_q *ChannelMonitorQuery) Exist(ctx context.Context) (bool, error) {
  269. ctx = setContextOp(ctx, _q.ctx, ent.OpQueryExist)
  270. switch _, err := _q.FirstID(ctx); {
  271. case IsNotFound(err):
  272. return false, nil
  273. case err != nil:
  274. return false, fmt.Errorf("ent: check existence: %w", err)
  275. default:
  276. return true, nil
  277. }
  278. }
  279. // ExistX is like Exist, but panics if an error occurs.
  280. func (_q *ChannelMonitorQuery) ExistX(ctx context.Context) bool {
  281. exist, err := _q.Exist(ctx)
  282. if err != nil {
  283. panic(err)
  284. }
  285. return exist
  286. }
  287. // Clone returns a duplicate of the ChannelMonitorQuery builder, including all associated steps. It can be
  288. // used to prepare common query builders and use them differently after the clone is made.
  289. func (_q *ChannelMonitorQuery) Clone() *ChannelMonitorQuery {
  290. if _q == nil {
  291. return nil
  292. }
  293. return &ChannelMonitorQuery{
  294. config: _q.config,
  295. ctx: _q.ctx.Clone(),
  296. order: append([]channelmonitor.OrderOption{}, _q.order...),
  297. inters: append([]Interceptor{}, _q.inters...),
  298. predicates: append([]predicate.ChannelMonitor{}, _q.predicates...),
  299. withHistory: _q.withHistory.Clone(),
  300. withDailyRollups: _q.withDailyRollups.Clone(),
  301. withRequestTemplate: _q.withRequestTemplate.Clone(),
  302. // clone intermediate query.
  303. sql: _q.sql.Clone(),
  304. path: _q.path,
  305. }
  306. }
  307. // WithHistory tells the query-builder to eager-load the nodes that are connected to
  308. // the "history" edge. The optional arguments are used to configure the query builder of the edge.
  309. func (_q *ChannelMonitorQuery) WithHistory(opts ...func(*ChannelMonitorHistoryQuery)) *ChannelMonitorQuery {
  310. query := (&ChannelMonitorHistoryClient{config: _q.config}).Query()
  311. for _, opt := range opts {
  312. opt(query)
  313. }
  314. _q.withHistory = query
  315. return _q
  316. }
  317. // WithDailyRollups tells the query-builder to eager-load the nodes that are connected to
  318. // the "daily_rollups" edge. The optional arguments are used to configure the query builder of the edge.
  319. func (_q *ChannelMonitorQuery) WithDailyRollups(opts ...func(*ChannelMonitorDailyRollupQuery)) *ChannelMonitorQuery {
  320. query := (&ChannelMonitorDailyRollupClient{config: _q.config}).Query()
  321. for _, opt := range opts {
  322. opt(query)
  323. }
  324. _q.withDailyRollups = query
  325. return _q
  326. }
  327. // WithRequestTemplate tells the query-builder to eager-load the nodes that are connected to
  328. // the "request_template" edge. The optional arguments are used to configure the query builder of the edge.
  329. func (_q *ChannelMonitorQuery) WithRequestTemplate(opts ...func(*ChannelMonitorRequestTemplateQuery)) *ChannelMonitorQuery {
  330. query := (&ChannelMonitorRequestTemplateClient{config: _q.config}).Query()
  331. for _, opt := range opts {
  332. opt(query)
  333. }
  334. _q.withRequestTemplate = query
  335. return _q
  336. }
  337. // GroupBy is used to group vertices by one or more fields/columns.
  338. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  339. //
  340. // Example:
  341. //
  342. // var v []struct {
  343. // CreatedAt time.Time `json:"created_at,omitempty"`
  344. // Count int `json:"count,omitempty"`
  345. // }
  346. //
  347. // client.ChannelMonitor.Query().
  348. // GroupBy(channelmonitor.FieldCreatedAt).
  349. // Aggregate(ent.Count()).
  350. // Scan(ctx, &v)
  351. func (_q *ChannelMonitorQuery) GroupBy(field string, fields ...string) *ChannelMonitorGroupBy {
  352. _q.ctx.Fields = append([]string{field}, fields...)
  353. grbuild := &ChannelMonitorGroupBy{build: _q}
  354. grbuild.flds = &_q.ctx.Fields
  355. grbuild.label = channelmonitor.Label
  356. grbuild.scan = grbuild.Scan
  357. return grbuild
  358. }
  359. // Select allows the selection one or more fields/columns for the given query,
  360. // instead of selecting all fields in the entity.
  361. //
  362. // Example:
  363. //
  364. // var v []struct {
  365. // CreatedAt time.Time `json:"created_at,omitempty"`
  366. // }
  367. //
  368. // client.ChannelMonitor.Query().
  369. // Select(channelmonitor.FieldCreatedAt).
  370. // Scan(ctx, &v)
  371. func (_q *ChannelMonitorQuery) Select(fields ...string) *ChannelMonitorSelect {
  372. _q.ctx.Fields = append(_q.ctx.Fields, fields...)
  373. sbuild := &ChannelMonitorSelect{ChannelMonitorQuery: _q}
  374. sbuild.label = channelmonitor.Label
  375. sbuild.flds, sbuild.scan = &_q.ctx.Fields, sbuild.Scan
  376. return sbuild
  377. }
  378. // Aggregate returns a ChannelMonitorSelect configured with the given aggregations.
  379. func (_q *ChannelMonitorQuery) Aggregate(fns ...AggregateFunc) *ChannelMonitorSelect {
  380. return _q.Select().Aggregate(fns...)
  381. }
  382. func (_q *ChannelMonitorQuery) prepareQuery(ctx context.Context) error {
  383. for _, inter := range _q.inters {
  384. if inter == nil {
  385. return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
  386. }
  387. if trv, ok := inter.(Traverser); ok {
  388. if err := trv.Traverse(ctx, _q); err != nil {
  389. return err
  390. }
  391. }
  392. }
  393. for _, f := range _q.ctx.Fields {
  394. if !channelmonitor.ValidColumn(f) {
  395. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  396. }
  397. }
  398. if _q.path != nil {
  399. prev, err := _q.path(ctx)
  400. if err != nil {
  401. return err
  402. }
  403. _q.sql = prev
  404. }
  405. return nil
  406. }
  407. func (_q *ChannelMonitorQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*ChannelMonitor, error) {
  408. var (
  409. nodes = []*ChannelMonitor{}
  410. _spec = _q.querySpec()
  411. loadedTypes = [3]bool{
  412. _q.withHistory != nil,
  413. _q.withDailyRollups != nil,
  414. _q.withRequestTemplate != nil,
  415. }
  416. )
  417. _spec.ScanValues = func(columns []string) ([]any, error) {
  418. return (*ChannelMonitor).scanValues(nil, columns)
  419. }
  420. _spec.Assign = func(columns []string, values []any) error {
  421. node := &ChannelMonitor{config: _q.config}
  422. nodes = append(nodes, node)
  423. node.Edges.loadedTypes = loadedTypes
  424. return node.assignValues(columns, values)
  425. }
  426. if len(_q.modifiers) > 0 {
  427. _spec.Modifiers = _q.modifiers
  428. }
  429. for i := range hooks {
  430. hooks[i](ctx, _spec)
  431. }
  432. if err := sqlgraph.QueryNodes(ctx, _q.driver, _spec); err != nil {
  433. return nil, err
  434. }
  435. if len(nodes) == 0 {
  436. return nodes, nil
  437. }
  438. if query := _q.withHistory; query != nil {
  439. if err := _q.loadHistory(ctx, query, nodes,
  440. func(n *ChannelMonitor) { n.Edges.History = []*ChannelMonitorHistory{} },
  441. func(n *ChannelMonitor, e *ChannelMonitorHistory) { n.Edges.History = append(n.Edges.History, e) }); err != nil {
  442. return nil, err
  443. }
  444. }
  445. if query := _q.withDailyRollups; query != nil {
  446. if err := _q.loadDailyRollups(ctx, query, nodes,
  447. func(n *ChannelMonitor) { n.Edges.DailyRollups = []*ChannelMonitorDailyRollup{} },
  448. func(n *ChannelMonitor, e *ChannelMonitorDailyRollup) {
  449. n.Edges.DailyRollups = append(n.Edges.DailyRollups, e)
  450. }); err != nil {
  451. return nil, err
  452. }
  453. }
  454. if query := _q.withRequestTemplate; query != nil {
  455. if err := _q.loadRequestTemplate(ctx, query, nodes, nil,
  456. func(n *ChannelMonitor, e *ChannelMonitorRequestTemplate) { n.Edges.RequestTemplate = e }); err != nil {
  457. return nil, err
  458. }
  459. }
  460. return nodes, nil
  461. }
  462. func (_q *ChannelMonitorQuery) loadHistory(ctx context.Context, query *ChannelMonitorHistoryQuery, nodes []*ChannelMonitor, init func(*ChannelMonitor), assign func(*ChannelMonitor, *ChannelMonitorHistory)) error {
  463. fks := make([]driver.Value, 0, len(nodes))
  464. nodeids := make(map[int64]*ChannelMonitor)
  465. for i := range nodes {
  466. fks = append(fks, nodes[i].ID)
  467. nodeids[nodes[i].ID] = nodes[i]
  468. if init != nil {
  469. init(nodes[i])
  470. }
  471. }
  472. if len(query.ctx.Fields) > 0 {
  473. query.ctx.AppendFieldOnce(channelmonitorhistory.FieldMonitorID)
  474. }
  475. query.Where(predicate.ChannelMonitorHistory(func(s *sql.Selector) {
  476. s.Where(sql.InValues(s.C(channelmonitor.HistoryColumn), fks...))
  477. }))
  478. neighbors, err := query.All(ctx)
  479. if err != nil {
  480. return err
  481. }
  482. for _, n := range neighbors {
  483. fk := n.MonitorID
  484. node, ok := nodeids[fk]
  485. if !ok {
  486. return fmt.Errorf(`unexpected referenced foreign-key "monitor_id" returned %v for node %v`, fk, n.ID)
  487. }
  488. assign(node, n)
  489. }
  490. return nil
  491. }
  492. func (_q *ChannelMonitorQuery) loadDailyRollups(ctx context.Context, query *ChannelMonitorDailyRollupQuery, nodes []*ChannelMonitor, init func(*ChannelMonitor), assign func(*ChannelMonitor, *ChannelMonitorDailyRollup)) error {
  493. fks := make([]driver.Value, 0, len(nodes))
  494. nodeids := make(map[int64]*ChannelMonitor)
  495. for i := range nodes {
  496. fks = append(fks, nodes[i].ID)
  497. nodeids[nodes[i].ID] = nodes[i]
  498. if init != nil {
  499. init(nodes[i])
  500. }
  501. }
  502. if len(query.ctx.Fields) > 0 {
  503. query.ctx.AppendFieldOnce(channelmonitordailyrollup.FieldMonitorID)
  504. }
  505. query.Where(predicate.ChannelMonitorDailyRollup(func(s *sql.Selector) {
  506. s.Where(sql.InValues(s.C(channelmonitor.DailyRollupsColumn), fks...))
  507. }))
  508. neighbors, err := query.All(ctx)
  509. if err != nil {
  510. return err
  511. }
  512. for _, n := range neighbors {
  513. fk := n.MonitorID
  514. node, ok := nodeids[fk]
  515. if !ok {
  516. return fmt.Errorf(`unexpected referenced foreign-key "monitor_id" returned %v for node %v`, fk, n.ID)
  517. }
  518. assign(node, n)
  519. }
  520. return nil
  521. }
  522. func (_q *ChannelMonitorQuery) loadRequestTemplate(ctx context.Context, query *ChannelMonitorRequestTemplateQuery, nodes []*ChannelMonitor, init func(*ChannelMonitor), assign func(*ChannelMonitor, *ChannelMonitorRequestTemplate)) error {
  523. ids := make([]int64, 0, len(nodes))
  524. nodeids := make(map[int64][]*ChannelMonitor)
  525. for i := range nodes {
  526. if nodes[i].TemplateID == nil {
  527. continue
  528. }
  529. fk := *nodes[i].TemplateID
  530. if _, ok := nodeids[fk]; !ok {
  531. ids = append(ids, fk)
  532. }
  533. nodeids[fk] = append(nodeids[fk], nodes[i])
  534. }
  535. if len(ids) == 0 {
  536. return nil
  537. }
  538. query.Where(channelmonitorrequesttemplate.IDIn(ids...))
  539. neighbors, err := query.All(ctx)
  540. if err != nil {
  541. return err
  542. }
  543. for _, n := range neighbors {
  544. nodes, ok := nodeids[n.ID]
  545. if !ok {
  546. return fmt.Errorf(`unexpected foreign-key "template_id" returned %v`, n.ID)
  547. }
  548. for i := range nodes {
  549. assign(nodes[i], n)
  550. }
  551. }
  552. return nil
  553. }
  554. func (_q *ChannelMonitorQuery) sqlCount(ctx context.Context) (int, error) {
  555. _spec := _q.querySpec()
  556. if len(_q.modifiers) > 0 {
  557. _spec.Modifiers = _q.modifiers
  558. }
  559. _spec.Node.Columns = _q.ctx.Fields
  560. if len(_q.ctx.Fields) > 0 {
  561. _spec.Unique = _q.ctx.Unique != nil && *_q.ctx.Unique
  562. }
  563. return sqlgraph.CountNodes(ctx, _q.driver, _spec)
  564. }
  565. func (_q *ChannelMonitorQuery) querySpec() *sqlgraph.QuerySpec {
  566. _spec := sqlgraph.NewQuerySpec(channelmonitor.Table, channelmonitor.Columns, sqlgraph.NewFieldSpec(channelmonitor.FieldID, field.TypeInt64))
  567. _spec.From = _q.sql
  568. if unique := _q.ctx.Unique; unique != nil {
  569. _spec.Unique = *unique
  570. } else if _q.path != nil {
  571. _spec.Unique = true
  572. }
  573. if fields := _q.ctx.Fields; len(fields) > 0 {
  574. _spec.Node.Columns = make([]string, 0, len(fields))
  575. _spec.Node.Columns = append(_spec.Node.Columns, channelmonitor.FieldID)
  576. for i := range fields {
  577. if fields[i] != channelmonitor.FieldID {
  578. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  579. }
  580. }
  581. if _q.withRequestTemplate != nil {
  582. _spec.Node.AddColumnOnce(channelmonitor.FieldTemplateID)
  583. }
  584. }
  585. if ps := _q.predicates; len(ps) > 0 {
  586. _spec.Predicate = func(selector *sql.Selector) {
  587. for i := range ps {
  588. ps[i](selector)
  589. }
  590. }
  591. }
  592. if limit := _q.ctx.Limit; limit != nil {
  593. _spec.Limit = *limit
  594. }
  595. if offset := _q.ctx.Offset; offset != nil {
  596. _spec.Offset = *offset
  597. }
  598. if ps := _q.order; len(ps) > 0 {
  599. _spec.Order = func(selector *sql.Selector) {
  600. for i := range ps {
  601. ps[i](selector)
  602. }
  603. }
  604. }
  605. return _spec
  606. }
  607. func (_q *ChannelMonitorQuery) sqlQuery(ctx context.Context) *sql.Selector {
  608. builder := sql.Dialect(_q.driver.Dialect())
  609. t1 := builder.Table(channelmonitor.Table)
  610. columns := _q.ctx.Fields
  611. if len(columns) == 0 {
  612. columns = channelmonitor.Columns
  613. }
  614. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  615. if _q.sql != nil {
  616. selector = _q.sql
  617. selector.Select(selector.Columns(columns...)...)
  618. }
  619. if _q.ctx.Unique != nil && *_q.ctx.Unique {
  620. selector.Distinct()
  621. }
  622. for _, m := range _q.modifiers {
  623. m(selector)
  624. }
  625. for _, p := range _q.predicates {
  626. p(selector)
  627. }
  628. for _, p := range _q.order {
  629. p(selector)
  630. }
  631. if offset := _q.ctx.Offset; offset != nil {
  632. // limit is mandatory for offset clause. We start
  633. // with default value, and override it below if needed.
  634. selector.Offset(*offset).Limit(math.MaxInt32)
  635. }
  636. if limit := _q.ctx.Limit; limit != nil {
  637. selector.Limit(*limit)
  638. }
  639. return selector
  640. }
  641. // ForUpdate locks the selected rows against concurrent updates, and prevent them from being
  642. // updated, deleted or "selected ... for update" by other sessions, until the transaction is
  643. // either committed or rolled-back.
  644. func (_q *ChannelMonitorQuery) ForUpdate(opts ...sql.LockOption) *ChannelMonitorQuery {
  645. if _q.driver.Dialect() == dialect.Postgres {
  646. _q.Unique(false)
  647. }
  648. _q.modifiers = append(_q.modifiers, func(s *sql.Selector) {
  649. s.ForUpdate(opts...)
  650. })
  651. return _q
  652. }
  653. // ForShare behaves similarly to ForUpdate, except that it acquires a shared mode lock
  654. // on any rows that are read. Other sessions can read the rows, but cannot modify them
  655. // until your transaction commits.
  656. func (_q *ChannelMonitorQuery) ForShare(opts ...sql.LockOption) *ChannelMonitorQuery {
  657. if _q.driver.Dialect() == dialect.Postgres {
  658. _q.Unique(false)
  659. }
  660. _q.modifiers = append(_q.modifiers, func(s *sql.Selector) {
  661. s.ForShare(opts...)
  662. })
  663. return _q
  664. }
  665. // ChannelMonitorGroupBy is the group-by builder for ChannelMonitor entities.
  666. type ChannelMonitorGroupBy struct {
  667. selector
  668. build *ChannelMonitorQuery
  669. }
  670. // Aggregate adds the given aggregation functions to the group-by query.
  671. func (_g *ChannelMonitorGroupBy) Aggregate(fns ...AggregateFunc) *ChannelMonitorGroupBy {
  672. _g.fns = append(_g.fns, fns...)
  673. return _g
  674. }
  675. // Scan applies the selector query and scans the result into the given value.
  676. func (_g *ChannelMonitorGroupBy) Scan(ctx context.Context, v any) error {
  677. ctx = setContextOp(ctx, _g.build.ctx, ent.OpQueryGroupBy)
  678. if err := _g.build.prepareQuery(ctx); err != nil {
  679. return err
  680. }
  681. return scanWithInterceptors[*ChannelMonitorQuery, *ChannelMonitorGroupBy](ctx, _g.build, _g, _g.build.inters, v)
  682. }
  683. func (_g *ChannelMonitorGroupBy) sqlScan(ctx context.Context, root *ChannelMonitorQuery, v any) error {
  684. selector := root.sqlQuery(ctx).Select()
  685. aggregation := make([]string, 0, len(_g.fns))
  686. for _, fn := range _g.fns {
  687. aggregation = append(aggregation, fn(selector))
  688. }
  689. if len(selector.SelectedColumns()) == 0 {
  690. columns := make([]string, 0, len(*_g.flds)+len(_g.fns))
  691. for _, f := range *_g.flds {
  692. columns = append(columns, selector.C(f))
  693. }
  694. columns = append(columns, aggregation...)
  695. selector.Select(columns...)
  696. }
  697. selector.GroupBy(selector.Columns(*_g.flds...)...)
  698. if err := selector.Err(); err != nil {
  699. return err
  700. }
  701. rows := &sql.Rows{}
  702. query, args := selector.Query()
  703. if err := _g.build.driver.Query(ctx, query, args, rows); err != nil {
  704. return err
  705. }
  706. defer rows.Close()
  707. return sql.ScanSlice(rows, v)
  708. }
  709. // ChannelMonitorSelect is the builder for selecting fields of ChannelMonitor entities.
  710. type ChannelMonitorSelect struct {
  711. *ChannelMonitorQuery
  712. selector
  713. }
  714. // Aggregate adds the given aggregation functions to the selector query.
  715. func (_s *ChannelMonitorSelect) Aggregate(fns ...AggregateFunc) *ChannelMonitorSelect {
  716. _s.fns = append(_s.fns, fns...)
  717. return _s
  718. }
  719. // Scan applies the selector query and scans the result into the given value.
  720. func (_s *ChannelMonitorSelect) Scan(ctx context.Context, v any) error {
  721. ctx = setContextOp(ctx, _s.ctx, ent.OpQuerySelect)
  722. if err := _s.prepareQuery(ctx); err != nil {
  723. return err
  724. }
  725. return scanWithInterceptors[*ChannelMonitorQuery, *ChannelMonitorSelect](ctx, _s.ChannelMonitorQuery, _s, _s.inters, v)
  726. }
  727. func (_s *ChannelMonitorSelect) sqlScan(ctx context.Context, root *ChannelMonitorQuery, v any) error {
  728. selector := root.sqlQuery(ctx)
  729. aggregation := make([]string, 0, len(_s.fns))
  730. for _, fn := range _s.fns {
  731. aggregation = append(aggregation, fn(selector))
  732. }
  733. switch n := len(*_s.selector.flds); {
  734. case n == 0 && len(aggregation) > 0:
  735. selector.Select(aggregation...)
  736. case n != 0 && len(aggregation) > 0:
  737. selector.AppendSelect(aggregation...)
  738. }
  739. rows := &sql.Rows{}
  740. query, args := selector.Query()
  741. if err := _s.driver.Query(ctx, query, args, rows); err != nil {
  742. return err
  743. }
  744. defer rows.Close()
  745. return sql.ScanSlice(rows, v)
  746. }