2021-01-01 20:34:47 +00:00
|
|
|
// Code generated by entc, DO NOT EDIT.
|
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-03-24 06:37:51 +00:00
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
|
|
"entgo.io/ent/schema/field"
|
2021-01-01 20:34:47 +00:00
|
|
|
"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) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
node *Password
|
|
|
|
)
|
|
|
|
if len(pc.hooks) == 0 {
|
|
|
|
if err = pc.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node, err = pc.sqlSave(ctx)
|
|
|
|
} else {
|
|
|
|
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 = pc.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pc.mutation = mutation
|
2021-09-13 13:48:02 +00:00
|
|
|
if node, err = pc.sqlSave(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mutation.id = &node.ID
|
2021-01-01 20:34:47 +00:00
|
|
|
mutation.done = true
|
|
|
|
return node, err
|
|
|
|
})
|
|
|
|
for i := len(pc.hooks) - 1; i >= 0; i-- {
|
2021-09-13 13:48:02 +00:00
|
|
|
if pc.hooks[i] == nil {
|
|
|
|
return nil, fmt.Errorf("db: uninitialized hook (forgotten import db/runtime?)")
|
|
|
|
}
|
2021-01-01 20:34:47 +00:00
|
|
|
mut = pc.hooks[i](mut)
|
|
|
|
}
|
|
|
|
if _, err := mut.Mutate(ctx, pc.mutation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:48:02 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 20:34:47 +00:00
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
|
|
func (pc *PasswordCreate) check() error {
|
|
|
|
if _, ok := pc.mutation.Email(); !ok {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "email", err: errors.New(`db: missing required field "Password.email"`)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if v, ok := pc.mutation.Email(); ok {
|
|
|
|
if err := password.EmailValidator(v); err != nil {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "email", err: fmt.Errorf(`db: validator failed for field "Password.email": %w`, err)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := pc.mutation.Hash(); !ok {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "hash", err: errors.New(`db: missing required field "Password.hash"`)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if _, ok := pc.mutation.Username(); !ok {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "username", err: errors.New(`db: missing required field "Password.username"`)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if v, ok := pc.mutation.Username(); ok {
|
|
|
|
if err := password.UsernameValidator(v); err != nil {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "username", err: fmt.Errorf(`db: validator failed for field "Password.username": %w`, err)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := pc.mutation.UserID(); !ok {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "user_id", err: errors.New(`db: missing required field "Password.user_id"`)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if v, ok := pc.mutation.UserID(); ok {
|
|
|
|
if err := password.UserIDValidator(v); err != nil {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "user_id", err: fmt.Errorf(`db: validator failed for field "Password.user_id": %w`, err)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *PasswordCreate) sqlSave(ctx context.Context) (*Password, error) {
|
|
|
|
_node, _spec := pc.createSpec()
|
|
|
|
if err := sqlgraph.CreateNode(ctx, pc.driver, _spec); err != nil {
|
2021-09-13 13:48:02 +00:00
|
|
|
if sqlgraph.IsConstraintError(err) {
|
|
|
|
err = &ConstraintError{err.Error(), err}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id := _spec.ID.Value.(int64)
|
|
|
|
_node.ID = int(id)
|
|
|
|
return _node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc *PasswordCreate) createSpec() (*Password, *sqlgraph.CreateSpec) {
|
|
|
|
var (
|
|
|
|
_node = &Password{config: pc.config}
|
|
|
|
_spec = &sqlgraph.CreateSpec{
|
|
|
|
Table: password.Table,
|
|
|
|
ID: &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeInt,
|
|
|
|
Column: password.FieldID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if value, ok := pc.mutation.Email(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: password.FieldEmail,
|
|
|
|
})
|
|
|
|
_node.Email = value
|
|
|
|
}
|
|
|
|
if value, ok := pc.mutation.Hash(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeBytes,
|
|
|
|
Value: value,
|
|
|
|
Column: password.FieldHash,
|
|
|
|
})
|
|
|
|
_node.Hash = value
|
|
|
|
}
|
|
|
|
if value, ok := pc.mutation.Username(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: password.FieldUsername,
|
|
|
|
})
|
|
|
|
_node.Username = value
|
|
|
|
}
|
|
|
|
if value, ok := pc.mutation.UserID(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: password.FieldUserID,
|
|
|
|
})
|
|
|
|
_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
|
|
|
|
nodes[i], specs[i] = builder.createSpec()
|
|
|
|
var err error
|
|
|
|
if i < len(mutators)-1 {
|
|
|
|
_, err = mutators[i+1].Mutate(root, pcb.builders[i+1].mutation)
|
|
|
|
} else {
|
2021-09-13 13:48:02 +00:00
|
|
|
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
|
2021-01-01 20:34:47 +00:00
|
|
|
// Invoke the actual operation on the latest mutation in the chain.
|
2021-09-13 13:48:02 +00:00
|
|
|
if err = sqlgraph.BatchCreate(ctx, pcb.driver, spec); err != nil {
|
|
|
|
if sqlgraph.IsConstraintError(err) {
|
|
|
|
err = &ConstraintError{err.Error(), err}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-13 13:48:02 +00:00
|
|
|
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)
|
|
|
|
}
|
2021-01-01 20:34:47 +00:00
|
|
|
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
|
|
|
|
}
|
2021-09-13 13:48:02 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|