github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/kit/sqlx/builder/builder_stmt_update.go (about)

     1  package builder
     2  
     3  import "context"
     4  
     5  type StmtUpdate struct {
     6  	tbl         *Table
     7  	modifiers   []string
     8  	assignments []*Assignment
     9  	adds        []Addition
    10  }
    11  
    12  func Update(tbl *Table, modifiers ...string) *StmtUpdate {
    13  	return &StmtUpdate{tbl: tbl, modifiers: modifiers}
    14  }
    15  
    16  func (s *StmtUpdate) IsNil() bool {
    17  	return s == nil || IsNilExpr(s.tbl) || len(s.assignments) == 0
    18  }
    19  
    20  func (s StmtUpdate) Set(assignments ...*Assignment) *StmtUpdate {
    21  	s.assignments = assignments
    22  	return &s
    23  }
    24  
    25  func (s StmtUpdate) Where(c SqlCondition, adds ...Addition) *StmtUpdate {
    26  	s.adds = []Addition{Where(c)}
    27  	if len(adds) > 0 {
    28  		s.adds = append(s.adds, adds...)
    29  	}
    30  	return &s
    31  }
    32  
    33  func (s *StmtUpdate) Ex(ctx context.Context) *Ex {
    34  	e := Expr("UPDATE")
    35  	if len(s.modifiers) > 0 {
    36  		for i := range s.modifiers {
    37  			e.WriteQueryByte(' ')
    38  			e.WriteQuery(s.modifiers[i])
    39  		}
    40  	}
    41  	e.WriteQueryByte(' ')
    42  	e.WriteExpr(s.tbl)
    43  	e.WriteQuery(" SET ")
    44  
    45  	WriteAssignments(e, s.assignments...)
    46  	WriteAdditions(e, s.adds...)
    47  	return e.Ex(ctx)
    48  }
    49  
    50  // TODO Update without condition warning