label_update.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "wechat-api/ent/label"
  9. "wechat-api/ent/labelrelationship"
  10. "wechat-api/ent/predicate"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // LabelUpdate is the builder for updating Label entities.
  16. type LabelUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *LabelMutation
  20. }
  21. // Where appends a list predicates to the LabelUpdate builder.
  22. func (lu *LabelUpdate) Where(ps ...predicate.Label) *LabelUpdate {
  23. lu.mutation.Where(ps...)
  24. return lu
  25. }
  26. // SetUpdatedAt sets the "updated_at" field.
  27. func (lu *LabelUpdate) SetUpdatedAt(t time.Time) *LabelUpdate {
  28. lu.mutation.SetUpdatedAt(t)
  29. return lu
  30. }
  31. // SetStatus sets the "status" field.
  32. func (lu *LabelUpdate) SetStatus(u uint8) *LabelUpdate {
  33. lu.mutation.ResetStatus()
  34. lu.mutation.SetStatus(u)
  35. return lu
  36. }
  37. // SetNillableStatus sets the "status" field if the given value is not nil.
  38. func (lu *LabelUpdate) SetNillableStatus(u *uint8) *LabelUpdate {
  39. if u != nil {
  40. lu.SetStatus(*u)
  41. }
  42. return lu
  43. }
  44. // AddStatus adds u to the "status" field.
  45. func (lu *LabelUpdate) AddStatus(u int8) *LabelUpdate {
  46. lu.mutation.AddStatus(u)
  47. return lu
  48. }
  49. // ClearStatus clears the value of the "status" field.
  50. func (lu *LabelUpdate) ClearStatus() *LabelUpdate {
  51. lu.mutation.ClearStatus()
  52. return lu
  53. }
  54. // SetDeletedAt sets the "deleted_at" field.
  55. func (lu *LabelUpdate) SetDeletedAt(t time.Time) *LabelUpdate {
  56. lu.mutation.SetDeletedAt(t)
  57. return lu
  58. }
  59. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  60. func (lu *LabelUpdate) SetNillableDeletedAt(t *time.Time) *LabelUpdate {
  61. if t != nil {
  62. lu.SetDeletedAt(*t)
  63. }
  64. return lu
  65. }
  66. // ClearDeletedAt clears the value of the "deleted_at" field.
  67. func (lu *LabelUpdate) ClearDeletedAt() *LabelUpdate {
  68. lu.mutation.ClearDeletedAt()
  69. return lu
  70. }
  71. // SetType sets the "type" field.
  72. func (lu *LabelUpdate) SetType(i int) *LabelUpdate {
  73. lu.mutation.ResetType()
  74. lu.mutation.SetType(i)
  75. return lu
  76. }
  77. // SetNillableType sets the "type" field if the given value is not nil.
  78. func (lu *LabelUpdate) SetNillableType(i *int) *LabelUpdate {
  79. if i != nil {
  80. lu.SetType(*i)
  81. }
  82. return lu
  83. }
  84. // AddType adds i to the "type" field.
  85. func (lu *LabelUpdate) AddType(i int) *LabelUpdate {
  86. lu.mutation.AddType(i)
  87. return lu
  88. }
  89. // SetName sets the "name" field.
  90. func (lu *LabelUpdate) SetName(s string) *LabelUpdate {
  91. lu.mutation.SetName(s)
  92. return lu
  93. }
  94. // SetNillableName sets the "name" field if the given value is not nil.
  95. func (lu *LabelUpdate) SetNillableName(s *string) *LabelUpdate {
  96. if s != nil {
  97. lu.SetName(*s)
  98. }
  99. return lu
  100. }
  101. // SetFrom sets the "from" field.
  102. func (lu *LabelUpdate) SetFrom(i int) *LabelUpdate {
  103. lu.mutation.ResetFrom()
  104. lu.mutation.SetFrom(i)
  105. return lu
  106. }
  107. // SetNillableFrom sets the "from" field if the given value is not nil.
  108. func (lu *LabelUpdate) SetNillableFrom(i *int) *LabelUpdate {
  109. if i != nil {
  110. lu.SetFrom(*i)
  111. }
  112. return lu
  113. }
  114. // AddFrom adds i to the "from" field.
  115. func (lu *LabelUpdate) AddFrom(i int) *LabelUpdate {
  116. lu.mutation.AddFrom(i)
  117. return lu
  118. }
  119. // SetMode sets the "mode" field.
  120. func (lu *LabelUpdate) SetMode(i int) *LabelUpdate {
  121. lu.mutation.ResetMode()
  122. lu.mutation.SetMode(i)
  123. return lu
  124. }
  125. // SetNillableMode sets the "mode" field if the given value is not nil.
  126. func (lu *LabelUpdate) SetNillableMode(i *int) *LabelUpdate {
  127. if i != nil {
  128. lu.SetMode(*i)
  129. }
  130. return lu
  131. }
  132. // AddMode adds i to the "mode" field.
  133. func (lu *LabelUpdate) AddMode(i int) *LabelUpdate {
  134. lu.mutation.AddMode(i)
  135. return lu
  136. }
  137. // SetConditions sets the "conditions" field.
  138. func (lu *LabelUpdate) SetConditions(s string) *LabelUpdate {
  139. lu.mutation.SetConditions(s)
  140. return lu
  141. }
  142. // SetNillableConditions sets the "conditions" field if the given value is not nil.
  143. func (lu *LabelUpdate) SetNillableConditions(s *string) *LabelUpdate {
  144. if s != nil {
  145. lu.SetConditions(*s)
  146. }
  147. return lu
  148. }
  149. // ClearConditions clears the value of the "conditions" field.
  150. func (lu *LabelUpdate) ClearConditions() *LabelUpdate {
  151. lu.mutation.ClearConditions()
  152. return lu
  153. }
  154. // AddLabelRelationshipIDs adds the "label_relationships" edge to the LabelRelationship entity by IDs.
  155. func (lu *LabelUpdate) AddLabelRelationshipIDs(ids ...uint64) *LabelUpdate {
  156. lu.mutation.AddLabelRelationshipIDs(ids...)
  157. return lu
  158. }
  159. // AddLabelRelationships adds the "label_relationships" edges to the LabelRelationship entity.
  160. func (lu *LabelUpdate) AddLabelRelationships(l ...*LabelRelationship) *LabelUpdate {
  161. ids := make([]uint64, len(l))
  162. for i := range l {
  163. ids[i] = l[i].ID
  164. }
  165. return lu.AddLabelRelationshipIDs(ids...)
  166. }
  167. // Mutation returns the LabelMutation object of the builder.
  168. func (lu *LabelUpdate) Mutation() *LabelMutation {
  169. return lu.mutation
  170. }
  171. // ClearLabelRelationships clears all "label_relationships" edges to the LabelRelationship entity.
  172. func (lu *LabelUpdate) ClearLabelRelationships() *LabelUpdate {
  173. lu.mutation.ClearLabelRelationships()
  174. return lu
  175. }
  176. // RemoveLabelRelationshipIDs removes the "label_relationships" edge to LabelRelationship entities by IDs.
  177. func (lu *LabelUpdate) RemoveLabelRelationshipIDs(ids ...uint64) *LabelUpdate {
  178. lu.mutation.RemoveLabelRelationshipIDs(ids...)
  179. return lu
  180. }
  181. // RemoveLabelRelationships removes "label_relationships" edges to LabelRelationship entities.
  182. func (lu *LabelUpdate) RemoveLabelRelationships(l ...*LabelRelationship) *LabelUpdate {
  183. ids := make([]uint64, len(l))
  184. for i := range l {
  185. ids[i] = l[i].ID
  186. }
  187. return lu.RemoveLabelRelationshipIDs(ids...)
  188. }
  189. // Save executes the query and returns the number of nodes affected by the update operation.
  190. func (lu *LabelUpdate) Save(ctx context.Context) (int, error) {
  191. if err := lu.defaults(); err != nil {
  192. return 0, err
  193. }
  194. return withHooks(ctx, lu.sqlSave, lu.mutation, lu.hooks)
  195. }
  196. // SaveX is like Save, but panics if an error occurs.
  197. func (lu *LabelUpdate) SaveX(ctx context.Context) int {
  198. affected, err := lu.Save(ctx)
  199. if err != nil {
  200. panic(err)
  201. }
  202. return affected
  203. }
  204. // Exec executes the query.
  205. func (lu *LabelUpdate) Exec(ctx context.Context) error {
  206. _, err := lu.Save(ctx)
  207. return err
  208. }
  209. // ExecX is like Exec, but panics if an error occurs.
  210. func (lu *LabelUpdate) ExecX(ctx context.Context) {
  211. if err := lu.Exec(ctx); err != nil {
  212. panic(err)
  213. }
  214. }
  215. // defaults sets the default values of the builder before save.
  216. func (lu *LabelUpdate) defaults() error {
  217. if _, ok := lu.mutation.UpdatedAt(); !ok {
  218. if label.UpdateDefaultUpdatedAt == nil {
  219. return fmt.Errorf("ent: uninitialized label.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  220. }
  221. v := label.UpdateDefaultUpdatedAt()
  222. lu.mutation.SetUpdatedAt(v)
  223. }
  224. return nil
  225. }
  226. func (lu *LabelUpdate) sqlSave(ctx context.Context) (n int, err error) {
  227. _spec := sqlgraph.NewUpdateSpec(label.Table, label.Columns, sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64))
  228. if ps := lu.mutation.predicates; len(ps) > 0 {
  229. _spec.Predicate = func(selector *sql.Selector) {
  230. for i := range ps {
  231. ps[i](selector)
  232. }
  233. }
  234. }
  235. if value, ok := lu.mutation.UpdatedAt(); ok {
  236. _spec.SetField(label.FieldUpdatedAt, field.TypeTime, value)
  237. }
  238. if value, ok := lu.mutation.Status(); ok {
  239. _spec.SetField(label.FieldStatus, field.TypeUint8, value)
  240. }
  241. if value, ok := lu.mutation.AddedStatus(); ok {
  242. _spec.AddField(label.FieldStatus, field.TypeUint8, value)
  243. }
  244. if lu.mutation.StatusCleared() {
  245. _spec.ClearField(label.FieldStatus, field.TypeUint8)
  246. }
  247. if value, ok := lu.mutation.DeletedAt(); ok {
  248. _spec.SetField(label.FieldDeletedAt, field.TypeTime, value)
  249. }
  250. if lu.mutation.DeletedAtCleared() {
  251. _spec.ClearField(label.FieldDeletedAt, field.TypeTime)
  252. }
  253. if value, ok := lu.mutation.GetType(); ok {
  254. _spec.SetField(label.FieldType, field.TypeInt, value)
  255. }
  256. if value, ok := lu.mutation.AddedType(); ok {
  257. _spec.AddField(label.FieldType, field.TypeInt, value)
  258. }
  259. if value, ok := lu.mutation.Name(); ok {
  260. _spec.SetField(label.FieldName, field.TypeString, value)
  261. }
  262. if value, ok := lu.mutation.From(); ok {
  263. _spec.SetField(label.FieldFrom, field.TypeInt, value)
  264. }
  265. if value, ok := lu.mutation.AddedFrom(); ok {
  266. _spec.AddField(label.FieldFrom, field.TypeInt, value)
  267. }
  268. if value, ok := lu.mutation.Mode(); ok {
  269. _spec.SetField(label.FieldMode, field.TypeInt, value)
  270. }
  271. if value, ok := lu.mutation.AddedMode(); ok {
  272. _spec.AddField(label.FieldMode, field.TypeInt, value)
  273. }
  274. if value, ok := lu.mutation.Conditions(); ok {
  275. _spec.SetField(label.FieldConditions, field.TypeString, value)
  276. }
  277. if lu.mutation.ConditionsCleared() {
  278. _spec.ClearField(label.FieldConditions, field.TypeString)
  279. }
  280. if lu.mutation.LabelRelationshipsCleared() {
  281. edge := &sqlgraph.EdgeSpec{
  282. Rel: sqlgraph.O2M,
  283. Inverse: false,
  284. Table: label.LabelRelationshipsTable,
  285. Columns: []string{label.LabelRelationshipsColumn},
  286. Bidi: false,
  287. Target: &sqlgraph.EdgeTarget{
  288. IDSpec: sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64),
  289. },
  290. }
  291. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  292. }
  293. if nodes := lu.mutation.RemovedLabelRelationshipsIDs(); len(nodes) > 0 && !lu.mutation.LabelRelationshipsCleared() {
  294. edge := &sqlgraph.EdgeSpec{
  295. Rel: sqlgraph.O2M,
  296. Inverse: false,
  297. Table: label.LabelRelationshipsTable,
  298. Columns: []string{label.LabelRelationshipsColumn},
  299. Bidi: false,
  300. Target: &sqlgraph.EdgeTarget{
  301. IDSpec: sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64),
  302. },
  303. }
  304. for _, k := range nodes {
  305. edge.Target.Nodes = append(edge.Target.Nodes, k)
  306. }
  307. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  308. }
  309. if nodes := lu.mutation.LabelRelationshipsIDs(); len(nodes) > 0 {
  310. edge := &sqlgraph.EdgeSpec{
  311. Rel: sqlgraph.O2M,
  312. Inverse: false,
  313. Table: label.LabelRelationshipsTable,
  314. Columns: []string{label.LabelRelationshipsColumn},
  315. Bidi: false,
  316. Target: &sqlgraph.EdgeTarget{
  317. IDSpec: sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64),
  318. },
  319. }
  320. for _, k := range nodes {
  321. edge.Target.Nodes = append(edge.Target.Nodes, k)
  322. }
  323. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  324. }
  325. if n, err = sqlgraph.UpdateNodes(ctx, lu.driver, _spec); err != nil {
  326. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  327. err = &NotFoundError{label.Label}
  328. } else if sqlgraph.IsConstraintError(err) {
  329. err = &ConstraintError{msg: err.Error(), wrap: err}
  330. }
  331. return 0, err
  332. }
  333. lu.mutation.done = true
  334. return n, nil
  335. }
  336. // LabelUpdateOne is the builder for updating a single Label entity.
  337. type LabelUpdateOne struct {
  338. config
  339. fields []string
  340. hooks []Hook
  341. mutation *LabelMutation
  342. }
  343. // SetUpdatedAt sets the "updated_at" field.
  344. func (luo *LabelUpdateOne) SetUpdatedAt(t time.Time) *LabelUpdateOne {
  345. luo.mutation.SetUpdatedAt(t)
  346. return luo
  347. }
  348. // SetStatus sets the "status" field.
  349. func (luo *LabelUpdateOne) SetStatus(u uint8) *LabelUpdateOne {
  350. luo.mutation.ResetStatus()
  351. luo.mutation.SetStatus(u)
  352. return luo
  353. }
  354. // SetNillableStatus sets the "status" field if the given value is not nil.
  355. func (luo *LabelUpdateOne) SetNillableStatus(u *uint8) *LabelUpdateOne {
  356. if u != nil {
  357. luo.SetStatus(*u)
  358. }
  359. return luo
  360. }
  361. // AddStatus adds u to the "status" field.
  362. func (luo *LabelUpdateOne) AddStatus(u int8) *LabelUpdateOne {
  363. luo.mutation.AddStatus(u)
  364. return luo
  365. }
  366. // ClearStatus clears the value of the "status" field.
  367. func (luo *LabelUpdateOne) ClearStatus() *LabelUpdateOne {
  368. luo.mutation.ClearStatus()
  369. return luo
  370. }
  371. // SetDeletedAt sets the "deleted_at" field.
  372. func (luo *LabelUpdateOne) SetDeletedAt(t time.Time) *LabelUpdateOne {
  373. luo.mutation.SetDeletedAt(t)
  374. return luo
  375. }
  376. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  377. func (luo *LabelUpdateOne) SetNillableDeletedAt(t *time.Time) *LabelUpdateOne {
  378. if t != nil {
  379. luo.SetDeletedAt(*t)
  380. }
  381. return luo
  382. }
  383. // ClearDeletedAt clears the value of the "deleted_at" field.
  384. func (luo *LabelUpdateOne) ClearDeletedAt() *LabelUpdateOne {
  385. luo.mutation.ClearDeletedAt()
  386. return luo
  387. }
  388. // SetType sets the "type" field.
  389. func (luo *LabelUpdateOne) SetType(i int) *LabelUpdateOne {
  390. luo.mutation.ResetType()
  391. luo.mutation.SetType(i)
  392. return luo
  393. }
  394. // SetNillableType sets the "type" field if the given value is not nil.
  395. func (luo *LabelUpdateOne) SetNillableType(i *int) *LabelUpdateOne {
  396. if i != nil {
  397. luo.SetType(*i)
  398. }
  399. return luo
  400. }
  401. // AddType adds i to the "type" field.
  402. func (luo *LabelUpdateOne) AddType(i int) *LabelUpdateOne {
  403. luo.mutation.AddType(i)
  404. return luo
  405. }
  406. // SetName sets the "name" field.
  407. func (luo *LabelUpdateOne) SetName(s string) *LabelUpdateOne {
  408. luo.mutation.SetName(s)
  409. return luo
  410. }
  411. // SetNillableName sets the "name" field if the given value is not nil.
  412. func (luo *LabelUpdateOne) SetNillableName(s *string) *LabelUpdateOne {
  413. if s != nil {
  414. luo.SetName(*s)
  415. }
  416. return luo
  417. }
  418. // SetFrom sets the "from" field.
  419. func (luo *LabelUpdateOne) SetFrom(i int) *LabelUpdateOne {
  420. luo.mutation.ResetFrom()
  421. luo.mutation.SetFrom(i)
  422. return luo
  423. }
  424. // SetNillableFrom sets the "from" field if the given value is not nil.
  425. func (luo *LabelUpdateOne) SetNillableFrom(i *int) *LabelUpdateOne {
  426. if i != nil {
  427. luo.SetFrom(*i)
  428. }
  429. return luo
  430. }
  431. // AddFrom adds i to the "from" field.
  432. func (luo *LabelUpdateOne) AddFrom(i int) *LabelUpdateOne {
  433. luo.mutation.AddFrom(i)
  434. return luo
  435. }
  436. // SetMode sets the "mode" field.
  437. func (luo *LabelUpdateOne) SetMode(i int) *LabelUpdateOne {
  438. luo.mutation.ResetMode()
  439. luo.mutation.SetMode(i)
  440. return luo
  441. }
  442. // SetNillableMode sets the "mode" field if the given value is not nil.
  443. func (luo *LabelUpdateOne) SetNillableMode(i *int) *LabelUpdateOne {
  444. if i != nil {
  445. luo.SetMode(*i)
  446. }
  447. return luo
  448. }
  449. // AddMode adds i to the "mode" field.
  450. func (luo *LabelUpdateOne) AddMode(i int) *LabelUpdateOne {
  451. luo.mutation.AddMode(i)
  452. return luo
  453. }
  454. // SetConditions sets the "conditions" field.
  455. func (luo *LabelUpdateOne) SetConditions(s string) *LabelUpdateOne {
  456. luo.mutation.SetConditions(s)
  457. return luo
  458. }
  459. // SetNillableConditions sets the "conditions" field if the given value is not nil.
  460. func (luo *LabelUpdateOne) SetNillableConditions(s *string) *LabelUpdateOne {
  461. if s != nil {
  462. luo.SetConditions(*s)
  463. }
  464. return luo
  465. }
  466. // ClearConditions clears the value of the "conditions" field.
  467. func (luo *LabelUpdateOne) ClearConditions() *LabelUpdateOne {
  468. luo.mutation.ClearConditions()
  469. return luo
  470. }
  471. // AddLabelRelationshipIDs adds the "label_relationships" edge to the LabelRelationship entity by IDs.
  472. func (luo *LabelUpdateOne) AddLabelRelationshipIDs(ids ...uint64) *LabelUpdateOne {
  473. luo.mutation.AddLabelRelationshipIDs(ids...)
  474. return luo
  475. }
  476. // AddLabelRelationships adds the "label_relationships" edges to the LabelRelationship entity.
  477. func (luo *LabelUpdateOne) AddLabelRelationships(l ...*LabelRelationship) *LabelUpdateOne {
  478. ids := make([]uint64, len(l))
  479. for i := range l {
  480. ids[i] = l[i].ID
  481. }
  482. return luo.AddLabelRelationshipIDs(ids...)
  483. }
  484. // Mutation returns the LabelMutation object of the builder.
  485. func (luo *LabelUpdateOne) Mutation() *LabelMutation {
  486. return luo.mutation
  487. }
  488. // ClearLabelRelationships clears all "label_relationships" edges to the LabelRelationship entity.
  489. func (luo *LabelUpdateOne) ClearLabelRelationships() *LabelUpdateOne {
  490. luo.mutation.ClearLabelRelationships()
  491. return luo
  492. }
  493. // RemoveLabelRelationshipIDs removes the "label_relationships" edge to LabelRelationship entities by IDs.
  494. func (luo *LabelUpdateOne) RemoveLabelRelationshipIDs(ids ...uint64) *LabelUpdateOne {
  495. luo.mutation.RemoveLabelRelationshipIDs(ids...)
  496. return luo
  497. }
  498. // RemoveLabelRelationships removes "label_relationships" edges to LabelRelationship entities.
  499. func (luo *LabelUpdateOne) RemoveLabelRelationships(l ...*LabelRelationship) *LabelUpdateOne {
  500. ids := make([]uint64, len(l))
  501. for i := range l {
  502. ids[i] = l[i].ID
  503. }
  504. return luo.RemoveLabelRelationshipIDs(ids...)
  505. }
  506. // Where appends a list predicates to the LabelUpdate builder.
  507. func (luo *LabelUpdateOne) Where(ps ...predicate.Label) *LabelUpdateOne {
  508. luo.mutation.Where(ps...)
  509. return luo
  510. }
  511. // Select allows selecting one or more fields (columns) of the returned entity.
  512. // The default is selecting all fields defined in the entity schema.
  513. func (luo *LabelUpdateOne) Select(field string, fields ...string) *LabelUpdateOne {
  514. luo.fields = append([]string{field}, fields...)
  515. return luo
  516. }
  517. // Save executes the query and returns the updated Label entity.
  518. func (luo *LabelUpdateOne) Save(ctx context.Context) (*Label, error) {
  519. if err := luo.defaults(); err != nil {
  520. return nil, err
  521. }
  522. return withHooks(ctx, luo.sqlSave, luo.mutation, luo.hooks)
  523. }
  524. // SaveX is like Save, but panics if an error occurs.
  525. func (luo *LabelUpdateOne) SaveX(ctx context.Context) *Label {
  526. node, err := luo.Save(ctx)
  527. if err != nil {
  528. panic(err)
  529. }
  530. return node
  531. }
  532. // Exec executes the query on the entity.
  533. func (luo *LabelUpdateOne) Exec(ctx context.Context) error {
  534. _, err := luo.Save(ctx)
  535. return err
  536. }
  537. // ExecX is like Exec, but panics if an error occurs.
  538. func (luo *LabelUpdateOne) ExecX(ctx context.Context) {
  539. if err := luo.Exec(ctx); err != nil {
  540. panic(err)
  541. }
  542. }
  543. // defaults sets the default values of the builder before save.
  544. func (luo *LabelUpdateOne) defaults() error {
  545. if _, ok := luo.mutation.UpdatedAt(); !ok {
  546. if label.UpdateDefaultUpdatedAt == nil {
  547. return fmt.Errorf("ent: uninitialized label.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  548. }
  549. v := label.UpdateDefaultUpdatedAt()
  550. luo.mutation.SetUpdatedAt(v)
  551. }
  552. return nil
  553. }
  554. func (luo *LabelUpdateOne) sqlSave(ctx context.Context) (_node *Label, err error) {
  555. _spec := sqlgraph.NewUpdateSpec(label.Table, label.Columns, sqlgraph.NewFieldSpec(label.FieldID, field.TypeUint64))
  556. id, ok := luo.mutation.ID()
  557. if !ok {
  558. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Label.id" for update`)}
  559. }
  560. _spec.Node.ID.Value = id
  561. if fields := luo.fields; len(fields) > 0 {
  562. _spec.Node.Columns = make([]string, 0, len(fields))
  563. _spec.Node.Columns = append(_spec.Node.Columns, label.FieldID)
  564. for _, f := range fields {
  565. if !label.ValidColumn(f) {
  566. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  567. }
  568. if f != label.FieldID {
  569. _spec.Node.Columns = append(_spec.Node.Columns, f)
  570. }
  571. }
  572. }
  573. if ps := luo.mutation.predicates; len(ps) > 0 {
  574. _spec.Predicate = func(selector *sql.Selector) {
  575. for i := range ps {
  576. ps[i](selector)
  577. }
  578. }
  579. }
  580. if value, ok := luo.mutation.UpdatedAt(); ok {
  581. _spec.SetField(label.FieldUpdatedAt, field.TypeTime, value)
  582. }
  583. if value, ok := luo.mutation.Status(); ok {
  584. _spec.SetField(label.FieldStatus, field.TypeUint8, value)
  585. }
  586. if value, ok := luo.mutation.AddedStatus(); ok {
  587. _spec.AddField(label.FieldStatus, field.TypeUint8, value)
  588. }
  589. if luo.mutation.StatusCleared() {
  590. _spec.ClearField(label.FieldStatus, field.TypeUint8)
  591. }
  592. if value, ok := luo.mutation.DeletedAt(); ok {
  593. _spec.SetField(label.FieldDeletedAt, field.TypeTime, value)
  594. }
  595. if luo.mutation.DeletedAtCleared() {
  596. _spec.ClearField(label.FieldDeletedAt, field.TypeTime)
  597. }
  598. if value, ok := luo.mutation.GetType(); ok {
  599. _spec.SetField(label.FieldType, field.TypeInt, value)
  600. }
  601. if value, ok := luo.mutation.AddedType(); ok {
  602. _spec.AddField(label.FieldType, field.TypeInt, value)
  603. }
  604. if value, ok := luo.mutation.Name(); ok {
  605. _spec.SetField(label.FieldName, field.TypeString, value)
  606. }
  607. if value, ok := luo.mutation.From(); ok {
  608. _spec.SetField(label.FieldFrom, field.TypeInt, value)
  609. }
  610. if value, ok := luo.mutation.AddedFrom(); ok {
  611. _spec.AddField(label.FieldFrom, field.TypeInt, value)
  612. }
  613. if value, ok := luo.mutation.Mode(); ok {
  614. _spec.SetField(label.FieldMode, field.TypeInt, value)
  615. }
  616. if value, ok := luo.mutation.AddedMode(); ok {
  617. _spec.AddField(label.FieldMode, field.TypeInt, value)
  618. }
  619. if value, ok := luo.mutation.Conditions(); ok {
  620. _spec.SetField(label.FieldConditions, field.TypeString, value)
  621. }
  622. if luo.mutation.ConditionsCleared() {
  623. _spec.ClearField(label.FieldConditions, field.TypeString)
  624. }
  625. if luo.mutation.LabelRelationshipsCleared() {
  626. edge := &sqlgraph.EdgeSpec{
  627. Rel: sqlgraph.O2M,
  628. Inverse: false,
  629. Table: label.LabelRelationshipsTable,
  630. Columns: []string{label.LabelRelationshipsColumn},
  631. Bidi: false,
  632. Target: &sqlgraph.EdgeTarget{
  633. IDSpec: sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64),
  634. },
  635. }
  636. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  637. }
  638. if nodes := luo.mutation.RemovedLabelRelationshipsIDs(); len(nodes) > 0 && !luo.mutation.LabelRelationshipsCleared() {
  639. edge := &sqlgraph.EdgeSpec{
  640. Rel: sqlgraph.O2M,
  641. Inverse: false,
  642. Table: label.LabelRelationshipsTable,
  643. Columns: []string{label.LabelRelationshipsColumn},
  644. Bidi: false,
  645. Target: &sqlgraph.EdgeTarget{
  646. IDSpec: sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64),
  647. },
  648. }
  649. for _, k := range nodes {
  650. edge.Target.Nodes = append(edge.Target.Nodes, k)
  651. }
  652. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  653. }
  654. if nodes := luo.mutation.LabelRelationshipsIDs(); len(nodes) > 0 {
  655. edge := &sqlgraph.EdgeSpec{
  656. Rel: sqlgraph.O2M,
  657. Inverse: false,
  658. Table: label.LabelRelationshipsTable,
  659. Columns: []string{label.LabelRelationshipsColumn},
  660. Bidi: false,
  661. Target: &sqlgraph.EdgeTarget{
  662. IDSpec: sqlgraph.NewFieldSpec(labelrelationship.FieldID, field.TypeUint64),
  663. },
  664. }
  665. for _, k := range nodes {
  666. edge.Target.Nodes = append(edge.Target.Nodes, k)
  667. }
  668. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  669. }
  670. _node = &Label{config: luo.config}
  671. _spec.Assign = _node.assignValues
  672. _spec.ScanValues = _node.scanValues
  673. if err = sqlgraph.UpdateNode(ctx, luo.driver, _spec); err != nil {
  674. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  675. err = &NotFoundError{label.Label}
  676. } else if sqlgraph.IsConstraintError(err) {
  677. err = &ConstraintError{msg: err.Error(), wrap: err}
  678. }
  679. return nil, err
  680. }
  681. luo.mutation.done = true
  682. return _node, nil
  683. }