builder_update.go 903 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2016 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package builder
  5. import (
  6. "fmt"
  7. )
  8. // Update creates an update Builder
  9. func Update(updates ...Eq) *Builder {
  10. builder := &Builder{cond: NewCond()}
  11. return builder.Update(updates...)
  12. }
  13. func (b *Builder) updateWriteTo(w Writer) error {
  14. if len(b.tableName) <= 0 {
  15. return ErrNoTableName
  16. }
  17. if len(b.updates) <= 0 {
  18. return ErrNoColumnToUpdate
  19. }
  20. if _, err := fmt.Fprintf(w, "UPDATE %s SET ", b.tableName); err != nil {
  21. return err
  22. }
  23. for i, s := range b.updates {
  24. if err := s.opWriteTo(",", w); err != nil {
  25. return err
  26. }
  27. if i != len(b.updates)-1 {
  28. if _, err := fmt.Fprint(w, ","); err != nil {
  29. return err
  30. }
  31. }
  32. }
  33. if _, err := fmt.Fprint(w, " WHERE "); err != nil {
  34. return err
  35. }
  36. return b.cond.WriteTo(w)
  37. }