mirror of https://github.com/dexidp/dex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
321 lines
9.2 KiB
321 lines
9.2 KiB
// Code generated by ent, DO NOT EDIT. |
|
|
|
package db |
|
|
|
import ( |
|
"context" |
|
"errors" |
|
"fmt" |
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph" |
|
"entgo.io/ent/schema/field" |
|
"github.com/dexidp/dex/storage/ent/db/password" |
|
) |
|
|
|
// PasswordCreate is the builder for creating a Password entity. |
|
type PasswordCreate struct { |
|
config |
|
mutation *PasswordMutation |
|
hooks []Hook |
|
} |
|
|
|
// SetEmail sets the "email" field. |
|
func (_c *PasswordCreate) SetEmail(v string) *PasswordCreate { |
|
_c.mutation.SetEmail(v) |
|
return _c |
|
} |
|
|
|
// SetHash sets the "hash" field. |
|
func (_c *PasswordCreate) SetHash(v []byte) *PasswordCreate { |
|
_c.mutation.SetHash(v) |
|
return _c |
|
} |
|
|
|
// SetUsername sets the "username" field. |
|
func (_c *PasswordCreate) SetUsername(v string) *PasswordCreate { |
|
_c.mutation.SetUsername(v) |
|
return _c |
|
} |
|
|
|
// SetName sets the "name" field. |
|
func (_c *PasswordCreate) SetName(v string) *PasswordCreate { |
|
_c.mutation.SetName(v) |
|
return _c |
|
} |
|
|
|
// SetNillableName sets the "name" field if the given value is not nil. |
|
func (_c *PasswordCreate) SetNillableName(v *string) *PasswordCreate { |
|
if v != nil { |
|
_c.SetName(*v) |
|
} |
|
return _c |
|
} |
|
|
|
// SetPreferredUsername sets the "preferred_username" field. |
|
func (_c *PasswordCreate) SetPreferredUsername(v string) *PasswordCreate { |
|
_c.mutation.SetPreferredUsername(v) |
|
return _c |
|
} |
|
|
|
// SetNillablePreferredUsername sets the "preferred_username" field if the given value is not nil. |
|
func (_c *PasswordCreate) SetNillablePreferredUsername(v *string) *PasswordCreate { |
|
if v != nil { |
|
_c.SetPreferredUsername(*v) |
|
} |
|
return _c |
|
} |
|
|
|
// SetEmailVerified sets the "email_verified" field. |
|
func (_c *PasswordCreate) SetEmailVerified(v bool) *PasswordCreate { |
|
_c.mutation.SetEmailVerified(v) |
|
return _c |
|
} |
|
|
|
// SetNillableEmailVerified sets the "email_verified" field if the given value is not nil. |
|
func (_c *PasswordCreate) SetNillableEmailVerified(v *bool) *PasswordCreate { |
|
if v != nil { |
|
_c.SetEmailVerified(*v) |
|
} |
|
return _c |
|
} |
|
|
|
// SetUserID sets the "user_id" field. |
|
func (_c *PasswordCreate) SetUserID(v string) *PasswordCreate { |
|
_c.mutation.SetUserID(v) |
|
return _c |
|
} |
|
|
|
// SetGroups sets the "groups" field. |
|
func (_c *PasswordCreate) SetGroups(v []string) *PasswordCreate { |
|
_c.mutation.SetGroups(v) |
|
return _c |
|
} |
|
|
|
// Mutation returns the PasswordMutation object of the builder. |
|
func (_c *PasswordCreate) Mutation() *PasswordMutation { |
|
return _c.mutation |
|
} |
|
|
|
// Save creates the Password in the database. |
|
func (_c *PasswordCreate) Save(ctx context.Context) (*Password, error) { |
|
_c.defaults() |
|
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) |
|
} |
|
|
|
// SaveX calls Save and panics if Save returns an error. |
|
func (_c *PasswordCreate) SaveX(ctx context.Context) *Password { |
|
v, err := _c.Save(ctx) |
|
if err != nil { |
|
panic(err) |
|
} |
|
return v |
|
} |
|
|
|
// Exec executes the query. |
|
func (_c *PasswordCreate) Exec(ctx context.Context) error { |
|
_, err := _c.Save(ctx) |
|
return err |
|
} |
|
|
|
// ExecX is like Exec, but panics if an error occurs. |
|
func (_c *PasswordCreate) ExecX(ctx context.Context) { |
|
if err := _c.Exec(ctx); err != nil { |
|
panic(err) |
|
} |
|
} |
|
|
|
// defaults sets the default values of the builder before save. |
|
func (_c *PasswordCreate) defaults() { |
|
if _, ok := _c.mutation.Name(); !ok { |
|
v := password.DefaultName |
|
_c.mutation.SetName(v) |
|
} |
|
if _, ok := _c.mutation.PreferredUsername(); !ok { |
|
v := password.DefaultPreferredUsername |
|
_c.mutation.SetPreferredUsername(v) |
|
} |
|
} |
|
|
|
// check runs all checks and user-defined validators on the builder. |
|
func (_c *PasswordCreate) check() error { |
|
if _, ok := _c.mutation.Email(); !ok { |
|
return &ValidationError{Name: "email", err: errors.New(`db: missing required field "Password.email"`)} |
|
} |
|
if v, ok := _c.mutation.Email(); ok { |
|
if err := password.EmailValidator(v); err != nil { |
|
return &ValidationError{Name: "email", err: fmt.Errorf(`db: validator failed for field "Password.email": %w`, err)} |
|
} |
|
} |
|
if _, ok := _c.mutation.Hash(); !ok { |
|
return &ValidationError{Name: "hash", err: errors.New(`db: missing required field "Password.hash"`)} |
|
} |
|
if _, ok := _c.mutation.Username(); !ok { |
|
return &ValidationError{Name: "username", err: errors.New(`db: missing required field "Password.username"`)} |
|
} |
|
if v, ok := _c.mutation.Username(); ok { |
|
if err := password.UsernameValidator(v); err != nil { |
|
return &ValidationError{Name: "username", err: fmt.Errorf(`db: validator failed for field "Password.username": %w`, err)} |
|
} |
|
} |
|
if _, ok := _c.mutation.Name(); !ok { |
|
return &ValidationError{Name: "name", err: errors.New(`db: missing required field "Password.name"`)} |
|
} |
|
if _, ok := _c.mutation.PreferredUsername(); !ok { |
|
return &ValidationError{Name: "preferred_username", err: errors.New(`db: missing required field "Password.preferred_username"`)} |
|
} |
|
if _, ok := _c.mutation.UserID(); !ok { |
|
return &ValidationError{Name: "user_id", err: errors.New(`db: missing required field "Password.user_id"`)} |
|
} |
|
if v, ok := _c.mutation.UserID(); ok { |
|
if err := password.UserIDValidator(v); err != nil { |
|
return &ValidationError{Name: "user_id", err: fmt.Errorf(`db: validator failed for field "Password.user_id": %w`, err)} |
|
} |
|
} |
|
return nil |
|
} |
|
|
|
func (_c *PasswordCreate) sqlSave(ctx context.Context) (*Password, error) { |
|
if err := _c.check(); err != nil { |
|
return nil, err |
|
} |
|
_node, _spec := _c.createSpec() |
|
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil { |
|
if sqlgraph.IsConstraintError(err) { |
|
err = &ConstraintError{msg: err.Error(), wrap: err} |
|
} |
|
return nil, err |
|
} |
|
id := _spec.ID.Value.(int64) |
|
_node.ID = int(id) |
|
_c.mutation.id = &_node.ID |
|
_c.mutation.done = true |
|
return _node, nil |
|
} |
|
|
|
func (_c *PasswordCreate) createSpec() (*Password, *sqlgraph.CreateSpec) { |
|
var ( |
|
_node = &Password{config: _c.config} |
|
_spec = sqlgraph.NewCreateSpec(password.Table, sqlgraph.NewFieldSpec(password.FieldID, field.TypeInt)) |
|
) |
|
if value, ok := _c.mutation.Email(); ok { |
|
_spec.SetField(password.FieldEmail, field.TypeString, value) |
|
_node.Email = value |
|
} |
|
if value, ok := _c.mutation.Hash(); ok { |
|
_spec.SetField(password.FieldHash, field.TypeBytes, value) |
|
_node.Hash = value |
|
} |
|
if value, ok := _c.mutation.Username(); ok { |
|
_spec.SetField(password.FieldUsername, field.TypeString, value) |
|
_node.Username = value |
|
} |
|
if value, ok := _c.mutation.Name(); ok { |
|
_spec.SetField(password.FieldName, field.TypeString, value) |
|
_node.Name = value |
|
} |
|
if value, ok := _c.mutation.PreferredUsername(); ok { |
|
_spec.SetField(password.FieldPreferredUsername, field.TypeString, value) |
|
_node.PreferredUsername = value |
|
} |
|
if value, ok := _c.mutation.EmailVerified(); ok { |
|
_spec.SetField(password.FieldEmailVerified, field.TypeBool, value) |
|
_node.EmailVerified = &value |
|
} |
|
if value, ok := _c.mutation.UserID(); ok { |
|
_spec.SetField(password.FieldUserID, field.TypeString, value) |
|
_node.UserID = value |
|
} |
|
if value, ok := _c.mutation.Groups(); ok { |
|
_spec.SetField(password.FieldGroups, field.TypeJSON, value) |
|
_node.Groups = value |
|
} |
|
return _node, _spec |
|
} |
|
|
|
// PasswordCreateBulk is the builder for creating many Password entities in bulk. |
|
type PasswordCreateBulk struct { |
|
config |
|
err error |
|
builders []*PasswordCreate |
|
} |
|
|
|
// Save creates the Password entities in the database. |
|
func (_c *PasswordCreateBulk) Save(ctx context.Context) ([]*Password, error) { |
|
if _c.err != nil { |
|
return nil, _c.err |
|
} |
|
specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) |
|
nodes := make([]*Password, len(_c.builders)) |
|
mutators := make([]Mutator, len(_c.builders)) |
|
for i := range _c.builders { |
|
func(i int, root context.Context) { |
|
builder := _c.builders[i] |
|
builder.defaults() |
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) { |
|
mutation, ok := m.(*PasswordMutation) |
|
if !ok { |
|
return nil, fmt.Errorf("unexpected mutation type %T", m) |
|
} |
|
if err := builder.check(); err != nil { |
|
return nil, err |
|
} |
|
builder.mutation = mutation |
|
var err error |
|
nodes[i], specs[i] = builder.createSpec() |
|
if i < len(mutators)-1 { |
|
_, err = mutators[i+1].Mutate(root, _c.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, _c.driver, spec); err != nil { |
|
if sqlgraph.IsConstraintError(err) { |
|
err = &ConstraintError{msg: err.Error(), wrap: err} |
|
} |
|
} |
|
} |
|
if err != nil { |
|
return nil, err |
|
} |
|
mutation.id = &nodes[i].ID |
|
if specs[i].ID.Value != nil { |
|
id := specs[i].ID.Value.(int64) |
|
nodes[i].ID = int(id) |
|
} |
|
mutation.done = true |
|
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, _c.builders[0].mutation); err != nil { |
|
return nil, err |
|
} |
|
} |
|
return nodes, nil |
|
} |
|
|
|
// SaveX is like Save, but panics if an error occurs. |
|
func (_c *PasswordCreateBulk) SaveX(ctx context.Context) []*Password { |
|
v, err := _c.Save(ctx) |
|
if err != nil { |
|
panic(err) |
|
} |
|
return v |
|
} |
|
|
|
// Exec executes the query. |
|
func (_c *PasswordCreateBulk) Exec(ctx context.Context) error { |
|
_, err := _c.Save(ctx) |
|
return err |
|
} |
|
|
|
// ExecX is like Exec, but panics if an error occurs. |
|
func (_c *PasswordCreateBulk) ExecX(ctx context.Context) { |
|
if err := _c.Exec(ctx); err != nil { |
|
panic(err) |
|
} |
|
}
|
|
|