chatsession_update.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. "wechat-api/ent/predicate"
  10. "entgo.io/ent/dialect/sql"
  11. "entgo.io/ent/dialect/sql/sqlgraph"
  12. "entgo.io/ent/schema/field"
  13. )
  14. // ChatSessionUpdate is the builder for updating ChatSession entities.
  15. type ChatSessionUpdate struct {
  16. config
  17. hooks []Hook
  18. mutation *ChatSessionMutation
  19. }
  20. // Where appends a list predicates to the ChatSessionUpdate builder.
  21. func (csu *ChatSessionUpdate) Where(ps ...predicate.ChatSession) *ChatSessionUpdate {
  22. csu.mutation.Where(ps...)
  23. return csu
  24. }
  25. // SetUpdatedAt sets the "updated_at" field.
  26. func (csu *ChatSessionUpdate) SetUpdatedAt(t time.Time) *ChatSessionUpdate {
  27. csu.mutation.SetUpdatedAt(t)
  28. return csu
  29. }
  30. // SetDeletedAt sets the "deleted_at" field.
  31. func (csu *ChatSessionUpdate) SetDeletedAt(t time.Time) *ChatSessionUpdate {
  32. csu.mutation.SetDeletedAt(t)
  33. return csu
  34. }
  35. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  36. func (csu *ChatSessionUpdate) SetNillableDeletedAt(t *time.Time) *ChatSessionUpdate {
  37. if t != nil {
  38. csu.SetDeletedAt(*t)
  39. }
  40. return csu
  41. }
  42. // ClearDeletedAt clears the value of the "deleted_at" field.
  43. func (csu *ChatSessionUpdate) ClearDeletedAt() *ChatSessionUpdate {
  44. csu.mutation.ClearDeletedAt()
  45. return csu
  46. }
  47. // SetName sets the "name" field.
  48. func (csu *ChatSessionUpdate) SetName(s string) *ChatSessionUpdate {
  49. csu.mutation.SetName(s)
  50. return csu
  51. }
  52. // SetNillableName sets the "name" field if the given value is not nil.
  53. func (csu *ChatSessionUpdate) SetNillableName(s *string) *ChatSessionUpdate {
  54. if s != nil {
  55. csu.SetName(*s)
  56. }
  57. return csu
  58. }
  59. // SetUserID sets the "user_id" field.
  60. func (csu *ChatSessionUpdate) SetUserID(u uint64) *ChatSessionUpdate {
  61. csu.mutation.ResetUserID()
  62. csu.mutation.SetUserID(u)
  63. return csu
  64. }
  65. // SetNillableUserID sets the "user_id" field if the given value is not nil.
  66. func (csu *ChatSessionUpdate) SetNillableUserID(u *uint64) *ChatSessionUpdate {
  67. if u != nil {
  68. csu.SetUserID(*u)
  69. }
  70. return csu
  71. }
  72. // AddUserID adds u to the "user_id" field.
  73. func (csu *ChatSessionUpdate) AddUserID(u int64) *ChatSessionUpdate {
  74. csu.mutation.AddUserID(u)
  75. return csu
  76. }
  77. // SetBotID sets the "bot_id" field.
  78. func (csu *ChatSessionUpdate) SetBotID(u uint64) *ChatSessionUpdate {
  79. csu.mutation.ResetBotID()
  80. csu.mutation.SetBotID(u)
  81. return csu
  82. }
  83. // SetNillableBotID sets the "bot_id" field if the given value is not nil.
  84. func (csu *ChatSessionUpdate) SetNillableBotID(u *uint64) *ChatSessionUpdate {
  85. if u != nil {
  86. csu.SetBotID(*u)
  87. }
  88. return csu
  89. }
  90. // AddBotID adds u to the "bot_id" field.
  91. func (csu *ChatSessionUpdate) AddBotID(u int64) *ChatSessionUpdate {
  92. csu.mutation.AddBotID(u)
  93. return csu
  94. }
  95. // SetBotType sets the "bot_type" field.
  96. func (csu *ChatSessionUpdate) SetBotType(u uint8) *ChatSessionUpdate {
  97. csu.mutation.ResetBotType()
  98. csu.mutation.SetBotType(u)
  99. return csu
  100. }
  101. // SetNillableBotType sets the "bot_type" field if the given value is not nil.
  102. func (csu *ChatSessionUpdate) SetNillableBotType(u *uint8) *ChatSessionUpdate {
  103. if u != nil {
  104. csu.SetBotType(*u)
  105. }
  106. return csu
  107. }
  108. // AddBotType adds u to the "bot_type" field.
  109. func (csu *ChatSessionUpdate) AddBotType(u int8) *ChatSessionUpdate {
  110. csu.mutation.AddBotType(u)
  111. return csu
  112. }
  113. // Mutation returns the ChatSessionMutation object of the builder.
  114. func (csu *ChatSessionUpdate) Mutation() *ChatSessionMutation {
  115. return csu.mutation
  116. }
  117. // Save executes the query and returns the number of nodes affected by the update operation.
  118. func (csu *ChatSessionUpdate) Save(ctx context.Context) (int, error) {
  119. if err := csu.defaults(); err != nil {
  120. return 0, err
  121. }
  122. return withHooks(ctx, csu.sqlSave, csu.mutation, csu.hooks)
  123. }
  124. // SaveX is like Save, but panics if an error occurs.
  125. func (csu *ChatSessionUpdate) SaveX(ctx context.Context) int {
  126. affected, err := csu.Save(ctx)
  127. if err != nil {
  128. panic(err)
  129. }
  130. return affected
  131. }
  132. // Exec executes the query.
  133. func (csu *ChatSessionUpdate) Exec(ctx context.Context) error {
  134. _, err := csu.Save(ctx)
  135. return err
  136. }
  137. // ExecX is like Exec, but panics if an error occurs.
  138. func (csu *ChatSessionUpdate) ExecX(ctx context.Context) {
  139. if err := csu.Exec(ctx); err != nil {
  140. panic(err)
  141. }
  142. }
  143. // defaults sets the default values of the builder before save.
  144. func (csu *ChatSessionUpdate) defaults() error {
  145. if _, ok := csu.mutation.UpdatedAt(); !ok {
  146. if chatsession.UpdateDefaultUpdatedAt == nil {
  147. return fmt.Errorf("ent: uninitialized chatsession.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  148. }
  149. v := chatsession.UpdateDefaultUpdatedAt()
  150. csu.mutation.SetUpdatedAt(v)
  151. }
  152. return nil
  153. }
  154. func (csu *ChatSessionUpdate) sqlSave(ctx context.Context) (n int, err error) {
  155. _spec := sqlgraph.NewUpdateSpec(chatsession.Table, chatsession.Columns, sqlgraph.NewFieldSpec(chatsession.FieldID, field.TypeUint64))
  156. if ps := csu.mutation.predicates; len(ps) > 0 {
  157. _spec.Predicate = func(selector *sql.Selector) {
  158. for i := range ps {
  159. ps[i](selector)
  160. }
  161. }
  162. }
  163. if value, ok := csu.mutation.UpdatedAt(); ok {
  164. _spec.SetField(chatsession.FieldUpdatedAt, field.TypeTime, value)
  165. }
  166. if value, ok := csu.mutation.DeletedAt(); ok {
  167. _spec.SetField(chatsession.FieldDeletedAt, field.TypeTime, value)
  168. }
  169. if csu.mutation.DeletedAtCleared() {
  170. _spec.ClearField(chatsession.FieldDeletedAt, field.TypeTime)
  171. }
  172. if value, ok := csu.mutation.Name(); ok {
  173. _spec.SetField(chatsession.FieldName, field.TypeString, value)
  174. }
  175. if value, ok := csu.mutation.UserID(); ok {
  176. _spec.SetField(chatsession.FieldUserID, field.TypeUint64, value)
  177. }
  178. if value, ok := csu.mutation.AddedUserID(); ok {
  179. _spec.AddField(chatsession.FieldUserID, field.TypeUint64, value)
  180. }
  181. if value, ok := csu.mutation.BotID(); ok {
  182. _spec.SetField(chatsession.FieldBotID, field.TypeUint64, value)
  183. }
  184. if value, ok := csu.mutation.AddedBotID(); ok {
  185. _spec.AddField(chatsession.FieldBotID, field.TypeUint64, value)
  186. }
  187. if value, ok := csu.mutation.BotType(); ok {
  188. _spec.SetField(chatsession.FieldBotType, field.TypeUint8, value)
  189. }
  190. if value, ok := csu.mutation.AddedBotType(); ok {
  191. _spec.AddField(chatsession.FieldBotType, field.TypeUint8, value)
  192. }
  193. if n, err = sqlgraph.UpdateNodes(ctx, csu.driver, _spec); err != nil {
  194. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  195. err = &NotFoundError{chatsession.Label}
  196. } else if sqlgraph.IsConstraintError(err) {
  197. err = &ConstraintError{msg: err.Error(), wrap: err}
  198. }
  199. return 0, err
  200. }
  201. csu.mutation.done = true
  202. return n, nil
  203. }
  204. // ChatSessionUpdateOne is the builder for updating a single ChatSession entity.
  205. type ChatSessionUpdateOne struct {
  206. config
  207. fields []string
  208. hooks []Hook
  209. mutation *ChatSessionMutation
  210. }
  211. // SetUpdatedAt sets the "updated_at" field.
  212. func (csuo *ChatSessionUpdateOne) SetUpdatedAt(t time.Time) *ChatSessionUpdateOne {
  213. csuo.mutation.SetUpdatedAt(t)
  214. return csuo
  215. }
  216. // SetDeletedAt sets the "deleted_at" field.
  217. func (csuo *ChatSessionUpdateOne) SetDeletedAt(t time.Time) *ChatSessionUpdateOne {
  218. csuo.mutation.SetDeletedAt(t)
  219. return csuo
  220. }
  221. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  222. func (csuo *ChatSessionUpdateOne) SetNillableDeletedAt(t *time.Time) *ChatSessionUpdateOne {
  223. if t != nil {
  224. csuo.SetDeletedAt(*t)
  225. }
  226. return csuo
  227. }
  228. // ClearDeletedAt clears the value of the "deleted_at" field.
  229. func (csuo *ChatSessionUpdateOne) ClearDeletedAt() *ChatSessionUpdateOne {
  230. csuo.mutation.ClearDeletedAt()
  231. return csuo
  232. }
  233. // SetName sets the "name" field.
  234. func (csuo *ChatSessionUpdateOne) SetName(s string) *ChatSessionUpdateOne {
  235. csuo.mutation.SetName(s)
  236. return csuo
  237. }
  238. // SetNillableName sets the "name" field if the given value is not nil.
  239. func (csuo *ChatSessionUpdateOne) SetNillableName(s *string) *ChatSessionUpdateOne {
  240. if s != nil {
  241. csuo.SetName(*s)
  242. }
  243. return csuo
  244. }
  245. // SetUserID sets the "user_id" field.
  246. func (csuo *ChatSessionUpdateOne) SetUserID(u uint64) *ChatSessionUpdateOne {
  247. csuo.mutation.ResetUserID()
  248. csuo.mutation.SetUserID(u)
  249. return csuo
  250. }
  251. // SetNillableUserID sets the "user_id" field if the given value is not nil.
  252. func (csuo *ChatSessionUpdateOne) SetNillableUserID(u *uint64) *ChatSessionUpdateOne {
  253. if u != nil {
  254. csuo.SetUserID(*u)
  255. }
  256. return csuo
  257. }
  258. // AddUserID adds u to the "user_id" field.
  259. func (csuo *ChatSessionUpdateOne) AddUserID(u int64) *ChatSessionUpdateOne {
  260. csuo.mutation.AddUserID(u)
  261. return csuo
  262. }
  263. // SetBotID sets the "bot_id" field.
  264. func (csuo *ChatSessionUpdateOne) SetBotID(u uint64) *ChatSessionUpdateOne {
  265. csuo.mutation.ResetBotID()
  266. csuo.mutation.SetBotID(u)
  267. return csuo
  268. }
  269. // SetNillableBotID sets the "bot_id" field if the given value is not nil.
  270. func (csuo *ChatSessionUpdateOne) SetNillableBotID(u *uint64) *ChatSessionUpdateOne {
  271. if u != nil {
  272. csuo.SetBotID(*u)
  273. }
  274. return csuo
  275. }
  276. // AddBotID adds u to the "bot_id" field.
  277. func (csuo *ChatSessionUpdateOne) AddBotID(u int64) *ChatSessionUpdateOne {
  278. csuo.mutation.AddBotID(u)
  279. return csuo
  280. }
  281. // SetBotType sets the "bot_type" field.
  282. func (csuo *ChatSessionUpdateOne) SetBotType(u uint8) *ChatSessionUpdateOne {
  283. csuo.mutation.ResetBotType()
  284. csuo.mutation.SetBotType(u)
  285. return csuo
  286. }
  287. // SetNillableBotType sets the "bot_type" field if the given value is not nil.
  288. func (csuo *ChatSessionUpdateOne) SetNillableBotType(u *uint8) *ChatSessionUpdateOne {
  289. if u != nil {
  290. csuo.SetBotType(*u)
  291. }
  292. return csuo
  293. }
  294. // AddBotType adds u to the "bot_type" field.
  295. func (csuo *ChatSessionUpdateOne) AddBotType(u int8) *ChatSessionUpdateOne {
  296. csuo.mutation.AddBotType(u)
  297. return csuo
  298. }
  299. // Mutation returns the ChatSessionMutation object of the builder.
  300. func (csuo *ChatSessionUpdateOne) Mutation() *ChatSessionMutation {
  301. return csuo.mutation
  302. }
  303. // Where appends a list predicates to the ChatSessionUpdate builder.
  304. func (csuo *ChatSessionUpdateOne) Where(ps ...predicate.ChatSession) *ChatSessionUpdateOne {
  305. csuo.mutation.Where(ps...)
  306. return csuo
  307. }
  308. // Select allows selecting one or more fields (columns) of the returned entity.
  309. // The default is selecting all fields defined in the entity schema.
  310. func (csuo *ChatSessionUpdateOne) Select(field string, fields ...string) *ChatSessionUpdateOne {
  311. csuo.fields = append([]string{field}, fields...)
  312. return csuo
  313. }
  314. // Save executes the query and returns the updated ChatSession entity.
  315. func (csuo *ChatSessionUpdateOne) Save(ctx context.Context) (*ChatSession, error) {
  316. if err := csuo.defaults(); err != nil {
  317. return nil, err
  318. }
  319. return withHooks(ctx, csuo.sqlSave, csuo.mutation, csuo.hooks)
  320. }
  321. // SaveX is like Save, but panics if an error occurs.
  322. func (csuo *ChatSessionUpdateOne) SaveX(ctx context.Context) *ChatSession {
  323. node, err := csuo.Save(ctx)
  324. if err != nil {
  325. panic(err)
  326. }
  327. return node
  328. }
  329. // Exec executes the query on the entity.
  330. func (csuo *ChatSessionUpdateOne) Exec(ctx context.Context) error {
  331. _, err := csuo.Save(ctx)
  332. return err
  333. }
  334. // ExecX is like Exec, but panics if an error occurs.
  335. func (csuo *ChatSessionUpdateOne) ExecX(ctx context.Context) {
  336. if err := csuo.Exec(ctx); err != nil {
  337. panic(err)
  338. }
  339. }
  340. // defaults sets the default values of the builder before save.
  341. func (csuo *ChatSessionUpdateOne) defaults() error {
  342. if _, ok := csuo.mutation.UpdatedAt(); !ok {
  343. if chatsession.UpdateDefaultUpdatedAt == nil {
  344. return fmt.Errorf("ent: uninitialized chatsession.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  345. }
  346. v := chatsession.UpdateDefaultUpdatedAt()
  347. csuo.mutation.SetUpdatedAt(v)
  348. }
  349. return nil
  350. }
  351. func (csuo *ChatSessionUpdateOne) sqlSave(ctx context.Context) (_node *ChatSession, err error) {
  352. _spec := sqlgraph.NewUpdateSpec(chatsession.Table, chatsession.Columns, sqlgraph.NewFieldSpec(chatsession.FieldID, field.TypeUint64))
  353. id, ok := csuo.mutation.ID()
  354. if !ok {
  355. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "ChatSession.id" for update`)}
  356. }
  357. _spec.Node.ID.Value = id
  358. if fields := csuo.fields; len(fields) > 0 {
  359. _spec.Node.Columns = make([]string, 0, len(fields))
  360. _spec.Node.Columns = append(_spec.Node.Columns, chatsession.FieldID)
  361. for _, f := range fields {
  362. if !chatsession.ValidColumn(f) {
  363. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  364. }
  365. if f != chatsession.FieldID {
  366. _spec.Node.Columns = append(_spec.Node.Columns, f)
  367. }
  368. }
  369. }
  370. if ps := csuo.mutation.predicates; len(ps) > 0 {
  371. _spec.Predicate = func(selector *sql.Selector) {
  372. for i := range ps {
  373. ps[i](selector)
  374. }
  375. }
  376. }
  377. if value, ok := csuo.mutation.UpdatedAt(); ok {
  378. _spec.SetField(chatsession.FieldUpdatedAt, field.TypeTime, value)
  379. }
  380. if value, ok := csuo.mutation.DeletedAt(); ok {
  381. _spec.SetField(chatsession.FieldDeletedAt, field.TypeTime, value)
  382. }
  383. if csuo.mutation.DeletedAtCleared() {
  384. _spec.ClearField(chatsession.FieldDeletedAt, field.TypeTime)
  385. }
  386. if value, ok := csuo.mutation.Name(); ok {
  387. _spec.SetField(chatsession.FieldName, field.TypeString, value)
  388. }
  389. if value, ok := csuo.mutation.UserID(); ok {
  390. _spec.SetField(chatsession.FieldUserID, field.TypeUint64, value)
  391. }
  392. if value, ok := csuo.mutation.AddedUserID(); ok {
  393. _spec.AddField(chatsession.FieldUserID, field.TypeUint64, value)
  394. }
  395. if value, ok := csuo.mutation.BotID(); ok {
  396. _spec.SetField(chatsession.FieldBotID, field.TypeUint64, value)
  397. }
  398. if value, ok := csuo.mutation.AddedBotID(); ok {
  399. _spec.AddField(chatsession.FieldBotID, field.TypeUint64, value)
  400. }
  401. if value, ok := csuo.mutation.BotType(); ok {
  402. _spec.SetField(chatsession.FieldBotType, field.TypeUint8, value)
  403. }
  404. if value, ok := csuo.mutation.AddedBotType(); ok {
  405. _spec.AddField(chatsession.FieldBotType, field.TypeUint8, value)
  406. }
  407. _node = &ChatSession{config: csuo.config}
  408. _spec.Assign = _node.assignValues
  409. _spec.ScanValues = _node.scanValues
  410. if err = sqlgraph.UpdateNode(ctx, csuo.driver, _spec); err != nil {
  411. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  412. err = &NotFoundError{chatsession.Label}
  413. } else if sqlgraph.IsConstraintError(err) {
  414. err = &ConstraintError{msg: err.Error(), wrap: err}
  415. }
  416. return nil, err
  417. }
  418. csuo.mutation.done = true
  419. return _node, nil
  420. }