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