bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/database/ent/machine_create.go (about) 1 // Code generated by entc, 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/sqlgraph" 12 "entgo.io/ent/schema/field" 13 "bitbucket.org/Aishee/synsec/pkg/database/ent/alert" 14 "bitbucket.org/Aishee/synsec/pkg/database/ent/machine" 15 ) 16 17 // MachineCreate is the builder for creating a Machine entity. 18 type MachineCreate struct { 19 config 20 mutation *MachineMutation 21 hooks []Hook 22 } 23 24 // SetCreatedAt sets the "created_at" field. 25 func (mc *MachineCreate) SetCreatedAt(t time.Time) *MachineCreate { 26 mc.mutation.SetCreatedAt(t) 27 return mc 28 } 29 30 // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. 31 func (mc *MachineCreate) SetNillableCreatedAt(t *time.Time) *MachineCreate { 32 if t != nil { 33 mc.SetCreatedAt(*t) 34 } 35 return mc 36 } 37 38 // SetUpdatedAt sets the "updated_at" field. 39 func (mc *MachineCreate) SetUpdatedAt(t time.Time) *MachineCreate { 40 mc.mutation.SetUpdatedAt(t) 41 return mc 42 } 43 44 // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. 45 func (mc *MachineCreate) SetNillableUpdatedAt(t *time.Time) *MachineCreate { 46 if t != nil { 47 mc.SetUpdatedAt(*t) 48 } 49 return mc 50 } 51 52 // SetMachineId sets the "machineId" field. 53 func (mc *MachineCreate) SetMachineId(s string) *MachineCreate { 54 mc.mutation.SetMachineId(s) 55 return mc 56 } 57 58 // SetPassword sets the "password" field. 59 func (mc *MachineCreate) SetPassword(s string) *MachineCreate { 60 mc.mutation.SetPassword(s) 61 return mc 62 } 63 64 // SetIpAddress sets the "ipAddress" field. 65 func (mc *MachineCreate) SetIpAddress(s string) *MachineCreate { 66 mc.mutation.SetIpAddress(s) 67 return mc 68 } 69 70 // SetScenarios sets the "scenarios" field. 71 func (mc *MachineCreate) SetScenarios(s string) *MachineCreate { 72 mc.mutation.SetScenarios(s) 73 return mc 74 } 75 76 // SetNillableScenarios sets the "scenarios" field if the given value is not nil. 77 func (mc *MachineCreate) SetNillableScenarios(s *string) *MachineCreate { 78 if s != nil { 79 mc.SetScenarios(*s) 80 } 81 return mc 82 } 83 84 // SetVersion sets the "version" field. 85 func (mc *MachineCreate) SetVersion(s string) *MachineCreate { 86 mc.mutation.SetVersion(s) 87 return mc 88 } 89 90 // SetNillableVersion sets the "version" field if the given value is not nil. 91 func (mc *MachineCreate) SetNillableVersion(s *string) *MachineCreate { 92 if s != nil { 93 mc.SetVersion(*s) 94 } 95 return mc 96 } 97 98 // SetIsValidated sets the "isValidated" field. 99 func (mc *MachineCreate) SetIsValidated(b bool) *MachineCreate { 100 mc.mutation.SetIsValidated(b) 101 return mc 102 } 103 104 // SetNillableIsValidated sets the "isValidated" field if the given value is not nil. 105 func (mc *MachineCreate) SetNillableIsValidated(b *bool) *MachineCreate { 106 if b != nil { 107 mc.SetIsValidated(*b) 108 } 109 return mc 110 } 111 112 // SetStatus sets the "status" field. 113 func (mc *MachineCreate) SetStatus(s string) *MachineCreate { 114 mc.mutation.SetStatus(s) 115 return mc 116 } 117 118 // SetNillableStatus sets the "status" field if the given value is not nil. 119 func (mc *MachineCreate) SetNillableStatus(s *string) *MachineCreate { 120 if s != nil { 121 mc.SetStatus(*s) 122 } 123 return mc 124 } 125 126 // AddAlertIDs adds the "alerts" edge to the Alert entity by IDs. 127 func (mc *MachineCreate) AddAlertIDs(ids ...int) *MachineCreate { 128 mc.mutation.AddAlertIDs(ids...) 129 return mc 130 } 131 132 // AddAlerts adds the "alerts" edges to the Alert entity. 133 func (mc *MachineCreate) AddAlerts(a ...*Alert) *MachineCreate { 134 ids := make([]int, len(a)) 135 for i := range a { 136 ids[i] = a[i].ID 137 } 138 return mc.AddAlertIDs(ids...) 139 } 140 141 // Mutation returns the MachineMutation object of the builder. 142 func (mc *MachineCreate) Mutation() *MachineMutation { 143 return mc.mutation 144 } 145 146 // Save creates the Machine in the database. 147 func (mc *MachineCreate) Save(ctx context.Context) (*Machine, error) { 148 var ( 149 err error 150 node *Machine 151 ) 152 mc.defaults() 153 if len(mc.hooks) == 0 { 154 if err = mc.check(); err != nil { 155 return nil, err 156 } 157 node, err = mc.sqlSave(ctx) 158 } else { 159 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 160 mutation, ok := m.(*MachineMutation) 161 if !ok { 162 return nil, fmt.Errorf("unexpected mutation type %T", m) 163 } 164 if err = mc.check(); err != nil { 165 return nil, err 166 } 167 mc.mutation = mutation 168 node, err = mc.sqlSave(ctx) 169 mutation.done = true 170 return node, err 171 }) 172 for i := len(mc.hooks) - 1; i >= 0; i-- { 173 mut = mc.hooks[i](mut) 174 } 175 if _, err := mut.Mutate(ctx, mc.mutation); err != nil { 176 return nil, err 177 } 178 } 179 return node, err 180 } 181 182 // SaveX calls Save and panics if Save returns an error. 183 func (mc *MachineCreate) SaveX(ctx context.Context) *Machine { 184 v, err := mc.Save(ctx) 185 if err != nil { 186 panic(err) 187 } 188 return v 189 } 190 191 // defaults sets the default values of the builder before save. 192 func (mc *MachineCreate) defaults() { 193 if _, ok := mc.mutation.CreatedAt(); !ok { 194 v := machine.DefaultCreatedAt() 195 mc.mutation.SetCreatedAt(v) 196 } 197 if _, ok := mc.mutation.UpdatedAt(); !ok { 198 v := machine.DefaultUpdatedAt() 199 mc.mutation.SetUpdatedAt(v) 200 } 201 if _, ok := mc.mutation.IsValidated(); !ok { 202 v := machine.DefaultIsValidated 203 mc.mutation.SetIsValidated(v) 204 } 205 } 206 207 // check runs all checks and user-defined validators on the builder. 208 func (mc *MachineCreate) check() error { 209 if _, ok := mc.mutation.CreatedAt(); !ok { 210 return &ValidationError{Name: "created_at", err: errors.New("ent: missing required field \"created_at\"")} 211 } 212 if _, ok := mc.mutation.UpdatedAt(); !ok { 213 return &ValidationError{Name: "updated_at", err: errors.New("ent: missing required field \"updated_at\"")} 214 } 215 if _, ok := mc.mutation.MachineId(); !ok { 216 return &ValidationError{Name: "machineId", err: errors.New("ent: missing required field \"machineId\"")} 217 } 218 if _, ok := mc.mutation.Password(); !ok { 219 return &ValidationError{Name: "password", err: errors.New("ent: missing required field \"password\"")} 220 } 221 if _, ok := mc.mutation.IpAddress(); !ok { 222 return &ValidationError{Name: "ipAddress", err: errors.New("ent: missing required field \"ipAddress\"")} 223 } 224 if v, ok := mc.mutation.Scenarios(); ok { 225 if err := machine.ScenariosValidator(v); err != nil { 226 return &ValidationError{Name: "scenarios", err: fmt.Errorf("ent: validator failed for field \"scenarios\": %w", err)} 227 } 228 } 229 if _, ok := mc.mutation.IsValidated(); !ok { 230 return &ValidationError{Name: "isValidated", err: errors.New("ent: missing required field \"isValidated\"")} 231 } 232 return nil 233 } 234 235 func (mc *MachineCreate) sqlSave(ctx context.Context) (*Machine, error) { 236 _node, _spec := mc.createSpec() 237 if err := sqlgraph.CreateNode(ctx, mc.driver, _spec); err != nil { 238 if cerr, ok := isSQLConstraintError(err); ok { 239 err = cerr 240 } 241 return nil, err 242 } 243 id := _spec.ID.Value.(int64) 244 _node.ID = int(id) 245 return _node, nil 246 } 247 248 func (mc *MachineCreate) createSpec() (*Machine, *sqlgraph.CreateSpec) { 249 var ( 250 _node = &Machine{config: mc.config} 251 _spec = &sqlgraph.CreateSpec{ 252 Table: machine.Table, 253 ID: &sqlgraph.FieldSpec{ 254 Type: field.TypeInt, 255 Column: machine.FieldID, 256 }, 257 } 258 ) 259 if value, ok := mc.mutation.CreatedAt(); ok { 260 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 261 Type: field.TypeTime, 262 Value: value, 263 Column: machine.FieldCreatedAt, 264 }) 265 _node.CreatedAt = value 266 } 267 if value, ok := mc.mutation.UpdatedAt(); ok { 268 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 269 Type: field.TypeTime, 270 Value: value, 271 Column: machine.FieldUpdatedAt, 272 }) 273 _node.UpdatedAt = value 274 } 275 if value, ok := mc.mutation.MachineId(); ok { 276 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 277 Type: field.TypeString, 278 Value: value, 279 Column: machine.FieldMachineId, 280 }) 281 _node.MachineId = value 282 } 283 if value, ok := mc.mutation.Password(); ok { 284 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 285 Type: field.TypeString, 286 Value: value, 287 Column: machine.FieldPassword, 288 }) 289 _node.Password = value 290 } 291 if value, ok := mc.mutation.IpAddress(); ok { 292 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 293 Type: field.TypeString, 294 Value: value, 295 Column: machine.FieldIpAddress, 296 }) 297 _node.IpAddress = value 298 } 299 if value, ok := mc.mutation.Scenarios(); ok { 300 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 301 Type: field.TypeString, 302 Value: value, 303 Column: machine.FieldScenarios, 304 }) 305 _node.Scenarios = value 306 } 307 if value, ok := mc.mutation.Version(); ok { 308 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 309 Type: field.TypeString, 310 Value: value, 311 Column: machine.FieldVersion, 312 }) 313 _node.Version = value 314 } 315 if value, ok := mc.mutation.IsValidated(); ok { 316 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 317 Type: field.TypeBool, 318 Value: value, 319 Column: machine.FieldIsValidated, 320 }) 321 _node.IsValidated = value 322 } 323 if value, ok := mc.mutation.Status(); ok { 324 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 325 Type: field.TypeString, 326 Value: value, 327 Column: machine.FieldStatus, 328 }) 329 _node.Status = value 330 } 331 if nodes := mc.mutation.AlertsIDs(); len(nodes) > 0 { 332 edge := &sqlgraph.EdgeSpec{ 333 Rel: sqlgraph.O2M, 334 Inverse: false, 335 Table: machine.AlertsTable, 336 Columns: []string{machine.AlertsColumn}, 337 Bidi: false, 338 Target: &sqlgraph.EdgeTarget{ 339 IDSpec: &sqlgraph.FieldSpec{ 340 Type: field.TypeInt, 341 Column: alert.FieldID, 342 }, 343 }, 344 } 345 for _, k := range nodes { 346 edge.Target.Nodes = append(edge.Target.Nodes, k) 347 } 348 _spec.Edges = append(_spec.Edges, edge) 349 } 350 return _node, _spec 351 } 352 353 // MachineCreateBulk is the builder for creating many Machine entities in bulk. 354 type MachineCreateBulk struct { 355 config 356 builders []*MachineCreate 357 } 358 359 // Save creates the Machine entities in the database. 360 func (mcb *MachineCreateBulk) Save(ctx context.Context) ([]*Machine, error) { 361 specs := make([]*sqlgraph.CreateSpec, len(mcb.builders)) 362 nodes := make([]*Machine, len(mcb.builders)) 363 mutators := make([]Mutator, len(mcb.builders)) 364 for i := range mcb.builders { 365 func(i int, root context.Context) { 366 builder := mcb.builders[i] 367 builder.defaults() 368 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 369 mutation, ok := m.(*MachineMutation) 370 if !ok { 371 return nil, fmt.Errorf("unexpected mutation type %T", m) 372 } 373 if err := builder.check(); err != nil { 374 return nil, err 375 } 376 builder.mutation = mutation 377 nodes[i], specs[i] = builder.createSpec() 378 var err error 379 if i < len(mutators)-1 { 380 _, err = mutators[i+1].Mutate(root, mcb.builders[i+1].mutation) 381 } else { 382 // Invoke the actual operation on the latest mutation in the chain. 383 if err = sqlgraph.BatchCreate(ctx, mcb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil { 384 if cerr, ok := isSQLConstraintError(err); ok { 385 err = cerr 386 } 387 } 388 } 389 mutation.done = true 390 if err != nil { 391 return nil, err 392 } 393 id := specs[i].ID.Value.(int64) 394 nodes[i].ID = int(id) 395 return nodes[i], nil 396 }) 397 for i := len(builder.hooks) - 1; i >= 0; i-- { 398 mut = builder.hooks[i](mut) 399 } 400 mutators[i] = mut 401 }(i, ctx) 402 } 403 if len(mutators) > 0 { 404 if _, err := mutators[0].Mutate(ctx, mcb.builders[0].mutation); err != nil { 405 return nil, err 406 } 407 } 408 return nodes, nil 409 } 410 411 // SaveX is like Save, but panics if an error occurs. 412 func (mcb *MachineCreateBulk) SaveX(ctx context.Context) []*Machine { 413 v, err := mcb.Save(ctx) 414 if err != nil { 415 panic(err) 416 } 417 return v 418 }