cond_like.go 1023 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. // FIXME: if use other regular express, this will be failed. but for compitable, keep this
  15. if like[1][0] == '%' || like[1][len(like[1])-1] == '%' {
  16. w.Append(like[1])
  17. } else {
  18. w.Append("%" + like[1] + "%")
  19. }
  20. return nil
  21. }
  22. // And implements And with other conditions
  23. func (like Like) And(conds ...Cond) Cond {
  24. return And(like, And(conds...))
  25. }
  26. // Or implements Or with other conditions
  27. func (like Like) Or(conds ...Cond) Cond {
  28. return Or(like, Or(conds...))
  29. }
  30. // IsValid tests if this condition is valid
  31. func (like Like) IsValid() bool {
  32. return len(like[0]) > 0 && len(like[1]) > 0
  33. }