token_update.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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/agent"
  9. "wechat-api/ent/predicate"
  10. "wechat-api/ent/token"
  11. "entgo.io/ent/dialect/sql"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. )
  15. // TokenUpdate is the builder for updating Token entities.
  16. type TokenUpdate struct {
  17. config
  18. hooks []Hook
  19. mutation *TokenMutation
  20. }
  21. // Where appends a list predicates to the TokenUpdate builder.
  22. func (tu *TokenUpdate) Where(ps ...predicate.Token) *TokenUpdate {
  23. tu.mutation.Where(ps...)
  24. return tu
  25. }
  26. // SetUpdatedAt sets the "updated_at" field.
  27. func (tu *TokenUpdate) SetUpdatedAt(t time.Time) *TokenUpdate {
  28. tu.mutation.SetUpdatedAt(t)
  29. return tu
  30. }
  31. // SetDeletedAt sets the "deleted_at" field.
  32. func (tu *TokenUpdate) SetDeletedAt(t time.Time) *TokenUpdate {
  33. tu.mutation.SetDeletedAt(t)
  34. return tu
  35. }
  36. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  37. func (tu *TokenUpdate) SetNillableDeletedAt(t *time.Time) *TokenUpdate {
  38. if t != nil {
  39. tu.SetDeletedAt(*t)
  40. }
  41. return tu
  42. }
  43. // ClearDeletedAt clears the value of the "deleted_at" field.
  44. func (tu *TokenUpdate) ClearDeletedAt() *TokenUpdate {
  45. tu.mutation.ClearDeletedAt()
  46. return tu
  47. }
  48. // SetExpireAt sets the "expire_at" field.
  49. func (tu *TokenUpdate) SetExpireAt(t time.Time) *TokenUpdate {
  50. tu.mutation.SetExpireAt(t)
  51. return tu
  52. }
  53. // SetNillableExpireAt sets the "expire_at" field if the given value is not nil.
  54. func (tu *TokenUpdate) SetNillableExpireAt(t *time.Time) *TokenUpdate {
  55. if t != nil {
  56. tu.SetExpireAt(*t)
  57. }
  58. return tu
  59. }
  60. // ClearExpireAt clears the value of the "expire_at" field.
  61. func (tu *TokenUpdate) ClearExpireAt() *TokenUpdate {
  62. tu.mutation.ClearExpireAt()
  63. return tu
  64. }
  65. // SetToken sets the "token" field.
  66. func (tu *TokenUpdate) SetToken(s string) *TokenUpdate {
  67. tu.mutation.SetToken(s)
  68. return tu
  69. }
  70. // SetNillableToken sets the "token" field if the given value is not nil.
  71. func (tu *TokenUpdate) SetNillableToken(s *string) *TokenUpdate {
  72. if s != nil {
  73. tu.SetToken(*s)
  74. }
  75. return tu
  76. }
  77. // ClearToken clears the value of the "token" field.
  78. func (tu *TokenUpdate) ClearToken() *TokenUpdate {
  79. tu.mutation.ClearToken()
  80. return tu
  81. }
  82. // SetMAC sets the "mac" field.
  83. func (tu *TokenUpdate) SetMAC(s string) *TokenUpdate {
  84. tu.mutation.SetMAC(s)
  85. return tu
  86. }
  87. // SetNillableMAC sets the "mac" field if the given value is not nil.
  88. func (tu *TokenUpdate) SetNillableMAC(s *string) *TokenUpdate {
  89. if s != nil {
  90. tu.SetMAC(*s)
  91. }
  92. return tu
  93. }
  94. // ClearMAC clears the value of the "mac" field.
  95. func (tu *TokenUpdate) ClearMAC() *TokenUpdate {
  96. tu.mutation.ClearMAC()
  97. return tu
  98. }
  99. // SetOrganizationID sets the "organization_id" field.
  100. func (tu *TokenUpdate) SetOrganizationID(u uint64) *TokenUpdate {
  101. tu.mutation.ResetOrganizationID()
  102. tu.mutation.SetOrganizationID(u)
  103. return tu
  104. }
  105. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  106. func (tu *TokenUpdate) SetNillableOrganizationID(u *uint64) *TokenUpdate {
  107. if u != nil {
  108. tu.SetOrganizationID(*u)
  109. }
  110. return tu
  111. }
  112. // AddOrganizationID adds u to the "organization_id" field.
  113. func (tu *TokenUpdate) AddOrganizationID(u int64) *TokenUpdate {
  114. tu.mutation.AddOrganizationID(u)
  115. return tu
  116. }
  117. // SetAgentID sets the "agent_id" field.
  118. func (tu *TokenUpdate) SetAgentID(u uint64) *TokenUpdate {
  119. tu.mutation.SetAgentID(u)
  120. return tu
  121. }
  122. // SetNillableAgentID sets the "agent_id" field if the given value is not nil.
  123. func (tu *TokenUpdate) SetNillableAgentID(u *uint64) *TokenUpdate {
  124. if u != nil {
  125. tu.SetAgentID(*u)
  126. }
  127. return tu
  128. }
  129. // SetCustomAgentBase sets the "custom_agent_base" field.
  130. func (tu *TokenUpdate) SetCustomAgentBase(s string) *TokenUpdate {
  131. tu.mutation.SetCustomAgentBase(s)
  132. return tu
  133. }
  134. // SetNillableCustomAgentBase sets the "custom_agent_base" field if the given value is not nil.
  135. func (tu *TokenUpdate) SetNillableCustomAgentBase(s *string) *TokenUpdate {
  136. if s != nil {
  137. tu.SetCustomAgentBase(*s)
  138. }
  139. return tu
  140. }
  141. // ClearCustomAgentBase clears the value of the "custom_agent_base" field.
  142. func (tu *TokenUpdate) ClearCustomAgentBase() *TokenUpdate {
  143. tu.mutation.ClearCustomAgentBase()
  144. return tu
  145. }
  146. // SetCustomAgentKey sets the "custom_agent_key" field.
  147. func (tu *TokenUpdate) SetCustomAgentKey(s string) *TokenUpdate {
  148. tu.mutation.SetCustomAgentKey(s)
  149. return tu
  150. }
  151. // SetNillableCustomAgentKey sets the "custom_agent_key" field if the given value is not nil.
  152. func (tu *TokenUpdate) SetNillableCustomAgentKey(s *string) *TokenUpdate {
  153. if s != nil {
  154. tu.SetCustomAgentKey(*s)
  155. }
  156. return tu
  157. }
  158. // ClearCustomAgentKey clears the value of the "custom_agent_key" field.
  159. func (tu *TokenUpdate) ClearCustomAgentKey() *TokenUpdate {
  160. tu.mutation.ClearCustomAgentKey()
  161. return tu
  162. }
  163. // SetOpenaiBase sets the "openai_base" field.
  164. func (tu *TokenUpdate) SetOpenaiBase(s string) *TokenUpdate {
  165. tu.mutation.SetOpenaiBase(s)
  166. return tu
  167. }
  168. // SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
  169. func (tu *TokenUpdate) SetNillableOpenaiBase(s *string) *TokenUpdate {
  170. if s != nil {
  171. tu.SetOpenaiBase(*s)
  172. }
  173. return tu
  174. }
  175. // ClearOpenaiBase clears the value of the "openai_base" field.
  176. func (tu *TokenUpdate) ClearOpenaiBase() *TokenUpdate {
  177. tu.mutation.ClearOpenaiBase()
  178. return tu
  179. }
  180. // SetOpenaiKey sets the "openai_key" field.
  181. func (tu *TokenUpdate) SetOpenaiKey(s string) *TokenUpdate {
  182. tu.mutation.SetOpenaiKey(s)
  183. return tu
  184. }
  185. // SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
  186. func (tu *TokenUpdate) SetNillableOpenaiKey(s *string) *TokenUpdate {
  187. if s != nil {
  188. tu.SetOpenaiKey(*s)
  189. }
  190. return tu
  191. }
  192. // ClearOpenaiKey clears the value of the "openai_key" field.
  193. func (tu *TokenUpdate) ClearOpenaiKey() *TokenUpdate {
  194. tu.mutation.ClearOpenaiKey()
  195. return tu
  196. }
  197. // SetAgent sets the "agent" edge to the Agent entity.
  198. func (tu *TokenUpdate) SetAgent(a *Agent) *TokenUpdate {
  199. return tu.SetAgentID(a.ID)
  200. }
  201. // Mutation returns the TokenMutation object of the builder.
  202. func (tu *TokenUpdate) Mutation() *TokenMutation {
  203. return tu.mutation
  204. }
  205. // ClearAgent clears the "agent" edge to the Agent entity.
  206. func (tu *TokenUpdate) ClearAgent() *TokenUpdate {
  207. tu.mutation.ClearAgent()
  208. return tu
  209. }
  210. // Save executes the query and returns the number of nodes affected by the update operation.
  211. func (tu *TokenUpdate) Save(ctx context.Context) (int, error) {
  212. if err := tu.defaults(); err != nil {
  213. return 0, err
  214. }
  215. return withHooks(ctx, tu.sqlSave, tu.mutation, tu.hooks)
  216. }
  217. // SaveX is like Save, but panics if an error occurs.
  218. func (tu *TokenUpdate) SaveX(ctx context.Context) int {
  219. affected, err := tu.Save(ctx)
  220. if err != nil {
  221. panic(err)
  222. }
  223. return affected
  224. }
  225. // Exec executes the query.
  226. func (tu *TokenUpdate) Exec(ctx context.Context) error {
  227. _, err := tu.Save(ctx)
  228. return err
  229. }
  230. // ExecX is like Exec, but panics if an error occurs.
  231. func (tu *TokenUpdate) ExecX(ctx context.Context) {
  232. if err := tu.Exec(ctx); err != nil {
  233. panic(err)
  234. }
  235. }
  236. // defaults sets the default values of the builder before save.
  237. func (tu *TokenUpdate) defaults() error {
  238. if _, ok := tu.mutation.UpdatedAt(); !ok {
  239. if token.UpdateDefaultUpdatedAt == nil {
  240. return fmt.Errorf("ent: uninitialized token.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  241. }
  242. v := token.UpdateDefaultUpdatedAt()
  243. tu.mutation.SetUpdatedAt(v)
  244. }
  245. return nil
  246. }
  247. // check runs all checks and user-defined validators on the builder.
  248. func (tu *TokenUpdate) check() error {
  249. if _, ok := tu.mutation.AgentID(); tu.mutation.AgentCleared() && !ok {
  250. return errors.New(`ent: clearing a required unique edge "Token.agent"`)
  251. }
  252. return nil
  253. }
  254. func (tu *TokenUpdate) sqlSave(ctx context.Context) (n int, err error) {
  255. if err := tu.check(); err != nil {
  256. return n, err
  257. }
  258. _spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
  259. if ps := tu.mutation.predicates; len(ps) > 0 {
  260. _spec.Predicate = func(selector *sql.Selector) {
  261. for i := range ps {
  262. ps[i](selector)
  263. }
  264. }
  265. }
  266. if value, ok := tu.mutation.UpdatedAt(); ok {
  267. _spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
  268. }
  269. if value, ok := tu.mutation.DeletedAt(); ok {
  270. _spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
  271. }
  272. if tu.mutation.DeletedAtCleared() {
  273. _spec.ClearField(token.FieldDeletedAt, field.TypeTime)
  274. }
  275. if value, ok := tu.mutation.ExpireAt(); ok {
  276. _spec.SetField(token.FieldExpireAt, field.TypeTime, value)
  277. }
  278. if tu.mutation.ExpireAtCleared() {
  279. _spec.ClearField(token.FieldExpireAt, field.TypeTime)
  280. }
  281. if value, ok := tu.mutation.Token(); ok {
  282. _spec.SetField(token.FieldToken, field.TypeString, value)
  283. }
  284. if tu.mutation.TokenCleared() {
  285. _spec.ClearField(token.FieldToken, field.TypeString)
  286. }
  287. if value, ok := tu.mutation.MAC(); ok {
  288. _spec.SetField(token.FieldMAC, field.TypeString, value)
  289. }
  290. if tu.mutation.MACCleared() {
  291. _spec.ClearField(token.FieldMAC, field.TypeString)
  292. }
  293. if value, ok := tu.mutation.OrganizationID(); ok {
  294. _spec.SetField(token.FieldOrganizationID, field.TypeUint64, value)
  295. }
  296. if value, ok := tu.mutation.AddedOrganizationID(); ok {
  297. _spec.AddField(token.FieldOrganizationID, field.TypeUint64, value)
  298. }
  299. if value, ok := tu.mutation.CustomAgentBase(); ok {
  300. _spec.SetField(token.FieldCustomAgentBase, field.TypeString, value)
  301. }
  302. if tu.mutation.CustomAgentBaseCleared() {
  303. _spec.ClearField(token.FieldCustomAgentBase, field.TypeString)
  304. }
  305. if value, ok := tu.mutation.CustomAgentKey(); ok {
  306. _spec.SetField(token.FieldCustomAgentKey, field.TypeString, value)
  307. }
  308. if tu.mutation.CustomAgentKeyCleared() {
  309. _spec.ClearField(token.FieldCustomAgentKey, field.TypeString)
  310. }
  311. if value, ok := tu.mutation.OpenaiBase(); ok {
  312. _spec.SetField(token.FieldOpenaiBase, field.TypeString, value)
  313. }
  314. if tu.mutation.OpenaiBaseCleared() {
  315. _spec.ClearField(token.FieldOpenaiBase, field.TypeString)
  316. }
  317. if value, ok := tu.mutation.OpenaiKey(); ok {
  318. _spec.SetField(token.FieldOpenaiKey, field.TypeString, value)
  319. }
  320. if tu.mutation.OpenaiKeyCleared() {
  321. _spec.ClearField(token.FieldOpenaiKey, field.TypeString)
  322. }
  323. if tu.mutation.AgentCleared() {
  324. edge := &sqlgraph.EdgeSpec{
  325. Rel: sqlgraph.M2O,
  326. Inverse: true,
  327. Table: token.AgentTable,
  328. Columns: []string{token.AgentColumn},
  329. Bidi: false,
  330. Target: &sqlgraph.EdgeTarget{
  331. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  332. },
  333. }
  334. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  335. }
  336. if nodes := tu.mutation.AgentIDs(); len(nodes) > 0 {
  337. edge := &sqlgraph.EdgeSpec{
  338. Rel: sqlgraph.M2O,
  339. Inverse: true,
  340. Table: token.AgentTable,
  341. Columns: []string{token.AgentColumn},
  342. Bidi: false,
  343. Target: &sqlgraph.EdgeTarget{
  344. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  345. },
  346. }
  347. for _, k := range nodes {
  348. edge.Target.Nodes = append(edge.Target.Nodes, k)
  349. }
  350. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  351. }
  352. if n, err = sqlgraph.UpdateNodes(ctx, tu.driver, _spec); err != nil {
  353. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  354. err = &NotFoundError{token.Label}
  355. } else if sqlgraph.IsConstraintError(err) {
  356. err = &ConstraintError{msg: err.Error(), wrap: err}
  357. }
  358. return 0, err
  359. }
  360. tu.mutation.done = true
  361. return n, nil
  362. }
  363. // TokenUpdateOne is the builder for updating a single Token entity.
  364. type TokenUpdateOne struct {
  365. config
  366. fields []string
  367. hooks []Hook
  368. mutation *TokenMutation
  369. }
  370. // SetUpdatedAt sets the "updated_at" field.
  371. func (tuo *TokenUpdateOne) SetUpdatedAt(t time.Time) *TokenUpdateOne {
  372. tuo.mutation.SetUpdatedAt(t)
  373. return tuo
  374. }
  375. // SetDeletedAt sets the "deleted_at" field.
  376. func (tuo *TokenUpdateOne) SetDeletedAt(t time.Time) *TokenUpdateOne {
  377. tuo.mutation.SetDeletedAt(t)
  378. return tuo
  379. }
  380. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  381. func (tuo *TokenUpdateOne) SetNillableDeletedAt(t *time.Time) *TokenUpdateOne {
  382. if t != nil {
  383. tuo.SetDeletedAt(*t)
  384. }
  385. return tuo
  386. }
  387. // ClearDeletedAt clears the value of the "deleted_at" field.
  388. func (tuo *TokenUpdateOne) ClearDeletedAt() *TokenUpdateOne {
  389. tuo.mutation.ClearDeletedAt()
  390. return tuo
  391. }
  392. // SetExpireAt sets the "expire_at" field.
  393. func (tuo *TokenUpdateOne) SetExpireAt(t time.Time) *TokenUpdateOne {
  394. tuo.mutation.SetExpireAt(t)
  395. return tuo
  396. }
  397. // SetNillableExpireAt sets the "expire_at" field if the given value is not nil.
  398. func (tuo *TokenUpdateOne) SetNillableExpireAt(t *time.Time) *TokenUpdateOne {
  399. if t != nil {
  400. tuo.SetExpireAt(*t)
  401. }
  402. return tuo
  403. }
  404. // ClearExpireAt clears the value of the "expire_at" field.
  405. func (tuo *TokenUpdateOne) ClearExpireAt() *TokenUpdateOne {
  406. tuo.mutation.ClearExpireAt()
  407. return tuo
  408. }
  409. // SetToken sets the "token" field.
  410. func (tuo *TokenUpdateOne) SetToken(s string) *TokenUpdateOne {
  411. tuo.mutation.SetToken(s)
  412. return tuo
  413. }
  414. // SetNillableToken sets the "token" field if the given value is not nil.
  415. func (tuo *TokenUpdateOne) SetNillableToken(s *string) *TokenUpdateOne {
  416. if s != nil {
  417. tuo.SetToken(*s)
  418. }
  419. return tuo
  420. }
  421. // ClearToken clears the value of the "token" field.
  422. func (tuo *TokenUpdateOne) ClearToken() *TokenUpdateOne {
  423. tuo.mutation.ClearToken()
  424. return tuo
  425. }
  426. // SetMAC sets the "mac" field.
  427. func (tuo *TokenUpdateOne) SetMAC(s string) *TokenUpdateOne {
  428. tuo.mutation.SetMAC(s)
  429. return tuo
  430. }
  431. // SetNillableMAC sets the "mac" field if the given value is not nil.
  432. func (tuo *TokenUpdateOne) SetNillableMAC(s *string) *TokenUpdateOne {
  433. if s != nil {
  434. tuo.SetMAC(*s)
  435. }
  436. return tuo
  437. }
  438. // ClearMAC clears the value of the "mac" field.
  439. func (tuo *TokenUpdateOne) ClearMAC() *TokenUpdateOne {
  440. tuo.mutation.ClearMAC()
  441. return tuo
  442. }
  443. // SetOrganizationID sets the "organization_id" field.
  444. func (tuo *TokenUpdateOne) SetOrganizationID(u uint64) *TokenUpdateOne {
  445. tuo.mutation.ResetOrganizationID()
  446. tuo.mutation.SetOrganizationID(u)
  447. return tuo
  448. }
  449. // SetNillableOrganizationID sets the "organization_id" field if the given value is not nil.
  450. func (tuo *TokenUpdateOne) SetNillableOrganizationID(u *uint64) *TokenUpdateOne {
  451. if u != nil {
  452. tuo.SetOrganizationID(*u)
  453. }
  454. return tuo
  455. }
  456. // AddOrganizationID adds u to the "organization_id" field.
  457. func (tuo *TokenUpdateOne) AddOrganizationID(u int64) *TokenUpdateOne {
  458. tuo.mutation.AddOrganizationID(u)
  459. return tuo
  460. }
  461. // SetAgentID sets the "agent_id" field.
  462. func (tuo *TokenUpdateOne) SetAgentID(u uint64) *TokenUpdateOne {
  463. tuo.mutation.SetAgentID(u)
  464. return tuo
  465. }
  466. // SetNillableAgentID sets the "agent_id" field if the given value is not nil.
  467. func (tuo *TokenUpdateOne) SetNillableAgentID(u *uint64) *TokenUpdateOne {
  468. if u != nil {
  469. tuo.SetAgentID(*u)
  470. }
  471. return tuo
  472. }
  473. // SetCustomAgentBase sets the "custom_agent_base" field.
  474. func (tuo *TokenUpdateOne) SetCustomAgentBase(s string) *TokenUpdateOne {
  475. tuo.mutation.SetCustomAgentBase(s)
  476. return tuo
  477. }
  478. // SetNillableCustomAgentBase sets the "custom_agent_base" field if the given value is not nil.
  479. func (tuo *TokenUpdateOne) SetNillableCustomAgentBase(s *string) *TokenUpdateOne {
  480. if s != nil {
  481. tuo.SetCustomAgentBase(*s)
  482. }
  483. return tuo
  484. }
  485. // ClearCustomAgentBase clears the value of the "custom_agent_base" field.
  486. func (tuo *TokenUpdateOne) ClearCustomAgentBase() *TokenUpdateOne {
  487. tuo.mutation.ClearCustomAgentBase()
  488. return tuo
  489. }
  490. // SetCustomAgentKey sets the "custom_agent_key" field.
  491. func (tuo *TokenUpdateOne) SetCustomAgentKey(s string) *TokenUpdateOne {
  492. tuo.mutation.SetCustomAgentKey(s)
  493. return tuo
  494. }
  495. // SetNillableCustomAgentKey sets the "custom_agent_key" field if the given value is not nil.
  496. func (tuo *TokenUpdateOne) SetNillableCustomAgentKey(s *string) *TokenUpdateOne {
  497. if s != nil {
  498. tuo.SetCustomAgentKey(*s)
  499. }
  500. return tuo
  501. }
  502. // ClearCustomAgentKey clears the value of the "custom_agent_key" field.
  503. func (tuo *TokenUpdateOne) ClearCustomAgentKey() *TokenUpdateOne {
  504. tuo.mutation.ClearCustomAgentKey()
  505. return tuo
  506. }
  507. // SetOpenaiBase sets the "openai_base" field.
  508. func (tuo *TokenUpdateOne) SetOpenaiBase(s string) *TokenUpdateOne {
  509. tuo.mutation.SetOpenaiBase(s)
  510. return tuo
  511. }
  512. // SetNillableOpenaiBase sets the "openai_base" field if the given value is not nil.
  513. func (tuo *TokenUpdateOne) SetNillableOpenaiBase(s *string) *TokenUpdateOne {
  514. if s != nil {
  515. tuo.SetOpenaiBase(*s)
  516. }
  517. return tuo
  518. }
  519. // ClearOpenaiBase clears the value of the "openai_base" field.
  520. func (tuo *TokenUpdateOne) ClearOpenaiBase() *TokenUpdateOne {
  521. tuo.mutation.ClearOpenaiBase()
  522. return tuo
  523. }
  524. // SetOpenaiKey sets the "openai_key" field.
  525. func (tuo *TokenUpdateOne) SetOpenaiKey(s string) *TokenUpdateOne {
  526. tuo.mutation.SetOpenaiKey(s)
  527. return tuo
  528. }
  529. // SetNillableOpenaiKey sets the "openai_key" field if the given value is not nil.
  530. func (tuo *TokenUpdateOne) SetNillableOpenaiKey(s *string) *TokenUpdateOne {
  531. if s != nil {
  532. tuo.SetOpenaiKey(*s)
  533. }
  534. return tuo
  535. }
  536. // ClearOpenaiKey clears the value of the "openai_key" field.
  537. func (tuo *TokenUpdateOne) ClearOpenaiKey() *TokenUpdateOne {
  538. tuo.mutation.ClearOpenaiKey()
  539. return tuo
  540. }
  541. // SetAgent sets the "agent" edge to the Agent entity.
  542. func (tuo *TokenUpdateOne) SetAgent(a *Agent) *TokenUpdateOne {
  543. return tuo.SetAgentID(a.ID)
  544. }
  545. // Mutation returns the TokenMutation object of the builder.
  546. func (tuo *TokenUpdateOne) Mutation() *TokenMutation {
  547. return tuo.mutation
  548. }
  549. // ClearAgent clears the "agent" edge to the Agent entity.
  550. func (tuo *TokenUpdateOne) ClearAgent() *TokenUpdateOne {
  551. tuo.mutation.ClearAgent()
  552. return tuo
  553. }
  554. // Where appends a list predicates to the TokenUpdate builder.
  555. func (tuo *TokenUpdateOne) Where(ps ...predicate.Token) *TokenUpdateOne {
  556. tuo.mutation.Where(ps...)
  557. return tuo
  558. }
  559. // Select allows selecting one or more fields (columns) of the returned entity.
  560. // The default is selecting all fields defined in the entity schema.
  561. func (tuo *TokenUpdateOne) Select(field string, fields ...string) *TokenUpdateOne {
  562. tuo.fields = append([]string{field}, fields...)
  563. return tuo
  564. }
  565. // Save executes the query and returns the updated Token entity.
  566. func (tuo *TokenUpdateOne) Save(ctx context.Context) (*Token, error) {
  567. if err := tuo.defaults(); err != nil {
  568. return nil, err
  569. }
  570. return withHooks(ctx, tuo.sqlSave, tuo.mutation, tuo.hooks)
  571. }
  572. // SaveX is like Save, but panics if an error occurs.
  573. func (tuo *TokenUpdateOne) SaveX(ctx context.Context) *Token {
  574. node, err := tuo.Save(ctx)
  575. if err != nil {
  576. panic(err)
  577. }
  578. return node
  579. }
  580. // Exec executes the query on the entity.
  581. func (tuo *TokenUpdateOne) Exec(ctx context.Context) error {
  582. _, err := tuo.Save(ctx)
  583. return err
  584. }
  585. // ExecX is like Exec, but panics if an error occurs.
  586. func (tuo *TokenUpdateOne) ExecX(ctx context.Context) {
  587. if err := tuo.Exec(ctx); err != nil {
  588. panic(err)
  589. }
  590. }
  591. // defaults sets the default values of the builder before save.
  592. func (tuo *TokenUpdateOne) defaults() error {
  593. if _, ok := tuo.mutation.UpdatedAt(); !ok {
  594. if token.UpdateDefaultUpdatedAt == nil {
  595. return fmt.Errorf("ent: uninitialized token.UpdateDefaultUpdatedAt (forgotten import ent/runtime?)")
  596. }
  597. v := token.UpdateDefaultUpdatedAt()
  598. tuo.mutation.SetUpdatedAt(v)
  599. }
  600. return nil
  601. }
  602. // check runs all checks and user-defined validators on the builder.
  603. func (tuo *TokenUpdateOne) check() error {
  604. if _, ok := tuo.mutation.AgentID(); tuo.mutation.AgentCleared() && !ok {
  605. return errors.New(`ent: clearing a required unique edge "Token.agent"`)
  606. }
  607. return nil
  608. }
  609. func (tuo *TokenUpdateOne) sqlSave(ctx context.Context) (_node *Token, err error) {
  610. if err := tuo.check(); err != nil {
  611. return _node, err
  612. }
  613. _spec := sqlgraph.NewUpdateSpec(token.Table, token.Columns, sqlgraph.NewFieldSpec(token.FieldID, field.TypeUint64))
  614. id, ok := tuo.mutation.ID()
  615. if !ok {
  616. return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Token.id" for update`)}
  617. }
  618. _spec.Node.ID.Value = id
  619. if fields := tuo.fields; len(fields) > 0 {
  620. _spec.Node.Columns = make([]string, 0, len(fields))
  621. _spec.Node.Columns = append(_spec.Node.Columns, token.FieldID)
  622. for _, f := range fields {
  623. if !token.ValidColumn(f) {
  624. return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
  625. }
  626. if f != token.FieldID {
  627. _spec.Node.Columns = append(_spec.Node.Columns, f)
  628. }
  629. }
  630. }
  631. if ps := tuo.mutation.predicates; len(ps) > 0 {
  632. _spec.Predicate = func(selector *sql.Selector) {
  633. for i := range ps {
  634. ps[i](selector)
  635. }
  636. }
  637. }
  638. if value, ok := tuo.mutation.UpdatedAt(); ok {
  639. _spec.SetField(token.FieldUpdatedAt, field.TypeTime, value)
  640. }
  641. if value, ok := tuo.mutation.DeletedAt(); ok {
  642. _spec.SetField(token.FieldDeletedAt, field.TypeTime, value)
  643. }
  644. if tuo.mutation.DeletedAtCleared() {
  645. _spec.ClearField(token.FieldDeletedAt, field.TypeTime)
  646. }
  647. if value, ok := tuo.mutation.ExpireAt(); ok {
  648. _spec.SetField(token.FieldExpireAt, field.TypeTime, value)
  649. }
  650. if tuo.mutation.ExpireAtCleared() {
  651. _spec.ClearField(token.FieldExpireAt, field.TypeTime)
  652. }
  653. if value, ok := tuo.mutation.Token(); ok {
  654. _spec.SetField(token.FieldToken, field.TypeString, value)
  655. }
  656. if tuo.mutation.TokenCleared() {
  657. _spec.ClearField(token.FieldToken, field.TypeString)
  658. }
  659. if value, ok := tuo.mutation.MAC(); ok {
  660. _spec.SetField(token.FieldMAC, field.TypeString, value)
  661. }
  662. if tuo.mutation.MACCleared() {
  663. _spec.ClearField(token.FieldMAC, field.TypeString)
  664. }
  665. if value, ok := tuo.mutation.OrganizationID(); ok {
  666. _spec.SetField(token.FieldOrganizationID, field.TypeUint64, value)
  667. }
  668. if value, ok := tuo.mutation.AddedOrganizationID(); ok {
  669. _spec.AddField(token.FieldOrganizationID, field.TypeUint64, value)
  670. }
  671. if value, ok := tuo.mutation.CustomAgentBase(); ok {
  672. _spec.SetField(token.FieldCustomAgentBase, field.TypeString, value)
  673. }
  674. if tuo.mutation.CustomAgentBaseCleared() {
  675. _spec.ClearField(token.FieldCustomAgentBase, field.TypeString)
  676. }
  677. if value, ok := tuo.mutation.CustomAgentKey(); ok {
  678. _spec.SetField(token.FieldCustomAgentKey, field.TypeString, value)
  679. }
  680. if tuo.mutation.CustomAgentKeyCleared() {
  681. _spec.ClearField(token.FieldCustomAgentKey, field.TypeString)
  682. }
  683. if value, ok := tuo.mutation.OpenaiBase(); ok {
  684. _spec.SetField(token.FieldOpenaiBase, field.TypeString, value)
  685. }
  686. if tuo.mutation.OpenaiBaseCleared() {
  687. _spec.ClearField(token.FieldOpenaiBase, field.TypeString)
  688. }
  689. if value, ok := tuo.mutation.OpenaiKey(); ok {
  690. _spec.SetField(token.FieldOpenaiKey, field.TypeString, value)
  691. }
  692. if tuo.mutation.OpenaiKeyCleared() {
  693. _spec.ClearField(token.FieldOpenaiKey, field.TypeString)
  694. }
  695. if tuo.mutation.AgentCleared() {
  696. edge := &sqlgraph.EdgeSpec{
  697. Rel: sqlgraph.M2O,
  698. Inverse: true,
  699. Table: token.AgentTable,
  700. Columns: []string{token.AgentColumn},
  701. Bidi: false,
  702. Target: &sqlgraph.EdgeTarget{
  703. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  704. },
  705. }
  706. _spec.Edges.Clear = append(_spec.Edges.Clear, edge)
  707. }
  708. if nodes := tuo.mutation.AgentIDs(); len(nodes) > 0 {
  709. edge := &sqlgraph.EdgeSpec{
  710. Rel: sqlgraph.M2O,
  711. Inverse: true,
  712. Table: token.AgentTable,
  713. Columns: []string{token.AgentColumn},
  714. Bidi: false,
  715. Target: &sqlgraph.EdgeTarget{
  716. IDSpec: sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64),
  717. },
  718. }
  719. for _, k := range nodes {
  720. edge.Target.Nodes = append(edge.Target.Nodes, k)
  721. }
  722. _spec.Edges.Add = append(_spec.Edges.Add, edge)
  723. }
  724. _node = &Token{config: tuo.config}
  725. _spec.Assign = _node.assignValues
  726. _spec.ScanValues = _node.scanValues
  727. if err = sqlgraph.UpdateNode(ctx, tuo.driver, _spec); err != nil {
  728. if _, ok := err.(*sqlgraph.NotFoundError); ok {
  729. err = &NotFoundError{token.Label}
  730. } else if sqlgraph.IsConstraintError(err) {
  731. err = &ConstraintError{msg: err.Error(), wrap: err}
  732. }
  733. return nil, err
  734. }
  735. tuo.mutation.done = true
  736. return _node, nil
  737. }