ent.go 6.7 KB

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