agent_create.go 30 KB

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