tutorial_create.go 28 KB

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