// 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/anzhiyu-c/anheyu-app/ent/predicate"
	"github.com/anzhiyu-c/anheyu-app/ent/visitorlog"
)

// VisitorLogUpdate is the builder for updating VisitorLog entities.
type VisitorLogUpdate struct {
	config
	hooks     []Hook
	mutation  *VisitorLogMutation
	modifiers []func(*sql.UpdateBuilder)
}

// Where appends a list predicates to the VisitorLogUpdate builder.
func (_u *VisitorLogUpdate) Where(ps ...predicate.VisitorLog) *VisitorLogUpdate {
	_u.mutation.Where(ps...)
	return _u
}

// SetVisitorID sets the "visitor_id" field.
func (_u *VisitorLogUpdate) SetVisitorID(v string) *VisitorLogUpdate {
	_u.mutation.SetVisitorID(v)
	return _u
}

// SetNillableVisitorID sets the "visitor_id" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableVisitorID(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetVisitorID(*v)
	}
	return _u
}

// SetSessionID sets the "session_id" field.
func (_u *VisitorLogUpdate) SetSessionID(v string) *VisitorLogUpdate {
	_u.mutation.SetSessionID(v)
	return _u
}

// SetNillableSessionID sets the "session_id" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableSessionID(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetSessionID(*v)
	}
	return _u
}

// ClearSessionID clears the value of the "session_id" field.
func (_u *VisitorLogUpdate) ClearSessionID() *VisitorLogUpdate {
	_u.mutation.ClearSessionID()
	return _u
}

// SetIPAddress sets the "ip_address" field.
func (_u *VisitorLogUpdate) SetIPAddress(v string) *VisitorLogUpdate {
	_u.mutation.SetIPAddress(v)
	return _u
}

// SetNillableIPAddress sets the "ip_address" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableIPAddress(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetIPAddress(*v)
	}
	return _u
}

// SetUserAgent sets the "user_agent" field.
func (_u *VisitorLogUpdate) SetUserAgent(v string) *VisitorLogUpdate {
	_u.mutation.SetUserAgent(v)
	return _u
}

// SetNillableUserAgent sets the "user_agent" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableUserAgent(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetUserAgent(*v)
	}
	return _u
}

// ClearUserAgent clears the value of the "user_agent" field.
func (_u *VisitorLogUpdate) ClearUserAgent() *VisitorLogUpdate {
	_u.mutation.ClearUserAgent()
	return _u
}

// SetReferer sets the "referer" field.
func (_u *VisitorLogUpdate) SetReferer(v string) *VisitorLogUpdate {
	_u.mutation.SetReferer(v)
	return _u
}

// SetNillableReferer sets the "referer" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableReferer(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetReferer(*v)
	}
	return _u
}

// ClearReferer clears the value of the "referer" field.
func (_u *VisitorLogUpdate) ClearReferer() *VisitorLogUpdate {
	_u.mutation.ClearReferer()
	return _u
}

// SetURLPath sets the "url_path" field.
func (_u *VisitorLogUpdate) SetURLPath(v string) *VisitorLogUpdate {
	_u.mutation.SetURLPath(v)
	return _u
}

// SetNillableURLPath sets the "url_path" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableURLPath(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetURLPath(*v)
	}
	return _u
}

// SetCountry sets the "country" field.
func (_u *VisitorLogUpdate) SetCountry(v string) *VisitorLogUpdate {
	_u.mutation.SetCountry(v)
	return _u
}

// SetNillableCountry sets the "country" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableCountry(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetCountry(*v)
	}
	return _u
}

// ClearCountry clears the value of the "country" field.
func (_u *VisitorLogUpdate) ClearCountry() *VisitorLogUpdate {
	_u.mutation.ClearCountry()
	return _u
}

// SetRegion sets the "region" field.
func (_u *VisitorLogUpdate) SetRegion(v string) *VisitorLogUpdate {
	_u.mutation.SetRegion(v)
	return _u
}

// SetNillableRegion sets the "region" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableRegion(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetRegion(*v)
	}
	return _u
}

// ClearRegion clears the value of the "region" field.
func (_u *VisitorLogUpdate) ClearRegion() *VisitorLogUpdate {
	_u.mutation.ClearRegion()
	return _u
}

