Files
ent/entc/integration/customid/ent/session_update.go
2023-03-09 22:08:18 +02:00

290 lines
8.4 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"
"entgo.io/ent/dialect/sql"
"entgo.io/ent/dialect/sql/sqlgraph"
"entgo.io/ent/entc/integration/customid/ent/device"
"entgo.io/ent/entc/integration/customid/ent/predicate"
"entgo.io/ent/entc/integration/customid/ent/schema"
"entgo.io/ent/entc/integration/customid/ent/session"
"entgo.io/ent/schema/field"
)
// SessionUpdate is the builder for updating Session entities.
type SessionUpdate struct {
config
hooks []Hook
mutation *SessionMutation
}
// Where appends a list predicates to the SessionUpdate builder.
func (su *SessionUpdate) Where(ps ...predicate.Session) *SessionUpdate {
su.mutation.Where(ps...)
return su
}
// SetDeviceID sets the "device" edge to the Device entity by ID.
func (su *SessionUpdate) SetDeviceID(id schema.ID) *SessionUpdate {
su.mutation.SetDeviceID(id)
return su
}
// SetNillableDeviceID sets the "device" edge to the Device entity by ID if the given value is not nil.
func (su *SessionUpdate) SetNillableDeviceID(id *schema.ID) *SessionUpdate {
if id != nil {
su = su.SetDeviceID(*id)
}
return su
}
// SetDevice sets the "device" edge to the Device entity.
func (su *SessionUpdate) SetDevice(d *Device) *SessionUpdate {
return su.SetDeviceID(d.ID)
}
// Mutation returns the SessionMutation object of the builder.
func (su *SessionUpdate) Mutation() *SessionMutation {
return su.mutation
}
// ClearDevice clears the "device" edge to the Device entity.
func (su *SessionUpdate) ClearDevice() *SessionUpdate {
su.mutation.ClearDevice()
return su
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (su *SessionUpdate) Save(ctx context.Context) (int, error) {
return withHooks[int, SessionMutation](ctx, su.sqlSave, su.mutation, su.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (su *SessionUpdate) SaveX(ctx context.Context) int {
affected, err := su.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (su *SessionUpdate) Exec(ctx context.Context) error {
_, err := su.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (su *SessionUpdate) ExecX(ctx context.Context) {
if err := su.Exec(ctx); err != nil {
panic(err)
}
}
func (su *SessionUpdate) sqlSave(ctx context.Context) (n int, err error) {
_spec := sqlgraph.NewUpdateSpec(session.Table, session.Columns, sqlgraph.NewFieldSpec(session.FieldID, field.TypeBytes))
if ps := su.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if su.mutation.DeviceCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: session.DeviceTable,
Columns: []string{session.DeviceColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(device.FieldID, field.TypeBytes),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := su.mutation.DeviceIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: session.DeviceTable,
Columns: []string{session.DeviceColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(device.FieldID, field.TypeBytes),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if n, err = sqlgraph.UpdateNodes(ctx, su.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{session.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
su.mutation.done = true
return n, nil
}
// SessionUpdateOne is the builder for updating a single Session entity.
type SessionUpdateOne struct {
config
fields []string
hooks []Hook
mutation *SessionMutation
}
// SetDeviceID sets the "device" edge to the Device entity by ID.
func (suo *SessionUpdateOne) SetDeviceID(id schema.ID) *SessionUpdateOne {
suo.mutation.SetDeviceID(id)
return suo
}
// SetNillableDeviceID sets the "device" edge to the Device entity by ID if the given value is not nil.
func (suo *SessionUpdateOne) SetNillableDeviceID(id *schema.ID) *SessionUpdateOne {
if id != nil {
suo = suo.SetDeviceID(*id)
}
return suo
}
// SetDevice sets the "device" edge to the Device entity.
func (suo *SessionUpdateOne) SetDevice(d *Device) *SessionUpdateOne {
return suo.SetDeviceID(d.ID)
}
// Mutation returns the SessionMutation object of the builder.
func (suo *SessionUpdateOne) Mutation() *SessionMutation {
return suo.mutation
}
// ClearDevice clears the "device" edge to the Device entity.
func (suo *SessionUpdateOne) ClearDevice() *SessionUpdateOne {
suo.mutation.ClearDevice()
return suo
}
// Where appends a list predicates to the SessionUpdate builder.
func (suo *SessionUpdateOne) Where(ps ...predicate.Session) *SessionUpdateOne {
suo.mutation.Where(ps...)
return suo
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (suo *SessionUpdateOne) Select(field string, fields ...string) *SessionUpdateOne {
suo.fields = append([]string{field}, fields...)
return suo
}
// Save executes the query and returns the updated Session entity.
func (suo *SessionUpdateOne) Save(ctx context.Context) (*Session, error) {
return withHooks[*Session, SessionMutation](ctx, suo.sqlSave, suo.mutation, suo.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (suo *SessionUpdateOne) SaveX(ctx context.Context) *Session {
node, err := suo.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (suo *SessionUpdateOne) Exec(ctx context.Context) error {
_, err := suo.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (suo *SessionUpdateOne) ExecX(ctx context.Context) {
if err := suo.Exec(ctx); err != nil {
panic(err)
}
}
func (suo *SessionUpdateOne) sqlSave(ctx context.Context) (_node *Session, err error) {
_spec := sqlgraph.NewUpdateSpec(session.Table, session.Columns, sqlgraph.NewFieldSpec(session.FieldID, field.TypeBytes))
id, ok := suo.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Session.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := suo.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, session.FieldID)
for _, f := range fields {
if !session.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != session.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := suo.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if suo.mutation.DeviceCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: session.DeviceTable,
Columns: []string{session.DeviceColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(device.FieldID, field.TypeBytes),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := suo.mutation.DeviceIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: session.DeviceTable,
Columns: []string{session.DeviceColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(device.FieldID, field.TypeBytes),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_node = &Session{config: suo.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, suo.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{session.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
suo.mutation.done = true
return _node, nil
}