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.

69 lines
1.3 KiB

  1. package builder
  2. import "fmt"
  3. type Neq map[string]interface{}
  4. var _ Cond = Neq{}
  5. func (neq Neq) WriteTo(w Writer) error {
  6. var args = make([]interface{}, 0, len(neq))
  7. var i = 0
  8. for k, v := range neq {
  9. switch v.(type) {
  10. case []int, []int64, []string, []int32, []int16, []int8:
  11. if err := NotIn(k, v).WriteTo(w); err != nil {
  12. return err
  13. }
  14. case expr:
  15. if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
  16. return err
  17. }
  18. if err := v.(expr).WriteTo(w); err != nil {
  19. return err
  20. }
  21. if _, err := fmt.Fprintf(w, ")"); err != nil {
  22. return err
  23. }
  24. case *Builder:
  25. if _, err := fmt.Fprintf(w, "%s<>(", k); err != nil {
  26. return err
  27. }
  28. if err := v.(*Builder).WriteTo(w); err != nil {
  29. return err
  30. }
  31. if _, err := fmt.Fprintf(w, ")"); err != nil {
  32. return err
  33. }
  34. default:
  35. if _, err := fmt.Fprintf(w, "%s<>?", k); err != nil {
  36. return err
  37. }
  38. args = append(args, v)
  39. }
  40. if i != len(neq)-1 {
  41. if _, err := fmt.Fprint(w, " AND "); err != nil {
  42. return err
  43. }
  44. }
  45. i = i + 1
  46. }
  47. w.Append(args...)
  48. return nil
  49. }
  50. func (neq Neq) And(conds ...Cond) Cond {
  51. return And(neq, And(conds...))
  52. }
  53. func (neq Neq) Or(conds ...Cond) Cond {
  54. return Or(neq, Or(conds...))
  55. }
  56. func (neq Neq) IsValid() bool {
  57. return len(neq) > 0
  58. }