Files

246 lines
7.1 KiB
Go
Raw Permalink Normal View History

2022-08-29 11:16:12 +04:00
// Code generated by ent, DO NOT EDIT.
2021-01-02 00:34:47 +04:00
package db
import (
"context"
"errors"
"fmt"
2021-03-24 10:37:51 +04:00
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/schema/field"
2021-01-02 00:34:47 +04:00
"github.com/dexidp/dex/storage/ent/db/offlinesession"
)
// OfflineSessionCreate is the builder for creating a OfflineSession entity.
type OfflineSessionCreate struct {
config
mutation *OfflineSessionMutation
hooks []Hook
}
// SetUserID sets the "user_id" field.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) SetUserID(v string) *OfflineSessionCreate {
_c.mutation.SetUserID(v)
return _c
2021-01-02 00:34:47 +04:00
}
// SetConnID sets the "conn_id" field.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) SetConnID(v string) *OfflineSessionCreate {
_c.mutation.SetConnID(v)
return _c
2021-01-02 00:34:47 +04:00
}
// SetRefresh sets the "refresh" field.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) SetRefresh(v []byte) *OfflineSessionCreate {
_c.mutation.SetRefresh(v)
return _c
2021-01-02 00:34:47 +04:00
}
// SetConnectorData sets the "connector_data" field.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) SetConnectorData(v []byte) *OfflineSessionCreate {
_c.mutation.SetConnectorData(v)
return _c
2021-01-02 00:34:47 +04:00
}
// SetID sets the "id" field.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) SetID(v string) *OfflineSessionCreate {
_c.mutation.SetID(v)
return _c
2021-01-02 00:34:47 +04:00
}
// Mutation returns the OfflineSessionMutation object of the builder.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) Mutation() *OfflineSessionMutation {
return _c.mutation
2021-01-02 00:34:47 +04:00
}
// Save creates the OfflineSession in the database.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) Save(ctx context.Context) (*OfflineSession, error) {
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
2021-01-02 00:34:47 +04:00
}
// SaveX calls Save and panics if Save returns an error.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) SaveX(ctx context.Context) *OfflineSession {
v, err := _c.Save(ctx)
2021-01-02 00:34:47 +04:00
if err != nil {
panic(err)
}
return v
}
2021-09-13 17:48:02 +04:00
// Exec executes the query.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
2021-09-13 17:48:02 +04:00
return err
}
// ExecX is like Exec, but panics if an error occurs.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
2021-09-13 17:48:02 +04:00
panic(err)
}
}
2021-01-02 00:34:47 +04:00
// check runs all checks and user-defined validators on the builder.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) check() error {
if _, ok := _c.mutation.UserID(); !ok {
2022-03-07 10:15:01 +01:00
return &ValidationError{Name: "user_id", err: errors.New(`db: missing required field "OfflineSession.user_id"`)}
2021-01-02 00:34:47 +04:00
}
2025-08-18 12:53:54 +02:00
if v, ok := _c.mutation.UserID(); ok {
2021-01-02 00:34:47 +04:00
if err := offlinesession.UserIDValidator(v); err != nil {
2022-03-07 10:15:01 +01:00
return &ValidationError{Name: "user_id", err: fmt.Errorf(`db: validator failed for field "OfflineSession.user_id": %w`, err)}
2021-01-02 00:34:47 +04:00
}
}
2025-08-18 12:53:54 +02:00
if _, ok := _c.mutation.ConnID(); !ok {
2022-03-07 10:15:01 +01:00
return &ValidationError{Name: "conn_id", err: errors.New(`db: missing required field "OfflineSession.conn_id"`)}
2021-01-02 00:34:47 +04:00
}
2025-08-18 12:53:54 +02:00
if v, ok := _c.mutation.ConnID(); ok {
2021-01-02 00:34:47 +04:00
if err := offlinesession.ConnIDValidator(v); err != nil {
2022-03-07 10:15:01 +01:00
return &ValidationError{Name: "conn_id", err: fmt.Errorf(`db: validator failed for field "OfflineSession.conn_id": %w`, err)}
2021-01-02 00:34:47 +04:00
}
}
2025-08-18 12:53:54 +02:00
if _, ok := _c.mutation.Refresh(); !ok {
2022-03-07 10:15:01 +01:00
return &ValidationError{Name: "refresh", err: errors.New(`db: missing required field "OfflineSession.refresh"`)}
2021-01-02 00:34:47 +04:00
}
2025-08-18 12:53:54 +02:00
if v, ok := _c.mutation.ID(); ok {
2021-01-02 00:34:47 +04:00
if err := offlinesession.IDValidator(v); err != nil {
2022-03-07 10:15:01 +01:00
return &ValidationError{Name: "id", err: fmt.Errorf(`db: validator failed for field "OfflineSession.id": %w`, err)}
2021-01-02 00:34:47 +04:00
}
}
return nil
}
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) sqlSave(ctx context.Context) (*OfflineSession, error) {
if err := _c.check(); err != nil {
2023-02-26 01:32:52 +01:00
return nil, err
}
2025-08-18 12:53:54 +02:00
_node, _spec := _c.createSpec()
if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
2021-09-13 17:48:02 +04:00
if sqlgraph.IsConstraintError(err) {
2022-08-29 11:16:12 +04:00
err = &ConstraintError{msg: err.Error(), wrap: err}
2021-01-02 00:34:47 +04:00
}
return nil, err
}
2022-03-07 10:15:01 +01:00
if _spec.ID.Value != nil {
if id, ok := _spec.ID.Value.(string); ok {
_node.ID = id
} else {
return nil, fmt.Errorf("unexpected OfflineSession.ID type: %T", _spec.ID.Value)
}
}
2025-08-18 12:53:54 +02:00
_c.mutation.id = &_node.ID
_c.mutation.done = true
2021-01-02 00:34:47 +04:00
return _node, nil
}
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreate) createSpec() (*OfflineSession, *sqlgraph.CreateSpec) {
2021-01-02 00:34:47 +04:00
var (
2025-08-18 12:53:54 +02:00
_node = &OfflineSession{config: _c.config}
2023-02-26 01:32:52 +01:00
_spec = sqlgraph.NewCreateSpec(offlinesession.Table, sqlgraph.NewFieldSpec(offlinesession.FieldID, field.TypeString))
2021-01-02 00:34:47 +04:00
)
2025-08-18 12:53:54 +02:00
if id, ok := _c.mutation.ID(); ok {
2021-01-02 00:34:47 +04:00
_node.ID = id
_spec.ID.Value = id
}
2025-08-18 12:53:54 +02:00
if value, ok := _c.mutation.UserID(); ok {
2023-02-26 01:32:52 +01:00
_spec.SetField(offlinesession.FieldUserID, field.TypeString, value)
2021-01-02 00:34:47 +04:00
_node.UserID = value
}
2025-08-18 12:53:54 +02:00
if value, ok := _c.mutation.ConnID(); ok {
2023-02-26 01:32:52 +01:00
_spec.SetField(offlinesession.FieldConnID, field.TypeString, value)
2021-01-02 00:34:47 +04:00
_node.ConnID = value
}
2025-08-18 12:53:54 +02:00
if value, ok := _c.mutation.Refresh(); ok {
2023-02-26 01:32:52 +01:00
_spec.SetField(offlinesession.FieldRefresh, field.TypeBytes, value)
2021-01-02 00:34:47 +04:00
_node.Refresh = value
}
2025-08-18 12:53:54 +02:00
if value, ok := _c.mutation.ConnectorData(); ok {
2023-02-26 01:32:52 +01:00
_spec.SetField(offlinesession.FieldConnectorData, field.TypeBytes, value)
2021-01-02 00:34:47 +04:00
_node.ConnectorData = &value
}
return _node, _spec
}
// OfflineSessionCreateBulk is the builder for creating many OfflineSession entities in bulk.
type OfflineSessionCreateBulk struct {
config
err error
2021-01-02 00:34:47 +04:00
builders []*OfflineSessionCreate
}
// Save creates the OfflineSession entities in the database.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreateBulk) Save(ctx context.Context) ([]*OfflineSession, error) {
if _c.err != nil {
return nil, _c.err
}
2025-08-18 12:53:54 +02:00
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*OfflineSession, len(_c.builders))
mutators := make([]Mutator, len(_c.builders))
for i := range _c.builders {
2021-01-02 00:34:47 +04:00
func(i int, root context.Context) {
2025-08-18 12:53:54 +02:00
builder := _c.builders[i]
2021-01-02 00:34:47 +04:00
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
mutation, ok := m.(*OfflineSessionMutation)
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
2023-05-12 12:34:56 -07:00
nodes[i], specs[i] = builder.createSpec()
2021-01-02 00:34:47 +04:00
if i < len(mutators)-1 {
2025-08-18 12:53:54 +02:00
_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
2021-01-02 00:34:47 +04:00
} else {
2021-09-13 17:48:02 +04:00
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
2021-01-02 00:34:47 +04:00
// Invoke the actual operation on the latest mutation in the chain.
2025-08-18 12:53:54 +02:00
if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
2021-09-13 17:48:02 +04:00
if sqlgraph.IsConstraintError(err) {
2022-08-29 11:16:12 +04:00
err = &ConstraintError{msg: err.Error(), wrap: err}
2021-01-02 00:34:47 +04:00
}
}
}
if err != nil {
return nil, err
}
2021-09-13 17:48:02 +04:00
mutation.id = &nodes[i].ID
mutation.done = true
2021-01-02 00:34:47 +04: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 {
2025-08-18 12:53:54 +02:00
if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
2021-01-02 00:34:47 +04:00
return nil, err
}
}
return nodes, nil
}
// SaveX is like Save, but panics if an error occurs.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreateBulk) SaveX(ctx context.Context) []*OfflineSession {
v, err := _c.Save(ctx)
2021-01-02 00:34:47 +04:00
if err != nil {
panic(err)
}
return v
}
2021-09-13 17:48:02 +04:00
// Exec executes the query.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
2021-09-13 17:48:02 +04:00
return err
}
// ExecX is like Exec, but panics if an error occurs.
2025-08-18 12:53:54 +02:00
func (_c *OfflineSessionCreateBulk) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
2021-09-13 17:48:02 +04:00
panic(err)
}
}