github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/lock_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/lock" 15 "github.com/crowdsecurity/crowdsec/pkg/database/ent/predicate" 16 ) 17 18 // LockUpdate is the builder for updating Lock entities. 19 type LockUpdate struct { 20 config 21 hooks []Hook 22 mutation *LockMutation 23 } 24 25 // Where appends a list predicates to the LockUpdate builder. 26 func (lu *LockUpdate) Where(ps ...predicate.Lock) *LockUpdate { 27 lu.mutation.Where(ps...) 28 return lu 29 } 30 31 // SetName sets the "name" field. 32 func (lu *LockUpdate) SetName(s string) *LockUpdate { 33 lu.mutation.SetName(s) 34 return lu 35 } 36 37 // SetNillableName sets the "name" field if the given value is not nil. 38 func (lu *LockUpdate) SetNillableName(s *string) *LockUpdate { 39 if s != nil { 40 lu.SetName(*s) 41 } 42 return lu 43 } 44 45 // SetCreatedAt sets the "created_at" field. 46 func (lu *LockUpdate) SetCreatedAt(t time.Time) *LockUpdate { 47 lu.mutation.SetCreatedAt(t) 48 return lu 49 } 50 51 // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. 52 func (lu *LockUpdate) SetNillableCreatedAt(t *time.Time) *LockUpdate { 53 if t != nil { 54 lu.SetCreatedAt(*t) 55 } 56 return lu 57 } 58 59 // Mutation returns the LockMutation object of the builder. 60 func (lu *LockUpdate) Mutation() *LockMutation { 61 return lu.mutation 62 } 63 64 // Save executes the query and returns the number of nodes affected by the update operation. 65 func (lu *LockUpdate) Save(ctx context.Context) (int, error) { 66 return withHooks(ctx, lu.sqlSave, lu.mutation, lu.hooks) 67 } 68 69 // SaveX is like Save, but panics if an error occurs. 70 func (lu *LockUpdate) SaveX(ctx context.Context) int { 71 affected, err := lu.Save(ctx) 72 if err != nil { 73 panic(err) 74 } 75 return affected 76 } 77 78 // Exec executes the query. 79 func (lu *LockUpdate) Exec(ctx context.Context) error { 80 _, err := lu.Save(ctx) 81 return err 82 } 83 84 // ExecX is like Exec, but panics if an error occurs. 85 func (lu *LockUpdate) ExecX(ctx context.Context) { 86 if err := lu.Exec(ctx); err != nil { 87 panic(err) 88 } 89 } 90 91 func (lu *LockUpdate) sqlSave(ctx context.Context) (n int, err error) { 92 _spec := sqlgraph.NewUpdateSpec(lock.Table, lock.Columns, sqlgraph.NewFieldSpec(lock.FieldID, field.TypeInt)) 93 if ps := lu.mutation.predicates; len(ps) > 0 { 94 _spec.Predicate = func(selector *sql.Selector) { 95 for i := range ps { 96 ps[i](selector) 97 } 98 } 99 } 100 if value, ok := lu.mutation.Name(); ok { 101 _spec.SetField(lock.FieldName, field.TypeString, value) 102 } 103 if value, ok := lu.mutation.CreatedAt(); ok { 104 _spec.SetField(lock.FieldCreatedAt, field.TypeTime, value) 105 } 106 if n, err = sqlgraph.UpdateNodes(ctx, lu.driver, _spec); err != nil { 107 if _, ok := err.(*sqlgraph.NotFoundError); ok { 108 err = &NotFoundError{lock.Label} 109 } else if sqlgraph.IsConstraintError(err) { 110 err = &ConstraintError{msg: err.Error(), wrap: err} 111 } 112 return 0, err 113 } 114 lu.mutation.done = true 115 return n, nil 116 } 117 118 // LockUpdateOne is the builder for updating a single Lock entity. 119 type LockUpdateOne struct { 120 config 121 fields []string 122 hooks []Hook 123 mutation *LockMutation 124 } 125 126 // SetName sets the "name" field. 127 func (luo *LockUpdateOne) SetName(s string) *LockUpdateOne { 128 luo.mutation.SetName(s) 129 return luo 130 } 131 132 // SetNillableName sets the "name" field if the given value is not nil. 133 func (luo *LockUpdateOne) SetNillableName(s *string) *LockUpdateOne { 134 if s != nil { 135 luo.SetName(*s) 136 } 137 return luo 138 } 139 140 // SetCreatedAt sets the "created_at" field. 141 func (luo *LockUpdateOne) SetCreatedAt(t time.Time) *LockUpdateOne { 142 luo.mutation.SetCreatedAt(t) 143 return luo 144 } 145 146 // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. 147 func (luo *LockUpdateOne) SetNillableCreatedAt(t *time.Time) *LockUpdateOne { 148 if t != nil { 149 luo.SetCreatedAt(*t) 150 } 151 return luo 152 } 153 154 // Mutation returns the LockMutation object of the builder. 155 func (luo *LockUpdateOne) Mutation() *LockMutation { 156 return luo.mutation 157 } 158 159 // Where appends a list predicates to the LockUpdate builder. 160 func (luo *LockUpdateOne) Where(ps ...predicate.Lock) *LockUpdateOne { 161 luo.mutation.Where(ps...) 162 return luo 163 } 164 165 // Select allows selecting one or more fields (columns) of the returned entity. 166 // The default is selecting all fields defined in the entity schema. 167 func (luo *LockUpdateOne) Select(field string, fields ...string) *LockUpdateOne { 168 luo.fields = append([]string{field}, fields...) 169 return luo 170 } 171 172 // Save executes the query and returns the updated Lock entity. 173 func (luo *LockUpdateOne) Save(ctx context.Context) (*Lock, error) { 174 return withHooks(ctx, luo.sqlSave, luo.mutation, luo.hooks) 175 } 176 177 // SaveX is like Save, but panics if an error occurs. 178 func (luo *LockUpdateOne) SaveX(ctx context.Context) *Lock { 179 node, err := luo.Save(ctx) 180 if err != nil { 181 panic(err) 182 } 183 return node 184 } 185 186 // Exec executes the query on the entity. 187 func (luo *LockUpdateOne) Exec(ctx context.Context) error { 188 _, err := luo.Save(ctx) 189 return err 190 } 191 192 // ExecX is like Exec, but panics if an error occurs. 193 func (luo *LockUpdateOne) ExecX(ctx context.Context) { 194 if err := luo.Exec(ctx); err != nil { 195 panic(err) 196 } 197 } 198 199 func (luo *LockUpdateOne) sqlSave(ctx context.Context) (_node *Lock, err error) { 200 _spec := sqlgraph.NewUpdateSpec(lock.Table, lock.Columns, sqlgraph.NewFieldSpec(lock.FieldID, field.TypeInt)) 201 id, ok := luo.mutation.ID() 202 if !ok { 203 return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Lock.id" for update`)} 204 } 205 _spec.Node.ID.Value = id 206 if fields := luo.fields; len(fields) > 0 { 207 _spec.Node.Columns = make([]string, 0, len(fields)) 208 _spec.Node.Columns = append(_spec.Node.Columns, lock.FieldID) 209 for _, f := range fields { 210 if !lock.ValidColumn(f) { 211 return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)} 212 } 213 if f != lock.FieldID { 214 _spec.Node.Columns = append(_spec.Node.Columns, f) 215 } 216 } 217 } 218 if ps := luo.mutation.predicates; len(ps) > 0 { 219 _spec.Predicate = func(selector *sql.Selector) { 220 for i := range ps { 221 ps[i](selector) 222 } 223 } 224 } 225 if value, ok := luo.mutation.Name(); ok { 226 _spec.SetField(lock.FieldName, field.TypeString, value) 227 } 228 if value, ok := luo.mutation.CreatedAt(); ok { 229 _spec.SetField(lock.FieldCreatedAt, field.TypeTime, value) 230 } 231 _node = &Lock{config: luo.config} 232 _spec.Assign = _node.assignValues 233 _spec.ScanValues = _node.scanValues 234 if err = sqlgraph.UpdateNode(ctx, luo.driver, _spec); err != nil { 235 if _, ok := err.(*sqlgraph.NotFoundError); ok { 236 err = &NotFoundError{lock.Label} 237 } else if sqlgraph.IsConstraintError(err) { 238 err = &ConstraintError{msg: err.Error(), wrap: err} 239 } 240 return nil, err 241 } 242 luo.mutation.done = true 243 return _node, nil 244 }