message_create.go 17 KB

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