message_update.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. "wechat-api/ent/predicate"
  9. "entgo.io/ent/dialect/sql"
  10. "entgo.io/ent/dialect/sql/sqlgraph"
  11. "entgo.io/ent/schema/field"
  12. )
  13. // MessageUpdate is the builder for updating Message entities.
  14. type MessageUpdate struct {
  15. config
  16. hooks []Hook
  17. mutation *MessageMutation
  18. }
  19. // Where appends a list predicates to the MessageUpdate builder.
  20. func (mu *MessageUpdate) Where(ps ...predicate.Message) *MessageUpdate {
  21. mu.mutation.Where(ps...)
  22. return mu
  23. }
  24. // SetWxWxid sets the "wx_wxid" field.
  25. func (mu *MessageUpdate) SetWxWxid(s string) *MessageUpdate {
  26. mu.mutation.SetWxWxid(s)
  27. return mu
  28. }
  29. // SetNillableWxWxid sets the "wx_wxid" field if the given value is not nil.
  30. func (mu *MessageUpdate) SetNillableWxWxid(s *string) *MessageUpdate {
  31. if s != nil {
  32. mu.SetWxWxid(*s)
  33. }
  34. return mu
  35. }
  36. // ClearWxWxid clears the value of the "wx_wxid" field.
  37. func (mu *MessageUpdate) ClearWxWxid() *MessageUpdate {
  38. mu.mutation.ClearWxWxid()
  39. return mu
  40. }
  41. // SetWxid sets the "wxid" field.
  42. func (mu *MessageUpdate) SetWxid(s string) *MessageUpdate {
  43. mu.mutation.SetWxid(s)
  44. return mu
  45. }
  46. // SetNillableWxid sets the "wxid" field if the given value is not nil.
  47. func (mu *MessageUpdate) SetNillableWxid(s *string) *MessageUpdate {
  48. if s != nil {
  49. mu.SetWxid(*s)
  50. }
  51. return mu
  52. }
  53. // SetContent sets the "content" field.
  54. func (mu *MessageUpdate) SetContent(s string) *MessageUpdate {
  55. mu.mutation.SetContent(s)
  56. return mu
  57. }
  58. // SetNillableContent sets the "content" field if the given value is not nil.
  59. func (mu *MessageUpdate) SetNillableContent(s *string) *MessageUpdate {
  60. if s != nil {
  61. mu.SetContent(*s)
  62. }
  63. return mu
  64. }
  65. // Mutation returns the MessageMutation object of the builder.
  66. func (mu *MessageUpdate) Mutation() *MessageMutation {
  67. return mu.mutation
  68. }
  69. // Save executes the query and returns the number of nodes affected by the update operation.
  70. func (mu *MessageUpdate) Save(ctx context.Context) (int, error) {
  71. return withHooks(ctx, mu.sqlSave, mu.mutation, mu.hooks)
  72. }
  73. // SaveX is like Save, but panics if an error occurs.
  74. func (mu *MessageUpdate) SaveX(ctx context.Context) int {
  75. affected, err := mu.Save(ctx)
  76. if err != nil {
  77. panic(err)
  78. }
  79. return affected
  80. }
  81. // Exec executes the query.
  82. func (mu *MessageUpdate) Exec(ctx context.Context) error {
  83. _, err := mu.Save(ctx)
  84. return err
  85. }
  86. // ExecX is like Exec, but panics if an error occurs.
  87. func (mu *MessageUpdate) ExecX(ctx context.Context) {
  88. if err := mu.Exec(ctx); err != nil {
  89. panic(err)
  90. }
  91. }
  92. func (mu *MessageUpdate) sqlSave(ctx context.Context) (n int, err error) {
  93. _spec := sqlgraph.NewUpdateSpec(message.Table, message.Columns, sqlgraph.NewFieldSpec(message.FieldID, field.TypeInt))
  94. if ps := mu.mutation.predicates; len(ps) > 0 {
  95. _spec.Predicate = func(selector *sql.Selector) {
  96. for i := range ps {
  97. ps[i](selector)
  98. }
  99. }
  100. }
  101. if value, ok := mu.mutation.WxWxid(); ok {
  102. _spec.SetField(message.FieldWxWxid, field.TypeString, value)
  103. }
  104. if mu.mutation.WxWxidCleared() {
  105. _spec.ClearField(message.FieldWxWxid, field.TypeString)
  106. }
  107. if value, ok := mu.mutation.Wxid(); ok {
  108. _spec.SetField(message.FieldWxid, field.TypeString, value)
  109. }
  110. if value, ok := mu.mutation.Content(); ok {
  111. _spec.SetField(message.FieldContent, field.TypeString, value)
  112. }
  113. if n, err = sqlgraph.UpdateNodes(ctx, mu.driver, _spec); err != nil {
  114. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  115. err = &NotFoundError{message.Label}
  116. } else if sqlgraph.IsConstraintError(err) {
  117. err = &ConstraintError{msg: err.Error(), wrap: err}
  118. }
  119. return 0, err
  120. }
  121. mu.mutation.done = true
  122. return n, nil
  123. }
  124. // MessageUpdateOne is the builder for updating a single Message entity.
  125. type MessageUpdateOne struct {
  126. config
  127. fields []string
  128. hooks []Hook
  129. mutation *MessageMutation
  130. }
  131. // SetWxWxid sets the "wx_wxid" field.
  132. func (muo *MessageUpdateOne) SetWxWxid(s string) *MessageUpdateOne {
  133. muo.mutation.SetWxWxid(s)
  134. return muo
  135. }
  136. // SetNillableWxWxid sets the "wx_wxid" field if the given value is not nil.
  137. func (muo *MessageUpdateOne) SetNillableWxWxid(s *string) *MessageUpdateOne {
  138. if s != nil {
  139. muo.SetWxWxid(*s)
  140. }
  141. return muo
  142. }
  143. // ClearWxWxid clears the value of the "wx_wxid" field.
  144. func (muo *MessageUpdateOne) ClearWxWxid() *MessageUpdateOne {
  145. muo.mutation.ClearWxWxid()
  146. return muo
  147. }
  148. // SetWxid sets the "wxid" field.
  149. func (muo *MessageUpdateOne) SetWxid(s string) *MessageUpdateOne {
  150. muo.mutation.SetWxid(s)
  151. return muo
  152. }
  153. // SetNillableWxid sets the "wxid" field if the given value is not nil.
  154. func (muo *MessageUpdateOne) SetNillableWxid(s *string) *MessageUpdateOne {
  155. if s != nil {
  156. muo.SetWxid(*s)
  157. }
  158. return muo
  159. }
  160. // SetContent sets the "content" field.
  161. func (muo *MessageUpdateOne) SetContent(s string) *MessageUpdateOne {
  162. muo.mutation.SetContent(s)
  163. return muo
  164. }
  165. // SetNillableContent sets the "content" field if the given value is not nil.
  166. func (muo *MessageUpdateOne) SetNillableContent(s *string) *MessageUpdateOne {
  167. if s != nil {
  168. muo.SetContent(*s)
  169. }
  170. return muo
  171. }
  172. // Mutation returns the MessageMutation object of the builder.
  173. func (muo *MessageUpdateOne) Mutation() *MessageMutation {
  174. return muo.mutation
  175. }
  176. // Where appends a list predicates to the MessageUpdate builder.
  177. func (muo *MessageUpdateOne) Where(ps ...predicate.Message) *MessageUpdateOne {
  178. muo.mutation.Where(ps...)
  179. return muo
  180. }
  181. // Select allows selecting one or more fields (columns) of the returned entity.
  182. // The default is selecting all fields defined in the entity schema.
  183. func (muo *MessageUpdateOne) Select(field string, fields ...string) *MessageUpdateOne {
  184. muo.fields = append([]string{field}, fields...)
  185. return muo
  186. }
  187. // Save executes the query and returns the updated Message entity.
  188. func (muo *MessageUpdateOne) Save(ctx context.Context) (*Message, error) {
  189. return withHooks(ctx, muo.sqlSave, muo.mutation, muo.hooks)
  190. }
  191. // SaveX is like Save, but panics if an error occurs.
  192. func (muo *MessageUpdateOne) SaveX(ctx context.Context) *Message {
  193. node, err := muo.Save(ctx)
  194. if err != nil {
  195. panic(err)
  196. }
  197. return node
  198. }
  199. // Exec executes the query on the entity.
  200. func (muo *MessageUpdateOne) Exec(ctx context.Context) error {
  201. _, err := muo.Save(ctx)
  202. return err
  203. }
  204. // ExecX is like Exec, but panics if an error occurs.
  205. func (muo *MessageUpdateOne) ExecX(ctx context.Context) {
  206. if err := muo.Exec(ctx); err != nil {
  207. panic(err)
  208. }
  209. }
  210. func (muo *MessageUpdateOne) sqlSave(ctx context.Context) (_node *Message, err error) {
  211. _spec := sqlgraph.NewUpdateSpec(message.Table, message.Columns, sqlgraph.NewFieldSpec(message.FieldID, field.TypeInt))
  212. id, ok := muo.mutation.ID()
  213. if !ok {
  214. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Message.id" for update`)}
  215. }
  216. _spec.Node.ID.Value = id
  217. if fields := muo.fields; len(fields) > 0 {
  218. _spec.Node.Columns = make([]string, 0, len(fields))
  219. _spec.Node.Columns = append(_spec.Node.Columns, message.FieldID)
  220. for _, f := range fields {
  221. if !message.ValidColumn(f) {
  222. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  223. }
  224. if f != message.FieldID {
  225. _spec.Node.Columns = append(_spec.Node.Columns, f)
  226. }
  227. }
  228. }
  229. if ps := muo.mutation.predicates; len(ps) > 0 {
  230. _spec.Predicate = func(selector *sql.Selector) {
  231. for i := range ps {
  232. ps[i](selector)
  233. }
  234. }
  235. }
  236. if value, ok := muo.mutation.WxWxid(); ok {
  237. _spec.SetField(message.FieldWxWxid, field.TypeString, value)
  238. }
  239. if muo.mutation.WxWxidCleared() {
  240. _spec.ClearField(message.FieldWxWxid, field.TypeString)
  241. }
  242. if value, ok := muo.mutation.Wxid(); ok {
  243. _spec.SetField(message.FieldWxid, field.TypeString, value)
  244. }
  245. if value, ok := muo.mutation.Content(); ok {
  246. _spec.SetField(message.FieldContent, field.TypeString, value)
  247. }
  248. _node = &Message{config: muo.config}
  249. _spec.Assign = _node.assignValues
  250. _spec.ScanValues = _node.scanValues
  251. if err = sqlgraph.UpdateNode(ctx, muo.driver, _spec); err != nil {
  252. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  253. err = &NotFoundError{message.Label}
  254. } else if sqlgraph.IsConstraintError(err) {
  255. err = &ConstraintError{msg: err.Error(), wrap: err}
  256. }
  257. return nil, err
  258. }
  259. muo.mutation.done = true
  260. return _node, nil
  261. }