cond_not.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. }
  20. if err := not[0].WriteTo(w); err != nil {
  21. return err
  22. }
  23. switch not[0].(type) {
  24. case condAnd, condOr:
  25. if _, err := fmt.Fprint(w, ")"); err != nil {
  26. return err
  27. }
  28. }
  29. return nil
  30. }
  31. // And implements And with other conditions
  32. func (not Not) And(conds ...Cond) Cond {
  33. return And(not, And(conds...))
  34. }
  35. // Or implements Or with other conditions
  36. func (not Not) Or(conds ...Cond) Cond {
  37. return Or(not, Or(conds...))
  38. }
  39. // IsValid tests if this condition is valid
  40. func (not Not) IsValid() bool {
  41. return not[0] != nil && not[0].IsValid()
  42. }