property_update.go•14.3 kB
// 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/schema/field"
"github.com/Southclaws/storyden/internal/ent/node"
"github.com/Southclaws/storyden/internal/ent/predicate"
"github.com/Southclaws/storyden/internal/ent/property"
"github.com/Southclaws/storyden/internal/ent/propertyschemafield"
"github.com/rs/xid"
)
// PropertyUpdate is the builder for updating Property entities.
type PropertyUpdate struct {
config
hooks []Hook
mutation *PropertyMutation
modifiers []func(*sql.UpdateBuilder)
}
// Where appends a list predicates to the PropertyUpdate builder.
func (_u *PropertyUpdate) Where(ps ...predicate.Property) *PropertyUpdate {
_u.mutation.Where(ps...)
return _u
}
// SetNodeID sets the "node_id" field.
func (_u *PropertyUpdate) SetNodeID(v xid.ID) *PropertyUpdate {
_u.mutation.SetNodeID(v)
return _u
}
// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (_u *PropertyUpdate) SetNillableNodeID(v *xid.ID) *PropertyUpdate {
if v != nil {
_u.SetNodeID(*v)
}
return _u
}
// SetFieldID sets the "field_id" field.
func (_u *PropertyUpdate) SetFieldID(v xid.ID) *PropertyUpdate {
_u.mutation.SetFieldID(v)
return _u
}
// SetNillableFieldID sets the "field_id" field if the given value is not nil.
func (_u *PropertyUpdate) SetNillableFieldID(v *xid.ID) *PropertyUpdate {
if v != nil {
_u.SetFieldID(*v)
}
return _u
}
// SetValue sets the "value" field.
func (_u *PropertyUpdate) SetValue(v string) *PropertyUpdate {
_u.mutation.SetValue(v)
return _u
}
// SetNillableValue sets the "value" field if the given value is not nil.
func (_u *PropertyUpdate) SetNillableValue(v *string) *PropertyUpdate {
if v != nil {
_u.SetValue(*v)
}
return _u
}
// SetNode sets the "node" edge to the Node entity.
func (_u *PropertyUpdate) SetNode(v *Node) *PropertyUpdate {
return _u.SetNodeID(v.ID)
}
// SetSchemaID sets the "schema" edge to the PropertySchemaField entity by ID.
func (_u *PropertyUpdate) SetSchemaID(id xid.ID) *PropertyUpdate {
_u.mutation.SetSchemaID(id)
return _u
}
// SetSchema sets the "schema" edge to the PropertySchemaField entity.
func (_u *PropertyUpdate) SetSchema(v *PropertySchemaField) *PropertyUpdate {
return _u.SetSchemaID(v.ID)
}
// Mutation returns the PropertyMutation object of the builder.
func (_u *PropertyUpdate) Mutation() *PropertyMutation {
return _u.mutation
}
// ClearNode clears the "node" edge to the Node entity.
func (_u *PropertyUpdate) ClearNode() *PropertyUpdate {
_u.mutation.ClearNode()
return _u
}
// ClearSchema clears the "schema" edge to the PropertySchemaField entity.
func (_u *PropertyUpdate) ClearSchema() *PropertyUpdate {
_u.mutation.ClearSchema()
return _u
}
// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *PropertyUpdate) Save(ctx context.Context) (int, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *PropertyUpdate) SaveX(ctx context.Context) int {
affected, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return affected
}
// Exec executes the query.
func (_u *PropertyUpdate) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *PropertyUpdate) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *PropertyUpdate) check() error {
if _u.mutation.NodeCleared() && len(_u.mutation.NodeIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Property.node"`)
}
if _u.mutation.SchemaCleared() && len(_u.mutation.SchemaIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Property.schema"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *PropertyUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *PropertyUpdate {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *PropertyUpdate) sqlSave(ctx context.Context) (_node int, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(property.Table, property.Columns, sqlgraph.NewFieldSpec(property.FieldID, field.TypeString))
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.Value(); ok {
_spec.SetField(property.FieldValue, field.TypeString, value)
}
if _u.mutation.NodeCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.NodeTable,
Columns: []string{property.NodeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.NodeIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.NodeTable,
Columns: []string{property.NodeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.SchemaCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.SchemaTable,
Columns: []string{property.SchemaColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(propertyschemafield.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.SchemaIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.SchemaTable,
Columns: []string{property.SchemaColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(propertyschemafield.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.AddModifiers(_u.modifiers...)
if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{property.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return 0, err
}
_u.mutation.done = true
return _node, nil
}
// PropertyUpdateOne is the builder for updating a single Property entity.
type PropertyUpdateOne struct {
config
fields []string
hooks []Hook
mutation *PropertyMutation
modifiers []func(*sql.UpdateBuilder)
}
// SetNodeID sets the "node_id" field.
func (_u *PropertyUpdateOne) SetNodeID(v xid.ID) *PropertyUpdateOne {
_u.mutation.SetNodeID(v)
return _u
}
// SetNillableNodeID sets the "node_id" field if the given value is not nil.
func (_u *PropertyUpdateOne) SetNillableNodeID(v *xid.ID) *PropertyUpdateOne {
if v != nil {
_u.SetNodeID(*v)
}
return _u
}
// SetFieldID sets the "field_id" field.
func (_u *PropertyUpdateOne) SetFieldID(v xid.ID) *PropertyUpdateOne {
_u.mutation.SetFieldID(v)
return _u
}
// SetNillableFieldID sets the "field_id" field if the given value is not nil.
func (_u *PropertyUpdateOne) SetNillableFieldID(v *xid.ID) *PropertyUpdateOne {
if v != nil {
_u.SetFieldID(*v)
}
return _u
}
// SetValue sets the "value" field.
func (_u *PropertyUpdateOne) SetValue(v string) *PropertyUpdateOne {
_u.mutation.SetValue(v)
return _u
}
// SetNillableValue sets the "value" field if the given value is not nil.
func (_u *PropertyUpdateOne) SetNillableValue(v *string) *PropertyUpdateOne {
if v != nil {
_u.SetValue(*v)
}
return _u
}
// SetNode sets the "node" edge to the Node entity.
func (_u *PropertyUpdateOne) SetNode(v *Node) *PropertyUpdateOne {
return _u.SetNodeID(v.ID)
}
// SetSchemaID sets the "schema" edge to the PropertySchemaField entity by ID.
func (_u *PropertyUpdateOne) SetSchemaID(id xid.ID) *PropertyUpdateOne {
_u.mutation.SetSchemaID(id)
return _u
}
// SetSchema sets the "schema" edge to the PropertySchemaField entity.
func (_u *PropertyUpdateOne) SetSchema(v *PropertySchemaField) *PropertyUpdateOne {
return _u.SetSchemaID(v.ID)
}
// Mutation returns the PropertyMutation object of the builder.
func (_u *PropertyUpdateOne) Mutation() *PropertyMutation {
return _u.mutation
}
// ClearNode clears the "node" edge to the Node entity.
func (_u *PropertyUpdateOne) ClearNode() *PropertyUpdateOne {
_u.mutation.ClearNode()
return _u
}
// ClearSchema clears the "schema" edge to the PropertySchemaField entity.
func (_u *PropertyUpdateOne) ClearSchema() *PropertyUpdateOne {
_u.mutation.ClearSchema()
return _u
}
// Where appends a list predicates to the PropertyUpdate builder.
func (_u *PropertyUpdateOne) Where(ps ...predicate.Property) *PropertyUpdateOne {
_u.mutation.Where(ps...)
return _u
}
// Select allows selecting one or more fields (columns) of the returned entity.
// The default is selecting all fields defined in the entity schema.
func (_u *PropertyUpdateOne) Select(field string, fields ...string) *PropertyUpdateOne {
_u.fields = append([]string{field}, fields...)
return _u
}
// Save executes the query and returns the updated Property entity.
func (_u *PropertyUpdateOne) Save(ctx context.Context) (*Property, error) {
return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}
// SaveX is like Save, but panics if an error occurs.
func (_u *PropertyUpdateOne) SaveX(ctx context.Context) *Property {
node, err := _u.Save(ctx)
if err != nil {
panic(err)
}
return node
}
// Exec executes the query on the entity.
func (_u *PropertyUpdateOne) Exec(ctx context.Context) error {
_, err := _u.Save(ctx)
return err
}
// ExecX is like Exec, but panics if an error occurs.
func (_u *PropertyUpdateOne) ExecX(ctx context.Context) {
if err := _u.Exec(ctx); err != nil {
panic(err)
}
}
// check runs all checks and user-defined validators on the builder.
func (_u *PropertyUpdateOne) check() error {
if _u.mutation.NodeCleared() && len(_u.mutation.NodeIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Property.node"`)
}
if _u.mutation.SchemaCleared() && len(_u.mutation.SchemaIDs()) > 0 {
return errors.New(`ent: clearing a required unique edge "Property.schema"`)
}
return nil
}
// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *PropertyUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *PropertyUpdateOne {
_u.modifiers = append(_u.modifiers, modifiers...)
return _u
}
func (_u *PropertyUpdateOne) sqlSave(ctx context.Context) (_node *Property, err error) {
if err := _u.check(); err != nil {
return _node, err
}
_spec := sqlgraph.NewUpdateSpec(property.Table, property.Columns, sqlgraph.NewFieldSpec(property.FieldID, field.TypeString))
id, ok := _u.mutation.ID()
if !ok {
return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "Property.id" for update`)}
}
_spec.Node.ID.Value = id
if fields := _u.fields; len(fields) > 0 {
_spec.Node.Columns = make([]string, 0, len(fields))
_spec.Node.Columns = append(_spec.Node.Columns, property.FieldID)
for _, f := range fields {
if !property.ValidColumn(f) {
return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
}
if f != property.FieldID {
_spec.Node.Columns = append(_spec.Node.Columns, f)
}
}
}
if ps := _u.mutation.predicates; len(ps) > 0 {
_spec.Predicate = func(selector *sql.Selector) {
for i := range ps {
ps[i](selector)
}
}
}
if value, ok := _u.mutation.Value(); ok {
_spec.SetField(property.FieldValue, field.TypeString, value)
}
if _u.mutation.NodeCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.NodeTable,
Columns: []string{property.NodeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.NodeIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.NodeTable,
Columns: []string{property.NodeColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(node.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
if _u.mutation.SchemaCleared() {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.SchemaTable,
Columns: []string{property.SchemaColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(propertyschemafield.FieldID, field.TypeString),
},
}
_spec.Edges.Clear = append(_spec.Edges.Clear, edge)
}
if nodes := _u.mutation.SchemaIDs(); len(nodes) > 0 {
edge := &sqlgraph.EdgeSpec{
Rel: sqlgraph.M2O,
Inverse: true,
Table: property.SchemaTable,
Columns: []string{property.SchemaColumn},
Bidi: false,
Target: &sqlgraph.EdgeTarget{
IDSpec: sqlgraph.NewFieldSpec(propertyschemafield.FieldID, field.TypeString),
},
}
for _, k := range nodes {
edge.Target.Nodes = append(edge.Target.Nodes, k)
}
_spec.Edges.Add = append(_spec.Edges.Add, edge)
}
_spec.AddModifiers(_u.modifiers...)
_node = &Property{config: _u.config}
_spec.Assign = _node.assignValues
_spec.ScanValues = _node.scanValues
if err = sqlgraph.UpdateNode(ctx, _u.driver, _spec); err != nil {
if _, ok := err.(*sqlgraph.NotFoundError); ok {
err = &NotFoundError{property.Label}
} else if sqlgraph.IsConstraintError(err) {
err = &ConstraintError{msg: err.Error(), wrap: err}
}
return nil, err
}
_u.mutation.done = true
return _node, nil
}