bouncer_create.go 11 KB

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