car_update.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. // Code generated by entc, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "code.osinet.fr/fgm/go__ent_demo/ent/car"
  9. "code.osinet.fr/fgm/go__ent_demo/ent/predicate"
  10. "code.osinet.fr/fgm/go__ent_demo/ent/user"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // CarUpdate is the builder for updating Car entities.
  16. type CarUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *CarMutation
  20. }
  21. // Where appends a list predicates to the CarUpdate builder.
  22. func (cu *CarUpdate) Where(ps ...predicate.Car) *CarUpdate {
  23. cu.mutation.Where(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 the User entity 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 the User entity 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 the User entity.
  49. func (cu *CarUpdate) SetOwner(u *User) *CarUpdate {
  50. return cu.SetOwnerID(u.ID)
  51. }
  52. // Mutation returns the CarMutation object of the builder.
  53. func (cu *CarUpdate) Mutation() *CarMutation {
  54. return cu.mutation
  55. }
  56. // ClearOwner clears the "owner" edge to the User entity.
  57. func (cu *CarUpdate) ClearOwner() *CarUpdate {
  58. cu.mutation.ClearOwner()
  59. return cu
  60. }
  61. // Save executes the query and returns the number of nodes affected by the update operation.
  62. func (cu *CarUpdate) Save(ctx context.Context) (int, error) {
  63. var (
  64. err error
  65. affected int
  66. )
  67. if len(cu.hooks) == 0 {
  68. affected, err = cu.sqlSave(ctx)
  69. } else {
  70. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  71. mutation, ok := m.(*CarMutation)
  72. if !ok {
  73. return nil, fmt.Errorf("unexpected mutation type %T", m)
  74. }
  75. cu.mutation = mutation
  76. affected, err = cu.sqlSave(ctx)
  77. mutation.done = true
  78. return affected, err
  79. })
  80. for i := len(cu.hooks) - 1; i >= 0; i-- {
  81. if cu.hooks[i] == nil {
  82. return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  83. }
  84. mut = cu.hooks[i](mut)
  85. }
  86. if _, err := mut.Mutate(ctx, cu.mutation); err != nil {
  87. return 0, err
  88. }
  89. }
  90. return affected, err
  91. }
  92. // SaveX is like Save, but panics if an error occurs.
  93. func (cu *CarUpdate) SaveX(ctx context.Context) int {
  94. affected, err := cu.Save(ctx)
  95. if err != nil {
  96. panic(err)
  97. }
  98. return affected
  99. }
  100. // Exec executes the query.
  101. func (cu *CarUpdate) Exec(ctx context.Context) error {
  102. _, err := cu.Save(ctx)
  103. return err
  104. }
  105. // ExecX is like Exec, but panics if an error occurs.
  106. func (cu *CarUpdate) ExecX(ctx context.Context) {
  107. if err := cu.Exec(ctx); err != nil {
  108. panic(err)
  109. }
  110. }
  111. func (cu *CarUpdate) sqlSave(ctx context.Context) (n int, err error) {
  112. _spec := &sqlgraph.UpdateSpec{
  113. Node: &sqlgraph.NodeSpec{
  114. Table: car.Table,
  115. Columns: car.Columns,
  116. ID: &sqlgraph.FieldSpec{
  117. Type: field.TypeInt,
  118. Column: car.FieldID,
  119. },
  120. },
  121. }
  122. if ps := cu.mutation.predicates; len(ps) > 0 {
  123. _spec.Predicate = func(selector *sql.Selector) {
  124. for i := range ps {
  125. ps[i](selector)
  126. }
  127. }
  128. }
  129. if value, ok := cu.mutation.Model(); ok {
  130. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  131. Type: field.TypeString,
  132. Value: value,
  133. Column: car.FieldModel,
  134. })
  135. }
  136. if value, ok := cu.mutation.RegisteredAt(); ok {
  137. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  138. Type: field.TypeTime,
  139. Value: value,
  140. Column: car.FieldRegisteredAt,
  141. })
  142. }
  143. if cu.mutation.OwnerCleared() {
  144. edge := &sqlgraph.EdgeSpec{
  145. Rel: sqlgraph.M2O,
  146. Inverse: true,
  147. Table: car.OwnerTable,
  148. Columns: []string{car.OwnerColumn},
  149. Bidi: false,
  150. Target: &sqlgraph.EdgeTarget{
  151. IDSpec: &sqlgraph.FieldSpec{
  152. Type: field.TypeInt,
  153. Column: user.FieldID,
  154. },
  155. },
  156. }
  157. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  158. }
  159. if nodes := cu.mutation.OwnerIDs(); len(nodes) > 0 {
  160. edge := &sqlgraph.EdgeSpec{
  161. Rel: sqlgraph.M2O,
  162. Inverse: true,
  163. Table: car.OwnerTable,
  164. Columns: []string{car.OwnerColumn},
  165. Bidi: false,
  166. Target: &sqlgraph.EdgeTarget{
  167. IDSpec: &sqlgraph.FieldSpec{
  168. Type: field.TypeInt,
  169. Column: user.FieldID,
  170. },
  171. },
  172. }
  173. for _, k := range nodes {
  174. edge.Target.Nodes = append(edge.Target.Nodes, k)
  175. }
  176. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  177. }
  178. if n, err = sqlgraph.UpdateNodes(ctx, cu.driver, _spec); err != nil {
  179. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  180. err = &NotFoundError{car.Label}
  181. } else if sqlgraph.IsConstraintError(err) {
  182. err = &ConstraintError{err.Error(), err}
  183. }
  184. return 0, err
  185. }
  186. return n, nil
  187. }
  188. // CarUpdateOne is the builder for updating a single Car entity.
  189. type CarUpdateOne struct {
  190. config
  191. fields []string
  192. hooks []Hook
  193. mutation *CarMutation
  194. }
  195. // SetModel sets the "model" field.
  196. func (cuo *CarUpdateOne) SetModel(s string) *CarUpdateOne {
  197. cuo.mutation.SetModel(s)
  198. return cuo
  199. }
  200. // SetRegisteredAt sets the "registered_at" field.
  201. func (cuo *CarUpdateOne) SetRegisteredAt(t time.Time) *CarUpdateOne {
  202. cuo.mutation.SetRegisteredAt(t)
  203. return cuo
  204. }
  205. // SetOwnerID sets the "owner" edge to the User entity by ID.
  206. func (cuo *CarUpdateOne) SetOwnerID(id int) *CarUpdateOne {
  207. cuo.mutation.SetOwnerID(id)
  208. return cuo
  209. }
  210. // SetNillableOwnerID sets the "owner" edge to the User entity by ID if the given value is not nil.
  211. func (cuo *CarUpdateOne) SetNillableOwnerID(id *int) *CarUpdateOne {
  212. if id != nil {
  213. cuo = cuo.SetOwnerID(*id)
  214. }
  215. return cuo
  216. }
  217. // SetOwner sets the "owner" edge to the User entity.
  218. func (cuo *CarUpdateOne) SetOwner(u *User) *CarUpdateOne {
  219. return cuo.SetOwnerID(u.ID)
  220. }
  221. // Mutation returns the CarMutation object of the builder.
  222. func (cuo *CarUpdateOne) Mutation() *CarMutation {
  223. return cuo.mutation
  224. }
  225. // ClearOwner clears the "owner" edge to the User entity.
  226. func (cuo *CarUpdateOne) ClearOwner() *CarUpdateOne {
  227. cuo.mutation.ClearOwner()
  228. return cuo
  229. }
  230. // Select allows selecting one or more fields (columns) of the returned entity.
  231. // The default is selecting all fields defined in the entity schema.
  232. func (cuo *CarUpdateOne) Select(field string, fields ...string) *CarUpdateOne {
  233. cuo.fields = append([]string{field}, fields...)
  234. return cuo
  235. }
  236. // Save executes the query and returns the updated Car entity.
  237. func (cuo *CarUpdateOne) Save(ctx context.Context) (*Car, error) {
  238. var (
  239. err error
  240. node *Car
  241. )
  242. if len(cuo.hooks) == 0 {
  243. node, err = cuo.sqlSave(ctx)
  244. } else {
  245. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  246. mutation, ok := m.(*CarMutation)
  247. if !ok {
  248. return nil, fmt.Errorf("unexpected mutation type %T", m)
  249. }
  250. cuo.mutation = mutation
  251. node, err = cuo.sqlSave(ctx)
  252. mutation.done = true
  253. return node, err
  254. })
  255. for i := len(cuo.hooks) - 1; i >= 0; i-- {
  256. if cuo.hooks[i] == nil {
  257. return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
  258. }
  259. mut = cuo.hooks[i](mut)
  260. }
  261. if _, err := mut.Mutate(ctx, cuo.mutation); err != nil {
  262. return nil, err
  263. }
  264. }
  265. return node, err
  266. }
  267. // SaveX is like Save, but panics if an error occurs.
  268. func (cuo *CarUpdateOne) SaveX(ctx context.Context) *Car {
  269. node, err := cuo.Save(ctx)
  270. if err != nil {
  271. panic(err)
  272. }
  273. return node
  274. }
  275. // Exec executes the query on the entity.
  276. func (cuo *CarUpdateOne) Exec(ctx context.Context) error {
  277. _, err := cuo.Save(ctx)
  278. return err
  279. }
  280. // ExecX is like Exec, but panics if an error occurs.
  281. func (cuo *CarUpdateOne) ExecX(ctx context.Context) {
  282. if err := cuo.Exec(ctx); err != nil {
  283. panic(err)
  284. }
  285. }
  286. func (cuo *CarUpdateOne) sqlSave(ctx context.Context) (_node *Car, err error) {
  287. _spec := &sqlgraph.UpdateSpec{
  288. Node: &sqlgraph.NodeSpec{
  289. Table: car.Table,
  290. Columns: car.Columns,
  291. ID: &sqlgraph.FieldSpec{
  292. Type: field.TypeInt,
  293. Column: car.FieldID,
  294. },
  295. },
  296. }
  297. id, ok := cuo.mutation.ID()
  298. if !ok {
  299. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Car.id" for update`)}
  300. }
  301. _spec.Node.ID.Value = id
  302. if fields := cuo.fields; len(fields) > 0 {
  303. _spec.Node.Columns = make([]string, 0, len(fields))
  304. _spec.Node.Columns = append(_spec.Node.Columns, car.FieldID)
  305. for _, f := range fields {
  306. if !car.ValidColumn(f) {
  307. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  308. }
  309. if f != car.FieldID {
  310. _spec.Node.Columns = append(_spec.Node.Columns, f)
  311. }
  312. }
  313. }
  314. if ps := cuo.mutation.predicates; len(ps) > 0 {
  315. _spec.Predicate = func(selector *sql.Selector) {
  316. for i := range ps {
  317. ps[i](selector)
  318. }
  319. }
  320. }
  321. if value, ok := cuo.mutation.Model(); ok {
  322. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  323. Type: field.TypeString,
  324. Value: value,
  325. Column: car.FieldModel,
  326. })
  327. }
  328. if value, ok := cuo.mutation.RegisteredAt(); ok {
  329. _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{
  330. Type: field.TypeTime,
  331. Value: value,
  332. Column: car.FieldRegisteredAt,
  333. })
  334. }
  335. if cuo.mutation.OwnerCleared() {
  336. edge := &sqlgraph.EdgeSpec{
  337. Rel: sqlgraph.M2O,
  338. Inverse: true,
  339. Table: car.OwnerTable,
  340. Columns: []string{car.OwnerColumn},
  341. Bidi: false,
  342. Target: &sqlgraph.EdgeTarget{
  343. IDSpec: &sqlgraph.FieldSpec{
  344. Type: field.TypeInt,
  345. Column: user.FieldID,
  346. },
  347. },
  348. }
  349. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  350. }
  351. if nodes := cuo.mutation.OwnerIDs(); len(nodes) > 0 {
  352. edge := &sqlgraph.EdgeSpec{
  353. Rel: sqlgraph.M2O,
  354. Inverse: true,
  355. Table: car.OwnerTable,
  356. Columns: []string{car.OwnerColumn},
  357. Bidi: false,
  358. Target: &sqlgraph.EdgeTarget{
  359. IDSpec: &sqlgraph.FieldSpec{
  360. Type: field.TypeInt,
  361. Column: user.FieldID,
  362. },
  363. },
  364. }
  365. for _, k := range nodes {
  366. edge.Target.Nodes = append(edge.Target.Nodes, k)
  367. }
  368. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  369. }
  370. _node = &Car{config: cuo.config}
  371. _spec.Assign = _node.assignValues
  372. _spec.ScanValues = _node.scanValues
  373. if err = sqlgraph.UpdateNode(ctx, cuo.driver, _spec); err != nil {
  374. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  375. err = &NotFoundError{car.Label}
  376. } else if sqlgraph.IsConstraintError(err) {
  377. err = &ConstraintError{err.Error(), err}
  378. }
  379. return nil, err
  380. }
  381. return _node, nil
  382. }