server_query.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "database/sql/driver"
  6. "fmt"
  7. "math"
  8. "wechat-api/ent/predicate"
  9. "wechat-api/ent/server"
  10. "wechat-api/ent/wx"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // ServerQuery is the builder for querying Server entities.
  16. type ServerQuery struct {
  17. config
  18. ctx *QueryContext
  19. order []server.OrderOption
  20. inters []Interceptor
  21. predicates []predicate.Server
  22. withWxs *WxQuery
  23. // intermediate query (i.e. traversal path).
  24. sql *sql.Selector
  25. path func(context.Context) (*sql.Selector, error)
  26. }
  27. // Where adds a new predicate for the ServerQuery builder.
  28. func (sq *ServerQuery) Where(ps ...predicate.Server) *ServerQuery {
  29. sq.predicates = append(sq.predicates, ps...)
  30. return sq
  31. }
  32. // Limit the number of records to be returned by this query.
  33. func (sq *ServerQuery) Limit(limit int) *ServerQuery {
  34. sq.ctx.Limit = &limit
  35. return sq
  36. }
  37. // Offset to start from.
  38. func (sq *ServerQuery) Offset(offset int) *ServerQuery {
  39. sq.ctx.Offset = &offset
  40. return sq
  41. }
  42. // Unique configures the query builder to filter duplicate records on query.
  43. // By default, unique is set to true, and can be disabled using this method.
  44. func (sq *ServerQuery) Unique(unique bool) *ServerQuery {
  45. sq.ctx.Unique = &unique
  46. return sq
  47. }
  48. // Order specifies how the records should be ordered.
  49. func (sq *ServerQuery) Order(o ...server.OrderOption) *ServerQuery {
  50. sq.order = append(sq.order, o...)
  51. return sq
  52. }
  53. // QueryWxs chains the current query on the "wxs" edge.
  54. func (sq *ServerQuery) QueryWxs() *WxQuery {
  55. query := (&WxClient{config: sq.config}).Query()
  56. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  57. if err := sq.prepareQuery(ctx); err != nil {
  58. return nil, err
  59. }
  60. selector := sq.sqlQuery(ctx)
  61. if err := selector.Err(); err != nil {
  62. return nil, err
  63. }
  64. step := sqlgraph.NewStep(
  65. sqlgraph.From(server.Table, server.FieldID, selector),
  66. sqlgraph.To(wx.Table, wx.FieldID),
  67. sqlgraph.Edge(sqlgraph.O2M, false, server.WxsTable, server.WxsColumn),
  68. )
  69. fromU = sqlgraph.SetNeighbors(sq.driver.Dialect(), step)
  70. return fromU, nil
  71. }
  72. return query
  73. }
  74. // First returns the first Server entity from the query.
  75. // Returns a *NotFoundError when no Server was found.
  76. func (sq *ServerQuery) First(ctx context.Context) (*Server, error) {
  77. nodes, err := sq.Limit(1).All(setContextOp(ctx, sq.ctx, "First"))
  78. if err != nil {
  79. return nil, err
  80. }
  81. if len(nodes) == 0 {
  82. return nil, &NotFoundError{server.Label}
  83. }
  84. return nodes[0], nil
  85. }
  86. // FirstX is like First, but panics if an error occurs.
  87. func (sq *ServerQuery) FirstX(ctx context.Context) *Server {
  88. node, err := sq.First(ctx)
  89. if err != nil && !IsNotFound(err) {
  90. panic(err)
  91. }
  92. return node
  93. }
  94. // FirstID returns the first Server ID from the query.
  95. // Returns a *NotFoundError when no Server ID was found.
  96. func (sq *ServerQuery) FirstID(ctx context.Context) (id uint64, err error) {
  97. var ids []uint64
  98. if ids, err = sq.Limit(1).IDs(setContextOp(ctx, sq.ctx, "FirstID")); err != nil {
  99. return
  100. }
  101. if len(ids) == 0 {
  102. err = &NotFoundError{server.Label}
  103. return
  104. }
  105. return ids[0], nil
  106. }
  107. // FirstIDX is like FirstID, but panics if an error occurs.
  108. func (sq *ServerQuery) FirstIDX(ctx context.Context) uint64 {
  109. id, err := sq.FirstID(ctx)
  110. if err != nil && !IsNotFound(err) {
  111. panic(err)
  112. }
  113. return id
  114. }
  115. // Only returns a single Server entity found by the query, ensuring it only returns one.
  116. // Returns a *NotSingularError when more than one Server entity is found.
  117. // Returns a *NotFoundError when no Server entities are found.
  118. func (sq *ServerQuery) Only(ctx context.Context) (*Server, error) {
  119. nodes, err := sq.Limit(2).All(setContextOp(ctx, sq.ctx, "Only"))
  120. if err != nil {
  121. return nil, err
  122. }
  123. switch len(nodes) {
  124. case 1:
  125. return nodes[0], nil
  126. case 0:
  127. return nil, &NotFoundError{server.Label}
  128. default:
  129. return nil, &NotSingularError{server.Label}
  130. }
  131. }
  132. // OnlyX is like Only, but panics if an error occurs.
  133. func (sq *ServerQuery) OnlyX(ctx context.Context) *Server {
  134. node, err := sq.Only(ctx)
  135. if err != nil {
  136. panic(err)
  137. }
  138. return node
  139. }
  140. // OnlyID is like Only, but returns the only Server ID in the query.
  141. // Returns a *NotSingularError when more than one Server ID is found.
  142. // Returns a *NotFoundError when no entities are found.
  143. func (sq *ServerQuery) OnlyID(ctx context.Context) (id uint64, err error) {
  144. var ids []uint64
  145. if ids, err = sq.Limit(2).IDs(setContextOp(ctx, sq.ctx, "OnlyID")); err != nil {
  146. return
  147. }
  148. switch len(ids) {
  149. case 1:
  150. id = ids[0]
  151. case 0:
  152. err = &NotFoundError{server.Label}
  153. default:
  154. err = &NotSingularError{server.Label}
  155. }
  156. return
  157. }
  158. // OnlyIDX is like OnlyID, but panics if an error occurs.
  159. func (sq *ServerQuery) OnlyIDX(ctx context.Context) uint64 {
  160. id, err := sq.OnlyID(ctx)
  161. if err != nil {
  162. panic(err)
  163. }
  164. return id
  165. }
  166. // All executes the query and returns a list of Servers.
  167. func (sq *ServerQuery) All(ctx context.Context) ([]*Server, error) {
  168. ctx = setContextOp(ctx, sq.ctx, "All")
  169. if err := sq.prepareQuery(ctx); err != nil {
  170. return nil, err
  171. }
  172. qr := querierAll[[]*Server, *ServerQuery]()
  173. return withInterceptors[[]*Server](ctx, sq, qr, sq.inters)
  174. }
  175. // AllX is like All, but panics if an error occurs.
  176. func (sq *ServerQuery) AllX(ctx context.Context) []*Server {
  177. nodes, err := sq.All(ctx)
  178. if err != nil {
  179. panic(err)
  180. }
  181. return nodes
  182. }
  183. // IDs executes the query and returns a list of Server IDs.
  184. func (sq *ServerQuery) IDs(ctx context.Context) (ids []uint64, err error) {
  185. if sq.ctx.Unique == nil && sq.path != nil {
  186. sq.Unique(true)
  187. }
  188. ctx = setContextOp(ctx, sq.ctx, "IDs")
  189. if err = sq.Select(server.FieldID).Scan(ctx, &ids); err != nil {
  190. return nil, err
  191. }
  192. return ids, nil
  193. }
  194. // IDsX is like IDs, but panics if an error occurs.
  195. func (sq *ServerQuery) IDsX(ctx context.Context) []uint64 {
  196. ids, err := sq.IDs(ctx)
  197. if err != nil {
  198. panic(err)
  199. }
  200. return ids
  201. }
  202. // Count returns the count of the given query.
  203. func (sq *ServerQuery) Count(ctx context.Context) (int, error) {
  204. ctx = setContextOp(ctx, sq.ctx, "Count")
  205. if err := sq.prepareQuery(ctx); err != nil {
  206. return 0, err
  207. }
  208. return withInterceptors[int](ctx, sq, querierCount[*ServerQuery](), sq.inters)
  209. }
  210. // CountX is like Count, but panics if an error occurs.
  211. func (sq *ServerQuery) CountX(ctx context.Context) int {
  212. count, err := sq.Count(ctx)
  213. if err != nil {
  214. panic(err)
  215. }
  216. return count
  217. }
  218. // Exist returns true if the query has elements in the graph.
  219. func (sq *ServerQuery) Exist(ctx context.Context) (bool, error) {
  220. ctx = setContextOp(ctx, sq.ctx, "Exist")
  221. switch _, err := sq.FirstID(ctx); {
  222. case IsNotFound(err):
  223. return false, nil
  224. case err != nil:
  225. return false, fmt.Errorf("ent: check existence: %w", err)
  226. default:
  227. return true, nil
  228. }
  229. }
  230. // ExistX is like Exist, but panics if an error occurs.
  231. func (sq *ServerQuery) ExistX(ctx context.Context) bool {
  232. exist, err := sq.Exist(ctx)
  233. if err != nil {
  234. panic(err)
  235. }
  236. return exist
  237. }
  238. // Clone returns a duplicate of the ServerQuery builder, including all associated steps. It can be
  239. // used to prepare common query builders and use them differently after the clone is made.
  240. func (sq *ServerQuery) Clone() *ServerQuery {
  241. if sq == nil {
  242. return nil
  243. }
  244. return &ServerQuery{
  245. config: sq.config,
  246. ctx: sq.ctx.Clone(),
  247. order: append([]server.OrderOption{}, sq.order...),
  248. inters: append([]Interceptor{}, sq.inters...),
  249. predicates: append([]predicate.Server{}, sq.predicates...),
  250. withWxs: sq.withWxs.Clone(),
  251. // clone intermediate query.
  252. sql: sq.sql.Clone(),
  253. path: sq.path,
  254. }
  255. }
  256. // WithWxs tells the query-builder to eager-load the nodes that are connected to
  257. // the "wxs" edge. The optional arguments are used to configure the query builder of the edge.
  258. func (sq *ServerQuery) WithWxs(opts ...func(*WxQuery)) *ServerQuery {
  259. query := (&WxClient{config: sq.config}).Query()
  260. for _, opt := range opts {
  261. opt(query)
  262. }
  263. sq.withWxs = query
  264. return sq
  265. }
  266. // GroupBy is used to group vertices by one or more fields/columns.
  267. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  268. //
  269. // Example:
  270. //
  271. // var v []struct {
  272. // CreatedAt time.Time `json:"created_at,omitempty"`
  273. // Count int `json:"count,omitempty"`
  274. // }
  275. //
  276. // client.Server.Query().
  277. // GroupBy(server.FieldCreatedAt).
  278. // Aggregate(ent.Count()).
  279. // Scan(ctx, &v)
  280. func (sq *ServerQuery) GroupBy(field string, fields ...string) *ServerGroupBy {
  281. sq.ctx.Fields = append([]string{field}, fields...)
  282. grbuild := &ServerGroupBy{build: sq}
  283. grbuild.flds = &sq.ctx.Fields
  284. grbuild.label = server.Label
  285. grbuild.scan = grbuild.Scan
  286. return grbuild
  287. }
  288. // Select allows the selection one or more fields/columns for the given query,
  289. // instead of selecting all fields in the entity.
  290. //
  291. // Example:
  292. //
  293. // var v []struct {
  294. // CreatedAt time.Time `json:"created_at,omitempty"`
  295. // }
  296. //
  297. // client.Server.Query().
  298. // Select(server.FieldCreatedAt).
  299. // Scan(ctx, &v)
  300. func (sq *ServerQuery) Select(fields ...string) *ServerSelect {
  301. sq.ctx.Fields = append(sq.ctx.Fields, fields...)
  302. sbuild := &ServerSelect{ServerQuery: sq}
  303. sbuild.label = server.Label
  304. sbuild.flds, sbuild.scan = &sq.ctx.Fields, sbuild.Scan
  305. return sbuild
  306. }
  307. // Aggregate returns a ServerSelect configured with the given aggregations.
  308. func (sq *ServerQuery) Aggregate(fns ...AggregateFunc) *ServerSelect {
  309. return sq.Select().Aggregate(fns...)
  310. }
  311. func (sq *ServerQuery) prepareQuery(ctx context.Context) error {
  312. for _, inter := range sq.inters {
  313. if inter == nil {
  314. return fmt.Errorf("ent: uninitialized interceptor (forgotten import ent/runtime?)")
  315. }
  316. if trv, ok := inter.(Traverser); ok {
  317. if err := trv.Traverse(ctx, sq); err != nil {
  318. return err
  319. }
  320. }
  321. }
  322. for _, f := range sq.ctx.Fields {
  323. if !server.ValidColumn(f) {
  324. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  325. }
  326. }
  327. if sq.path != nil {
  328. prev, err := sq.path(ctx)
  329. if err != nil {
  330. return err
  331. }
  332. sq.sql = prev
  333. }
  334. return nil
  335. }
  336. func (sq *ServerQuery) sqlAll(ctx context.Context, hooks ...queryHook) ([]*Server, error) {
  337. var (
  338. nodes = []*Server{}
  339. _spec = sq.querySpec()
  340. loadedTypes = [1]bool{
  341. sq.withWxs != nil,
  342. }
  343. )
  344. _spec.ScanValues = func(columns []string) ([]any, error) {
  345. return (*Server).scanValues(nil, columns)
  346. }
  347. _spec.Assign = func(columns []string, values []any) error {
  348. node := &Server{config: sq.config}
  349. nodes = append(nodes, node)
  350. node.Edges.loadedTypes = loadedTypes
  351. return node.assignValues(columns, values)
  352. }
  353. for i := range hooks {
  354. hooks[i](ctx, _spec)
  355. }
  356. if err := sqlgraph.QueryNodes(ctx, sq.driver, _spec); err != nil {
  357. return nil, err
  358. }
  359. if len(nodes) == 0 {
  360. return nodes, nil
  361. }
  362. if query := sq.withWxs; query != nil {
  363. if err := sq.loadWxs(ctx, query, nodes,
  364. func(n *Server) { n.Edges.Wxs = []*Wx{} },
  365. func(n *Server, e *Wx) { n.Edges.Wxs = append(n.Edges.Wxs, e) }); err != nil {
  366. return nil, err
  367. }
  368. }
  369. return nodes, nil
  370. }
  371. func (sq *ServerQuery) loadWxs(ctx context.Context, query *WxQuery, nodes []*Server, init func(*Server), assign func(*Server, *Wx)) error {
  372. fks := make([]driver.Value, 0, len(nodes))
  373. nodeids := make(map[uint64]*Server)
  374. for i := range nodes {
  375. fks = append(fks, nodes[i].ID)
  376. nodeids[nodes[i].ID] = nodes[i]
  377. if init != nil {
  378. init(nodes[i])
  379. }
  380. }
  381. query.withFKs = true
  382. if len(query.ctx.Fields) > 0 {
  383. query.ctx.AppendFieldOnce(wx.FieldServerID)
  384. }
  385. query.Where(predicate.Wx(func(s *sql.Selector) {
  386. s.Where(sql.InValues(s.C(server.WxsColumn), fks...))
  387. }))
  388. neighbors, err := query.All(ctx)
  389. if err != nil {
  390. return err
  391. }
  392. for _, n := range neighbors {
  393. fk := n.ServerID
  394. node, ok := nodeids[fk]
  395. if !ok {
  396. return fmt.Errorf(`unexpected referenced foreign-key "server_id" returned %v for node %v`, fk, n.ID)
  397. }
  398. assign(node, n)
  399. }
  400. return nil
  401. }
  402. func (sq *ServerQuery) sqlCount(ctx context.Context) (int, error) {
  403. _spec := sq.querySpec()
  404. _spec.Node.Columns = sq.ctx.Fields
  405. if len(sq.ctx.Fields) > 0 {
  406. _spec.Unique = sq.ctx.Unique != nil && *sq.ctx.Unique
  407. }
  408. return sqlgraph.CountNodes(ctx, sq.driver, _spec)
  409. }
  410. func (sq *ServerQuery) querySpec() *sqlgraph.QuerySpec {
  411. _spec := sqlgraph.NewQuerySpec(server.Table, server.Columns, sqlgraph.NewFieldSpec(server.FieldID, field.TypeUint64))
  412. _spec.From = sq.sql
  413. if unique := sq.ctx.Unique; unique != nil {
  414. _spec.Unique = *unique
  415. } else if sq.path != nil {
  416. _spec.Unique = true
  417. }
  418. if fields := sq.ctx.Fields; len(fields) > 0 {
  419. _spec.Node.Columns = make([]string, 0, len(fields))
  420. _spec.Node.Columns = append(_spec.Node.Columns, server.FieldID)
  421. for i := range fields {
  422. if fields[i] != server.FieldID {
  423. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  424. }
  425. }
  426. }
  427. if ps := sq.predicates; len(ps) > 0 {
  428. _spec.Predicate = func(selector *sql.Selector) {
  429. for i := range ps {
  430. ps[i](selector)
  431. }
  432. }
  433. }
  434. if limit := sq.ctx.Limit; limit != nil {
  435. _spec.Limit = *limit
  436. }
  437. if offset := sq.ctx.Offset; offset != nil {
  438. _spec.Offset = *offset
  439. }
  440. if ps := sq.order; len(ps) > 0 {
  441. _spec.Order = func(selector *sql.Selector) {
  442. for i := range ps {
  443. ps[i](selector)
  444. }
  445. }
  446. }
  447. return _spec
  448. }
  449. func (sq *ServerQuery) sqlQuery(ctx context.Context) *sql.Selector {
  450. builder := sql.Dialect(sq.driver.Dialect())
  451. t1 := builder.Table(server.Table)
  452. columns := sq.ctx.Fields
  453. if len(columns) == 0 {
  454. columns = server.Columns
  455. }
  456. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  457. if sq.sql != nil {
  458. selector = sq.sql
  459. selector.Select(selector.Columns(columns...)...)
  460. }
  461. if sq.ctx.Unique != nil && *sq.ctx.Unique {
  462. selector.Distinct()
  463. }
  464. for _, p := range sq.predicates {
  465. p(selector)
  466. }
  467. for _, p := range sq.order {
  468. p(selector)
  469. }
  470. if offset := sq.ctx.Offset; offset != nil {
  471. // limit is mandatory for offset clause. We start
  472. // with default value, and override it below if needed.
  473. selector.Offset(*offset).Limit(math.MaxInt32)
  474. }
  475. if limit := sq.ctx.Limit; limit != nil {
  476. selector.Limit(*limit)
  477. }
  478. return selector
  479. }
  480. // ServerGroupBy is the group-by builder for Server entities.
  481. type ServerGroupBy struct {
  482. selector
  483. build *ServerQuery
  484. }
  485. // Aggregate adds the given aggregation functions to the group-by query.
  486. func (sgb *ServerGroupBy) Aggregate(fns ...AggregateFunc) *ServerGroupBy {
  487. sgb.fns = append(sgb.fns, fns...)
  488. return sgb
  489. }
  490. // Scan applies the selector query and scans the result into the given value.
  491. func (sgb *ServerGroupBy) Scan(ctx context.Context, v any) error {
  492. ctx = setContextOp(ctx, sgb.build.ctx, "GroupBy")
  493. if err := sgb.build.prepareQuery(ctx); err != nil {
  494. return err
  495. }
  496. return scanWithInterceptors[*ServerQuery, *ServerGroupBy](ctx, sgb.build, sgb, sgb.build.inters, v)
  497. }
  498. func (sgb *ServerGroupBy) sqlScan(ctx context.Context, root *ServerQuery, v any) error {
  499. selector := root.sqlQuery(ctx).Select()
  500. aggregation := make([]string, 0, len(sgb.fns))
  501. for _, fn := range sgb.fns {
  502. aggregation = append(aggregation, fn(selector))
  503. }
  504. if len(selector.SelectedColumns()) == 0 {
  505. columns := make([]string, 0, len(*sgb.flds)+len(sgb.fns))
  506. for _, f := range *sgb.flds {
  507. columns = append(columns, selector.C(f))
  508. }
  509. columns = append(columns, aggregation...)
  510. selector.Select(columns...)
  511. }
  512. selector.GroupBy(selector.Columns(*sgb.flds...)...)
  513. if err := selector.Err(); err != nil {
  514. return err
  515. }
  516. rows := &sql.Rows{}
  517. query, args := selector.Query()
  518. if err := sgb.build.driver.Query(ctx, query, args, rows); err != nil {
  519. return err
  520. }
  521. defer rows.Close()
  522. return sql.ScanSlice(rows, v)
  523. }
  524. // ServerSelect is the builder for selecting fields of Server entities.
  525. type ServerSelect struct {
  526. *ServerQuery
  527. selector
  528. }
  529. // Aggregate adds the given aggregation functions to the selector query.
  530. func (ss *ServerSelect) Aggregate(fns ...AggregateFunc) *ServerSelect {
  531. ss.fns = append(ss.fns, fns...)
  532. return ss
  533. }
  534. // Scan applies the selector query and scans the result into the given value.
  535. func (ss *ServerSelect) Scan(ctx context.Context, v any) error {
  536. ctx = setContextOp(ctx, ss.ctx, "Select")
  537. if err := ss.prepareQuery(ctx); err != nil {
  538. return err
  539. }
  540. return scanWithInterceptors[*ServerQuery, *ServerSelect](ctx, ss.ServerQuery, ss, ss.inters, v)
  541. }
  542. func (ss *ServerSelect) sqlScan(ctx context.Context, root *ServerQuery, v any) error {
  543. selector := root.sqlQuery(ctx)
  544. aggregation := make([]string, 0, len(ss.fns))
  545. for _, fn := range ss.fns {
  546. aggregation = append(aggregation, fn(selector))
  547. }
  548. switch n := len(*ss.selector.flds); {
  549. case n == 0 && len(aggregation) > 0:
  550. selector.Select(aggregation...)
  551. case n != 0 && len(aggregation) > 0:
  552. selector.AppendSelect(aggregation...)
  553. }
  554. rows := &sql.Rows{}
  555. query, args := selector.Query()
  556. if err := ss.driver.Query(ctx, query, args, rows); err != nil {
  557. return err
  558. }
  559. defer rows.Close()
  560. return sql.ScanSlice(rows, v)
  561. }