token_update.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. // Mutation returns the TokenMutation object of the builder.
  94. func (tu *TokenUpdate) Mutation() *TokenMutation {
  95. return tu.mutation
  96. }
  97. // Save executes the query and returns the number of nodes affected by the update operation.
  98. func (tu *TokenUpdate) Save(ctx context.Context) (int, error) {
  99. if err := tu.defaults(); err != nil {
  100. return 0, err
  101. }
  102. return withHooks(ctx, tu.sqlSave, tu.mutation, tu.hooks)
  103. }
  104. // SaveX is like Save, but panics if an error occurs.
  105. func (tu *TokenUpdate) SaveX(ctx context.Context) int {
  106. affected, err := tu.Save(ctx)
  107. if err != nil {
  108. panic(err)
  109. }
  110. return affected
  111. }
  112. // Exec executes the query.
  113. func (tu *TokenUpdate) Exec(ctx context.Context) error {
  114. _, err := tu.Save(ctx)
  115. return err
  116. }
  117. // ExecX is like Exec, but panics if an error occurs.
  118. func (tu *TokenUpdate) ExecX(ctx context.Context) {
  119. if err := tu.Exec(ctx); err != nil {
  120. panic(err)
  121. }
  122. }
  123. // defaults sets the default values of the builder before save.
  124. func (tu *TokenUpdate) defaults() error {
  125. if _, ok := tu.mutation.UpdatedAt(); !ok {
  126. if token.UpdateDefaultUpdatedAt == nil {
  127. return fmt.Errorf("ent: uninitialized token.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  128. }
  129. v := token.UpdateDefaultUpdatedAt()
  130. tu.mutation.SetUpdatedAt(v)
  131. }
  132. return nil
  133. }
  134. func (tu *TokenUpdate) sqlSave(ctx context.Context) (n int, err error) {
  135. _spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
  136. if ps := tu.mutation.predicates; len(ps) > 0 {
  137. _spec.Predicate = func(selector *sql.Selector) {
  138. for i := range ps {
  139. ps[i](selector)
  140. }
  141. }
  142. }
  143. if value, ok := tu.mutation.UpdatedAt(); ok {
  144. _spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
  145. }
  146. if value, ok := tu.mutation.DeletedAt(); ok {
  147. _spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
  148. }
  149. if tu.mutation.DeletedAtCleared() {
  150. _spec.ClearField(token.FieldDeletedAt, field.TypeTime)
  151. }
  152. if value, ok := tu.mutation.ExpireAt(); ok {
  153. _spec.SetField(token.FieldExpireAt, field.TypeTime, value)
  154. }
  155. if tu.mutation.ExpireAtCleared() {
  156. _spec.ClearField(token.FieldExpireAt, field.TypeTime)
  157. }
  158. if value, ok := tu.mutation.Token(); ok {
  159. _spec.SetField(token.FieldToken, field.TypeString, value)
  160. }
  161. if tu.mutation.TokenCleared() {
  162. _spec.ClearField(token.FieldToken, field.TypeString)
  163. }
  164. if value, ok := tu.mutation.MAC(); ok {
  165. _spec.SetField(token.FieldMAC, field.TypeString, value)
  166. }
  167. if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil {
  168. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  169. err = &NotFoundError{token.Label}
  170. } else if sqlgraph.IsConstraintError(err) {
  171. err = &ConstraintError{msg: err.Error(), wrap: err}
  172. }
  173. return 0, err
  174. }
  175. tu.mutation.done = true
  176. return n, nil
  177. }
  178. // TokenUpdateOne is the builder for updating a single Token entity.
  179. type TokenUpdateOne struct {
  180. config
  181. fields []string
  182. hooks []Hook
  183. mutation *TokenMutation
  184. }
  185. // SetUpdatedAt sets the "updated_at" field.
  186. func (tuo *TokenUpdateOne) SetUpdatedAt(t time.Time) *TokenUpdateOne {
  187. tuo.mutation.SetUpdatedAt(t)
  188. return tuo
  189. }
  190. // SetDeletedAt sets the "deleted_at" field.
  191. func (tuo *TokenUpdateOne) SetDeletedAt(t time.Time) *TokenUpdateOne {
  192. tuo.mutation.SetDeletedAt(t)
  193. return tuo
  194. }
  195. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  196. func (tuo *TokenUpdateOne) SetNillableDeletedAt(t *time.Time) *TokenUpdateOne {
  197. if t != nil {
  198. tuo.SetDeletedAt(*t)
  199. }
  200. return tuo
  201. }
  202. // ClearDeletedAt clears the value of the "deleted_at" field.
  203. func (tuo *TokenUpdateOne) ClearDeletedAt() *TokenUpdateOne {
  204. tuo.mutation.ClearDeletedAt()
  205. return tuo
  206. }
  207. // SetExpireAt sets the "expire_at" field.
  208. func (tuo *TokenUpdateOne) SetExpireAt(t time.Time) *TokenUpdateOne {
  209. tuo.mutation.SetExpireAt(t)
  210. return tuo
  211. }
  212. // SetNillableExpireAt sets the "expire_at" field if the given value is not nil.
  213. func (tuo *TokenUpdateOne) SetNillableExpireAt(t *time.Time) *TokenUpdateOne {
  214. if t != nil {
  215. tuo.SetExpireAt(*t)
  216. }
  217. return tuo
  218. }
  219. // ClearExpireAt clears the value of the "expire_at" field.
  220. func (tuo *TokenUpdateOne) ClearExpireAt() *TokenUpdateOne {
  221. tuo.mutation.ClearExpireAt()
  222. return tuo
  223. }
  224. // SetToken sets the "token" field.
  225. func (tuo *TokenUpdateOne) SetToken(s string) *TokenUpdateOne {
  226. tuo.mutation.SetToken(s)
  227. return tuo
  228. }
  229. // SetNillableToken sets the "token" field if the given value is not nil.
  230. func (tuo *TokenUpdateOne) SetNillableToken(s *string) *TokenUpdateOne {
  231. if s != nil {
  232. tuo.SetToken(*s)
  233. }
  234. return tuo
  235. }
  236. // ClearToken clears the value of the "token" field.
  237. func (tuo *TokenUpdateOne) ClearToken() *TokenUpdateOne {
  238. tuo.mutation.ClearToken()
  239. return tuo
  240. }
  241. // SetMAC sets the "mac" field.
  242. func (tuo *TokenUpdateOne) SetMAC(s string) *TokenUpdateOne {
  243. tuo.mutation.SetMAC(s)
  244. return tuo
  245. }
  246. // SetNillableMAC sets the "mac" field if the given value is not nil.
  247. func (tuo *TokenUpdateOne) SetNillableMAC(s *string) *TokenUpdateOne {
  248. if s != nil {
  249. tuo.SetMAC(*s)
  250. }
  251. return tuo
  252. }
  253. // Mutation returns the TokenMutation object of the builder.
  254. func (tuo *TokenUpdateOne) Mutation() *TokenMutation {
  255. return tuo.mutation
  256. }
  257. // Where appends a list predicates to the TokenUpdate builder.
  258. func (tuo *TokenUpdateOne) Where(ps ...predicate.Token) *TokenUpdateOne {
  259. tuo.mutation.Where(ps...)
  260. return tuo
  261. }
  262. // Select allows selecting one or more fields (columns) of the returned entity.
  263. // The default is selecting all fields defined in the entity schema.
  264. func (tuo *TokenUpdateOne) Select(field string, fields ...string) *TokenUpdateOne {
  265. tuo.fields = append([]string{field}, fields...)
  266. return tuo
  267. }
  268. // Save executes the query and returns the updated Token entity.
  269. func (tuo *TokenUpdateOne) Save(ctx context.Context) (*Token, error) {
  270. if err := tuo.defaults(); err != nil {
  271. return nil, err
  272. }
  273. return withHooks(ctx, tuo.sqlSave, tuo.mutation, tuo.hooks)
  274. }
  275. // SaveX is like Save, but panics if an error occurs.
  276. func (tuo *TokenUpdateOne) SaveX(ctx context.Context) *Token {
  277. node, err := tuo.Save(ctx)
  278. if err != nil {
  279. panic(err)
  280. }
  281. return node
  282. }
  283. // Exec executes the query on the entity.
  284. func (tuo *TokenUpdateOne) Exec(ctx context.Context) error {
  285. _, err := tuo.Save(ctx)
  286. return err
  287. }
  288. // ExecX is like Exec, but panics if an error occurs.
  289. func (tuo *TokenUpdateOne) ExecX(ctx context.Context) {
  290. if err := tuo.Exec(ctx); err != nil {
  291. panic(err)
  292. }
  293. }
  294. // defaults sets the default values of the builder before save.
  295. func (tuo *TokenUpdateOne) defaults() error {
  296. if _, ok := tuo.mutation.UpdatedAt(); !ok {
  297. if token.UpdateDefaultUpdatedAt == nil {
  298. return fmt.Errorf("ent: uninitialized token.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  299. }
  300. v := token.UpdateDefaultUpdatedAt()
  301. tuo.mutation.SetUpdatedAt(v)
  302. }
  303. return nil
  304. }
  305. func (tuo *TokenUpdateOne) sqlSave(ctx context.Context) (_node *Token, err error) {
  306. _spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
  307. id, ok := tuo.mutation.ID()
  308. if !ok {
  309. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Token.id" for update`)}
  310. }
  311. _spec.Node.ID.Value = id
  312. if fields := tuo.fields; len(fields) > 0 {
  313. _spec.Node.Columns = make([]string, 0, len(fields))
  314. _spec.Node.Columns = append(_spec.Node.Columns, token.FieldID)
  315. for _, f := range fields {
  316. if !token.ValidColumn(f) {
  317. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  318. }
  319. if f != token.FieldID {
  320. _spec.Node.Columns = append(_spec.Node.Columns, f)
  321. }
  322. }
  323. }
  324. if ps := tuo.mutation.predicates; len(ps) > 0 {
  325. _spec.Predicate = func(selector *sql.Selector) {
  326. for i := range ps {
  327. ps[i](selector)
  328. }
  329. }
  330. }
  331. if value, ok := tuo.mutation.UpdatedAt(); ok {
  332. _spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
  333. }
  334. if value, ok := tuo.mutation.DeletedAt(); ok {
  335. _spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
  336. }
  337. if tuo.mutation.DeletedAtCleared() {
  338. _spec.ClearField(token.FieldDeletedAt, field.TypeTime)
  339. }
  340. if value, ok := tuo.mutation.ExpireAt(); ok {
  341. _spec.SetField(token.FieldExpireAt, field.TypeTime, value)
  342. }
  343. if tuo.mutation.ExpireAtCleared() {
  344. _spec.ClearField(token.FieldExpireAt, field.TypeTime)
  345. }
  346. if value, ok := tuo.mutation.Token(); ok {
  347. _spec.SetField(token.FieldToken, field.TypeString, value)
  348. }
  349. if tuo.mutation.TokenCleared() {
  350. _spec.ClearField(token.FieldToken, field.TypeString)
  351. }
  352. if value, ok := tuo.mutation.MAC(); ok {
  353. _spec.SetField(token.FieldMAC, field.TypeString, value)
  354. }
  355. _node = &Token{config: tuo.config}
  356. _spec.Assign = _node.assignValues
  357. _spec.ScanValues = _node.scanValues
  358. if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
  359. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  360. err = &NotFoundError{token.Label}
  361. } else if sqlgraph.IsConstraintError(err) {
  362. err = &ConstraintError{msg: err.Error(), wrap: err}
  363. }
  364. return nil, err
  365. }
  366. tuo.mutation.done = true
  367. return _node, nil
  368. }