2022-08-29 07:16:12 +00:00
|
|
|
// Code generated by ent, DO NOT EDIT.
|
2021-01-01 20:34:47 +00:00
|
|
|
|
|
|
|
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/connector"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConnectorCreate is the builder for creating a Connector entity.
|
|
|
|
type ConnectorCreate struct {
|
|
|
|
config
|
|
|
|
mutation *ConnectorMutation
|
|
|
|
hooks []Hook
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetType sets the "type" field.
|
|
|
|
func (cc *ConnectorCreate) SetType(s string) *ConnectorCreate {
|
|
|
|
cc.mutation.SetType(s)
|
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetName sets the "name" field.
|
|
|
|
func (cc *ConnectorCreate) SetName(s string) *ConnectorCreate {
|
|
|
|
cc.mutation.SetName(s)
|
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetResourceVersion sets the "resource_version" field.
|
|
|
|
func (cc *ConnectorCreate) SetResourceVersion(s string) *ConnectorCreate {
|
|
|
|
cc.mutation.SetResourceVersion(s)
|
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig sets the "config" field.
|
|
|
|
func (cc *ConnectorCreate) SetConfig(b []byte) *ConnectorCreate {
|
|
|
|
cc.mutation.SetConfig(b)
|
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetID sets the "id" field.
|
|
|
|
func (cc *ConnectorCreate) SetID(s string) *ConnectorCreate {
|
|
|
|
cc.mutation.SetID(s)
|
|
|
|
return cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mutation returns the ConnectorMutation object of the builder.
|
|
|
|
func (cc *ConnectorCreate) Mutation() *ConnectorMutation {
|
|
|
|
return cc.mutation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save creates the Connector in the database.
|
|
|
|
func (cc *ConnectorCreate) Save(ctx context.Context) (*Connector, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
node *Connector
|
|
|
|
)
|
|
|
|
if len(cc.hooks) == 0 {
|
|
|
|
if err = cc.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node, err = cc.sqlSave(ctx)
|
|
|
|
} else {
|
|
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
|
|
mutation, ok := m.(*ConnectorMutation)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
|
|
}
|
|
|
|
if err = cc.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cc.mutation = mutation
|
2021-09-13 13:48:02 +00:00
|
|
|
if node, err = cc.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(cc.hooks) - 1; i >= 0; i-- {
|
2021-09-13 13:48:02 +00:00
|
|
|
if cc.hooks[i] == nil {
|
|
|
|
return nil, fmt.Errorf("db: uninitialized hook (forgotten import db/runtime?)")
|
|
|
|
}
|
2021-01-01 20:34:47 +00:00
|
|
|
mut = cc.hooks[i](mut)
|
|
|
|
}
|
2022-08-29 07:16:12 +00:00
|
|
|
v, err := mut.Mutate(ctx, cc.mutation)
|
|
|
|
if err != nil {
|
2021-01-01 20:34:47 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-08-29 07:16:12 +00:00
|
|
|
nv, ok := v.(*Connector)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected node type %T returned from ConnectorMutation", v)
|
|
|
|
}
|
|
|
|
node = nv
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
|
|
func (cc *ConnectorCreate) SaveX(ctx context.Context) *Connector {
|
|
|
|
v, err := cc.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:48:02 +00:00
|
|
|
// Exec executes the query.
|
|
|
|
func (cc *ConnectorCreate) Exec(ctx context.Context) error {
|
|
|
|
_, err := cc.Save(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
|
|
func (cc *ConnectorCreate) ExecX(ctx context.Context) {
|
|
|
|
if err := cc.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 (cc *ConnectorCreate) check() error {
|
|
|
|
if _, ok := cc.mutation.GetType(); !ok {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "type", err: errors.New(`db: missing required field "Connector.type"`)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if v, ok := cc.mutation.GetType(); ok {
|
|
|
|
if err := connector.TypeValidator(v); err != nil {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "type", err: fmt.Errorf(`db: validator failed for field "Connector.type": %w`, err)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := cc.mutation.Name(); !ok {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "name", err: errors.New(`db: missing required field "Connector.name"`)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if v, ok := cc.mutation.Name(); ok {
|
|
|
|
if err := connector.NameValidator(v); err != nil {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "name", err: fmt.Errorf(`db: validator failed for field "Connector.name": %w`, err)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := cc.mutation.ResourceVersion(); !ok {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "resource_version", err: errors.New(`db: missing required field "Connector.resource_version"`)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if _, ok := cc.mutation.Config(); !ok {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "config", err: errors.New(`db: missing required field "Connector.config"`)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
if v, ok := cc.mutation.ID(); ok {
|
|
|
|
if err := connector.IDValidator(v); err != nil {
|
2022-03-07 09:15:01 +00:00
|
|
|
return &ValidationError{Name: "id", err: fmt.Errorf(`db: validator failed for field "Connector.id": %w`, err)}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *ConnectorCreate) sqlSave(ctx context.Context) (*Connector, error) {
|
|
|
|
_node, _spec := cc.createSpec()
|
|
|
|
if err := sqlgraph.CreateNode(ctx, cc.driver, _spec); err != nil {
|
2021-09-13 13:48:02 +00:00
|
|
|
if sqlgraph.IsConstraintError(err) {
|
2022-08-29 07:16:12 +00:00
|
|
|
err = &ConstraintError{msg: err.Error(), wrap: err}
|
2021-01-01 20:34:47 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-07 09:15:01 +00:00
|
|
|
if _spec.ID.Value != nil {
|
|
|
|
if id, ok := _spec.ID.Value.(string); ok {
|
|
|
|
_node.ID = id
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("unexpected Connector.ID type: %T", _spec.ID.Value)
|
|
|
|
}
|
|
|
|
}
|
2021-01-01 20:34:47 +00:00
|
|
|
return _node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cc *ConnectorCreate) createSpec() (*Connector, *sqlgraph.CreateSpec) {
|
|
|
|
var (
|
|
|
|
_node = &Connector{config: cc.config}
|
|
|
|
_spec = &sqlgraph.CreateSpec{
|
|
|
|
Table: connector.Table,
|
|
|
|
ID: &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Column: connector.FieldID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if id, ok := cc.mutation.ID(); ok {
|
|
|
|
_node.ID = id
|
|
|
|
_spec.ID.Value = id
|
|
|
|
}
|
|
|
|
if value, ok := cc.mutation.GetType(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: connector.FieldType,
|
|
|
|
})
|
|
|
|
_node.Type = value
|
|
|
|
}
|
|
|
|
if value, ok := cc.mutation.Name(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: connector.FieldName,
|
|
|
|
})
|
|
|
|
_node.Name = value
|
|
|
|
}
|
|
|
|
if value, ok := cc.mutation.ResourceVersion(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: connector.FieldResourceVersion,
|
|
|
|
})
|
|
|
|
_node.ResourceVersion = value
|
|
|
|
}
|
|
|
|
if value, ok := cc.mutation.Config(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeBytes,
|
|
|
|
Value: value,
|
|
|
|
Column: connector.FieldConfig,
|
|
|
|
})
|
|
|
|
_node.Config = value
|
|
|
|
}
|
|
|
|
return _node, _spec
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectorCreateBulk is the builder for creating many Connector entities in bulk.
|
|
|
|
type ConnectorCreateBulk struct {
|
|
|
|
config
|
|
|
|
builders []*ConnectorCreate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save creates the Connector entities in the database.
|
|
|
|
func (ccb *ConnectorCreateBulk) Save(ctx context.Context) ([]*Connector, error) {
|
|
|
|
specs := make([]*sqlgraph.CreateSpec, len(ccb.builders))
|
|
|
|
nodes := make([]*Connector, len(ccb.builders))
|
|
|
|
mutators := make([]Mutator, len(ccb.builders))
|
|
|
|
for i := range ccb.builders {
|
|
|
|
func(i int, root context.Context) {
|
|
|
|
builder := ccb.builders[i]
|
|
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
|
|
mutation, ok := m.(*ConnectorMutation)
|
|
|
|
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, ccb.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, ccb.driver, spec); err != nil {
|
|
|
|
if sqlgraph.IsConstraintError(err) {
|
2022-08-29 07:16:12 +00:00
|
|
|
err = &ConstraintError{msg: err.Error(), wrap: 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
|
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, ccb.builders[0].mutation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
|
|
func (ccb *ConnectorCreateBulk) SaveX(ctx context.Context) []*Connector {
|
|
|
|
v, err := ccb.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2021-09-13 13:48:02 +00:00
|
|
|
|
|
|
|
// Exec executes the query.
|
|
|
|
func (ccb *ConnectorCreateBulk) Exec(ctx context.Context) error {
|
|
|
|
_, err := ccb.Save(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
|
|
func (ccb *ConnectorCreateBulk) ExecX(ctx context.Context) {
|
|
|
|
if err := ccb.Exec(ctx); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|