本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.0KB

  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. // UpdateCond defines an interface that cond could be used with update
  9. type UpdateCond interface {
  10. IsValid() bool
  11. OpWriteTo(op string, w Writer) error
  12. }
  13. // Update creates an update Builder
  14. func Update(updates ...Cond) *Builder {
  15. builder := &Builder{cond: NewCond()}
  16. return builder.Update(updates...)
  17. }
  18. func (b *Builder) updateWriteTo(w Writer) error {
  19. if len(b.from) <= 0 {
  20. return ErrNoTableName
  21. }
  22. if len(b.updates) <= 0 {
  23. return ErrNoColumnToUpdate
  24. }
  25. if _, err := fmt.Fprintf(w, "UPDATE %s SET ", b.from); err != nil {
  26. return err
  27. }
  28. for i, s := range b.updates {
  29. if err := s.OpWriteTo(",", w); err != nil {
  30. return err
  31. }
  32. if i != len(b.updates)-1 {
  33. if _, err := fmt.Fprint(w, ","); err != nil {
  34. return err
  35. }
  36. }
  37. }
  38. if _, err := fmt.Fprint(w, " WHERE "); err != nil {
  39. return err
  40. }
  41. return b.cond.WriteTo(w)
  42. }
上海开阖软件有限公司 沪ICP备12045867号-1