github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/bouncer_create.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/sqlgraph"
    12  	"entgo.io/ent/schema/field"
    13  	"github.com/crowdsecurity/crowdsec/pkg/database/ent/bouncer"
    14  )
    15  
    16  // BouncerCreate is the builder for creating a Bouncer entity.
    17  type BouncerCreate struct {
    18  	config
    19  	mutation *BouncerMutation
    20  	hooks    []Hook
    21  }
    22  
    23  // SetCreatedAt sets the "created_at" field.
    24  func (bc *BouncerCreate) SetCreatedAt(t time.Time) *BouncerCreate {
    25  	bc.mutation.SetCreatedAt(t)
    26  	return bc
    27  }
    28  
    29  // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
    30  func (bc *BouncerCreate) SetNillableCreatedAt(t *time.Time) *BouncerCreate {
    31  	if t != nil {
    32  		bc.SetCreatedAt(*t)
    33  	}
    34  	return bc
    35  }
    36  
    37  // SetUpdatedAt sets the "updated_at" field.
    38  func (bc *BouncerCreate) SetUpdatedAt(t time.Time) *BouncerCreate {
    39  	bc.mutation.SetUpdatedAt(t)
    40  	return bc
    41  }
    42  
    43  // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
    44  func (bc *BouncerCreate) SetNillableUpdatedAt(t *time.Time) *BouncerCreate {
    45  	if t != nil {
    46  		bc.SetUpdatedAt(*t)
    47  	}
    48  	return bc
    49  }
    50  
    51  // SetName sets the "name" field.
    52  func (bc *BouncerCreate) SetName(s string) *BouncerCreate {
    53  	bc.mutation.SetName(s)
    54  	return bc
    55  }
    56  
    57  // SetAPIKey sets the "api_key" field.
    58  func (bc *BouncerCreate) SetAPIKey(s string) *BouncerCreate {
    59  	bc.mutation.SetAPIKey(s)
    60  	return bc
    61  }
    62  
    63  // SetRevoked sets the "revoked" field.
    64  func (bc *BouncerCreate) SetRevoked(b bool) *BouncerCreate {
    65  	bc.mutation.SetRevoked(b)
    66  	return bc
    67  }
    68  
    69  // SetIPAddress sets the "ip_address" field.
    70  func (bc *BouncerCreate) SetIPAddress(s string) *BouncerCreate {
    71  	bc.mutation.SetIPAddress(s)
    72  	return bc
    73  }
    74  
    75  // SetNillableIPAddress sets the "ip_address" field if the given value is not nil.
    76  func (bc *BouncerCreate) SetNillableIPAddress(s *string) *BouncerCreate {
    77  	if s != nil {
    78  		bc.SetIPAddress(*s)
    79  	}
    80  	return bc
    81  }
    82  
    83  // SetType sets the "type" field.
    84  func (bc *BouncerCreate) SetType(s string) *BouncerCreate {
    85  	bc.mutation.SetType(s)
    86  	return bc
    87  }
    88  
    89  // SetNillableType sets the "type" field if the given value is not nil.
    90  func (bc *BouncerCreate) SetNillableType(s *string) *BouncerCreate {
    91  	if s != nil {
    92  		bc.SetType(*s)
    93  	}
    94  	return bc
    95  }
    96  
    97  // SetVersion sets the "version" field.
    98  func (bc *BouncerCreate) SetVersion(s string) *BouncerCreate {
    99  	bc.mutation.SetVersion(s)
   100  	return bc
   101  }
   102  
   103  // SetNillableVersion sets the "version" field if the given value is not nil.
   104  func (bc *BouncerCreate) SetNillableVersion(s *string) *BouncerCreate {
   105  	if s != nil {
   106  		bc.SetVersion(*s)
   107  	}
   108  	return bc
   109  }
   110  
   111  // SetUntil sets the "until" field.
   112  func (bc *BouncerCreate) SetUntil(t time.Time) *BouncerCreate {
   113  	bc.mutation.SetUntil(t)
   114  	return bc
   115  }
   116  
   117  // SetNillableUntil sets the "until" field if the given value is not nil.
   118  func (bc *BouncerCreate) SetNillableUntil(t *time.Time) *BouncerCreate {
   119  	if t != nil {
   120  		bc.SetUntil(*t)
   121  	}
   122  	return bc
   123  }
   124  
   125  // SetLastPull sets the "last_pull" field.
   126  func (bc *BouncerCreate) SetLastPull(t time.Time) *BouncerCreate {
   127  	bc.mutation.SetLastPull(t)
   128  	return bc
   129  }
   130  
   131  // SetNillableLastPull sets the "last_pull" field if the given value is not nil.
   132  func (bc *BouncerCreate) SetNillableLastPull(t *time.Time) *BouncerCreate {
   133  	if t != nil {
   134  		bc.SetLastPull(*t)
   135  	}
   136  	return bc
   137  }
   138  
   139  // SetAuthType sets the "auth_type" field.
   140  func (bc *BouncerCreate) SetAuthType(s string) *BouncerCreate {
   141  	bc.mutation.SetAuthType(s)
   142  	return bc
   143  }
   144  
   145  // SetNillableAuthType sets the "auth_type" field if the given value is not nil.
   146  func (bc *BouncerCreate) SetNillableAuthType(s *string) *BouncerCreate {
   147  	if s != nil {
   148  		bc.SetAuthType(*s)
   149  	}
   150  	return bc
   151  }
   152  
   153  // Mutation returns the BouncerMutation object of the builder.
   154  func (bc *BouncerCreate) Mutation() *BouncerMutation {
   155  	return bc.mutation
   156  }
   157  
   158  // Save creates the Bouncer in the database.
   159  func (bc *BouncerCreate) Save(ctx context.Context) (*Bouncer, error) {
   160  	bc.defaults()
   161  	return withHooks(ctx, bc.sqlSave, bc.mutation, bc.hooks)
   162  }
   163  
   164  // SaveX calls Save and panics if Save returns an error.
   165  func (bc *BouncerCreate) SaveX(ctx context.Context) *Bouncer {
   166  	v, err := bc.Save(ctx)
   167  	if err != nil {
   168  		panic(err)
   169  	}
   170  	return v
   171  }
   172  
   173  // Exec executes the query.
   174  func (bc *BouncerCreate) Exec(ctx context.Context) error {
   175  	_, err := bc.Save(ctx)
   176  	return err
   177  }
   178  
   179  // ExecX is like Exec, but panics if an error occurs.
   180  func (bc *BouncerCreate) ExecX(ctx context.Context) {
   181  	if err := bc.Exec(ctx); err != nil {
   182  		panic(err)
   183  	}
   184  }
   185  
   186  // defaults sets the default values of the builder before save.
   187  func (bc *BouncerCreate) defaults() {
   188  	if _, ok := bc.mutation.CreatedAt(); !ok {
   189  		v := bouncer.DefaultCreatedAt()
   190  		bc.mutation.SetCreatedAt(v)
   191  	}
   192  	if _, ok := bc.mutation.UpdatedAt(); !ok {
   193  		v := bouncer.DefaultUpdatedAt()
   194  		bc.mutation.SetUpdatedAt(v)
   195  	}
   196  	if _, ok := bc.mutation.IPAddress(); !ok {
   197  		v := bouncer.DefaultIPAddress
   198  		bc.mutation.SetIPAddress(v)
   199  	}
   200  	if _, ok := bc.mutation.Until(); !ok {
   201  		v := bouncer.DefaultUntil()
   202  		bc.mutation.SetUntil(v)
   203  	}
   204  	if _, ok := bc.mutation.LastPull(); !ok {
   205  		v := bouncer.DefaultLastPull()
   206  		bc.mutation.SetLastPull(v)
   207  	}
   208  	if _, ok := bc.mutation.AuthType(); !ok {
   209  		v := bouncer.DefaultAuthType
   210  		bc.mutation.SetAuthType(v)
   211  	}
   212  }
   213  
   214  // check runs all checks and user-defined validators on the builder.
   215  func (bc *BouncerCreate) check() error {
   216  	if _, ok := bc.mutation.Name(); !ok {
   217  		return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Bouncer.name"`)}
   218  	}
   219  	if _, ok := bc.mutation.APIKey(); !ok {
   220  		return &ValidationError{Name: "api_key", err: errors.New(`ent: missing required field "Bouncer.api_key"`)}
   221  	}
   222  	if _, ok := bc.mutation.Revoked(); !ok {
   223  		return &ValidationError{Name: "revoked", err: errors.New(`ent: missing required field "Bouncer.revoked"`)}
   224  	}
   225  	if _, ok := bc.mutation.LastPull(); !ok {
   226  		return &ValidationError{Name: "last_pull", err: errors.New(`ent: missing required field "Bouncer.last_pull"`)}
   227  	}
   228  	if _, ok := bc.mutation.AuthType(); !ok {
   229  		return &ValidationError{Name: "auth_type", err: errors.New(`ent: missing required field "Bouncer.auth_type"`)}
   230  	}
   231  	return nil
   232  }
   233  
   234  func (bc *BouncerCreate) sqlSave(ctx context.Context) (*Bouncer, error) {
   235  	if err := bc.check(); err != nil {
   236  		return nil, err
   237  	}
   238  	_node, _spec := bc.createSpec()
   239  	if err := sqlgraph.CreateNode(ctx, bc.driver, _spec); err != nil {
   240  		if sqlgraph.IsConstraintError(err) {
   241  			err = &ConstraintError{msg: err.Error(), wrap: err}
   242  		}
   243  		return nil, err
   244  	}
   245  	id := _spec.ID.Value.(int64)
   246  	_node.ID = int(id)
   247  	bc.mutation.id = &_node.ID
   248  	bc.mutation.done = true
   249  	return _node, nil
   250  }
   251  
   252  func (bc *BouncerCreate) createSpec() (*Bouncer, *sqlgraph.CreateSpec) {
   253  	var (
   254  		_node = &Bouncer{config: bc.config}
   255  		_spec = sqlgraph.NewCreateSpec(bouncer.Table, sqlgraph.NewFieldSpec(bouncer.FieldID, field.TypeInt))
   256  	)
   257  	if value, ok := bc.mutation.CreatedAt(); ok {
   258  		_spec.SetField(bouncer.FieldCreatedAt, field.TypeTime, value)
   259  		_node.CreatedAt = &value
   260  	}
   261  	if value, ok := bc.mutation.UpdatedAt(); ok {
   262  		_spec.SetField(bouncer.FieldUpdatedAt, field.TypeTime, value)
   263  		_node.UpdatedAt = &value
   264  	}
   265  	if value, ok := bc.mutation.Name(); ok {
   266  		_spec.SetField(bouncer.FieldName, field.TypeString, value)
   267  		_node.Name = value
   268  	}
   269  	if value, ok := bc.mutation.APIKey(); ok {
   270  		_spec.SetField(bouncer.FieldAPIKey, field.TypeString, value)
   271  		_node.APIKey = value
   272  	}
   273  	if value, ok := bc.mutation.Revoked(); ok {
   274  		_spec.SetField(bouncer.FieldRevoked, field.TypeBool, value)
   275  		_node.Revoked = value
   276  	}
   277  	if value, ok := bc.mutation.IPAddress(); ok {
   278  		_spec.SetField(bouncer.FieldIPAddress, field.TypeString, value)
   279  		_node.IPAddress = value
   280  	}
   281  	if value, ok := bc.mutation.GetType(); ok {
   282  		_spec.SetField(bouncer.FieldType, field.TypeString, value)
   283  		_node.Type = value
   284  	}
   285  	if value, ok := bc.mutation.Version(); ok {
   286  		_spec.SetField(bouncer.FieldVersion, field.TypeString, value)
   287  		_node.Version = value
   288  	}
   289  	if value, ok := bc.mutation.Until(); ok {
   290  		_spec.SetField(bouncer.FieldUntil, field.TypeTime, value)
   291  		_node.Until = value
   292  	}
   293  	if value, ok := bc.mutation.LastPull(); ok {
   294  		_spec.SetField(bouncer.FieldLastPull, field.TypeTime, value)
   295  		_node.LastPull = value
   296  	}
   297  	if value, ok := bc.mutation.AuthType(); ok {
   298  		_spec.SetField(bouncer.FieldAuthType, field.TypeString, value)
   299  		_node.AuthType = value
   300  	}
   301  	return _node, _spec
   302  }
   303  
   304  // BouncerCreateBulk is the builder for creating many Bouncer entities in bulk.
   305  type BouncerCreateBulk struct {
   306  	config
   307  	err      error
   308  	builders []*BouncerCreate
   309  }
   310  
   311  // Save creates the Bouncer entities in the database.
   312  func (bcb *BouncerCreateBulk) Save(ctx context.Context) ([]*Bouncer, error) {
   313  	if bcb.err != nil {
   314  		return nil, bcb.err
   315  	}
   316  	specs := make([]*sqlgraph.CreateSpec, len(bcb.builders))
   317  	nodes := make([]*Bouncer, len(bcb.builders))
   318  	mutators := make([]Mutator, len(bcb.builders))
   319  	for i := range bcb.builders {
   320  		func(i int, root context.Context) {
   321  			builder := bcb.builders[i]
   322  			builder.defaults()
   323  			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
   324  				mutation, ok := m.(*BouncerMutation)
   325  				if !ok {
   326  					return nil, fmt.Errorf("unexpected mutation type %T", m)
   327  				}
   328  				if err := builder.check(); err != nil {
   329  					return nil, err
   330  				}
   331  				builder.mutation = mutation
   332  				var err error
   333  				nodes[i], specs[i] = builder.createSpec()
   334  				if i < len(mutators)-1 {
   335  					_, err = mutators[i+1].Mutate(root, bcb.builders[i+1].mutation)
   336  				} else {
   337  					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
   338  					// Invoke the actual operation on the latest mutation in the chain.
   339  					if err = sqlgraph.BatchCreate(ctx, bcb.driver, spec); err != nil {
   340  						if sqlgraph.IsConstraintError(err) {
   341  							err = &ConstraintError{msg: err.Error(), wrap: err}
   342  						}
   343  					}
   344  				}
   345  				if err != nil {
   346  					return nil, err
   347  				}
   348  				mutation.id = &nodes[i].ID
   349  				if specs[i].ID.Value != nil {
   350  					id := specs[i].ID.Value.(int64)
   351  					nodes[i].ID = int(id)
   352  				}
   353  				mutation.done = true
   354  				return nodes[i], nil
   355  			})
   356  			for i := len(builder.hooks) - 1; i >= 0; i-- {
   357  				mut = builder.hooks[i](mut)
   358  			}
   359  			mutators[i] = mut
   360  		}(i, ctx)
   361  	}
   362  	if len(mutators) > 0 {
   363  		if _, err := mutators[0].Mutate(ctx, bcb.builders[0].mutation); err != nil {
   364  			return nil, err
   365  		}
   366  	}
   367  	return nodes, nil
   368  }
   369  
   370  // SaveX is like Save, but panics if an error occurs.
   371  func (bcb *BouncerCreateBulk) SaveX(ctx context.Context) []*Bouncer {
   372  	v, err := bcb.Save(ctx)
   373  	if err != nil {
   374  		panic(err)
   375  	}
   376  	return v
   377  }
   378  
   379  // Exec executes the query.
   380  func (bcb *BouncerCreateBulk) Exec(ctx context.Context) error {
   381  	_, err := bcb.Save(ctx)
   382  	return err
   383  }
   384  
   385  // ExecX is like Exec, but panics if an error occurs.
   386  func (bcb *BouncerCreateBulk) ExecX(ctx context.Context) {
   387  	if err := bcb.Exec(ctx); err != nil {
   388  		panic(err)
   389  	}
   390  }