agent_create.go 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. "entgo.io/ent/dialect/sql"
  10. "entgo.io/ent/dialect/sql/sqlgraph"
  11. "entgo.io/ent/schema/field"
  12. )
  13. // AgentCreate is the builder for creating a Agent entity.
  14. type AgentCreate struct {
  15. config
  16. mutation *AgentMutation
  17. hooks []Hook
  18. conflict []sql.ConflictOption
  19. }
  20. // SetCreatedAt sets the "created_at" field.
  21. func (ac *AgentCreate) SetCreatedAt(t time.Time) *AgentCreate {
  22. ac.mutation.SetCreatedAt(t)
  23. return ac
  24. }
  25. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  26. func (ac *AgentCreate) SetNillableCreatedAt(t *time.Time) *AgentCreate {
  27. if t != nil {
  28. ac.SetCreatedAt(*t)
  29. }
  30. return ac
  31. }
  32. // SetUpdatedAt sets the "updated_at" field.
  33. func (ac *AgentCreate) SetUpdatedAt(t time.Time) *AgentCreate {
  34. ac.mutation.SetUpdatedAt(t)
  35. return ac
  36. }
  37. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  38. func (ac *AgentCreate) SetNillableUpdatedAt(t *time.Time) *AgentCreate {
  39. if t != nil {
  40. ac.SetUpdatedAt(*t)
  41. }
  42. return ac
  43. }
  44. // SetDeletedAt sets the "deleted_at" field.
  45. func (ac *AgentCreate) SetDeletedAt(t time.Time) *AgentCreate {
  46. ac.mutation.SetDeletedAt(t)
  47. return ac
  48. }
  49. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  50. func (ac *AgentCreate) SetNillableDeletedAt(t *time.Time) *AgentCreate {
  51. if t != nil {
  52. ac.SetDeletedAt(*t)
  53. }
  54. return ac
  55. }
  56. // SetName sets the "name" field.
  57. func (ac *AgentCreate) SetName(s string) *AgentCreate {
  58. ac.mutation.SetName(s)
  59. return ac
  60. }
  61. // SetRole sets the "role" field.
  62. func (ac *AgentCreate) SetRole(s string) *AgentCreate {
  63. ac.mutation.SetRole(s)
  64. return ac
  65. }
  66. // SetStatus sets the "status" field.
  67. func (ac *AgentCreate) SetStatus(i int) *AgentCreate {
  68. ac.mutation.SetStatus(i)
  69. return ac
  70. }
  71. // SetNillableStatus sets the "status" field if the given value is not nil.
  72. func (ac *AgentCreate) SetNillableStatus(i *int) *AgentCreate {
  73. if i != nil {
  74. ac.SetStatus(*i)
  75. }
  76. return ac
  77. }
  78. // SetBackground sets the "background" field.
  79. func (ac *AgentCreate) SetBackground(s string) *AgentCreate {
  80. ac.mutation.SetBackground(s)
  81. return ac
  82. }
  83. // SetExamples sets the "examples" field.
  84. func (ac *AgentCreate) SetExamples(s string) *AgentCreate {
  85. ac.mutation.SetExamples(s)
  86. return ac
  87. }
  88. // SetOrganizationID sets the "organization_id" field.
  89. func (ac *AgentCreate) SetOrganizationID(u uint64) *AgentCreate {
  90. ac.mutation.SetOrganizationID(u)
  91. return ac
  92. }
  93. // SetID sets the "id" field.
  94. func (ac *AgentCreate) SetID(u uint64) *AgentCreate {
  95. ac.mutation.SetID(u)
  96. return ac
  97. }
  98. // Mutation returns the AgentMutation object of the builder.
  99. func (ac *AgentCreate) Mutation() *AgentMutation {
  100. return ac.mutation
  101. }
  102. // Save creates the Agent in the database.
  103. func (ac *AgentCreate) Save(ctx context.Context) (*Agent, error) {
  104. if err := ac.defaults(); err != nil {
  105. return nil, err
  106. }
  107. return withHooks(ctx, ac.sqlSave, ac.mutation, ac.hooks)
  108. }
  109. // SaveX calls Save and panics if Save returns an error.
  110. func (ac *AgentCreate) SaveX(ctx context.Context) *Agent {
  111. v, err := ac.Save(ctx)
  112. if err != nil {
  113. panic(err)
  114. }
  115. return v
  116. }
  117. // Exec executes the query.
  118. func (ac *AgentCreate) Exec(ctx context.Context) error {
  119. _, err := ac.Save(ctx)
  120. return err
  121. }
  122. // ExecX is like Exec, but panics if an error occurs.
  123. func (ac *AgentCreate) ExecX(ctx context.Context) {
  124. if err := ac.Exec(ctx); err != nil {
  125. panic(err)
  126. }
  127. }
  128. // defaults sets the default values of the builder before save.
  129. func (ac *AgentCreate) defaults() error {
  130. if _, ok := ac.mutation.CreatedAt(); !ok {
  131. if agent.DefaultCreatedAt == nil {
  132. return fmt.Errorf("ent: uninitialized agent.DefaultCreatedAt (forgotten import ent/runtime?)")
  133. }
  134. v := agent.DefaultCreatedAt()
  135. ac.mutation.SetCreatedAt(v)
  136. }
  137. if _, ok := ac.mutation.UpdatedAt(); !ok {
  138. if agent.DefaultUpdatedAt == nil {
  139. return fmt.Errorf("ent: uninitialized agent.DefaultUpdatedAt (forgotten import ent/runtime?)")
  140. }
  141. v := agent.DefaultUpdatedAt()
  142. ac.mutation.SetUpdatedAt(v)
  143. }
  144. if _, ok := ac.mutation.Status(); !ok {
  145. v := agent.DefaultStatus
  146. ac.mutation.SetStatus(v)
  147. }
  148. return nil
  149. }
  150. // check runs all checks and user-defined validators on the builder.
  151. func (ac *AgentCreate) check() error {
  152. if _, ok := ac.mutation.CreatedAt(); !ok {
  153. return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Agent.created_at"`)}
  154. }
  155. if _, ok := ac.mutation.UpdatedAt(); !ok {
  156. return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Agent.updated_at"`)}
  157. }
  158. if _, ok := ac.mutation.Name(); !ok {
  159. return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "Agent.name"`)}
  160. }
  161. if v, ok := ac.mutation.Name(); ok {
  162. if err := agent.NameValidator(v); err != nil {
  163. return &ValidationError{Name: "name", err: fmt.Errorf(`ent: validator failed for field "Agent.name": %w`, err)}
  164. }
  165. }
  166. if _, ok := ac.mutation.Role(); !ok {
  167. return &ValidationError{Name: "role", err: errors.New(`ent: missing required field "Agent.role"`)}
  168. }
  169. if v, ok := ac.mutation.Role(); ok {
  170. if err := agent.RoleValidator(v); err != nil {
  171. return &ValidationError{Name: "role", err: fmt.Errorf(`ent: validator failed for field "Agent.role": %w`, err)}
  172. }
  173. }
  174. if _, ok := ac.mutation.Background(); !ok {
  175. return &ValidationError{Name: "background", err: errors.New(`ent: missing required field "Agent.background"`)}
  176. }
  177. if v, ok := ac.mutation.Background(); ok {
  178. if err := agent.BackgroundValidator(v); err != nil {
  179. return &ValidationError{Name: "background", err: fmt.Errorf(`ent: validator failed for field "Agent.background": %w`, err)}
  180. }
  181. }
  182. if _, ok := ac.mutation.Examples(); !ok {
  183. return &ValidationError{Name: "examples", err: errors.New(`ent: missing required field "Agent.examples"`)}
  184. }
  185. if v, ok := ac.mutation.Examples(); ok {
  186. if err := agent.ExamplesValidator(v); err != nil {
  187. return &ValidationError{Name: "examples", err: fmt.Errorf(`ent: validator failed for field "Agent.examples": %w`, err)}
  188. }
  189. }
  190. if _, ok := ac.mutation.OrganizationID(); !ok {
  191. return &ValidationError{Name: "organization_id", err: errors.New(`ent: missing required field "Agent.organization_id"`)}
  192. }
  193. return nil
  194. }
  195. func (ac *AgentCreate) sqlSave(ctx context.Context) (*Agent, error) {
  196. if err := ac.check(); err != nil {
  197. return nil, err
  198. }
  199. _node, _spec := ac.createSpec()
  200. if err := sqlgraph.CreateNode(ctx, ac.driver, _spec); err != nil {
  201. if sqlgraph.IsConstraintError(err) {
  202. err = &ConstraintError{msg: err.Error(), wrap: err}
  203. }
  204. return nil, err
  205. }
  206. if _spec.ID.Value != _node.ID {
  207. id := _spec.ID.Value.(int64)
  208. _node.ID = uint64(id)
  209. }
  210. ac.mutation.id = &_node.ID
  211. ac.mutation.done = true
  212. return _node, nil
  213. }
  214. func (ac *AgentCreate) createSpec() (*Agent, *sqlgraph.CreateSpec) {
  215. var (
  216. _node = &Agent{config: ac.config}
  217. _spec = sqlgraph.NewCreateSpec(agent.Table, sqlgraph.NewFieldSpec(agent.FieldID, field.TypeUint64))
  218. )
  219. _spec.OnConflict = ac.conflict
  220. if id, ok := ac.mutation.ID(); ok {
  221. _node.ID = id
  222. _spec.ID.Value = id
  223. }
  224. if value, ok := ac.mutation.CreatedAt(); ok {
  225. _spec.SetField(agent.FieldCreatedAt, field.TypeTime, value)
  226. _node.CreatedAt = value
  227. }
  228. if value, ok := ac.mutation.UpdatedAt(); ok {
  229. _spec.SetField(agent.FieldUpdatedAt, field.TypeTime, value)
  230. _node.UpdatedAt = value
  231. }
  232. if value, ok := ac.mutation.DeletedAt(); ok {
  233. _spec.SetField(agent.FieldDeletedAt, field.TypeTime, value)
  234. _node.DeletedAt = value
  235. }
  236. if value, ok := ac.mutation.Name(); ok {
  237. _spec.SetField(agent.FieldName, field.TypeString, value)
  238. _node.Name = value
  239. }
  240. if value, ok := ac.mutation.Role(); ok {
  241. _spec.SetField(agent.FieldRole, field.TypeString, value)
  242. _node.Role = value
  243. }
  244. if value, ok := ac.mutation.Status(); ok {
  245. _spec.SetField(agent.FieldStatus, field.TypeInt, value)
  246. _node.Status = value
  247. }
  248. if value, ok := ac.mutation.Background(); ok {
  249. _spec.SetField(agent.FieldBackground, field.TypeString, value)
  250. _node.Background = value
  251. }
  252. if value, ok := ac.mutation.Examples(); ok {
  253. _spec.SetField(agent.FieldExamples, field.TypeString, value)
  254. _node.Examples = value
  255. }
  256. if value, ok := ac.mutation.OrganizationID(); ok {
  257. _spec.SetField(agent.FieldOrganizationID, field.TypeUint64, value)
  258. _node.OrganizationID = value
  259. }
  260. return _node, _spec
  261. }
  262. // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
  263. // of the `INSERT` statement. For example:
  264. //
  265. // client.Agent.Create().
  266. // SetCreatedAt(v).
  267. // OnConflict(
  268. // // Update the row with the new values
  269. // // the was proposed for insertion.
  270. // sql.ResolveWithNewValues(),
  271. // ).
  272. // // Override some of the fields with custom
  273. // // update values.
  274. // Update(func(u *ent.AgentUpsert) {
  275. // SetCreatedAt(v+v).
  276. // }).
  277. // Exec(ctx)
  278. func (ac *AgentCreate) OnConflict(opts ...sql.ConflictOption) *AgentUpsertOne {
  279. ac.conflict = opts
  280. return &AgentUpsertOne{
  281. create: ac,
  282. }
  283. }
  284. // OnConflictColumns calls `OnConflict` and configures the columns
  285. // as conflict target. Using this option is equivalent to using:
  286. //
  287. // client.Agent.Create().
  288. // OnConflict(sql.ConflictColumns(columns...)).
  289. // Exec(ctx)
  290. func (ac *AgentCreate) OnConflictColumns(columns ...string) *AgentUpsertOne {
  291. ac.conflict = append(ac.conflict, sql.ConflictColumns(columns...))
  292. return &AgentUpsertOne{
  293. create: ac,
  294. }
  295. }
  296. type (
  297. // AgentUpsertOne is the builder for "upsert"-ing
  298. // one Agent node.
  299. AgentUpsertOne struct {
  300. create *AgentCreate
  301. }
  302. // AgentUpsert is the "OnConflict" setter.
  303. AgentUpsert struct {
  304. *sql.UpdateSet
  305. }
  306. )
  307. // SetUpdatedAt sets the "updated_at" field.
  308. func (u *AgentUpsert) SetUpdatedAt(v time.Time) *AgentUpsert {
  309. u.Set(agent.FieldUpdatedAt, v)
  310. return u
  311. }
  312. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  313. func (u *AgentUpsert) UpdateUpdatedAt() *AgentUpsert {
  314. u.SetExcluded(agent.FieldUpdatedAt)
  315. return u
  316. }
  317. // SetDeletedAt sets the "deleted_at" field.
  318. func (u *AgentUpsert) SetDeletedAt(v time.Time) *AgentUpsert {
  319. u.Set(agent.FieldDeletedAt, v)
  320. return u
  321. }
  322. // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
  323. func (u *AgentUpsert) UpdateDeletedAt() *AgentUpsert {
  324. u.SetExcluded(agent.FieldDeletedAt)
  325. return u
  326. }
  327. // ClearDeletedAt clears the value of the "deleted_at" field.
  328. func (u *AgentUpsert) ClearDeletedAt() *AgentUpsert {
  329. u.SetNull(agent.FieldDeletedAt)
  330. return u
  331. }
  332. // SetName sets the "name" field.
  333. func (u *AgentUpsert) SetName(v string) *AgentUpsert {
  334. u.Set(agent.FieldName, v)
  335. return u
  336. }
  337. // UpdateName sets the "name" field to the value that was provided on create.
  338. func (u *AgentUpsert) UpdateName() *AgentUpsert {
  339. u.SetExcluded(agent.FieldName)
  340. return u
  341. }
  342. // SetRole sets the "role" field.
  343. func (u *AgentUpsert) SetRole(v string) *AgentUpsert {
  344. u.Set(agent.FieldRole, v)
  345. return u
  346. }
  347. // UpdateRole sets the "role" field to the value that was provided on create.
  348. func (u *AgentUpsert) UpdateRole() *AgentUpsert {
  349. u.SetExcluded(agent.FieldRole)
  350. return u
  351. }
  352. // SetStatus sets the "status" field.
  353. func (u *AgentUpsert) SetStatus(v int) *AgentUpsert {
  354. u.Set(agent.FieldStatus, v)
  355. return u
  356. }
  357. // UpdateStatus sets the "status" field to the value that was provided on create.
  358. func (u *AgentUpsert) UpdateStatus() *AgentUpsert {
  359. u.SetExcluded(agent.FieldStatus)
  360. return u
  361. }
  362. // AddStatus adds v to the "status" field.
  363. func (u *AgentUpsert) AddStatus(v int) *AgentUpsert {
  364. u.Add(agent.FieldStatus, v)
  365. return u
  366. }
  367. // ClearStatus clears the value of the "status" field.
  368. func (u *AgentUpsert) ClearStatus() *AgentUpsert {
  369. u.SetNull(agent.FieldStatus)
  370. return u
  371. }
  372. // SetBackground sets the "background" field.
  373. func (u *AgentUpsert) SetBackground(v string) *AgentUpsert {
  374. u.Set(agent.FieldBackground, v)
  375. return u
  376. }
  377. // UpdateBackground sets the "background" field to the value that was provided on create.
  378. func (u *AgentUpsert) UpdateBackground() *AgentUpsert {
  379. u.SetExcluded(agent.FieldBackground)
  380. return u
  381. }
  382. // SetExamples sets the "examples" field.
  383. func (u *AgentUpsert) SetExamples(v string) *AgentUpsert {
  384. u.Set(agent.FieldExamples, v)
  385. return u
  386. }
  387. // UpdateExamples sets the "examples" field to the value that was provided on create.
  388. func (u *AgentUpsert) UpdateExamples() *AgentUpsert {
  389. u.SetExcluded(agent.FieldExamples)
  390. return u
  391. }
  392. // SetOrganizationID sets the "organization_id" field.
  393. func (u *AgentUpsert) SetOrganizationID(v uint64) *AgentUpsert {
  394. u.Set(agent.FieldOrganizationID, v)
  395. return u
  396. }
  397. // UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
  398. func (u *AgentUpsert) UpdateOrganizationID() *AgentUpsert {
  399. u.SetExcluded(agent.FieldOrganizationID)
  400. return u
  401. }
  402. // AddOrganizationID adds v to the "organization_id" field.
  403. func (u *AgentUpsert) AddOrganizationID(v uint64) *AgentUpsert {
  404. u.Add(agent.FieldOrganizationID, v)
  405. return u
  406. }
  407. // UpdateNewValues updates the mutable fields using the new values that were set on create except the ID field.
  408. // Using this option is equivalent to using:
  409. //
  410. // client.Agent.Create().
  411. // OnConflict(
  412. // sql.ResolveWithNewValues(),
  413. // sql.ResolveWith(func(u *sql.UpdateSet) {
  414. // u.SetIgnore(agent.FieldID)
  415. // }),
  416. // ).
  417. // Exec(ctx)
  418. func (u *AgentUpsertOne) UpdateNewValues() *AgentUpsertOne {
  419. u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
  420. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
  421. if _, exists := u.create.mutation.ID(); exists {
  422. s.SetIgnore(agent.FieldID)
  423. }
  424. if _, exists := u.create.mutation.CreatedAt(); exists {
  425. s.SetIgnore(agent.FieldCreatedAt)
  426. }
  427. }))
  428. return u
  429. }
  430. // Ignore sets each column to itself in case of conflict.
  431. // Using this option is equivalent to using:
  432. //
  433. // client.Agent.Create().
  434. // OnConflict(sql.ResolveWithIgnore()).
  435. // Exec(ctx)
  436. func (u *AgentUpsertOne) Ignore() *AgentUpsertOne {
  437. u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
  438. return u
  439. }
  440. // DoNothing configures the conflict_action to `DO NOTHING`.
  441. // Supported only by SQLite and PostgreSQL.
  442. func (u *AgentUpsertOne) DoNothing() *AgentUpsertOne {
  443. u.create.conflict = append(u.create.conflict, sql.DoNothing())
  444. return u
  445. }
  446. // Update allows overriding fields `UPDATE` values. See the AgentCreate.OnConflict
  447. // documentation for more info.
  448. func (u *AgentUpsertOne) Update(set func(*AgentUpsert)) *AgentUpsertOne {
  449. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
  450. set(&AgentUpsert{UpdateSet: update})
  451. }))
  452. return u
  453. }
  454. // SetUpdatedAt sets the "updated_at" field.
  455. func (u *AgentUpsertOne) SetUpdatedAt(v time.Time) *AgentUpsertOne {
  456. return u.Update(func(s *AgentUpsert) {
  457. s.SetUpdatedAt(v)
  458. })
  459. }
  460. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  461. func (u *AgentUpsertOne) UpdateUpdatedAt() *AgentUpsertOne {
  462. return u.Update(func(s *AgentUpsert) {
  463. s.UpdateUpdatedAt()
  464. })
  465. }
  466. // SetDeletedAt sets the "deleted_at" field.
  467. func (u *AgentUpsertOne) SetDeletedAt(v time.Time) *AgentUpsertOne {
  468. return u.Update(func(s *AgentUpsert) {
  469. s.SetDeletedAt(v)
  470. })
  471. }
  472. // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
  473. func (u *AgentUpsertOne) UpdateDeletedAt() *AgentUpsertOne {
  474. return u.Update(func(s *AgentUpsert) {
  475. s.UpdateDeletedAt()
  476. })
  477. }
  478. // ClearDeletedAt clears the value of the "deleted_at" field.
  479. func (u *AgentUpsertOne) ClearDeletedAt() *AgentUpsertOne {
  480. return u.Update(func(s *AgentUpsert) {
  481. s.ClearDeletedAt()
  482. })
  483. }
  484. // SetName sets the "name" field.
  485. func (u *AgentUpsertOne) SetName(v string) *AgentUpsertOne {
  486. return u.Update(func(s *AgentUpsert) {
  487. s.SetName(v)
  488. })
  489. }
  490. // UpdateName sets the "name" field to the value that was provided on create.
  491. func (u *AgentUpsertOne) UpdateName() *AgentUpsertOne {
  492. return u.Update(func(s *AgentUpsert) {
  493. s.UpdateName()
  494. })
  495. }
  496. // SetRole sets the "role" field.
  497. func (u *AgentUpsertOne) SetRole(v string) *AgentUpsertOne {
  498. return u.Update(func(s *AgentUpsert) {
  499. s.SetRole(v)
  500. })
  501. }
  502. // UpdateRole sets the "role" field to the value that was provided on create.
  503. func (u *AgentUpsertOne) UpdateRole() *AgentUpsertOne {
  504. return u.Update(func(s *AgentUpsert) {
  505. s.UpdateRole()
  506. })
  507. }
  508. // SetStatus sets the "status" field.
  509. func (u *AgentUpsertOne) SetStatus(v int) *AgentUpsertOne {
  510. return u.Update(func(s *AgentUpsert) {
  511. s.SetStatus(v)
  512. })
  513. }
  514. // AddStatus adds v to the "status" field.
  515. func (u *AgentUpsertOne) AddStatus(v int) *AgentUpsertOne {
  516. return u.Update(func(s *AgentUpsert) {
  517. s.AddStatus(v)
  518. })
  519. }
  520. // UpdateStatus sets the "status" field to the value that was provided on create.
  521. func (u *AgentUpsertOne) UpdateStatus() *AgentUpsertOne {
  522. return u.Update(func(s *AgentUpsert) {
  523. s.UpdateStatus()
  524. })
  525. }
  526. // ClearStatus clears the value of the "status" field.
  527. func (u *AgentUpsertOne) ClearStatus() *AgentUpsertOne {
  528. return u.Update(func(s *AgentUpsert) {
  529. s.ClearStatus()
  530. })
  531. }
  532. // SetBackground sets the "background" field.
  533. func (u *AgentUpsertOne) SetBackground(v string) *AgentUpsertOne {
  534. return u.Update(func(s *AgentUpsert) {
  535. s.SetBackground(v)
  536. })
  537. }
  538. // UpdateBackground sets the "background" field to the value that was provided on create.
  539. func (u *AgentUpsertOne) UpdateBackground() *AgentUpsertOne {
  540. return u.Update(func(s *AgentUpsert) {
  541. s.UpdateBackground()
  542. })
  543. }
  544. // SetExamples sets the "examples" field.
  545. func (u *AgentUpsertOne) SetExamples(v string) *AgentUpsertOne {
  546. return u.Update(func(s *AgentUpsert) {
  547. s.SetExamples(v)
  548. })
  549. }
  550. // UpdateExamples sets the "examples" field to the value that was provided on create.
  551. func (u *AgentUpsertOne) UpdateExamples() *AgentUpsertOne {
  552. return u.Update(func(s *AgentUpsert) {
  553. s.UpdateExamples()
  554. })
  555. }
  556. // SetOrganizationID sets the "organization_id" field.
  557. func (u *AgentUpsertOne) SetOrganizationID(v uint64) *AgentUpsertOne {
  558. return u.Update(func(s *AgentUpsert) {
  559. s.SetOrganizationID(v)
  560. })
  561. }
  562. // AddOrganizationID adds v to the "organization_id" field.
  563. func (u *AgentUpsertOne) AddOrganizationID(v uint64) *AgentUpsertOne {
  564. return u.Update(func(s *AgentUpsert) {
  565. s.AddOrganizationID(v)
  566. })
  567. }
  568. // UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
  569. func (u *AgentUpsertOne) UpdateOrganizationID() *AgentUpsertOne {
  570. return u.Update(func(s *AgentUpsert) {
  571. s.UpdateOrganizationID()
  572. })
  573. }
  574. // Exec executes the query.
  575. func (u *AgentUpsertOne) Exec(ctx context.Context) error {
  576. if len(u.create.conflict) == 0 {
  577. return errors.New("ent: missing options for AgentCreate.OnConflict")
  578. }
  579. return u.create.Exec(ctx)
  580. }
  581. // ExecX is like Exec, but panics if an error occurs.
  582. func (u *AgentUpsertOne) ExecX(ctx context.Context) {
  583. if err := u.create.Exec(ctx); err != nil {
  584. panic(err)
  585. }
  586. }
  587. // Exec executes the UPSERT query and returns the inserted/updated ID.
  588. func (u *AgentUpsertOne) ID(ctx context.Context) (id uint64, err error) {
  589. node, err := u.create.Save(ctx)
  590. if err != nil {
  591. return id, err
  592. }
  593. return node.ID, nil
  594. }
  595. // IDX is like ID, but panics if an error occurs.
  596. func (u *AgentUpsertOne) IDX(ctx context.Context) uint64 {
  597. id, err := u.ID(ctx)
  598. if err != nil {
  599. panic(err)
  600. }
  601. return id
  602. }
  603. // AgentCreateBulk is the builder for creating many Agent entities in bulk.
  604. type AgentCreateBulk struct {
  605. config
  606. err error
  607. builders []*AgentCreate
  608. conflict []sql.ConflictOption
  609. }
  610. // Save creates the Agent entities in the database.
  611. func (acb *AgentCreateBulk) Save(ctx context.Context) ([]*Agent, error) {
  612. if acb.err != nil {
  613. return nil, acb.err
  614. }
  615. specs := make([]*sqlgraph.CreateSpec, len(acb.builders))
  616. nodes := make([]*Agent, len(acb.builders))
  617. mutators := make([]Mutator, len(acb.builders))
  618. for i := range acb.builders {
  619. func(i int, root context.Context) {
  620. builder := acb.builders[i]
  621. builder.defaults()
  622. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  623. mutation, ok := m.(*AgentMutation)
  624. if !ok {
  625. return nil, fmt.Errorf("unexpected mutation type %T", m)
  626. }
  627. if err := builder.check(); err != nil {
  628. return nil, err
  629. }
  630. builder.mutation = mutation
  631. var err error
  632. nodes[i], specs[i] = builder.createSpec()
  633. if i < len(mutators)-1 {
  634. _, err = mutators[i+1].Mutate(root, acb.builders[i+1].mutation)
  635. } else {
  636. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  637. spec.OnConflict = acb.conflict
  638. // Invoke the actual operation on the latest mutation in the chain.
  639. if err = sqlgraph.BatchCreate(ctx, acb.driver, spec); err != nil {
  640. if sqlgraph.IsConstraintError(err) {
  641. err = &ConstraintError{msg: err.Error(), wrap: err}
  642. }
  643. }
  644. }
  645. if err != nil {
  646. return nil, err
  647. }
  648. mutation.id = &nodes[i].ID
  649. if specs[i].ID.Value != nil && nodes[i].ID == 0 {
  650. id := specs[i].ID.Value.(int64)
  651. nodes[i].ID = uint64(id)
  652. }
  653. mutation.done = true
  654. return nodes[i], nil
  655. })
  656. for i := len(builder.hooks) - 1; i >= 0; i-- {
  657. mut = builder.hooks[i](mut)
  658. }
  659. mutators[i] = mut
  660. }(i, ctx)
  661. }
  662. if len(mutators) > 0 {
  663. if _, err := mutators[0].Mutate(ctx, acb.builders[0].mutation); err != nil {
  664. return nil, err
  665. }
  666. }
  667. return nodes, nil
  668. }
  669. // SaveX is like Save, but panics if an error occurs.
  670. func (acb *AgentCreateBulk) SaveX(ctx context.Context) []*Agent {
  671. v, err := acb.Save(ctx)
  672. if err != nil {
  673. panic(err)
  674. }
  675. return v
  676. }
  677. // Exec executes the query.
  678. func (acb *AgentCreateBulk) Exec(ctx context.Context) error {
  679. _, err := acb.Save(ctx)
  680. return err
  681. }
  682. // ExecX is like Exec, but panics if an error occurs.
  683. func (acb *AgentCreateBulk) ExecX(ctx context.Context) {
  684. if err := acb.Exec(ctx); err != nil {
  685. panic(err)
  686. }
  687. }
  688. // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
  689. // of the `INSERT` statement. For example:
  690. //
  691. // client.Agent.CreateBulk(builders...).
  692. // OnConflict(
  693. // // Update the row with the new values
  694. // // the was proposed for insertion.
  695. // sql.ResolveWithNewValues(),
  696. // ).
  697. // // Override some of the fields with custom
  698. // // update values.
  699. // Update(func(u *ent.AgentUpsert) {
  700. // SetCreatedAt(v+v).
  701. // }).
  702. // Exec(ctx)
  703. func (acb *AgentCreateBulk) OnConflict(opts ...sql.ConflictOption) *AgentUpsertBulk {
  704. acb.conflict = opts
  705. return &AgentUpsertBulk{
  706. create: acb,
  707. }
  708. }
  709. // OnConflictColumns calls `OnConflict` and configures the columns
  710. // as conflict target. Using this option is equivalent to using:
  711. //
  712. // client.Agent.Create().
  713. // OnConflict(sql.ConflictColumns(columns...)).
  714. // Exec(ctx)
  715. func (acb *AgentCreateBulk) OnConflictColumns(columns ...string) *AgentUpsertBulk {
  716. acb.conflict = append(acb.conflict, sql.ConflictColumns(columns...))
  717. return &AgentUpsertBulk{
  718. create: acb,
  719. }
  720. }
  721. // AgentUpsertBulk is the builder for "upsert"-ing
  722. // a bulk of Agent nodes.
  723. type AgentUpsertBulk struct {
  724. create *AgentCreateBulk
  725. }
  726. // UpdateNewValues updates the mutable fields using the new values that
  727. // were set on create. Using this option is equivalent to using:
  728. //
  729. // client.Agent.Create().
  730. // OnConflict(
  731. // sql.ResolveWithNewValues(),
  732. // sql.ResolveWith(func(u *sql.UpdateSet) {
  733. // u.SetIgnore(agent.FieldID)
  734. // }),
  735. // ).
  736. // Exec(ctx)
  737. func (u *AgentUpsertBulk) UpdateNewValues() *AgentUpsertBulk {
  738. u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
  739. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(s *sql.UpdateSet) {
  740. for _, b := range u.create.builders {
  741. if _, exists := b.mutation.ID(); exists {
  742. s.SetIgnore(agent.FieldID)
  743. }
  744. if _, exists := b.mutation.CreatedAt(); exists {
  745. s.SetIgnore(agent.FieldCreatedAt)
  746. }
  747. }
  748. }))
  749. return u
  750. }
  751. // Ignore sets each column to itself in case of conflict.
  752. // Using this option is equivalent to using:
  753. //
  754. // client.Agent.Create().
  755. // OnConflict(sql.ResolveWithIgnore()).
  756. // Exec(ctx)
  757. func (u *AgentUpsertBulk) Ignore() *AgentUpsertBulk {
  758. u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
  759. return u
  760. }
  761. // DoNothing configures the conflict_action to `DO NOTHING`.
  762. // Supported only by SQLite and PostgreSQL.
  763. func (u *AgentUpsertBulk) DoNothing() *AgentUpsertBulk {
  764. u.create.conflict = append(u.create.conflict, sql.DoNothing())
  765. return u
  766. }
  767. // Update allows overriding fields `UPDATE` values. See the AgentCreateBulk.OnConflict
  768. // documentation for more info.
  769. func (u *AgentUpsertBulk) Update(set func(*AgentUpsert)) *AgentUpsertBulk {
  770. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
  771. set(&AgentUpsert{UpdateSet: update})
  772. }))
  773. return u
  774. }
  775. // SetUpdatedAt sets the "updated_at" field.
  776. func (u *AgentUpsertBulk) SetUpdatedAt(v time.Time) *AgentUpsertBulk {
  777. return u.Update(func(s *AgentUpsert) {
  778. s.SetUpdatedAt(v)
  779. })
  780. }
  781. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  782. func (u *AgentUpsertBulk) UpdateUpdatedAt() *AgentUpsertBulk {
  783. return u.Update(func(s *AgentUpsert) {
  784. s.UpdateUpdatedAt()
  785. })
  786. }
  787. // SetDeletedAt sets the "deleted_at" field.
  788. func (u *AgentUpsertBulk) SetDeletedAt(v time.Time) *AgentUpsertBulk {
  789. return u.Update(func(s *AgentUpsert) {
  790. s.SetDeletedAt(v)
  791. })
  792. }
  793. // UpdateDeletedAt sets the "deleted_at" field to the value that was provided on create.
  794. func (u *AgentUpsertBulk) UpdateDeletedAt() *AgentUpsertBulk {
  795. return u.Update(func(s *AgentUpsert) {
  796. s.UpdateDeletedAt()
  797. })
  798. }
  799. // ClearDeletedAt clears the value of the "deleted_at" field.
  800. func (u *AgentUpsertBulk) ClearDeletedAt() *AgentUpsertBulk {
  801. return u.Update(func(s *AgentUpsert) {
  802. s.ClearDeletedAt()
  803. })
  804. }
  805. // SetName sets the "name" field.
  806. func (u *AgentUpsertBulk) SetName(v string) *AgentUpsertBulk {
  807. return u.Update(func(s *AgentUpsert) {
  808. s.SetName(v)
  809. })
  810. }
  811. // UpdateName sets the "name" field to the value that was provided on create.
  812. func (u *AgentUpsertBulk) UpdateName() *AgentUpsertBulk {
  813. return u.Update(func(s *AgentUpsert) {
  814. s.UpdateName()
  815. })
  816. }
  817. // SetRole sets the "role" field.
  818. func (u *AgentUpsertBulk) SetRole(v string) *AgentUpsertBulk {
  819. return u.Update(func(s *AgentUpsert) {
  820. s.SetRole(v)
  821. })
  822. }
  823. // UpdateRole sets the "role" field to the value that was provided on create.
  824. func (u *AgentUpsertBulk) UpdateRole() *AgentUpsertBulk {
  825. return u.Update(func(s *AgentUpsert) {
  826. s.UpdateRole()
  827. })
  828. }
  829. // SetStatus sets the "status" field.
  830. func (u *AgentUpsertBulk) SetStatus(v int) *AgentUpsertBulk {
  831. return u.Update(func(s *AgentUpsert) {
  832. s.SetStatus(v)
  833. })
  834. }
  835. // AddStatus adds v to the "status" field.
  836. func (u *AgentUpsertBulk) AddStatus(v int) *AgentUpsertBulk {
  837. return u.Update(func(s *AgentUpsert) {
  838. s.AddStatus(v)
  839. })
  840. }
  841. // UpdateStatus sets the "status" field to the value that was provided on create.
  842. func (u *AgentUpsertBulk) UpdateStatus() *AgentUpsertBulk {
  843. return u.Update(func(s *AgentUpsert) {
  844. s.UpdateStatus()
  845. })
  846. }
  847. // ClearStatus clears the value of the "status" field.
  848. func (u *AgentUpsertBulk) ClearStatus() *AgentUpsertBulk {
  849. return u.Update(func(s *AgentUpsert) {
  850. s.ClearStatus()
  851. })
  852. }
  853. // SetBackground sets the "background" field.
  854. func (u *AgentUpsertBulk) SetBackground(v string) *AgentUpsertBulk {
  855. return u.Update(func(s *AgentUpsert) {
  856. s.SetBackground(v)
  857. })
  858. }
  859. // UpdateBackground sets the "background" field to the value that was provided on create.
  860. func (u *AgentUpsertBulk) UpdateBackground() *AgentUpsertBulk {
  861. return u.Update(func(s *AgentUpsert) {
  862. s.UpdateBackground()
  863. })
  864. }
  865. // SetExamples sets the "examples" field.
  866. func (u *AgentUpsertBulk) SetExamples(v string) *AgentUpsertBulk {
  867. return u.Update(func(s *AgentUpsert) {
  868. s.SetExamples(v)
  869. })
  870. }
  871. // UpdateExamples sets the "examples" field to the value that was provided on create.
  872. func (u *AgentUpsertBulk) UpdateExamples() *AgentUpsertBulk {
  873. return u.Update(func(s *AgentUpsert) {
  874. s.UpdateExamples()
  875. })
  876. }
  877. // SetOrganizationID sets the "organization_id" field.
  878. func (u *AgentUpsertBulk) SetOrganizationID(v uint64) *AgentUpsertBulk {
  879. return u.Update(func(s *AgentUpsert) {
  880. s.SetOrganizationID(v)
  881. })
  882. }
  883. // AddOrganizationID adds v to the "organization_id" field.
  884. func (u *AgentUpsertBulk) AddOrganizationID(v uint64) *AgentUpsertBulk {
  885. return u.Update(func(s *AgentUpsert) {
  886. s.AddOrganizationID(v)
  887. })
  888. }
  889. // UpdateOrganizationID sets the "organization_id" field to the value that was provided on create.
  890. func (u *AgentUpsertBulk) UpdateOrganizationID() *AgentUpsertBulk {
  891. return u.Update(func(s *AgentUpsert) {
  892. s.UpdateOrganizationID()
  893. })
  894. }
  895. // Exec executes the query.
  896. func (u *AgentUpsertBulk) Exec(ctx context.Context) error {
  897. if u.create.err != nil {
  898. return u.create.err
  899. }
  900. for i, b := range u.create.builders {
  901. if len(b.conflict) != 0 {
  902. return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the AgentCreateBulk instead", i)
  903. }
  904. }
  905. if len(u.create.conflict) == 0 {
  906. return errors.New("ent: missing options for AgentCreateBulk.OnConflict")
  907. }
  908. return u.create.Exec(ctx)
  909. }
  910. // ExecX is like Exec, but panics if an error occurs.
  911. func (u *AgentUpsertBulk) ExecX(ctx context.Context) {
  912. if err := u.create.Exec(ctx); err != nil {
  913. panic(err)
  914. }
  915. }