ent.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "errors"
  5. "fmt"
  6. "todo/ent/todo"
  7. "entgo.io/ent"
  8. "entgo.io/ent/dialect/sql"
  9. )
  10. // ent aliases to avoid import conflicts in user's code.
  11. type (
  12. Op = ent.Op
  13. Hook = ent.Hook
  14. Value = ent.Value
  15. Query = ent.Query
  16. Policy = ent.Policy
  17. Mutator = ent.Mutator
  18. Mutation = ent.Mutation
  19. MutateFunc = ent.MutateFunc
  20. )
  21. // OrderFunc applies an ordering on the sql selector.
  22. type OrderFunc func(*sql.Selector)
  23. // columnChecker returns a function indicates if the column exists in the given column.
  24. func columnChecker(table string) func(string) error {
  25. checks := map[string]func(string) bool{
  26. todo.Table: todo.ValidColumn,
  27. }
  28. check, ok := checks[table]
  29. if !ok {
  30. return func(string) error {
  31. return fmt.Errorf("unknown table %q", table)
  32. }
  33. }
  34. return func(column string) error {
  35. if !check(column) {
  36. return fmt.Errorf("unknown column %q for table %q", column, table)
  37. }
  38. return nil
  39. }
  40. }
  41. // Asc applies the given fields in ASC order.
  42. func Asc(fields ...string) OrderFunc {
  43. return func(s *sql.Selector) {
  44. check := columnChecker(s.TableName())
  45. for _, f := range fields {
  46. if err := check(f); err != nil {
  47. s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)})
  48. }
  49. s.OrderBy(sql.Asc(s.C(f)))
  50. }
  51. }
  52. }
  53. // Desc applies the given fields in DESC order.
  54. func Desc(fields ...string) OrderFunc {
  55. return func(s *sql.Selector) {
  56. check := columnChecker(s.TableName())
  57. for _, f := range fields {
  58. if err := check(f); err != nil {
  59. s.AddError(&ValidationError{Name: f, err: fmt.Errorf("ent: %w", err)})
  60. }
  61. s.OrderBy(sql.Desc(s.C(f)))
  62. }
  63. }
  64. }
  65. // AggregateFunc applies an aggregation step on the group-by traversal/selector.
  66. type AggregateFunc func(*sql.Selector) string
  67. // As is a pseudo aggregation function for renaming another other functions with custom names. For example:
  68. //
  69. // GroupBy(field1, field2).
  70. // Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")).
  71. // Scan(ctx, &v)
  72. //
  73. func As(fn AggregateFunc, end string) AggregateFunc {
  74. return func(s *sql.Selector) string {
  75. return sql.As(fn(s), end)
  76. }
  77. }
  78. // Count applies the "count" aggregation function on each group.
  79. func Count() AggregateFunc {
  80. return func(s *sql.Selector) string {
  81. return sql.Count("*")
  82. }
  83. }
  84. // Max applies the "max" aggregation function on the given field of each group.
  85. func Max(field string) AggregateFunc {
  86. return func(s *sql.Selector) string {
  87. check := columnChecker(s.TableName())
  88. if err := check(field); err != nil {
  89. s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
  90. return ""
  91. }
  92. return sql.Max(s.C(field))
  93. }
  94. }
  95. // Mean applies the "mean" aggregation function on the given field of each group.
  96. func Mean(field string) AggregateFunc {
  97. return func(s *sql.Selector) string {
  98. check := columnChecker(s.TableName())
  99. if err := check(field); err != nil {
  100. s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
  101. return ""
  102. }
  103. return sql.Avg(s.C(field))
  104. }
  105. }
  106. // Min applies the "min" aggregation function on the given field of each group.
  107. func Min(field string) AggregateFunc {
  108. return func(s *sql.Selector) string {
  109. check := columnChecker(s.TableName())
  110. if err := check(field); err != nil {
  111. s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
  112. return ""
  113. }
  114. return sql.Min(s.C(field))
  115. }
  116. }
  117. // Sum applies the "sum" aggregation function on the given field of each group.
  118. func Sum(field string) AggregateFunc {
  119. return func(s *sql.Selector) string {
  120. check := columnChecker(s.TableName())
  121. if err := check(field); err != nil {
  122. s.AddError(&ValidationError{Name: field, err: fmt.Errorf("ent: %w", err)})
  123. return ""
  124. }
  125. return sql.Sum(s.C(field))
  126. }
  127. }
  128. // ValidationError returns when validating a field or edge fails.
  129. type ValidationError struct {
  130. Name string // Field or edge name.
  131. err error
  132. }
  133. // Error implements the error interface.
  134. func (e *ValidationError) Error() string {
  135. return e.err.Error()
  136. }
  137. // Unwrap implements the errors.Wrapper interface.
  138. func (e *ValidationError) Unwrap() error {
  139. return e.err
  140. }
  141. // IsValidationError returns a boolean indicating whether the error is a validation error.
  142. func IsValidationError(err error) bool {
  143. if err == nil {
  144. return false
  145. }
  146. var e *ValidationError
  147. return errors.As(err, &e)
  148. }
  149. // NotFoundError returns when trying to fetch a specific entity and it was not found in the database.
  150. type NotFoundError struct {
  151. label string
  152. }
  153. // Error implements the error interface.
  154. func (e *NotFoundError) Error() string {
  155. return "ent: " + e.label + " not found"
  156. }
  157. // IsNotFound returns a boolean indicating whether the error is a not found error.
  158. func IsNotFound(err error) bool {
  159. if err == nil {
  160. return false
  161. }
  162. var e *NotFoundError
  163. return errors.As(err, &e)
  164. }
  165. // MaskNotFound masks not found error.
  166. func MaskNotFound(err error) error {
  167. if IsNotFound(err) {
  168. return nil
  169. }
  170. return err
  171. }
  172. // NotSingularError returns when trying to fetch a singular entity and more then one was found in the database.
  173. type NotSingularError struct {
  174. label string
  175. }
  176. // Error implements the error interface.
  177. func (e *NotSingularError) Error() string {
  178. return "ent: " + e.label + " not singular"
  179. }
  180. // IsNotSingular returns a boolean indicating whether the error is a not singular error.
  181. func IsNotSingular(err error) bool {
  182. if err == nil {
  183. return false
  184. }
  185. var e *NotSingularError
  186. return errors.As(err, &e)
  187. }
  188. // NotLoadedError returns when trying to get a node that was not loaded by the query.
  189. type NotLoadedError struct {
  190. edge string
  191. }
  192. // Error implements the error interface.
  193. func (e *NotLoadedError) Error() string {
  194. return "ent: " + e.edge + " edge was not loaded"
  195. }
  196. // IsNotLoaded returns a boolean indicating whether the error is a not loaded error.
  197. func IsNotLoaded(err error) bool {
  198. if err == nil {
  199. return false
  200. }
  201. var e *NotLoadedError
  202. return errors.As(err, &e)
  203. }
  204. // ConstraintError returns when trying to create/update one or more entities and
  205. // one or more of their constraints failed. For example, violation of edge or
  206. // field uniqueness.
  207. type ConstraintError struct {
  208. msg string
  209. wrap error
  210. }
  211. // Error implements the error interface.
  212. func (e ConstraintError) Error() string {
  213. return "ent: constraint failed: " + e.msg
  214. }
  215. // Unwrap implements the errors.Wrapper interface.
  216. func (e *ConstraintError) Unwrap() error {
  217. return e.wrap
  218. }
  219. // IsConstraintError returns a boolean indicating whether the error is a constraint failure.
  220. func IsConstraintError(err error) bool {
  221. if err == nil {
  222. return false
  223. }
  224. var e *ConstraintError
  225. return errors.As(err, &e)
  226. }