alert_create.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "entgo.io/ent/dialect/sql/sqlgraph"
  9. "entgo.io/ent/schema/field"
  10. "github.com/crowdsecurity/crowdsec/pkg/database/ent/alert"
  11. "github.com/crowdsecurity/crowdsec/pkg/database/ent/decision"
  12. "github.com/crowdsecurity/crowdsec/pkg/database/ent/event"
  13. "github.com/crowdsecurity/crowdsec/pkg/database/ent/machine"
  14. "github.com/crowdsecurity/crowdsec/pkg/database/ent/meta"
  15. )
  16. // AlertCreate is the builder for creating a Alert entity.
  17. type AlertCreate struct {
  18. config
  19. mutation *AlertMutation
  20. hooks []Hook
  21. }
  22. // SetCreatedAt sets the "created_at" field.
  23. func (ac *AlertCreate) SetCreatedAt(t time.Time) *AlertCreate {
  24. ac.mutation.SetCreatedAt(t)
  25. return ac
  26. }
  27. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  28. func (ac *AlertCreate) SetNillableCreatedAt(t *time.Time) *AlertCreate {
  29. if t != nil {
  30. ac.SetCreatedAt(*t)
  31. }
  32. return ac
  33. }
  34. // SetUpdatedAt sets the "updated_at" field.
  35. func (ac *AlertCreate) SetUpdatedAt(t time.Time) *AlertCreate {
  36. ac.mutation.SetUpdatedAt(t)
  37. return ac
  38. }
  39. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  40. func (ac *AlertCreate) SetNillableUpdatedAt(t *time.Time) *AlertCreate {
  41. if t != nil {
  42. ac.SetUpdatedAt(*t)
  43. }
  44. return ac
  45. }
  46. // SetScenario sets the "scenario" field.
  47. func (ac *AlertCreate) SetScenario(s string) *AlertCreate {
  48. ac.mutation.SetScenario(s)
  49. return ac
  50. }
  51. // SetBucketId sets the "bucketId" field.
  52. func (ac *AlertCreate) SetBucketId(s string) *AlertCreate {
  53. ac.mutation.SetBucketId(s)
  54. return ac
  55. }
  56. // SetNillableBucketId sets the "bucketId" field if the given value is not nil.
  57. func (ac *AlertCreate) SetNillableBucketId(s *string) *AlertCreate {
  58. if s != nil {
  59. ac.SetBucketId(*s)
  60. }
  61. return ac
  62. }
  63. // SetMessage sets the "message" field.
  64. func (ac *AlertCreate) SetMessage(s string) *AlertCreate {
  65. ac.mutation.SetMessage(s)
  66. return ac
  67. }
  68. // SetNillableMessage sets the "message" field if the given value is not nil.
  69. func (ac *AlertCreate) SetNillableMessage(s *string) *AlertCreate {
  70. if s != nil {
  71. ac.SetMessage(*s)
  72. }
  73. return ac
  74. }
  75. // SetEventsCount sets the "eventsCount" field.
  76. func (ac *AlertCreate) SetEventsCount(i int32) *AlertCreate {
  77. ac.mutation.SetEventsCount(i)
  78. return ac
  79. }
  80. // SetNillableEventsCount sets the "eventsCount" field if the given value is not nil.
  81. func (ac *AlertCreate) SetNillableEventsCount(i *int32) *AlertCreate {
  82. if i != nil {
  83. ac.SetEventsCount(*i)
  84. }
  85. return ac
  86. }
  87. // SetStartedAt sets the "startedAt" field.
  88. func (ac *AlertCreate) SetStartedAt(t time.Time) *AlertCreate {
  89. ac.mutation.SetStartedAt(t)
  90. return ac
  91. }
  92. // SetNillableStartedAt sets the "startedAt" field if the given value is not nil.
  93. func (ac *AlertCreate) SetNillableStartedAt(t *time.Time) *AlertCreate {
  94. if t != nil {
  95. ac.SetStartedAt(*t)
  96. }
  97. return ac
  98. }
  99. // SetStoppedAt sets the "stoppedAt" field.
  100. func (ac *AlertCreate) SetStoppedAt(t time.Time) *AlertCreate {
  101. ac.mutation.SetStoppedAt(t)
  102. return ac
  103. }
  104. // SetNillableStoppedAt sets the "stoppedAt" field if the given value is not nil.
  105. func (ac *AlertCreate) SetNillableStoppedAt(t *time.Time) *AlertCreate {
  106. if t != nil {
  107. ac.SetStoppedAt(*t)
  108. }
  109. return ac
  110. }
  111. // SetSourceIp sets the "sourceIp" field.
  112. func (ac *AlertCreate) SetSourceIp(s string) *AlertCreate {
  113. ac.mutation.SetSourceIp(s)
  114. return ac
  115. }
  116. // SetNillableSourceIp sets the "sourceIp" field if the given value is not nil.
  117. func (ac *AlertCreate) SetNillableSourceIp(s *string) *AlertCreate {
  118. if s != nil {
  119. ac.SetSourceIp(*s)
  120. }
  121. return ac
  122. }
  123. // SetSourceRange sets the "sourceRange" field.
  124. func (ac *AlertCreate) SetSourceRange(s string) *AlertCreate {
  125. ac.mutation.SetSourceRange(s)
  126. return ac
  127. }
  128. // SetNillableSourceRange sets the "sourceRange" field if the given value is not nil.
  129. func (ac *AlertCreate) SetNillableSourceRange(s *string) *AlertCreate {
  130. if s != nil {
  131. ac.SetSourceRange(*s)
  132. }
  133. return ac
  134. }
  135. // SetSourceAsNumber sets the "sourceAsNumber" field.
  136. func (ac *AlertCreate) SetSourceAsNumber(s string) *AlertCreate {
  137. ac.mutation.SetSourceAsNumber(s)
  138. return ac
  139. }
  140. // SetNillableSourceAsNumber sets the "sourceAsNumber" field if the given value is not nil.
  141. func (ac *AlertCreate) SetNillableSourceAsNumber(s *string) *AlertCreate {
  142. if s != nil {
  143. ac.SetSourceAsNumber(*s)
  144. }
  145. return ac
  146. }
  147. // SetSourceAsName sets the "sourceAsName" field.
  148. func (ac *AlertCreate) SetSourceAsName(s string) *AlertCreate {
  149. ac.mutation.SetSourceAsName(s)
  150. return ac
  151. }
  152. // SetNillableSourceAsName sets the "sourceAsName" field if the given value is not nil.
  153. func (ac *AlertCreate) SetNillableSourceAsName(s *string) *AlertCreate {
  154. if s != nil {
  155. ac.SetSourceAsName(*s)
  156. }
  157. return ac
  158. }
  159. // SetSourceCountry sets the "sourceCountry" field.
  160. func (ac *AlertCreate) SetSourceCountry(s string) *AlertCreate {
  161. ac.mutation.SetSourceCountry(s)
  162. return ac
  163. }
  164. // SetNillableSourceCountry sets the "sourceCountry" field if the given value is not nil.
  165. func (ac *AlertCreate) SetNillableSourceCountry(s *string) *AlertCreate {
  166. if s != nil {
  167. ac.SetSourceCountry(*s)
  168. }
  169. return ac
  170. }
  171. // SetSourceLatitude sets the "sourceLatitude" field.
  172. func (ac *AlertCreate) SetSourceLatitude(f float32) *AlertCreate {
  173. ac.mutation.SetSourceLatitude(f)
  174. return ac
  175. }
  176. // SetNillableSourceLatitude sets the "sourceLatitude" field if the given value is not nil.
  177. func (ac *AlertCreate) SetNillableSourceLatitude(f *float32) *AlertCreate {
  178. if f != nil {
  179. ac.SetSourceLatitude(*f)
  180. }
  181. return ac
  182. }
  183. // SetSourceLongitude sets the "sourceLongitude" field.
  184. func (ac *AlertCreate) SetSourceLongitude(f float32) *AlertCreate {
  185. ac.mutation.SetSourceLongitude(f)
  186. return ac
  187. }
  188. // SetNillableSourceLongitude sets the "sourceLongitude" field if the given value is not nil.
  189. func (ac *AlertCreate) SetNillableSourceLongitude(f *float32) *AlertCreate {
  190. if f != nil {
  191. ac.SetSourceLongitude(*f)
  192. }
  193. return ac
  194. }
  195. // SetSourceScope sets the "sourceScope" field.
  196. func (ac *AlertCreate) SetSourceScope(s string) *AlertCreate {
  197. ac.mutation.SetSourceScope(s)
  198. return ac
  199. }
  200. // SetNillableSourceScope sets the "sourceScope" field if the given value is not nil.
  201. func (ac *AlertCreate) SetNillableSourceScope(s *string) *AlertCreate {
  202. if s != nil {
  203. ac.SetSourceScope(*s)
  204. }
  205. return ac
  206. }
  207. // SetSourceValue sets the "sourceValue" field.
  208. func (ac *AlertCreate) SetSourceValue(s string) *AlertCreate {
  209. ac.mutation.SetSourceValue(s)
  210. return ac
  211. }
  212. // SetNillableSourceValue sets the "sourceValue" field if the given value is not nil.
  213. func (ac *AlertCreate) SetNillableSourceValue(s *string) *AlertCreate {
  214. if s != nil {
  215. ac.SetSourceValue(*s)
  216. }
  217. return ac
  218. }
  219. // SetCapacity sets the "capacity" field.
  220. func (ac *AlertCreate) SetCapacity(i int32) *AlertCreate {
  221. ac.mutation.SetCapacity(i)
  222. return ac
  223. }
  224. // SetNillableCapacity sets the "capacity" field if the given value is not nil.
  225. func (ac *AlertCreate) SetNillableCapacity(i *int32) *AlertCreate {
  226. if i != nil {
  227. ac.SetCapacity(*i)
  228. }
  229. return ac
  230. }
  231. // SetLeakSpeed sets the "leakSpeed" field.
  232. func (ac *AlertCreate) SetLeakSpeed(s string) *AlertCreate {
  233. ac.mutation.SetLeakSpeed(s)
  234. return ac
  235. }
  236. // SetNillableLeakSpeed sets the "leakSpeed" field if the given value is not nil.
  237. func (ac *AlertCreate) SetNillableLeakSpeed(s *string) *AlertCreate {
  238. if s != nil {
  239. ac.SetLeakSpeed(*s)
  240. }
  241. return ac
  242. }
  243. // SetScenarioVersion sets the "scenarioVersion" field.
  244. func (ac *AlertCreate) SetScenarioVersion(s string) *AlertCreate {
  245. ac.mutation.SetScenarioVersion(s)
  246. return ac
  247. }
  248. // SetNillableScenarioVersion sets the "scenarioVersion" field if the given value is not nil.
  249. func (ac *AlertCreate) SetNillableScenarioVersion(s *string) *AlertCreate {
  250. if s != nil {
  251. ac.SetScenarioVersion(*s)
  252. }
  253. return ac
  254. }
  255. // SetScenarioHash sets the "scenarioHash" field.
  256. func (ac *AlertCreate) SetScenarioHash(s string) *AlertCreate {
  257. ac.mutation.SetScenarioHash(s)
  258. return ac
  259. }
  260. // SetNillableScenarioHash sets the "scenarioHash" field if the given value is not nil.
  261. func (ac *AlertCreate) SetNillableScenarioHash(s *string) *AlertCreate {
  262. if s != nil {
  263. ac.SetScenarioHash(*s)
  264. }
  265. return ac
  266. }
  267. // SetSimulated sets the "simulated" field.
  268. func (ac *AlertCreate) SetSimulated(b bool) *AlertCreate {
  269. ac.mutation.SetSimulated(b)
  270. return ac
  271. }
  272. // SetNillableSimulated sets the "simulated" field if the given value is not nil.
  273. func (ac *AlertCreate) SetNillableSimulated(b *bool) *AlertCreate {
  274. if b != nil {
  275. ac.SetSimulated(*b)
  276. }
  277. return ac
  278. }
  279. // SetOwnerID sets the "owner" edge to the Machine entity by ID.
  280. func (ac *AlertCreate) SetOwnerID(id int) *AlertCreate {
  281. ac.mutation.SetOwnerID(id)
  282. return ac
  283. }
  284. // SetNillableOwnerID sets the "owner" edge to the Machine entity by ID if the given value is not nil.
  285. func (ac *AlertCreate) SetNillableOwnerID(id *int) *AlertCreate {
  286. if id != nil {
  287. ac = ac.SetOwnerID(*id)
  288. }
  289. return ac
  290. }
  291. // SetOwner sets the "owner" edge to the Machine entity.
  292. func (ac *AlertCreate) SetOwner(m *Machine) *AlertCreate {
  293. return ac.SetOwnerID(m.ID)
  294. }
  295. // AddDecisionIDs adds the "decisions" edge to the Decision entity by IDs.
  296. func (ac *AlertCreate) AddDecisionIDs(ids ...int) *AlertCreate {
  297. ac.mutation.AddDecisionIDs(ids...)
  298. return ac
  299. }
  300. // AddDecisions adds the "decisions" edges to the Decision entity.
  301. func (ac *AlertCreate) AddDecisions(d ...*Decision) *AlertCreate {
  302. ids := make([]int, len(d))
  303. for i := range d {
  304. ids[i] = d[i].ID
  305. }
  306. return ac.AddDecisionIDs(ids...)
  307. }
  308. // AddEventIDs adds the "events" edge to the Event entity by IDs.
  309. func (ac *AlertCreate) AddEventIDs(ids ...int) *AlertCreate {
  310. ac.mutation.AddEventIDs(ids...)
  311. return ac
  312. }
  313. // AddEvents adds the "events" edges to the Event entity.
  314. func (ac *AlertCreate) AddEvents(e ...*Event) *AlertCreate {
  315. ids := make([]int, len(e))
  316. for i := range e {
  317. ids[i] = e[i].ID
  318. }
  319. return ac.AddEventIDs(ids...)
  320. }
  321. // AddMetaIDs adds the "metas" edge to the Meta entity by IDs.
  322. func (ac *AlertCreate) AddMetaIDs(ids ...int) *AlertCreate {
  323. ac.mutation.AddMetaIDs(ids...)
  324. return ac
  325. }
  326. // AddMetas adds the "metas" edges to the Meta entity.
  327. func (ac *AlertCreate) AddMetas(m ...*Meta) *AlertCreate {
  328. ids := make([]int, len(m))
  329. for i := range m {
  330. ids[i] = m[i].ID
  331. }
  332. return ac.AddMetaIDs(ids...)
  333. }
  334. // Mutation returns the AlertMutation object of the builder.
  335. func (ac *AlertCreate) Mutation() *AlertMutation {
  336. return ac.mutation
  337. }
  338. // Save creates the Alert in the database.
  339. func (ac *AlertCreate) Save(ctx context.Context) (*Alert, error) {
  340. var (
  341. err error
  342. node *Alert
  343. )
  344. ac.defaults()
  345. if len(ac.hooks) == 0 {
  346. if err = ac.check(); err != nil {
  347. return nil, err
  348. }
  349. node, err = ac.sqlSave(ctx)
  350. } else {
  351. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  352. mutation, ok := m.(*AlertMutation)
  353. if !ok {
  354. return nil, fmt.Errorf("unexpected mutation type %T", m)
  355. }
  356. if err = ac.check(); err != nil {
  357. return nil, err
  358. }
  359. ac.mutation = mutation
  360. if node, err = ac.sqlSave(ctx); err != nil {
  361. return nil, err
  362. }
  363. mutation.id = &node.ID
  364. mutation.done = true
  365. return node, err
  366. })
  367. for i := len(ac.hooks) - 1; i >= 0; i-- {
  368. if ac.hooks[i] == nil {
  369. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  370. }
  371. mut = ac.hooks[i](mut)
  372. }
  373. if _, err := mut.Mutate(ctx, ac.mutation); err != nil {
  374. return nil, err
  375. }
  376. }
  377. return node, err
  378. }
  379. // SaveX calls Save and panics if Save returns an error.
  380. func (ac *AlertCreate) SaveX(ctx context.Context) *Alert {
  381. v, err := ac.Save(ctx)
  382. if err != nil {
  383. panic(err)
  384. }
  385. return v
  386. }
  387. // Exec executes the query.
  388. func (ac *AlertCreate) Exec(ctx context.Context) error {
  389. _, err := ac.Save(ctx)
  390. return err
  391. }
  392. // ExecX is like Exec, but panics if an error occurs.
  393. func (ac *AlertCreate) ExecX(ctx context.Context) {
  394. if err := ac.Exec(ctx); err != nil {
  395. panic(err)
  396. }
  397. }
  398. // defaults sets the default values of the builder before save.
  399. func (ac *AlertCreate) defaults() {
  400. if _, ok := ac.mutation.CreatedAt(); !ok {
  401. v := alert.DefaultCreatedAt()
  402. ac.mutation.SetCreatedAt(v)
  403. }
  404. if _, ok := ac.mutation.UpdatedAt(); !ok {
  405. v := alert.DefaultUpdatedAt()
  406. ac.mutation.SetUpdatedAt(v)
  407. }
  408. if _, ok := ac.mutation.BucketId(); !ok {
  409. v := alert.DefaultBucketId
  410. ac.mutation.SetBucketId(v)
  411. }
  412. if _, ok := ac.mutation.Message(); !ok {
  413. v := alert.DefaultMessage
  414. ac.mutation.SetMessage(v)
  415. }
  416. if _, ok := ac.mutation.EventsCount(); !ok {
  417. v := alert.DefaultEventsCount
  418. ac.mutation.SetEventsCount(v)
  419. }
  420. if _, ok := ac.mutation.StartedAt(); !ok {
  421. v := alert.DefaultStartedAt()
  422. ac.mutation.SetStartedAt(v)
  423. }
  424. if _, ok := ac.mutation.StoppedAt(); !ok {
  425. v := alert.DefaultStoppedAt()
  426. ac.mutation.SetStoppedAt(v)
  427. }
  428. if _, ok := ac.mutation.Simulated(); !ok {
  429. v := alert.DefaultSimulated
  430. ac.mutation.SetSimulated(v)
  431. }
  432. }
  433. // check runs all checks and user-defined validators on the builder.
  434. func (ac *AlertCreate) check() error {
  435. if _, ok := ac.mutation.Scenario(); !ok {
  436. return &ValidationError{Name: "scenario", err: errors.New(`ent: missing required field "scenario"`)}
  437. }
  438. if _, ok := ac.mutation.Simulated(); !ok {
  439. return &ValidationError{Name: "simulated", err: errors.New(`ent: missing required field "simulated"`)}
  440. }
  441. return nil
  442. }
  443. func (ac *AlertCreate) sqlSave(ctx context.Context) (*Alert, error) {
  444. _node, _spec := ac.createSpec()
  445. if err := sqlgraph.CreateNode(ctx, ac.driver, _spec); err != nil {
  446. if sqlgraph.IsConstraintError(err) {
  447. err = &ConstraintError{err.Error(), err}
  448. }
  449. return nil, err
  450. }
  451. id := _spec.ID.Value.(int64)
  452. _node.ID = int(id)
  453. return _node, nil
  454. }
  455. func (ac *AlertCreate) createSpec() (*Alert, *sqlgraph.CreateSpec) {
  456. var (
  457. _node = &Alert{config: ac.config}
  458. _spec = &sqlgraph.CreateSpec{
  459. Table: alert.Table,
  460. ID: &sqlgraph.FieldSpec{
  461. Type: field.TypeInt,
  462. Column: alert.FieldID,
  463. },
  464. }
  465. )
  466. if value, ok := ac.mutation.CreatedAt(); ok {
  467. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  468. Type: field.TypeTime,
  469. Value: value,
  470. Column: alert.FieldCreatedAt,
  471. })
  472. _node.CreatedAt = &value
  473. }
  474. if value, ok := ac.mutation.UpdatedAt(); ok {
  475. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  476. Type: field.TypeTime,
  477. Value: value,
  478. Column: alert.FieldUpdatedAt,
  479. })
  480. _node.UpdatedAt = &value
  481. }
  482. if value, ok := ac.mutation.Scenario(); ok {
  483. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  484. Type: field.TypeString,
  485. Value: value,
  486. Column: alert.FieldScenario,
  487. })
  488. _node.Scenario = value
  489. }
  490. if value, ok := ac.mutation.BucketId(); ok {
  491. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  492. Type: field.TypeString,
  493. Value: value,
  494. Column: alert.FieldBucketId,
  495. })
  496. _node.BucketId = value
  497. }
  498. if value, ok := ac.mutation.Message(); ok {
  499. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  500. Type: field.TypeString,
  501. Value: value,
  502. Column: alert.FieldMessage,
  503. })
  504. _node.Message = value
  505. }
  506. if value, ok := ac.mutation.EventsCount(); ok {
  507. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  508. Type: field.TypeInt32,
  509. Value: value,
  510. Column: alert.FieldEventsCount,
  511. })
  512. _node.EventsCount = value
  513. }
  514. if value, ok := ac.mutation.StartedAt(); ok {
  515. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  516. Type: field.TypeTime,
  517. Value: value,
  518. Column: alert.FieldStartedAt,
  519. })
  520. _node.StartedAt = value
  521. }
  522. if value, ok := ac.mutation.StoppedAt(); ok {
  523. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  524. Type: field.TypeTime,
  525. Value: value,
  526. Column: alert.FieldStoppedAt,
  527. })
  528. _node.StoppedAt = value
  529. }
  530. if value, ok := ac.mutation.SourceIp(); ok {
  531. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  532. Type: field.TypeString,
  533. Value: value,
  534. Column: alert.FieldSourceIp,
  535. })
  536. _node.SourceIp = value
  537. }
  538. if value, ok := ac.mutation.SourceRange(); ok {
  539. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  540. Type: field.TypeString,
  541. Value: value,
  542. Column: alert.FieldSourceRange,
  543. })
  544. _node.SourceRange = value
  545. }
  546. if value, ok := ac.mutation.SourceAsNumber(); ok {
  547. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  548. Type: field.TypeString,
  549. Value: value,
  550. Column: alert.FieldSourceAsNumber,
  551. })
  552. _node.SourceAsNumber = value
  553. }
  554. if value, ok := ac.mutation.SourceAsName(); ok {
  555. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  556. Type: field.TypeString,
  557. Value: value,
  558. Column: alert.FieldSourceAsName,
  559. })
  560. _node.SourceAsName = value
  561. }
  562. if value, ok := ac.mutation.SourceCountry(); ok {
  563. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  564. Type: field.TypeString,
  565. Value: value,
  566. Column: alert.FieldSourceCountry,
  567. })
  568. _node.SourceCountry = value
  569. }
  570. if value, ok := ac.mutation.SourceLatitude(); ok {
  571. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  572. Type: field.TypeFloat32,
  573. Value: value,
  574. Column: alert.FieldSourceLatitude,
  575. })
  576. _node.SourceLatitude = value
  577. }
  578. if value, ok := ac.mutation.SourceLongitude(); ok {
  579. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  580. Type: field.TypeFloat32,
  581. Value: value,
  582. Column: alert.FieldSourceLongitude,
  583. })
  584. _node.SourceLongitude = value
  585. }
  586. if value, ok := ac.mutation.SourceScope(); ok {
  587. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  588. Type: field.TypeString,
  589. Value: value,
  590. Column: alert.FieldSourceScope,
  591. })
  592. _node.SourceScope = value
  593. }
  594. if value, ok := ac.mutation.SourceValue(); ok {
  595. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  596. Type: field.TypeString,
  597. Value: value,
  598. Column: alert.FieldSourceValue,
  599. })
  600. _node.SourceValue = value
  601. }
  602. if value, ok := ac.mutation.Capacity(); ok {
  603. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  604. Type: field.TypeInt32,
  605. Value: value,
  606. Column: alert.FieldCapacity,
  607. })
  608. _node.Capacity = value
  609. }
  610. if value, ok := ac.mutation.LeakSpeed(); ok {
  611. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  612. Type: field.TypeString,
  613. Value: value,
  614. Column: alert.FieldLeakSpeed,
  615. })
  616. _node.LeakSpeed = value
  617. }
  618. if value, ok := ac.mutation.ScenarioVersion(); ok {
  619. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  620. Type: field.TypeString,
  621. Value: value,
  622. Column: alert.FieldScenarioVersion,
  623. })
  624. _node.ScenarioVersion = value
  625. }
  626. if value, ok := ac.mutation.ScenarioHash(); ok {
  627. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  628. Type: field.TypeString,
  629. Value: value,
  630. Column: alert.FieldScenarioHash,
  631. })
  632. _node.ScenarioHash = value
  633. }
  634. if value, ok := ac.mutation.Simulated(); ok {
  635. _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
  636. Type: field.TypeBool,
  637. Value: value,
  638. Column: alert.FieldSimulated,
  639. })
  640. _node.Simulated = value
  641. }
  642. if nodes := ac.mutation.OwnerIDs(); len(nodes) > 0 {
  643. edge := &sqlgraph.EdgeSpec{
  644. Rel: sqlgraph.M2O,
  645. Inverse: true,
  646. Table: alert.OwnerTable,
  647. Columns: []string{alert.OwnerColumn},
  648. Bidi: false,
  649. Target: &sqlgraph.EdgeTarget{
  650. IDSpec: &sqlgraph.FieldSpec{
  651. Type: field.TypeInt,
  652. Column: machine.FieldID,
  653. },
  654. },
  655. }
  656. for _, k := range nodes {
  657. edge.Target.Nodes = append(edge.Target.Nodes, k)
  658. }
  659. _node.machine_alerts = &nodes[0]
  660. _spec.Edges = append(_spec.Edges, edge)
  661. }
  662. if nodes := ac.mutation.DecisionsIDs(); len(nodes) > 0 {
  663. edge := &sqlgraph.EdgeSpec{
  664. Rel: sqlgraph.O2M,
  665. Inverse: false,
  666. Table: alert.DecisionsTable,
  667. Columns: []string{alert.DecisionsColumn},
  668. Bidi: false,
  669. Target: &sqlgraph.EdgeTarget{
  670. IDSpec: &sqlgraph.FieldSpec{
  671. Type: field.TypeInt,
  672. Column: decision.FieldID,
  673. },
  674. },
  675. }
  676. for _, k := range nodes {
  677. edge.Target.Nodes = append(edge.Target.Nodes, k)
  678. }
  679. _spec.Edges = append(_spec.Edges, edge)
  680. }
  681. if nodes := ac.mutation.EventsIDs(); len(nodes) > 0 {
  682. edge := &sqlgraph.EdgeSpec{
  683. Rel: sqlgraph.O2M,
  684. Inverse: false,
  685. Table: alert.EventsTable,
  686. Columns: []string{alert.EventsColumn},
  687. Bidi: false,
  688. Target: &sqlgraph.EdgeTarget{
  689. IDSpec: &sqlgraph.FieldSpec{
  690. Type: field.TypeInt,
  691. Column: event.FieldID,
  692. },
  693. },
  694. }
  695. for _, k := range nodes {
  696. edge.Target.Nodes = append(edge.Target.Nodes, k)
  697. }
  698. _spec.Edges = append(_spec.Edges, edge)
  699. }
  700. if nodes := ac.mutation.MetasIDs(); len(nodes) > 0 {
  701. edge := &sqlgraph.EdgeSpec{
  702. Rel: sqlgraph.O2M,
  703. Inverse: false,
  704. Table: alert.MetasTable,
  705. Columns: []string{alert.MetasColumn},
  706. Bidi: false,
  707. Target: &sqlgraph.EdgeTarget{
  708. IDSpec: &sqlgraph.FieldSpec{
  709. Type: field.TypeInt,
  710. Column: meta.FieldID,
  711. },
  712. },
  713. }
  714. for _, k := range nodes {
  715. edge.Target.Nodes = append(edge.Target.Nodes, k)
  716. }
  717. _spec.Edges = append(_spec.Edges, edge)
  718. }
  719. return _node, _spec
  720. }
  721. // AlertCreateBulk is the builder for creating many Alert entities in bulk.
  722. type AlertCreateBulk struct {
  723. config
  724. builders []*AlertCreate
  725. }
  726. // Save creates the Alert entities in the database.
  727. func (acb *AlertCreateBulk) Save(ctx context.Context) ([]*Alert, error) {
  728. specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
  729. nodes := make([]*Alert, len(acb.builders))
  730. mutators := make([]Mutator, len(acb.builders))
  731. for i := range acb.builders {
  732. func(i int, root context.Context) {
  733. builder := acb.builders[i]
  734. builder.defaults()
  735. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  736. mutation, ok := m.(*AlertMutation)
  737. if !ok {
  738. return nil, fmt.Errorf("unexpected mutation type %T", m)
  739. }
  740. if err := builder.check(); err != nil {
  741. return nil, err
  742. }
  743. builder.mutation = mutation
  744. nodes[i], specs[i] = builder.createSpec()
  745. var err error
  746. if i < len(mutators)-1 {
  747. _, err = mutators[i+1].Mutate(root, acb.builders[i+1].mutation)
  748. } else {
  749. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  750. // Invoke the actual operation on the latest mutation in the chain.
  751. if err = sqlgraph.BatchCreate(ctx, acb.driver, spec); err != nil {
  752. if sqlgraph.IsConstraintError(err) {
  753. err = &ConstraintError{err.Error(), err}
  754. }
  755. }
  756. }
  757. if err != nil {
  758. return nil, err
  759. }
  760. mutation.id = &nodes[i].ID
  761. mutation.done = true
  762. if specs[i].ID.Value != nil {
  763. id := specs[i].ID.Value.(int64)
  764. nodes[i].ID = int(id)
  765. }
  766. return nodes[i], nil
  767. })
  768. for i := len(builder.hooks) - 1; i >= 0; i-- {
  769. mut = builder.hooks[i](mut)
  770. }
  771. mutators[i] = mut
  772. }(i, ctx)
  773. }
  774. if len(mutators) > 0 {
  775. if _, err := mutators[0].Mutate(ctx, acb.builders[0].mutation); err != nil {
  776. return nil, err
  777. }
  778. }
  779. return nodes, nil
  780. }
  781. // SaveX is like Save, but panics if an error occurs.
  782. func (acb *AlertCreateBulk) SaveX(ctx context.Context) []*Alert {
  783. v, err := acb.Save(ctx)
  784. if err != nil {
  785. panic(err)
  786. }
  787. return v
  788. }
  789. // Exec executes the query.
  790. func (acb *AlertCreateBulk) Exec(ctx context.Context) error {
  791. _, err := acb.Save(ctx)
  792. return err
  793. }
  794. // ExecX is like Exec, but panics if an error occurs.
  795. func (acb *AlertCreateBulk) ExecX(ctx context.Context) {
  796. if err := acb.Exec(ctx); err != nil {
  797. panic(err)
  798. }
  799. }