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