本站源代码
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.

66 lines
1.3KB

  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. }
上海开阖软件有限公司 沪ICP备12045867号-1