promocodeusage_create.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "entgo.io/ent/dialect/sql"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. "github.com/Wei-Shaw/sub2api/ent/promocode"
  12. "github.com/Wei-Shaw/sub2api/ent/promocodeusage"
  13. "github.com/Wei-Shaw/sub2api/ent/user"
  14. )
  15. // PromoCodeUsageCreate is the builder for creating a PromoCodeUsage entity.
  16. type PromoCodeUsageCreate struct {
  17. config
  18. mutation *PromoCodeUsageMutation
  19. hooks []Hook
  20. conflict []sql.ConflictOption
  21. }
  22. // SetPromoCodeID sets the "promo_code_id" field.
  23. func (_c *PromoCodeUsageCreate) SetPromoCodeID(v int64) *PromoCodeUsageCreate {
  24. _c.mutation.SetPromoCodeID(v)
  25. return _c
  26. }
  27. // SetUserID sets the "user_id" field.
  28. func (_c *PromoCodeUsageCreate) SetUserID(v int64) *PromoCodeUsageCreate {
  29. _c.mutation.SetUserID(v)
  30. return _c
  31. }
  32. // SetBonusAmount sets the "bonus_amount" field.
  33. func (_c *PromoCodeUsageCreate) SetBonusAmount(v float64) *PromoCodeUsageCreate {
  34. _c.mutation.SetBonusAmount(v)
  35. return _c
  36. }
  37. // SetUsedAt sets the "used_at" field.
  38. func (_c *PromoCodeUsageCreate) SetUsedAt(v time.Time) *PromoCodeUsageCreate {
  39. _c.mutation.SetUsedAt(v)
  40. return _c
  41. }
  42. // SetNillableUsedAt sets the "used_at" field if the given value is not nil.
  43. func (_c *PromoCodeUsageCreate) SetNillableUsedAt(v *time.Time) *PromoCodeUsageCreate {
  44. if v != nil {
  45. _c.SetUsedAt(*v)
  46. }
  47. return _c
  48. }
  49. // SetPromoCode sets the "promo_code" edge to the PromoCode entity.
  50. func (_c *PromoCodeUsageCreate) SetPromoCode(v *PromoCode) *PromoCodeUsageCreate {
  51. return _c.SetPromoCodeID(v.ID)
  52. }
  53. // SetUser sets the "user" edge to the User entity.
  54. func (_c *PromoCodeUsageCreate) SetUser(v *User) *PromoCodeUsageCreate {
  55. return _c.SetUserID(v.ID)
  56. }
  57. // Mutation returns the PromoCodeUsageMutation object of the builder.
  58. func (_c *PromoCodeUsageCreate) Mutation() *PromoCodeUsageMutation {
  59. return _c.mutation
  60. }
  61. // Save creates the PromoCodeUsage in the database.
  62. func (_c *PromoCodeUsageCreate) Save(ctx context.Context) (*PromoCodeUsage, error) {
  63. _c.defaults()
  64. return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
  65. }
  66. // SaveX calls Save and panics if Save returns an error.
  67. func (_c *PromoCodeUsageCreate) SaveX(ctx context.Context) *PromoCodeUsage {
  68. v, err := _c.Save(ctx)
  69. if err != nil {
  70. panic(err)
  71. }
  72. return v
  73. }
  74. // Exec executes the query.
  75. func (_c *PromoCodeUsageCreate) Exec(ctx context.Context) error {
  76. _, err := _c.Save(ctx)
  77. return err
  78. }
  79. // ExecX is like Exec, but panics if an error occurs.
  80. func (_c *PromoCodeUsageCreate) ExecX(ctx context.Context) {
  81. if err := _c.Exec(ctx); err != nil {
  82. panic(err)
  83. }
  84. }
  85. // defaults sets the default values of the builder before save.
  86. func (_c *PromoCodeUsageCreate) defaults() {
  87. if _, ok := _c.mutation.UsedAt(); !ok {
  88. v := promocodeusage.DefaultUsedAt()
  89. _c.mutation.SetUsedAt(v)
  90. }
  91. }
  92. // check runs all checks and user-defined validators on the builder.
  93. func (_c *PromoCodeUsageCreate) check() error {
  94. if _, ok := _c.mutation.PromoCodeID(); !ok {
  95. return &ValidationError{Name: "promo_code_id", err: errors.New(`ent: missing required field "PromoCodeUsage.promo_code_id"`)}
  96. }
  97. if _, ok := _c.mutation.UserID(); !ok {
  98. return &ValidationError{Name: "user_id", err: errors.New(`ent: missing required field "PromoCodeUsage.user_id"`)}
  99. }
  100. if _, ok := _c.mutation.BonusAmount(); !ok {
  101. return &ValidationError{Name: "bonus_amount", err: errors.New(`ent: missing required field "PromoCodeUsage.bonus_amount"`)}
  102. }
  103. if _, ok := _c.mutation.UsedAt(); !ok {
  104. return &ValidationError{Name: "used_at", err: errors.New(`ent: missing required field "PromoCodeUsage.used_at"`)}
  105. }
  106. if len(_c.mutation.PromoCodeIDs()) == 0 {
  107. return &ValidationError{Name: "promo_code", err: errors.New(`ent: missing required edge "PromoCodeUsage.promo_code"`)}
  108. }
  109. if len(_c.mutation.UserIDs()) == 0 {
  110. return &ValidationError{Name: "user", err: errors.New(`ent: missing required edge "PromoCodeUsage.user"`)}
  111. }
  112. return nil
  113. }
  114. func (_c *PromoCodeUsageCreate) sqlSave(ctx context.Context) (*PromoCodeUsage, error) {
  115. if err := _c.check(); err != nil {
  116. return nil, err
  117. }
  118. _node, _spec := _c.createSpec()
  119. if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
  120. if sqlgraph.IsConstraintError(err) {
  121. err = &ConstraintError{msg: err.Error(), wrap: err}
  122. }
  123. return nil, err
  124. }
  125. id := _spec.ID.Value.(int64)
  126. _node.ID = int64(id)
  127. _c.mutation.id = &_node.ID
  128. _c.mutation.done = true
  129. return _node, nil
  130. }
  131. func (_c *PromoCodeUsageCreate) createSpec() (*PromoCodeUsage, *sqlgraph.CreateSpec) {
  132. var (
  133. _node = &PromoCodeUsage{config: _c.config}
  134. _spec = sqlgraph.NewCreateSpec(promocodeusage.Table, sqlgraph.NewFieldSpec(promocodeusage.FieldID, field.TypeInt64))
  135. )
  136. _spec.OnConflict = _c.conflict
  137. if value, ok := _c.mutation.BonusAmount(); ok {
  138. _spec.SetField(promocodeusage.FieldBonusAmount, field.TypeFloat64, value)
  139. _node.BonusAmount = value
  140. }
  141. if value, ok := _c.mutation.UsedAt(); ok {
  142. _spec.SetField(promocodeusage.FieldUsedAt, field.TypeTime, value)
  143. _node.UsedAt = value
  144. }
  145. if nodes := _c.mutation.PromoCodeIDs(); len(nodes) > 0 {
  146. edge := &sqlgraph.EdgeSpec{
  147. Rel: sqlgraph.M2O,
  148. Inverse: true,
  149. Table: promocodeusage.PromoCodeTable,
  150. Columns: []string{promocodeusage.PromoCodeColumn},
  151. Bidi: false,
  152. Target: &sqlgraph.EdgeTarget{
  153. IDSpec: sqlgraph.NewFieldSpec(promocode.FieldID, field.TypeInt64),
  154. },
  155. }
  156. for _, k := range nodes {
  157. edge.Target.Nodes = append(edge.Target.Nodes, k)
  158. }
  159. _node.PromoCodeID = nodes[0]
  160. _spec.Edges = append(_spec.Edges, edge)
  161. }
  162. if nodes := _c.mutation.UserIDs(); len(nodes) > 0 {
  163. edge := &sqlgraph.EdgeSpec{
  164. Rel: sqlgraph.M2O,
  165. Inverse: true,
  166. Table: promocodeusage.UserTable,
  167. Columns: []string{promocodeusage.UserColumn},
  168. Bidi: false,
  169. Target: &sqlgraph.EdgeTarget{
  170. IDSpec: sqlgraph.NewFieldSpec(user.FieldID, field.TypeInt64),
  171. },
  172. }
  173. for _, k := range nodes {
  174. edge.Target.Nodes = append(edge.Target.Nodes, k)
  175. }
  176. _node.UserID = nodes[0]
  177. _spec.Edges = append(_spec.Edges, edge)
  178. }
  179. return _node, _spec
  180. }
  181. // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
  182. // of the `INSERT` statement. For example:
  183. //
  184. // client.PromoCodeUsage.Create().
  185. // SetPromoCodeID(v).
  186. // OnConflict(
  187. // // Update the row with the new values
  188. // // the was proposed for insertion.
  189. // sql.ResolveWithNewValues(),
  190. // ).
  191. // // Override some of the fields with custom
  192. // // update values.
  193. // Update(func(u *ent.PromoCodeUsageUpsert) {
  194. // SetPromoCodeID(v+v).
  195. // }).
  196. // Exec(ctx)
  197. func (_c *PromoCodeUsageCreate) OnConflict(opts ...sql.ConflictOption) *PromoCodeUsageUpsertOne {
  198. _c.conflict = opts
  199. return &PromoCodeUsageUpsertOne{
  200. create: _c,
  201. }
  202. }
  203. // OnConflictColumns calls `OnConflict` and configures the columns
  204. // as conflict target. Using this option is equivalent to using:
  205. //
  206. // client.PromoCodeUsage.Create().
  207. // OnConflict(sql.ConflictColumns(columns...)).
  208. // Exec(ctx)
  209. func (_c *PromoCodeUsageCreate) OnConflictColumns(columns ...string) *PromoCodeUsageUpsertOne {
  210. _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
  211. return &PromoCodeUsageUpsertOne{
  212. create: _c,
  213. }
  214. }
  215. type (
  216. // PromoCodeUsageUpsertOne is the builder for "upsert"-ing
  217. // one PromoCodeUsage node.
  218. PromoCodeUsageUpsertOne struct {
  219. create *PromoCodeUsageCreate
  220. }
  221. // PromoCodeUsageUpsert is the "OnConflict" setter.
  222. PromoCodeUsageUpsert struct {
  223. *sql.UpdateSet
  224. }
  225. )
  226. // SetPromoCodeID sets the "promo_code_id" field.
  227. func (u *PromoCodeUsageUpsert) SetPromoCodeID(v int64) *PromoCodeUsageUpsert {
  228. u.Set(promocodeusage.FieldPromoCodeID, v)
  229. return u
  230. }
  231. // UpdatePromoCodeID sets the "promo_code_id" field to the value that was provided on create.
  232. func (u *PromoCodeUsageUpsert) UpdatePromoCodeID() *PromoCodeUsageUpsert {
  233. u.SetExcluded(promocodeusage.FieldPromoCodeID)
  234. return u
  235. }
  236. // SetUserID sets the "user_id" field.
  237. func (u *PromoCodeUsageUpsert) SetUserID(v int64) *PromoCodeUsageUpsert {
  238. u.Set(promocodeusage.FieldUserID, v)
  239. return u
  240. }
  241. // UpdateUserID sets the "user_id" field to the value that was provided on create.
  242. func (u *PromoCodeUsageUpsert) UpdateUserID() *PromoCodeUsageUpsert {
  243. u.SetExcluded(promocodeusage.FieldUserID)
  244. return u
  245. }
  246. // SetBonusAmount sets the "bonus_amount" field.
  247. func (u *PromoCodeUsageUpsert) SetBonusAmount(v float64) *PromoCodeUsageUpsert {
  248. u.Set(promocodeusage.FieldBonusAmount, v)
  249. return u
  250. }
  251. // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
  252. func (u *PromoCodeUsageUpsert) UpdateBonusAmount() *PromoCodeUsageUpsert {
  253. u.SetExcluded(promocodeusage.FieldBonusAmount)
  254. return u
  255. }
  256. // AddBonusAmount adds v to the "bonus_amount" field.
  257. func (u *PromoCodeUsageUpsert) AddBonusAmount(v float64) *PromoCodeUsageUpsert {
  258. u.Add(promocodeusage.FieldBonusAmount, v)
  259. return u
  260. }
  261. // SetUsedAt sets the "used_at" field.
  262. func (u *PromoCodeUsageUpsert) SetUsedAt(v time.Time) *PromoCodeUsageUpsert {
  263. u.Set(promocodeusage.FieldUsedAt, v)
  264. return u
  265. }
  266. // UpdateUsedAt sets the "used_at" field to the value that was provided on create.
  267. func (u *PromoCodeUsageUpsert) UpdateUsedAt() *PromoCodeUsageUpsert {
  268. u.SetExcluded(promocodeusage.FieldUsedAt)
  269. return u
  270. }
  271. // UpdateNewValues updates the mutable fields using the new values that were set on create.
  272. // Using this option is equivalent to using:
  273. //
  274. // client.PromoCodeUsage.Create().
  275. // OnConflict(
  276. // sql.ResolveWithNewValues(),
  277. // ).
  278. // Exec(ctx)
  279. func (u *PromoCodeUsageUpsertOne) UpdateNewValues() *PromoCodeUsageUpsertOne {
  280. u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
  281. return u
  282. }
  283. // Ignore sets each column to itself in case of conflict.
  284. // Using this option is equivalent to using:
  285. //
  286. // client.PromoCodeUsage.Create().
  287. // OnConflict(sql.ResolveWithIgnore()).
  288. // Exec(ctx)
  289. func (u *PromoCodeUsageUpsertOne) Ignore() *PromoCodeUsageUpsertOne {
  290. u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
  291. return u
  292. }
  293. // DoNothing configures the conflict_action to `DO NOTHING`.
  294. // Supported only by SQLite and PostgreSQL.
  295. func (u *PromoCodeUsageUpsertOne) DoNothing() *PromoCodeUsageUpsertOne {
  296. u.create.conflict = append(u.create.conflict, sql.DoNothing())
  297. return u
  298. }
  299. // Update allows overriding fields `UPDATE` values. See the PromoCodeUsageCreate.OnConflict
  300. // documentation for more info.
  301. func (u *PromoCodeUsageUpsertOne) Update(set func(*PromoCodeUsageUpsert)) *PromoCodeUsageUpsertOne {
  302. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
  303. set(&PromoCodeUsageUpsert{UpdateSet: update})
  304. }))
  305. return u
  306. }
  307. // SetPromoCodeID sets the "promo_code_id" field.
  308. func (u *PromoCodeUsageUpsertOne) SetPromoCodeID(v int64) *PromoCodeUsageUpsertOne {
  309. return u.Update(func(s *PromoCodeUsageUpsert) {
  310. s.SetPromoCodeID(v)
  311. })
  312. }
  313. // UpdatePromoCodeID sets the "promo_code_id" field to the value that was provided on create.
  314. func (u *PromoCodeUsageUpsertOne) UpdatePromoCodeID() *PromoCodeUsageUpsertOne {
  315. return u.Update(func(s *PromoCodeUsageUpsert) {
  316. s.UpdatePromoCodeID()
  317. })
  318. }
  319. // SetUserID sets the "user_id" field.
  320. func (u *PromoCodeUsageUpsertOne) SetUserID(v int64) *PromoCodeUsageUpsertOne {
  321. return u.Update(func(s *PromoCodeUsageUpsert) {
  322. s.SetUserID(v)
  323. })
  324. }
  325. // UpdateUserID sets the "user_id" field to the value that was provided on create.
  326. func (u *PromoCodeUsageUpsertOne) UpdateUserID() *PromoCodeUsageUpsertOne {
  327. return u.Update(func(s *PromoCodeUsageUpsert) {
  328. s.UpdateUserID()
  329. })
  330. }
  331. // SetBonusAmount sets the "bonus_amount" field.
  332. func (u *PromoCodeUsageUpsertOne) SetBonusAmount(v float64) *PromoCodeUsageUpsertOne {
  333. return u.Update(func(s *PromoCodeUsageUpsert) {
  334. s.SetBonusAmount(v)
  335. })
  336. }
  337. // AddBonusAmount adds v to the "bonus_amount" field.
  338. func (u *PromoCodeUsageUpsertOne) AddBonusAmount(v float64) *PromoCodeUsageUpsertOne {
  339. return u.Update(func(s *PromoCodeUsageUpsert) {
  340. s.AddBonusAmount(v)
  341. })
  342. }
  343. // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
  344. func (u *PromoCodeUsageUpsertOne) UpdateBonusAmount() *PromoCodeUsageUpsertOne {
  345. return u.Update(func(s *PromoCodeUsageUpsert) {
  346. s.UpdateBonusAmount()
  347. })
  348. }
  349. // SetUsedAt sets the "used_at" field.
  350. func (u *PromoCodeUsageUpsertOne) SetUsedAt(v time.Time) *PromoCodeUsageUpsertOne {
  351. return u.Update(func(s *PromoCodeUsageUpsert) {
  352. s.SetUsedAt(v)
  353. })
  354. }
  355. // UpdateUsedAt sets the "used_at" field to the value that was provided on create.
  356. func (u *PromoCodeUsageUpsertOne) UpdateUsedAt() *PromoCodeUsageUpsertOne {
  357. return u.Update(func(s *PromoCodeUsageUpsert) {
  358. s.UpdateUsedAt()
  359. })
  360. }
  361. // Exec executes the query.
  362. func (u *PromoCodeUsageUpsertOne) Exec(ctx context.Context) error {
  363. if len(u.create.conflict) == 0 {
  364. return errors.New("ent: missing options for PromoCodeUsageCreate.OnConflict")
  365. }
  366. return u.create.Exec(ctx)
  367. }
  368. // ExecX is like Exec, but panics if an error occurs.
  369. func (u *PromoCodeUsageUpsertOne) ExecX(ctx context.Context) {
  370. if err := u.create.Exec(ctx); err != nil {
  371. panic(err)
  372. }
  373. }
  374. // Exec executes the UPSERT query and returns the inserted/updated ID.
  375. func (u *PromoCodeUsageUpsertOne) ID(ctx context.Context) (id int64, err error) {
  376. node, err := u.create.Save(ctx)
  377. if err != nil {
  378. return id, err
  379. }
  380. return node.ID, nil
  381. }
  382. // IDX is like ID, but panics if an error occurs.
  383. func (u *PromoCodeUsageUpsertOne) IDX(ctx context.Context) int64 {
  384. id, err := u.ID(ctx)
  385. if err != nil {
  386. panic(err)
  387. }
  388. return id
  389. }
  390. // PromoCodeUsageCreateBulk is the builder for creating many PromoCodeUsage entities in bulk.
  391. type PromoCodeUsageCreateBulk struct {
  392. config
  393. err error
  394. builders []*PromoCodeUsageCreate
  395. conflict []sql.ConflictOption
  396. }
  397. // Save creates the PromoCodeUsage entities in the database.
  398. func (_c *PromoCodeUsageCreateBulk) Save(ctx context.Context) ([]*PromoCodeUsage, error) {
  399. if _c.err != nil {
  400. return nil, _c.err
  401. }
  402. specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
  403. nodes := make([]*PromoCodeUsage, len(_c.builders))
  404. mutators := make([]Mutator, len(_c.builders))
  405. for i := range _c.builders {
  406. func(i int, root context.Context) {
  407. builder := _c.builders[i]
  408. builder.defaults()
  409. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  410. mutation, ok := m.(*PromoCodeUsageMutation)
  411. if !ok {
  412. return nil, fmt.Errorf("unexpected mutation type %T", m)
  413. }
  414. if err := builder.check(); err != nil {
  415. return nil, err
  416. }
  417. builder.mutation = mutation
  418. var err error
  419. nodes[i], specs[i] = builder.createSpec()
  420. if i < len(mutators)-1 {
  421. _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
  422. } else {
  423. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  424. spec.OnConflict = _c.conflict
  425. // Invoke the actual operation on the latest mutation in the chain.
  426. if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
  427. if sqlgraph.IsConstraintError(err) {
  428. err = &ConstraintError{msg: err.Error(), wrap: err}
  429. }
  430. }
  431. }
  432. if err != nil {
  433. return nil, err
  434. }
  435. mutation.id = &nodes[i].ID
  436. if specs[i].ID.Value != nil {
  437. id := specs[i].ID.Value.(int64)
  438. nodes[i].ID = int64(id)
  439. }
  440. mutation.done = true
  441. return nodes[i], nil
  442. })
  443. for i := len(builder.hooks) - 1; i >= 0; i-- {
  444. mut = builder.hooks[i](mut)
  445. }
  446. mutators[i] = mut
  447. }(i, ctx)
  448. }
  449. if len(mutators) > 0 {
  450. if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
  451. return nil, err
  452. }
  453. }
  454. return nodes, nil
  455. }
  456. // SaveX is like Save, but panics if an error occurs.
  457. func (_c *PromoCodeUsageCreateBulk) SaveX(ctx context.Context) []*PromoCodeUsage {
  458. v, err := _c.Save(ctx)
  459. if err != nil {
  460. panic(err)
  461. }
  462. return v
  463. }
  464. // Exec executes the query.
  465. func (_c *PromoCodeUsageCreateBulk) Exec(ctx context.Context) error {
  466. _, err := _c.Save(ctx)
  467. return err
  468. }
  469. // ExecX is like Exec, but panics if an error occurs.
  470. func (_c *PromoCodeUsageCreateBulk) ExecX(ctx context.Context) {
  471. if err := _c.Exec(ctx); err != nil {
  472. panic(err)
  473. }
  474. }
  475. // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
  476. // of the `INSERT` statement. For example:
  477. //
  478. // client.PromoCodeUsage.CreateBulk(builders...).
  479. // OnConflict(
  480. // // Update the row with the new values
  481. // // the was proposed for insertion.
  482. // sql.ResolveWithNewValues(),
  483. // ).
  484. // // Override some of the fields with custom
  485. // // update values.
  486. // Update(func(u *ent.PromoCodeUsageUpsert) {
  487. // SetPromoCodeID(v+v).
  488. // }).
  489. // Exec(ctx)
  490. func (_c *PromoCodeUsageCreateBulk) OnConflict(opts ...sql.ConflictOption) *PromoCodeUsageUpsertBulk {
  491. _c.conflict = opts
  492. return &PromoCodeUsageUpsertBulk{
  493. create: _c,
  494. }
  495. }
  496. // OnConflictColumns calls `OnConflict` and configures the columns
  497. // as conflict target. Using this option is equivalent to using:
  498. //
  499. // client.PromoCodeUsage.Create().
  500. // OnConflict(sql.ConflictColumns(columns...)).
  501. // Exec(ctx)
  502. func (_c *PromoCodeUsageCreateBulk) OnConflictColumns(columns ...string) *PromoCodeUsageUpsertBulk {
  503. _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
  504. return &PromoCodeUsageUpsertBulk{
  505. create: _c,
  506. }
  507. }
  508. // PromoCodeUsageUpsertBulk is the builder for "upsert"-ing
  509. // a bulk of PromoCodeUsage nodes.
  510. type PromoCodeUsageUpsertBulk struct {
  511. create *PromoCodeUsageCreateBulk
  512. }
  513. // UpdateNewValues updates the mutable fields using the new values that
  514. // were set on create. Using this option is equivalent to using:
  515. //
  516. // client.PromoCodeUsage.Create().
  517. // OnConflict(
  518. // sql.ResolveWithNewValues(),
  519. // ).
  520. // Exec(ctx)
  521. func (u *PromoCodeUsageUpsertBulk) UpdateNewValues() *PromoCodeUsageUpsertBulk {
  522. u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
  523. return u
  524. }
  525. // Ignore sets each column to itself in case of conflict.
  526. // Using this option is equivalent to using:
  527. //
  528. // client.PromoCodeUsage.Create().
  529. // OnConflict(sql.ResolveWithIgnore()).
  530. // Exec(ctx)
  531. func (u *PromoCodeUsageUpsertBulk) Ignore() *PromoCodeUsageUpsertBulk {
  532. u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
  533. return u
  534. }
  535. // DoNothing configures the conflict_action to `DO NOTHING`.
  536. // Supported only by SQLite and PostgreSQL.
  537. func (u *PromoCodeUsageUpsertBulk) DoNothing() *PromoCodeUsageUpsertBulk {
  538. u.create.conflict = append(u.create.conflict, sql.DoNothing())
  539. return u
  540. }
  541. // Update allows overriding fields `UPDATE` values. See the PromoCodeUsageCreateBulk.OnConflict
  542. // documentation for more info.
  543. func (u *PromoCodeUsageUpsertBulk) Update(set func(*PromoCodeUsageUpsert)) *PromoCodeUsageUpsertBulk {
  544. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
  545. set(&PromoCodeUsageUpsert{UpdateSet: update})
  546. }))
  547. return u
  548. }
  549. // SetPromoCodeID sets the "promo_code_id" field.
  550. func (u *PromoCodeUsageUpsertBulk) SetPromoCodeID(v int64) *PromoCodeUsageUpsertBulk {
  551. return u.Update(func(s *PromoCodeUsageUpsert) {
  552. s.SetPromoCodeID(v)
  553. })
  554. }
  555. // UpdatePromoCodeID sets the "promo_code_id" field to the value that was provided on create.
  556. func (u *PromoCodeUsageUpsertBulk) UpdatePromoCodeID() *PromoCodeUsageUpsertBulk {
  557. return u.Update(func(s *PromoCodeUsageUpsert) {
  558. s.UpdatePromoCodeID()
  559. })
  560. }
  561. // SetUserID sets the "user_id" field.
  562. func (u *PromoCodeUsageUpsertBulk) SetUserID(v int64) *PromoCodeUsageUpsertBulk {
  563. return u.Update(func(s *PromoCodeUsageUpsert) {
  564. s.SetUserID(v)
  565. })
  566. }
  567. // UpdateUserID sets the "user_id" field to the value that was provided on create.
  568. func (u *PromoCodeUsageUpsertBulk) UpdateUserID() *PromoCodeUsageUpsertBulk {
  569. return u.Update(func(s *PromoCodeUsageUpsert) {
  570. s.UpdateUserID()
  571. })
  572. }
  573. // SetBonusAmount sets the "bonus_amount" field.
  574. func (u *PromoCodeUsageUpsertBulk) SetBonusAmount(v float64) *PromoCodeUsageUpsertBulk {
  575. return u.Update(func(s *PromoCodeUsageUpsert) {
  576. s.SetBonusAmount(v)
  577. })
  578. }
  579. // AddBonusAmount adds v to the "bonus_amount" field.
  580. func (u *PromoCodeUsageUpsertBulk) AddBonusAmount(v float64) *PromoCodeUsageUpsertBulk {
  581. return u.Update(func(s *PromoCodeUsageUpsert) {
  582. s.AddBonusAmount(v)
  583. })
  584. }
  585. // UpdateBonusAmount sets the "bonus_amount" field to the value that was provided on create.
  586. func (u *PromoCodeUsageUpsertBulk) UpdateBonusAmount() *PromoCodeUsageUpsertBulk {
  587. return u.Update(func(s *PromoCodeUsageUpsert) {
  588. s.UpdateBonusAmount()
  589. })
  590. }
  591. // SetUsedAt sets the "used_at" field.
  592. func (u *PromoCodeUsageUpsertBulk) SetUsedAt(v time.Time) *PromoCodeUsageUpsertBulk {
  593. return u.Update(func(s *PromoCodeUsageUpsert) {
  594. s.SetUsedAt(v)
  595. })
  596. }
  597. // UpdateUsedAt sets the "used_at" field to the value that was provided on create.
  598. func (u *PromoCodeUsageUpsertBulk) UpdateUsedAt() *PromoCodeUsageUpsertBulk {
  599. return u.Update(func(s *PromoCodeUsageUpsert) {
  600. s.UpdateUsedAt()
  601. })
  602. }
  603. // Exec executes the query.
  604. func (u *PromoCodeUsageUpsertBulk) Exec(ctx context.Context) error {
  605. if u.create.err != nil {
  606. return u.create.err
  607. }
  608. for i, b := range u.create.builders {
  609. if len(b.conflict) != 0 {
  610. return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the PromoCodeUsageCreateBulk instead", i)
  611. }
  612. }
  613. if len(u.create.conflict) == 0 {
  614. return errors.New("ent: missing options for PromoCodeUsageCreateBulk.OnConflict")
  615. }
  616. return u.create.Exec(ctx)
  617. }
  618. // ExecX is like Exec, but panics if an error occurs.
  619. func (u *PromoCodeUsageUpsertBulk) ExecX(ctx context.Context) {
  620. if err := u.create.Exec(ctx); err != nil {
  621. panic(err)
  622. }
  623. }