cond_eq.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 (
  6. "fmt"
  7. "sort"
  8. )
  9. // Incr implements a type used by Eq
  10. type Incr int
  11. // Decr implements a type used by Eq
  12. type Decr int
  13. // Eq defines equals conditions
  14. type Eq map[string]interface{}
  15. var _ Cond = Eq{}
  16. func (eq Eq) opWriteTo(op string, w Writer) error {
  17. var i = 0
  18. for _, k := range eq.sortedKeys() {
  19. v := eq[k]
  20. switch v.(type) {
  21. case []int, []int64, []string, []int32, []int16, []int8, []uint, []uint64, []uint32, []uint16, []interface{}:
  22. if err := In(k, v).WriteTo(w); err != nil {
  23. return err
  24. }
  25. case expr:
  26. if _, err := fmt.Fprintf(w, "%s=(", k); err != nil {
  27. return err
  28. }
  29. if err := v.(expr).WriteTo(w); err != nil {
  30. return err
  31. }
  32. if _, err := fmt.Fprintf(w, ")"); err != nil {
  33. return err
  34. }
  35. case *Builder:
  36. if _, err := fmt.Fprintf(w, "%s=(", k); err != nil {
  37. return err
  38. }
  39. if err := v.(*Builder).WriteTo(w); err != nil {
  40. return err
  41. }
  42. if _, err := fmt.Fprintf(w, ")"); err != nil {
  43. return err
  44. }
  45. case Incr:
  46. if _, err := fmt.Fprintf(w, "%s=%s+?", k, k); err != nil {
  47. return err
  48. }
  49. w.Append(int(v.(Incr)))
  50. case Decr:
  51. if _, err := fmt.Fprintf(w, "%s=%s-?", k, k); err != nil {
  52. return err
  53. }
  54. w.Append(int(v.(Decr)))
  55. default:
  56. if _, err := fmt.Fprintf(w, "%s=?", k); err != nil {
  57. return err
  58. }
  59. w.Append(v)
  60. }
  61. if i != len(eq)-1 {
  62. if _, err := fmt.Fprint(w, op); err != nil {
  63. return err
  64. }
  65. }
  66. i = i + 1
  67. }
  68. return nil
  69. }
  70. // WriteTo writes SQL to Writer
  71. func (eq Eq) WriteTo(w Writer) error {
  72. return eq.opWriteTo(" AND ", w)
  73. }
  74. // And implements And with other conditions
  75. func (eq Eq) And(conds ...Cond) Cond {
  76. return And(eq, And(conds...))
  77. }
  78. // Or implements Or with other conditions
  79. func (eq Eq) Or(conds ...Cond) Cond {
  80. return Or(eq, Or(conds...))
  81. }
  82. // IsValid tests if this Eq is valid
  83. func (eq Eq) IsValid() bool {
  84. return len(eq) > 0
  85. }
  86. // sortedKeys returns all keys of this Eq sorted with sort.Strings.
  87. // It is used internally for consistent ordering when generating
  88. // SQL, see https://github.com/go-xorm/builder/issues/10
  89. func (eq Eq) sortedKeys() []string {
  90. keys := make([]string, 0, len(eq))
  91. for key := range eq {
  92. keys = append(keys, key)
  93. }
  94. sort.Strings(keys)
  95. return keys
  96. }