token_update.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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/predicate"
  9. "wechat-api/ent/token"
  10. "entgo.io/ent/dialect/sql"
  11. "entgo.io/ent/dialect/sql/sqlgraph"
  12. "entgo.io/ent/schema/field"
  13. )
  14. // TokenUpdate is the builder for updating Token entities.
  15. type TokenUpdate struct {
  16. config
  17. hooks []Hook
  18. mutation *TokenMutation
  19. }
  20. // Where appends a list predicates to the TokenUpdate builder.
  21. func (tu *TokenUpdate) Where(ps ...predicate.Token) *TokenUpdate {
  22. tu.mutation.Where(ps...)
  23. return tu
  24. }
  25. // SetUpdatedAt sets the "updated_at" field.
  26. func (tu *TokenUpdate) SetUpdatedAt(t time.Time) *TokenUpdate {
  27. tu.mutation.SetUpdatedAt(t)
  28. return tu
  29. }
  30. // SetDeletedAt sets the "deleted_at" field.
  31. func (tu *TokenUpdate) SetDeletedAt(t time.Time) *TokenUpdate {
  32. tu.mutation.SetDeletedAt(t)
  33. return tu
  34. }
  35. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  36. func (tu *TokenUpdate) SetNillableDeletedAt(t *time.Time) *TokenUpdate {
  37. if t != nil {
  38. tu.SetDeletedAt(*t)
  39. }
  40. return tu
  41. }
  42. // ClearDeletedAt clears the value of the "deleted_at" field.
  43. func (tu *TokenUpdate) ClearDeletedAt() *TokenUpdate {
  44. tu.mutation.ClearDeletedAt()
  45. return tu
  46. }
  47. // SetExpireAt sets the "expire_at" field.
  48. func (tu *TokenUpdate) SetExpireAt(t time.Time) *TokenUpdate {
  49. tu.mutation.SetExpireAt(t)
  50. return tu
  51. }
  52. // SetNillableExpireAt sets the "expire_at" field if the given value is not nil.
  53. func (tu *TokenUpdate) SetNillableExpireAt(t *time.Time) *TokenUpdate {
  54. if t != nil {
  55. tu.SetExpireAt(*t)
  56. }
  57. return tu
  58. }
  59. // ClearExpireAt clears the value of the "expire_at" field.
  60. func (tu *TokenUpdate) ClearExpireAt() *TokenUpdate {
  61. tu.mutation.ClearExpireAt()
  62. return tu
  63. }
  64. // SetToken sets the "token" field.
  65. func (tu *TokenUpdate) SetToken(s string) *TokenUpdate {
  66. tu.mutation.SetToken(s)
  67. return tu
  68. }
  69. // SetNillableToken sets the "token" field if the given value is not nil.
  70. func (tu *TokenUpdate) SetNillableToken(s *string) *TokenUpdate {
  71. if s != nil {
  72. tu.SetToken(*s)
  73. }
  74. return tu
  75. }
  76. // ClearToken clears the value of the "token" field.
  77. func (tu *TokenUpdate) ClearToken() *TokenUpdate {
  78. tu.mutation.ClearToken()
  79. return tu
  80. }
  81. // SetMAC sets the "mac" field.
  82. func (tu *TokenUpdate) SetMAC(s string) *TokenUpdate {
  83. tu.mutation.SetMAC(s)
  84. return tu
  85. }
  86. // SetNillableMAC sets the "mac" field if the given value is not nil.
  87. func (tu *TokenUpdate) SetNillableMAC(s *string) *TokenUpdate {
  88. if s != nil {
  89. tu.SetMAC(*s)
  90. }
  91. return tu
  92. }
  93. // SetOrganizationID sets the "organization_id" field.
  94. func (tu *TokenUpdate) SetOrganizationID(u uint64) *TokenUpdate {
  95. tu.mutation.ResetOrganizationID()
  96. tu.mutation.SetOrganizationID(u)
  97. return tu
  98. }
  99. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  100. func (tu *TokenUpdate) SetNillableOrganizationID(u *uint64) *TokenUpdate {
  101. if u != nil {
  102. tu.SetOrganizationID(*u)
  103. }
  104. return tu
  105. }
  106. // AddOrganizationID adds u to the "organization_id" field.
  107. func (tu *TokenUpdate) AddOrganizationID(u int64) *TokenUpdate {
  108. tu.mutation.AddOrganizationID(u)
  109. return tu
  110. }
  111. // Mutation returns the TokenMutation object of the builder.
  112. func (tu *TokenUpdate) Mutation() *TokenMutation {
  113. return tu.mutation
  114. }
  115. // Save executes the query and returns the number of nodes affected by the update operation.
  116. func (tu *TokenUpdate) Save(ctx context.Context) (int, error) {
  117. if err := tu.defaults(); err != nil {
  118. return 0, err
  119. }
  120. return withHooks(ctx, tu.sqlSave, tu.mutation, tu.hooks)
  121. }
  122. // SaveX is like Save, but panics if an error occurs.
  123. func (tu *TokenUpdate) SaveX(ctx context.Context) int {
  124. affected, err := tu.Save(ctx)
  125. if err != nil {
  126. panic(err)
  127. }
  128. return affected
  129. }
  130. // Exec executes the query.
  131. func (tu *TokenUpdate) Exec(ctx context.Context) error {
  132. _, err := tu.Save(ctx)
  133. return err
  134. }
  135. // ExecX is like Exec, but panics if an error occurs.
  136. func (tu *TokenUpdate) ExecX(ctx context.Context) {
  137. if err := tu.Exec(ctx); err != nil {
  138. panic(err)
  139. }
  140. }
  141. // defaults sets the default values of the builder before save.
  142. func (tu *TokenUpdate) defaults() error {
  143. if _, ok := tu.mutation.UpdatedAt(); !ok {
  144. if token.UpdateDefaultUpdatedAt == nil {
  145. return fmt.Errorf("ent: uninitialized token.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  146. }
  147. v := token.UpdateDefaultUpdatedAt()
  148. tu.mutation.SetUpdatedAt(v)
  149. }
  150. return nil
  151. }
  152. func (tu *TokenUpdate) sqlSave(ctx context.Context) (n int, err error) {
  153. _spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
  154. if ps := tu.mutation.predicates; len(ps) > 0 {
  155. _spec.Predicate = func(selector *sql.Selector) {
  156. for i := range ps {
  157. ps[i](selector)
  158. }
  159. }
  160. }
  161. if value, ok := tu.mutation.UpdatedAt(); ok {
  162. _spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
  163. }
  164. if value, ok := tu.mutation.DeletedAt(); ok {
  165. _spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
  166. }
  167. if tu.mutation.DeletedAtCleared() {
  168. _spec.ClearField(token.FieldDeletedAt, field.TypeTime)
  169. }
  170. if value, ok := tu.mutation.ExpireAt(); ok {
  171. _spec.SetField(token.FieldExpireAt, field.TypeTime, value)
  172. }
  173. if tu.mutation.ExpireAtCleared() {
  174. _spec.ClearField(token.FieldExpireAt, field.TypeTime)
  175. }
  176. if value, ok := tu.mutation.Token(); ok {
  177. _spec.SetField(token.FieldToken, field.TypeString, value)
  178. }
  179. if tu.mutation.TokenCleared() {
  180. _spec.ClearField(token.FieldToken, field.TypeString)
  181. }
  182. if value, ok := tu.mutation.MAC(); ok {
  183. _spec.SetField(token.FieldMAC, field.TypeString, value)
  184. }
  185. if value, ok := tu.mutation.OrganizationID(); ok {
  186. _spec.SetField(token.FieldOrganizationID, field.TypeUint64, value)
  187. }
  188. if value, ok := tu.mutation.AddedOrganizationID(); ok {
  189. _spec.AddField(token.FieldOrganizationID, field.TypeUint64, value)
  190. }
  191. if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil {
  192. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  193. err = &NotFoundError{token.Label}
  194. } else if sqlgraph.IsConstraintError(err) {
  195. err = &ConstraintError{msg: err.Error(), wrap: err}
  196. }
  197. return 0, err
  198. }
  199. tu.mutation.done = true
  200. return n, nil
  201. }
  202. // TokenUpdateOne is the builder for updating a single Token entity.
  203. type TokenUpdateOne struct {
  204. config
  205. fields []string
  206. hooks []Hook
  207. mutation *TokenMutation
  208. }
  209. // SetUpdatedAt sets the "updated_at" field.
  210. func (tuo *TokenUpdateOne) SetUpdatedAt(t time.Time) *TokenUpdateOne {
  211. tuo.mutation.SetUpdatedAt(t)
  212. return tuo
  213. }
  214. // SetDeletedAt sets the "deleted_at" field.
  215. func (tuo *TokenUpdateOne) SetDeletedAt(t time.Time) *TokenUpdateOne {
  216. tuo.mutation.SetDeletedAt(t)
  217. return tuo
  218. }
  219. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  220. func (tuo *TokenUpdateOne) SetNillableDeletedAt(t *time.Time) *TokenUpdateOne {
  221. if t != nil {
  222. tuo.SetDeletedAt(*t)
  223. }
  224. return tuo
  225. }
  226. // ClearDeletedAt clears the value of the "deleted_at" field.
  227. func (tuo *TokenUpdateOne) ClearDeletedAt() *TokenUpdateOne {
  228. tuo.mutation.ClearDeletedAt()
  229. return tuo
  230. }
  231. // SetExpireAt sets the "expire_at" field.
  232. func (tuo *TokenUpdateOne) SetExpireAt(t time.Time) *TokenUpdateOne {
  233. tuo.mutation.SetExpireAt(t)
  234. return tuo
  235. }
  236. // SetNillableExpireAt sets the "expire_at" field if the given value is not nil.
  237. func (tuo *TokenUpdateOne) SetNillableExpireAt(t *time.Time) *TokenUpdateOne {
  238. if t != nil {
  239. tuo.SetExpireAt(*t)
  240. }
  241. return tuo
  242. }
  243. // ClearExpireAt clears the value of the "expire_at" field.
  244. func (tuo *TokenUpdateOne) ClearExpireAt() *TokenUpdateOne {
  245. tuo.mutation.ClearExpireAt()
  246. return tuo
  247. }
  248. // SetToken sets the "token" field.
  249. func (tuo *TokenUpdateOne) SetToken(s string) *TokenUpdateOne {
  250. tuo.mutation.SetToken(s)
  251. return tuo
  252. }
  253. // SetNillableToken sets the "token" field if the given value is not nil.
  254. func (tuo *TokenUpdateOne) SetNillableToken(s *string) *TokenUpdateOne {
  255. if s != nil {
  256. tuo.SetToken(*s)
  257. }
  258. return tuo
  259. }
  260. // ClearToken clears the value of the "token" field.
  261. func (tuo *TokenUpdateOne) ClearToken() *TokenUpdateOne {
  262. tuo.mutation.ClearToken()
  263. return tuo
  264. }
  265. // SetMAC sets the "mac" field.
  266. func (tuo *TokenUpdateOne) SetMAC(s string) *TokenUpdateOne {
  267. tuo.mutation.SetMAC(s)
  268. return tuo
  269. }
  270. // SetNillableMAC sets the "mac" field if the given value is not nil.
  271. func (tuo *TokenUpdateOne) SetNillableMAC(s *string) *TokenUpdateOne {
  272. if s != nil {
  273. tuo.SetMAC(*s)
  274. }
  275. return tuo
  276. }
  277. // SetOrganizationID sets the "organization_id" field.
  278. func (tuo *TokenUpdateOne) SetOrganizationID(u uint64) *TokenUpdateOne {
  279. tuo.mutation.ResetOrganizationID()
  280. tuo.mutation.SetOrganizationID(u)
  281. return tuo
  282. }
  283. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  284. func (tuo *TokenUpdateOne) SetNillableOrganizationID(u *uint64) *TokenUpdateOne {
  285. if u != nil {
  286. tuo.SetOrganizationID(*u)
  287. }
  288. return tuo
  289. }
  290. // AddOrganizationID adds u to the "organization_id" field.
  291. func (tuo *TokenUpdateOne) AddOrganizationID(u int64) *TokenUpdateOne {
  292. tuo.mutation.AddOrganizationID(u)
  293. return tuo
  294. }
  295. // Mutation returns the TokenMutation object of the builder.
  296. func (tuo *TokenUpdateOne) Mutation() *TokenMutation {
  297. return tuo.mutation
  298. }
  299. // Where appends a list predicates to the TokenUpdate builder.
  300. func (tuo *TokenUpdateOne) Where(ps ...predicate.Token) *TokenUpdateOne {
  301. tuo.mutation.Where(ps...)
  302. return tuo
  303. }
  304. // Select allows selecting one or more fields (columns) of the returned entity.
  305. // The default is selecting all fields defined in the entity schema.
  306. func (tuo *TokenUpdateOne) Select(field string, fields ...string) *TokenUpdateOne {
  307. tuo.fields = append([]string{field}, fields...)
  308. return tuo
  309. }
  310. // Save executes the query and returns the updated Token entity.
  311. func (tuo *TokenUpdateOne) Save(ctx context.Context) (*Token, error) {
  312. if err := tuo.defaults(); err != nil {
  313. return nil, err
  314. }
  315. return withHooks(ctx, tuo.sqlSave, tuo.mutation, tuo.hooks)
  316. }
  317. // SaveX is like Save, but panics if an error occurs.
  318. func (tuo *TokenUpdateOne) SaveX(ctx context.Context) *Token {
  319. node, err := tuo.Save(ctx)
  320. if err != nil {
  321. panic(err)
  322. }
  323. return node
  324. }
  325. // Exec executes the query on the entity.
  326. func (tuo *TokenUpdateOne) Exec(ctx context.Context) error {
  327. _, err := tuo.Save(ctx)
  328. return err
  329. }
  330. // ExecX is like Exec, but panics if an error occurs.
  331. func (tuo *TokenUpdateOne) ExecX(ctx context.Context) {
  332. if err := tuo.Exec(ctx); err != nil {
  333. panic(err)
  334. }
  335. }
  336. // defaults sets the default values of the builder before save.
  337. func (tuo *TokenUpdateOne) defaults() error {
  338. if _, ok := tuo.mutation.UpdatedAt(); !ok {
  339. if token.UpdateDefaultUpdatedAt == nil {
  340. return fmt.Errorf("ent: uninitialized token.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  341. }
  342. v := token.UpdateDefaultUpdatedAt()
  343. tuo.mutation.SetUpdatedAt(v)
  344. }
  345. return nil
  346. }
  347. func (tuo *TokenUpdateOne) sqlSave(ctx context.Context) (_node *Token, err error) {
  348. _spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
  349. id, ok := tuo.mutation.ID()
  350. if !ok {
  351. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Token.id" for update`)}
  352. }
  353. _spec.Node.ID.Value = id
  354. if fields := tuo.fields; len(fields) > 0 {
  355. _spec.Node.Columns = make([]string, 0, len(fields))
  356. _spec.Node.Columns = append(_spec.Node.Columns, token.FieldID)
  357. for _, f := range fields {
  358. if !token.ValidColumn(f) {
  359. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  360. }
  361. if f != token.FieldID {
  362. _spec.Node.Columns = append(_spec.Node.Columns, f)
  363. }
  364. }
  365. }
  366. if ps := tuo.mutation.predicates; len(ps) > 0 {
  367. _spec.Predicate = func(selector *sql.Selector) {
  368. for i := range ps {
  369. ps[i](selector)
  370. }
  371. }
  372. }
  373. if value, ok := tuo.mutation.UpdatedAt(); ok {
  374. _spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
  375. }
  376. if value, ok := tuo.mutation.DeletedAt(); ok {
  377. _spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
  378. }
  379. if tuo.mutation.DeletedAtCleared() {
  380. _spec.ClearField(token.FieldDeletedAt, field.TypeTime)
  381. }
  382. if value, ok := tuo.mutation.ExpireAt(); ok {
  383. _spec.SetField(token.FieldExpireAt, field.TypeTime, value)
  384. }
  385. if tuo.mutation.ExpireAtCleared() {
  386. _spec.ClearField(token.FieldExpireAt, field.TypeTime)
  387. }
  388. if value, ok := tuo.mutation.Token(); ok {
  389. _spec.SetField(token.FieldToken, field.TypeString, value)
  390. }
  391. if tuo.mutation.TokenCleared() {
  392. _spec.ClearField(token.FieldToken, field.TypeString)
  393. }
  394. if value, ok := tuo.mutation.MAC(); ok {
  395. _spec.SetField(token.FieldMAC, field.TypeString, value)
  396. }
  397. if value, ok := tuo.mutation.OrganizationID(); ok {
  398. _spec.SetField(token.FieldOrganizationID, field.TypeUint64, value)
  399. }
  400. if value, ok := tuo.mutation.AddedOrganizationID(); ok {
  401. _spec.AddField(token.FieldOrganizationID, field.TypeUint64, value)
  402. }
  403. _node = &Token{config: tuo.config}
  404. _spec.Assign = _node.assignValues
  405. _spec.ScanValues = _node.scanValues
  406. if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
  407. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  408. err = &NotFoundError{token.Label}
  409. } else if sqlgraph.IsConstraintError(err) {
  410. err = &ConstraintError{msg: err.Error(), wrap: err}
  411. }
  412. return nil, err
  413. }
  414. tuo.mutation.done = true
  415. return _node, nil
  416. }