chatsession_create.go 27 KB

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