123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527 |
- // Code generated by entc, DO NOT EDIT.
- package ent
- import (
- "context"
- "errors"
- "fmt"
- "code.osinet.fr/fgm/go__ent_demo/ent/car"
- "code.osinet.fr/fgm/go__ent_demo/ent/group"
- "code.osinet.fr/fgm/go__ent_demo/ent/user"
- "entgo.io/ent/dialect/sql/sqlgraph"
- "entgo.io/ent/schema/field"
- )
- // UserCreate is the builder for creating a User entity.
- type UserCreate struct {
- config
- mutation *UserMutation
- hooks []Hook
- }
- // SetAge sets the "age" field.
- func (uc *UserCreate) SetAge(i int) *UserCreate {
- uc.mutation.SetAge(i)
- return uc
- }
- // SetName sets the "name" field.
- func (uc *UserCreate) SetName(s string) *UserCreate {
- uc.mutation.SetName(s)
- return uc
- }
- // SetNillableName sets the "name" field if the given value is not nil.
- func (uc *UserCreate) SetNillableName(s *string) *UserCreate {
- if s != nil {
- uc.SetName(*s)
- }
- return uc
- }
- // SetPassword sets the "password" field.
- func (uc *UserCreate) SetPassword(s string) *UserCreate {
- uc.mutation.SetPassword(s)
- return uc
- }
- // SetNillablePassword sets the "password" field if the given value is not nil.
- func (uc *UserCreate) SetNillablePassword(s *string) *UserCreate {
- if s != nil {
- uc.SetPassword(*s)
- }
- return uc
- }
- // SetSize sets the "size" field.
- func (uc *UserCreate) SetSize(u user.Size) *UserCreate {
- uc.mutation.SetSize(u)
- return uc
- }
- // SetNillableSize sets the "size" field if the given value is not nil.
- func (uc *UserCreate) SetNillableSize(u *user.Size) *UserCreate {
- if u != nil {
- uc.SetSize(*u)
- }
- return uc
- }
- // SetSpouseID sets the "spouse_id" field.
- func (uc *UserCreate) SetSpouseID(i int) *UserCreate {
- uc.mutation.SetSpouseID(i)
- return uc
- }
- // SetNillableSpouseID sets the "spouse_id" field if the given value is not nil.
- func (uc *UserCreate) SetNillableSpouseID(i *int) *UserCreate {
- if i != nil {
- uc.SetSpouseID(*i)
- }
- return uc
- }
- // AddCarIDs adds the "cars" edge to the Car entity by IDs.
- func (uc *UserCreate) AddCarIDs(ids ...int) *UserCreate {
- uc.mutation.AddCarIDs(ids...)
- return uc
- }
- // AddCars adds the "cars" edges to the Car entity.
- func (uc *UserCreate) AddCars(c ...*Car) *UserCreate {
- ids := make([]int, len(c))
- for i := range c {
- ids[i] = c[i].ID
- }
- return uc.AddCarIDs(ids...)
- }
- // AddGroupIDs adds the "groups" edge to the Group entity by IDs.
- func (uc *UserCreate) AddGroupIDs(ids ...int) *UserCreate {
- uc.mutation.AddGroupIDs(ids...)
- return uc
- }
- // AddGroups adds the "groups" edges to the Group entity.
- func (uc *UserCreate) AddGroups(g ...*Group) *UserCreate {
- ids := make([]int, len(g))
- for i := range g {
- ids[i] = g[i].ID
- }
- return uc.AddGroupIDs(ids...)
- }
- // SetSpouse sets the "spouse" edge to the User entity.
- func (uc *UserCreate) SetSpouse(u *User) *UserCreate {
- return uc.SetSpouseID(u.ID)
- }
- // AddFollowerIDs adds the "followers" edge to the User entity by IDs.
- func (uc *UserCreate) AddFollowerIDs(ids ...int) *UserCreate {
- uc.mutation.AddFollowerIDs(ids...)
- return uc
- }
- // AddFollowers adds the "followers" edges to the User entity.
- func (uc *UserCreate) AddFollowers(u ...*User) *UserCreate {
- ids := make([]int, len(u))
- for i := range u {
- ids[i] = u[i].ID
- }
- return uc.AddFollowerIDs(ids...)
- }
- // AddFollowingIDs adds the "following" edge to the User entity by IDs.
- func (uc *UserCreate) AddFollowingIDs(ids ...int) *UserCreate {
- uc.mutation.AddFollowingIDs(ids...)
- return uc
- }
- // AddFollowing adds the "following" edges to the User entity.
- func (uc *UserCreate) AddFollowing(u ...*User) *UserCreate {
- ids := make([]int, len(u))
- for i := range u {
- ids[i] = u[i].ID
- }
- return uc.AddFollowingIDs(ids...)
- }
- // AddFriendIDs adds the "friends" edge to the User entity by IDs.
- func (uc *UserCreate) AddFriendIDs(ids ...int) *UserCreate {
- uc.mutation.AddFriendIDs(ids...)
- return uc
- }
- // AddFriends adds the "friends" edges to the User entity.
- func (uc *UserCreate) AddFriends(u ...*User) *UserCreate {
- ids := make([]int, len(u))
- for i := range u {
- ids[i] = u[i].ID
- }
- return uc.AddFriendIDs(ids...)
- }
- // Mutation returns the UserMutation object of the builder.
- func (uc *UserCreate) Mutation() *UserMutation {
- return uc.mutation
- }
- // Save creates the User in the database.
- func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
- var (
- err error
- node *User
- )
- uc.defaults()
- if len(uc.hooks) == 0 {
- if err = uc.check(); err != nil {
- return nil, err
- }
- node, err = uc.sqlSave(ctx)
- } else {
- var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
- mutation, ok := m.(*UserMutation)
- if !ok {
- return nil, fmt.Errorf("unexpected mutation type %T", m)
- }
- if err = uc.check(); err != nil {
- return nil, err
- }
- uc.mutation = mutation
- if node, err = uc.sqlSave(ctx); err != nil {
- return nil, err
- }
- mutation.id = &node.ID
- mutation.done = true
- return node, err
- })
- for i := len(uc.hooks) - 1; i >= 0; i-- {
- if uc.hooks[i] == nil {
- return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
- }
- mut = uc.hooks[i](mut)
- }
- if _, err := mut.Mutate(ctx, uc.mutation); err != nil {
- return nil, err
- }
- }
- return node, err
- }
- // SaveX calls Save and panics if Save returns an error.
- func (uc *UserCreate) SaveX(ctx context.Context) *User {
- v, err := uc.Save(ctx)
- if err != nil {
- panic(err)
- }
- return v
- }
- // Exec executes the query.
- func (uc *UserCreate) Exec(ctx context.Context) error {
- _, err := uc.Save(ctx)
- return err
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (uc *UserCreate) ExecX(ctx context.Context) {
- if err := uc.Exec(ctx); err != nil {
- panic(err)
- }
- }
- // defaults sets the default values of the builder before save.
- func (uc *UserCreate) defaults() {
- if _, ok := uc.mutation.Name(); !ok {
- v := user.DefaultName
- uc.mutation.SetName(v)
- }
- if _, ok := uc.mutation.Size(); !ok {
- v := user.DefaultSize
- uc.mutation.SetSize(v)
- }
- }
- // check runs all checks and user-defined validators on the builder.
- func (uc *UserCreate) check() error {
- if _, ok := uc.mutation.Age(); !ok {
- return &ValidationError{Name: "age", err: errors.New(`ent: missing required field "User.age"`)}
- }
- if v, ok := uc.mutation.Age(); ok {
- if err := user.AgeValidator(v); err != nil {
- return &ValidationError{Name: "age", err: fmt.Errorf(`ent: validator failed for field "User.age": %w`, err)}
- }
- }
- if _, ok := uc.mutation.Name(); !ok {
- return &ValidationError{Name: "name", err: errors.New(`ent: missing required field "User.name"`)}
- }
- if _, ok := uc.mutation.Size(); !ok {
- return &ValidationError{Name: "size", err: errors.New(`ent: missing required field "User.size"`)}
- }
- if v, ok := uc.mutation.Size(); ok {
- if err := user.SizeValidator(v); err != nil {
- return &ValidationError{Name: "size", err: fmt.Errorf(`ent: validator failed for field "User.size": %w`, err)}
- }
- }
- return nil
- }
- func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
- _node, _spec := uc.createSpec()
- if err := sqlgraph.CreateNode(ctx, uc.driver, _spec); err != nil {
- if sqlgraph.IsConstraintError(err) {
- err = &ConstraintError{err.Error(), err}
- }
- return nil, err
- }
- id := _spec.ID.Value.(int64)
- _node.ID = int(id)
- return _node, nil
- }
- func (uc *UserCreate) createSpec() (*User, *sqlgraph.CreateSpec) {
- var (
- _node = &User{config: uc.config}
- _spec = &sqlgraph.CreateSpec{
- Table: user.Table,
- ID: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: user.FieldID,
- },
- }
- )
- if value, ok := uc.mutation.Age(); ok {
- _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Value: value,
- Column: user.FieldAge,
- })
- _node.Age = value
- }
- if value, ok := uc.mutation.Name(); ok {
- _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
- Type: field.TypeString,
- Value: value,
- Column: user.FieldName,
- })
- _node.Name = value
- }
- if value, ok := uc.mutation.Password(); ok {
- _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
- Type: field.TypeString,
- Value: value,
- Column: user.FieldPassword,
- })
- _node.Password = value
- }
- if value, ok := uc.mutation.Size(); ok {
- _spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
- Type: field.TypeEnum,
- Value: value,
- Column: user.FieldSize,
- })
- _node.Size = value
- }
- if nodes := uc.mutation.CarsIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2M,
- Inverse: false,
- Table: user.CarsTable,
- Columns: []string{user.CarsColumn},
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: car.FieldID,
- },
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges = append(_spec.Edges, edge)
- }
- if nodes := uc.mutation.GroupsIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.M2M,
- Inverse: true,
- Table: user.GroupsTable,
- Columns: user.GroupsPrimaryKey,
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: group.FieldID,
- },
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges = append(_spec.Edges, edge)
- }
- if nodes := uc.mutation.SpouseIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.O2O,
- Inverse: false,
- Table: user.SpouseTable,
- Columns: []string{user.SpouseColumn},
- Bidi: true,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: user.FieldID,
- },
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _node.SpouseID = nodes[0]
- _spec.Edges = append(_spec.Edges, edge)
- }
- if nodes := uc.mutation.FollowersIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.M2M,
- Inverse: true,
- Table: user.FollowersTable,
- Columns: user.FollowersPrimaryKey,
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: user.FieldID,
- },
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges = append(_spec.Edges, edge)
- }
- if nodes := uc.mutation.FollowingIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.M2M,
- Inverse: false,
- Table: user.FollowingTable,
- Columns: user.FollowingPrimaryKey,
- Bidi: false,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: user.FieldID,
- },
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges = append(_spec.Edges, edge)
- }
- if nodes := uc.mutation.FriendsIDs(); len(nodes) > 0 {
- edge := &sqlgraph.EdgeSpec{
- Rel: sqlgraph.M2M,
- Inverse: false,
- Table: user.FriendsTable,
- Columns: user.FriendsPrimaryKey,
- Bidi: true,
- Target: &sqlgraph.EdgeTarget{
- IDSpec: &sqlgraph.FieldSpec{
- Type: field.TypeInt,
- Column: user.FieldID,
- },
- },
- }
- for _, k := range nodes {
- edge.Target.Nodes = append(edge.Target.Nodes, k)
- }
- _spec.Edges = append(_spec.Edges, edge)
- }
- return _node, _spec
- }
- // UserCreateBulk is the builder for creating many User entities in bulk.
- type UserCreateBulk struct {
- config
- builders []*UserCreate
- }
- // Save creates the User entities in the database.
- func (ucb *UserCreateBulk) Save(ctx context.Context) ([]*User, error) {
- specs := make([]*sqlgraph.CreateSpec, len(ucb.builders))
- nodes := make([]*User, len(ucb.builders))
- mutators := make([]Mutator, len(ucb.builders))
- for i := range ucb.builders {
- func(i int, root context.Context) {
- builder := ucb.builders[i]
- builder.defaults()
- var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
- mutation, ok := m.(*UserMutation)
- if !ok {
- return nil, fmt.Errorf("unexpected mutation type %T", m)
- }
- if err := builder.check(); err != nil {
- return nil, err
- }
- builder.mutation = mutation
- nodes[i], specs[i] = builder.createSpec()
- var err error
- if i < len(mutators)-1 {
- _, err = mutators[i+1].Mutate(root, ucb.builders[i+1].mutation)
- } else {
- spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
- // Invoke the actual operation on the latest mutation in the chain.
- if err = sqlgraph.BatchCreate(ctx, ucb.driver, spec); err != nil {
- if sqlgraph.IsConstraintError(err) {
- err = &ConstraintError{err.Error(), err}
- }
- }
- }
- if err != nil {
- return nil, err
- }
- mutation.id = &nodes[i].ID
- mutation.done = true
- if specs[i].ID.Value != nil {
- id := specs[i].ID.Value.(int64)
- nodes[i].ID = int(id)
- }
- return nodes[i], nil
- })
- for i := len(builder.hooks) - 1; i >= 0; i-- {
- mut = builder.hooks[i](mut)
- }
- mutators[i] = mut
- }(i, ctx)
- }
- if len(mutators) > 0 {
- if _, err := mutators[0].Mutate(ctx, ucb.builders[0].mutation); err != nil {
- return nil, err
- }
- }
- return nodes, nil
- }
- // SaveX is like Save, but panics if an error occurs.
- func (ucb *UserCreateBulk) SaveX(ctx context.Context) []*User {
- v, err := ucb.Save(ctx)
- if err != nil {
- panic(err)
- }
- return v
- }
- // Exec executes the query.
- func (ucb *UserCreateBulk) Exec(ctx context.Context) error {
- _, err := ucb.Save(ctx)
- return err
- }
- // ExecX is like Exec, but panics if an error occurs.
- func (ucb *UserCreateBulk) ExecX(ctx context.Context) {
- if err := ucb.Exec(ctx); err != nil {
- panic(err)
- }
- }
|