xunji_create.go 28 KB

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