agent_create.go 31 KB

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