setting_create.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "time"
  8. "entgo.io/ent/dialect/sql"
  9. "entgo.io/ent/dialect/sql/sqlgraph"
  10. "entgo.io/ent/schema/field"
  11. "github.com/Wei-Shaw/sub2api/ent/setting"
  12. )
  13. // SettingCreate is the builder for creating a Setting entity.
  14. type SettingCreate struct {
  15. config
  16. mutation *SettingMutation
  17. hooks []Hook
  18. conflict []sql.ConflictOption
  19. }
  20. // SetKey sets the "key" field.
  21. func (_c *SettingCreate) SetKey(v string) *SettingCreate {
  22. _c.mutation.SetKey(v)
  23. return _c
  24. }
  25. // SetValue sets the "value" field.
  26. func (_c *SettingCreate) SetValue(v string) *SettingCreate {
  27. _c.mutation.SetValue(v)
  28. return _c
  29. }
  30. // SetUpdatedAt sets the "updated_at" field.
  31. func (_c *SettingCreate) SetUpdatedAt(v time.Time) *SettingCreate {
  32. _c.mutation.SetUpdatedAt(v)
  33. return _c
  34. }
  35. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  36. func (_c *SettingCreate) SetNillableUpdatedAt(v *time.Time) *SettingCreate {
  37. if v != nil {
  38. _c.SetUpdatedAt(*v)
  39. }
  40. return _c
  41. }
  42. // Mutation returns the SettingMutation object of the builder.
  43. func (_c *SettingCreate) Mutation() *SettingMutation {
  44. return _c.mutation
  45. }
  46. // Save creates the Setting in the database.
  47. func (_c *SettingCreate) Save(ctx context.Context) (*Setting, error) {
  48. _c.defaults()
  49. return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
  50. }
  51. // SaveX calls Save and panics if Save returns an error.
  52. func (_c *SettingCreate) SaveX(ctx context.Context) *Setting {
  53. v, err := _c.Save(ctx)
  54. if err != nil {
  55. panic(err)
  56. }
  57. return v
  58. }
  59. // Exec executes the query.
  60. func (_c *SettingCreate) Exec(ctx context.Context) error {
  61. _, err := _c.Save(ctx)
  62. return err
  63. }
  64. // ExecX is like Exec, but panics if an error occurs.
  65. func (_c *SettingCreate) ExecX(ctx context.Context) {
  66. if err := _c.Exec(ctx); err != nil {
  67. panic(err)
  68. }
  69. }
  70. // defaults sets the default values of the builder before save.
  71. func (_c *SettingCreate) defaults() {
  72. if _, ok := _c.mutation.UpdatedAt(); !ok {
  73. v := setting.DefaultUpdatedAt()
  74. _c.mutation.SetUpdatedAt(v)
  75. }
  76. }
  77. // check runs all checks and user-defined validators on the builder.
  78. func (_c *SettingCreate) check() error {
  79. if _, ok := _c.mutation.Key(); !ok {
  80. return &ValidationError{Name: "key", err: errors.New(`ent: missing required field "Setting.key"`)}
  81. }
  82. if v, ok := _c.mutation.Key(); ok {
  83. if err := setting.KeyValidator(v); err != nil {
  84. return &ValidationError{Name: "key", err: fmt.Errorf(`ent: validator failed for field "Setting.key": %w`, err)}
  85. }
  86. }
  87. if _, ok := _c.mutation.Value(); !ok {
  88. return &ValidationError{Name: "value", err: errors.New(`ent: missing required field "Setting.value"`)}
  89. }
  90. if _, ok := _c.mutation.UpdatedAt(); !ok {
  91. return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Setting.updated_at"`)}
  92. }
  93. return nil
  94. }
  95. func (_c *SettingCreate) sqlSave(ctx context.Context) (*Setting, error) {
  96. if err := _c.check(); err != nil {
  97. return nil, err
  98. }
  99. _node, _spec := _c.createSpec()
  100. if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
  101. if sqlgraph.IsConstraintError(err) {
  102. err = &ConstraintError{msg: err.Error(), wrap: err}
  103. }
  104. return nil, err
  105. }
  106. id := _spec.ID.Value.(int64)
  107. _node.ID = int64(id)
  108. _c.mutation.id = &_node.ID
  109. _c.mutation.done = true
  110. return _node, nil
  111. }
  112. func (_c *SettingCreate) createSpec() (*Setting, *sqlgraph.CreateSpec) {
  113. var (
  114. _node = &Setting{config: _c.config}
  115. _spec = sqlgraph.NewCreateSpec(setting.Table, sqlgraph.NewFieldSpec(setting.FieldID, field.TypeInt64))
  116. )
  117. _spec.OnConflict = _c.conflict
  118. if value, ok := _c.mutation.Key(); ok {
  119. _spec.SetField(setting.FieldKey, field.TypeString, value)
  120. _node.Key = value
  121. }
  122. if value, ok := _c.mutation.Value(); ok {
  123. _spec.SetField(setting.FieldValue, field.TypeString, value)
  124. _node.Value = value
  125. }
  126. if value, ok := _c.mutation.UpdatedAt(); ok {
  127. _spec.SetField(setting.FieldUpdatedAt, field.TypeTime, value)
  128. _node.UpdatedAt = value
  129. }
  130. return _node, _spec
  131. }
  132. // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
  133. // of the `INSERT` statement. For example:
  134. //
  135. // client.Setting.Create().
  136. // SetKey(v).
  137. // OnConflict(
  138. // // Update the row with the new values
  139. // // the was proposed for insertion.
  140. // sql.ResolveWithNewValues(),
  141. // ).
  142. // // Override some of the fields with custom
  143. // // update values.
  144. // Update(func(u *ent.SettingUpsert) {
  145. // SetKey(v+v).
  146. // }).
  147. // Exec(ctx)
  148. func (_c *SettingCreate) OnConflict(opts ...sql.ConflictOption) *SettingUpsertOne {
  149. _c.conflict = opts
  150. return &SettingUpsertOne{
  151. create: _c,
  152. }
  153. }
  154. // OnConflictColumns calls `OnConflict` and configures the columns
  155. // as conflict target. Using this option is equivalent to using:
  156. //
  157. // client.Setting.Create().
  158. // OnConflict(sql.ConflictColumns(columns...)).
  159. // Exec(ctx)
  160. func (_c *SettingCreate) OnConflictColumns(columns ...string) *SettingUpsertOne {
  161. _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
  162. return &SettingUpsertOne{
  163. create: _c,
  164. }
  165. }
  166. type (
  167. // SettingUpsertOne is the builder for "upsert"-ing
  168. // one Setting node.
  169. SettingUpsertOne struct {
  170. create *SettingCreate
  171. }
  172. // SettingUpsert is the "OnConflict" setter.
  173. SettingUpsert struct {
  174. *sql.UpdateSet
  175. }
  176. )
  177. // SetKey sets the "key" field.
  178. func (u *SettingUpsert) SetKey(v string) *SettingUpsert {
  179. u.Set(setting.FieldKey, v)
  180. return u
  181. }
  182. // UpdateKey sets the "key" field to the value that was provided on create.
  183. func (u *SettingUpsert) UpdateKey() *SettingUpsert {
  184. u.SetExcluded(setting.FieldKey)
  185. return u
  186. }
  187. // SetValue sets the "value" field.
  188. func (u *SettingUpsert) SetValue(v string) *SettingUpsert {
  189. u.Set(setting.FieldValue, v)
  190. return u
  191. }
  192. // UpdateValue sets the "value" field to the value that was provided on create.
  193. func (u *SettingUpsert) UpdateValue() *SettingUpsert {
  194. u.SetExcluded(setting.FieldValue)
  195. return u
  196. }
  197. // SetUpdatedAt sets the "updated_at" field.
  198. func (u *SettingUpsert) SetUpdatedAt(v time.Time) *SettingUpsert {
  199. u.Set(setting.FieldUpdatedAt, v)
  200. return u
  201. }
  202. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  203. func (u *SettingUpsert) UpdateUpdatedAt() *SettingUpsert {
  204. u.SetExcluded(setting.FieldUpdatedAt)
  205. return u
  206. }
  207. // UpdateNewValues updates the mutable fields using the new values that were set on create.
  208. // Using this option is equivalent to using:
  209. //
  210. // client.Setting.Create().
  211. // OnConflict(
  212. // sql.ResolveWithNewValues(),
  213. // ).
  214. // Exec(ctx)
  215. func (u *SettingUpsertOne) UpdateNewValues() *SettingUpsertOne {
  216. u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
  217. return u
  218. }
  219. // Ignore sets each column to itself in case of conflict.
  220. // Using this option is equivalent to using:
  221. //
  222. // client.Setting.Create().
  223. // OnConflict(sql.ResolveWithIgnore()).
  224. // Exec(ctx)
  225. func (u *SettingUpsertOne) Ignore() *SettingUpsertOne {
  226. u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
  227. return u
  228. }
  229. // DoNothing configures the conflict_action to `DO NOTHING`.
  230. // Supported only by SQLite and PostgreSQL.
  231. func (u *SettingUpsertOne) DoNothing() *SettingUpsertOne {
  232. u.create.conflict = append(u.create.conflict, sql.DoNothing())
  233. return u
  234. }
  235. // Update allows overriding fields `UPDATE` values. See the SettingCreate.OnConflict
  236. // documentation for more info.
  237. func (u *SettingUpsertOne) Update(set func(*SettingUpsert)) *SettingUpsertOne {
  238. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
  239. set(&SettingUpsert{UpdateSet: update})
  240. }))
  241. return u
  242. }
  243. // SetKey sets the "key" field.
  244. func (u *SettingUpsertOne) SetKey(v string) *SettingUpsertOne {
  245. return u.Update(func(s *SettingUpsert) {
  246. s.SetKey(v)
  247. })
  248. }
  249. // UpdateKey sets the "key" field to the value that was provided on create.
  250. func (u *SettingUpsertOne) UpdateKey() *SettingUpsertOne {
  251. return u.Update(func(s *SettingUpsert) {
  252. s.UpdateKey()
  253. })
  254. }
  255. // SetValue sets the "value" field.
  256. func (u *SettingUpsertOne) SetValue(v string) *SettingUpsertOne {
  257. return u.Update(func(s *SettingUpsert) {
  258. s.SetValue(v)
  259. })
  260. }
  261. // UpdateValue sets the "value" field to the value that was provided on create.
  262. func (u *SettingUpsertOne) UpdateValue() *SettingUpsertOne {
  263. return u.Update(func(s *SettingUpsert) {
  264. s.UpdateValue()
  265. })
  266. }
  267. // SetUpdatedAt sets the "updated_at" field.
  268. func (u *SettingUpsertOne) SetUpdatedAt(v time.Time) *SettingUpsertOne {
  269. return u.Update(func(s *SettingUpsert) {
  270. s.SetUpdatedAt(v)
  271. })
  272. }
  273. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  274. func (u *SettingUpsertOne) UpdateUpdatedAt() *SettingUpsertOne {
  275. return u.Update(func(s *SettingUpsert) {
  276. s.UpdateUpdatedAt()
  277. })
  278. }
  279. // Exec executes the query.
  280. func (u *SettingUpsertOne) Exec(ctx context.Context) error {
  281. if len(u.create.conflict) == 0 {
  282. return errors.New("ent: missing options for SettingCreate.OnConflict")
  283. }
  284. return u.create.Exec(ctx)
  285. }
  286. // ExecX is like Exec, but panics if an error occurs.
  287. func (u *SettingUpsertOne) ExecX(ctx context.Context) {
  288. if err := u.create.Exec(ctx); err != nil {
  289. panic(err)
  290. }
  291. }
  292. // Exec executes the UPSERT query and returns the inserted/updated ID.
  293. func (u *SettingUpsertOne) ID(ctx context.Context) (id int64, err error) {
  294. node, err := u.create.Save(ctx)
  295. if err != nil {
  296. return id, err
  297. }
  298. return node.ID, nil
  299. }
  300. // IDX is like ID, but panics if an error occurs.
  301. func (u *SettingUpsertOne) IDX(ctx context.Context) int64 {
  302. id, err := u.ID(ctx)
  303. if err != nil {
  304. panic(err)
  305. }
  306. return id
  307. }
  308. // SettingCreateBulk is the builder for creating many Setting entities in bulk.
  309. type SettingCreateBulk struct {
  310. config
  311. err error
  312. builders []*SettingCreate
  313. conflict []sql.ConflictOption
  314. }
  315. // Save creates the Setting entities in the database.
  316. func (_c *SettingCreateBulk) Save(ctx context.Context) ([]*Setting, error) {
  317. if _c.err != nil {
  318. return nil, _c.err
  319. }
  320. specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
  321. nodes := make([]*Setting, len(_c.builders))
  322. mutators := make([]Mutator, len(_c.builders))
  323. for i := range _c.builders {
  324. func(i int, root context.Context) {
  325. builder := _c.builders[i]
  326. builder.defaults()
  327. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  328. mutation, ok := m.(*SettingMutation)
  329. if !ok {
  330. return nil, fmt.Errorf("unexpected mutation type %T", m)
  331. }
  332. if err := builder.check(); err != nil {
  333. return nil, err
  334. }
  335. builder.mutation = mutation
  336. var err error
  337. nodes[i], specs[i] = builder.createSpec()
  338. if i < len(mutators)-1 {
  339. _, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
  340. } else {
  341. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  342. spec.OnConflict = _c.conflict
  343. // Invoke the actual operation on the latest mutation in the chain.
  344. if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
  345. if sqlgraph.IsConstraintError(err) {
  346. err = &ConstraintError{msg: err.Error(), wrap: err}
  347. }
  348. }
  349. }
  350. if err != nil {
  351. return nil, err
  352. }
  353. mutation.id = &nodes[i].ID
  354. if specs[i].ID.Value != nil {
  355. id := specs[i].ID.Value.(int64)
  356. nodes[i].ID = int64(id)
  357. }
  358. mutation.done = true
  359. return nodes[i], nil
  360. })
  361. for i := len(builder.hooks) - 1; i >= 0; i-- {
  362. mut = builder.hooks[i](mut)
  363. }
  364. mutators[i] = mut
  365. }(i, ctx)
  366. }
  367. if len(mutators) > 0 {
  368. if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
  369. return nil, err
  370. }
  371. }
  372. return nodes, nil
  373. }
  374. // SaveX is like Save, but panics if an error occurs.
  375. func (_c *SettingCreateBulk) SaveX(ctx context.Context) []*Setting {
  376. v, err := _c.Save(ctx)
  377. if err != nil {
  378. panic(err)
  379. }
  380. return v
  381. }
  382. // Exec executes the query.
  383. func (_c *SettingCreateBulk) Exec(ctx context.Context) error {
  384. _, err := _c.Save(ctx)
  385. return err
  386. }
  387. // ExecX is like Exec, but panics if an error occurs.
  388. func (_c *SettingCreateBulk) ExecX(ctx context.Context) {
  389. if err := _c.Exec(ctx); err != nil {
  390. panic(err)
  391. }
  392. }
  393. // OnConflict allows configuring the `ON CONFLICT` / `ON DUPLICATE KEY` clause
  394. // of the `INSERT` statement. For example:
  395. //
  396. // client.Setting.CreateBulk(builders...).
  397. // OnConflict(
  398. // // Update the row with the new values
  399. // // the was proposed for insertion.
  400. // sql.ResolveWithNewValues(),
  401. // ).
  402. // // Override some of the fields with custom
  403. // // update values.
  404. // Update(func(u *ent.SettingUpsert) {
  405. // SetKey(v+v).
  406. // }).
  407. // Exec(ctx)
  408. func (_c *SettingCreateBulk) OnConflict(opts ...sql.ConflictOption) *SettingUpsertBulk {
  409. _c.conflict = opts
  410. return &SettingUpsertBulk{
  411. create: _c,
  412. }
  413. }
  414. // OnConflictColumns calls `OnConflict` and configures the columns
  415. // as conflict target. Using this option is equivalent to using:
  416. //
  417. // client.Setting.Create().
  418. // OnConflict(sql.ConflictColumns(columns...)).
  419. // Exec(ctx)
  420. func (_c *SettingCreateBulk) OnConflictColumns(columns ...string) *SettingUpsertBulk {
  421. _c.conflict = append(_c.conflict, sql.ConflictColumns(columns...))
  422. return &SettingUpsertBulk{
  423. create: _c,
  424. }
  425. }
  426. // SettingUpsertBulk is the builder for "upsert"-ing
  427. // a bulk of Setting nodes.
  428. type SettingUpsertBulk struct {
  429. create *SettingCreateBulk
  430. }
  431. // UpdateNewValues updates the mutable fields using the new values that
  432. // were set on create. Using this option is equivalent to using:
  433. //
  434. // client.Setting.Create().
  435. // OnConflict(
  436. // sql.ResolveWithNewValues(),
  437. // ).
  438. // Exec(ctx)
  439. func (u *SettingUpsertBulk) UpdateNewValues() *SettingUpsertBulk {
  440. u.create.conflict = append(u.create.conflict, sql.ResolveWithNewValues())
  441. return u
  442. }
  443. // Ignore sets each column to itself in case of conflict.
  444. // Using this option is equivalent to using:
  445. //
  446. // client.Setting.Create().
  447. // OnConflict(sql.ResolveWithIgnore()).
  448. // Exec(ctx)
  449. func (u *SettingUpsertBulk) Ignore() *SettingUpsertBulk {
  450. u.create.conflict = append(u.create.conflict, sql.ResolveWithIgnore())
  451. return u
  452. }
  453. // DoNothing configures the conflict_action to `DO NOTHING`.
  454. // Supported only by SQLite and PostgreSQL.
  455. func (u *SettingUpsertBulk) DoNothing() *SettingUpsertBulk {
  456. u.create.conflict = append(u.create.conflict, sql.DoNothing())
  457. return u
  458. }
  459. // Update allows overriding fields `UPDATE` values. See the SettingCreateBulk.OnConflict
  460. // documentation for more info.
  461. func (u *SettingUpsertBulk) Update(set func(*SettingUpsert)) *SettingUpsertBulk {
  462. u.create.conflict = append(u.create.conflict, sql.ResolveWith(func(update *sql.UpdateSet) {
  463. set(&SettingUpsert{UpdateSet: update})
  464. }))
  465. return u
  466. }
  467. // SetKey sets the "key" field.
  468. func (u *SettingUpsertBulk) SetKey(v string) *SettingUpsertBulk {
  469. return u.Update(func(s *SettingUpsert) {
  470. s.SetKey(v)
  471. })
  472. }
  473. // UpdateKey sets the "key" field to the value that was provided on create.
  474. func (u *SettingUpsertBulk) UpdateKey() *SettingUpsertBulk {
  475. return u.Update(func(s *SettingUpsert) {
  476. s.UpdateKey()
  477. })
  478. }
  479. // SetValue sets the "value" field.
  480. func (u *SettingUpsertBulk) SetValue(v string) *SettingUpsertBulk {
  481. return u.Update(func(s *SettingUpsert) {
  482. s.SetValue(v)
  483. })
  484. }
  485. // UpdateValue sets the "value" field to the value that was provided on create.
  486. func (u *SettingUpsertBulk) UpdateValue() *SettingUpsertBulk {
  487. return u.Update(func(s *SettingUpsert) {
  488. s.UpdateValue()
  489. })
  490. }
  491. // SetUpdatedAt sets the "updated_at" field.
  492. func (u *SettingUpsertBulk) SetUpdatedAt(v time.Time) *SettingUpsertBulk {
  493. return u.Update(func(s *SettingUpsert) {
  494. s.SetUpdatedAt(v)
  495. })
  496. }
  497. // UpdateUpdatedAt sets the "updated_at" field to the value that was provided on create.
  498. func (u *SettingUpsertBulk) UpdateUpdatedAt() *SettingUpsertBulk {
  499. return u.Update(func(s *SettingUpsert) {
  500. s.UpdateUpdatedAt()
  501. })
  502. }
  503. // Exec executes the query.
  504. func (u *SettingUpsertBulk) Exec(ctx context.Context) error {
  505. if u.create.err != nil {
  506. return u.create.err
  507. }
  508. for i, b := range u.create.builders {
  509. if len(b.conflict) != 0 {
  510. return fmt.Errorf("ent: OnConflict was set for builder %d. Set it on the SettingCreateBulk instead", i)
  511. }
  512. }
  513. if len(u.create.conflict) == 0 {
  514. return errors.New("ent: missing options for SettingCreateBulk.OnConflict")
  515. }
  516. return u.create.Exec(ctx)
  517. }
  518. // ExecX is like Exec, but panics if an error occurs.
  519. func (u *SettingUpsertBulk) ExecX(ctx context.Context) {
  520. if err := u.create.Exec(ctx); err != nil {
  521. panic(err)
  522. }
  523. }