gitee.com/eden-framework/sqlx@v0.0.3/builder/stmt_update.go (about)

     1  package builder
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  )
     7  
     8  var (
     9  	UpdateNeedLimitByWhere = errors.New("no where limit for update")
    10  )
    11  
    12  func Update(table *Table, modifiers ...string) *StmtUpdate {
    13  	return &StmtUpdate{
    14  		table:     table,
    15  		modifiers: modifiers,
    16  	}
    17  }
    18  
    19  type StmtUpdate struct {
    20  	table       *Table
    21  	modifiers   []string
    22  	assignments []*Assignment
    23  	additions   []Addition
    24  }
    25  
    26  func (s *StmtUpdate) IsNil() bool {
    27  	return s == nil || IsNilExpr(s.table) || len(s.assignments) == 0
    28  }
    29  
    30  func (s StmtUpdate) Set(assignments ...*Assignment) *StmtUpdate {
    31  	s.assignments = assignments
    32  	return &s
    33  }
    34  
    35  func (s StmtUpdate) Where(c SqlCondition, additions ...Addition) *StmtUpdate {
    36  	s.additions = []Addition{Where(c)}
    37  	if len(additions) > 0 {
    38  		s.additions = append(s.additions, additions...)
    39  	}
    40  	return &s
    41  }
    42  
    43  func (s *StmtUpdate) Ex(ctx context.Context) *Ex {
    44  	e := Expr("UPDATE")
    45  
    46  	if len(s.modifiers) > 0 {
    47  		for i := range s.modifiers {
    48  			e.WriteByte(' ')
    49  			e.WriteString(s.modifiers[i])
    50  		}
    51  	}
    52  
    53  	e.WriteByte(' ')
    54  	e.WriteExpr(s.table)
    55  	e.WriteString(" SET ")
    56  
    57  	WriteAssignments(e, s.assignments...)
    58  	WriteAdditions(e, s.additions...)
    59  
    60  	return e.Ex(ctx)
    61  }