todo_query.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "database/sql/driver"
  6. "errors"
  7. "fmt"
  8. "math"
  9. "todo/ent/predicate"
  10. "todo/ent/todo"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // TodoQuery is the builder for querying Todo entities.
  16. type TodoQuery struct {
  17. config
  18. limit *int
  19. offset *int
  20. unique *bool
  21. order []OrderFunc
  22. fields []string
  23. predicates []predicate.Todo
  24. // eager-loading edges.
  25. withChildren *TodoQuery
  26. withParent *TodoQuery
  27. withFKs bool
  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 TodoQuery builder.
  33. func (tq *TodoQuery) Where(ps ...predicate.Todo) *TodoQuery {
  34. tq.predicates = append(tq.predicates, ps...)
  35. return tq
  36. }
  37. // Limit adds a limit step to the query.
  38. func (tq *TodoQuery) Limit(limit int) *TodoQuery {
  39. tq.limit = &limit
  40. return tq
  41. }
  42. // Offset adds an offset step to the query.
  43. func (tq *TodoQuery) Offset(offset int) *TodoQuery {
  44. tq.offset = &offset
  45. return tq
  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 (tq *TodoQuery) Unique(unique bool) *TodoQuery {
  50. tq.unique = &unique
  51. return tq
  52. }
  53. // Order adds an order step to the query.
  54. func (tq *TodoQuery) Order(o ...OrderFunc) *TodoQuery {
  55. tq.order = append(tq.order, o...)
  56. return tq
  57. }
  58. // QueryChildren chains the current query on the "children" edge.
  59. func (tq *TodoQuery) QueryChildren() *TodoQuery {
  60. query := &TodoQuery{config: tq.config}
  61. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  62. if err := tq.prepareQuery(ctx); err != nil {
  63. return nil, err
  64. }
  65. selector := tq.sqlQuery(ctx)
  66. if err := selector.Err(); err != nil {
  67. return nil, err
  68. }
  69. step := sqlgraph.NewStep(
  70. sqlgraph.From(todo.Table, todo.FieldID, selector),
  71. sqlgraph.To(todo.Table, todo.FieldID),
  72. sqlgraph.Edge(sqlgraph.O2M, true, todo.ChildrenTable, todo.ChildrenColumn),
  73. )
  74. fromU = sqlgraph.SetNeighbors(tq.driver.Dialect(), step)
  75. return fromU, nil
  76. }
  77. return query
  78. }
  79. // QueryParent chains the current query on the "parent" edge.
  80. func (tq *TodoQuery) QueryParent() *TodoQuery {
  81. query := &TodoQuery{config: tq.config}
  82. query.path = func(ctx context.Context) (fromU *sql.Selector, err error) {
  83. if err := tq.prepareQuery(ctx); err != nil {
  84. return nil, err
  85. }
  86. selector := tq.sqlQuery(ctx)
  87. if err := selector.Err(); err != nil {
  88. return nil, err
  89. }
  90. step := sqlgraph.NewStep(
  91. sqlgraph.From(todo.Table, todo.FieldID, selector),
  92. sqlgraph.To(todo.Table, todo.FieldID),
  93. sqlgraph.Edge(sqlgraph.M2O, false, todo.ParentTable, todo.ParentColumn),
  94. )
  95. fromU = sqlgraph.SetNeighbors(tq.driver.Dialect(), step)
  96. return fromU, nil
  97. }
  98. return query
  99. }
  100. // First returns the first Todo entity from the query.
  101. // Returns a *NotFoundError when no Todo was found.
  102. func (tq *TodoQuery) First(ctx context.Context) (*Todo, error) {
  103. nodes, err := tq.Limit(1).All(ctx)
  104. if err != nil {
  105. return nil, err
  106. }
  107. if len(nodes) == 0 {
  108. return nil, &NotFoundError{todo.Label}
  109. }
  110. return nodes[0], nil
  111. }
  112. // FirstX is like First, but panics if an error occurs.
  113. func (tq *TodoQuery) FirstX(ctx context.Context) *Todo {
  114. node, err := tq.First(ctx)
  115. if err != nil && !IsNotFound(err) {
  116. panic(err)
  117. }
  118. return node
  119. }
  120. // FirstID returns the first Todo ID from the query.
  121. // Returns a *NotFoundError when no Todo ID was found.
  122. func (tq *TodoQuery) FirstID(ctx context.Context) (id int, err error) {
  123. var ids []int
  124. if ids, err = tq.Limit(1).IDs(ctx); err != nil {
  125. return
  126. }
  127. if len(ids) == 0 {
  128. err = &NotFoundError{todo.Label}
  129. return
  130. }
  131. return ids[0], nil
  132. }
  133. // FirstIDX is like FirstID, but panics if an error occurs.
  134. func (tq *TodoQuery) FirstIDX(ctx context.Context) int {
  135. id, err := tq.FirstID(ctx)
  136. if err != nil && !IsNotFound(err) {
  137. panic(err)
  138. }
  139. return id
  140. }
  141. // Only returns a single Todo entity found by the query, ensuring it only returns one.
  142. // Returns a *NotSingularError when more than one Todo entity is found.
  143. // Returns a *NotFoundError when no Todo entities are found.
  144. func (tq *TodoQuery) Only(ctx context.Context) (*Todo, error) {
  145. nodes, err := tq.Limit(2).All(ctx)
  146. if err != nil {
  147. return nil, err
  148. }
  149. switch len(nodes) {
  150. case 1:
  151. return nodes[0], nil
  152. case 0:
  153. return nil, &NotFoundError{todo.Label}
  154. default:
  155. return nil, &NotSingularError{todo.Label}
  156. }
  157. }
  158. // OnlyX is like Only, but panics if an error occurs.
  159. func (tq *TodoQuery) OnlyX(ctx context.Context) *Todo {
  160. node, err := tq.Only(ctx)
  161. if err != nil {
  162. panic(err)
  163. }
  164. return node
  165. }
  166. // OnlyID is like Only, but returns the only Todo ID in the query.
  167. // Returns a *NotSingularError when more than one Todo ID is found.
  168. // Returns a *NotFoundError when no entities are found.
  169. func (tq *TodoQuery) OnlyID(ctx context.Context) (id int, err error) {
  170. var ids []int
  171. if ids, err = tq.Limit(2).IDs(ctx); err != nil {
  172. return
  173. }
  174. switch len(ids) {
  175. case 1:
  176. id = ids[0]
  177. case 0:
  178. err = &NotFoundError{todo.Label}
  179. default:
  180. err = &NotSingularError{todo.Label}
  181. }
  182. return
  183. }
  184. // OnlyIDX is like OnlyID, but panics if an error occurs.
  185. func (tq *TodoQuery) OnlyIDX(ctx context.Context) int {
  186. id, err := tq.OnlyID(ctx)
  187. if err != nil {
  188. panic(err)
  189. }
  190. return id
  191. }
  192. // All executes the query and returns a list of Todos.
  193. func (tq *TodoQuery) All(ctx context.Context) ([]*Todo, error) {
  194. if err := tq.prepareQuery(ctx); err != nil {
  195. return nil, err
  196. }
  197. return tq.sqlAll(ctx)
  198. }
  199. // AllX is like All, but panics if an error occurs.
  200. func (tq *TodoQuery) AllX(ctx context.Context) []*Todo {
  201. nodes, err := tq.All(ctx)
  202. if err != nil {
  203. panic(err)
  204. }
  205. return nodes
  206. }
  207. // IDs executes the query and returns a list of Todo IDs.
  208. func (tq *TodoQuery) IDs(ctx context.Context) ([]int, error) {
  209. var ids []int
  210. if err := tq.Select(todo.FieldID).Scan(ctx, &ids); err != nil {
  211. return nil, err
  212. }
  213. return ids, nil
  214. }
  215. // IDsX is like IDs, but panics if an error occurs.
  216. func (tq *TodoQuery) IDsX(ctx context.Context) []int {
  217. ids, err := tq.IDs(ctx)
  218. if err != nil {
  219. panic(err)
  220. }
  221. return ids
  222. }
  223. // Count returns the count of the given query.
  224. func (tq *TodoQuery) Count(ctx context.Context) (int, error) {
  225. if err := tq.prepareQuery(ctx); err != nil {
  226. return 0, err
  227. }
  228. return tq.sqlCount(ctx)
  229. }
  230. // CountX is like Count, but panics if an error occurs.
  231. func (tq *TodoQuery) CountX(ctx context.Context) int {
  232. count, err := tq.Count(ctx)
  233. if err != nil {
  234. panic(err)
  235. }
  236. return count
  237. }
  238. // Exist returns true if the query has elements in the graph.
  239. func (tq *TodoQuery) Exist(ctx context.Context) (bool, error) {
  240. if err := tq.prepareQuery(ctx); err != nil {
  241. return false, err
  242. }
  243. return tq.sqlExist(ctx)
  244. }
  245. // ExistX is like Exist, but panics if an error occurs.
  246. func (tq *TodoQuery) ExistX(ctx context.Context) bool {
  247. exist, err := tq.Exist(ctx)
  248. if err != nil {
  249. panic(err)
  250. }
  251. return exist
  252. }
  253. // Clone returns a duplicate of the TodoQuery builder, including all associated steps. It can be
  254. // used to prepare common query builders and use them differently after the clone is made.
  255. func (tq *TodoQuery) Clone() *TodoQuery {
  256. if tq == nil {
  257. return nil
  258. }
  259. return &TodoQuery{
  260. config: tq.config,
  261. limit: tq.limit,
  262. offset: tq.offset,
  263. order: append([]OrderFunc{}, tq.order...),
  264. predicates: append([]predicate.Todo{}, tq.predicates...),
  265. withChildren: tq.withChildren.Clone(),
  266. withParent: tq.withParent.Clone(),
  267. // clone intermediate query.
  268. sql: tq.sql.Clone(),
  269. path: tq.path,
  270. unique: tq.unique,
  271. }
  272. }
  273. // WithChildren tells the query-builder to eager-load the nodes that are connected to
  274. // the "children" edge. The optional arguments are used to configure the query builder of the edge.
  275. func (tq *TodoQuery) WithChildren(opts ...func(*TodoQuery)) *TodoQuery {
  276. query := &TodoQuery{config: tq.config}
  277. for _, opt := range opts {
  278. opt(query)
  279. }
  280. tq.withChildren = query
  281. return tq
  282. }
  283. // WithParent tells the query-builder to eager-load the nodes that are connected to
  284. // the "parent" edge. The optional arguments are used to configure the query builder of the edge.
  285. func (tq *TodoQuery) WithParent(opts ...func(*TodoQuery)) *TodoQuery {
  286. query := &TodoQuery{config: tq.config}
  287. for _, opt := range opts {
  288. opt(query)
  289. }
  290. tq.withParent = query
  291. return tq
  292. }
  293. // GroupBy is used to group vertices by one or more fields/columns.
  294. // It is often used with aggregate functions, like: count, max, mean, min, sum.
  295. //
  296. // Example:
  297. //
  298. // var v []struct {
  299. // Text string `json:"text,omitempty"`
  300. // Count int `json:"count,omitempty"`
  301. // }
  302. //
  303. // client.Todo.Query().
  304. // GroupBy(todo.FieldText).
  305. // Aggregate(ent.Count()).
  306. // Scan(ctx, &v)
  307. //
  308. func (tq *TodoQuery) GroupBy(field string, fields ...string) *TodoGroupBy {
  309. group := &TodoGroupBy{config: tq.config}
  310. group.fields = append([]string{field}, fields...)
  311. group.path = func(ctx context.Context) (prev *sql.Selector, err error) {
  312. if err := tq.prepareQuery(ctx); err != nil {
  313. return nil, err
  314. }
  315. return tq.sqlQuery(ctx), nil
  316. }
  317. return group
  318. }
  319. // Select allows the selection one or more fields/columns for the given query,
  320. // instead of selecting all fields in the entity.
  321. //
  322. // Example:
  323. //
  324. // var v []struct {
  325. // Text string `json:"text,omitempty"`
  326. // }
  327. //
  328. // client.Todo.Query().
  329. // Select(todo.FieldText).
  330. // Scan(ctx, &v)
  331. //
  332. func (tq *TodoQuery) Select(fields ...string) *TodoSelect {
  333. tq.fields = append(tq.fields, fields...)
  334. return &TodoSelect{TodoQuery: tq}
  335. }
  336. func (tq *TodoQuery) prepareQuery(ctx context.Context) error {
  337. for _, f := range tq.fields {
  338. if !todo.ValidColumn(f) {
  339. return &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  340. }
  341. }
  342. if tq.path != nil {
  343. prev, err := tq.path(ctx)
  344. if err != nil {
  345. return err
  346. }
  347. tq.sql = prev
  348. }
  349. return nil
  350. }
  351. func (tq *TodoQuery) sqlAll(ctx context.Context) ([]*Todo, error) {
  352. var (
  353. nodes = []*Todo{}
  354. withFKs = tq.withFKs
  355. _spec = tq.querySpec()
  356. loadedTypes = [2]bool{
  357. tq.withChildren != nil,
  358. tq.withParent != nil,
  359. }
  360. )
  361. if tq.withParent != nil {
  362. withFKs = true
  363. }
  364. if withFKs {
  365. _spec.Node.Columns = append(_spec.Node.Columns, todo.ForeignKeys...)
  366. }
  367. _spec.ScanValues = func(columns []string) ([]interface{}, error) {
  368. node := &Todo{config: tq.config}
  369. nodes = append(nodes, node)
  370. return node.scanValues(columns)
  371. }
  372. _spec.Assign = func(columns []string, values []interface{}) error {
  373. if len(nodes) == 0 {
  374. return fmt.Errorf("ent: Assign called without calling ScanValues")
  375. }
  376. node := nodes[len(nodes)-1]
  377. node.Edges.loadedTypes = loadedTypes
  378. return node.assignValues(columns, values)
  379. }
  380. if err := sqlgraph.QueryNodes(ctx, tq.driver, _spec); err != nil {
  381. return nil, err
  382. }
  383. if len(nodes) == 0 {
  384. return nodes, nil
  385. }
  386. if query := tq.withChildren; query != nil {
  387. fks := make([]driver.Value, 0, len(nodes))
  388. nodeids := make(map[int]*Todo)
  389. for i := range nodes {
  390. fks = append(fks, nodes[i].ID)
  391. nodeids[nodes[i].ID] = nodes[i]
  392. nodes[i].Edges.Children = []*Todo{}
  393. }
  394. query.withFKs = true
  395. query.Where(predicate.Todo(func(s *sql.Selector) {
  396. s.Where(sql.InValues(todo.ChildrenColumn, fks...))
  397. }))
  398. neighbors, err := query.All(ctx)
  399. if err != nil {
  400. return nil, err
  401. }
  402. for _, n := range neighbors {
  403. fk := n.todo_parent
  404. if fk == nil {
  405. return nil, fmt.Errorf(`foreign-key "todo_parent" is nil for node %v`, n.ID)
  406. }
  407. node, ok := nodeids[*fk]
  408. if !ok {
  409. return nil, fmt.Errorf(`unexpected foreign-key "todo_parent" returned %v for node %v`, *fk, n.ID)
  410. }
  411. node.Edges.Children = append(node.Edges.Children, n)
  412. }
  413. }
  414. if query := tq.withParent; query != nil {
  415. ids := make([]int, 0, len(nodes))
  416. nodeids := make(map[int][]*Todo)
  417. for i := range nodes {
  418. if nodes[i].todo_parent == nil {
  419. continue
  420. }
  421. fk := *nodes[i].todo_parent
  422. if _, ok := nodeids[fk]; !ok {
  423. ids = append(ids, fk)
  424. }
  425. nodeids[fk] = append(nodeids[fk], nodes[i])
  426. }
  427. query.Where(todo.IDIn(ids...))
  428. neighbors, err := query.All(ctx)
  429. if err != nil {
  430. return nil, err
  431. }
  432. for _, n := range neighbors {
  433. nodes, ok := nodeids[n.ID]
  434. if !ok {
  435. return nil, fmt.Errorf(`unexpected foreign-key "todo_parent" returned %v`, n.ID)
  436. }
  437. for i := range nodes {
  438. nodes[i].Edges.Parent = n
  439. }
  440. }
  441. }
  442. return nodes, nil
  443. }
  444. func (tq *TodoQuery) sqlCount(ctx context.Context) (int, error) {
  445. _spec := tq.querySpec()
  446. _spec.Node.Columns = tq.fields
  447. if len(tq.fields) > 0 {
  448. _spec.Unique = tq.unique != nil && *tq.unique
  449. }
  450. return sqlgraph.CountNodes(ctx, tq.driver, _spec)
  451. }
  452. func (tq *TodoQuery) sqlExist(ctx context.Context) (bool, error) {
  453. n, err := tq.sqlCount(ctx)
  454. if err != nil {
  455. return false, fmt.Errorf("ent: check existence: %w", err)
  456. }
  457. return n > 0, nil
  458. }
  459. func (tq *TodoQuery) querySpec() *sqlgraph.QuerySpec {
  460. _spec := &sqlgraph.QuerySpec{
  461. Node: &sqlgraph.NodeSpec{
  462. Table: todo.Table,
  463. Columns: todo.Columns,
  464. ID: &sqlgraph.FieldSpec{
  465. Type: field.TypeInt,
  466. Column: todo.FieldID,
  467. },
  468. },
  469. From: tq.sql,
  470. Unique: true,
  471. }
  472. if unique := tq.unique; unique != nil {
  473. _spec.Unique = *unique
  474. }
  475. if fields := tq.fields; len(fields) > 0 {
  476. _spec.Node.Columns = make([]string, 0, len(fields))
  477. _spec.Node.Columns = append(_spec.Node.Columns, todo.FieldID)
  478. for i := range fields {
  479. if fields[i] != todo.FieldID {
  480. _spec.Node.Columns = append(_spec.Node.Columns, fields[i])
  481. }
  482. }
  483. }
  484. if ps := tq.predicates; len(ps) > 0 {
  485. _spec.Predicate = func(selector *sql.Selector) {
  486. for i := range ps {
  487. ps[i](selector)
  488. }
  489. }
  490. }
  491. if limit := tq.limit; limit != nil {
  492. _spec.Limit = *limit
  493. }
  494. if offset := tq.offset; offset != nil {
  495. _spec.Offset = *offset
  496. }
  497. if ps := tq.order; len(ps) > 0 {
  498. _spec.Order = func(selector *sql.Selector) {
  499. for i := range ps {
  500. ps[i](selector)
  501. }
  502. }
  503. }
  504. return _spec
  505. }
  506. func (tq *TodoQuery) sqlQuery(ctx context.Context) *sql.Selector {
  507. builder := sql.Dialect(tq.driver.Dialect())
  508. t1 := builder.Table(todo.Table)
  509. columns := tq.fields
  510. if len(columns) == 0 {
  511. columns = todo.Columns
  512. }
  513. selector := builder.Select(t1.Columns(columns...)...).From(t1)
  514. if tq.sql != nil {
  515. selector = tq.sql
  516. selector.Select(selector.Columns(columns...)...)
  517. }
  518. if tq.unique != nil && *tq.unique {
  519. selector.Distinct()
  520. }
  521. for _, p := range tq.predicates {
  522. p(selector)
  523. }
  524. for _, p := range tq.order {
  525. p(selector)
  526. }
  527. if offset := tq.offset; offset != nil {
  528. // limit is mandatory for offset clause. We start
  529. // with default value, and override it below if needed.
  530. selector.Offset(*offset).Limit(math.MaxInt32)
  531. }
  532. if limit := tq.limit; limit != nil {
  533. selector.Limit(*limit)
  534. }
  535. return selector
  536. }
  537. // TodoGroupBy is the group-by builder for Todo entities.
  538. type TodoGroupBy struct {
  539. config
  540. fields []string
  541. fns []AggregateFunc
  542. // intermediate query (i.e. traversal path).
  543. sql *sql.Selector
  544. path func(context.Context) (*sql.Selector, error)
  545. }
  546. // Aggregate adds the given aggregation functions to the group-by query.
  547. func (tgb *TodoGroupBy) Aggregate(fns ...AggregateFunc) *TodoGroupBy {
  548. tgb.fns = append(tgb.fns, fns...)
  549. return tgb
  550. }
  551. // Scan applies the group-by query and scans the result into the given value.
  552. func (tgb *TodoGroupBy) Scan(ctx context.Context, v interface{}) error {
  553. query, err := tgb.path(ctx)
  554. if err != nil {
  555. return err
  556. }
  557. tgb.sql = query
  558. return tgb.sqlScan(ctx, v)
  559. }
  560. // ScanX is like Scan, but panics if an error occurs.
  561. func (tgb *TodoGroupBy) ScanX(ctx context.Context, v interface{}) {
  562. if err := tgb.Scan(ctx, v); err != nil {
  563. panic(err)
  564. }
  565. }
  566. // Strings returns list of strings from group-by.
  567. // It is only allowed when executing a group-by query with one field.
  568. func (tgb *TodoGroupBy) Strings(ctx context.Context) ([]string, error) {
  569. if len(tgb.fields) > 1 {
  570. return nil, errors.New("ent: TodoGroupBy.Strings is not achievable when grouping more than 1 field")
  571. }
  572. var v []string
  573. if err := tgb.Scan(ctx, &v); err != nil {
  574. return nil, err
  575. }
  576. return v, nil
  577. }
  578. // StringsX is like Strings, but panics if an error occurs.
  579. func (tgb *TodoGroupBy) StringsX(ctx context.Context) []string {
  580. v, err := tgb.Strings(ctx)
  581. if err != nil {
  582. panic(err)
  583. }
  584. return v
  585. }
  586. // String returns a single string from a group-by query.
  587. // It is only allowed when executing a group-by query with one field.
  588. func (tgb *TodoGroupBy) String(ctx context.Context) (_ string, err error) {
  589. var v []string
  590. if v, err = tgb.Strings(ctx); err != nil {
  591. return
  592. }
  593. switch len(v) {
  594. case 1:
  595. return v[0], nil
  596. case 0:
  597. err = &NotFoundError{todo.Label}
  598. default:
  599. err = fmt.Errorf("ent: TodoGroupBy.Strings returned %d results when one was expected", len(v))
  600. }
  601. return
  602. }
  603. // StringX is like String, but panics if an error occurs.
  604. func (tgb *TodoGroupBy) StringX(ctx context.Context) string {
  605. v, err := tgb.String(ctx)
  606. if err != nil {
  607. panic(err)
  608. }
  609. return v
  610. }
  611. // Ints returns list of ints from group-by.
  612. // It is only allowed when executing a group-by query with one field.
  613. func (tgb *TodoGroupBy) Ints(ctx context.Context) ([]int, error) {
  614. if len(tgb.fields) > 1 {
  615. return nil, errors.New("ent: TodoGroupBy.Ints is not achievable when grouping more than 1 field")
  616. }
  617. var v []int
  618. if err := tgb.Scan(ctx, &v); err != nil {
  619. return nil, err
  620. }
  621. return v, nil
  622. }
  623. // IntsX is like Ints, but panics if an error occurs.
  624. func (tgb *TodoGroupBy) IntsX(ctx context.Context) []int {
  625. v, err := tgb.Ints(ctx)
  626. if err != nil {
  627. panic(err)
  628. }
  629. return v
  630. }
  631. // Int returns a single int from a group-by query.
  632. // It is only allowed when executing a group-by query with one field.
  633. func (tgb *TodoGroupBy) Int(ctx context.Context) (_ int, err error) {
  634. var v []int
  635. if v, err = tgb.Ints(ctx); err != nil {
  636. return
  637. }
  638. switch len(v) {
  639. case 1:
  640. return v[0], nil
  641. case 0:
  642. err = &NotFoundError{todo.Label}
  643. default:
  644. err = fmt.Errorf("ent: TodoGroupBy.Ints returned %d results when one was expected", len(v))
  645. }
  646. return
  647. }
  648. // IntX is like Int, but panics if an error occurs.
  649. func (tgb *TodoGroupBy) IntX(ctx context.Context) int {
  650. v, err := tgb.Int(ctx)
  651. if err != nil {
  652. panic(err)
  653. }
  654. return v
  655. }
  656. // Float64s returns list of float64s from group-by.
  657. // It is only allowed when executing a group-by query with one field.
  658. func (tgb *TodoGroupBy) Float64s(ctx context.Context) ([]float64, error) {
  659. if len(tgb.fields) > 1 {
  660. return nil, errors.New("ent: TodoGroupBy.Float64s is not achievable when grouping more than 1 field")
  661. }
  662. var v []float64
  663. if err := tgb.Scan(ctx, &v); err != nil {
  664. return nil, err
  665. }
  666. return v, nil
  667. }
  668. // Float64sX is like Float64s, but panics if an error occurs.
  669. func (tgb *TodoGroupBy) Float64sX(ctx context.Context) []float64 {
  670. v, err := tgb.Float64s(ctx)
  671. if err != nil {
  672. panic(err)
  673. }
  674. return v
  675. }
  676. // Float64 returns a single float64 from a group-by query.
  677. // It is only allowed when executing a group-by query with one field.
  678. func (tgb *TodoGroupBy) Float64(ctx context.Context) (_ float64, err error) {
  679. var v []float64
  680. if v, err = tgb.Float64s(ctx); err != nil {
  681. return
  682. }
  683. switch len(v) {
  684. case 1:
  685. return v[0], nil
  686. case 0:
  687. err = &NotFoundError{todo.Label}
  688. default:
  689. err = fmt.Errorf("ent: TodoGroupBy.Float64s returned %d results when one was expected", len(v))
  690. }
  691. return
  692. }
  693. // Float64X is like Float64, but panics if an error occurs.
  694. func (tgb *TodoGroupBy) Float64X(ctx context.Context) float64 {
  695. v, err := tgb.Float64(ctx)
  696. if err != nil {
  697. panic(err)
  698. }
  699. return v
  700. }
  701. // Bools returns list of bools from group-by.
  702. // It is only allowed when executing a group-by query with one field.
  703. func (tgb *TodoGroupBy) Bools(ctx context.Context) ([]bool, error) {
  704. if len(tgb.fields) > 1 {
  705. return nil, errors.New("ent: TodoGroupBy.Bools is not achievable when grouping more than 1 field")
  706. }
  707. var v []bool
  708. if err := tgb.Scan(ctx, &v); err != nil {
  709. return nil, err
  710. }
  711. return v, nil
  712. }
  713. // BoolsX is like Bools, but panics if an error occurs.
  714. func (tgb *TodoGroupBy) BoolsX(ctx context.Context) []bool {
  715. v, err := tgb.Bools(ctx)
  716. if err != nil {
  717. panic(err)
  718. }
  719. return v
  720. }
  721. // Bool returns a single bool from a group-by query.
  722. // It is only allowed when executing a group-by query with one field.
  723. func (tgb *TodoGroupBy) Bool(ctx context.Context) (_ bool, err error) {
  724. var v []bool
  725. if v, err = tgb.Bools(ctx); err != nil {
  726. return
  727. }
  728. switch len(v) {
  729. case 1:
  730. return v[0], nil
  731. case 0:
  732. err = &NotFoundError{todo.Label}
  733. default:
  734. err = fmt.Errorf("ent: TodoGroupBy.Bools returned %d results when one was expected", len(v))
  735. }
  736. return
  737. }
  738. // BoolX is like Bool, but panics if an error occurs.
  739. func (tgb *TodoGroupBy) BoolX(ctx context.Context) bool {
  740. v, err := tgb.Bool(ctx)
  741. if err != nil {
  742. panic(err)
  743. }
  744. return v
  745. }
  746. func (tgb *TodoGroupBy) sqlScan(ctx context.Context, v interface{}) error {
  747. for _, f := range tgb.fields {
  748. if !todo.ValidColumn(f) {
  749. return &ValidationError{Name: f, err: fmt.Errorf("invalid field %q for group-by", f)}
  750. }
  751. }
  752. selector := tgb.sqlQuery()
  753. if err := selector.Err(); err != nil {
  754. return err
  755. }
  756. rows := &sql.Rows{}
  757. query, args := selector.Query()
  758. if err := tgb.driver.Query(ctx, query, args, rows); err != nil {
  759. return err
  760. }
  761. defer rows.Close()
  762. return sql.ScanSlice(rows, v)
  763. }
  764. func (tgb *TodoGroupBy) sqlQuery() *sql.Selector {
  765. selector := tgb.sql.Select()
  766. aggregation := make([]string, 0, len(tgb.fns))
  767. for _, fn := range tgb.fns {
  768. aggregation = append(aggregation, fn(selector))
  769. }
  770. // If no columns were selected in a custom aggregation function, the default
  771. // selection is the fields used for "group-by", and the aggregation functions.
  772. if len(selector.SelectedColumns()) == 0 {
  773. columns := make([]string, 0, len(tgb.fields)+len(tgb.fns))
  774. for _, f := range tgb.fields {
  775. columns = append(columns, selector.C(f))
  776. }
  777. columns = append(columns, aggregation...)
  778. selector.Select(columns...)
  779. }
  780. return selector.GroupBy(selector.Columns(tgb.fields...)...)
  781. }
  782. // TodoSelect is the builder for selecting fields of Todo entities.
  783. type TodoSelect struct {
  784. *TodoQuery
  785. // intermediate query (i.e. traversal path).
  786. sql *sql.Selector
  787. }
  788. // Scan applies the selector query and scans the result into the given value.
  789. func (ts *TodoSelect) Scan(ctx context.Context, v interface{}) error {
  790. if err := ts.prepareQuery(ctx); err != nil {
  791. return err
  792. }
  793. ts.sql = ts.TodoQuery.sqlQuery(ctx)
  794. return ts.sqlScan(ctx, v)
  795. }
  796. // ScanX is like Scan, but panics if an error occurs.
  797. func (ts *TodoSelect) ScanX(ctx context.Context, v interface{}) {
  798. if err := ts.Scan(ctx, v); err != nil {
  799. panic(err)
  800. }
  801. }
  802. // Strings returns list of strings from a selector. It is only allowed when selecting one field.
  803. func (ts *TodoSelect) Strings(ctx context.Context) ([]string, error) {
  804. if len(ts.fields) > 1 {
  805. return nil, errors.New("ent: TodoSelect.Strings is not achievable when selecting more than 1 field")
  806. }
  807. var v []string
  808. if err := ts.Scan(ctx, &v); err != nil {
  809. return nil, err
  810. }
  811. return v, nil
  812. }
  813. // StringsX is like Strings, but panics if an error occurs.
  814. func (ts *TodoSelect) StringsX(ctx context.Context) []string {
  815. v, err := ts.Strings(ctx)
  816. if err != nil {
  817. panic(err)
  818. }
  819. return v
  820. }
  821. // String returns a single string from a selector. It is only allowed when selecting one field.
  822. func (ts *TodoSelect) String(ctx context.Context) (_ string, err error) {
  823. var v []string
  824. if v, err = ts.Strings(ctx); err != nil {
  825. return
  826. }
  827. switch len(v) {
  828. case 1:
  829. return v[0], nil
  830. case 0:
  831. err = &NotFoundError{todo.Label}
  832. default:
  833. err = fmt.Errorf("ent: TodoSelect.Strings returned %d results when one was expected", len(v))
  834. }
  835. return
  836. }
  837. // StringX is like String, but panics if an error occurs.
  838. func (ts *TodoSelect) StringX(ctx context.Context) string {
  839. v, err := ts.String(ctx)
  840. if err != nil {
  841. panic(err)
  842. }
  843. return v
  844. }
  845. // Ints returns list of ints from a selector. It is only allowed when selecting one field.
  846. func (ts *TodoSelect) Ints(ctx context.Context) ([]int, error) {
  847. if len(ts.fields) > 1 {
  848. return nil, errors.New("ent: TodoSelect.Ints is not achievable when selecting more than 1 field")
  849. }
  850. var v []int
  851. if err := ts.Scan(ctx, &v); err != nil {
  852. return nil, err
  853. }
  854. return v, nil
  855. }
  856. // IntsX is like Ints, but panics if an error occurs.
  857. func (ts *TodoSelect) IntsX(ctx context.Context) []int {
  858. v, err := ts.Ints(ctx)
  859. if err != nil {
  860. panic(err)
  861. }
  862. return v
  863. }
  864. // Int returns a single int from a selector. It is only allowed when selecting one field.
  865. func (ts *TodoSelect) Int(ctx context.Context) (_ int, err error) {
  866. var v []int
  867. if v, err = ts.Ints(ctx); err != nil {
  868. return
  869. }
  870. switch len(v) {
  871. case 1:
  872. return v[0], nil
  873. case 0:
  874. err = &NotFoundError{todo.Label}
  875. default:
  876. err = fmt.Errorf("ent: TodoSelect.Ints returned %d results when one was expected", len(v))
  877. }
  878. return
  879. }
  880. // IntX is like Int, but panics if an error occurs.
  881. func (ts *TodoSelect) IntX(ctx context.Context) int {
  882. v, err := ts.Int(ctx)
  883. if err != nil {
  884. panic(err)
  885. }
  886. return v
  887. }
  888. // Float64s returns list of float64s from a selector. It is only allowed when selecting one field.
  889. func (ts *TodoSelect) Float64s(ctx context.Context) ([]float64, error) {
  890. if len(ts.fields) > 1 {
  891. return nil, errors.New("ent: TodoSelect.Float64s is not achievable when selecting more than 1 field")
  892. }
  893. var v []float64
  894. if err := ts.Scan(ctx, &v); err != nil {
  895. return nil, err
  896. }
  897. return v, nil
  898. }
  899. // Float64sX is like Float64s, but panics if an error occurs.
  900. func (ts *TodoSelect) Float64sX(ctx context.Context) []float64 {
  901. v, err := ts.Float64s(ctx)
  902. if err != nil {
  903. panic(err)
  904. }
  905. return v
  906. }
  907. // Float64 returns a single float64 from a selector. It is only allowed when selecting one field.
  908. func (ts *TodoSelect) Float64(ctx context.Context) (_ float64, err error) {
  909. var v []float64
  910. if v, err = ts.Float64s(ctx); err != nil {
  911. return
  912. }
  913. switch len(v) {
  914. case 1:
  915. return v[0], nil
  916. case 0:
  917. err = &NotFoundError{todo.Label}
  918. default:
  919. err = fmt.Errorf("ent: TodoSelect.Float64s returned %d results when one was expected", len(v))
  920. }
  921. return
  922. }
  923. // Float64X is like Float64, but panics if an error occurs.
  924. func (ts *TodoSelect) Float64X(ctx context.Context) float64 {
  925. v, err := ts.Float64(ctx)
  926. if err != nil {
  927. panic(err)
  928. }
  929. return v
  930. }
  931. // Bools returns list of bools from a selector. It is only allowed when selecting one field.
  932. func (ts *TodoSelect) Bools(ctx context.Context) ([]bool, error) {
  933. if len(ts.fields) > 1 {
  934. return nil, errors.New("ent: TodoSelect.Bools is not achievable when selecting more than 1 field")
  935. }
  936. var v []bool
  937. if err := ts.Scan(ctx, &v); err != nil {
  938. return nil, err
  939. }
  940. return v, nil
  941. }
  942. // BoolsX is like Bools, but panics if an error occurs.
  943. func (ts *TodoSelect) BoolsX(ctx context.Context) []bool {
  944. v, err := ts.Bools(ctx)
  945. if err != nil {
  946. panic(err)
  947. }
  948. return v
  949. }
  950. // Bool returns a single bool from a selector. It is only allowed when selecting one field.
  951. func (ts *TodoSelect) Bool(ctx context.Context) (_ bool, err error) {
  952. var v []bool
  953. if v, err = ts.Bools(ctx); err != nil {
  954. return
  955. }
  956. switch len(v) {
  957. case 1:
  958. return v[0], nil
  959. case 0:
  960. err = &NotFoundError{todo.Label}
  961. default:
  962. err = fmt.Errorf("ent: TodoSelect.Bools returned %d results when one was expected", len(v))
  963. }
  964. return
  965. }
  966. // BoolX is like Bool, but panics if an error occurs.
  967. func (ts *TodoSelect) BoolX(ctx context.Context) bool {
  968. v, err := ts.Bool(ctx)
  969. if err != nil {
  970. panic(err)
  971. }
  972. return v
  973. }
  974. func (ts *TodoSelect) sqlScan(ctx context.Context, v interface{}) error {
  975. rows := &sql.Rows{}
  976. query, args := ts.sql.Query()
  977. if err := ts.driver.Query(ctx, query, args, rows); err != nil {
  978. return err
  979. }
  980. defer rows.Close()
  981. return sql.ScanSlice(rows, v)
  982. }