bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/database/ent/decision_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/decision"
    15  )
    16  
    17  // DecisionCreate is the builder for creating a Decision entity.
    18  type DecisionCreate struct {
    19  	config
    20  	mutation *DecisionMutation
    21  	hooks    []Hook
    22  }
    23  
    24  // SetCreatedAt sets the "created_at" field.
    25  func (dc *DecisionCreate) SetCreatedAt(t time.Time) *DecisionCreate {
    26  	dc.mutation.SetCreatedAt(t)
    27  	return dc
    28  }
    29  
    30  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
    31  func (dc *DecisionCreate) SetNillableCreatedAt(t *time.Time) *DecisionCreate {
    32  	if t != nil {
    33  		dc.SetCreatedAt(*t)
    34  	}
    35  	return dc
    36  }
    37  
    38  // SetUpdatedAt sets the "updated_at" field.
    39  func (dc *DecisionCreate) SetUpdatedAt(t time.Time) *DecisionCreate {
    40  	dc.mutation.SetUpdatedAt(t)
    41  	return dc
    42  }
    43  
    44  // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
    45  func (dc *DecisionCreate) SetNillableUpdatedAt(t *time.Time) *DecisionCreate {
    46  	if t != nil {
    47  		dc.SetUpdatedAt(*t)
    48  	}
    49  	return dc
    50  }
    51  
    52  // SetUntil sets the "until" field.
    53  func (dc *DecisionCreate) SetUntil(t time.Time) *DecisionCreate {
    54  	dc.mutation.SetUntil(t)
    55  	return dc
    56  }
    57  
    58  // SetScenario sets the "scenario" field.
    59  func (dc *DecisionCreate) SetScenario(s string) *DecisionCreate {
    60  	dc.mutation.SetScenario(s)
    61  	return dc
    62  }
    63  
    64  // SetType sets the "type" field.
    65  func (dc *DecisionCreate) SetType(s string) *DecisionCreate {
    66  	dc.mutation.SetType(s)
    67  	return dc
    68  }
    69  
    70  // SetStartIP sets the "start_ip" field.
    71  func (dc *DecisionCreate) SetStartIP(i int64) *DecisionCreate {
    72  	dc.mutation.SetStartIP(i)
    73  	return dc
    74  }
    75  
    76  // SetNillableStartIP sets the "start_ip" field if the given value is not nil.
    77  func (dc *DecisionCreate) SetNillableStartIP(i *int64) *DecisionCreate {
    78  	if i != nil {
    79  		dc.SetStartIP(*i)
    80  	}
    81  	return dc
    82  }
    83  
    84  // SetEndIP sets the "end_ip" field.
    85  func (dc *DecisionCreate) SetEndIP(i int64) *DecisionCreate {
    86  	dc.mutation.SetEndIP(i)
    87  	return dc
    88  }
    89  
    90  // SetNillableEndIP sets the "end_ip" field if the given value is not nil.
    91  func (dc *DecisionCreate) SetNillableEndIP(i *int64) *DecisionCreate {
    92  	if i != nil {
    93  		dc.SetEndIP(*i)
    94  	}
    95  	return dc
    96  }
    97  
    98  // SetStartSuffix sets the "start_suffix" field.
    99  func (dc *DecisionCreate) SetStartSuffix(i int64) *DecisionCreate {
   100  	dc.mutation.SetStartSuffix(i)
   101  	return dc
   102  }
   103  
   104  // SetNillableStartSuffix sets the "start_suffix" field if the given value is not nil.
   105  func (dc *DecisionCreate) SetNillableStartSuffix(i *int64) *DecisionCreate {
   106  	if i != nil {
   107  		dc.SetStartSuffix(*i)
   108  	}
   109  	return dc
   110  }
   111  
   112  // SetEndSuffix sets the "end_suffix" field.
   113  func (dc *DecisionCreate) SetEndSuffix(i int64) *DecisionCreate {
   114  	dc.mutation.SetEndSuffix(i)
   115  	return dc
   116  }
   117  
   118  // SetNillableEndSuffix sets the "end_suffix" field if the given value is not nil.
   119  func (dc *DecisionCreate) SetNillableEndSuffix(i *int64) *DecisionCreate {
   120  	if i != nil {
   121  		dc.SetEndSuffix(*i)
   122  	}
   123  	return dc
   124  }
   125  
   126  // SetIPSize sets the "ip_size" field.
   127  func (dc *DecisionCreate) SetIPSize(i int64) *DecisionCreate {
   128  	dc.mutation.SetIPSize(i)
   129  	return dc
   130  }
   131  
   132  // SetNillableIPSize sets the "ip_size" field if the given value is not nil.
   133  func (dc *DecisionCreate) SetNillableIPSize(i *int64) *DecisionCreate {
   134  	if i != nil {
   135  		dc.SetIPSize(*i)
   136  	}
   137  	return dc
   138  }
   139  
   140  // SetScope sets the "scope" field.
   141  func (dc *DecisionCreate) SetScope(s string) *DecisionCreate {
   142  	dc.mutation.SetScope(s)
   143  	return dc
   144  }
   145  
   146  // SetValue sets the "value" field.
   147  func (dc *DecisionCreate) SetValue(s string) *DecisionCreate {
   148  	dc.mutation.SetValue(s)
   149  	return dc
   150  }
   151  
   152  // SetOrigin sets the "origin" field.
   153  func (dc *DecisionCreate) SetOrigin(s string) *DecisionCreate {
   154  	dc.mutation.SetOrigin(s)
   155  	return dc
   156  }
   157  
   158  // SetSimulated sets the "simulated" field.
   159  func (dc *DecisionCreate) SetSimulated(b bool) *DecisionCreate {
   160  	dc.mutation.SetSimulated(b)
   161  	return dc
   162  }
   163  
   164  // SetNillableSimulated sets the "simulated" field if the given value is not nil.
   165  func (dc *DecisionCreate) SetNillableSimulated(b *bool) *DecisionCreate {
   166  	if b != nil {
   167  		dc.SetSimulated(*b)
   168  	}
   169  	return dc
   170  }
   171  
   172  // SetOwnerID sets the "owner" edge to the Alert entity by ID.
   173  func (dc *DecisionCreate) SetOwnerID(id int) *DecisionCreate {
   174  	dc.mutation.SetOwnerID(id)
   175  	return dc
   176  }
   177  
   178  // SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil.
   179  func (dc *DecisionCreate) SetNillableOwnerID(id *int) *DecisionCreate {
   180  	if id != nil {
   181  		dc = dc.SetOwnerID(*id)
   182  	}
   183  	return dc
   184  }
   185  
   186  // SetOwner sets the "owner" edge to the Alert entity.
   187  func (dc *DecisionCreate) SetOwner(a *Alert) *DecisionCreate {
   188  	return dc.SetOwnerID(a.ID)
   189  }
   190  
   191  // Mutation returns the DecisionMutation object of the builder.
   192  func (dc *DecisionCreate) Mutation() *DecisionMutation {
   193  	return dc.mutation
   194  }
   195  
   196  // Save creates the Decision in the database.
   197  func (dc *DecisionCreate) Save(ctx context.Context) (*Decision, error) {
   198  	var (
   199  		err  error
   200  		node *Decision
   201  	)
   202  	dc.defaults()
   203  	if len(dc.hooks) == 0 {
   204  		if err = dc.check(); err != nil {
   205  			return nil, err
   206  		}
   207  		node, err = dc.sqlSave(ctx)
   208  	} else {
   209  		var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   210  			mutation, ok := m.(*DecisionMutation)
   211  			if !ok {
   212  				return nil, fmt.Errorf("unexpected mutation type %T", m)
   213  			}
   214  			if err = dc.check(); err != nil {
   215  				return nil, err
   216  			}
   217  			dc.mutation = mutation
   218  			node, err = dc.sqlSave(ctx)
   219  			mutation.done = true
   220  			return node, err
   221  		})
   222  		for i := len(dc.hooks) - 1; i >= 0; i-- {
   223  			mut = dc.hooks[i](mut)
   224  		}
   225  		if _, err := mut.Mutate(ctx, dc.mutation); err != nil {
   226  			return nil, err
   227  		}
   228  	}
   229  	return node, err
   230  }
   231  
   232  // SaveX calls Save and panics if Save returns an error.
   233  func (dc *DecisionCreate) SaveX(ctx context.Context) *Decision {
   234  	v, err := dc.Save(ctx)
   235  	if err != nil {
   236  		panic(err)
   237  	}
   238  	return v
   239  }
   240  
   241  // defaults sets the default values of the builder before save.
   242  func (dc *DecisionCreate) defaults() {
   243  	if _, ok := dc.mutation.CreatedAt(); !ok {
   244  		v := decision.DefaultCreatedAt()
   245  		dc.mutation.SetCreatedAt(v)
   246  	}
   247  	if _, ok := dc.mutation.UpdatedAt(); !ok {
   248  		v := decision.DefaultUpdatedAt()
   249  		dc.mutation.SetUpdatedAt(v)
   250  	}
   251  	if _, ok := dc.mutation.Simulated(); !ok {
   252  		v := decision.DefaultSimulated
   253  		dc.mutation.SetSimulated(v)
   254  	}
   255  }
   256  
   257  // check runs all checks and user-defined validators on the builder.
   258  func (dc *DecisionCreate) check() error {
   259  	if _, ok := dc.mutation.CreatedAt(); !ok {
   260  		return &ValidationError{Name: "created_at", err: errors.New("ent: missing required field \"created_at\"")}
   261  	}
   262  	if _, ok := dc.mutation.UpdatedAt(); !ok {
   263  		return &ValidationError{Name: "updated_at", err: errors.New("ent: missing required field \"updated_at\"")}
   264  	}
   265  	if _, ok := dc.mutation.Until(); !ok {
   266  		return &ValidationError{Name: "until", err: errors.New("ent: missing required field \"until\"")}
   267  	}
   268  	if _, ok := dc.mutation.Scenario(); !ok {
   269  		return &ValidationError{Name: "scenario", err: errors.New("ent: missing required field \"scenario\"")}
   270  	}
   271  	if _, ok := dc.mutation.GetType(); !ok {
   272  		return &ValidationError{Name: "type", err: errors.New("ent: missing required field \"type\"")}
   273  	}
   274  	if _, ok := dc.mutation.Scope(); !ok {
   275  		return &ValidationError{Name: "scope", err: errors.New("ent: missing required field \"scope\"")}
   276  	}
   277  	if _, ok := dc.mutation.Value(); !ok {
   278  		return &ValidationError{Name: "value", err: errors.New("ent: missing required field \"value\"")}
   279  	}
   280  	if _, ok := dc.mutation.Origin(); !ok {
   281  		return &ValidationError{Name: "origin", err: errors.New("ent: missing required field \"origin\"")}
   282  	}
   283  	if _, ok := dc.mutation.Simulated(); !ok {
   284  		return &ValidationError{Name: "simulated", err: errors.New("ent: missing required field \"simulated\"")}
   285  	}
   286  	return nil
   287  }
   288  
   289  func (dc *DecisionCreate) sqlSave(ctx context.Context) (*Decision, error) {
   290  	_node, _spec := dc.createSpec()
   291  	if err := sqlgraph.CreateNode(ctx, dc.driver, _spec); err != nil {
   292  		if cerr, ok := isSQLConstraintError(err); ok {
   293  			err = cerr
   294  		}
   295  		return nil, err
   296  	}
   297  	id := _spec.ID.Value.(int64)
   298  	_node.ID = int(id)
   299  	return _node, nil
   300  }
   301  
   302  func (dc *DecisionCreate) createSpec() (*Decision, *sqlgraph.CreateSpec) {
   303  	var (
   304  		_node = &Decision{config: dc.config}
   305  		_spec = &sqlgraph.CreateSpec{
   306  			Table: decision.Table,
   307  			ID: &sqlgraph.FieldSpec{
   308  				Type:   field.TypeInt,
   309  				Column: decision.FieldID,
   310  			},
   311  		}
   312  	)
   313  	if value, ok := dc.mutation.CreatedAt(); ok {
   314  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   315  			Type:   field.TypeTime,
   316  			Value:  value,
   317  			Column: decision.FieldCreatedAt,
   318  		})
   319  		_node.CreatedAt = value
   320  	}
   321  	if value, ok := dc.mutation.UpdatedAt(); ok {
   322  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   323  			Type:   field.TypeTime,
   324  			Value:  value,
   325  			Column: decision.FieldUpdatedAt,
   326  		})
   327  		_node.UpdatedAt = value
   328  	}
   329  	if value, ok := dc.mutation.Until(); ok {
   330  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   331  			Type:   field.TypeTime,
   332  			Value:  value,
   333  			Column: decision.FieldUntil,
   334  		})
   335  		_node.Until = value
   336  	}
   337  	if value, ok := dc.mutation.Scenario(); ok {
   338  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   339  			Type:   field.TypeString,
   340  			Value:  value,
   341  			Column: decision.FieldScenario,
   342  		})
   343  		_node.Scenario = value
   344  	}
   345  	if value, ok := dc.mutation.GetType(); ok {
   346  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   347  			Type:   field.TypeString,
   348  			Value:  value,
   349  			Column: decision.FieldType,
   350  		})
   351  		_node.Type = value
   352  	}
   353  	if value, ok := dc.mutation.StartIP(); ok {
   354  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   355  			Type:   field.TypeInt64,
   356  			Value:  value,
   357  			Column: decision.FieldStartIP,
   358  		})
   359  		_node.StartIP = value
   360  	}
   361  	if value, ok := dc.mutation.EndIP(); ok {
   362  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   363  			Type:   field.TypeInt64,
   364  			Value:  value,
   365  			Column: decision.FieldEndIP,
   366  		})
   367  		_node.EndIP = value
   368  	}
   369  	if value, ok := dc.mutation.StartSuffix(); ok {
   370  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   371  			Type:   field.TypeInt64,
   372  			Value:  value,
   373  			Column: decision.FieldStartSuffix,
   374  		})
   375  		_node.StartSuffix = value
   376  	}
   377  	if value, ok := dc.mutation.EndSuffix(); ok {
   378  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   379  			Type:   field.TypeInt64,
   380  			Value:  value,
   381  			Column: decision.FieldEndSuffix,
   382  		})
   383  		_node.EndSuffix = value
   384  	}
   385  	if value, ok := dc.mutation.IPSize(); ok {
   386  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   387  			Type:   field.TypeInt64,
   388  			Value:  value,
   389  			Column: decision.FieldIPSize,
   390  		})
   391  		_node.IPSize = value
   392  	}
   393  	if value, ok := dc.mutation.Scope(); ok {
   394  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   395  			Type:   field.TypeString,
   396  			Value:  value,
   397  			Column: decision.FieldScope,
   398  		})
   399  		_node.Scope = value
   400  	}
   401  	if value, ok := dc.mutation.Value(); ok {
   402  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   403  			Type:   field.TypeString,
   404  			Value:  value,
   405  			Column: decision.FieldValue,
   406  		})
   407  		_node.Value = value
   408  	}
   409  	if value, ok := dc.mutation.Origin(); ok {
   410  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   411  			Type:   field.TypeString,
   412  			Value:  value,
   413  			Column: decision.FieldOrigin,
   414  		})
   415  		_node.Origin = value
   416  	}
   417  	if value, ok := dc.mutation.Simulated(); ok {
   418  		_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
   419  			Type:   field.TypeBool,
   420  			Value:  value,
   421  			Column: decision.FieldSimulated,
   422  		})
   423  		_node.Simulated = value
   424  	}
   425  	if nodes := dc.mutation.OwnerIDs(); len(nodes) > 0 {
   426  		edge := &sqlgraph.EdgeSpec{
   427  			Rel:     sqlgraph.M2O,
   428  			Inverse: true,
   429  			Table:   decision.OwnerTable,
   430  			Columns: []string{decision.OwnerColumn},
   431  			Bidi:    false,
   432  			Target: &sqlgraph.EdgeTarget{
   433  				IDSpec: &sqlgraph.FieldSpec{
   434  					Type:   field.TypeInt,
   435  					Column: alert.FieldID,
   436  				},
   437  			},
   438  		}
   439  		for _, k := range nodes {
   440  			edge.Target.Nodes = append(edge.Target.Nodes, k)
   441  		}
   442  		_node.alert_decisions = &nodes[0]
   443  		_spec.Edges = append(_spec.Edges, edge)
   444  	}
   445  	return _node, _spec
   446  }
   447  
   448  // DecisionCreateBulk is the builder for creating many Decision entities in bulk.
   449  type DecisionCreateBulk struct {
   450  	config
   451  	builders []*DecisionCreate
   452  }
   453  
   454  // Save creates the Decision entities in the database.
   455  func (dcb *DecisionCreateBulk) Save(ctx context.Context) ([]*Decision, error) {
   456  	specs := make([]*sqlgraph.CreateSpec, len(dcb.builders))
   457  	nodes := make([]*Decision, len(dcb.builders))
   458  	mutators := make([]Mutator, len(dcb.builders))
   459  	for i := range dcb.builders {
   460  		func(i int, root context.Context) {
   461  			builder := dcb.builders[i]
   462  			builder.defaults()
   463  			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   464  				mutation, ok := m.(*DecisionMutation)
   465  				if !ok {
   466  					return nil, fmt.Errorf("unexpected mutation type %T", m)
   467  				}
   468  				if err := builder.check(); err != nil {
   469  					return nil, err
   470  				}
   471  				builder.mutation = mutation
   472  				nodes[i], specs[i] = builder.createSpec()
   473  				var err error
   474  				if i < len(mutators)-1 {
   475  					_, err = mutators[i+1].Mutate(root, dcb.builders[i+1].mutation)
   476  				} else {
   477  					// Invoke the actual operation on the latest mutation in the chain.
   478  					if err = sqlgraph.BatchCreate(ctx, dcb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil {
   479  						if cerr, ok := isSQLConstraintError(err); ok {
   480  							err = cerr
   481  						}
   482  					}
   483  				}
   484  				mutation.done = true
   485  				if err != nil {
   486  					return nil, err
   487  				}
   488  				id := specs[i].ID.Value.(int64)
   489  				nodes[i].ID = int(id)
   490  				return nodes[i], nil
   491  			})
   492  			for i := len(builder.hooks) - 1; i >= 0; i-- {
   493  				mut = builder.hooks[i](mut)
   494  			}
   495  			mutators[i] = mut
   496  		}(i, ctx)
   497  	}
   498  	if len(mutators) > 0 {
   499  		if _, err := mutators[0].Mutate(ctx, dcb.builders[0].mutation); err != nil {
   500  			return nil, err
   501  		}
   502  	}
   503  	return nodes, nil
   504  }
   505  
   506  // SaveX is like Save, but panics if an error occurs.
   507  func (dcb *DecisionCreateBulk) SaveX(ctx context.Context) []*Decision {
   508  	v, err := dcb.Save(ctx)
   509  	if err != nil {
   510  		panic(err)
   511  	}
   512  	return v
   513  }