car_update.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "fmt"
  6. "time"
  7. "code.osinet.fr/fgm/entdemo/ent/car"
  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. // CarUpdate is the builder for updating Car entities.
  15. type CarUpdate struct {
  16. config
  17. hooks []Hook
  18. mutation *CarMutation
  19. predicates []predicate.Car
  20. }
  21. // Where adds a new predicate for the builder.
  22. func (cu *CarUpdate) Where(ps ...predicate.Car) *CarUpdate {
  23. cu.predicates = append(cu.predicates, ps...)
  24. return cu
  25. }
  26. // SetModel sets the model field.
  27. func (cu *CarUpdate) SetModel(s string) *CarUpdate {
  28. cu.mutation.SetModel(s)
  29. return cu
  30. }
  31. // SetRegisteredAt sets the registered_at field.
  32. func (cu *CarUpdate) SetRegisteredAt(t time.Time) *CarUpdate {
  33. cu.mutation.SetRegisteredAt(t)
  34. return cu
  35. }
  36. // SetOwnerID sets the owner edge to User by id.
  37. func (cu *CarUpdate) SetOwnerID(id int) *CarUpdate {
  38. cu.mutation.SetOwnerID(id)
  39. return cu
  40. }
  41. // SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
  42. func (cu *CarUpdate) SetNillableOwnerID(id *int) *CarUpdate {
  43. if id != nil {
  44. cu = cu.SetOwnerID(*id)
  45. }
  46. return cu
  47. }
  48. // SetOwner sets the owner edge to User.
  49. func (cu *CarUpdate) SetOwner(u *User) *CarUpdate {
  50. return cu.SetOwnerID(u.ID)
  51. }
  52. // ClearOwner clears the owner edge to User.
  53. func (cu *CarUpdate) ClearOwner() *CarUpdate {
  54. cu.mutation.ClearOwner()
  55. return cu
  56. }
  57. // Save executes the query and returns the number of rows/vertices matched by this operation.
  58. func (cu *CarUpdate) Save(ctx context.Context) (int, error) {
  59. var (
  60. err error
  61. affected int
  62. )
  63. if len(cu.hooks) == 0 {
  64. affected, err = cu.sqlSave(ctx)
  65. } else {
  66. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  67. mutation, ok := m.(*CarMutation)
  68. if !ok {
  69. return nil, fmt.Errorf("unexpected mutation type %T", m)
  70. }
  71. cu.mutation = mutation
  72. affected, err = cu.sqlSave(ctx)
  73. return affected, err
  74. })
  75. for i := len(cu.hooks) - 1; i >= 0; i-- {
  76. mut = cu.hooks[i](mut)
  77. }
  78. if _, err := mut.Mutate(ctx, cu.mutation); err != nil {
  79. return 0, err
  80. }
  81. }
  82. return affected, err
  83. }
  84. // SaveX is like Save, but panics if an error occurs.
  85. func (cu *CarUpdate) SaveX(ctx context.Context) int {
  86. affected, err := cu.Save(ctx)
  87. if err != nil {
  88. panic(err)
  89. }
  90. return affected
  91. }
  92. // Exec executes the query.
  93. func (cu *CarUpdate) Exec(ctx context.Context) error {
  94. _, err := cu.Save(ctx)
  95. return err
  96. }
  97. // ExecX is like Exec, but panics if an error occurs.
  98. func (cu *CarUpdate) ExecX(ctx context.Context) {
  99. if err := cu.Exec(ctx); err != nil {
  100. panic(err)
  101. }
  102. }
  103. func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) {
  104. _spec := &sqlgraph.UpdateSpec{
  105. Node: &sqlgraph.NodeSpec{
  106. Table: car.Table,
  107. Columns: car.Columns,
  108. ID: &sqlgraph.FieldSpec{
  109. Type: field.TypeInt,
  110. Column: car.FieldID,
  111. },
  112. },
  113. }
  114. if ps := cu.predicates; len(ps) > 0 {
  115. _spec.Predicate = func(selector *sql.Selector) {
  116. for i := range ps {
  117. ps[i](selector)
  118. }
  119. }
  120. }
  121. if value, ok := cu.mutation.Model(); ok {
  122. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  123. Type: field.TypeString,
  124. Value: value,
  125. Column: car.FieldModel,
  126. })
  127. }
  128. if value, ok := cu.mutation.RegisteredAt(); ok {
  129. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  130. Type: field.TypeTime,
  131. Value: value,
  132. Column: car.FieldRegisteredAt,
  133. })
  134. }
  135. if cu.mutation.OwnerCleared() {
  136. edge := &sqlgraph.EdgeSpec{
  137. Rel: sqlgraph.M2O,
  138. Inverse: true,
  139. Table: car.OwnerTable,
  140. Columns: []string{car.OwnerColumn},
  141. Bidi: false,
  142. Target: &sqlgraph.EdgeTarget{
  143. IDSpec: &sqlgraph.FieldSpec{
  144. Type: field.TypeInt,
  145. Column: user.FieldID,
  146. },
  147. },
  148. }
  149. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  150. }
  151. if nodes := cu.mutation.OwnerIDs(); len(nodes) > 0 {
  152. edge := &sqlgraph.EdgeSpec{
  153. Rel: sqlgraph.M2O,
  154. Inverse: true,
  155. Table: car.OwnerTable,
  156. Columns: []string{car.OwnerColumn},
  157. Bidi: false,
  158. Target: &sqlgraph.EdgeTarget{
  159. IDSpec: &sqlgraph.FieldSpec{
  160. Type: field.TypeInt,
  161. Column: user.FieldID,
  162. },
  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 n, err = sqlgraph.UpdateNodes(ctx, cu.driver, _spec); err != nil {
  171. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  172. err = &NotFoundError{car.Label}
  173. } else if cerr, ok := isSQLConstraintError(err); ok {
  174. err = cerr
  175. }
  176. return 0, err
  177. }
  178. return n, nil
  179. }
  180. // CarUpdateOne is the builder for updating a single Car entity.
  181. type CarUpdateOne struct {
  182. config
  183. hooks []Hook
  184. mutation *CarMutation
  185. }
  186. // SetModel sets the model field.
  187. func (cuo *CarUpdateOne) SetModel(s string) *CarUpdateOne {
  188. cuo.mutation.SetModel(s)
  189. return cuo
  190. }
  191. // SetRegisteredAt sets the registered_at field.
  192. func (cuo *CarUpdateOne) SetRegisteredAt(t time.Time) *CarUpdateOne {
  193. cuo.mutation.SetRegisteredAt(t)
  194. return cuo
  195. }
  196. // SetOwnerID sets the owner edge to User by id.
  197. func (cuo *CarUpdateOne) SetOwnerID(id int) *CarUpdateOne {
  198. cuo.mutation.SetOwnerID(id)
  199. return cuo
  200. }
  201. // SetNillableOwnerID sets the owner edge to User by id if the given value is not nil.
  202. func (cuo *CarUpdateOne) SetNillableOwnerID(id *int) *CarUpdateOne {
  203. if id != nil {
  204. cuo = cuo.SetOwnerID(*id)
  205. }
  206. return cuo
  207. }
  208. // SetOwner sets the owner edge to User.
  209. func (cuo *CarUpdateOne) SetOwner(u *User) *CarUpdateOne {
  210. return cuo.SetOwnerID(u.ID)
  211. }
  212. // ClearOwner clears the owner edge to User.
  213. func (cuo *CarUpdateOne) ClearOwner() *CarUpdateOne {
  214. cuo.mutation.ClearOwner()
  215. return cuo
  216. }
  217. // Save executes the query and returns the updated entity.
  218. func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) {
  219. var (
  220. err error
  221. node *Car
  222. )
  223. if len(cuo.hooks) == 0 {
  224. node, err = cuo.sqlSave(ctx)
  225. } else {
  226. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  227. mutation, ok := m.(*CarMutation)
  228. if !ok {
  229. return nil, fmt.Errorf("unexpected mutation type %T", m)
  230. }
  231. cuo.mutation = mutation
  232. node, err = cuo.sqlSave(ctx)
  233. return node, err
  234. })
  235. for i := len(cuo.hooks) - 1; i >= 0; i-- {
  236. mut = cuo.hooks[i](mut)
  237. }
  238. if _, err := mut.Mutate(ctx, cuo.mutation); err != nil {
  239. return nil, err
  240. }
  241. }
  242. return node, err
  243. }
  244. // SaveX is like Save, but panics if an error occurs.
  245. func (cuo *CarUpdateOne) SaveX(ctx context.Context) *Car {
  246. c, err := cuo.Save(ctx)
  247. if err != nil {
  248. panic(err)
  249. }
  250. return c
  251. }
  252. // Exec executes the query on the entity.
  253. func (cuo *CarUpdateOne) Exec(ctx context.Context) error {
  254. _, err := cuo.Save(ctx)
  255. return err
  256. }
  257. // ExecX is like Exec, but panics if an error occurs.
  258. func (cuo *CarUpdateOne) ExecX(ctx context.Context) {
  259. if err := cuo.Exec(ctx); err != nil {
  260. panic(err)
  261. }
  262. }
  263. func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (c *Car, err error) {
  264. _spec := &sqlgraph.UpdateSpec{
  265. Node: &sqlgraph.NodeSpec{
  266. Table: car.Table,
  267. Columns: car.Columns,
  268. ID: &sqlgraph.FieldSpec{
  269. Type: field.TypeInt,
  270. Column: car.FieldID,
  271. },
  272. },
  273. }
  274. id, ok := cuo.mutation.ID()
  275. if !ok {
  276. return nil, fmt.Errorf("missing Car.ID for update")
  277. }
  278. _spec.Node.ID.Value = id
  279. if value, ok := cuo.mutation.Model(); ok {
  280. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  281. Type: field.TypeString,
  282. Value: value,
  283. Column: car.FieldModel,
  284. })
  285. }
  286. if value, ok := cuo.mutation.RegisteredAt(); ok {
  287. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  288. Type: field.TypeTime,
  289. Value: value,
  290. Column: car.FieldRegisteredAt,
  291. })
  292. }
  293. if cuo.mutation.OwnerCleared() {
  294. edge := &sqlgraph.EdgeSpec{
  295. Rel: sqlgraph.M2O,
  296. Inverse: true,
  297. Table: car.OwnerTable,
  298. Columns: []string{car.OwnerColumn},
  299. Bidi: false,
  300. Target: &sqlgraph.EdgeTarget{
  301. IDSpec: &sqlgraph.FieldSpec{
  302. Type: field.TypeInt,
  303. Column: user.FieldID,
  304. },
  305. },
  306. }
  307. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  308. }
  309. if nodes := cuo.mutation.OwnerIDs(); len(nodes) > 0 {
  310. edge := &sqlgraph.EdgeSpec{
  311. Rel: sqlgraph.M2O,
  312. Inverse: true,
  313. Table: car.OwnerTable,
  314. Columns: []string{car.OwnerColumn},
  315. Bidi: false,
  316. Target: &sqlgraph.EdgeTarget{
  317. IDSpec: &sqlgraph.FieldSpec{
  318. Type: field.TypeInt,
  319. Column: user.FieldID,
  320. },
  321. },
  322. }
  323. for _, k := range nodes {
  324. edge.Target.Nodes = append(edge.Target.Nodes, k)
  325. }
  326. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  327. }
  328. c = &Car{config: cuo.config}
  329. _spec.Assign = c.assignValues
  330. _spec.ScanValues = c.scanValues()
  331. if err = sqlgraph.UpdateNode(ctx, cuo.driver, _spec); err != nil {
  332. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  333. err = &NotFoundError{car.Label}
  334. } else if cerr, ok := isSQLConstraintError(err); ok {
  335. err = cerr
  336. }
  337. return nil, err
  338. }
  339. return c, nil
  340. }