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.
233 lines
6.6 KiB
233 lines
6.6 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 (pc *PasswordCreate) SetEmail(s string) *PasswordCreate { |
|
pc.mutation.SetEmail(s) |
|
return pc |
|
} |
|
|
|
// SetHash sets the "hash" field. |
|
func (pc *PasswordCreate) SetHash(b []byte) *PasswordCreate { |
|
pc.mutation.SetHash(b) |
|
return pc |
|
} |
|
|
|
// SetUsername sets the "username" field. |
|
func (pc *PasswordCreate) SetUsername(s string) *PasswordCreate { |
|
pc.mutation.SetUsername(s) |
|
return pc |
|
} |
|
|
|
// SetUserID sets the "user_id" field. |
|
func (pc *PasswordCreate) SetUserID(s string) *PasswordCreate { |
|
pc.mutation.SetUserID(s) |
|
return pc |
|
} |
|
|
|
// Mutation returns the PasswordMutation object of the builder. |
|
func (pc *PasswordCreate) Mutation() *PasswordMutation { |
|
return pc.mutation |
|
} |
|
|
|
// Save creates the Password in the database. |
|
func (pc *PasswordCreate) Save(ctx context.Context) (*Password, error) { |
|
return withHooks(ctx, pc.sqlSave, pc.mutation, pc.hooks) |
|
} |
|
|
|
// SaveX calls Save and panics if Save returns an error. |
|
func (pc *PasswordCreate) SaveX(ctx context.Context) *Password { |
|
v, err := pc.Save(ctx) |
|
if err != nil { |
|
panic(err) |
|
} |
|
return v |
|
} |
|
|
|
// Exec executes the query. |
|
func (pc *PasswordCreate) Exec(ctx context.Context) error { |
|
_, err := pc.Save(ctx) |
|
return err |
|
} |
|
|
|
// ExecX is like Exec, but panics if an error occurs. |
|
func (pc *PasswordCreate) ExecX(ctx context.Context) { |
|
if err := pc.Exec(ctx); err != nil { |
|
panic(err) |
|
} |
|
} |
|
|
|
// check runs all checks and user-defined validators on the builder. |
|
func (pc *PasswordCreate) check() error { |
|
if _, ok := pc.mutation.Email(); !ok { |
|
return &ValidationError{Name: "email", err: errors.New(`db: missing required field "Password.email"`)} |
|
} |
|
if v, ok := pc.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 := pc.mutation.Hash(); !ok { |
|
return &ValidationError{Name: "hash", err: errors.New(`db: missing required field "Password.hash"`)} |
|
} |
|
if _, ok := pc.mutation.Username(); !ok { |
|
return &ValidationError{Name: "username", err: errors.New(`db: missing required field "Password.username"`)} |
|
} |
|
if v, ok := pc.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 := pc.mutation.UserID(); !ok { |
|
return &ValidationError{Name: "user_id", err: errors.New(`db: missing required field "Password.user_id"`)} |
|
} |
|
if v, ok := pc.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 (pc *PasswordCreate) sqlSave(ctx context.Context) (*Password, error) { |
|
if err := pc.check(); err != nil { |
|
return nil, err |
|
} |
|
_node, _spec := pc.createSpec() |
|
if err := sqlgraph.CreateNode(ctx, pc.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) |
|
pc.mutation.id = &_node.ID |
|
pc.mutation.done = true |
|
return _node, nil |
|
} |
|
|
|
func (pc *PasswordCreate) createSpec() (*Password, *sqlgraph.CreateSpec) { |
|
var ( |
|
_node = &Password{config: pc.config} |
|
_spec = sqlgraph.NewCreateSpec(password.Table, sqlgraph.NewFieldSpec(password.FieldID, field.TypeInt)) |
|
) |
|
if value, ok := pc.mutation.Email(); ok { |
|
_spec.SetField(password.FieldEmail, field.TypeString, value) |
|
_node.Email = value |
|
} |
|
if value, ok := pc.mutation.Hash(); ok { |
|
_spec.SetField(password.FieldHash, field.TypeBytes, value) |
|
_node.Hash = value |
|
} |
|
if value, ok := pc.mutation.Username(); ok { |
|
_spec.SetField(password.FieldUsername, field.TypeString, value) |
|
_node.Username = value |
|
} |
|
if value, ok := pc.mutation.UserID(); ok { |
|
_spec.SetField(password.FieldUserID, field.TypeString, value) |
|
_node.UserID = value |
|
} |
|
return _node, _spec |
|
} |
|
|
|
// PasswordCreateBulk is the builder for creating many Password entities in bulk. |
|
type PasswordCreateBulk struct { |
|
config |
|
builders []*PasswordCreate |
|
} |
|
|
|
// Save creates the Password entities in the database. |
|
func (pcb *PasswordCreateBulk) Save(ctx context.Context) ([]*Password, error) { |
|
specs := make([]*sqlgraph.CreateSpec, len(pcb.builders)) |
|
nodes := make([]*Password, len(pcb.builders)) |
|
mutators := make([]Mutator, len(pcb.builders)) |
|
for i := range pcb.builders { |
|
func(i int, root context.Context) { |
|
builder := pcb.builders[i] |
|
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, pcb.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, pcb.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, pcb.builders[0].mutation); err != nil { |
|
return nil, err |
|
} |
|
} |
|
return nodes, nil |
|
} |
|
|
|
// SaveX is like Save, but panics if an error occurs. |
|
func (pcb *PasswordCreateBulk) SaveX(ctx context.Context) []*Password { |
|
v, err := pcb.Save(ctx) |
|
if err != nil { |
|
panic(err) |
|
} |
|
return v |
|
} |
|
|
|
// Exec executes the query. |
|
func (pcb *PasswordCreateBulk) Exec(ctx context.Context) error { |
|
_, err := pcb.Save(ctx) |
|
return err |
|
} |
|
|
|
// ExecX is like Exec, but panics if an error occurs. |
|
func (pcb *PasswordCreateBulk) ExecX(ctx context.Context) { |
|
if err := pcb.Exec(ctx); err != nil { |
|
panic(err) |
|
} |
|
}
|
|
|