// Code generated by entc, DO NOT EDIT. package ent import ( "context" "fmt" "github.com/facebook/ent/dialect/sql" "github.com/facebook/ent/dialect/sql/sqlgraph" "github.com/facebook/ent/schema/field" "github.com/roleypoly/roleypoly/src/db/ent/predicate" "github.com/roleypoly/roleypoly/src/db/ent/session" ) // SessionUpdate is the builder for updating Session entities. type SessionUpdate struct { config hooks []Hook mutation *SessionMutation predicates []predicate.Session } // Where adds a new predicate for the builder. func (su *SessionUpdate) Where(ps ...predicate.Session) *SessionUpdate { su.predicates = append(su.predicates, ps...) return su } // Mutation returns the SessionMutation object of the builder. func (su *SessionUpdate) Mutation() *SessionMutation { return su.mutation } // Save executes the query and returns the number of rows/vertices matched by this operation. func (su *SessionUpdate) Save(ctx context.Context) (int, error) { if _, ok := su.mutation.UpdateTime(); !ok { v := session.UpdateDefaultUpdateTime() su.mutation.SetUpdateTime(v) } 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-- { 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.TypeInt, Column: session.FieldID, }, }, } if ps := su.predicates; len(ps) > 0 { _spec.Predicate = func(selector *sql.Selector) { for i := range ps { ps[i](selector) } } } if value, ok := su.mutation.UpdateTime(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: session.FieldUpdateTime, }) } if n, err = sqlgraph.UpdateNodes(ctx, su.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{session.Label} } else if cerr, ok := isSQLConstraintError(err); ok { err = cerr } return 0, err } return n, nil } // SessionUpdateOne is the builder for updating a single Session entity. type SessionUpdateOne struct { config hooks []Hook mutation *SessionMutation } // Mutation returns the SessionMutation object of the builder. func (suo *SessionUpdateOne) Mutation() *SessionMutation { return suo.mutation } // Save executes the query and returns the updated entity. func (suo *SessionUpdateOne) Save(ctx context.Context) (*Session, error) { if _, ok := suo.mutation.UpdateTime(); !ok { v := session.UpdateDefaultUpdateTime() suo.mutation.SetUpdateTime(v) } 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-- { mut = suo.hooks[i](mut) } if _, err := mut.Mutate(ctx, suo.mutation); err != nil { return nil, err } } return node, err } // SaveX is like Save, but panics if an error occurs. func (suo *SessionUpdateOne) SaveX(ctx context.Context) *Session { s, err := suo.Save(ctx) if err != nil { panic(err) } return s } // 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) (s *Session, err error) { _spec := &sqlgraph.UpdateSpec{ Node: &sqlgraph.NodeSpec{ Table: session.Table, Columns: session.Columns, ID: &sqlgraph.FieldSpec{ Type: field.TypeInt, Column: session.FieldID, }, }, } id, ok := suo.mutation.ID() if !ok { return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing Session.ID for update")} } _spec.Node.ID.Value = id if value, ok := suo.mutation.UpdateTime(); ok { _spec.Fields.Set = append(_spec.Fields.Set, &sqlgraph.FieldSpec{ Type: field.TypeTime, Value: value, Column: session.FieldUpdateTime, }) } s = &Session{config: suo.config} _spec.Assign = s.assignValues _spec.ScanValues = s.scanValues() if err = sqlgraph.UpdateNode(ctx, suo.driver, _spec); err != nil { if _, ok := err.(*sqlgraph.NotFoundError); ok { err = &NotFoundError{session.Label} } else if cerr, ok := isSQLConstraintError(err); ok { err = cerr } return nil, err } return s, nil }