creditbalance_create.go 29 KB

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