token_create.go 23 KB

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