// SetCity sets the "city" field.
func (_u *VisitorLogUpdate) SetCity(v string) *VisitorLogUpdate {
	_u.mutation.SetCity(v)
	return _u
}

// SetNillableCity sets the "city" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableCity(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetCity(*v)
	}
	return _u
}

// ClearCity clears the value of the "city" field.
func (_u *VisitorLogUpdate) ClearCity() *VisitorLogUpdate {
	_u.mutation.ClearCity()
	return _u
}

// SetBrowser sets the "browser" field.
func (_u *VisitorLogUpdate) SetBrowser(v string) *VisitorLogUpdate {
	_u.mutation.SetBrowser(v)
	return _u
}

// SetNillableBrowser sets the "browser" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableBrowser(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetBrowser(*v)
	}
	return _u
}

// ClearBrowser clears the value of the "browser" field.
func (_u *VisitorLogUpdate) ClearBrowser() *VisitorLogUpdate {
	_u.mutation.ClearBrowser()
	return _u
}

// SetOs sets the "os" field.
func (_u *VisitorLogUpdate) SetOs(v string) *VisitorLogUpdate {
	_u.mutation.SetOs(v)
	return _u
}

// SetNillableOs sets the "os" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableOs(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetOs(*v)
	}
	return _u
}

// ClearOs clears the value of the "os" field.
func (_u *VisitorLogUpdate) ClearOs() *VisitorLogUpdate {
	_u.mutation.ClearOs()
	return _u
}

// SetDevice sets the "device" field.
func (_u *VisitorLogUpdate) SetDevice(v string) *VisitorLogUpdate {
	_u.mutation.SetDevice(v)
	return _u
}

// SetNillableDevice sets the "device" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableDevice(v *string) *VisitorLogUpdate {
	if v != nil {
		_u.SetDevice(*v)
	}
	return _u
}

// ClearDevice clears the value of the "device" field.
func (_u *VisitorLogUpdate) ClearDevice() *VisitorLogUpdate {
	_u.mutation.ClearDevice()
	return _u
}

// SetDuration sets the "duration" field.
func (_u *VisitorLogUpdate) SetDuration(v int) *VisitorLogUpdate {
	_u.mutation.ResetDuration()
	_u.mutation.SetDuration(v)
	return _u
}

// SetNillableDuration sets the "duration" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableDuration(v *int) *VisitorLogUpdate {
	if v != nil {
		_u.SetDuration(*v)
	}
	return _u
}

// AddDuration adds value to the "duration" field.
func (_u *VisitorLogUpdate) AddDuration(v int) *VisitorLogUpdate {
	_u.mutation.AddDuration(v)
	return _u
}

// SetIsBounce sets the "is_bounce" field.
func (_u *VisitorLogUpdate) SetIsBounce(v bool) *VisitorLogUpdate {
	_u.mutation.SetIsBounce(v)
	return _u
}

// SetNillableIsBounce sets the "is_bounce" field if the given value is not nil.
func (_u *VisitorLogUpdate) SetNillableIsBounce(v *bool) *VisitorLogUpdate {
	if v != nil {
		_u.SetIsBounce(*v)
	}
	return _u
}

// Mutation returns the VisitorLogMutation object of the builder.
func (_u *VisitorLogUpdate) Mutation() *VisitorLogMutation {
	return _u.mutation
}

// Save executes the query and returns the number of nodes affected by the update operation.
func (_u *VisitorLogUpdate) 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 *VisitorLogUpdate) SaveX(ctx context.Context) int {
	affected, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return affected
}

