ent.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "strings"
  7. "github.com/facebookincubator/ent"
  8. "github.com/facebookincubator/ent/dialect"
  9. "github.com/facebookincubator/ent/dialect/sql"
  10. "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
  11. "golang.org/x/xerrors"
  12. )
  13. // ent aliases to avoid import conflict in user's code.
  14. type (
  15. Op = ent.Op
  16. Hook = ent.Hook
  17. Value = ent.Value
  18. Query = ent.Query
  19. Mutator = ent.Mutator
  20. Mutation = ent.Mutation
  21. MutateFunc = ent.MutateFunc
  22. )
  23. // Order applies an ordering on either graph traversal or sql selector.
  24. type Order func(*sql.Selector)
  25. // Asc applies the given fields in ASC order.
  26. func Asc(fields ...string) Order {
  27. return func(s *sql.Selector) {
  28. for _, f := range fields {
  29. s.OrderBy(sql.Asc(f))
  30. }
  31. }
  32. }
  33. // Desc applies the given fields in DESC order.
  34. func Desc(fields ...string) Order {
  35. return func(s *sql.Selector) {
  36. for _, f := range fields {
  37. s.OrderBy(sql.Desc(f))
  38. }
  39. }
  40. }
  41. // Aggregate applies an aggregation step on the group-by traversal/selector.
  42. type Aggregate func(*sql.Selector) string
  43. // As is a pseudo aggregation function for renaming another other functions with custom names. For example:
  44. //
  45. // GroupBy(field1, field2).
  46. // Aggregate(ent.As(ent.Sum(field1), "sum_field1"), (ent.As(ent.Sum(field2), "sum_field2")).
  47. // Scan(ctx, &v)
  48. //
  49. func As(fn Aggregate, end string) Aggregate {
  50. return func(s *sql.Selector) string {
  51. return sql.As(fn(s), end)
  52. }
  53. }
  54. // Count applies the "count" aggregation function on each group.
  55. func Count() Aggregate {
  56. return func(s *sql.Selector) string {
  57. return sql.Count("*")
  58. }
  59. }
  60. // Max applies the "max" aggregation function on the given field of each group.
  61. func Max(field string) Aggregate {
  62. return func(s *sql.Selector) string {
  63. return sql.Max(s.C(field))
  64. }
  65. }
  66. // Mean applies the "mean" aggregation function on the given field of each group.
  67. func Mean(field string) Aggregate {
  68. return func(s *sql.Selector) string {
  69. return sql.Avg(s.C(field))
  70. }
  71. }
  72. // Min applies the "min" aggregation function on the given field of each group.
  73. func Min(field string) Aggregate {
  74. return func(s *sql.Selector) string {
  75. return sql.Min(s.C(field))
  76. }
  77. }
  78. // Sum applies the "sum" aggregation function on the given field of each group.
  79. func Sum(field string) Aggregate {
  80. return func(s *sql.Selector) string {
  81. return sql.Sum(s.C(field))
  82. }
  83. }
  84. // NotFoundError returns when trying to fetch a specific entity and it was not found in the database.
  85. type NotFoundError struct {
  86. label string
  87. }
  88. // Error implements the error interface.
  89. func (e *NotFoundError) Error() string {
  90. return "ent: " + e.label + " not found"
  91. }
  92. // IsNotFound returns a boolean indicating whether the error is a not found error.
  93. func IsNotFound(err error) bool {
  94. if err == nil {
  95. return false
  96. }
  97. var e *NotFoundError
  98. return xerrors.As(err, &e)
  99. }
  100. // MaskNotFound masks nor found error.
  101. func MaskNotFound(err error) error {
  102. if IsNotFound(err) {
  103. return nil
  104. }
  105. return err
  106. }
  107. // NotSingularError returns when trying to fetch a singular entity and more then one was found in the database.
  108. type NotSingularError struct {
  109. label string
  110. }
  111. // Error implements the error interface.
  112. func (e *NotSingularError) Error() string {
  113. return "ent: " + e.label + " not singular"
  114. }
  115. // IsNotSingular returns a boolean indicating whether the error is a not singular error.
  116. func IsNotSingular(err error) bool {
  117. if err == nil {
  118. return false
  119. }
  120. var e *NotSingularError
  121. return xerrors.As(err, &e)
  122. }
  123. // NotLoadedError returns when trying to get a node that was not loaded by the query.
  124. type NotLoadedError struct {
  125. edge string
  126. }
  127. // Error implements the error interface.
  128. func (e *NotLoadedError) Error() string {
  129. return "ent: " + e.edge + " edge was not loaded"
  130. }
  131. // IsNotLoaded returns a boolean indicating whether the error is a not loaded error.
  132. func IsNotLoaded(err error) bool {
  133. if err == nil {
  134. return false
  135. }
  136. var e *NotLoadedError
  137. return xerrors.As(err, &e)
  138. }
  139. // ConstraintError returns when trying to create/update one or more entities and
  140. // one or more of their constraints failed. For example, violation of edge or
  141. // field uniqueness.
  142. type ConstraintError struct {
  143. msg string
  144. wrap error
  145. }
  146. // Error implements the error interface.
  147. func (e ConstraintError) Error() string {
  148. return "ent: constraint failed: " + e.msg
  149. }
  150. // Unwrap implements the errors.Wrapper interface.
  151. func (e *ConstraintError) Unwrap() error {
  152. return e.wrap
  153. }
  154. // IsConstraintError returns a boolean indicating whether the error is a constraint failure.
  155. func IsConstraintError(err error) bool {
  156. if err == nil {
  157. return false
  158. }
  159. var e *ConstraintError
  160. return xerrors.As(err, &e)
  161. }
  162. func isSQLConstraintError(err error) (*ConstraintError, bool) {
  163. var (
  164. msg = err.Error()
  165. // error format per dialect.
  166. errors = [...]string{
  167. "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY).
  168. "UNIQUE constraint failed", // SQLite.
  169. "duplicate key value violates unique constraint", // PostgreSQL.
  170. }
  171. )
  172. if _, ok := err.(*sqlgraph.ConstraintError); ok {
  173. return &ConstraintError{msg, err}, true
  174. }
  175. for i := range errors {
  176. if strings.Contains(msg, errors[i]) {
  177. return &ConstraintError{msg, err}, true
  178. }
  179. }
  180. return nil, false
  181. }
  182. // rollback calls to tx.Rollback and wraps the given error with the rollback error if occurred.
  183. func rollback(tx dialect.Tx, err error) error {
  184. if rerr := tx.Rollback(); rerr != nil {
  185. err = fmt.Errorf("%s: %v", err.Error(), rerr)
  186. }
  187. if err, ok := isSQLConstraintError(err); ok {
  188. return err
  189. }
  190. return err
  191. }
  192. // insertLastID invokes the insert query on the transaction and returns the LastInsertID.
  193. func insertLastID(ctx context.Context, tx dialect.Tx, insert *sql.InsertBuilder) (int64, error) {
  194. query, args := insert.Query()
  195. // PostgreSQL does not support the LastInsertId() method of sql.Result
  196. // on Exec, and should be extracted manually using the `RETURNING` clause.
  197. if insert.Dialect() == dialect.Postgres {
  198. rows := &sql.Rows{}
  199. if err := tx.Query(ctx, query, args, rows); err != nil {
  200. return 0, err
  201. }
  202. defer rows.Close()
  203. if !rows.Next() {
  204. return 0, fmt.Errorf("no rows found for query: %v", query)
  205. }
  206. var id int64
  207. if err := rows.Scan(&id); err != nil {
  208. return 0, err
  209. }
  210. return id, nil
  211. }
  212. // MySQL, SQLite, etc.
  213. var res sql.Result
  214. if err := tx.Exec(ctx, query, args, &res); err != nil {
  215. return 0, err
  216. }
  217. id, err := res.LastInsertId()
  218. if err != nil {
  219. return 0, err
  220. }
  221. return id, nil
  222. }
  223. // keys returns the keys/ids from the edge map.
  224. func keys(m map[int]struct{}) []int {
  225. s := make([]int, 0, len(m))
  226. for id := range m {
  227. s = append(s, id)
  228. }
  229. return s
  230. }