server_create.go 27 KB

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