setting_update.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "entgo.io/ent/dialect/sql"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. "github.com/Wei-Shaw/sub2api/ent/predicate"
  12. "github.com/Wei-Shaw/sub2api/ent/setting"
  13. )
  14. // SettingUpdate is the builder for updating Setting entities.
  15. type SettingUpdate struct {
  16. config
  17. hooks []Hook
  18. mutation *SettingMutation
  19. }
  20. // Where appends a list predicates to the SettingUpdate builder.
  21. func (_u *SettingUpdate) Where(ps ...predicate.Setting) *SettingUpdate {
  22. _u.mutation.Where(ps...)
  23. return _u
  24. }
  25. // SetKey sets the "key" field.
  26. func (_u *SettingUpdate) SetKey(v string) *SettingUpdate {
  27. _u.mutation.SetKey(v)
  28. return _u
  29. }
  30. // SetNillableKey sets the "key" field if the given value is not nil.
  31. func (_u *SettingUpdate) SetNillableKey(v *string) *SettingUpdate {
  32. if v != nil {
  33. _u.SetKey(*v)
  34. }
  35. return _u
  36. }
  37. // SetValue sets the "value" field.
  38. func (_u *SettingUpdate) SetValue(v string) *SettingUpdate {
  39. _u.mutation.SetValue(v)
  40. return _u
  41. }
  42. // SetNillableValue sets the "value" field if the given value is not nil.
  43. func (_u *SettingUpdate) SetNillableValue(v *string) *SettingUpdate {
  44. if v != nil {
  45. _u.SetValue(*v)
  46. }
  47. return _u
  48. }
  49. // SetUpdatedAt sets the "updated_at" field.
  50. func (_u *SettingUpdate) SetUpdatedAt(v time.Time) *SettingUpdate {
  51. _u.mutation.SetUpdatedAt(v)
  52. return _u
  53. }
  54. // Mutation returns the SettingMutation object of the builder.
  55. func (_u *SettingUpdate) Mutation() *SettingMutation {
  56. return _u.mutation
  57. }
  58. // Save executes the query and returns the number of nodes affected by the update operation.
  59. func (_u *SettingUpdate) Save(ctx context.Context) (int, error) {
  60. _u.defaults()
  61. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  62. }
  63. // SaveX is like Save, but panics if an error occurs.
  64. func (_u *SettingUpdate) SaveX(ctx context.Context) int {
  65. affected, err := _u.Save(ctx)
  66. if err != nil {
  67. panic(err)
  68. }
  69. return affected
  70. }
  71. // Exec executes the query.
  72. func (_u *SettingUpdate) Exec(ctx context.Context) error {
  73. _, err := _u.Save(ctx)
  74. return err
  75. }
  76. // ExecX is like Exec, but panics if an error occurs.
  77. func (_u *SettingUpdate) ExecX(ctx context.Context) {
  78. if err := _u.Exec(ctx); err != nil {
  79. panic(err)
  80. }
  81. }
  82. // defaults sets the default values of the builder before save.
  83. func (_u *SettingUpdate) defaults() {
  84. if _, ok := _u.mutation.UpdatedAt(); !ok {
  85. v := setting.UpdateDefaultUpdatedAt()
  86. _u.mutation.SetUpdatedAt(v)
  87. }
  88. }
  89. // check runs all checks and user-defined validators on the builder.
  90. func (_u *SettingUpdate) check() error {
  91. if v, ok := _u.mutation.Key(); ok {
  92. if err := setting.KeyValidator(v); err != nil {
  93. return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "Setting.key": %w`, err)}
  94. }
  95. }
  96. return nil
  97. }
  98. func (_u *SettingUpdate) sqlSave(ctx context.Context) (_node int, err error) {
  99. if err := _u.check(); err != nil {
  100. return _node, err
  101. }
  102. _spec := sqlgraph.NewUpdateSpec(setting.Table, setting.Columns, sqlgraph.NewFieldSpec(setting.FieldID, field.TypeInt64))
  103. if ps := _u.mutation.predicates; len(ps) > 0 {
  104. _spec.Predicate = func(selector *sql.Selector) {
  105. for i := range ps {
  106. ps[i](selector)
  107. }
  108. }
  109. }
  110. if value, ok := _u.mutation.Key(); ok {
  111. _spec.SetField(setting.FieldKey, field.TypeString, value)
  112. }
  113. if value, ok := _u.mutation.Value(); ok {
  114. _spec.SetField(setting.FieldValue, field.TypeString, value)
  115. }
  116. if value, ok := _u.mutation.UpdatedAt(); ok {
  117. _spec.SetField(setting.FieldUpdatedAt, field.TypeTime, value)
  118. }
  119. if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
  120. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  121. err = &NotFoundError{setting.Label}
  122. } else if sqlgraph.IsConstraintError(err) {
  123. err = &ConstraintError{msg: err.Error(), wrap: err}
  124. }
  125. return 0, err
  126. }
  127. _u.mutation.done = true
  128. return _node, nil
  129. }
  130. // SettingUpdateOne is the builder for updating a single Setting entity.
  131. type SettingUpdateOne struct {
  132. config
  133. fields []string
  134. hooks []Hook
  135. mutation *SettingMutation
  136. }
  137. // SetKey sets the "key" field.
  138. func (_u *SettingUpdateOne) SetKey(v string) *SettingUpdateOne {
  139. _u.mutation.SetKey(v)
  140. return _u
  141. }
  142. // SetNillableKey sets the "key" field if the given value is not nil.
  143. func (_u *SettingUpdateOne) SetNillableKey(v *string) *SettingUpdateOne {
  144. if v != nil {
  145. _u.SetKey(*v)
  146. }
  147. return _u
  148. }
  149. // SetValue sets the "value" field.
  150. func (_u *SettingUpdateOne) SetValue(v string) *SettingUpdateOne {
  151. _u.mutation.SetValue(v)
  152. return _u
  153. }
  154. // SetNillableValue sets the "value" field if the given value is not nil.
  155. func (_u *SettingUpdateOne) SetNillableValue(v *string) *SettingUpdateOne {
  156. if v != nil {
  157. _u.SetValue(*v)
  158. }
  159. return _u
  160. }
  161. // SetUpdatedAt sets the "updated_at" field.
  162. func (_u *SettingUpdateOne) SetUpdatedAt(v time.Time) *SettingUpdateOne {
  163. _u.mutation.SetUpdatedAt(v)
  164. return _u
  165. }
  166. // Mutation returns the SettingMutation object of the builder.
  167. func (_u *SettingUpdateOne) Mutation() *SettingMutation {
  168. return _u.mutation
  169. }
  170. // Where appends a list predicates to the SettingUpdate builder.
  171. func (_u *SettingUpdateOne) Where(ps ...predicate.Setting) *SettingUpdateOne {
  172. _u.mutation.Where(ps...)
  173. return _u
  174. }
  175. // Select allows selecting one or more fields (columns) of the returned entity.
  176. // The default is selecting all fields defined in the entity schema.
  177. func (_u *SettingUpdateOne) Select(field string, fields ...string) *SettingUpdateOne {
  178. _u.fields = append([]string{field}, fields...)
  179. return _u
  180. }
  181. // Save executes the query and returns the updated Setting entity.
  182. func (_u *SettingUpdateOne) Save(ctx context.Context) (*Setting, error) {
  183. _u.defaults()
  184. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  185. }
  186. // SaveX is like Save, but panics if an error occurs.
  187. func (_u *SettingUpdateOne) SaveX(ctx context.Context) *Setting {
  188. node, err := _u.Save(ctx)
  189. if err != nil {
  190. panic(err)
  191. }
  192. return node
  193. }
  194. // Exec executes the query on the entity.
  195. func (_u *SettingUpdateOne) Exec(ctx context.Context) error {
  196. _, err := _u.Save(ctx)
  197. return err
  198. }
  199. // ExecX is like Exec, but panics if an error occurs.
  200. func (_u *SettingUpdateOne) ExecX(ctx context.Context) {
  201. if err := _u.Exec(ctx); err != nil {
  202. panic(err)
  203. }
  204. }
  205. // defaults sets the default values of the builder before save.
  206. func (_u *SettingUpdateOne) defaults() {
  207. if _, ok := _u.mutation.UpdatedAt(); !ok {
  208. v := setting.UpdateDefaultUpdatedAt()
  209. _u.mutation.SetUpdatedAt(v)
  210. }
  211. }
  212. // check runs all checks and user-defined validators on the builder.
  213. func (_u *SettingUpdateOne) check() error {
  214. if v, ok := _u.mutation.Key(); ok {
  215. if err := setting.KeyValidator(v); err != nil {
  216. return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "Setting.key": %w`, err)}
  217. }
  218. }
  219. return nil
  220. }
  221. func (_u *SettingUpdateOne) sqlSave(ctx context.Context) (_node *Setting, err error) {
  222. if err := _u.check(); err != nil {
  223. return _node, err
  224. }
  225. _spec := sqlgraph.NewUpdateSpec(setting.Table, setting.Columns, sqlgraph.NewFieldSpec(setting.FieldID, field.TypeInt64))
  226. id, ok := _u.mutation.ID()
  227. if !ok {
  228. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Setting.id" for update`)}
  229. }
  230. _spec.Node.ID.Value = id
  231. if fields := _u.fields; len(fields) > 0 {
  232. _spec.Node.Columns = make([]string, 0, len(fields))
  233. _spec.Node.Columns = append(_spec.Node.Columns, setting.FieldID)
  234. for _, f := range fields {
  235. if !setting.ValidColumn(f) {
  236. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  237. }
  238. if f != setting.FieldID {
  239. _spec.Node.Columns = append(_spec.Node.Columns, f)
  240. }
  241. }
  242. }
  243. if ps := _u.mutation.predicates; len(ps) > 0 {
  244. _spec.Predicate = func(selector *sql.Selector) {
  245. for i := range ps {
  246. ps[i](selector)
  247. }
  248. }
  249. }
  250. if value, ok := _u.mutation.Key(); ok {
  251. _spec.SetField(setting.FieldKey, field.TypeString, value)
  252. }
  253. if value, ok := _u.mutation.Value(); ok {
  254. _spec.SetField(setting.FieldValue, field.TypeString, value)
  255. }
  256. if value, ok := _u.mutation.UpdatedAt(); ok {
  257. _spec.SetField(setting.FieldUpdatedAt, field.TypeTime, value)
  258. }
  259. _node = &Setting{config: _u.config}
  260. _spec.Assign = _node.assignValues
  261. _spec.ScanValues = _node.scanValues
  262. if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
  263. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  264. err = &NotFoundError{setting.Label}
  265. } else if sqlgraph.IsConstraintError(err) {
  266. err = &ConstraintError{msg: err.Error(), wrap: err}
  267. }
  268. return nil, err
  269. }
  270. _u.mutation.done = true
  271. return _node, nil
  272. }