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.

78 lines
1.6 KiB

  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. }