Files
upstream-ent/examples/migration/ent/sessiondevice_create.go
upstream-mirror 4b3ad0b611
Some checks failed
Atlas CI Public / lint (push) Failing after 13s
Atlas CI Public / sync (push) Has been skipped
snapshot of https://github.com/ent/ent.git (master, 2026-09-15; blobs > 131072B omitted)
2026-09-15 22:12:36 +05:00

323 lines
9.5 KiB
Go

// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
// Code generated by ent, DO NOT EDIT.
package ent
import (
"context"
"errors"
"fmt"
"time"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/examples/migration/ent/session"
"entgo.io/ent/examples/migration/ent/sessiondevice"
"entgo.io/ent/schema/field"
"github.com/google/uuid"
)
// SessionDeviceCreate is the builder for creating a SessionDevice entity.
type SessionDeviceCreate struct {
config
mutation *SessionDeviceMutation
hooks []Hook
}
// SetIPAddress sets the "ip_address" field.
func (_c *SessionDeviceCreate) SetIPAddress(v string) *SessionDeviceCreate {
_c.mutation.SetIPAddress(v)
return _c
}
// SetUserAgent sets the "user_agent" field.
func (_c *SessionDeviceCreate) SetUserAgent(v string) *SessionDeviceCreate {
_c.mutation.SetUserAgent(v)
return _c
}
// SetLocation sets the "location" field.
func (_c *SessionDeviceCreate) SetLocation(v string) *SessionDeviceCreate {
_c.mutation.SetLocation(v)
return _c
}
// SetCreatedAt sets the "created_at" field.
func (_c *SessionDeviceCreate) SetCreatedAt(v time.Time) *SessionDeviceCreate {
_c.mutation.SetCreatedAt(v)
return _c
}
// SetUpdatedAt sets the "updated_at" field.
func (_c *SessionDeviceCreate) SetUpdatedAt(v time.Time) *SessionDeviceCreate {
_c.mutation.SetUpdatedAt(v)
return _c
}
// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *SessionDeviceCreate) SetNillableUpdatedAt(v *time.Time) *SessionDeviceCreate {
if v != nil {
_c.SetUpdatedAt(*v)
}
return _c
}
// SetID sets the "id" field.
func (_c *SessionDeviceCreate) SetID(v uuid.UUID) *SessionDeviceCreate {
_c.mutation.SetID(v)
return _c
}
// SetNillableID sets the "id" field if the given value is not nil.
func (_c *SessionDeviceCreate) SetNillableID(v *uuid.UUID) *SessionDeviceCreate {
if v != nil {
_c.SetID(*v)
}
return _c
}
// AddSessionIDs adds the "sessions" edge to the Session entity by IDs.
func (_c *SessionDeviceCreate) AddSessionIDs(ids ...uuid.UUID) *SessionDeviceCreate {
_c.mutation.AddSessionIDs(ids...)
return _c
}
// AddSessions adds the "sessions" edges to the Session entity.
func (_c *SessionDeviceCreate) AddSessions(v ...*Session) *SessionDeviceCreate {
ids := make([]uuid.UUID, len(v))
for i := range v {
ids[i] = v[i].ID
}
return _c.AddSessionIDs(ids...)
}
// Mutation returns the SessionDeviceMutation object of the builder.
func (_c *SessionDeviceCreate) Mutation() *SessionDeviceMutation {
return _c.mutation
}
// Save creates the SessionDevice in the database.
func (_c *SessionDeviceCreate) Save(ctx context.Context) (*SessionDevice, error) {
_c.defaults()
return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}
// SaveX calls Save and panics if Save returns an error.
func (_c *SessionDeviceCreate) SaveX(ctx context.Context) *SessionDevice {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *SessionDeviceCreate) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *SessionDeviceCreate) 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 *SessionDeviceCreate) defaults() {
if _, ok := _c.mutation.ID(); !ok {
v := sessiondevice.DefaultID()
_c.mutation.SetID(v)
}
}
// check runs all checks and user-defined validators on the builder.
func (_c *SessionDeviceCreate) check() error {
if _, ok := _c.mutation.IPAddress(); !ok {
return &ValidationError{Name: "ip_address", err: errors.New(`ent: missing required field "SessionDevice.ip_address"`)}
}
if v, ok := _c.mutation.IPAddress(); ok {
if err := sessiondevice.IPAddressValidator(v); err != nil {
return &ValidationError{Name: "ip_address", err: fmt.Errorf(`ent: validator failed for field "SessionDevice.ip_address": %w`, err)}
}
}
if _, ok := _c.mutation.UserAgent(); !ok {
return &ValidationError{Name: "user_agent", err: errors.New(`ent: missing required field "SessionDevice.user_agent"`)}
}
if v, ok := _c.mutation.UserAgent(); ok {
if err := sessiondevice.UserAgentValidator(v); err != nil {
return &ValidationError{Name: "user_agent", err: fmt.Errorf(`ent: validator failed for field "SessionDevice.user_agent": %w`, err)}
}
}
if _, ok := _c.mutation.Location(); !ok {
return &ValidationError{Name: "location", err: errors.New(`ent: missing required field "SessionDevice.location"`)}
}
if v, ok := _c.mutation.Location(); ok {
if err := sessiondevice.LocationValidator(v); err != nil {
return &ValidationError{Name: "location", err: fmt.Errorf(`ent: validator failed for field "SessionDevice.location": %w`, err)}
}
}
if _, ok := _c.mutation.CreatedAt(); !ok {
return &ValidationError{Name: "created_at", err: errors.New(`ent: missing required field "SessionDevice.created_at"`)}
}
return nil
}
func (_c *SessionDeviceCreate) sqlSave(ctx context.Context) (*SessionDevice, 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.(*uuid.UUID); ok {
_node.ID = *id
} else if err := _node.ID.Scan(_spec.ID.Value); err != nil {
return nil, err
}
}
_c.mutation.id = &_node.ID
_c.mutation.done = true
return _node, nil
}
func (_c *SessionDeviceCreate) createSpec() (*SessionDevice, *sqlgraph.CreateSpec) {
var (
_node = &SessionDevice{config: _c.config}
_spec = sqlgraph.NewCreateSpec(sessiondevice.Table, sqlgraph.NewFieldSpec(sessiondevice.FieldID, field.TypeUUID))
)
if id, ok := _c.mutation.ID(); ok {
_node.ID = id
_spec.ID.Value = &id
}
if value, ok := _c.mutation.IPAddress(); ok {
_spec.SetField(sessiondevice.FieldIPAddress, field.TypeString, value)
_node.IPAddress = value
}
if value, ok := _c.mutation.UserAgent(); ok {
_spec.SetField(sessiondevice.FieldUserAgent, field.TypeString, value)
_node.UserAgent = value
}
if value, ok := _c.mutation.Location(); ok {
_spec.SetField(sessiondevice.FieldLocation, field.TypeString, value)
_node.Location = value
}
if value, ok := _c.mutation.CreatedAt(); ok {
_spec.SetField(sessiondevice.FieldCreatedAt, field.TypeTime, value)
_node.CreatedAt = value
}
if value, ok := _c.mutation.UpdatedAt(); ok {
_spec.SetField(sessiondevice.FieldUpdatedAt, field.TypeTime, value)
_node.UpdatedAt = value
}
if nodes := _c.mutation.SessionsIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.O2M,
Inverse: false,
Table: sessiondevice.SessionsTable,
Columns: []string{sessiondevice.SessionsColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(session.FieldID, field.TypeUUID),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges = append(_spec.Edges, edge)
}
return _node, _spec
}
// SessionDeviceCreateBulk is the builder for creating many SessionDevice entities in bulk.
type SessionDeviceCreateBulk struct {
config
err error
builders []*SessionDeviceCreate
}
// Save creates the SessionDevice entities in the database.
func (_c *SessionDeviceCreateBulk) Save(ctx context.Context) ([]*SessionDevice, error) {
if _c.err != nil {
return nil, _c.err
}
specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
nodes := make([]*SessionDevice, 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.(*SessionDeviceMutation)
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 *SessionDeviceCreateBulk) SaveX(ctx context.Context) []*SessionDevice {
v, err := _c.Save(ctx)
if err != nil {
panic(err)
}
return v
}
// Exec executes the query.
func (_c *SessionDeviceCreateBulk) Exec(ctx context.Context) error {
_, err := _c.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_c *SessionDeviceCreateBulk) ExecX(ctx context.Context) {
if err := _c.Exec(ctx); err != nil {
panic(err)
}
}