child_create.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. // Code generated by ent, DO NOT EDIT.
  2. package ent
  3. import (
  4. "context"
  5. "errors"
  6. "fmt"
  7. "sikey/w303a/http/internal/data/ent/child"
  8. "sikey/w303a/http/internal/data/ent/childref"
  9. "sikey/w303a/http/internal/data/ent/contacts"
  10. "sikey/w303a/http/internal/data/ent/devicebind"
  11. "time"
  12. "entgo.io/ent/dialect/sql/sqlgraph"
  13. "entgo.io/ent/schema/field"
  14. "github.com/google/uuid"
  15. )
  16. // ChildCreate is the builder for creating a Child entity.
  17. type ChildCreate struct {
  18. config
  19. mutation *ChildMutation
  20. hooks []Hook
  21. }
  22. // SetCreatedAt sets the "created_at" field.
  23. func (cc *ChildCreate) SetCreatedAt(t time.Time) *ChildCreate {
  24. cc.mutation.SetCreatedAt(t)
  25. return cc
  26. }
  27. // SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
  28. func (cc *ChildCreate) SetNillableCreatedAt(t *time.Time) *ChildCreate {
  29. if t != nil {
  30. cc.SetCreatedAt(*t)
  31. }
  32. return cc
  33. }
  34. // SetUpdatedAt sets the "updated_at" field.
  35. func (cc *ChildCreate) SetUpdatedAt(t time.Time) *ChildCreate {
  36. cc.mutation.SetUpdatedAt(t)
  37. return cc
  38. }
  39. // SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
  40. func (cc *ChildCreate) SetNillableUpdatedAt(t *time.Time) *ChildCreate {
  41. if t != nil {
  42. cc.SetUpdatedAt(*t)
  43. }
  44. return cc
  45. }
  46. // SetDeletedAt sets the "deleted_at" field.
  47. func (cc *ChildCreate) SetDeletedAt(t time.Time) *ChildCreate {
  48. cc.mutation.SetDeletedAt(t)
  49. return cc
  50. }
  51. // SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
  52. func (cc *ChildCreate) SetNillableDeletedAt(t *time.Time) *ChildCreate {
  53. if t != nil {
  54. cc.SetDeletedAt(*t)
  55. }
  56. return cc
  57. }
  58. // SetChildName sets the "child_name" field.
  59. func (cc *ChildCreate) SetChildName(s string) *ChildCreate {
  60. cc.mutation.SetChildName(s)
  61. return cc
  62. }
  63. // SetPhoneNumber sets the "phone_number" field.
  64. func (cc *ChildCreate) SetPhoneNumber(s string) *ChildCreate {
  65. cc.mutation.SetPhoneNumber(s)
  66. return cc
  67. }
  68. // SetAreaCode sets the "area_code" field.
  69. func (cc *ChildCreate) SetAreaCode(s string) *ChildCreate {
  70. cc.mutation.SetAreaCode(s)
  71. return cc
  72. }
  73. // SetNillableAreaCode sets the "area_code" field if the given value is not nil.
  74. func (cc *ChildCreate) SetNillableAreaCode(s *string) *ChildCreate {
  75. if s != nil {
  76. cc.SetAreaCode(*s)
  77. }
  78. return cc
  79. }
  80. // SetAvatar sets the "avatar" field.
  81. func (cc *ChildCreate) SetAvatar(s string) *ChildCreate {
  82. cc.mutation.SetAvatar(s)
  83. return cc
  84. }
  85. // SetNillableAvatar sets the "avatar" field if the given value is not nil.
  86. func (cc *ChildCreate) SetNillableAvatar(s *string) *ChildCreate {
  87. if s != nil {
  88. cc.SetAvatar(*s)
  89. }
  90. return cc
  91. }
  92. // SetGender sets the "gender" field.
  93. func (cc *ChildCreate) SetGender(i int8) *ChildCreate {
  94. cc.mutation.SetGender(i)
  95. return cc
  96. }
  97. // SetNillableGender sets the "gender" field if the given value is not nil.
  98. func (cc *ChildCreate) SetNillableGender(i *int8) *ChildCreate {
  99. if i != nil {
  100. cc.SetGender(*i)
  101. }
  102. return cc
  103. }
  104. // SetHeight sets the "height" field.
  105. func (cc *ChildCreate) SetHeight(i int32) *ChildCreate {
  106. cc.mutation.SetHeight(i)
  107. return cc
  108. }
  109. // SetNillableHeight sets the "height" field if the given value is not nil.
  110. func (cc *ChildCreate) SetNillableHeight(i *int32) *ChildCreate {
  111. if i != nil {
  112. cc.SetHeight(*i)
  113. }
  114. return cc
  115. }
  116. // SetWeight sets the "weight" field.
  117. func (cc *ChildCreate) SetWeight(i int32) *ChildCreate {
  118. cc.mutation.SetWeight(i)
  119. return cc
  120. }
  121. // SetNillableWeight sets the "weight" field if the given value is not nil.
  122. func (cc *ChildCreate) SetNillableWeight(i *int32) *ChildCreate {
  123. if i != nil {
  124. cc.SetWeight(*i)
  125. }
  126. return cc
  127. }
  128. // SetBirthday sets the "birthday" field.
  129. func (cc *ChildCreate) SetBirthday(s string) *ChildCreate {
  130. cc.mutation.SetBirthday(s)
  131. return cc
  132. }
  133. // SetNillableBirthday sets the "birthday" field if the given value is not nil.
  134. func (cc *ChildCreate) SetNillableBirthday(s *string) *ChildCreate {
  135. if s != nil {
  136. cc.SetBirthday(*s)
  137. }
  138. return cc
  139. }
  140. // SetIsEnableBlockUnknownCall sets the "is_enable_block_unknown_call" field.
  141. func (cc *ChildCreate) SetIsEnableBlockUnknownCall(b bool) *ChildCreate {
  142. cc.mutation.SetIsEnableBlockUnknownCall(b)
  143. return cc
  144. }
  145. // SetNillableIsEnableBlockUnknownCall sets the "is_enable_block_unknown_call" field if the given value is not nil.
  146. func (cc *ChildCreate) SetNillableIsEnableBlockUnknownCall(b *bool) *ChildCreate {
  147. if b != nil {
  148. cc.SetIsEnableBlockUnknownCall(*b)
  149. }
  150. return cc
  151. }
  152. // SetIsEnableFence sets the "is_enable_fence" field.
  153. func (cc *ChildCreate) SetIsEnableFence(b bool) *ChildCreate {
  154. cc.mutation.SetIsEnableFence(b)
  155. return cc
  156. }
  157. // SetNillableIsEnableFence sets the "is_enable_fence" field if the given value is not nil.
  158. func (cc *ChildCreate) SetNillableIsEnableFence(b *bool) *ChildCreate {
  159. if b != nil {
  160. cc.SetIsEnableFence(*b)
  161. }
  162. return cc
  163. }
  164. // SetID sets the "id" field.
  165. func (cc *ChildCreate) SetID(u uuid.UUID) *ChildCreate {
  166. cc.mutation.SetID(u)
  167. return cc
  168. }
  169. // SetNillableID sets the "id" field if the given value is not nil.
  170. func (cc *ChildCreate) SetNillableID(u *uuid.UUID) *ChildCreate {
  171. if u != nil {
  172. cc.SetID(*u)
  173. }
  174. return cc
  175. }
  176. // AddChildRefIDs adds the "childRefs" edge to the ChildRef entity by IDs.
  177. func (cc *ChildCreate) AddChildRefIDs(ids ...int64) *ChildCreate {
  178. cc.mutation.AddChildRefIDs(ids...)
  179. return cc
  180. }
  181. // AddChildRefs adds the "childRefs" edges to the ChildRef entity.
  182. func (cc *ChildCreate) AddChildRefs(c ...*ChildRef) *ChildCreate {
  183. ids := make([]int64, len(c))
  184. for i := range c {
  185. ids[i] = c[i].ID
  186. }
  187. return cc.AddChildRefIDs(ids...)
  188. }
  189. // AddChildDeviceIDs adds the "childDevice" edge to the DeviceBind entity by IDs.
  190. func (cc *ChildCreate) AddChildDeviceIDs(ids ...int64) *ChildCreate {
  191. cc.mutation.AddChildDeviceIDs(ids...)
  192. return cc
  193. }
  194. // AddChildDevice adds the "childDevice" edges to the DeviceBind entity.
  195. func (cc *ChildCreate) AddChildDevice(d ...*DeviceBind) *ChildCreate {
  196. ids := make([]int64, len(d))
  197. for i := range d {
  198. ids[i] = d[i].ID
  199. }
  200. return cc.AddChildDeviceIDs(ids...)
  201. }
  202. // AddChildContactIDs adds the "childContacts" edge to the Contacts entity by IDs.
  203. func (cc *ChildCreate) AddChildContactIDs(ids ...int) *ChildCreate {
  204. cc.mutation.AddChildContactIDs(ids...)
  205. return cc
  206. }
  207. // AddChildContacts adds the "childContacts" edges to the Contacts entity.
  208. func (cc *ChildCreate) AddChildContacts(c ...*Contacts) *ChildCreate {
  209. ids := make([]int, len(c))
  210. for i := range c {
  211. ids[i] = c[i].ID
  212. }
  213. return cc.AddChildContactIDs(ids...)
  214. }
  215. // Mutation returns the ChildMutation object of the builder.
  216. func (cc *ChildCreate) Mutation() *ChildMutation {
  217. return cc.mutation
  218. }
  219. // Save creates the Child in the database.
  220. func (cc *ChildCreate) Save(ctx context.Context) (*Child, error) {
  221. cc.defaults()
  222. return withHooks(ctx, cc.sqlSave, cc.mutation, cc.hooks)
  223. }
  224. // SaveX calls Save and panics if Save returns an error.
  225. func (cc *ChildCreate) SaveX(ctx context.Context) *Child {
  226. v, err := cc.Save(ctx)
  227. if err != nil {
  228. panic(err)
  229. }
  230. return v
  231. }
  232. // Exec executes the query.
  233. func (cc *ChildCreate) Exec(ctx context.Context) error {
  234. _, err := cc.Save(ctx)
  235. return err
  236. }
  237. // ExecX is like Exec, but panics if an error occurs.
  238. func (cc *ChildCreate) ExecX(ctx context.Context) {
  239. if err := cc.Exec(ctx); err != nil {
  240. panic(err)
  241. }
  242. }
  243. // defaults sets the default values of the builder before save.
  244. func (cc *ChildCreate) defaults() {
  245. if _, ok := cc.mutation.CreatedAt(); !ok {
  246. v := child.DefaultCreatedAt()
  247. cc.mutation.SetCreatedAt(v)
  248. }
  249. if _, ok := cc.mutation.UpdatedAt(); !ok {
  250. v := child.DefaultUpdatedAt()
  251. cc.mutation.SetUpdatedAt(v)
  252. }
  253. if _, ok := cc.mutation.AreaCode(); !ok {
  254. v := child.DefaultAreaCode
  255. cc.mutation.SetAreaCode(v)
  256. }
  257. if _, ok := cc.mutation.Gender(); !ok {
  258. v := child.DefaultGender
  259. cc.mutation.SetGender(v)
  260. }
  261. if _, ok := cc.mutation.Height(); !ok {
  262. v := child.DefaultHeight
  263. cc.mutation.SetHeight(v)
  264. }
  265. if _, ok := cc.mutation.Weight(); !ok {
  266. v := child.DefaultWeight
  267. cc.mutation.SetWeight(v)
  268. }
  269. if _, ok := cc.mutation.IsEnableBlockUnknownCall(); !ok {
  270. v := child.DefaultIsEnableBlockUnknownCall
  271. cc.mutation.SetIsEnableBlockUnknownCall(v)
  272. }
  273. if _, ok := cc.mutation.IsEnableFence(); !ok {
  274. v := child.DefaultIsEnableFence
  275. cc.mutation.SetIsEnableFence(v)
  276. }
  277. if _, ok := cc.mutation.ID(); !ok {
  278. v := child.DefaultID()
  279. cc.mutation.SetID(v)
  280. }
  281. }
  282. // check runs all checks and user-defined validators on the builder.
  283. func (cc *ChildCreate) check() error {
  284. if _, ok := cc.mutation.CreatedAt(); !ok {
  285. return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "Child.created_at"`)}
  286. }
  287. if _, ok := cc.mutation.UpdatedAt(); !ok {
  288. return &ValidationError{Name: "updated_at", err: errors.New(`ent: missing required field "Child.updated_at"`)}
  289. }
  290. if _, ok := cc.mutation.ChildName(); !ok {
  291. return &ValidationError{Name: "child_name", err: errors.New(`ent: missing required field "Child.child_name"`)}
  292. }
  293. if v, ok := cc.mutation.ChildName(); ok {
  294. if err := child.ChildNameValidator(v); err != nil {
  295. return &ValidationError{Name: "child_name", err: fmt.Errorf(`ent: validator failed for field "Child.child_name": %w`, err)}
  296. }
  297. }
  298. if _, ok := cc.mutation.PhoneNumber(); !ok {
  299. return &ValidationError{Name: "phone_number", err: errors.New(`ent: missing required field "Child.phone_number"`)}
  300. }
  301. if v, ok := cc.mutation.PhoneNumber(); ok {
  302. if err := child.PhoneNumberValidator(v); err != nil {
  303. return &ValidationError{Name: "phone_number", err: fmt.Errorf(`ent: validator failed for field "Child.phone_number": %w`, err)}
  304. }
  305. }
  306. if _, ok := cc.mutation.AreaCode(); !ok {
  307. return &ValidationError{Name: "area_code", err: errors.New(`ent: missing required field "Child.area_code"`)}
  308. }
  309. if v, ok := cc.mutation.AreaCode(); ok {
  310. if err := child.AreaCodeValidator(v); err != nil {
  311. return &ValidationError{Name: "area_code", err: fmt.Errorf(`ent: validator failed for field "Child.area_code": %w`, err)}
  312. }
  313. }
  314. if v, ok := cc.mutation.Avatar(); ok {
  315. if err := child.AvatarValidator(v); err != nil {
  316. return &ValidationError{Name: "avatar", err: fmt.Errorf(`ent: validator failed for field "Child.avatar": %w`, err)}
  317. }
  318. }
  319. if _, ok := cc.mutation.Gender(); !ok {
  320. return &ValidationError{Name: "gender", err: errors.New(`ent: missing required field "Child.gender"`)}
  321. }
  322. if v, ok := cc.mutation.Gender(); ok {
  323. if err := child.GenderValidator(v); err != nil {
  324. return &ValidationError{Name: "gender", err: fmt.Errorf(`ent: validator failed for field "Child.gender": %w`, err)}
  325. }
  326. }
  327. if _, ok := cc.mutation.Height(); !ok {
  328. return &ValidationError{Name: "height", err: errors.New(`ent: missing required field "Child.height"`)}
  329. }
  330. if v, ok := cc.mutation.Height(); ok {
  331. if err := child.HeightValidator(v); err != nil {
  332. return &ValidationError{Name: "height", err: fmt.Errorf(`ent: validator failed for field "Child.height": %w`, err)}
  333. }
  334. }
  335. if _, ok := cc.mutation.Weight(); !ok {
  336. return &ValidationError{Name: "weight", err: errors.New(`ent: missing required field "Child.weight"`)}
  337. }
  338. if v, ok := cc.mutation.Weight(); ok {
  339. if err := child.WeightValidator(v); err != nil {
  340. return &ValidationError{Name: "weight", err: fmt.Errorf(`ent: validator failed for field "Child.weight": %w`, err)}
  341. }
  342. }
  343. if v, ok := cc.mutation.Birthday(); ok {
  344. if err := child.BirthdayValidator(v); err != nil {
  345. return &ValidationError{Name: "birthday", err: fmt.Errorf(`ent: validator failed for field "Child.birthday": %w`, err)}
  346. }
  347. }
  348. if _, ok := cc.mutation.IsEnableBlockUnknownCall(); !ok {
  349. return &ValidationError{Name: "is_enable_block_unknown_call", err: errors.New(`ent: missing required field "Child.is_enable_block_unknown_call"`)}
  350. }
  351. if _, ok := cc.mutation.IsEnableFence(); !ok {
  352. return &ValidationError{Name: "is_enable_fence", err: errors.New(`ent: missing required field "Child.is_enable_fence"`)}
  353. }
  354. return nil
  355. }
  356. func (cc *ChildCreate) sqlSave(ctx context.Context) (*Child, error) {
  357. if err := cc.check(); err != nil {
  358. return nil, err
  359. }
  360. _node, _spec := cc.createSpec()
  361. if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
  362. if sqlgraph.IsConstraintError(err) {
  363. err = &ConstraintError{msg: err.Error(), wrap: err}
  364. }
  365. return nil, err
  366. }
  367. if _spec.ID.Value != nil {
  368. if id, ok := _spec.ID.Value.(*uuid.UUID); ok {
  369. _node.ID = *id
  370. } else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
  371. return nil, err
  372. }
  373. }
  374. cc.mutation.id = &_node.ID
  375. cc.mutation.done = true
  376. return _node, nil
  377. }
  378. func (cc *ChildCreate) createSpec() (*Child, *sqlgraph.CreateSpec) {
  379. var (
  380. _node = &Child{config: cc.config}
  381. _spec = sqlgraph.NewCreateSpec(child.Table, sqlgraph.NewFieldSpec(child.FieldID, field.TypeUUID))
  382. )
  383. if id, ok := cc.mutation.ID(); ok {
  384. _node.ID = id
  385. _spec.ID.Value = &id
  386. }
  387. if value, ok := cc.mutation.CreatedAt(); ok {
  388. _spec.SetField(child.FieldCreatedAt, field.TypeTime, value)
  389. _node.CreatedAt = value
  390. }
  391. if value, ok := cc.mutation.UpdatedAt(); ok {
  392. _spec.SetField(child.FieldUpdatedAt, field.TypeTime, value)
  393. _node.UpdatedAt = value
  394. }
  395. if value, ok := cc.mutation.DeletedAt(); ok {
  396. _spec.SetField(child.FieldDeletedAt, field.TypeTime, value)
  397. _node.DeletedAt = &value
  398. }
  399. if value, ok := cc.mutation.ChildName(); ok {
  400. _spec.SetField(child.FieldChildName, field.TypeString, value)
  401. _node.ChildName = value
  402. }
  403. if value, ok := cc.mutation.PhoneNumber(); ok {
  404. _spec.SetField(child.FieldPhoneNumber, field.TypeString, value)
  405. _node.PhoneNumber = value
  406. }
  407. if value, ok := cc.mutation.AreaCode(); ok {
  408. _spec.SetField(child.FieldAreaCode, field.TypeString, value)
  409. _node.AreaCode = value
  410. }
  411. if value, ok := cc.mutation.Avatar(); ok {
  412. _spec.SetField(child.FieldAvatar, field.TypeString, value)
  413. _node.Avatar = &value
  414. }
  415. if value, ok := cc.mutation.Gender(); ok {
  416. _spec.SetField(child.FieldGender, field.TypeInt8, value)
  417. _node.Gender = value
  418. }
  419. if value, ok := cc.mutation.Height(); ok {
  420. _spec.SetField(child.FieldHeight, field.TypeInt32, value)
  421. _node.Height = value
  422. }
  423. if value, ok := cc.mutation.Weight(); ok {
  424. _spec.SetField(child.FieldWeight, field.TypeInt32, value)
  425. _node.Weight = value
  426. }
  427. if value, ok := cc.mutation.Birthday(); ok {
  428. _spec.SetField(child.FieldBirthday, field.TypeString, value)
  429. _node.Birthday = &value
  430. }
  431. if value, ok := cc.mutation.IsEnableBlockUnknownCall(); ok {
  432. _spec.SetField(child.FieldIsEnableBlockUnknownCall, field.TypeBool, value)
  433. _node.IsEnableBlockUnknownCall = value
  434. }
  435. if value, ok := cc.mutation.IsEnableFence(); ok {
  436. _spec.SetField(child.FieldIsEnableFence, field.TypeBool, value)
  437. _node.IsEnableFence = value
  438. }
  439. if nodes := cc.mutation.ChildRefsIDs(); len(nodes) > 0 {
  440. edge := &sqlgraph.EdgeSpec{
  441. Rel: sqlgraph.O2M,
  442. Inverse: false,
  443. Table: child.ChildRefsTable,
  444. Columns: []string{child.ChildRefsColumn},
  445. Bidi: false,
  446. Target: &sqlgraph.EdgeTarget{
  447. IDSpec: sqlgraph.NewFieldSpec(childref.FieldID, field.TypeInt64),
  448. },
  449. }
  450. for _, k := range nodes {
  451. edge.Target.Nodes = append(edge.Target.Nodes, k)
  452. }
  453. _spec.Edges = append(_spec.Edges, edge)
  454. }
  455. if nodes := cc.mutation.ChildDeviceIDs(); len(nodes) > 0 {
  456. edge := &sqlgraph.EdgeSpec{
  457. Rel: sqlgraph.O2M,
  458. Inverse: false,
  459. Table: child.ChildDeviceTable,
  460. Columns: []string{child.ChildDeviceColumn},
  461. Bidi: false,
  462. Target: &sqlgraph.EdgeTarget{
  463. IDSpec: sqlgraph.NewFieldSpec(devicebind.FieldID, field.TypeInt64),
  464. },
  465. }
  466. for _, k := range nodes {
  467. edge.Target.Nodes = append(edge.Target.Nodes, k)
  468. }
  469. _spec.Edges = append(_spec.Edges, edge)
  470. }
  471. if nodes := cc.mutation.ChildContactsIDs(); len(nodes) > 0 {
  472. edge := &sqlgraph.EdgeSpec{
  473. Rel: sqlgraph.O2M,
  474. Inverse: false,
  475. Table: child.ChildContactsTable,
  476. Columns: []string{child.ChildContactsColumn},
  477. Bidi: false,
  478. Target: &sqlgraph.EdgeTarget{
  479. IDSpec: sqlgraph.NewFieldSpec(contacts.FieldID, field.TypeInt),
  480. },
  481. }
  482. for _, k := range nodes {
  483. edge.Target.Nodes = append(edge.Target.Nodes, k)
  484. }
  485. _spec.Edges = append(_spec.Edges, edge)
  486. }
  487. return _node, _spec
  488. }
  489. // ChildCreateBulk is the builder for creating many Child entities in bulk.
  490. type ChildCreateBulk struct {
  491. config
  492. err error
  493. builders []*ChildCreate
  494. }
  495. // Save creates the Child entities in the database.
  496. func (ccb *ChildCreateBulk) Save(ctx context.Context) ([]*Child, error) {
  497. if ccb.err != nil {
  498. return nil, ccb.err
  499. }
  500. specs := make([]*sqlgraph.CreateSpec, len(ccb.builders))
  501. nodes := make([]*Child, len(ccb.builders))
  502. mutators := make([]Mutator, len(ccb.builders))
  503. for i := range ccb.builders {
  504. func(i int, root context.Context) {
  505. builder := ccb.builders[i]
  506. builder.defaults()
  507. var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
  508. mutation, ok := m.(*ChildMutation)
  509. if !ok {
  510. return nil, fmt.Errorf("unexpected mutation type %T", m)
  511. }
  512. if err := builder.check(); err != nil {
  513. return nil, err
  514. }
  515. builder.mutation = mutation
  516. var err error
  517. nodes[i], specs[i] = builder.createSpec()
  518. if i < len(mutators)-1 {
  519. _, err = mutators[i+1].Mutate(root, ccb.builders[i+1].mutation)
  520. } else {
  521. spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
  522. // Invoke the actual operation on the latest mutation in the chain.
  523. if err = sqlgraph.BatchCreate(ctx, ccb.driver, spec); err != nil {
  524. if sqlgraph.IsConstraintError(err) {
  525. err = &ConstraintError{msg: err.Error(), wrap: err}
  526. }
  527. }
  528. }
  529. if err != nil {
  530. return nil, err
  531. }
  532. mutation.id = &nodes[i].ID
  533. mutation.done = true
  534. return nodes[i], nil
  535. })
  536. for i := len(builder.hooks) - 1; i >= 0; i-- {
  537. mut = builder.hooks[i](mut)
  538. }
  539. mutators[i] = mut
  540. }(i, ctx)
  541. }
  542. if len(mutators) > 0 {
  543. if _, err := mutators[0].Mutate(ctx, ccb.builders[0].mutation); err != nil {
  544. return nil, err
  545. }
  546. }
  547. return nodes, nil
  548. }
  549. // SaveX is like Save, but panics if an error occurs.
  550. func (ccb *ChildCreateBulk) SaveX(ctx context.Context) []*Child {
  551. v, err := ccb.Save(ctx)
  552. if err != nil {
  553. panic(err)
  554. }
  555. return v
  556. }
  557. // Exec executes the query.
  558. func (ccb *ChildCreateBulk) Exec(ctx context.Context) error {
  559. _, err := ccb.Save(ctx)
  560. return err
  561. }
  562. // ExecX is like Exec, but panics if an error occurs.
  563. func (ccb *ChildCreateBulk) ExecX(ctx context.Context) {
  564. if err := ccb.Exec(ctx); err != nil {
  565. panic(err)
  566. }
  567. }