accountgroup_update.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "entgo.io/ent/dialect/sql"
  8. "entgo.io/ent/dialect/sql/sqlgraph"
  9. "entgo.io/ent/schema/field"
  10. "github.com/Wei-Shaw/sub2api/ent/account"
  11. "github.com/Wei-Shaw/sub2api/ent/accountgroup"
  12. "github.com/Wei-Shaw/sub2api/ent/group"
  13. "github.com/Wei-Shaw/sub2api/ent/predicate"
  14. )
  15. // AccountGroupUpdate is the builder for updating AccountGroup entities.
  16. type AccountGroupUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *AccountGroupMutation
  20. }
  21. // Where appends a list predicates to the AccountGroupUpdate builder.
  22. func (_u *AccountGroupUpdate) Where(ps ...predicate.AccountGroup) *AccountGroupUpdate {
  23. _u.mutation.Where(ps...)
  24. return _u
  25. }
  26. // SetAccountID sets the "account_id" field.
  27. func (_u *AccountGroupUpdate) SetAccountID(v int64) *AccountGroupUpdate {
  28. _u.mutation.SetAccountID(v)
  29. return _u
  30. }
  31. // SetNillableAccountID sets the "account_id" field if the given value is not nil.
  32. func (_u *AccountGroupUpdate) SetNillableAccountID(v *int64) *AccountGroupUpdate {
  33. if v != nil {
  34. _u.SetAccountID(*v)
  35. }
  36. return _u
  37. }
  38. // SetGroupID sets the "group_id" field.
  39. func (_u *AccountGroupUpdate) SetGroupID(v int64) *AccountGroupUpdate {
  40. _u.mutation.SetGroupID(v)
  41. return _u
  42. }
  43. // SetNillableGroupID sets the "group_id" field if the given value is not nil.
  44. func (_u *AccountGroupUpdate) SetNillableGroupID(v *int64) *AccountGroupUpdate {
  45. if v != nil {
  46. _u.SetGroupID(*v)
  47. }
  48. return _u
  49. }
  50. // SetPriority sets the "priority" field.
  51. func (_u *AccountGroupUpdate) SetPriority(v int) *AccountGroupUpdate {
  52. _u.mutation.ResetPriority()
  53. _u.mutation.SetPriority(v)
  54. return _u
  55. }
  56. // SetNillablePriority sets the "priority" field if the given value is not nil.
  57. func (_u *AccountGroupUpdate) SetNillablePriority(v *int) *AccountGroupUpdate {
  58. if v != nil {
  59. _u.SetPriority(*v)
  60. }
  61. return _u
  62. }
  63. // AddPriority adds value to the "priority" field.
  64. func (_u *AccountGroupUpdate) AddPriority(v int) *AccountGroupUpdate {
  65. _u.mutation.AddPriority(v)
  66. return _u
  67. }
  68. // SetAccount sets the "account" edge to the Account entity.
  69. func (_u *AccountGroupUpdate) SetAccount(v *Account) *AccountGroupUpdate {
  70. return _u.SetAccountID(v.ID)
  71. }
  72. // SetGroup sets the "group" edge to the Group entity.
  73. func (_u *AccountGroupUpdate) SetGroup(v *Group) *AccountGroupUpdate {
  74. return _u.SetGroupID(v.ID)
  75. }
  76. // Mutation returns the AccountGroupMutation object of the builder.
  77. func (_u *AccountGroupUpdate) Mutation() *AccountGroupMutation {
  78. return _u.mutation
  79. }
  80. // ClearAccount clears the "account" edge to the Account entity.
  81. func (_u *AccountGroupUpdate) ClearAccount() *AccountGroupUpdate {
  82. _u.mutation.ClearAccount()
  83. return _u
  84. }
  85. // ClearGroup clears the "group" edge to the Group entity.
  86. func (_u *AccountGroupUpdate) ClearGroup() *AccountGroupUpdate {
  87. _u.mutation.ClearGroup()
  88. return _u
  89. }
  90. // Save executes the query and returns the number of nodes affected by the update operation.
  91. func (_u *AccountGroupUpdate) Save(ctx context.Context) (int, error) {
  92. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  93. }
  94. // SaveX is like Save, but panics if an error occurs.
  95. func (_u *AccountGroupUpdate) SaveX(ctx context.Context) int {
  96. affected, err := _u.Save(ctx)
  97. if err != nil {
  98. panic(err)
  99. }
  100. return affected
  101. }
  102. // Exec executes the query.
  103. func (_u *AccountGroupUpdate) Exec(ctx context.Context) error {
  104. _, err := _u.Save(ctx)
  105. return err
  106. }
  107. // ExecX is like Exec, but panics if an error occurs.
  108. func (_u *AccountGroupUpdate) ExecX(ctx context.Context) {
  109. if err := _u.Exec(ctx); err != nil {
  110. panic(err)
  111. }
  112. }
  113. // check runs all checks and user-defined validators on the builder.
  114. func (_u *AccountGroupUpdate) check() error {
  115. if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
  116. return errors.New(`ent: clearing a required unique edge "AccountGroup.account"`)
  117. }
  118. if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
  119. return errors.New(`ent: clearing a required unique edge "AccountGroup.group"`)
  120. }
  121. return nil
  122. }
  123. func (_u *AccountGroupUpdate) sqlSave(ctx context.Context) (_node int, err error) {
  124. if err := _u.check(); err != nil {
  125. return _node, err
  126. }
  127. _spec := sqlgraph.NewUpdateSpec(accountgroup.Table, accountgroup.Columns, sqlgraph.NewFieldSpec(accountgroup.FieldAccountID, field.TypeInt64), sqlgraph.NewFieldSpec(accountgroup.FieldGroupID, field.TypeInt64))
  128. if ps := _u.mutation.predicates; len(ps) > 0 {
  129. _spec.Predicate = func(selector *sql.Selector) {
  130. for i := range ps {
  131. ps[i](selector)
  132. }
  133. }
  134. }
  135. if value, ok := _u.mutation.Priority(); ok {
  136. _spec.SetField(accountgroup.FieldPriority, field.TypeInt, value)
  137. }
  138. if value, ok := _u.mutation.AddedPriority(); ok {
  139. _spec.AddField(accountgroup.FieldPriority, field.TypeInt, value)
  140. }
  141. if _u.mutation.AccountCleared() {
  142. edge := &sqlgraph.EdgeSpec{
  143. Rel: sqlgraph.M2O,
  144. Inverse: false,
  145. Table: accountgroup.AccountTable,
  146. Columns: []string{accountgroup.AccountColumn},
  147. Bidi: false,
  148. Target: &sqlgraph.EdgeTarget{
  149. IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
  150. },
  151. }
  152. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  153. }
  154. if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
  155. edge := &sqlgraph.EdgeSpec{
  156. Rel: sqlgraph.M2O,
  157. Inverse: false,
  158. Table: accountgroup.AccountTable,
  159. Columns: []string{accountgroup.AccountColumn},
  160. Bidi: false,
  161. Target: &sqlgraph.EdgeTarget{
  162. IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
  163. },
  164. }
  165. for _, k := range nodes {
  166. edge.Target.Nodes = append(edge.Target.Nodes, k)
  167. }
  168. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  169. }
  170. if _u.mutation.GroupCleared() {
  171. edge := &sqlgraph.EdgeSpec{
  172. Rel: sqlgraph.M2O,
  173. Inverse: false,
  174. Table: accountgroup.GroupTable,
  175. Columns: []string{accountgroup.GroupColumn},
  176. Bidi: false,
  177. Target: &sqlgraph.EdgeTarget{
  178. IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
  179. },
  180. }
  181. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  182. }
  183. if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
  184. edge := &sqlgraph.EdgeSpec{
  185. Rel: sqlgraph.M2O,
  186. Inverse: false,
  187. Table: accountgroup.GroupTable,
  188. Columns: []string{accountgroup.GroupColumn},
  189. Bidi: false,
  190. Target: &sqlgraph.EdgeTarget{
  191. IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
  192. },
  193. }
  194. for _, k := range nodes {
  195. edge.Target.Nodes = append(edge.Target.Nodes, k)
  196. }
  197. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  198. }
  199. if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
  200. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  201. err = &NotFoundError{accountgroup.Label}
  202. } else if sqlgraph.IsConstraintError(err) {
  203. err = &ConstraintError{msg: err.Error(), wrap: err}
  204. }
  205. return 0, err
  206. }
  207. _u.mutation.done = true
  208. return _node, nil
  209. }
  210. // AccountGroupUpdateOne is the builder for updating a single AccountGroup entity.
  211. type AccountGroupUpdateOne struct {
  212. config
  213. fields []string
  214. hooks []Hook
  215. mutation *AccountGroupMutation
  216. }
  217. // SetAccountID sets the "account_id" field.
  218. func (_u *AccountGroupUpdateOne) SetAccountID(v int64) *AccountGroupUpdateOne {
  219. _u.mutation.SetAccountID(v)
  220. return _u
  221. }
  222. // SetNillableAccountID sets the "account_id" field if the given value is not nil.
  223. func (_u *AccountGroupUpdateOne) SetNillableAccountID(v *int64) *AccountGroupUpdateOne {
  224. if v != nil {
  225. _u.SetAccountID(*v)
  226. }
  227. return _u
  228. }
  229. // SetGroupID sets the "group_id" field.
  230. func (_u *AccountGroupUpdateOne) SetGroupID(v int64) *AccountGroupUpdateOne {
  231. _u.mutation.SetGroupID(v)
  232. return _u
  233. }
  234. // SetNillableGroupID sets the "group_id" field if the given value is not nil.
  235. func (_u *AccountGroupUpdateOne) SetNillableGroupID(v *int64) *AccountGroupUpdateOne {
  236. if v != nil {
  237. _u.SetGroupID(*v)
  238. }
  239. return _u
  240. }
  241. // SetPriority sets the "priority" field.
  242. func (_u *AccountGroupUpdateOne) SetPriority(v int) *AccountGroupUpdateOne {
  243. _u.mutation.ResetPriority()
  244. _u.mutation.SetPriority(v)
  245. return _u
  246. }
  247. // SetNillablePriority sets the "priority" field if the given value is not nil.
  248. func (_u *AccountGroupUpdateOne) SetNillablePriority(v *int) *AccountGroupUpdateOne {
  249. if v != nil {
  250. _u.SetPriority(*v)
  251. }
  252. return _u
  253. }
  254. // AddPriority adds value to the "priority" field.
  255. func (_u *AccountGroupUpdateOne) AddPriority(v int) *AccountGroupUpdateOne {
  256. _u.mutation.AddPriority(v)
  257. return _u
  258. }
  259. // SetAccount sets the "account" edge to the Account entity.
  260. func (_u *AccountGroupUpdateOne) SetAccount(v *Account) *AccountGroupUpdateOne {
  261. return _u.SetAccountID(v.ID)
  262. }
  263. // SetGroup sets the "group" edge to the Group entity.
  264. func (_u *AccountGroupUpdateOne) SetGroup(v *Group) *AccountGroupUpdateOne {
  265. return _u.SetGroupID(v.ID)
  266. }
  267. // Mutation returns the AccountGroupMutation object of the builder.
  268. func (_u *AccountGroupUpdateOne) Mutation() *AccountGroupMutation {
  269. return _u.mutation
  270. }
  271. // ClearAccount clears the "account" edge to the Account entity.
  272. func (_u *AccountGroupUpdateOne) ClearAccount() *AccountGroupUpdateOne {
  273. _u.mutation.ClearAccount()
  274. return _u
  275. }
  276. // ClearGroup clears the "group" edge to the Group entity.
  277. func (_u *AccountGroupUpdateOne) ClearGroup() *AccountGroupUpdateOne {
  278. _u.mutation.ClearGroup()
  279. return _u
  280. }
  281. // Where appends a list predicates to the AccountGroupUpdate builder.
  282. func (_u *AccountGroupUpdateOne) Where(ps ...predicate.AccountGroup) *AccountGroupUpdateOne {
  283. _u.mutation.Where(ps...)
  284. return _u
  285. }
  286. // Select allows selecting one or more fields (columns) of the returned entity.
  287. // The default is selecting all fields defined in the entity schema.
  288. func (_u *AccountGroupUpdateOne) Select(field string, fields ...string) *AccountGroupUpdateOne {
  289. _u.fields = append([]string{field}, fields...)
  290. return _u
  291. }
  292. // Save executes the query and returns the updated AccountGroup entity.
  293. func (_u *AccountGroupUpdateOne) Save(ctx context.Context) (*AccountGroup, error) {
  294. return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
  295. }
  296. // SaveX is like Save, but panics if an error occurs.
  297. func (_u *AccountGroupUpdateOne) SaveX(ctx context.Context) *AccountGroup {
  298. node, err := _u.Save(ctx)
  299. if err != nil {
  300. panic(err)
  301. }
  302. return node
  303. }
  304. // Exec executes the query on the entity.
  305. func (_u *AccountGroupUpdateOne) Exec(ctx context.Context) error {
  306. _, err := _u.Save(ctx)
  307. return err
  308. }
  309. // ExecX is like Exec, but panics if an error occurs.
  310. func (_u *AccountGroupUpdateOne) ExecX(ctx context.Context) {
  311. if err := _u.Exec(ctx); err != nil {
  312. panic(err)
  313. }
  314. }
  315. // check runs all checks and user-defined validators on the builder.
  316. func (_u *AccountGroupUpdateOne) check() error {
  317. if _u.mutation.AccountCleared() && len(_u.mutation.AccountIDs()) > 0 {
  318. return errors.New(`ent: clearing a required unique edge "AccountGroup.account"`)
  319. }
  320. if _u.mutation.GroupCleared() && len(_u.mutation.GroupIDs()) > 0 {
  321. return errors.New(`ent: clearing a required unique edge "AccountGroup.group"`)
  322. }
  323. return nil
  324. }
  325. func (_u *AccountGroupUpdateOne) sqlSave(ctx context.Context) (_node *AccountGroup, err error) {
  326. if err := _u.check(); err != nil {
  327. return _node, err
  328. }
  329. _spec := sqlgraph.NewUpdateSpec(accountgroup.Table, accountgroup.Columns, sqlgraph.NewFieldSpec(accountgroup.FieldAccountID, field.TypeInt64), sqlgraph.NewFieldSpec(accountgroup.FieldGroupID, field.TypeInt64))
  330. if id, ok := _u.mutation.AccountID(); !ok {
  331. return nil, &ValidationError{Name: "account_id", err: errors.New(`ent: missing "AccountGroup.account_id" for update`)}
  332. } else {
  333. _spec.Node.CompositeID[0].Value = id
  334. }
  335. if id, ok := _u.mutation.GroupID(); !ok {
  336. return nil, &ValidationError{Name: "group_id", err: errors.New(`ent: missing "AccountGroup.group_id" for update`)}
  337. } else {
  338. _spec.Node.CompositeID[1].Value = id
  339. }
  340. if fields := _u.fields; len(fields) > 0 {
  341. _spec.Node.Columns = make([]string, len(fields))
  342. for i, f := range fields {
  343. if !accountgroup.ValidColumn(f) {
  344. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  345. }
  346. _spec.Node.Columns[i] = f
  347. }
  348. }
  349. if ps := _u.mutation.predicates; len(ps) > 0 {
  350. _spec.Predicate = func(selector *sql.Selector) {
  351. for i := range ps {
  352. ps[i](selector)
  353. }
  354. }
  355. }
  356. if value, ok := _u.mutation.Priority(); ok {
  357. _spec.SetField(accountgroup.FieldPriority, field.TypeInt, value)
  358. }
  359. if value, ok := _u.mutation.AddedPriority(); ok {
  360. _spec.AddField(accountgroup.FieldPriority, field.TypeInt, value)
  361. }
  362. if _u.mutation.AccountCleared() {
  363. edge := &sqlgraph.EdgeSpec{
  364. Rel: sqlgraph.M2O,
  365. Inverse: false,
  366. Table: accountgroup.AccountTable,
  367. Columns: []string{accountgroup.AccountColumn},
  368. Bidi: false,
  369. Target: &sqlgraph.EdgeTarget{
  370. IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
  371. },
  372. }
  373. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  374. }
  375. if nodes := _u.mutation.AccountIDs(); len(nodes) > 0 {
  376. edge := &sqlgraph.EdgeSpec{
  377. Rel: sqlgraph.M2O,
  378. Inverse: false,
  379. Table: accountgroup.AccountTable,
  380. Columns: []string{accountgroup.AccountColumn},
  381. Bidi: false,
  382. Target: &sqlgraph.EdgeTarget{
  383. IDSpec: sqlgraph.NewFieldSpec(account.FieldID, field.TypeInt64),
  384. },
  385. }
  386. for _, k := range nodes {
  387. edge.Target.Nodes = append(edge.Target.Nodes, k)
  388. }
  389. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  390. }
  391. if _u.mutation.GroupCleared() {
  392. edge := &sqlgraph.EdgeSpec{
  393. Rel: sqlgraph.M2O,
  394. Inverse: false,
  395. Table: accountgroup.GroupTable,
  396. Columns: []string{accountgroup.GroupColumn},
  397. Bidi: false,
  398. Target: &sqlgraph.EdgeTarget{
  399. IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
  400. },
  401. }
  402. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  403. }
  404. if nodes := _u.mutation.GroupIDs(); len(nodes) > 0 {
  405. edge := &sqlgraph.EdgeSpec{
  406. Rel: sqlgraph.M2O,
  407. Inverse: false,
  408. Table: accountgroup.GroupTable,
  409. Columns: []string{accountgroup.GroupColumn},
  410. Bidi: false,
  411. Target: &sqlgraph.EdgeTarget{
  412. IDSpec: sqlgraph.NewFieldSpec(group.FieldID, field.TypeInt64),
  413. },
  414. }
  415. for _, k := range nodes {
  416. edge.Target.Nodes = append(edge.Target.Nodes, k)
  417. }
  418. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  419. }
  420. _node = &AccountGroup{config: _u.config}
  421. _spec.Assign = _node.assignValues
  422. _spec.ScanValues = _node.scanValues
  423. if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
  424. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  425. err = &NotFoundError{accountgroup.Label}
  426. } else if sqlgraph.IsConstraintError(err) {
  427. err = &ConstraintError{msg: err.Error(), wrap: err}
  428. }
  429. return nil, err
  430. }
  431. _u.mutation.done = true
  432. return _node, nil
  433. }