mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
* entc/gen: fixed ConstraintError fields name * fix: run go generate * entc/gen: fixed Filter fields name * fix: run go generate again for entql
372 lines
9.9 KiB
Go
372 lines
9.9 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) {
|
|
var (
|
|
err error
|
|
affected int
|
|
)
|
|
if len(su.hooks) == 0 {
|
|
affected, err = su.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*SessionMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
su.mutation = mutation
|
|
affected, err = su.sqlSave(ctx)
|
|
mutation.done = true
|
|
return affected, err
|
|
})
|
|
for i := len(su.hooks) - 1; i >= 0; i-- {
|
|
if su.hooks[i] == nil {
|
|
return 0, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
|
}
|
|
mut = su.hooks[i](mut)
|
|
}
|
|
if _, err := mut.Mutate(ctx, su.mutation); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return affected, err
|
|
}
|
|
|
|
// 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.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: session.Table,
|
|
Columns: session.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeBytes,
|
|
Column: session.FieldID,
|
|
},
|
|
},
|
|
}
|
|
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.FieldSpec{
|
|
Type: field.TypeBytes,
|
|
Column: device.FieldID,
|
|
},
|
|
},
|
|
}
|
|
_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.FieldSpec{
|
|
Type: field.TypeBytes,
|
|
Column: device.FieldID,
|
|
},
|
|
},
|
|
}
|
|
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
|
|
}
|
|
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
|
|
}
|
|
|
|
// 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) {
|
|
var (
|
|
err error
|
|
node *Session
|
|
)
|
|
if len(suo.hooks) == 0 {
|
|
node, err = suo.sqlSave(ctx)
|
|
} else {
|
|
var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
|
|
mutation, ok := m.(*SessionMutation)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected mutation type %T", m)
|
|
}
|
|
suo.mutation = mutation
|
|
node, err = suo.sqlSave(ctx)
|
|
mutation.done = true
|
|
return node, err
|
|
})
|
|
for i := len(suo.hooks) - 1; i >= 0; i-- {
|
|
if suo.hooks[i] == nil {
|
|
return nil, fmt.Errorf("ent: uninitialized hook (forgotten import ent/runtime?)")
|
|
}
|
|
mut = suo.hooks[i](mut)
|
|
}
|
|
v, err := mut.Mutate(ctx, suo.mutation)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
nv, ok := v.(*Session)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unexpected node type %T returned from SessionMutation", v)
|
|
}
|
|
node = nv
|
|
}
|
|
return node, err
|
|
}
|
|
|
|
// 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.UpdateSpec{
|
|
Node: &sqlgraph.NodeSpec{
|
|
Table: session.Table,
|
|
Columns: session.Columns,
|
|
ID: &sqlgraph.FieldSpec{
|
|
Type: field.TypeBytes,
|
|
Column: session.FieldID,
|
|
},
|
|
},
|
|
}
|
|
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.FieldSpec{
|
|
Type: field.TypeBytes,
|
|
Column: device.FieldID,
|
|
},
|
|
},
|
|
}
|
|
_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.FieldSpec{
|
|
Type: field.TypeBytes,
|
|
Column: device.FieldID,
|
|
},
|
|
},
|
|
}
|
|
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
|
|
}
|
|
return _node, nil
|
|
}
|