category_create.go 22 KB

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