cond_not.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // Not defines NOT condition
  7. type Not [1]Cond
  8. var _ Cond = Not{}
  9. // WriteTo writes SQL to Writer
  10. func (not Not) WriteTo(w Writer) error {
  11. if _, err := fmt.Fprint(w, "NOT "); err != nil {
  12. return err
  13. }
  14. switch not[0].(type) {
  15. case condAnd, condOr:
  16. if _, err := fmt.Fprint(w, "("); err != nil {
  17. return err
  18. }
  19. case Eq:
  20. if len(not[0].(Eq)) > 1 {
  21. if _, err := fmt.Fprint(w, "("); err != nil {
  22. return err
  23. }
  24. }
  25. case Neq:
  26. if len(not[0].(Neq)) > 1 {
  27. if _, err := fmt.Fprint(w, "("); err != nil {
  28. return err
  29. }
  30. }
  31. }
  32. if err := not[0].WriteTo(w); err != nil {
  33. return err
  34. }
  35. switch not[0].(type) {
  36. case condAnd, condOr:
  37. if _, err := fmt.Fprint(w, ")"); err != nil {
  38. return err
  39. }
  40. case Eq:
  41. if len(not[0].(Eq)) > 1 {
  42. if _, err := fmt.Fprint(w, ")"); err != nil {
  43. return err
  44. }
  45. }
  46. case Neq:
  47. if len(not[0].(Neq)) > 1 {
  48. if _, err := fmt.Fprint(w, ")"); err != nil {
  49. return err
  50. }
  51. }
  52. }
  53. return nil
  54. }
  55. // And implements And with other conditions
  56. func (not Not) And(conds ...Cond) Cond {
  57. return And(not, And(conds...))
  58. }
  59. // Or implements Or with other conditions
  60. func (not Not) Or(conds ...Cond) Cond {
  61. return Or(not, Or(conds...))
  62. }
  63. // IsValid tests if this condition is valid
  64. func (not Not) IsValid() bool {
  65. return not[0] != nil && not[0].IsValid()
  66. }