xunji_create.go 30 KB

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