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.

cond_eq.go 2.4KB

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