cond_neq.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. // Neq defines not equal conditions
  7. type Neq map[string]interface{}
  8. var _ Cond = Neq{}
  9. // WriteTo writes SQL to Writer
  10. func (neq Neq) WriteTo(w Writer) error {
  11. var args = make([]interface{}, 0, len(neq))
  12. var i = 0
  13. for k, v := range neq {
  14. switch v.(type) {
  15. case []int, []int64, []string, []int32, []int16, []int8:
  16. if err := NotIn(k, v).WriteTo(w); err != nil {
  17. return err
  18. }
  19. case expr:
  20. if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
  21. return err
  22. }
  23. if err := v.(expr).WriteTo(w); err != nil {
  24. return err
  25. }
  26. if _, err := fmt.Fprintf(w, ")"); err != nil {
  27. return err
  28. }
  29. case *Builder:
  30. if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
  31. return err
  32. }
  33. if err := v.(*Builder).WriteTo(w); err != nil {
  34. return err
  35. }
  36. if _, err := fmt.Fprintf(w, ")"); err != nil {
  37. return err
  38. }
  39. default:
  40. if _, err := fmt.Fprintf(w, "%s<>?", k); err != nil {
  41. return err
  42. }
  43. args = append(args, v)
  44. }
  45. if i != len(neq)-1 {
  46. if _, err := fmt.Fprint(w, " AND "); err != nil {
  47. return err
  48. }
  49. }
  50. i = i + 1
  51. }
  52. w.Append(args...)
  53. return nil
  54. }
  55. // And implements And with other conditions
  56. func (neq Neq) And(conds ...Cond) Cond {
  57. return And(neq, And(conds...))
  58. }
  59. // Or implements Or with other conditions
  60. func (neq Neq) Or(conds ...Cond) Cond {
  61. return Or(neq, Or(conds...))
  62. }
  63. // IsValid tests if this condition is valid
  64. func (neq Neq) IsValid() bool {
  65. return len(neq) > 0
  66. }