bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/database/ent/event_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/event" 15 ) 16 17 // EventCreate is the builder for creating a Event entity. 18 type EventCreate struct { 19 config 20 mutation *EventMutation 21 hooks []Hook 22 } 23 24 // SetCreatedAt sets the "created_at" field. 25 func (ec *EventCreate) SetCreatedAt(t time.Time) *EventCreate { 26 ec.mutation.SetCreatedAt(t) 27 return ec 28 } 29 30 // SetNillableCreatedAt sets the "created_at" field if the given value is not nil. 31 func (ec *EventCreate) SetNillableCreatedAt(t *time.Time) *EventCreate { 32 if t != nil { 33 ec.SetCreatedAt(*t) 34 } 35 return ec 36 } 37 38 // SetUpdatedAt sets the "updated_at" field. 39 func (ec *EventCreate) SetUpdatedAt(t time.Time) *EventCreate { 40 ec.mutation.SetUpdatedAt(t) 41 return ec 42 } 43 44 // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil. 45 func (ec *EventCreate) SetNillableUpdatedAt(t *time.Time) *EventCreate { 46 if t != nil { 47 ec.SetUpdatedAt(*t) 48 } 49 return ec 50 } 51 52 // SetTime sets the "time" field. 53 func (ec *EventCreate) SetTime(t time.Time) *EventCreate { 54 ec.mutation.SetTime(t) 55 return ec 56 } 57 58 // SetSerialized sets the "serialized" field. 59 func (ec *EventCreate) SetSerialized(s string) *EventCreate { 60 ec.mutation.SetSerialized(s) 61 return ec 62 } 63 64 // SetOwnerID sets the "owner" edge to the Alert entity by ID. 65 func (ec *EventCreate) SetOwnerID(id int) *EventCreate { 66 ec.mutation.SetOwnerID(id) 67 return ec 68 } 69 70 // SetNillableOwnerID sets the "owner" edge to the Alert entity by ID if the given value is not nil. 71 func (ec *EventCreate) SetNillableOwnerID(id *int) *EventCreate { 72 if id != nil { 73 ec = ec.SetOwnerID(*id) 74 } 75 return ec 76 } 77 78 // SetOwner sets the "owner" edge to the Alert entity. 79 func (ec *EventCreate) SetOwner(a *Alert) *EventCreate { 80 return ec.SetOwnerID(a.ID) 81 } 82 83 // Mutation returns the EventMutation object of the builder. 84 func (ec *EventCreate) Mutation() *EventMutation { 85 return ec.mutation 86 } 87 88 // Save creates the Event in the database. 89 func (ec *EventCreate) Save(ctx context.Context) (*Event, error) { 90 var ( 91 err error 92 node *Event 93 ) 94 ec.defaults() 95 if len(ec.hooks) == 0 { 96 if err = ec.check(); err != nil { 97 return nil, err 98 } 99 node, err = ec.sqlSave(ctx) 100 } else { 101 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 102 mutation, ok := m.(*EventMutation) 103 if !ok { 104 return nil, fmt.Errorf("unexpected mutation type %T", m) 105 } 106 if err = ec.check(); err != nil { 107 return nil, err 108 } 109 ec.mutation = mutation 110 node, err = ec.sqlSave(ctx) 111 mutation.done = true 112 return node, err 113 }) 114 for i := len(ec.hooks) - 1; i >= 0; i-- { 115 mut = ec.hooks[i](mut) 116 } 117 if _, err := mut.Mutate(ctx, ec.mutation); err != nil { 118 return nil, err 119 } 120 } 121 return node, err 122 } 123 124 // SaveX calls Save and panics if Save returns an error. 125 func (ec *EventCreate) SaveX(ctx context.Context) *Event { 126 v, err := ec.Save(ctx) 127 if err != nil { 128 panic(err) 129 } 130 return v 131 } 132 133 // defaults sets the default values of the builder before save. 134 func (ec *EventCreate) defaults() { 135 if _, ok := ec.mutation.CreatedAt(); !ok { 136 v := event.DefaultCreatedAt() 137 ec.mutation.SetCreatedAt(v) 138 } 139 if _, ok := ec.mutation.UpdatedAt(); !ok { 140 v := event.DefaultUpdatedAt() 141 ec.mutation.SetUpdatedAt(v) 142 } 143 } 144 145 // check runs all checks and user-defined validators on the builder. 146 func (ec *EventCreate) check() error { 147 if _, ok := ec.mutation.CreatedAt(); !ok { 148 return &ValidationError{Name: "created_at", err: errors.New("ent: missing required field \"created_at\"")} 149 } 150 if _, ok := ec.mutation.UpdatedAt(); !ok { 151 return &ValidationError{Name: "updated_at", err: errors.New("ent: missing required field \"updated_at\"")} 152 } 153 if _, ok := ec.mutation.Time(); !ok { 154 return &ValidationError{Name: "time", err: errors.New("ent: missing required field \"time\"")} 155 } 156 if _, ok := ec.mutation.Serialized(); !ok { 157 return &ValidationError{Name: "serialized", err: errors.New("ent: missing required field \"serialized\"")} 158 } 159 if v, ok := ec.mutation.Serialized(); ok { 160 if err := event.SerializedValidator(v); err != nil { 161 return &ValidationError{Name: "serialized", err: fmt.Errorf("ent: validator failed for field \"serialized\": %w", err)} 162 } 163 } 164 return nil 165 } 166 167 func (ec *EventCreate) sqlSave(ctx context.Context) (*Event, error) { 168 _node, _spec := ec.createSpec() 169 if err := sqlgraph.CreateNode(ctx, ec.driver, _spec); err != nil { 170 if cerr, ok := isSQLConstraintError(err); ok { 171 err = cerr 172 } 173 return nil, err 174 } 175 id := _spec.ID.Value.(int64) 176 _node.ID = int(id) 177 return _node, nil 178 } 179 180 func (ec *EventCreate) createSpec() (*Event, *sqlgraph.CreateSpec) { 181 var ( 182 _node = &Event{config: ec.config} 183 _spec = &sqlgraph.CreateSpec{ 184 Table: event.Table, 185 ID: &sqlgraph.FieldSpec{ 186 Type: field.TypeInt, 187 Column: event.FieldID, 188 }, 189 } 190 ) 191 if value, ok := ec.mutation.CreatedAt(); ok { 192 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 193 Type: field.TypeTime, 194 Value: value, 195 Column: event.FieldCreatedAt, 196 }) 197 _node.CreatedAt = value 198 } 199 if value, ok := ec.mutation.UpdatedAt(); ok { 200 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 201 Type: field.TypeTime, 202 Value: value, 203 Column: event.FieldUpdatedAt, 204 }) 205 _node.UpdatedAt = value 206 } 207 if value, ok := ec.mutation.Time(); ok { 208 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 209 Type: field.TypeTime, 210 Value: value, 211 Column: event.FieldTime, 212 }) 213 _node.Time = value 214 } 215 if value, ok := ec.mutation.Serialized(); ok { 216 _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{ 217 Type: field.TypeString, 218 Value: value, 219 Column: event.FieldSerialized, 220 }) 221 _node.Serialized = value 222 } 223 if nodes := ec.mutation.OwnerIDs(); len(nodes) > 0 { 224 edge := &sqlgraph.EdgeSpec{ 225 Rel: sqlgraph.M2O, 226 Inverse: true, 227 Table: event.OwnerTable, 228 Columns: []string{event.OwnerColumn}, 229 Bidi: false, 230 Target: &sqlgraph.EdgeTarget{ 231 IDSpec: &sqlgraph.FieldSpec{ 232 Type: field.TypeInt, 233 Column: alert.FieldID, 234 }, 235 }, 236 } 237 for _, k := range nodes { 238 edge.Target.Nodes = append(edge.Target.Nodes, k) 239 } 240 _node.alert_events = &nodes[0] 241 _spec.Edges = append(_spec.Edges, edge) 242 } 243 return _node, _spec 244 } 245 246 // EventCreateBulk is the builder for creating many Event entities in bulk. 247 type EventCreateBulk struct { 248 config 249 builders []*EventCreate 250 } 251 252 // Save creates the Event entities in the database. 253 func (ecb *EventCreateBulk) Save(ctx context.Context) ([]*Event, error) { 254 specs := make([]*sqlgraph.CreateSpec, len(ecb.builders)) 255 nodes := make([]*Event, len(ecb.builders)) 256 mutators := make([]Mutator, len(ecb.builders)) 257 for i := range ecb.builders { 258 func(i int, root context.Context) { 259 builder := ecb.builders[i] 260 builder.defaults() 261 var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { 262 mutation, ok := m.(*EventMutation) 263 if !ok { 264 return nil, fmt.Errorf("unexpected mutation type %T", m) 265 } 266 if err := builder.check(); err != nil { 267 return nil, err 268 } 269 builder.mutation = mutation 270 nodes[i], specs[i] = builder.createSpec() 271 var err error 272 if i < len(mutators)-1 { 273 _, err = mutators[i+1].Mutate(root, ecb.builders[i+1].mutation) 274 } else { 275 // Invoke the actual operation on the latest mutation in the chain. 276 if err = sqlgraph.BatchCreate(ctx, ecb.driver, &sqlgraph.BatchCreateSpec{Nodes: specs}); err != nil { 277 if cerr, ok := isSQLConstraintError(err); ok { 278 err = cerr 279 } 280 } 281 } 282 mutation.done = true 283 if err != nil { 284 return nil, err 285 } 286 id := specs[i].ID.Value.(int64) 287 nodes[i].ID = int(id) 288 return nodes[i], nil 289 }) 290 for i := len(builder.hooks) - 1; i >= 0; i-- { 291 mut = builder.hooks[i](mut) 292 } 293 mutators[i] = mut 294 }(i, ctx) 295 } 296 if len(mutators) > 0 { 297 if _, err := mutators[0].Mutate(ctx, ecb.builders[0].mutation); err != nil { 298 return nil, err 299 } 300 } 301 return nodes, nil 302 } 303 304 // SaveX is like Save, but panics if an error occurs. 305 func (ecb *EventCreateBulk) SaveX(ctx context.Context) []*Event { 306 v, err := ecb.Save(ctx) 307 if err != nil { 308 panic(err) 309 } 310 return v 311 }