cond_between.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. // Between implmentes between condition
  7. type Between struct {
  8. Col string
  9. LessVal interface{}
  10. MoreVal interface{}
  11. }
  12. var _ Cond = Between{}
  13. // WriteTo write data to Writer
  14. func (between Between) WriteTo(w Writer) error {
  15. if _, err := fmt.Fprintf(w, "%s BETWEEN ", between.Col); err != nil {
  16. return err
  17. }
  18. if lv, ok := between.LessVal.(expr); ok {
  19. if err := lv.WriteTo(w); err != nil {
  20. return err
  21. }
  22. } else {
  23. if _, err := fmt.Fprint(w, "?"); err != nil {
  24. return err
  25. }
  26. w.Append(between.LessVal)
  27. }
  28. if _, err := fmt.Fprint(w, " AND "); err != nil {
  29. return err
  30. }
  31. if mv, ok := between.MoreVal.(expr); ok {
  32. if err := mv.WriteTo(w); err != nil {
  33. return err
  34. }
  35. } else {
  36. if _, err := fmt.Fprint(w, "?"); err != nil {
  37. return err
  38. }
  39. w.Append(between.MoreVal)
  40. }
  41. return nil
  42. }
  43. // And implments And with other conditions
  44. func (between Between) And(conds ...Cond) Cond {
  45. return And(between, And(conds...))
  46. }
  47. // Or implments Or with other conditions
  48. func (between Between) Or(conds ...Cond) Cond {
  49. return Or(between, Or(conds...))
  50. }
  51. // IsValid tests if the condition is valid
  52. func (between Between) IsValid() bool {
  53. return len(between.Col) > 0
  54. }