xunji_create.go 30 KB

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