agent_create.go 31 KB

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