cond_like.go 839 B

123456789101112131415161718192021222324252627282930313233343536
  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 "fmt"
  6. // Like defines like condition
  7. type Like [2]string
  8. var _ Cond = Like{"", ""}
  9. // WriteTo write SQL to Writer
  10. func (like Like) WriteTo(w Writer) error {
  11. if _, err := fmt.Fprintf(w, "%s LIKE ?", like[0]); err != nil {
  12. return err
  13. }
  14. w.Append("%" + like[1] + "%")
  15. return nil
  16. }
  17. // And implements And with other conditions
  18. func (like Like) And(conds ...Cond) Cond {
  19. return And(like, And(conds...))
  20. }
  21. // Or implements Or with other conditions
  22. func (like Like) Or(conds ...Cond) Cond {
  23. return Or(like, Or(conds...))
  24. }
  25. // IsValid tests if this condition is valid
  26. func (like Like) IsValid() bool {
  27. return len(like[0]) > 0 && len(like[1]) > 0
  28. }