user_update.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "code.osinet.fr/fgm/entdemo/ent/car"
  7. "code.osinet.fr/fgm/entdemo/ent/group"
  8. "code.osinet.fr/fgm/entdemo/ent/predicate"
  9. "code.osinet.fr/fgm/entdemo/ent/user"
  10. "github.com/facebookincubator/ent/dialect/sql"
  11. "github.com/facebookincubator/ent/dialect/sql/sqlgraph"
  12. "github.com/facebookincubator/ent/schema/field"
  13. )
  14. // UserUpdate is the builder for updating User entities.
  15. type UserUpdate struct {
  16. config
  17. hooks []Hook
  18. mutation *UserMutation
  19. predicates []predicate.User
  20. }
  21. // Where adds a new predicate for the builder.
  22. func (uu *UserUpdate) Where(ps ...predicate.User) *UserUpdate {
  23. uu.predicates = append(uu.predicates, ps...)
  24. return uu
  25. }
  26. // SetAge sets the age field.
  27. func (uu *UserUpdate) SetAge(i int) *UserUpdate {
  28. uu.mutation.ResetAge()
  29. uu.mutation.SetAge(i)
  30. return uu
  31. }
  32. // AddAge adds i to age.
  33. func (uu *UserUpdate) AddAge(i int) *UserUpdate {
  34. uu.mutation.AddAge(i)
  35. return uu
  36. }
  37. // SetName sets the name field.
  38. func (uu *UserUpdate) SetName(s string) *UserUpdate {
  39. uu.mutation.SetName(s)
  40. return uu
  41. }
  42. // SetNillableName sets the name field if the given value is not nil.
  43. func (uu *UserUpdate) SetNillableName(s *string) *UserUpdate {
  44. if s != nil {
  45. uu.SetName(*s)
  46. }
  47. return uu
  48. }
  49. // AddCarIDs adds the cars edge to Car by ids.
  50. func (uu *UserUpdate) AddCarIDs(ids ...int) *UserUpdate {
  51. uu.mutation.AddCarIDs(ids...)
  52. return uu
  53. }
  54. // AddCars adds the cars edges to Car.
  55. func (uu *UserUpdate) AddCars(c ...*Car) *UserUpdate {
  56. ids := make([]int, len(c))
  57. for i := range c {
  58. ids[i] = c[i].ID
  59. }
  60. return uu.AddCarIDs(ids...)
  61. }
  62. // AddGroupIDs adds the groups edge to Group by ids.
  63. func (uu *UserUpdate) AddGroupIDs(ids ...int) *UserUpdate {
  64. uu.mutation.AddGroupIDs(ids...)
  65. return uu
  66. }
  67. // AddGroups adds the groups edges to Group.
  68. func (uu *UserUpdate) AddGroups(g ...*Group) *UserUpdate {
  69. ids := make([]int, len(g))
  70. for i := range g {
  71. ids[i] = g[i].ID
  72. }
  73. return uu.AddGroupIDs(ids...)
  74. }
  75. // RemoveCarIDs removes the cars edge to Car by ids.
  76. func (uu *UserUpdate) RemoveCarIDs(ids ...int) *UserUpdate {
  77. uu.mutation.RemoveCarIDs(ids...)
  78. return uu
  79. }
  80. // RemoveCars removes cars edges to Car.
  81. func (uu *UserUpdate) RemoveCars(c ...*Car) *UserUpdate {
  82. ids := make([]int, len(c))
  83. for i := range c {
  84. ids[i] = c[i].ID
  85. }
  86. return uu.RemoveCarIDs(ids...)
  87. }
  88. // RemoveGroupIDs removes the groups edge to Group by ids.
  89. func (uu *UserUpdate) RemoveGroupIDs(ids ...int) *UserUpdate {
  90. uu.mutation.RemoveGroupIDs(ids...)
  91. return uu
  92. }
  93. // RemoveGroups removes groups edges to Group.
  94. func (uu *UserUpdate) RemoveGroups(g ...*Group) *UserUpdate {
  95. ids := make([]int, len(g))
  96. for i := range g {
  97. ids[i] = g[i].ID
  98. }
  99. return uu.RemoveGroupIDs(ids...)
  100. }
  101. // Save executes the query and returns the number of rows/vertices matched by this operation.
  102. func (uu *UserUpdate) Save(ctx context.Context) (int, error) {
  103. if v, ok := uu.mutation.Age(); ok {
  104. if err := user.AgeValidator(v); err != nil {
  105. return 0, fmt.Errorf("ent: validator failed for field \"age\": %v", err)
  106. }
  107. }
  108. var (
  109. err error
  110. affected int
  111. )
  112. if len(uu.hooks) == 0 {
  113. affected, err = uu.sqlSave(ctx)
  114. } else {
  115. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  116. mutation, ok := m.(*UserMutation)
  117. if !ok {
  118. return nil, fmt.Errorf("unexpected mutation type %T", m)
  119. }
  120. uu.mutation = mutation
  121. affected, err = uu.sqlSave(ctx)
  122. return affected, err
  123. })
  124. for i := len(uu.hooks) - 1; i >= 0; i-- {
  125. mut = uu.hooks[i](mut)
  126. }
  127. if _, err := mut.Mutate(ctx, uu.mutation); err != nil {
  128. return 0, err
  129. }
  130. }
  131. return affected, err
  132. }
  133. // SaveX is like Save, but panics if an error occurs.
  134. func (uu *UserUpdate) SaveX(ctx context.Context) int {
  135. affected, err := uu.Save(ctx)
  136. if err != nil {
  137. panic(err)
  138. }
  139. return affected
  140. }
  141. // Exec executes the query.
  142. func (uu *UserUpdate) Exec(ctx context.Context) error {
  143. _, err := uu.Save(ctx)
  144. return err
  145. }
  146. // ExecX is like Exec, but panics if an error occurs.
  147. func (uu *UserUpdate) ExecX(ctx context.Context) {
  148. if err := uu.Exec(ctx); err != nil {
  149. panic(err)
  150. }
  151. }
  152. func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
  153. _spec := &sqlgraph.UpdateSpec{
  154. Node: &sqlgraph.NodeSpec{
  155. Table: user.Table,
  156. Columns: user.Columns,
  157. ID: &sqlgraph.FieldSpec{
  158. Type: field.TypeInt,
  159. Column: user.FieldID,
  160. },
  161. },
  162. }
  163. if ps := uu.predicates; len(ps) > 0 {
  164. _spec.Predicate = func(selector *sql.Selector) {
  165. for i := range ps {
  166. ps[i](selector)
  167. }
  168. }
  169. }
  170. if value, ok := uu.mutation.Age(); ok {
  171. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  172. Type: field.TypeInt,
  173. Value: value,
  174. Column: user.FieldAge,
  175. })
  176. }
  177. if value, ok := uu.mutation.AddedAge(); ok {
  178. _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
  179. Type: field.TypeInt,
  180. Value: value,
  181. Column: user.FieldAge,
  182. })
  183. }
  184. if value, ok := uu.mutation.Name(); ok {
  185. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  186. Type: field.TypeString,
  187. Value: value,
  188. Column: user.FieldName,
  189. })
  190. }
  191. if nodes := uu.mutation.RemovedCarsIDs(); len(nodes) > 0 {
  192. edge := &sqlgraph.EdgeSpec{
  193. Rel: sqlgraph.O2M,
  194. Inverse: false,
  195. Table: user.CarsTable,
  196. Columns: []string{user.CarsColumn},
  197. Bidi: false,
  198. Target: &sqlgraph.EdgeTarget{
  199. IDSpec: &sqlgraph.FieldSpec{
  200. Type: field.TypeInt,
  201. Column: car.FieldID,
  202. },
  203. },
  204. }
  205. for _, k := range nodes {
  206. edge.Target.Nodes = append(edge.Target.Nodes, k)
  207. }
  208. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  209. }
  210. if nodes := uu.mutation.CarsIDs(); len(nodes) > 0 {
  211. edge := &sqlgraph.EdgeSpec{
  212. Rel: sqlgraph.O2M,
  213. Inverse: false,
  214. Table: user.CarsTable,
  215. Columns: []string{user.CarsColumn},
  216. Bidi: false,
  217. Target: &sqlgraph.EdgeTarget{
  218. IDSpec: &sqlgraph.FieldSpec{
  219. Type: field.TypeInt,
  220. Column: car.FieldID,
  221. },
  222. },
  223. }
  224. for _, k := range nodes {
  225. edge.Target.Nodes = append(edge.Target.Nodes, k)
  226. }
  227. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  228. }
  229. if nodes := uu.mutation.RemovedGroupsIDs(); len(nodes) > 0 {
  230. edge := &sqlgraph.EdgeSpec{
  231. Rel: sqlgraph.M2M,
  232. Inverse: true,
  233. Table: user.GroupsTable,
  234. Columns: user.GroupsPrimaryKey,
  235. Bidi: false,
  236. Target: &sqlgraph.EdgeTarget{
  237. IDSpec: &sqlgraph.FieldSpec{
  238. Type: field.TypeInt,
  239. Column: group.FieldID,
  240. },
  241. },
  242. }
  243. for _, k := range nodes {
  244. edge.Target.Nodes = append(edge.Target.Nodes, k)
  245. }
  246. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  247. }
  248. if nodes := uu.mutation.GroupsIDs(); len(nodes) > 0 {
  249. edge := &sqlgraph.EdgeSpec{
  250. Rel: sqlgraph.M2M,
  251. Inverse: true,
  252. Table: user.GroupsTable,
  253. Columns: user.GroupsPrimaryKey,
  254. Bidi: false,
  255. Target: &sqlgraph.EdgeTarget{
  256. IDSpec: &sqlgraph.FieldSpec{
  257. Type: field.TypeInt,
  258. Column: group.FieldID,
  259. },
  260. },
  261. }
  262. for _, k := range nodes {
  263. edge.Target.Nodes = append(edge.Target.Nodes, k)
  264. }
  265. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  266. }
  267. if n, err = sqlgraph.UpdateNodes(ctx, uu.driver, _spec); err != nil {
  268. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  269. err = &NotFoundError{user.Label}
  270. } else if cerr, ok := isSQLConstraintError(err); ok {
  271. err = cerr
  272. }
  273. return 0, err
  274. }
  275. return n, nil
  276. }
  277. // UserUpdateOne is the builder for updating a single User entity.
  278. type UserUpdateOne struct {
  279. config
  280. hooks []Hook
  281. mutation *UserMutation
  282. }
  283. // SetAge sets the age field.
  284. func (uuo *UserUpdateOne) SetAge(i int) *UserUpdateOne {
  285. uuo.mutation.ResetAge()
  286. uuo.mutation.SetAge(i)
  287. return uuo
  288. }
  289. // AddAge adds i to age.
  290. func (uuo *UserUpdateOne) AddAge(i int) *UserUpdateOne {
  291. uuo.mutation.AddAge(i)
  292. return uuo
  293. }
  294. // SetName sets the name field.
  295. func (uuo *UserUpdateOne) SetName(s string) *UserUpdateOne {
  296. uuo.mutation.SetName(s)
  297. return uuo
  298. }
  299. // SetNillableName sets the name field if the given value is not nil.
  300. func (uuo *UserUpdateOne) SetNillableName(s *string) *UserUpdateOne {
  301. if s != nil {
  302. uuo.SetName(*s)
  303. }
  304. return uuo
  305. }
  306. // AddCarIDs adds the cars edge to Car by ids.
  307. func (uuo *UserUpdateOne) AddCarIDs(ids ...int) *UserUpdateOne {
  308. uuo.mutation.AddCarIDs(ids...)
  309. return uuo
  310. }
  311. // AddCars adds the cars edges to Car.
  312. func (uuo *UserUpdateOne) AddCars(c ...*Car) *UserUpdateOne {
  313. ids := make([]int, len(c))
  314. for i := range c {
  315. ids[i] = c[i].ID
  316. }
  317. return uuo.AddCarIDs(ids...)
  318. }
  319. // AddGroupIDs adds the groups edge to Group by ids.
  320. func (uuo *UserUpdateOne) AddGroupIDs(ids ...int) *UserUpdateOne {
  321. uuo.mutation.AddGroupIDs(ids...)
  322. return uuo
  323. }
  324. // AddGroups adds the groups edges to Group.
  325. func (uuo *UserUpdateOne) AddGroups(g ...*Group) *UserUpdateOne {
  326. ids := make([]int, len(g))
  327. for i := range g {
  328. ids[i] = g[i].ID
  329. }
  330. return uuo.AddGroupIDs(ids...)
  331. }
  332. // RemoveCarIDs removes the cars edge to Car by ids.
  333. func (uuo *UserUpdateOne) RemoveCarIDs(ids ...int) *UserUpdateOne {
  334. uuo.mutation.RemoveCarIDs(ids...)
  335. return uuo
  336. }
  337. // RemoveCars removes cars edges to Car.
  338. func (uuo *UserUpdateOne) RemoveCars(c ...*Car) *UserUpdateOne {
  339. ids := make([]int, len(c))
  340. for i := range c {
  341. ids[i] = c[i].ID
  342. }
  343. return uuo.RemoveCarIDs(ids...)
  344. }
  345. // RemoveGroupIDs removes the groups edge to Group by ids.
  346. func (uuo *UserUpdateOne) RemoveGroupIDs(ids ...int) *UserUpdateOne {
  347. uuo.mutation.RemoveGroupIDs(ids...)
  348. return uuo
  349. }
  350. // RemoveGroups removes groups edges to Group.
  351. func (uuo *UserUpdateOne) RemoveGroups(g ...*Group) *UserUpdateOne {
  352. ids := make([]int, len(g))
  353. for i := range g {
  354. ids[i] = g[i].ID
  355. }
  356. return uuo.RemoveGroupIDs(ids...)
  357. }
  358. // Save executes the query and returns the updated entity.
  359. func (uuo *UserUpdateOne) Save(ctx context.Context) (*User, error) {
  360. if v, ok := uuo.mutation.Age(); ok {
  361. if err := user.AgeValidator(v); err != nil {
  362. return nil, fmt.Errorf("ent: validator failed for field \"age\": %v", err)
  363. }
  364. }
  365. var (
  366. err error
  367. node *User
  368. )
  369. if len(uuo.hooks) == 0 {
  370. node, err = uuo.sqlSave(ctx)
  371. } else {
  372. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  373. mutation, ok := m.(*UserMutation)
  374. if !ok {
  375. return nil, fmt.Errorf("unexpected mutation type %T", m)
  376. }
  377. uuo.mutation = mutation
  378. node, err = uuo.sqlSave(ctx)
  379. return node, err
  380. })
  381. for i := len(uuo.hooks) - 1; i >= 0; i-- {
  382. mut = uuo.hooks[i](mut)
  383. }
  384. if _, err := mut.Mutate(ctx, uuo.mutation); err != nil {
  385. return nil, err
  386. }
  387. }
  388. return node, err
  389. }
  390. // SaveX is like Save, but panics if an error occurs.
  391. func (uuo *UserUpdateOne) SaveX(ctx context.Context) *User {
  392. u, err := uuo.Save(ctx)
  393. if err != nil {
  394. panic(err)
  395. }
  396. return u
  397. }
  398. // Exec executes the query on the entity.
  399. func (uuo *UserUpdateOne) Exec(ctx context.Context) error {
  400. _, err := uuo.Save(ctx)
  401. return err
  402. }
  403. // ExecX is like Exec, but panics if an error occurs.
  404. func (uuo *UserUpdateOne) ExecX(ctx context.Context) {
  405. if err := uuo.Exec(ctx); err != nil {
  406. panic(err)
  407. }
  408. }
  409. func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
  410. _spec := &sqlgraph.UpdateSpec{
  411. Node: &sqlgraph.NodeSpec{
  412. Table: user.Table,
  413. Columns: user.Columns,
  414. ID: &sqlgraph.FieldSpec{
  415. Type: field.TypeInt,
  416. Column: user.FieldID,
  417. },
  418. },
  419. }
  420. id, ok := uuo.mutation.ID()
  421. if !ok {
  422. return nil, fmt.Errorf("missing User.ID for update")
  423. }
  424. _spec.Node.ID.Value = id
  425. if value, ok := uuo.mutation.Age(); ok {
  426. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  427. Type: field.TypeInt,
  428. Value: value,
  429. Column: user.FieldAge,
  430. })
  431. }
  432. if value, ok := uuo.mutation.AddedAge(); ok {
  433. _spec.Fields.Add = append(_spec.Fields.Add, &sqlgraph.FieldSpec{
  434. Type: field.TypeInt,
  435. Value: value,
  436. Column: user.FieldAge,
  437. })
  438. }
  439. if value, ok := uuo.mutation.Name(); ok {
  440. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  441. Type: field.TypeString,
  442. Value: value,
  443. Column: user.FieldName,
  444. })
  445. }
  446. if nodes := uuo.mutation.RemovedCarsIDs(); len(nodes) > 0 {
  447. edge := &sqlgraph.EdgeSpec{
  448. Rel: sqlgraph.O2M,
  449. Inverse: false,
  450. Table: user.CarsTable,
  451. Columns: []string{user.CarsColumn},
  452. Bidi: false,
  453. Target: &sqlgraph.EdgeTarget{
  454. IDSpec: &sqlgraph.FieldSpec{
  455. Type: field.TypeInt,
  456. Column: car.FieldID,
  457. },
  458. },
  459. }
  460. for _, k := range nodes {
  461. edge.Target.Nodes = append(edge.Target.Nodes, k)
  462. }
  463. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  464. }
  465. if nodes := uuo.mutation.CarsIDs(); len(nodes) > 0 {
  466. edge := &sqlgraph.EdgeSpec{
  467. Rel: sqlgraph.O2M,
  468. Inverse: false,
  469. Table: user.CarsTable,
  470. Columns: []string{user.CarsColumn},
  471. Bidi: false,
  472. Target: &sqlgraph.EdgeTarget{
  473. IDSpec: &sqlgraph.FieldSpec{
  474. Type: field.TypeInt,
  475. Column: car.FieldID,
  476. },
  477. },
  478. }
  479. for _, k := range nodes {
  480. edge.Target.Nodes = append(edge.Target.Nodes, k)
  481. }
  482. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  483. }
  484. if nodes := uuo.mutation.RemovedGroupsIDs(); len(nodes) > 0 {
  485. edge := &sqlgraph.EdgeSpec{
  486. Rel: sqlgraph.M2M,
  487. Inverse: true,
  488. Table: user.GroupsTable,
  489. Columns: user.GroupsPrimaryKey,
  490. Bidi: false,
  491. Target: &sqlgraph.EdgeTarget{
  492. IDSpec: &sqlgraph.FieldSpec{
  493. Type: field.TypeInt,
  494. Column: group.FieldID,
  495. },
  496. },
  497. }
  498. for _, k := range nodes {
  499. edge.Target.Nodes = append(edge.Target.Nodes, k)
  500. }
  501. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  502. }
  503. if nodes := uuo.mutation.GroupsIDs(); len(nodes) > 0 {
  504. edge := &sqlgraph.EdgeSpec{
  505. Rel: sqlgraph.M2M,
  506. Inverse: true,
  507. Table: user.GroupsTable,
  508. Columns: user.GroupsPrimaryKey,
  509. Bidi: false,
  510. Target: &sqlgraph.EdgeTarget{
  511. IDSpec: &sqlgraph.FieldSpec{
  512. Type: field.TypeInt,
  513. Column: group.FieldID,
  514. },
  515. },
  516. }
  517. for _, k := range nodes {
  518. edge.Target.Nodes = append(edge.Target.Nodes, k)
  519. }
  520. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  521. }
  522. u = &User{config: uuo.config}
  523. _spec.Assign = u.assignValues
  524. _spec.ScanValues = u.scanValues()
  525. if err = sqlgraph.UpdateNode(ctx, uuo.driver, _spec); err != nil {
  526. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  527. err = &NotFoundError{user.Label}
  528. } else if cerr, ok := isSQLConstraintError(err); ok {
  529. err = cerr
  530. }
  531. return nil, err
  532. }
  533. return u, nil
  534. }