// Exec executes the query.
func (_u *VisitorLogUpdate) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *VisitorLogUpdate) 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 *VisitorLogUpdate) check() error {
	if v, ok := _u.mutation.VisitorID(); ok {
		if err := visitorlog.VisitorIDValidator(v); err != nil {
			return &ValidationError{Name: "visitor_id", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.visitor_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.SessionID(); ok {
		if err := visitorlog.SessionIDValidator(v); err != nil {
			return &ValidationError{Name: "session_id", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.session_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.IPAddress(); ok {
		if err := visitorlog.IPAddressValidator(v); err != nil {
			return &ValidationError{Name: "ip_address", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.ip_address": %w`, err)}
		}
	}
	if v, ok := _u.mutation.UserAgent(); ok {
		if err := visitorlog.UserAgentValidator(v); err != nil {
			return &ValidationError{Name: "user_agent", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.user_agent": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Referer(); ok {
		if err := visitorlog.RefererValidator(v); err != nil {
			return &ValidationError{Name: "referer", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.referer": %w`, err)}
		}
	}
	if v, ok := _u.mutation.URLPath(); ok {
		if err := visitorlog.URLPathValidator(v); err != nil {
			return &ValidationError{Name: "url_path", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.url_path": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Country(); ok {
		if err := visitorlog.CountryValidator(v); err != nil {
			return &ValidationError{Name: "country", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.country": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Region(); ok {
		if err := visitorlog.RegionValidator(v); err != nil {
			return &ValidationError{Name: "region", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.region": %w`, err)}
		}
	}
	if v, ok := _u.mutation.City(); ok {
		if err := visitorlog.CityValidator(v); err != nil {
			return &ValidationError{Name: "city", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.city": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Browser(); ok {
		if err := visitorlog.BrowserValidator(v); err != nil {
			return &ValidationError{Name: "browser", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.browser": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Os(); ok {
		if err := visitorlog.OsValidator(v); err != nil {
			return &ValidationError{Name: "os", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.os": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Device(); ok {
		if err := visitorlog.DeviceValidator(v); err != nil {
			return &ValidationError{Name: "device", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.device": %w`, err)}
		}
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *VisitorLogUpdate) Modify(modifiers ...func(u *sql.UpdateBuilder)) *VisitorLogUpdate {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *VisitorLogUpdate) sqlSave(ctx context.Context) (_node int, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(visitorlog.Table, visitorlog.Columns, sqlgraph.NewFieldSpec(visitorlog.FieldID, field.TypeUint))
	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.VisitorID(); ok {
		_spec.SetField(visitorlog.FieldVisitorID, field.TypeString, value)
	}
	if value, ok := _u.mutation.SessionID(); ok {
		_spec.SetField(visitorlog.FieldSessionID, field.TypeString, value)
	}
	if _u.mutation.SessionIDCleared() {
		_spec.ClearField(visitorlog.FieldSessionID, field.TypeString)
	}
	if value, ok := _u.mutation.IPAddress(); ok {
		_spec.SetField(visitorlog.FieldIPAddress, field.TypeString, value)
	}
	if value, ok := _u.mutation.UserAgent(); ok {
		_spec.SetField(visitorlog.FieldUserAgent, field.TypeString, value)
	}
	if _u.mutation.UserAgentCleared() {
		_spec.ClearField(visitorlog.FieldUserAgent, field.TypeString)
	}
	if value, ok := _u.mutation.Referer(); ok {
		_spec.SetField(visitorlog.FieldReferer, field.TypeString, value)
	}
	if _u.mutation.RefererCleared() {
		_spec.ClearField(visitorlog.FieldReferer, field.TypeString)
	}
	if value, ok := _u.mutation.URLPath(); ok {
		_spec.SetField(visitorlog.FieldURLPath, field.TypeString, value)
	}
	if value, ok := _u.mutation.Country(); ok {
		_spec.SetField(visitorlog.FieldCountry, field.TypeString, value)
	}
	if _u.mutation.CountryCleared() {
		_spec.ClearField(visitorlog.FieldCountry, field.TypeString)
	}
	if value, ok := _u.mutation.Region(); ok {
		_spec.SetField(visitorlog.FieldRegion, field.TypeString, value)
	}
	if _u.mutation.RegionCleared() {
		_spec.ClearField(visitorlog.FieldRegion, field.TypeString)
	}
	if value, ok := _u.mutation.City(); ok {
		_spec.SetField(visitorlog.FieldCity, field.TypeString, value)
	}
	if _u.mutation.CityCleared() {
		_spec.ClearField(visitorlog.FieldCity, field.TypeString)
	}
	if value, ok := _u.mutation.Browser(); ok {
		_spec.SetField(visitorlog.FieldBrowser, field.TypeString, value)
	}
	if _u.mutation.BrowserCleared() {
		_spec.ClearField(visitorlog.FieldBrowser, field.TypeString)
	}
	if value, ok := _u.mutation.Os(); ok {
		_spec.SetField(visitorlog.FieldOs, field.TypeString, value)
	}
	if _u.mutation.OsCleared() {
		_spec.ClearField(visitorlog.FieldOs, field.TypeString)
	}
	if value, ok := _u.mutation.Device(); ok {
		_spec.SetField(visitorlog.FieldDevice, field.TypeString, value)
	}
	if _u.mutation.DeviceCleared() {
		_spec.ClearField(visitorlog.FieldDevice, field.TypeString)
	}
	if value, ok := _u.mutation.Duration(); ok {
		_spec.SetField(visitorlog.FieldDuration, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedDuration(); ok {
		_spec.AddField(visitorlog.FieldDuration, field.TypeInt, value)
	}
	if value, ok := _u.mutation.IsBounce(); ok {
		_spec.SetField(visitorlog.FieldIsBounce, field.TypeBool, value)
	}
	_spec.AddModifiers(_u.modifiers...)
	if _node, err = sqlgraph.UpdateNodes(ctx, _u.driver, _spec); err != nil {
		if _, ok := err.(*sqlgraph.NotFoundError); ok {
			err = &NotFoundError{visitorlog.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return 0, err
	}
	_u.mutation.done = true
	return _node, nil
}

// VisitorLogUpdateOne is the builder for updating a single VisitorLog entity.
type VisitorLogUpdateOne struct {
	config
	fields    []string
	hooks     []Hook
	mutation  *VisitorLogMutation
	modifiers []func(*sql.UpdateBuilder)
}

// SetVisitorID sets the "visitor_id" field.
func (_u *VisitorLogUpdateOne) SetVisitorID(v string) *VisitorLogUpdateOne {
	_u.mutation.SetVisitorID(v)
	return _u
}

// SetNillableVisitorID sets the "visitor_id" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableVisitorID(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetVisitorID(*v)
	}
	return _u
}

// SetSessionID sets the "session_id" field.
func (_u *VisitorLogUpdateOne) SetSessionID(v string) *VisitorLogUpdateOne {
	_u.mutation.SetSessionID(v)
	return _u
}

// SetNillableSessionID sets the "session_id" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableSessionID(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetSessionID(*v)
	}
	return _u
}

// ClearSessionID clears the value of the "session_id" field.
func (_u *VisitorLogUpdateOne) ClearSessionID() *VisitorLogUpdateOne {
	_u.mutation.ClearSessionID()
	return _u
}

// SetIPAddress sets the "ip_address" field.
func (_u *VisitorLogUpdateOne) SetIPAddress(v string) *VisitorLogUpdateOne {
	_u.mutation.SetIPAddress(v)
	return _u
}

// SetNillableIPAddress sets the "ip_address" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableIPAddress(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetIPAddress(*v)
	}
	return _u
}

// SetUserAgent sets the "user_agent" field.
func (_u *VisitorLogUpdateOne) SetUserAgent(v string) *VisitorLogUpdateOne {
	_u.mutation.SetUserAgent(v)
	return _u
}

// SetNillableUserAgent sets the "user_agent" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableUserAgent(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetUserAgent(*v)
	}
	return _u
}

// ClearUserAgent clears the value of the "user_agent" field.
func (_u *VisitorLogUpdateOne) ClearUserAgent() *VisitorLogUpdateOne {
	_u.mutation.ClearUserAgent()
	return _u
}

// SetReferer sets the "referer" field.
func (_u *VisitorLogUpdateOne) SetReferer(v string) *VisitorLogUpdateOne {
	_u.mutation.SetReferer(v)
	return _u
}

// SetNillableReferer sets the "referer" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableReferer(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetReferer(*v)
	}
	return _u
}

// ClearReferer clears the value of the "referer" field.
func (_u *VisitorLogUpdateOne) ClearReferer() *VisitorLogUpdateOne {
	_u.mutation.ClearReferer()
	return _u
}

// SetURLPath sets the "url_path" field.
func (_u *VisitorLogUpdateOne) SetURLPath(v string) *VisitorLogUpdateOne {
	_u.mutation.SetURLPath(v)
	return _u
}

// SetNillableURLPath sets the "url_path" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableURLPath(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetURLPath(*v)
	}
	return _u
}

// SetCountry sets the "country" field.
func (_u *VisitorLogUpdateOne) SetCountry(v string) *VisitorLogUpdateOne {
	_u.mutation.SetCountry(v)
	return _u
}

// SetNillableCountry sets the "country" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableCountry(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetCountry(*v)
	}
	return _u
}

// ClearCountry clears the value of the "country" field.
func (_u *VisitorLogUpdateOne) ClearCountry() *VisitorLogUpdateOne {
	_u.mutation.ClearCountry()
	return _u
}

// SetRegion sets the "region" field.
func (_u *VisitorLogUpdateOne) SetRegion(v string) *VisitorLogUpdateOne {
	_u.mutation.SetRegion(v)
	return _u
}

// SetNillableRegion sets the "region" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableRegion(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetRegion(*v)
	}
	return _u
}

// ClearRegion clears the value of the "region" field.
func (_u *VisitorLogUpdateOne) ClearRegion() *VisitorLogUpdateOne {
	_u.mutation.ClearRegion()
	return _u
}

// SetCity sets the "city" field.
func (_u *VisitorLogUpdateOne) SetCity(v string) *VisitorLogUpdateOne {
	_u.mutation.SetCity(v)
	return _u
}

// SetNillableCity sets the "city" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableCity(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetCity(*v)
	}
	return _u
}

// ClearCity clears the value of the "city" field.
func (_u *VisitorLogUpdateOne) ClearCity() *VisitorLogUpdateOne {
	_u.mutation.ClearCity()
	return _u
}

// SetBrowser sets the "browser" field.
func (_u *VisitorLogUpdateOne) SetBrowser(v string) *VisitorLogUpdateOne {
	_u.mutation.SetBrowser(v)
	return _u
}

// SetNillableBrowser sets the "browser" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableBrowser(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetBrowser(*v)
	}
	return _u
}

// ClearBrowser clears the value of the "browser" field.
func (_u *VisitorLogUpdateOne) ClearBrowser() *VisitorLogUpdateOne {
	_u.mutation.ClearBrowser()
	return _u
}

// SetOs sets the "os" field.
func (_u *VisitorLogUpdateOne) SetOs(v string) *VisitorLogUpdateOne {
	_u.mutation.SetOs(v)
	return _u
}

// SetNillableOs sets the "os" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableOs(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetOs(*v)
	}
	return _u
}

// ClearOs clears the value of the "os" field.
func (_u *VisitorLogUpdateOne) ClearOs() *VisitorLogUpdateOne {
	_u.mutation.ClearOs()
	return _u
}

// SetDevice sets the "device" field.
func (_u *VisitorLogUpdateOne) SetDevice(v string) *VisitorLogUpdateOne {
	_u.mutation.SetDevice(v)
	return _u
}

// SetNillableDevice sets the "device" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableDevice(v *string) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetDevice(*v)
	}
	return _u
}

// ClearDevice clears the value of the "device" field.
func (_u *VisitorLogUpdateOne) ClearDevice() *VisitorLogUpdateOne {
	_u.mutation.ClearDevice()
	return _u
}

// SetDuration sets the "duration" field.
func (_u *VisitorLogUpdateOne) SetDuration(v int) *VisitorLogUpdateOne {
	_u.mutation.ResetDuration()
	_u.mutation.SetDuration(v)
	return _u
}

// SetNillableDuration sets the "duration" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableDuration(v *int) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetDuration(*v)
	}
	return _u
}

// AddDuration adds value to the "duration" field.
func (_u *VisitorLogUpdateOne) AddDuration(v int) *VisitorLogUpdateOne {
	_u.mutation.AddDuration(v)
	return _u
}

// SetIsBounce sets the "is_bounce" field.
func (_u *VisitorLogUpdateOne) SetIsBounce(v bool) *VisitorLogUpdateOne {
	_u.mutation.SetIsBounce(v)
	return _u
}

// SetNillableIsBounce sets the "is_bounce" field if the given value is not nil.
func (_u *VisitorLogUpdateOne) SetNillableIsBounce(v *bool) *VisitorLogUpdateOne {
	if v != nil {
		_u.SetIsBounce(*v)
	}
	return _u
}

// Mutation returns the VisitorLogMutation object of the builder.
func (_u *VisitorLogUpdateOne) Mutation() *VisitorLogMutation {
	return _u.mutation
}

// Where appends a list predicates to the VisitorLogUpdate builder.
func (_u *VisitorLogUpdateOne) Where(ps ...predicate.VisitorLog) *VisitorLogUpdateOne {
	_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 *VisitorLogUpdateOne) Select(field string, fields ...string) *VisitorLogUpdateOne {
	_u.fields = append([]string{field}, fields...)
	return _u
}

// Save executes the query and returns the updated VisitorLog entity.
func (_u *VisitorLogUpdateOne) Save(ctx context.Context) (*VisitorLog, error) {
	return withHooks(ctx, _u.sqlSave, _u.mutation, _u.hooks)
}

// SaveX is like Save, but panics if an error occurs.
func (_u *VisitorLogUpdateOne) SaveX(ctx context.Context) *VisitorLog {
	node, err := _u.Save(ctx)
	if err != nil {
		panic(err)
	}
	return node
}

// Exec executes the query on the entity.
func (_u *VisitorLogUpdateOne) Exec(ctx context.Context) error {
	_, err := _u.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_u *VisitorLogUpdateOne) 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 *VisitorLogUpdateOne) check() error {
	if v, ok := _u.mutation.VisitorID(); ok {
		if err := visitorlog.VisitorIDValidator(v); err != nil {
			return &ValidationError{Name: "visitor_id", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.visitor_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.SessionID(); ok {
		if err := visitorlog.SessionIDValidator(v); err != nil {
			return &ValidationError{Name: "session_id", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.session_id": %w`, err)}
		}
	}
	if v, ok := _u.mutation.IPAddress(); ok {
		if err := visitorlog.IPAddressValidator(v); err != nil {
			return &ValidationError{Name: "ip_address", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.ip_address": %w`, err)}
		}
	}
	if v, ok := _u.mutation.UserAgent(); ok {
		if err := visitorlog.UserAgentValidator(v); err != nil {
			return &ValidationError{Name: "user_agent", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.user_agent": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Referer(); ok {
		if err := visitorlog.RefererValidator(v); err != nil {
			return &ValidationError{Name: "referer", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.referer": %w`, err)}
		}
	}
	if v, ok := _u.mutation.URLPath(); ok {
		if err := visitorlog.URLPathValidator(v); err != nil {
			return &ValidationError{Name: "url_path", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.url_path": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Country(); ok {
		if err := visitorlog.CountryValidator(v); err != nil {
			return &ValidationError{Name: "country", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.country": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Region(); ok {
		if err := visitorlog.RegionValidator(v); err != nil {
			return &ValidationError{Name: "region", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.region": %w`, err)}
		}
	}
	if v, ok := _u.mutation.City(); ok {
		if err := visitorlog.CityValidator(v); err != nil {
			return &ValidationError{Name: "city", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.city": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Browser(); ok {
		if err := visitorlog.BrowserValidator(v); err != nil {
			return &ValidationError{Name: "browser", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.browser": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Os(); ok {
		if err := visitorlog.OsValidator(v); err != nil {
			return &ValidationError{Name: "os", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.os": %w`, err)}
		}
	}
	if v, ok := _u.mutation.Device(); ok {
		if err := visitorlog.DeviceValidator(v); err != nil {
			return &ValidationError{Name: "device", err: fmt.Errorf(`ent: validator failed for field "VisitorLog.device": %w`, err)}
		}
	}
	return nil
}

// Modify adds a statement modifier for attaching custom logic to the UPDATE statement.
func (_u *VisitorLogUpdateOne) Modify(modifiers ...func(u *sql.UpdateBuilder)) *VisitorLogUpdateOne {
	_u.modifiers = append(_u.modifiers, modifiers...)
	return _u
}

func (_u *VisitorLogUpdateOne) sqlSave(ctx context.Context) (_node *VisitorLog, err error) {
	if err := _u.check(); err != nil {
		return _node, err
	}
	_spec := sqlgraph.NewUpdateSpec(visitorlog.Table, visitorlog.Columns, sqlgraph.NewFieldSpec(visitorlog.FieldID, field.TypeUint))
	id, ok := _u.mutation.ID()
	if !ok {
		return nil, &ValidationError{Name: "id", err: errors.New(`ent: missing "VisitorLog.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, visitorlog.FieldID)
		for _, f := range fields {
			if !visitorlog.ValidColumn(f) {
				return nil, &ValidationError{Name: f, err: fmt.Errorf("ent: invalid field %q for query", f)}
			}
			if f != visitorlog.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.VisitorID(); ok {
		_spec.SetField(visitorlog.FieldVisitorID, field.TypeString, value)
	}
	if value, ok := _u.mutation.SessionID(); ok {
		_spec.SetField(visitorlog.FieldSessionID, field.TypeString, value)
	}
	if _u.mutation.SessionIDCleared() {
		_spec.ClearField(visitorlog.FieldSessionID, field.TypeString)
	}
	if value, ok := _u.mutation.IPAddress(); ok {
		_spec.SetField(visitorlog.FieldIPAddress, field.TypeString, value)
	}
	if value, ok := _u.mutation.UserAgent(); ok {
		_spec.SetField(visitorlog.FieldUserAgent, field.TypeString, value)
	}
	if _u.mutation.UserAgentCleared() {
		_spec.ClearField(visitorlog.FieldUserAgent, field.TypeString)
	}
	if value, ok := _u.mutation.Referer(); ok {
		_spec.SetField(visitorlog.FieldReferer, field.TypeString, value)
	}
	if _u.mutation.RefererCleared() {
		_spec.ClearField(visitorlog.FieldReferer, field.TypeString)
	}
	if value, ok := _u.mutation.URLPath(); ok {
		_spec.SetField(visitorlog.FieldURLPath, field.TypeString, value)
	}
	if value, ok := _u.mutation.Country(); ok {
		_spec.SetField(visitorlog.FieldCountry, field.TypeString, value)
	}
	if _u.mutation.CountryCleared() {
		_spec.ClearField(visitorlog.FieldCountry, field.TypeString)
	}
	if value, ok := _u.mutation.Region(); ok {
		_spec.SetField(visitorlog.FieldRegion, field.TypeString, value)
	}
	if _u.mutation.RegionCleared() {
		_spec.ClearField(visitorlog.FieldRegion, field.TypeString)
	}
	if value, ok := _u.mutation.City(); ok {
		_spec.SetField(visitorlog.FieldCity, field.TypeString, value)
	}
	if _u.mutation.CityCleared() {
		_spec.ClearField(visitorlog.FieldCity, field.TypeString)
	}
	if value, ok := _u.mutation.Browser(); ok {
		_spec.SetField(visitorlog.FieldBrowser, field.TypeString, value)
	}
	if _u.mutation.BrowserCleared() {
		_spec.ClearField(visitorlog.FieldBrowser, field.TypeString)
	}
	if value, ok := _u.mutation.Os(); ok {
		_spec.SetField(visitorlog.FieldOs, field.TypeString, value)
	}
	if _u.mutation.OsCleared() {
		_spec.ClearField(visitorlog.FieldOs, field.TypeString)
	}
	if value, ok := _u.mutation.Device(); ok {
		_spec.SetField(visitorlog.FieldDevice, field.TypeString, value)
	}
	if _u.mutation.DeviceCleared() {
		_spec.ClearField(visitorlog.FieldDevice, field.TypeString)
	}
	if value, ok := _u.mutation.Duration(); ok {
		_spec.SetField(visitorlog.FieldDuration, field.TypeInt, value)
	}
	if value, ok := _u.mutation.AddedDuration(); ok {
		_spec.AddField(visitorlog.FieldDuration, field.TypeInt, value)
	}
	if value, ok := _u.mutation.IsBounce(); ok {
		_spec.SetField(visitorlog.FieldIsBounce, field.TypeBool, value)
	}
	_spec.AddModifiers(_u.modifiers...)
	_node = &VisitorLog{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{visitorlog.Label}
		} else if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	_u.mutation.done = true
	return _node, nil
}
