proxy_query.go 23 KB

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