github.com/crowdsecurity/crowdsec@v1.6.1/pkg/database/ent/configitem_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/configitem" 14 ) 15 16 // ConfigItemCreate is the builder for creating a ConfigItem entity. 17 type ConfigItemCreate struct { 18 config 19 mutation *ConfigItemMutation 20 hooks []Hook 21 } 22 23 // SetCreatedAt sets the "created_at" field. 24 func (cic *ConfigItemCreate) SetCreatedAt(t time.Time) *ConfigItemCreate { 25 cic.mutation.SetCreatedAt(t) 26 return cic 27 } 28 29 // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. 30 func (cic *ConfigItemCreate) SetNillableCreatedAt(t *time.Time) *ConfigItemCreate { 31 if t != nil { 32 cic.SetCreatedAt(*t) 33 } 34 return cic 35 } 36 37 // SetUpdatedAt sets the "updated_at" field. 38 func (cic *ConfigItemCreate) SetUpdatedAt(t time.Time) *ConfigItemCreate { 39 cic.mutation.SetUpdatedAt(t) 40 return cic 41 } 42 43 // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. 44 func (cic *ConfigItemCreate) SetNillableUpdatedAt(t *time.Time) *ConfigItemCreate { 45 if t != nil { 46 cic.SetUpdatedAt(*t) 47 } 48 return cic 49 } 50 51 // SetName sets the "name" field. 52 func (cic *ConfigItemCreate) SetName(s string) *ConfigItemCreate { 53 cic.mutation.SetName(s) 54 return cic 55 } 56 57 // SetValue sets the "value" field. 58 func (cic *ConfigItemCreate) SetValue(s string) *ConfigItemCreate { 59 cic.mutation.SetValue(s) 60 return cic 61 } 62 63 // Mutation returns the ConfigItemMutation object of the builder. 64 func (cic *ConfigItemCreate) Mutation() *ConfigItemMutation { 65 return cic.mutation 66 } 67 68 // Save creates the ConfigItem in the database. 69 func (cic *ConfigItemCreate) Save(ctx context.Context) (*ConfigItem, error) { 70 cic.defaults() 71 return withHooks(ctx, cic.sqlSave, cic.mutation, cic.hooks) 72 } 73 74 // SaveX calls Save and panics if Save returns an error. 75 func (cic *ConfigItemCreate) SaveX(ctx context.Context) *ConfigItem { 76 v, err := cic.Save(ctx) 77 if err != nil { 78 panic(err) 79 } 80 return v 81 } 82 83 // Exec executes the query. 84 func (cic *ConfigItemCreate) Exec(ctx context.Context) error { 85 _, err := cic.Save(ctx) 86 return err 87 } 88 89 // ExecX is like Exec, but panics if an error occurs. 90 func (cic *ConfigItemCreate) ExecX(ctx context.Context) { 91 if err := cic.Exec(ctx); err != nil { 92 panic(err) 93 } 94 } 95 96 // defaults sets the default values of the builder before save. 97 func (cic *ConfigItemCreate) defaults() { 98 if _, ok := cic.mutation.CreatedAt(); !ok { 99 v := configitem.DefaultCreatedAt() 100 cic.mutation.SetCreatedAt(v) 101 } 102 if _, ok := cic.mutation.UpdatedAt(); !ok { 103 v := configitem.DefaultUpdatedAt() 104 cic.mutation.SetUpdatedAt(v) 105 } 106 } 107 108 // check runs all checks and user-defined validators on the builder. 109 func (cic *ConfigItemCreate) check() error { 110 if _, ok := cic.mutation.Name(); !ok { 111 return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "ConfigItem.name"`)} 112 } 113 if _, ok := cic.mutation.Value(); !ok { 114 return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "ConfigItem.value"`)} 115 } 116 return nil 117 } 118 119 func (cic *ConfigItemCreate) sqlSave(ctx context.Context) (*ConfigItem, error) { 120 if err := cic.check(); err != nil { 121 return nil, err 122 } 123 _node, _spec := cic.createSpec() 124 if err := sqlgraph.CreateNode(ctx, cic.driver, _spec); err != nil { 125 if sqlgraph.IsConstraintError(err) { 126 err = &ConstraintError{msg: err.Error(), wrap: err} 127 } 128 return nil, err 129 } 130 id := _spec.ID.Value.(int64) 131 _node.ID = int(id) 132 cic.mutation.id = &_node.ID 133 cic.mutation.done = true 134 return _node, nil 135 } 136 137 func (cic *ConfigItemCreate) createSpec() (*ConfigItem, *sqlgraph.CreateSpec) { 138 var ( 139 _node = &ConfigItem{config: cic.config} 140 _spec = sqlgraph.NewCreateSpec(configitem.Table, sqlgraph.NewFieldSpec(configitem.FieldID, field.TypeInt)) 141 ) 142 if value, ok := cic.mutation.CreatedAt(); ok { 143 _spec.SetField(configitem.FieldCreatedAt, field.TypeTime, value) 144 _node.CreatedAt = &value 145 } 146 if value, ok := cic.mutation.UpdatedAt(); ok { 147 _spec.SetField(configitem.FieldUpdatedAt, field.TypeTime, value) 148 _node.UpdatedAt = &value 149 } 150 if value, ok := cic.mutation.Name(); ok { 151 _spec.SetField(configitem.FieldName, field.TypeString, value) 152 _node.Name = value 153 } 154 if value, ok := cic.mutation.Value(); ok { 155 _spec.SetField(configitem.FieldValue, field.TypeString, value) 156 _node.Value = value 157 } 158 return _node, _spec 159 } 160 161 // ConfigItemCreateBulk is the builder for creating many ConfigItem entities in bulk. 162 type ConfigItemCreateBulk struct { 163 config 164 err error 165 builders []*ConfigItemCreate 166 } 167 168 // Save creates the ConfigItem entities in the database. 169 func (cicb *ConfigItemCreateBulk) Save(ctx context.Context) ([]*ConfigItem, error) { 170 if cicb.err != nil { 171 return nil, cicb.err 172 } 173 specs := make([]*sqlgraph.CreateSpec, len(cicb.builders)) 174 nodes := make([]*ConfigItem, len(cicb.builders)) 175 mutators := make([]Mutator, len(cicb.builders)) 176 for i := range cicb.builders { 177 func(i int, root context.Context) { 178 builder := cicb.builders[i] 179 builder.defaults() 180 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 181 mutation, ok := m.(*ConfigItemMutation) 182 if !ok { 183 return nil, fmt.Errorf("unexpected mutation type %T", m) 184 } 185 if err := builder.check(); err != nil { 186 return nil, err 187 } 188 builder.mutation = mutation 189 var err error 190 nodes[i], specs[i] = builder.createSpec() 191 if i < len(mutators)-1 { 192 _, err = mutators[i+1].Mutate(root, cicb.builders[i+1].mutation) 193 } else { 194 spec := &sqlgraph.BatchCreateSpec{Nodes: specs} 195 // Invoke the actual operation on the latest mutation in the chain. 196 if err = sqlgraph.BatchCreate(ctx, cicb.driver, spec); err != nil { 197 if sqlgraph.IsConstraintError(err) { 198 err = &ConstraintError{msg: err.Error(), wrap: err} 199 } 200 } 201 } 202 if err != nil { 203 return nil, err 204 } 205 mutation.id = &nodes[i].ID 206 if specs[i].ID.Value != nil { 207 id := specs[i].ID.Value.(int64) 208 nodes[i].ID = int(id) 209 } 210 mutation.done = true 211 return nodes[i], nil 212 }) 213 for i := len(builder.hooks) - 1; i >= 0; i-- { 214 mut = builder.hooks[i](mut) 215 } 216 mutators[i] = mut 217 }(i, ctx) 218 } 219 if len(mutators) > 0 { 220 if _, err := mutators[0].Mutate(ctx, cicb.builders[0].mutation); err != nil { 221 return nil, err 222 } 223 } 224 return nodes, nil 225 } 226 227 // SaveX is like Save, but panics if an error occurs. 228 func (cicb *ConfigItemCreateBulk) SaveX(ctx context.Context) []*ConfigItem { 229 v, err := cicb.Save(ctx) 230 if err != nil { 231 panic(err) 232 } 233 return v 234 } 235 236 // Exec executes the query. 237 func (cicb *ConfigItemCreateBulk) Exec(ctx context.Context) error { 238 _, err := cicb.Save(ctx) 239 return err 240 } 241 242 // ExecX is like Exec, but panics if an error occurs. 243 func (cicb *ConfigItemCreateBulk) ExecX(ctx context.Context) { 244 if err := cicb.Exec(ctx); err != nil { 245 panic(err) 246 } 247 }