github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/configitem_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/configitem" 15 "github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate" 16 ) 17 18 // ConfigItemUpdate is the builder for updating ConfigItem entities. 19 type ConfigItemUpdate struct { 20 config 21 hooks []Hook 22 mutation *ConfigItemMutation 23 } 24 25 // Where appends a list predicates to the ConfigItemUpdate builder. 26 func (ciu *ConfigItemUpdate) Where(ps ...predicate.ConfigItem) *ConfigItemUpdate { 27 ciu.mutation.Where(ps...) 28 return ciu 29 } 30 31 // SetCreatedAt sets the "created_at" field. 32 func (ciu *ConfigItemUpdate) SetCreatedAt(t time.Time) *ConfigItemUpdate { 33 ciu.mutation.SetCreatedAt(t) 34 return ciu 35 } 36 37 // ClearCreatedAt clears the value of the "created_at" field. 38 func (ciu *ConfigItemUpdate) ClearCreatedAt() *ConfigItemUpdate { 39 ciu.mutation.ClearCreatedAt() 40 return ciu 41 } 42 43 // SetUpdatedAt sets the "updated_at" field. 44 func (ciu *ConfigItemUpdate) SetUpdatedAt(t time.Time) *ConfigItemUpdate { 45 ciu.mutation.SetUpdatedAt(t) 46 return ciu 47 } 48 49 // ClearUpdatedAt clears the value of the "updated_at" field. 50 func (ciu *ConfigItemUpdate) ClearUpdatedAt() *ConfigItemUpdate { 51 ciu.mutation.ClearUpdatedAt() 52 return ciu 53 } 54 55 // SetName sets the "name" field. 56 func (ciu *ConfigItemUpdate) SetName(s string) *ConfigItemUpdate { 57 ciu.mutation.SetName(s) 58 return ciu 59 } 60 61 // SetNillableName sets the "name" field if the given value is not nil. 62 func (ciu *ConfigItemUpdate) SetNillableName(s *string) *ConfigItemUpdate { 63 if s != nil { 64 ciu.SetName(*s) 65 } 66 return ciu 67 } 68 69 // SetValue sets the "value" field. 70 func (ciu *ConfigItemUpdate) SetValue(s string) *ConfigItemUpdate { 71 ciu.mutation.SetValue(s) 72 return ciu 73 } 74 75 // SetNillableValue sets the "value" field if the given value is not nil. 76 func (ciu *ConfigItemUpdate) SetNillableValue(s *string) *ConfigItemUpdate { 77 if s != nil { 78 ciu.SetValue(*s) 79 } 80 return ciu 81 } 82 83 // Mutation returns the ConfigItemMutation object of the builder. 84 func (ciu *ConfigItemUpdate) Mutation() *ConfigItemMutation { 85 return ciu.mutation 86 } 87 88 // Save executes the query and returns the number of nodes affected by the update operation. 89 func (ciu *ConfigItemUpdate) Save(ctx context.Context) (int, error) { 90 ciu.defaults() 91 return withHooks(ctx, ciu.sqlSave, ciu.mutation, ciu.hooks) 92 } 93 94 // SaveX is like Save, but panics if an error occurs. 95 func (ciu *ConfigItemUpdate) SaveX(ctx context.Context) int { 96 affected, err := ciu.Save(ctx) 97 if err != nil { 98 panic(err) 99 } 100 return affected 101 } 102 103 // Exec executes the query. 104 func (ciu *ConfigItemUpdate) Exec(ctx context.Context) error { 105 _, err := ciu.Save(ctx) 106 return err 107 } 108 109 // ExecX is like Exec, but panics if an error occurs. 110 func (ciu *ConfigItemUpdate) ExecX(ctx context.Context) { 111 if err := ciu.Exec(ctx); err != nil { 112 panic(err) 113 } 114 } 115 116 // defaults sets the default values of the builder before save. 117 func (ciu *ConfigItemUpdate) defaults() { 118 if _, ok := ciu.mutation.CreatedAt(); !ok && !ciu.mutation.CreatedAtCleared() { 119 v := configitem.UpdateDefaultCreatedAt() 120 ciu.mutation.SetCreatedAt(v) 121 } 122 if _, ok := ciu.mutation.UpdatedAt(); !ok && !ciu.mutation.UpdatedAtCleared() { 123 v := configitem.UpdateDefaultUpdatedAt() 124 ciu.mutation.SetUpdatedAt(v) 125 } 126 } 127 128 func (ciu *ConfigItemUpdate) sqlSave(ctx context.Context) (n int, err error) { 129 _spec := sqlgraph.NewUpdateSpec(configitem.Table, configitem.Columns, sqlgraph.NewFieldSpec(configitem.FieldID, field.TypeInt)) 130 if ps := ciu.mutation.predicates; len(ps) > 0 { 131 _spec.Predicate = func(selector *sql.Selector) { 132 for i := range ps { 133 ps[i](selector) 134 } 135 } 136 } 137 if value, ok := ciu.mutation.CreatedAt(); ok { 138 _spec.SetField(configitem.FieldCreatedAt, field.TypeTime, value) 139 } 140 if ciu.mutation.CreatedAtCleared() { 141 _spec.ClearField(configitem.FieldCreatedAt, field.TypeTime) 142 } 143 if value, ok := ciu.mutation.UpdatedAt(); ok { 144 _spec.SetField(configitem.FieldUpdatedAt, field.TypeTime, value) 145 } 146 if ciu.mutation.UpdatedAtCleared() { 147 _spec.ClearField(configitem.FieldUpdatedAt, field.TypeTime) 148 } 149 if value, ok := ciu.mutation.Name(); ok { 150 _spec.SetField(configitem.FieldName, field.TypeString, value) 151 } 152 if value, ok := ciu.mutation.Value(); ok { 153 _spec.SetField(configitem.FieldValue, field.TypeString, value) 154 } 155 if n, err = sqlgraph.UpdateNodes(ctx, ciu.driver, _spec); err != nil { 156 if _, ok := err.(*sqlgraph.NotFoundError); ok { 157 err = &NotFoundError{configitem.Label} 158 } else if sqlgraph.IsConstraintError(err) { 159 err = &ConstraintError{msg: err.Error(), wrap: err} 160 } 161 return 0, err 162 } 163 ciu.mutation.done = true 164 return n, nil 165 } 166 167 // ConfigItemUpdateOne is the builder for updating a single ConfigItem entity. 168 type ConfigItemUpdateOne struct { 169 config 170 fields []string 171 hooks []Hook 172 mutation *ConfigItemMutation 173 } 174 175 // SetCreatedAt sets the "created_at" field. 176 func (ciuo *ConfigItemUpdateOne) SetCreatedAt(t time.Time) *ConfigItemUpdateOne { 177 ciuo.mutation.SetCreatedAt(t) 178 return ciuo 179 } 180 181 // ClearCreatedAt clears the value of the "created_at" field. 182 func (ciuo *ConfigItemUpdateOne) ClearCreatedAt() *ConfigItemUpdateOne { 183 ciuo.mutation.ClearCreatedAt() 184 return ciuo 185 } 186 187 // SetUpdatedAt sets the "updated_at" field. 188 func (ciuo *ConfigItemUpdateOne) SetUpdatedAt(t time.Time) *ConfigItemUpdateOne { 189 ciuo.mutation.SetUpdatedAt(t) 190 return ciuo 191 } 192 193 // ClearUpdatedAt clears the value of the "updated_at" field. 194 func (ciuo *ConfigItemUpdateOne) ClearUpdatedAt() *ConfigItemUpdateOne { 195 ciuo.mutation.ClearUpdatedAt() 196 return ciuo 197 } 198 199 // SetName sets the "name" field. 200 func (ciuo *ConfigItemUpdateOne) SetName(s string) *ConfigItemUpdateOne { 201 ciuo.mutation.SetName(s) 202 return ciuo 203 } 204 205 // SetNillableName sets the "name" field if the given value is not nil. 206 func (ciuo *ConfigItemUpdateOne) SetNillableName(s *string) *ConfigItemUpdateOne { 207 if s != nil { 208 ciuo.SetName(*s) 209 } 210 return ciuo 211 } 212 213 // SetValue sets the "value" field. 214 func (ciuo *ConfigItemUpdateOne) SetValue(s string) *ConfigItemUpdateOne { 215 ciuo.mutation.SetValue(s) 216 return ciuo 217 } 218 219 // SetNillableValue sets the "value" field if the given value is not nil. 220 func (ciuo *ConfigItemUpdateOne) SetNillableValue(s *string) *ConfigItemUpdateOne { 221 if s != nil { 222 ciuo.SetValue(*s) 223 } 224 return ciuo 225 } 226 227 // Mutation returns the ConfigItemMutation object of the builder. 228 func (ciuo *ConfigItemUpdateOne) Mutation() *ConfigItemMutation { 229 return ciuo.mutation 230 } 231 232 // Where appends a list predicates to the ConfigItemUpdate builder. 233 func (ciuo *ConfigItemUpdateOne) Where(ps ...predicate.ConfigItem) *ConfigItemUpdateOne { 234 ciuo.mutation.Where(ps...) 235 return ciuo 236 } 237 238 // Select allows selecting one or more fields (columns) of the returned entity. 239 // The default is selecting all fields defined in the entity schema. 240 func (ciuo *ConfigItemUpdateOne) Select(field string, fields ...string) *ConfigItemUpdateOne { 241 ciuo.fields = append([]string{field}, fields...) 242 return ciuo 243 } 244 245 // Save executes the query and returns the updated ConfigItem entity. 246 func (ciuo *ConfigItemUpdateOne) Save(ctx context.Context) (*ConfigItem, error) { 247 ciuo.defaults() 248 return withHooks(ctx, ciuo.sqlSave, ciuo.mutation, ciuo.hooks) 249 } 250 251 // SaveX is like Save, but panics if an error occurs. 252 func (ciuo *ConfigItemUpdateOne) SaveX(ctx context.Context) *ConfigItem { 253 node, err := ciuo.Save(ctx) 254 if err != nil { 255 panic(err) 256 } 257 return node 258 } 259 260 // Exec executes the query on the entity. 261 func (ciuo *ConfigItemUpdateOne) Exec(ctx context.Context) error { 262 _, err := ciuo.Save(ctx) 263 return err 264 } 265 266 // ExecX is like Exec, but panics if an error occurs. 267 func (ciuo *ConfigItemUpdateOne) ExecX(ctx context.Context) { 268 if err := ciuo.Exec(ctx); err != nil { 269 panic(err) 270 } 271 } 272 273 // defaults sets the default values of the builder before save. 274 func (ciuo *ConfigItemUpdateOne) defaults() { 275 if _, ok := ciuo.mutation.CreatedAt(); !ok && !ciuo.mutation.CreatedAtCleared() { 276 v := configitem.UpdateDefaultCreatedAt() 277 ciuo.mutation.SetCreatedAt(v) 278 } 279 if _, ok := ciuo.mutation.UpdatedAt(); !ok && !ciuo.mutation.UpdatedAtCleared() { 280 v := configitem.UpdateDefaultUpdatedAt() 281 ciuo.mutation.SetUpdatedAt(v) 282 } 283 } 284 285 func (ciuo *ConfigItemUpdateOne) sqlSave(ctx context.Context) (_node *ConfigItem, err error) { 286 _spec := sqlgraph.NewUpdateSpec(configitem.Table, configitem.Columns, sqlgraph.NewFieldSpec(configitem.FieldID, field.TypeInt)) 287 id, ok := ciuo.mutation.ID() 288 if !ok { 289 return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ConfigItem.id" for update`)} 290 } 291 _spec.Node.ID.Value = id 292 if fields := ciuo.fields; len(fields) > 0 { 293 _spec.Node.Columns = make([]string, 0, len(fields)) 294 _spec.Node.Columns = append(_spec.Node.Columns, configitem.FieldID) 295 for _, f := range fields { 296 if !configitem.ValidColumn(f) { 297 return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 298 } 299 if f != configitem.FieldID { 300 _spec.Node.Columns = append(_spec.Node.Columns, f) 301 } 302 } 303 } 304 if ps := ciuo.mutation.predicates; len(ps) > 0 { 305 _spec.Predicate = func(selector *sql.Selector) { 306 for i := range ps { 307 ps[i](selector) 308 } 309 } 310 } 311 if value, ok := ciuo.mutation.CreatedAt(); ok { 312 _spec.SetField(configitem.FieldCreatedAt, field.TypeTime, value) 313 } 314 if ciuo.mutation.CreatedAtCleared() { 315 _spec.ClearField(configitem.FieldCreatedAt, field.TypeTime) 316 } 317 if value, ok := ciuo.mutation.UpdatedAt(); ok { 318 _spec.SetField(configitem.FieldUpdatedAt, field.TypeTime, value) 319 } 320 if ciuo.mutation.UpdatedAtCleared() { 321 _spec.ClearField(configitem.FieldUpdatedAt, field.TypeTime) 322 } 323 if value, ok := ciuo.mutation.Name(); ok { 324 _spec.SetField(configitem.FieldName, field.TypeString, value) 325 } 326 if value, ok := ciuo.mutation.Value(); ok { 327 _spec.SetField(configitem.FieldValue, field.TypeString, value) 328 } 329 _node = &ConfigItem{config: ciuo.config} 330 _spec.Assign = _node.assignValues 331 _spec.ScanValues = _node.scanValues 332 if err = sqlgraph.UpdateNode(ctx, ciuo.driver, _spec); err != nil { 333 if _, ok := err.(*sqlgraph.NotFoundError); ok { 334 err = &NotFoundError{configitem.Label} 335 } else if sqlgraph.IsConstraintError(err) { 336 err = &ConstraintError{msg: err.Error(), wrap: err} 337 } 338 return nil, err 339 } 340 ciuo.mutation.done = true 341 return _node, nil 342 }