event_create.go 8.7 KB

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