label_create.go 31 KB

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