2021-01-02 02:04:47 +05:30
|
|
|
// Code generated by entc, DO NOT EDIT.
|
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-03-24 12:07:51 +05:30
|
|
|
"entgo.io/ent/dialect/sql/sqlgraph"
|
|
|
|
"entgo.io/ent/schema/field"
|
2021-01-02 02:04:47 +05:30
|
|
|
"github.com/dexidp/dex/storage/ent/db/devicetoken"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DeviceTokenCreate is the builder for creating a DeviceToken entity.
|
|
|
|
type DeviceTokenCreate struct {
|
|
|
|
config
|
|
|
|
mutation *DeviceTokenMutation
|
|
|
|
hooks []Hook
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDeviceCode sets the "device_code" field.
|
|
|
|
func (dtc *DeviceTokenCreate) SetDeviceCode(s string) *DeviceTokenCreate {
|
|
|
|
dtc.mutation.SetDeviceCode(s)
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetStatus sets the "status" field.
|
|
|
|
func (dtc *DeviceTokenCreate) SetStatus(s string) *DeviceTokenCreate {
|
|
|
|
dtc.mutation.SetStatus(s)
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetToken sets the "token" field.
|
|
|
|
func (dtc *DeviceTokenCreate) SetToken(b []byte) *DeviceTokenCreate {
|
|
|
|
dtc.mutation.SetToken(b)
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetExpiry sets the "expiry" field.
|
|
|
|
func (dtc *DeviceTokenCreate) SetExpiry(t time.Time) *DeviceTokenCreate {
|
|
|
|
dtc.mutation.SetExpiry(t)
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLastRequest sets the "last_request" field.
|
|
|
|
func (dtc *DeviceTokenCreate) SetLastRequest(t time.Time) *DeviceTokenCreate {
|
|
|
|
dtc.mutation.SetLastRequest(t)
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPollInterval sets the "poll_interval" field.
|
|
|
|
func (dtc *DeviceTokenCreate) SetPollInterval(i int) *DeviceTokenCreate {
|
|
|
|
dtc.mutation.SetPollInterval(i)
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
2022-07-27 21:32:18 +05:30
|
|
|
// SetCodeChallenge sets the "code_challenge" field.
|
|
|
|
func (dtc *DeviceTokenCreate) SetCodeChallenge(s string) *DeviceTokenCreate {
|
|
|
|
dtc.mutation.SetCodeChallenge(s)
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillableCodeChallenge sets the "code_challenge" field if the given value is not nil.
|
|
|
|
func (dtc *DeviceTokenCreate) SetNillableCodeChallenge(s *string) *DeviceTokenCreate {
|
|
|
|
if s != nil {
|
|
|
|
dtc.SetCodeChallenge(*s)
|
|
|
|
}
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCodeChallengeMethod sets the "code_challenge_method" field.
|
|
|
|
func (dtc *DeviceTokenCreate) SetCodeChallengeMethod(s string) *DeviceTokenCreate {
|
|
|
|
dtc.mutation.SetCodeChallengeMethod(s)
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetNillableCodeChallengeMethod sets the "code_challenge_method" field if the given value is not nil.
|
|
|
|
func (dtc *DeviceTokenCreate) SetNillableCodeChallengeMethod(s *string) *DeviceTokenCreate {
|
|
|
|
if s != nil {
|
|
|
|
dtc.SetCodeChallengeMethod(*s)
|
|
|
|
}
|
|
|
|
return dtc
|
|
|
|
}
|
|
|
|
|
2021-01-02 02:04:47 +05:30
|
|
|
// Mutation returns the DeviceTokenMutation object of the builder.
|
|
|
|
func (dtc *DeviceTokenCreate) Mutation() *DeviceTokenMutation {
|
|
|
|
return dtc.mutation
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save creates the DeviceToken in the database.
|
|
|
|
func (dtc *DeviceTokenCreate) Save(ctx context.Context) (*DeviceToken, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
node *DeviceToken
|
|
|
|
)
|
2022-07-27 21:32:18 +05:30
|
|
|
dtc.defaults()
|
2021-01-02 02:04:47 +05:30
|
|
|
if len(dtc.hooks) == 0 {
|
|
|
|
if err = dtc.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
node, err = dtc.sqlSave(ctx)
|
|
|
|
} else {
|
|
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
|
|
mutation, ok := m.(*DeviceTokenMutation)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
|
|
}
|
|
|
|
if err = dtc.check(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dtc.mutation = mutation
|
2021-09-13 19:18:02 +05:30
|
|
|
if node, err = dtc.sqlSave(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mutation.id = &node.ID
|
2021-01-02 02:04:47 +05:30
|
|
|
mutation.done = true
|
|
|
|
return node, err
|
|
|
|
})
|
|
|
|
for i := len(dtc.hooks) - 1; i >= 0; i-- {
|
2021-09-13 19:18:02 +05:30
|
|
|
if dtc.hooks[i] == nil {
|
|
|
|
return nil, fmt.Errorf("db: uninitialized hook (forgotten import db/runtime?)")
|
|
|
|
}
|
2021-01-02 02:04:47 +05:30
|
|
|
mut = dtc.hooks[i](mut)
|
|
|
|
}
|
|
|
|
if _, err := mut.Mutate(ctx, dtc.mutation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveX calls Save and panics if Save returns an error.
|
|
|
|
func (dtc *DeviceTokenCreate) SaveX(ctx context.Context) *DeviceToken {
|
|
|
|
v, err := dtc.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2021-09-13 19:18:02 +05:30
|
|
|
// Exec executes the query.
|
|
|
|
func (dtc *DeviceTokenCreate) Exec(ctx context.Context) error {
|
|
|
|
_, err := dtc.Save(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
|
|
func (dtc *DeviceTokenCreate) ExecX(ctx context.Context) {
|
|
|
|
if err := dtc.Exec(ctx); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-27 21:32:18 +05:30
|
|
|
// defaults sets the default values of the builder before save.
|
|
|
|
func (dtc *DeviceTokenCreate) defaults() {
|
|
|
|
if _, ok := dtc.mutation.CodeChallenge(); !ok {
|
|
|
|
v := devicetoken.DefaultCodeChallenge
|
|
|
|
dtc.mutation.SetCodeChallenge(v)
|
|
|
|
}
|
|
|
|
if _, ok := dtc.mutation.CodeChallengeMethod(); !ok {
|
|
|
|
v := devicetoken.DefaultCodeChallengeMethod
|
|
|
|
dtc.mutation.SetCodeChallengeMethod(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 02:04:47 +05:30
|
|
|
// check runs all checks and user-defined validators on the builder.
|
|
|
|
func (dtc *DeviceTokenCreate) check() error {
|
|
|
|
if _, ok := dtc.mutation.DeviceCode(); !ok {
|
2022-03-07 14:45:01 +05:30
|
|
|
return &ValidationError{Name: "device_code", err: errors.New(`db: missing required field "DeviceToken.device_code"`)}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
|
|
|
if v, ok := dtc.mutation.DeviceCode(); ok {
|
|
|
|
if err := devicetoken.DeviceCodeValidator(v); err != nil {
|
2022-03-07 14:45:01 +05:30
|
|
|
return &ValidationError{Name: "device_code", err: fmt.Errorf(`db: validator failed for field "DeviceToken.device_code": %w`, err)}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := dtc.mutation.Status(); !ok {
|
2022-03-07 14:45:01 +05:30
|
|
|
return &ValidationError{Name: "status", err: errors.New(`db: missing required field "DeviceToken.status"`)}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
|
|
|
if v, ok := dtc.mutation.Status(); ok {
|
|
|
|
if err := devicetoken.StatusValidator(v); err != nil {
|
2022-03-07 14:45:01 +05:30
|
|
|
return &ValidationError{Name: "status", err: fmt.Errorf(`db: validator failed for field "DeviceToken.status": %w`, err)}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, ok := dtc.mutation.Expiry(); !ok {
|
2022-03-07 14:45:01 +05:30
|
|
|
return &ValidationError{Name: "expiry", err: errors.New(`db: missing required field "DeviceToken.expiry"`)}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
|
|
|
if _, ok := dtc.mutation.LastRequest(); !ok {
|
2022-03-07 14:45:01 +05:30
|
|
|
return &ValidationError{Name: "last_request", err: errors.New(`db: missing required field "DeviceToken.last_request"`)}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
|
|
|
if _, ok := dtc.mutation.PollInterval(); !ok {
|
2022-03-07 14:45:01 +05:30
|
|
|
return &ValidationError{Name: "poll_interval", err: errors.New(`db: missing required field "DeviceToken.poll_interval"`)}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
2022-07-27 21:32:18 +05:30
|
|
|
if _, ok := dtc.mutation.CodeChallenge(); !ok {
|
|
|
|
return &ValidationError{Name: "code_challenge", err: errors.New(`db: missing required field "DeviceToken.code_challenge"`)}
|
|
|
|
}
|
|
|
|
if _, ok := dtc.mutation.CodeChallengeMethod(); !ok {
|
|
|
|
return &ValidationError{Name: "code_challenge_method", err: errors.New(`db: missing required field "DeviceToken.code_challenge_method"`)}
|
|
|
|
}
|
2021-01-02 02:04:47 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dtc *DeviceTokenCreate) sqlSave(ctx context.Context) (*DeviceToken, error) {
|
|
|
|
_node, _spec := dtc.createSpec()
|
|
|
|
if err := sqlgraph.CreateNode(ctx, dtc.driver, _spec); err != nil {
|
2021-09-13 19:18:02 +05:30
|
|
|
if sqlgraph.IsConstraintError(err) {
|
|
|
|
err = &ConstraintError{err.Error(), err}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id := _spec.ID.Value.(int64)
|
|
|
|
_node.ID = int(id)
|
|
|
|
return _node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dtc *DeviceTokenCreate) createSpec() (*DeviceToken, *sqlgraph.CreateSpec) {
|
|
|
|
var (
|
|
|
|
_node = &DeviceToken{config: dtc.config}
|
|
|
|
_spec = &sqlgraph.CreateSpec{
|
|
|
|
Table: devicetoken.Table,
|
|
|
|
ID: &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeInt,
|
|
|
|
Column: devicetoken.FieldID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if value, ok := dtc.mutation.DeviceCode(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: devicetoken.FieldDeviceCode,
|
|
|
|
})
|
|
|
|
_node.DeviceCode = value
|
|
|
|
}
|
|
|
|
if value, ok := dtc.mutation.Status(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: devicetoken.FieldStatus,
|
|
|
|
})
|
|
|
|
_node.Status = value
|
|
|
|
}
|
|
|
|
if value, ok := dtc.mutation.Token(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeBytes,
|
|
|
|
Value: value,
|
|
|
|
Column: devicetoken.FieldToken,
|
|
|
|
})
|
|
|
|
_node.Token = &value
|
|
|
|
}
|
|
|
|
if value, ok := dtc.mutation.Expiry(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeTime,
|
|
|
|
Value: value,
|
|
|
|
Column: devicetoken.FieldExpiry,
|
|
|
|
})
|
|
|
|
_node.Expiry = value
|
|
|
|
}
|
|
|
|
if value, ok := dtc.mutation.LastRequest(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeTime,
|
|
|
|
Value: value,
|
|
|
|
Column: devicetoken.FieldLastRequest,
|
|
|
|
})
|
|
|
|
_node.LastRequest = value
|
|
|
|
}
|
|
|
|
if value, ok := dtc.mutation.PollInterval(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeInt,
|
|
|
|
Value: value,
|
|
|
|
Column: devicetoken.FieldPollInterval,
|
|
|
|
})
|
|
|
|
_node.PollInterval = value
|
|
|
|
}
|
2022-07-27 21:32:18 +05:30
|
|
|
if value, ok := dtc.mutation.CodeChallenge(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: devicetoken.FieldCodeChallenge,
|
|
|
|
})
|
|
|
|
_node.CodeChallenge = value
|
|
|
|
}
|
|
|
|
if value, ok := dtc.mutation.CodeChallengeMethod(); ok {
|
|
|
|
_spec.Fields = append(_spec.Fields, &sqlgraph.FieldSpec{
|
|
|
|
Type: field.TypeString,
|
|
|
|
Value: value,
|
|
|
|
Column: devicetoken.FieldCodeChallengeMethod,
|
|
|
|
})
|
|
|
|
_node.CodeChallengeMethod = value
|
|
|
|
}
|
2021-01-02 02:04:47 +05:30
|
|
|
return _node, _spec
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeviceTokenCreateBulk is the builder for creating many DeviceToken entities in bulk.
|
|
|
|
type DeviceTokenCreateBulk struct {
|
|
|
|
config
|
|
|
|
builders []*DeviceTokenCreate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save creates the DeviceToken entities in the database.
|
|
|
|
func (dtcb *DeviceTokenCreateBulk) Save(ctx context.Context) ([]*DeviceToken, error) {
|
|
|
|
specs := make([]*sqlgraph.CreateSpec, len(dtcb.builders))
|
|
|
|
nodes := make([]*DeviceToken, len(dtcb.builders))
|
|
|
|
mutators := make([]Mutator, len(dtcb.builders))
|
|
|
|
for i := range dtcb.builders {
|
|
|
|
func(i int, root context.Context) {
|
|
|
|
builder := dtcb.builders[i]
|
2022-07-27 21:32:18 +05:30
|
|
|
builder.defaults()
|
2021-01-02 02:04:47 +05:30
|
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
|
|
mutation, ok := m.(*DeviceTokenMutation)
|
|
|
|
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, dtcb.builders[i+1].mutation)
|
|
|
|
} else {
|
2021-09-13 19:18:02 +05:30
|
|
|
spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
|
2021-01-02 02:04:47 +05:30
|
|
|
// Invoke the actual operation on the latest mutation in the chain.
|
2021-09-13 19:18:02 +05:30
|
|
|
if err = sqlgraph.BatchCreate(ctx, dtcb.driver, spec); err != nil {
|
|
|
|
if sqlgraph.IsConstraintError(err) {
|
|
|
|
err = &ConstraintError{err.Error(), err}
|
2021-01-02 02:04:47 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-13 19:18:02 +05:30
|
|
|
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-02 02:04:47 +05:30
|
|
|
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, dtcb.builders[0].mutation); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nodes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveX is like Save, but panics if an error occurs.
|
|
|
|
func (dtcb *DeviceTokenCreateBulk) SaveX(ctx context.Context) []*DeviceToken {
|
|
|
|
v, err := dtcb.Save(ctx)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
2021-09-13 19:18:02 +05:30
|
|
|
|
|
|
|
// Exec executes the query.
|
|
|
|
func (dtcb *DeviceTokenCreateBulk) Exec(ctx context.Context) error {
|
|
|
|
_, err := dtcb.Save(ctx)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecX is like Exec, but panics if an error occurs.
|
|
|
|
func (dtcb *DeviceTokenCreateBulk) ExecX(ctx context.Context) {
|
|
|
|
if err := dtcb.Exec(ctx); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|