// Code generated by ent, DO NOT EDIT. package db import ( "context" "errors" "fmt" "time" "entgo.io/ent/dialect/sql/sqlgraph" "entgo.io/ent/schema/field" "github.com/dexidp/dex/storage/ent/db/authsession" ) // AuthSessionCreate is the builder for creating a AuthSession entity. type AuthSessionCreate struct { config mutation *AuthSessionMutation hooks []Hook } // SetUserID sets the "user_id" field. func (_c *AuthSessionCreate) SetUserID(v string) *AuthSessionCreate { _c.mutation.SetUserID(v) return _c } // SetConnectorID sets the "connector_id" field. func (_c *AuthSessionCreate) SetConnectorID(v string) *AuthSessionCreate { _c.mutation.SetConnectorID(v) return _c } // SetNonce sets the "nonce" field. func (_c *AuthSessionCreate) SetNonce(v string) *AuthSessionCreate { _c.mutation.SetNonce(v) return _c } // SetClientStates sets the "client_states" field. func (_c *AuthSessionCreate) SetClientStates(v []byte) *AuthSessionCreate { _c.mutation.SetClientStates(v) return _c } // SetCreatedAt sets the "created_at" field. func (_c *AuthSessionCreate) SetCreatedAt(v time.Time) *AuthSessionCreate { _c.mutation.SetCreatedAt(v) return _c } // SetLastActivity sets the "last_activity" field. func (_c *AuthSessionCreate) SetLastActivity(v time.Time) *AuthSessionCreate { _c.mutation.SetLastActivity(v) return _c } // SetIPAddress sets the "ip_address" field. func (_c *AuthSessionCreate) SetIPAddress(v string) *AuthSessionCreate { _c.mutation.SetIPAddress(v) return _c } // SetNillableIPAddress sets the "ip_address" field if the given value is not nil. func (_c *AuthSessionCreate) SetNillableIPAddress(v *string) *AuthSessionCreate { if v != nil { _c.SetIPAddress(*v) } return _c } // SetUserAgent sets the "user_agent" field. func (_c *AuthSessionCreate) SetUserAgent(v string) *AuthSessionCreate { _c.mutation.SetUserAgent(v) return _c } // SetNillableUserAgent sets the "user_agent" field if the given value is not nil. func (_c *AuthSessionCreate) SetNillableUserAgent(v *string) *AuthSessionCreate { if v != nil { _c.SetUserAgent(*v) } return _c } // SetAbsoluteExpiry sets the "absolute_expiry" field. func (_c *AuthSessionCreate) SetAbsoluteExpiry(v time.Time) *AuthSessionCreate { _c.mutation.SetAbsoluteExpiry(v) return _c } // SetIdleExpiry sets the "idle_expiry" field. func (_c *AuthSessionCreate) SetIdleExpiry(v time.Time) *AuthSessionCreate { _c.mutation.SetIdleExpiry(v) return _c } // SetConnectorData sets the "connector_data" field. func (_c *AuthSessionCreate) SetConnectorData(v []byte) *AuthSessionCreate { _c.mutation.SetConnectorData(v) return _c } // SetID sets the "id" field. func (_c *AuthSessionCreate) SetID(v string) *AuthSessionCreate { _c.mutation.SetID(v) return _c } // Mutation returns the AuthSessionMutation object of the builder. func (_c *AuthSessionCreate) Mutation() *AuthSessionMutation { return _c.mutation } // Save creates the AuthSession in the database. func (_c *AuthSessionCreate) Save(ctx context.Context) (*AuthSession, error) { _c.defaults() return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks) } // SaveX calls Save and panics if Save returns an error. func (_c *AuthSessionCreate) SaveX(ctx context.Context) *AuthSession { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *AuthSessionCreate) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *AuthSessionCreate) 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 *AuthSessionCreate) defaults() { if _, ok := _c.mutation.IPAddress(); !ok { v := authsession.DefaultIPAddress _c.mutation.SetIPAddress(v) } if _, ok := _c.mutation.UserAgent(); !ok { v := authsession.DefaultUserAgent _c.mutation.SetUserAgent(v) } } // check runs all checks and user-defined validators on the builder. func (_c *AuthSessionCreate) check() error { if _, ok := _c.mutation.UserID(); !ok { return &ValidationError{Name: "user_id", err: errors.New(`db: missing required field "AuthSession.user_id"`)} } if v, ok := _c.mutation.UserID(); ok { if err := authsession.UserIDValidator(v); err != nil { return &ValidationError{Name: "user_id", err: fmt.Errorf(`db: validator failed for field "AuthSession.user_id": %w`, err)} } } if _, ok := _c.mutation.ConnectorID(); !ok { return &ValidationError{Name: "connector_id", err: errors.New(`db: missing required field "AuthSession.connector_id"`)} } if v, ok := _c.mutation.ConnectorID(); ok { if err := authsession.ConnectorIDValidator(v); err != nil { return &ValidationError{Name: "connector_id", err: fmt.Errorf(`db: validator failed for field "AuthSession.connector_id": %w`, err)} } } if _, ok := _c.mutation.Nonce(); !ok { return &ValidationError{Name: "nonce", err: errors.New(`db: missing required field "AuthSession.nonce"`)} } if v, ok := _c.mutation.Nonce(); ok { if err := authsession.NonceValidator(v); err != nil { return &ValidationError{Name: "nonce", err: fmt.Errorf(`db: validator failed for field "AuthSession.nonce": %w`, err)} } } if _, ok := _c.mutation.ClientStates(); !ok { return &ValidationError{Name: "client_states", err: errors.New(`db: missing required field "AuthSession.client_states"`)} } if _, ok := _c.mutation.CreatedAt(); !ok { return &ValidationError{Name: "created_at", err: errors.New(`db: missing required field "AuthSession.created_at"`)} } if _, ok := _c.mutation.LastActivity(); !ok { return &ValidationError{Name: "last_activity", err: errors.New(`db: missing required field "AuthSession.last_activity"`)} } if _, ok := _c.mutation.IPAddress(); !ok { return &ValidationError{Name: "ip_address", err: errors.New(`db: missing required field "AuthSession.ip_address"`)} } if _, ok := _c.mutation.UserAgent(); !ok { return &ValidationError{Name: "user_agent", err: errors.New(`db: missing required field "AuthSession.user_agent"`)} } if _, ok := _c.mutation.AbsoluteExpiry(); !ok { return &ValidationError{Name: "absolute_expiry", err: errors.New(`db: missing required field "AuthSession.absolute_expiry"`)} } if _, ok := _c.mutation.IdleExpiry(); !ok { return &ValidationError{Name: "idle_expiry", err: errors.New(`db: missing required field "AuthSession.idle_expiry"`)} } if v, ok := _c.mutation.ID(); ok { if err := authsession.IDValidator(v); err != nil { return &ValidationError{Name: "id", err: fmt.Errorf(`db: validator failed for field "AuthSession.id": %w`, err)} } } return nil } func (_c *AuthSessionCreate) sqlSave(ctx context.Context) (*AuthSession, 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 } if _spec.ID.Value != nil { if id, ok := _spec.ID.Value.(string); ok { _node.ID = id } else { return nil, fmt.Errorf("unexpected AuthSession.ID type: %T", _spec.ID.Value) } } _c.mutation.id = &_node.ID _c.mutation.done = true return _node, nil } func (_c *AuthSessionCreate) createSpec() (*AuthSession, *sqlgraph.CreateSpec) { var ( _node = &AuthSession{config: _c.config} _spec = sqlgraph.NewCreateSpec(authsession.Table, sqlgraph.NewFieldSpec(authsession.FieldID, field.TypeString)) ) if id, ok := _c.mutation.ID(); ok { _node.ID = id _spec.ID.Value = id } if value, ok := _c.mutation.UserID(); ok { _spec.SetField(authsession.FieldUserID, field.TypeString, value) _node.UserID = value } if value, ok := _c.mutation.ConnectorID(); ok { _spec.SetField(authsession.FieldConnectorID, field.TypeString, value) _node.ConnectorID = value } if value, ok := _c.mutation.Nonce(); ok { _spec.SetField(authsession.FieldNonce, field.TypeString, value) _node.Nonce = value } if value, ok := _c.mutation.ClientStates(); ok { _spec.SetField(authsession.FieldClientStates, field.TypeBytes, value) _node.ClientStates = value } if value, ok := _c.mutation.CreatedAt(); ok { _spec.SetField(authsession.FieldCreatedAt, field.TypeTime, value) _node.CreatedAt = value } if value, ok := _c.mutation.LastActivity(); ok { _spec.SetField(authsession.FieldLastActivity, field.TypeTime, value) _node.LastActivity = value } if value, ok := _c.mutation.IPAddress(); ok { _spec.SetField(authsession.FieldIPAddress, field.TypeString, value) _node.IPAddress = value } if value, ok := _c.mutation.UserAgent(); ok { _spec.SetField(authsession.FieldUserAgent, field.TypeString, value) _node.UserAgent = value } if value, ok := _c.mutation.AbsoluteExpiry(); ok { _spec.SetField(authsession.FieldAbsoluteExpiry, field.TypeTime, value) _node.AbsoluteExpiry = value } if value, ok := _c.mutation.IdleExpiry(); ok { _spec.SetField(authsession.FieldIdleExpiry, field.TypeTime, value) _node.IdleExpiry = value } if value, ok := _c.mutation.ConnectorData(); ok { _spec.SetField(authsession.FieldConnectorData, field.TypeBytes, value) _node.ConnectorData = &value } return _node, _spec } // AuthSessionCreateBulk is the builder for creating many AuthSession entities in bulk. type AuthSessionCreateBulk struct { config err error builders []*AuthSessionCreate } // Save creates the AuthSession entities in the database. func (_c *AuthSessionCreateBulk) Save(ctx context.Context) ([]*AuthSession, error) { if _c.err != nil { return nil, _c.err } specs := make([]*sqlgraph.CreateSpec, len(_c.builders)) nodes := make([]*AuthSession, 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.(*AuthSessionMutation) 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 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 *AuthSessionCreateBulk) SaveX(ctx context.Context) []*AuthSession { v, err := _c.Save(ctx) if err != nil { panic(err) } return v } // Exec executes the query. func (_c *AuthSessionCreateBulk) Exec(ctx context.Context) error { _, err := _c.Save(ctx) return err } // ExecX is like Exec, but panics if an error occurs. func (_c *AuthSessionCreateBulk) ExecX(ctx context.Context) { if err := _c.Exec(ctx); err != nil { panic(err) } }