category_update.go 12 KB

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