github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/event_update.go (about) 1 // Code generated by ent, DO NOT EDIT. 2 3 package ent 4 5 import ( 6 "context" 7 "errors" 8 "fmt" 9 "time" 10 11 "entgo.io/ent/dialect/sql" 12 "entgo.io/ent/dialect/sql/sqlgraph" 13 "entgo.io/ent/schema/field" 14 "github.com/crowdsecurity/crowdsec/pkg/database/ent/alert" 15 "github.com/crowdsecurity/crowdsec/pkg/database/ent/event" 16 "github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate" 17 ) 18 19 // EventUpdate is the builder for updating Event entities. 20 type EventUpdate struct { 21 config 22 hooks []Hook 23 mutation *EventMutation 24 } 25 26 // Where appends a list predicates to the EventUpdate builder. 27 func (eu *EventUpdate) Where(ps ...predicate.Event) *EventUpdate { 28 eu.mutation.Where(ps...) 29 return eu 30 } 31 32 // SetCreatedAt sets the "created_at" field. 33 func (eu *EventUpdate) SetCreatedAt(t time.Time) *EventUpdate { 34 eu.mutation.SetCreatedAt(t) 35 return eu 36 } 37 38 // ClearCreatedAt clears the value of the "created_at" field. 39 func (eu *EventUpdate) ClearCreatedAt() *EventUpdate { 40 eu.mutation.ClearCreatedAt() 41 return eu 42 } 43 44 // SetUpdatedAt sets the "updated_at" field. 45 func (eu *EventUpdate) SetUpdatedAt(t time.Time) *EventUpdate { 46 eu.mutation.SetUpdatedAt(t) 47 return eu 48 } 49 50 // ClearUpdatedAt clears the value of the "updated_at" field. 51 func (eu *EventUpdate) ClearUpdatedAt() *EventUpdate { 52 eu.mutation.ClearUpdatedAt() 53 return eu 54 } 55 56 // SetTime sets the "time" field. 57 func (eu *EventUpdate) SetTime(t time.Time) *EventUpdate { 58 eu.mutation.SetTime(t) 59 return eu 60 } 61 62 // SetNillableTime sets the "time" field if the given value is not nil. 63 func (eu *EventUpdate) SetNillableTime(t *time.Time) *EventUpdate { 64 if t != nil { 65 eu.SetTime(*t) 66 } 67 return eu 68 } 69 70 // SetSerialized sets the "serialized" field. 71 func (eu *EventUpdate) SetSerialized(s string) *EventUpdate { 72 eu.mutation.SetSerialized(s) 73 return eu 74 } 75 76 // SetNillableSerialized sets the "serialized" field if the given value is not nil. 77 func (eu *EventUpdate) SetNillableSerialized(s *string) *EventUpdate { 78 if s != nil { 79 eu.SetSerialized(*s) 80 } 81 return eu 82 } 83 84 // SetAlertEvents sets the "alert_events" field. 85 func (eu *EventUpdate) SetAlertEvents(i int) *EventUpdate { 86 eu.mutation.SetAlertEvents(i) 87 return eu 88 } 89 90 // SetNillableAlertEvents sets the "alert_events" field if the given value is not nil. 91 func (eu *EventUpdate) SetNillableAlertEvents(i *int) *EventUpdate { 92 if i != nil { 93 eu.SetAlertEvents(*i) 94 } 95 return eu 96 } 97 98 // ClearAlertEvents clears the value of the "alert_events" field. 99 func (eu *EventUpdate) ClearAlertEvents() *EventUpdate { 100 eu.mutation.ClearAlertEvents() 101 return eu 102 } 103 104 // SetOwnerID sets the "owner" edge to the Alert entity by ID. 105 func (eu *EventUpdate) SetOwnerID(id int) *EventUpdate { 106 eu.mutation.SetOwnerID(id) 107 return eu 108 } 109 110 // SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil. 111 func (eu *EventUpdate) SetNillableOwnerID(id *int) *EventUpdate { 112 if id != nil { 113 eu = eu.SetOwnerID(*id) 114 } 115 return eu 116 } 117 118 // SetOwner sets the "owner" edge to the Alert entity. 119 func (eu *EventUpdate) SetOwner(a *Alert) *EventUpdate { 120 return eu.SetOwnerID(a.ID) 121 } 122 123 // Mutation returns the EventMutation object of the builder. 124 func (eu *EventUpdate) Mutation() *EventMutation { 125 return eu.mutation 126 } 127 128 // ClearOwner clears the "owner" edge to the Alert entity. 129 func (eu *EventUpdate) ClearOwner() *EventUpdate { 130 eu.mutation.ClearOwner() 131 return eu 132 } 133 134 // Save executes the query and returns the number of nodes affected by the update operation. 135 func (eu *EventUpdate) Save(ctx context.Context) (int, error) { 136 eu.defaults() 137 return withHooks(ctx, eu.sqlSave, eu.mutation, eu.hooks) 138 } 139 140 // SaveX is like Save, but panics if an error occurs. 141 func (eu *EventUpdate) SaveX(ctx context.Context) int { 142 affected, err := eu.Save(ctx) 143 if err != nil { 144 panic(err) 145 } 146 return affected 147 } 148 149 // Exec executes the query. 150 func (eu *EventUpdate) Exec(ctx context.Context) error { 151 _, err := eu.Save(ctx) 152 return err 153 } 154 155 // ExecX is like Exec, but panics if an error occurs. 156 func (eu *EventUpdate) ExecX(ctx context.Context) { 157 if err := eu.Exec(ctx); err != nil { 158 panic(err) 159 } 160 } 161 162 // defaults sets the default values of the builder before save. 163 func (eu *EventUpdate) defaults() { 164 if _, ok := eu.mutation.CreatedAt(); !ok && !eu.mutation.CreatedAtCleared() { 165 v := event.UpdateDefaultCreatedAt() 166 eu.mutation.SetCreatedAt(v) 167 } 168 if _, ok := eu.mutation.UpdatedAt(); !ok && !eu.mutation.UpdatedAtCleared() { 169 v := event.UpdateDefaultUpdatedAt() 170 eu.mutation.SetUpdatedAt(v) 171 } 172 } 173 174 // check runs all checks and user-defined validators on the builder. 175 func (eu *EventUpdate) check() error { 176 if v, ok := eu.mutation.Serialized(); ok { 177 if err := event.SerializedValidator(v); err != nil { 178 return &ValidationError{Name: "serialized", err: fmt.Errorf(`ent: validator failed for field "Event.serialized": %w`, err)} 179 } 180 } 181 return nil 182 } 183 184 func (eu *EventUpdate) sqlSave(ctx context.Context) (n int, err error) { 185 if err := eu.check(); err != nil { 186 return n, err 187 } 188 _spec := sqlgraph.NewUpdateSpec(event.Table, event.Columns, sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt)) 189 if ps := eu.mutation.predicates; len(ps) > 0 { 190 _spec.Predicate = func(selector *sql.Selector) { 191 for i := range ps { 192 ps[i](selector) 193 } 194 } 195 } 196 if value, ok := eu.mutation.CreatedAt(); ok { 197 _spec.SetField(event.FieldCreatedAt, field.TypeTime, value) 198 } 199 if eu.mutation.CreatedAtCleared() { 200 _spec.ClearField(event.FieldCreatedAt, field.TypeTime) 201 } 202 if value, ok := eu.mutation.UpdatedAt(); ok { 203 _spec.SetField(event.FieldUpdatedAt, field.TypeTime, value) 204 } 205 if eu.mutation.UpdatedAtCleared() { 206 _spec.ClearField(event.FieldUpdatedAt, field.TypeTime) 207 } 208 if value, ok := eu.mutation.Time(); ok { 209 _spec.SetField(event.FieldTime, field.TypeTime, value) 210 } 211 if value, ok := eu.mutation.Serialized(); ok { 212 _spec.SetField(event.FieldSerialized, field.TypeString, value) 213 } 214 if eu.mutation.OwnerCleared() { 215 edge := &sqlgraph.EdgeSpec{ 216 Rel: sqlgraph.M2O, 217 Inverse: true, 218 Table: event.OwnerTable, 219 Columns: []string{event.OwnerColumn}, 220 Bidi: false, 221 Target: &sqlgraph.EdgeTarget{ 222 IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt), 223 }, 224 } 225 _spec.Edges.Clear = append(_spec.Edges.Clear, edge) 226 } 227 if nodes := eu.mutation.OwnerIDs(); len(nodes) > 0 { 228 edge := &sqlgraph.EdgeSpec{ 229 Rel: sqlgraph.M2O, 230 Inverse: true, 231 Table: event.OwnerTable, 232 Columns: []string{event.OwnerColumn}, 233 Bidi: false, 234 Target: &sqlgraph.EdgeTarget{ 235 IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt), 236 }, 237 } 238 for _, k := range nodes { 239 edge.Target.Nodes = append(edge.Target.Nodes, k) 240 } 241 _spec.Edges.Add = append(_spec.Edges.Add, edge) 242 } 243 if n, err = sqlgraph.UpdateNodes(ctx, eu.driver, _spec); err != nil { 244 if _, ok := err.(*sqlgraph.NotFoundError); ok { 245 err = &NotFoundError{event.Label} 246 } else if sqlgraph.IsConstraintError(err) { 247 err = &ConstraintError{msg: err.Error(), wrap: err} 248 } 249 return 0, err 250 } 251 eu.mutation.done = true 252 return n, nil 253 } 254 255 // EventUpdateOne is the builder for updating a single Event entity. 256 type EventUpdateOne struct { 257 config 258 fields []string 259 hooks []Hook 260 mutation *EventMutation 261 } 262 263 // SetCreatedAt sets the "created_at" field. 264 func (euo *EventUpdateOne) SetCreatedAt(t time.Time) *EventUpdateOne { 265 euo.mutation.SetCreatedAt(t) 266 return euo 267 } 268 269 // ClearCreatedAt clears the value of the "created_at" field. 270 func (euo *EventUpdateOne) ClearCreatedAt() *EventUpdateOne { 271 euo.mutation.ClearCreatedAt() 272 return euo 273 } 274 275 // SetUpdatedAt sets the "updated_at" field. 276 func (euo *EventUpdateOne) SetUpdatedAt(t time.Time) *EventUpdateOne { 277 euo.mutation.SetUpdatedAt(t) 278 return euo 279 } 280 281 // ClearUpdatedAt clears the value of the "updated_at" field. 282 func (euo *EventUpdateOne) ClearUpdatedAt() *EventUpdateOne { 283 euo.mutation.ClearUpdatedAt() 284 return euo 285 } 286 287 // SetTime sets the "time" field. 288 func (euo *EventUpdateOne) SetTime(t time.Time) *EventUpdateOne { 289 euo.mutation.SetTime(t) 290 return euo 291 } 292 293 // SetNillableTime sets the "time" field if the given value is not nil. 294 func (euo *EventUpdateOne) SetNillableTime(t *time.Time) *EventUpdateOne { 295 if t != nil { 296 euo.SetTime(*t) 297 } 298 return euo 299 } 300 301 // SetSerialized sets the "serialized" field. 302 func (euo *EventUpdateOne) SetSerialized(s string) *EventUpdateOne { 303 euo.mutation.SetSerialized(s) 304 return euo 305 } 306 307 // SetNillableSerialized sets the "serialized" field if the given value is not nil. 308 func (euo *EventUpdateOne) SetNillableSerialized(s *string) *EventUpdateOne { 309 if s != nil { 310 euo.SetSerialized(*s) 311 } 312 return euo 313 } 314 315 // SetAlertEvents sets the "alert_events" field. 316 func (euo *EventUpdateOne) SetAlertEvents(i int) *EventUpdateOne { 317 euo.mutation.SetAlertEvents(i) 318 return euo 319 } 320 321 // SetNillableAlertEvents sets the "alert_events" field if the given value is not nil. 322 func (euo *EventUpdateOne) SetNillableAlertEvents(i *int) *EventUpdateOne { 323 if i != nil { 324 euo.SetAlertEvents(*i) 325 } 326 return euo 327 } 328 329 // ClearAlertEvents clears the value of the "alert_events" field. 330 func (euo *EventUpdateOne) ClearAlertEvents() *EventUpdateOne { 331 euo.mutation.ClearAlertEvents() 332 return euo 333 } 334 335 // SetOwnerID sets the "owner" edge to the Alert entity by ID. 336 func (euo *EventUpdateOne) SetOwnerID(id int) *EventUpdateOne { 337 euo.mutation.SetOwnerID(id) 338 return euo 339 } 340 341 // SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil. 342 func (euo *EventUpdateOne) SetNillableOwnerID(id *int) *EventUpdateOne { 343 if id != nil { 344 euo = euo.SetOwnerID(*id) 345 } 346 return euo 347 } 348 349 // SetOwner sets the "owner" edge to the Alert entity. 350 func (euo *EventUpdateOne) SetOwner(a *Alert) *EventUpdateOne { 351 return euo.SetOwnerID(a.ID) 352 } 353 354 // Mutation returns the EventMutation object of the builder. 355 func (euo *EventUpdateOne) Mutation() *EventMutation { 356 return euo.mutation 357 } 358 359 // ClearOwner clears the "owner" edge to the Alert entity. 360 func (euo *EventUpdateOne) ClearOwner() *EventUpdateOne { 361 euo.mutation.ClearOwner() 362 return euo 363 } 364 365 // Where appends a list predicates to the EventUpdate builder. 366 func (euo *EventUpdateOne) Where(ps ...predicate.Event) *EventUpdateOne { 367 euo.mutation.Where(ps...) 368 return euo 369 } 370 371 // Select allows selecting one or more fields (columns) of the returned entity. 372 // The default is selecting all fields defined in the entity schema. 373 func (euo *EventUpdateOne) Select(field string, fields ...string) *EventUpdateOne { 374 euo.fields = append([]string{field}, fields...) 375 return euo 376 } 377 378 // Save executes the query and returns the updated Event entity. 379 func (euo *EventUpdateOne) Save(ctx context.Context) (*Event, error) { 380 euo.defaults() 381 return withHooks(ctx, euo.sqlSave, euo.mutation, euo.hooks) 382 } 383 384 // SaveX is like Save, but panics if an error occurs. 385 func (euo *EventUpdateOne) SaveX(ctx context.Context) *Event { 386 node, err := euo.Save(ctx) 387 if err != nil { 388 panic(err) 389 } 390 return node 391 } 392 393 // Exec executes the query on the entity. 394 func (euo *EventUpdateOne) Exec(ctx context.Context) error { 395 _, err := euo.Save(ctx) 396 return err 397 } 398 399 // ExecX is like Exec, but panics if an error occurs. 400 func (euo *EventUpdateOne) ExecX(ctx context.Context) { 401 if err := euo.Exec(ctx); err != nil { 402 panic(err) 403 } 404 } 405 406 // defaults sets the default values of the builder before save. 407 func (euo *EventUpdateOne) defaults() { 408 if _, ok := euo.mutation.CreatedAt(); !ok && !euo.mutation.CreatedAtCleared() { 409 v := event.UpdateDefaultCreatedAt() 410 euo.mutation.SetCreatedAt(v) 411 } 412 if _, ok := euo.mutation.UpdatedAt(); !ok && !euo.mutation.UpdatedAtCleared() { 413 v := event.UpdateDefaultUpdatedAt() 414 euo.mutation.SetUpdatedAt(v) 415 } 416 } 417 418 // check runs all checks and user-defined validators on the builder. 419 func (euo *EventUpdateOne) check() error { 420 if v, ok := euo.mutation.Serialized(); ok { 421 if err := event.SerializedValidator(v); err != nil { 422 return &ValidationError{Name: "serialized", err: fmt.Errorf(`ent: validator failed for field "Event.serialized": %w`, err)} 423 } 424 } 425 return nil 426 } 427 428 func (euo *EventUpdateOne) sqlSave(ctx context.Context) (_node *Event, err error) { 429 if err := euo.check(); err != nil { 430 return _node, err 431 } 432 _spec := sqlgraph.NewUpdateSpec(event.Table, event.Columns, sqlgraph.NewFieldSpec(event.FieldID, field.TypeInt)) 433 id, ok := euo.mutation.ID() 434 if !ok { 435 return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Event.id" for update`)} 436 } 437 _spec.Node.ID.Value = id 438 if fields := euo.fields; len(fields) > 0 { 439 _spec.Node.Columns = make([]string, 0, len(fields)) 440 _spec.Node.Columns = append(_spec.Node.Columns, event.FieldID) 441 for _, f := range fields { 442 if !event.ValidColumn(f) { 443 return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 444 } 445 if f != event.FieldID { 446 _spec.Node.Columns = append(_spec.Node.Columns, f) 447 } 448 } 449 } 450 if ps := euo.mutation.predicates; len(ps) > 0 { 451 _spec.Predicate = func(selector *sql.Selector) { 452 for i := range ps { 453 ps[i](selector) 454 } 455 } 456 } 457 if value, ok := euo.mutation.CreatedAt(); ok { 458 _spec.SetField(event.FieldCreatedAt, field.TypeTime, value) 459 } 460 if euo.mutation.CreatedAtCleared() { 461 _spec.ClearField(event.FieldCreatedAt, field.TypeTime) 462 } 463 if value, ok := euo.mutation.UpdatedAt(); ok { 464 _spec.SetField(event.FieldUpdatedAt, field.TypeTime, value) 465 } 466 if euo.mutation.UpdatedAtCleared() { 467 _spec.ClearField(event.FieldUpdatedAt, field.TypeTime) 468 } 469 if value, ok := euo.mutation.Time(); ok { 470 _spec.SetField(event.FieldTime, field.TypeTime, value) 471 } 472 if value, ok := euo.mutation.Serialized(); ok { 473 _spec.SetField(event.FieldSerialized, field.TypeString, value) 474 } 475 if euo.mutation.OwnerCleared() { 476 edge := &sqlgraph.EdgeSpec{ 477 Rel: sqlgraph.M2O, 478 Inverse: true, 479 Table: event.OwnerTable, 480 Columns: []string{event.OwnerColumn}, 481 Bidi: false, 482 Target: &sqlgraph.EdgeTarget{ 483 IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt), 484 }, 485 } 486 _spec.Edges.Clear = append(_spec.Edges.Clear, edge) 487 } 488 if nodes := euo.mutation.OwnerIDs(); len(nodes) > 0 { 489 edge := &sqlgraph.EdgeSpec{ 490 Rel: sqlgraph.M2O, 491 Inverse: true, 492 Table: event.OwnerTable, 493 Columns: []string{event.OwnerColumn}, 494 Bidi: false, 495 Target: &sqlgraph.EdgeTarget{ 496 IDSpec: sqlgraph.NewFieldSpec(alert.FieldID, field.TypeInt), 497 }, 498 } 499 for _, k := range nodes { 500 edge.Target.Nodes = append(edge.Target.Nodes, k) 501 } 502 _spec.Edges.Add = append(_spec.Edges.Add, edge) 503 } 504 _node = &Event{config: euo.config} 505 _spec.Assign = _node.assignValues 506 _spec.ScanValues = _node.scanValues 507 if err = sqlgraph.UpdateNode(ctx, euo.driver, _spec); err != nil { 508 if _, ok := err.(*sqlgraph.NotFoundError); ok { 509 err = &NotFoundError{event.Label} 510 } else if sqlgraph.IsConstraintError(err) { 511 err = &ConstraintError{msg: err.Error(), wrap: err} 512 } 513 return nil, err 514 } 515 euo.mutation.done = true 516 return _node, nil 517 }